Class: Uniword::Builder::ThemeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/builder/theme_builder.rb

Overview

Builds and applies themes to documents.

Themes control the overall look of a document through color schemes, font schemes, and format schemes.

Examples:

Apply a bundled theme

doc.theme('atlas')

Apply and customize a theme

doc.theme('office') do |t|
  t.colors(accent1: 'FF0000', accent2: '00FF00')
  t.fonts(major: 'Georgia', minor: 'Calibri')
end

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ ThemeBuilder

Returns a new instance of ThemeBuilder.



19
20
21
# File 'lib/uniword/builder/theme_builder.rb', line 19

def initialize(document)
  @document = document
end

Instance Method Details

#apply(name) ⇒ self

Apply a named bundled theme

Bundled themes are YAML files in data/themes/. Available themes: Themes::Theme.available_themes

Parameters:

  • name (String)

    Theme name (e.g., ‘atlas’, ‘badge’, ‘office’)

Returns:

  • (self)


30
31
32
33
34
35
# File 'lib/uniword/builder/theme_builder.rb', line 30

def apply(name)
  friendly = Themes::Theme.load(name)
  @document.model.theme =
    Themes::ThemeTransformation.new.to_word(friendly)
  self
end

#apply_file(path, variant: nil) ⇒ self

Apply a theme from a .thmx file

Parameters:

  • path (String)

    Path to .thmx theme file

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

    Theme variant

Returns:

  • (self)


42
43
44
45
46
# File 'lib/uniword/builder/theme_builder.rb', line 42

def apply_file(path, variant: nil)
  @document.model.theme =
    Drawingml::Theme.from_thmx(path, variant: variant)
  self
end

#availableArray<String>

List available bundled themes

Returns:

  • (Array<String>)

    Theme names



93
94
95
# File 'lib/uniword/builder/theme_builder.rb', line 93

def available
  Themes::Theme.available_themes
end

#color(slot) ⇒ String?

Get a theme color value

Parameters:

  • slot (Symbol, String)

    Color slot (e.g., :accent1, :dk1)

Returns:

  • (String, nil)

    Hex color value



86
87
88
# File 'lib/uniword/builder/theme_builder.rb', line 86

def color(slot)
  @document.model.theme&.color(slot.to_sym)
end

#colors(**overrides) ⇒ self

Override theme colors

Parameters:

  • overrides (Hash)

    Color slot overrides (e.g., accent1: ‘FF0000’)

Returns:

  • (self)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/uniword/builder/theme_builder.rb', line 52

def colors(**overrides)
  theme = @document.model.theme
  return self unless theme

  scheme = theme.theme_elements&.clr_scheme
  return self unless scheme

  overrides.each do |slot, color|
    scheme[slot.to_sym] = color
  end
  self
end

#fonts(major: nil, minor: nil) ⇒ self

Override theme fonts

Parameters:

  • major (String, nil) (defaults to: nil)

    Major font (headings)

  • minor (String, nil) (defaults to: nil)

    Minor font (body)

Returns:

  • (self)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/uniword/builder/theme_builder.rb', line 70

def fonts(major: nil, minor: nil)
  theme = @document.model.theme
  return self unless theme

  fs = theme.theme_elements&.font_scheme
  return self unless fs

  fs.major_font = major if major
  fs.minor_font = minor if minor
  self
end