Class: Uniword::Drawingml::Theme

Inherits:
Lutaml::Model::Serializable
  • Object
show all
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

Examples:

Create a custom theme

theme = Uniword::Drawingml::Theme.new(name: 'Corporate')
theme.color_scheme.colors[:accent1] = '0066CC'
theme.font_scheme.major_font = 'Helvetica'

Apply theme to document

doc = Uniword::Wordprocessingml::DocumentRoot.new
doc.theme = theme

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Theme

Initialize theme

Parameters:

  • attributes (Hash) (defaults to: {})

    Theme attributes



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_filesObject

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_xmlObject

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_fileObject

Source .thmx file path (for reference)



83
84
85
# File 'lib/uniword/drawingml/theme.rb', line 83

def source_file
  @source_file
end

#variantsObject

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

Examples:

Load theme

theme = Theme.from_thmx('Atlas.thmx')

Load with variant

theme = Theme.from_thmx('Atlas.thmx', variant: 2)

Parameters:

  • path (String)

    Path to .thmx file

  • variant (String, Integer, nil) (defaults to: nil)

    Optional variant to apply

Returns:

  • (Theme)

    Loaded theme



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

Parameters:

  • document (Document)

    Target 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)

Parameters:

  • color_name (String, Symbol)

    The color name

Returns:

  • (String, nil)

    The RGB hex color value



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_schemeObject

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

#dupTheme

Duplicate the theme

Returns:

  • (Theme)

    A deep copy of this 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_schemeObject

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

#inspectString

Provide detailed inspection for debugging

Returns:

  • (String)

    A readable representation of the theme



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_fontString?

Get the major font (for headings)

Returns:

  • (String, nil)

    The major font name



211
212
213
# File 'lib/uniword/drawingml/theme.rb', line 211

def major_font
  font_scheme&.major_font
end

#minor_fontString?

Get the minor font (for body text)

Returns:

  • (String, nil)

    The minor font name



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

Parameters:

  • theme_color (String)

    OOXML themeColor value (e.g., “text1”, “accent1”)

Returns:

  • (String, nil)

    The RGB hex color value



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

Parameters:

  • theme_font_ref (String)

    OOXML themeFont value (e.g., “majorAscii”, “minorHAnsi”)

Returns:

  • (String, nil)

    The 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

Returns:

  • (Boolean)

    true if valid



144
145
146
# File 'lib/uniword/drawingml/theme.rb', line 144

def valid?
  !name.nil? && !name.empty?
end