Class: Arrolio::Font::Embedder

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/font/embedder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, font_path, base_font_name: nil) ⇒ Embedder

Returns a new instance of Embedder.



10
11
12
13
14
15
16
17
18
# File 'lib/arrolio/font/embedder.rb', line 10

def initialize(document, font_path, base_font_name: nil)
  @document = document
  @font_path = font_path
  @base_font_name = base_font_name || File.basename(font_path, '.*')
  @fontisan_font = Fontisan::FontLoader.load(font_path)
  @unicode_to_gid = @fontisan_font.table('cmap').unicode_mappings.transform_keys(&:to_i)
  @subset_cp_to_gid = nil
  @subset_gid_to_cp = nil
end

Instance Attribute Details

#base_font_nameObject (readonly)

Returns the value of attribute base_font_name.



8
9
10
# File 'lib/arrolio/font/embedder.rb', line 8

def base_font_name
  @base_font_name
end

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/arrolio/font/embedder.rb', line 8

def document
  @document
end

#font_pathObject (readonly)

Returns the value of attribute font_path.



8
9
10
# File 'lib/arrolio/font/embedder.rb', line 8

def font_path
  @font_path
end

Instance Method Details

#embed(codepoints) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/arrolio/font/embedder.rb', line 37

def embed(codepoints)
  sub = subset(codepoints)
  bytes = sub[:bytes]
  extract_subset_cmap(bytes)
  stream = @document.add(
    { Length: bytes.bytesize, Length1: bytes.bytesize },
    type: Pdfrb::Model::Cos::Stream
  )
  stream.stream = bytes
  font_file2 = Pdfrb::Model::Reference.new(stream.oid, stream.gen)
  descriptor = @document.add(
    { Type: :FontDescriptor, FontName: @base_font_name.to_sym,
      Flags: 32, FontBBox: font_bbox, ItalicAngle: 0,
      Ascent: ascent, Descent: descent, CapHeight: cap_height,
      StemV: 80, FontFile2: font_file2 },
    type: Pdfrb::Model::Type::FontDescriptor
  )
  cid_font = @document.add(
    { Type: :Font, Subtype: :CIDFontType2,
      BaseFont: @base_font_name.to_sym,
      CIDSystemInfo: { Registry: 'Adobe', Ordering: 'Identity', Supplement: 0 },
      CIDToGIDMap: :Identity,
      FontDescriptor: Pdfrb::Model::Reference.new(descriptor.oid, descriptor.gen),
      W: widths_array },
    type: Pdfrb::Model::Type::CIDFont
  )
  tounicode_data = build_tounicode_stream
  tounicode = @document.add(
    { Length: tounicode_data.bytesize },
    type: Pdfrb::Model::Cos::Stream
  )
  tounicode.stream = tounicode_data
  type0 = @document.add(
    { Type: :Font, Subtype: :Type0,
      BaseFont: @base_font_name.to_sym,
      Encoding: :'Identity-H',
      DescendantFonts: [Pdfrb::Model::Reference.new(cid_font.oid, cid_font.gen)],
      ToUnicode: Pdfrb::Model::Reference.new(tounicode.oid, tounicode.gen) },
    type: Pdfrb::Model::Type::FontType0
  )
  Pdfrb::Model::Reference.new(type0.oid, type0.gen)
end

#glyph_id_for_codepoint(codepoint) ⇒ Object



20
21
22
# File 'lib/arrolio/font/embedder.rb', line 20

def glyph_id_for_codepoint(codepoint)
  @unicode_to_gid[codepoint.to_i] || 0
end

#subset(codepoints) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/arrolio/font/embedder.rb', line 29

def subset(codepoints)
  gids = codepoints.map { |cp| glyph_id_for_codepoint(cp) }.uniq.sort
  gids << 0
  options = Fontisan::Subset::Options.new(profile: 'pdf')
  builder = Fontisan::Subset::Builder.new(@fontisan_font, gids, options)
  { bytes: builder.build, gid_map: builder.mapping }
end

#subset_gid_for_codepoint(codepoint) ⇒ Object



24
25
26
27
# File 'lib/arrolio/font/embedder.rb', line 24

def subset_gid_for_codepoint(codepoint)
  return glyph_id_for_codepoint(codepoint) unless @subset_cp_to_gid
  @subset_cp_to_gid[codepoint.to_i] || 0
end