Class: Uniword::Wordprocessingml::FontReplacer

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/wordprocessingml/font_replacer.rb

Overview

Replaces one font family with another across a document — Word's Home → Replace → Replace Fonts as an API.

Covers style definitions and defaults, body content (paragraphs, tables, hyperlinks), headers and footers, footnotes, endnotes, comments, and numbering levels. Theme font references (asciiTheme/hAnsiTheme/...) are NOT rewritten — those are controlled by the theme's font scheme (see DocumentRoot#apply_font_scheme).

Examples:

Replace Calibri with Carlito throughout a document

replacer = FontReplacer.new(from: "Calibri", to: "Carlito")
replacer.replace(document)
puts replacer.count # number of rFonts values rewritten

Constant Summary collapse

FONT_ATTRIBUTES =

RunFonts attributes rewritten by font replacement.

%i[ascii h_ansi east_asia cs].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:) ⇒ FontReplacer

Create a replacer.

Parameters:

  • from (String)

    Font family to replace (exact match, Word semantics)

  • to (String)

    Replacement font family



28
29
30
31
32
# File 'lib/uniword/wordprocessingml/font_replacer.rb', line 28

def initialize(from:, to:)
  @from = from.to_s
  @to = to.to_s
  @count = 0
end

Instance Attribute Details

#countInteger (readonly)

Number of rFonts attribute values replaced so far.

Returns:

  • (Integer)

    Replacement count



37
38
39
# File 'lib/uniword/wordprocessingml/font_replacer.rb', line 37

def count
  @count
end

Instance Method Details

#replace(document) ⇒ Integer

Replace @from with @to throughout the document (mutated).

Parameters:

Returns:

  • (Integer)

    Replacement count



43
44
45
46
47
48
49
50
51
# File 'lib/uniword/wordprocessingml/font_replacer.rb', line 43

def replace(document)
  replace_in_styles(document.styles_configuration)
  replace_in_numbering(document.numbering_configuration)
  replace_in_container(document.body)
  part_containers(document).each do |container|
    replace_in_paragraphs(container)
  end
  count
end