Module: Pdfrb::Font::TrueType::Wrapper

Defined in:
lib/pdfrb/font/true_type/wrapper.rb

Class Method Summary collapse

Class Method Details

.build_tounicode(document, _ttf) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pdfrb/font/true_type/wrapper.rb', line 77

def build_tounicode(document, _ttf)
  table = Pdfrb::Font::Encoding::WinAnsiEncoding::TABLE
  mapping = {}
  table.each_with_index { |cp, byte| mapping[byte] = cp if cp }

  cmap_data = Pdfrb::Font::CMap::Writer.write(mapping)
  stream = document.add(
    { Length: cmap_data.bytesize },
    type: Pdfrb::Model::Cos::Stream
  )
  stream.stream = cmap_data
  stream
end

.build_widths(ttf) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pdfrb/font/true_type/wrapper.rb', line 56

def build_widths(ttf)
  widths = Array.new(256, 500)
  table = Pdfrb::Font::Encoding::WinAnsiEncoding::TABLE
  Maxp.new(ttf.maxp_table)

  table.each_with_index do |cp, byte|
    next unless cp

    begin
      gid = ttf.cmap.glyph_id_for(cp)
      next unless gid&.positive?

      w = ttf.hmtx.advance_width(gid)
      widths[byte] = w if w&.positive?
    rescue StandardError
      next
    end
  end
  widths
end

.create_font_descriptor(document, ttf, ps_name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pdfrb/font/true_type/wrapper.rb', line 37

def create_font_descriptor(document, ttf, ps_name)
  head = ttf.head
  os2 = ttf.os2
  document.add(
    {
      Type: :FontDescriptor,
      FontName: ps_name.to_sym,
      Flags: 32,
      FontBBox: head&.bbox || [0, 0, 1000, 1000],
      ItalicAngle: 0,
      Ascent: os2&.s_typo_ascender || ttf.hhea&.ascender || 800,
      Descent: os2&.s_typo_descender || ttf.hhea&.descender || -200,
      CapHeight: os2&.s_cap_height || 700,
      StemV: 80,
    },
    type: Pdfrb::Model::Cos::Dictionary
  )
end

.embed(document, ttf_data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pdfrb/font/true_type/wrapper.rb', line 9

def embed(document, ttf_data, **)
  ttf = File.new(ttf_data)
  name = ttf.name_table_parsed
  ps_name = name&.ps_name || "EmbeddedFont"

  fd = create_font_descriptor(document, ttf, ps_name)
  widths = build_widths(ttf)
  tu_stream = build_tounicode(document, ttf)
  font_file = embed_font_file(document, ttf_data)

  fd.value[:FontFile2] = Pdfrb::Model::Reference.new(font_file.oid, font_file.gen)

  document.add(
    {
      Type: :Font,
      Subtype: :TrueType,
      BaseFont: ps_name.to_sym,
      FirstChar: 0,
      LastChar: 255,
      Widths: widths,
      FontDescriptor: Pdfrb::Model::Reference.new(fd.oid, fd.gen),
      ToUnicode: Pdfrb::Model::Reference.new(tu_stream.oid, tu_stream.gen),
      Encoding: :WinAnsiEncoding,
    },
    type: Pdfrb::Model::Type::FontTrueType
  )
end

.embed_font_file(document, ttf_data) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/pdfrb/font/true_type/wrapper.rb', line 91

def embed_font_file(document, ttf_data)
  stream = document.add(
    { Length: ttf_data.bytesize },
    type: Pdfrb::Model::Cos::Stream
  )
  stream.stream = ttf_data
  stream
end