Class: Idml::Render::FontSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/idml/render/font_setup.rb

Overview

Resolves the document font and wraps it in a TextEngine::PdfrbFontMetrics adapter. Encapsulates:

1. Finding the IDML document's first non-missing font file
 via `Pdfrb::FontResolver#find_by_ps_name`.
2. Registering that file with pdfrb's Fonts collection.
3. Wrapping the resulting Symbol resource in
 `PdfrbFontMetrics` for the text engine.

Extracted from Pipeline for SRP — Pipeline orchestrates, this object owns the font-setup dance.

Constant Summary collapse

DEFAULT_FONT =
Render::DEFAULT_FONT
REGULAR_STYLE_NAMES =

Style names treated as the "Regular" default for body text, in priority order. Used to pick the right font from a family when no per-run AppliedFont is specified — matches InDesign's default-text-font behavior.

%w[Regular Normal Book Roman].freeze

Instance Method Summary collapse

Constructor Details

#initialize(package:, font_search_paths: nil) ⇒ FontSetup



25
26
27
28
# File 'lib/idml/render/font_setup.rb', line 25

def initialize(package:, font_search_paths: nil)
  @package = package
  @font_search_paths = font_search_paths
end

Instance Method Details

#font_map(writer) ⇒ Object

Pre-registers every non-missing font in the document and returns a map of family_name → pdfrb Symbol resource. Used by the renderer to resolve per-run AppliedFont references. Falls back to DEFAULT_FONT for families that can't be resolved.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/idml/render/font_setup.rb', line 57

def font_map(writer)
  map = {}
  return map unless @package&.fonts

  @package.fonts.font_family.each do |family|
    next unless family.name

    map[family.name] = resource_for_family(writer, family)
  end
  map
end

#font_resolvable?Boolean

True if at least one non-missing font declared in the document can be located on disk. Lets callers (and tests) gate font-embedding assertions on the document's fonts actually being installed, rather than silently falling back to the default.



74
75
76
# File 'lib/idml/render/font_setup.rb', line 74

def font_resolvable?
  !resolve_document_font_path.nil?
end

#metrics_for(writer, font_resource) ⇒ Object

Wraps a registered font resource in a PdfrbFontMetrics adapter. Returns nil when the resource is the default fallback (no real metrics available).



45
46
47
48
49
50
51
# File 'lib/idml/render/font_setup.rb', line 45

def metrics_for(writer, font_resource)
  return nil if font_resource == DEFAULT_FONT

  TextEngine::PdfrbFontMetrics.new(writer.document.fonts, font_resource)
rescue StandardError
  nil
end

#register(writer) ⇒ Object

Registers the resolved font with writer and returns the Symbol resource. Falls back to Render::DEFAULT_FONT ("Helvetica") when the document has no resolvable font.



33
34
35
36
37
38
39
40
# File 'lib/idml/render/font_setup.rb', line 33

def register(writer)
  path = resolve_document_font_path
  return DEFAULT_FONT unless path

  writer.register_font(path)
rescue StandardError
  DEFAULT_FONT
end

#resource_for_family(writer, family) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/idml/render/font_setup.rb', line 78

def resource_for_family(writer, family)
  path = find_font_file(family)
  return DEFAULT_FONT unless path

  writer.register_font(path)
rescue StandardError
  DEFAULT_FONT
end