Class: Uniword::Themes::ThemeTransformation

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/themes/theme_transformation.rb

Overview

Transforms between FriendlyTheme and WordTheme models.

This transformation is bidirectional:

  • to_word: Themes::Theme → Drawingml::Theme (for document generation)
  • from_word: Drawingml::Theme → Themes::Theme (for extraction/editing)

Architecture:

  • Stateless transformation (no instance state)
  • Follows Transformation pattern from lutaml-model
  • Separates concerns: models own serialization, this class owns conversion

Examples:

Convert friendly theme to Word theme

transformation = ThemeTransformation.new
word_theme = transformation.to_word(friendly_theme)

Extract friendly theme from Word theme

friendly = transformation.from_word(word_theme)

Full roundtrip

word_theme = transformation.to_word(friendly)
extracted = transformation.from_word(word_theme)
friendly.to_yaml == extracted.to_yaml  # => true (semantically)

Constant Summary collapse

COLOR_KEYS =

Standard color keys in a theme

%i[dk1 lt1 dk2 lt2 accent1 accent2 accent3 accent4 accent5 accent6 hlink
fol_hlink].freeze

Instance Method Summary collapse

Instance Method Details

#build_color_scheme(friendly_colors) ⇒ Drawingml::ColorScheme?

Build OOXML ColorScheme from friendly color scheme.

Public converter, used by partial theme edits (Design → Colors style scheme swaps that keep fonts and formats untouched).

Parameters:

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/uniword/themes/theme_transformation.rb', line 62

def build_color_scheme(friendly_colors)
  return nil unless friendly_colors

  Drawingml::ColorScheme.new(
    name: friendly_colors.name,
    dk1: build_color_reference(friendly_colors[:dk1], Drawingml::Dk1Color),
    lt1: build_color_reference(friendly_colors[:lt1], Drawingml::Lt1Color),
    dk2: build_color_reference(friendly_colors[:dk2], Drawingml::Dk2Color),
    lt2: build_color_reference(friendly_colors[:lt2], Drawingml::Lt2Color),
    accent1: build_color_reference(friendly_colors[:accent1], Drawingml::Accent1Color),
    accent2: build_color_reference(friendly_colors[:accent2], Drawingml::Accent2Color),
    accent3: build_color_reference(friendly_colors[:accent3], Drawingml::Accent3Color),
    accent4: build_color_reference(friendly_colors[:accent4], Drawingml::Accent4Color),
    accent5: build_color_reference(friendly_colors[:accent5], Drawingml::Accent5Color),
    accent6: build_color_reference(friendly_colors[:accent6], Drawingml::Accent6Color),
    hlink: build_color_reference(friendly_colors[:hlink], Drawingml::HlinkColor),
    fol_hlink: build_color_reference(friendly_colors[:fol_hlink], Drawingml::FolHlinkColor),
  )
end

#build_font_scheme(friendly_fonts) ⇒ Drawingml::FontScheme?

Build OOXML FontScheme from friendly font scheme.

Public converter, used by partial theme edits (Design → Fonts style scheme swaps that keep colors and formats untouched).

Parameters:

Returns:



89
90
91
92
93
94
95
96
97
# File 'lib/uniword/themes/theme_transformation.rb', line 89

def build_font_scheme(friendly_fonts)
  return nil unless friendly_fonts

  Drawingml::FontScheme.new(
    name: friendly_fonts.name,
    major_font_obj: build_major_font(friendly_fonts),
    minor_font_obj: build_minor_font(friendly_fonts),
  )
end

#default_office_themeDrawingml::Theme

A fresh parse of the bundled default Office theme.

Each call re-parses the bundled XML so every returned theme (and its elements) is independent — callers may mutate it freely.

Returns:



105
106
107
# File 'lib/uniword/themes/theme_transformation.rb', line 105

def default_office_theme
  Drawingml::Theme.from_xml(File.read(OFFICE_THEME_PATH))
end

#from_word(word_theme) ⇒ Themes::Theme

Transform Word theme to friendly theme

Parameters:

Returns:



47
48
49
50
51
52
53
# File 'lib/uniword/themes/theme_transformation.rb', line 47

def from_word(word_theme)
  Theme.new(
    name: word_theme.name,
    color_scheme: extract_color_scheme(word_theme),
    font_scheme: extract_font_scheme(word_theme),
  )
end

#to_word(friendly_theme) ⇒ Drawingml::Theme

Transform friendly theme to Word theme

Parameters:

Returns:



36
37
38
39
40
41
# File 'lib/uniword/themes/theme_transformation.rb', line 36

def to_word(friendly_theme)
  Drawingml::Theme.new(
    name: friendly_theme.name,
    theme_elements: build_theme_elements(friendly_theme),
  )
end