Class: Acrofill::Fonts

Inherits:
Object
  • Object
show all
Defined in:
lib/acrofill/fonts.rb

Overview

The form's /DR /Font dictionary: the metrics a widget's /DA font resource actually implies, and the reference to put in a generated appearance's /Resources.

Real templates embed their own faces, and those font dictionaries carry their own /Widths and /FontDescriptor. The appearance stream is drawn with that very font, so it has to be measured with it too — laying an embedded face out against standard-14 tables misplaces every centered or right-aligned value. pdftk reads the dictionary; these are the same rules, measured against pdftk-java 3.3.3 (see benchmark/geometry_diff.rb):

widths     /Widths + /FirstChar, else the standard-14 table
ascender   /FontDescriptor /Ascent, else standard-14 AFM, else 800
FontBBox   /FontDescriptor /FontBBox, else standard-14 AFM, else 900/-200
codes      /Encoding /Differences remap the value's bytes

The standard-14 fallbacks apply only to a BaseFont that literally names one of the fourteen; a face merely resembling one (ArialMT and friends) gets pdftk's generic defaults vertically, while its widths still fall back to the closest standard table, there being nothing better to measure with.

Constant Summary collapse

DEFAULT_ASCENDER =
800
DEFAULT_DESCENDER =
-200
DEFAULT_BBOX_TOP =
900
DEFAULT_BBOX_BOTTOM =
-200
# A code outside /FirstChar../LastChar draws as zero-width: pdftk does
# not consult /MissingWidth (verified — setting it changes nothing).
OUT_OF_RANGE_WIDTH =

A code outside /FirstChar../LastChar draws as zero-width: pdftk does not consult /MissingWidth (verified — setting it changes nothing).

0
SUBSET_PREFIX =
/\A[A-Z]{6}\+/

Instance Method Summary collapse

Constructor Details

#initialize(doc, acroform) ⇒ Fonts

Returns a new instance of Fonts.



34
35
36
37
38
39
# File 'lib/acrofill/fonts.rb', line 34

def initialize(doc, acroform)
  @doc = doc
  @acroform = acroform
  @metrics = {}
  @references = {}
end

Instance Method Details

#metrics(resource_name) ⇒ Object

Metrics::Font for the font a /DA string names.



42
43
44
45
# File 'lib/acrofill/fonts.rb', line 42

def metrics(resource_name)
  key = resource_name.to_sym
  @metrics[key] ||= build(@doc.deref(dr_fonts[key]))
end

#reference(resource_name) ⇒ Object

Indirect reference to that font, for the appearance /Resources. Fonts absent from /DR share one registered Helvetica; a /DR font stored as a direct dictionary is promoted to an indirect object once, not once per widget.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/acrofill/fonts.rb', line 51

def reference(resource_name)
  key = resource_name.to_sym
  @references[key] ||=
    begin
      found = dr_fonts[key]
      # #metrics already degrades to standard Helvetica for a /DR entry
      # that is not a font dictionary; writing that same entry into the
      # appearance's /Resources would point /Tf at a non-font object (or
      # at a dangling reference the Writer serializes as `null`), so the
      # two have to agree on what counts as usable.
      @doc.deref(found).is_a?(Hash) ? @doc.ref_for(found) : fallback
    end
end