Class: Uniword::Resource::ThemeTransition

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/resource/theme_transition.rb

Overview

Detects Microsoft Word built-in themes and transitions them to Uniword equivalents using the color fingerprint mapping.

Examples:

Auto-detect and transition

doc = Uniword::Document.open("ms_report.docx")
result = ThemeTransition.auto_transition!(doc)
# => { uniword_slug: "meridian", ms_name: "Atlas" }

Detect by theme object

slug = ThemeTransition.detect_ms_theme(doc.theme)
# => "meridian"

Detect by name string

slug = ThemeTransition.from_ms_name("Atlas")
# => "meridian"

Defined Under Namespace

Classes: TransitionResult

Constant Summary collapse

FINGERPRINT_KEYS =

Color keys used for fingerprint matching. These 3 colors are sufficient to uniquely identify each MS theme.

%w[accent1 accent2 dk2].freeze

Class Method Summary collapse

Class Method Details

.apply_uniword_theme(document, uniword_slug) ⇒ void

This method returns an undefined value.

Apply a specific Uniword theme to a document

Parameters:



86
87
88
89
90
# File 'lib/uniword/resource/theme_transition.rb', line 86

def self.apply_uniword_theme(document, uniword_slug)
  friendly = Themes::Theme.load(uniword_slug)
  word_theme = Themes::ThemeTransformation.new.to_word(friendly)
  Themes::ThemeApplicator.new.apply(word_theme, document)
end

.auto_transition!(document) ⇒ TransitionResult?

Auto-transition: detect MS theme in document, apply Uniword equivalent

Parameters:

Returns:



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

def self.auto_transition!(document)
  word_theme = document.theme
  return nil unless word_theme

  # Try detection by color fingerprint
  uniword_slug = detect_ms_theme(word_theme)

  # Fall back to detection by theme name
  uniword_slug = from_ms_name(word_theme.name) if uniword_slug.nil? && word_theme.respond_to?(:name) && word_theme.name

  return nil unless uniword_slug

  ms_name = ThemeMappingLoader.uniword_to_ms_theme(uniword_slug)

  # Apply the Uniword theme
  apply_uniword_theme(document, uniword_slug)

  TransitionResult.new(uniword_slug: uniword_slug, ms_name: ms_name)
end

.detect_ms_theme(drawingml_theme) ⇒ String?

Detect which Uniword theme corresponds to the MS theme in a Drawingml::Theme

Parameters:

Returns:

  • (String, nil)

    Uniword theme slug or nil if no match



32
33
34
35
36
37
38
39
# File 'lib/uniword/resource/theme_transition.rb', line 32

def self.detect_ms_theme(drawingml_theme)
  return nil unless drawingml_theme

  colors = extract_colors(drawingml_theme)
  return nil unless colors

  ThemeMappingLoader.find_by_colors(colors)
end

.extract_colors(drawingml_theme) ⇒ Hash?

Extract color values from a Drawingml::Theme’s color scheme

Parameters:

Returns:

  • (Hash, nil)

    Hash with string keys and hex values



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/uniword/resource/theme_transition.rb', line 96

def self.extract_colors(drawingml_theme)
  cs = drawingml_theme.theme_elements&.clr_scheme
  return nil unless cs

  {
    "dk1" => extract_hex(cs, :dk1),
    "lt1" => extract_hex(cs, :lt1),
    "dk2" => extract_hex(cs, :dk2),
    "lt2" => extract_hex(cs, :lt2),
    "accent1" => extract_hex(cs, :accent1),
    "accent2" => extract_hex(cs, :accent2),
    "accent3" => extract_hex(cs, :accent3),
    "accent4" => extract_hex(cs, :accent4),
    "accent5" => extract_hex(cs, :accent5),
    "accent6" => extract_hex(cs, :accent6),
  }
end

.extract_hex(color_scheme, key) ⇒ String?

Extract hex value from a color scheme color slot

Parameters:

Returns:

  • (String, nil)

    Hex color value



119
120
121
# File 'lib/uniword/resource/theme_transition.rb', line 119

def self.extract_hex(color_scheme, key)
  color_scheme[key]
end

.from_ms_name(ms_name) ⇒ String?

Look up Uniword theme by MS theme name string

Parameters:

  • ms_name (String)

    MS theme display name (e.g., “Atlas”)

Returns:

  • (String, nil)

    Uniword theme slug or nil



45
46
47
# File 'lib/uniword/resource/theme_transition.rb', line 45

def self.from_ms_name(ms_name)
  ThemeMappingLoader.ms_to_uniword_theme(ms_name)
end

.styleset_from_ms_name(ms_name) ⇒ String?

Look up Uniword styleset by MS styleset name string

Parameters:

  • ms_name (String)

    MS styleset display name (e.g., “Distinctive”)

Returns:

  • (String, nil)

    Uniword styleset slug or nil



53
54
55
# File 'lib/uniword/resource/theme_transition.rb', line 53

def self.styleset_from_ms_name(ms_name)
  ThemeMappingLoader.ms_to_uniword_styleset(ms_name)
end