Class: Uniword::Drawingml::Theme
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Uniword::Drawingml::Theme
- Defined in:
- lib/uniword/drawingml/theme.rb
Overview
Represents a document theme containing color and font schemes.
Themes provide a consistent set of colors, fonts, and effects that can be applied throughout a document.
NOTE: Convenience alias Uniword::Theme is available via lib/uniword.rb
Constant Summary collapse
- THEME_COLOR_MAP =
Mapping from OOXML themeColor attribute values to ColorScheme slots OOXML uses text1/background1 in styles, but dk1/lt1 in the theme definition
{ "text1" => :dk1, "text2" => :dk2, "background1" => :lt1, "background2" => :lt2, "accent1" => :accent1, "accent2" => :accent2, "accent3" => :accent3, "accent4" => :accent4, "accent5" => :accent5, "accent6" => :accent6, "hyperlink" => :hlink, "followedHyperlink" => :fol_hlink, }.freeze
Instance Attribute Summary collapse
-
#media_files ⇒ Object
Media files associated with theme (images, etc.) Hash of filename => MediaFile objects.
-
#raw_xml ⇒ Object
Raw XML for perfect round-trip preservation Used when theme structure can’t be fully modeled yet.
-
#source_file ⇒ Object
Source .thmx file path (for reference).
-
#variants ⇒ Object
Theme variants (Hash of variant_id => ThemeVariant).
Class Method Summary collapse
-
.from_thmx(path, variant: nil) ⇒ Theme
Load theme from .thmx file.
Instance Method Summary collapse
-
#apply_to(document) ⇒ void
Apply this theme to a document.
-
#color(color_name) ⇒ String?
Get a theme color by name (accepts both scheme names and OOXML names).
-
#color_scheme ⇒ Object
Get color scheme (compatibility accessor).
-
#color_scheme=(scheme) ⇒ Object
Set color scheme (compatibility accessor).
-
#dup ⇒ Theme
Duplicate the theme.
-
#font_scheme ⇒ Object
Get font scheme (compatibility accessor).
-
#font_scheme=(scheme) ⇒ Object
Set font scheme (compatibility accessor).
-
#initialize(attributes = {}) ⇒ Theme
constructor
Initialize theme.
-
#inspect ⇒ String
Provide detailed inspection for debugging.
-
#major_font ⇒ String?
Get the major font (for headings).
-
#minor_font ⇒ String?
Get the minor font (for body text).
-
#resolve_color(theme_color) ⇒ String?
Resolve an OOXML themeColor value to its RGB hex.
-
#resolve_font(theme_font_ref) ⇒ String?
Resolve an OOXML themeFont reference to its typeface name.
-
#valid? ⇒ Boolean
Check if theme is valid.
Constructor Details
#initialize(attributes = {}) ⇒ Theme
Initialize theme
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/uniword/drawingml/theme.rb', line 96 def initialize(attributes = {}) super @theme_elements ||= ThemeElements.new @object_defaults ||= ObjectDefaults.new @extra_clr_scheme_lst ||= ExtraColorSchemeList.new @ext_lst ||= ExtensionList.new @variants = {} @source_file = nil @media_files ||= {} end |
Instance Attribute Details
#media_files ⇒ Object
Media files associated with theme (images, etc.) Hash of filename => MediaFile objects
87 88 89 |
# File 'lib/uniword/drawingml/theme.rb', line 87 def media_files @media_files end |
#raw_xml ⇒ Object
Raw XML for perfect round-trip preservation Used when theme structure can’t be fully modeled yet
91 92 93 |
# File 'lib/uniword/drawingml/theme.rb', line 91 def raw_xml @raw_xml end |
#source_file ⇒ Object
Source .thmx file path (for reference)
83 84 85 |
# File 'lib/uniword/drawingml/theme.rb', line 83 def source_file @source_file end |
#variants ⇒ Object
Theme variants (Hash of variant_id => ThemeVariant)
80 81 82 |
# File 'lib/uniword/drawingml/theme.rb', line 80 def variants @variants end |
Class Method Details
.from_thmx(path, variant: nil) ⇒ Theme
Load theme from .thmx file
244 245 246 247 248 249 250 251 |
# File 'lib/uniword/drawingml/theme.rb', line 244 def self.from_thmx(path, variant: nil) loader = Uniword::Themes::ThemeLoader.new if variant loader.load_with_variant(path, variant) else loader.load(path) end end |
Instance Method Details
#apply_to(document) ⇒ void
This method returns an undefined value.
Apply this theme to a document
257 258 259 260 |
# File 'lib/uniword/drawingml/theme.rb', line 257 def apply_to(document) applicator = Uniword::Themes::ThemeApplicator.new applicator.apply(self, document) end |
#color(color_name) ⇒ String?
Get a theme color by name (accepts both scheme names and OOXML names)
169 170 171 172 173 |
# File 'lib/uniword/drawingml/theme.rb', line 169 def color(color_name) key = color_name.to_s scheme_key = THEME_COLOR_MAP[key] || key.to_sym color_scheme&.[](scheme_key) end |
#color_scheme ⇒ Object
Get color scheme (compatibility accessor)
108 109 110 |
# File 'lib/uniword/drawingml/theme.rb', line 108 def color_scheme @theme_elements&.clr_scheme end |
#color_scheme=(scheme) ⇒ Object
Set color scheme (compatibility accessor)
113 114 115 116 |
# File 'lib/uniword/drawingml/theme.rb', line 113 def color_scheme=(scheme) @theme_elements ||= ThemeElements.new @theme_elements.clr_scheme = scheme end |
#dup ⇒ Theme
Duplicate the theme
132 133 134 135 136 137 138 139 |
# File 'lib/uniword/drawingml/theme.rb', line 132 def dup new_theme = Theme.new(name: name) new_theme.color_scheme = color_scheme.dup if color_scheme new_theme.font_scheme = font_scheme.dup if font_scheme new_theme.variants = @variants.dup if @variants new_theme.source_file = @source_file new_theme end |
#font_scheme ⇒ Object
Get font scheme (compatibility accessor)
119 120 121 |
# File 'lib/uniword/drawingml/theme.rb', line 119 def font_scheme @theme_elements&.font_scheme end |
#font_scheme=(scheme) ⇒ Object
Set font scheme (compatibility accessor)
124 125 126 127 |
# File 'lib/uniword/drawingml/theme.rb', line 124 def font_scheme=(scheme) @theme_elements ||= ThemeElements.new @theme_elements.font_scheme = scheme end |
#inspect ⇒ String
Provide detailed inspection for debugging
225 226 227 228 229 230 231 |
# File 'lib/uniword/drawingml/theme.rb', line 225 def inspect "#<Uniword::Drawingml::Theme name=#{name.inspect} " \ "colors=#{@color_scheme&.colors&.count || 0} " \ "major_font=#{major_font.inspect} " \ "minor_font=#{minor_font.inspect} " \ "variants=#{@variants&.keys&.count || 0}>" end |
#major_font ⇒ String?
Get the major font (for headings)
211 212 213 |
# File 'lib/uniword/drawingml/theme.rb', line 211 def major_font font_scheme&.major_font end |
#minor_font ⇒ String?
Get the minor font (for body text)
218 219 220 |
# File 'lib/uniword/drawingml/theme.rb', line 218 def minor_font font_scheme&.minor_font end |
#resolve_color(theme_color) ⇒ String?
Resolve an OOXML themeColor value to its RGB hex
179 180 181 182 183 |
# File 'lib/uniword/drawingml/theme.rb', line 179 def resolve_color(theme_color) return nil unless theme_color color(theme_color) end |
#resolve_font(theme_font_ref) ⇒ String?
Resolve an OOXML themeFont reference to its typeface name
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/uniword/drawingml/theme.rb', line 189 def resolve_font(theme_font_ref) return nil unless theme_font_ref && font_scheme case theme_font_ref when "majorAscii", "majorHAnsi" font_scheme.major_font when "majorEastAsia" font_scheme.major_east_asian when "majorBidi" font_scheme.major_complex_script when "minorAscii", "minorHAnsi" font_scheme.minor_font when "minorEastAsia" font_scheme.minor_east_asian when "minorBidi" font_scheme.minor_complex_script end end |
#valid? ⇒ Boolean
Check if theme is valid
144 145 146 |
# File 'lib/uniword/drawingml/theme.rb', line 144 def valid? !name.nil? && !name.empty? end |