Class: Pdfrb::FontLoader::Type3

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/font_loader/type3.rb

Overview

Type3 font loader. Type3 fonts define glyphs as PDF drawing procedures (content streams) rather than outlines. Used for custom symbol sets and pixel-art fonts.

Each glyph is a (name, width, procedure) triple where the procedure is a content stream drawing in a 1000x1000 glyph grid. The loader emits a /Type /Font /Subtype /Type3 font dict with /CharProcs mapping glyph names to streams.

Defined Under Namespace

Classes: GlyphSpec

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document:, glyphs:, bounding_box:, matrix: nil) ⇒ Type3

Returns a new instance of Type3.

Parameters:

  • document (Pdfrb::Document)

    the document to embed in.

  • glyphs (Array<GlyphSpec>)

    glyph definitions.

  • bounding_box (Array<Numeric>)

    4-number BBox.

  • matrix (Array<Numeric>, nil) (defaults to: nil)

    6-number FontMatrix (default: [0.001 0 0 0.001 0 0]).



23
24
25
26
27
28
# File 'lib/pdfrb/font_loader/type3.rb', line 23

def initialize(document:, glyphs:, bounding_box:, matrix: nil)
  @document = document
  @glyphs = glyphs
  @bounding_box = bounding_box
  @matrix = matrix || [0.001, 0, 0, 0.001, 0, 0]
end

Instance Attribute Details

#bounding_boxObject (readonly)

Returns the value of attribute bounding_box.



16
17
18
# File 'lib/pdfrb/font_loader/type3.rb', line 16

def bounding_box
  @bounding_box
end

#documentObject (readonly)

Returns the value of attribute document.



16
17
18
# File 'lib/pdfrb/font_loader/type3.rb', line 16

def document
  @document
end

#glyphsObject (readonly)

Returns the value of attribute glyphs.



16
17
18
# File 'lib/pdfrb/font_loader/type3.rb', line 16

def glyphs
  @glyphs
end

#matrixObject (readonly)

Returns the value of attribute matrix.



16
17
18
# File 'lib/pdfrb/font_loader/type3.rb', line 16

def matrix
  @matrix
end

Instance Method Details

#buildObject

Build and return the Type3 font dictionary. Each glyph procedure becomes an indirect stream object referenced from /CharProcs.



33
34
35
36
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
# File 'lib/pdfrb/font_loader/type3.rb', line 33

def build
  char_procs = {}
  widths = []
  encoding = { Type: :Encoding, Differences: [] }

  glyphs.each_with_index do |glyph, i|
    code = 32 + i
    stream = document.add({ Length: glyph.procedure.bytesize },
                          type: Pdfrb::Model::Cos::Stream)
    stream.stream = glyph.procedure
    char_procs[glyph.name.to_sym] = Pdfrb::Model::Reference.new(stream.oid, stream.gen)
    widths[code] = glyph.width
    encoding[:Differences] << code << glyph.name.to_sym
  end

  document.add(
    {
      Type: :Font,
      Subtype: :Type3,
      FontBBox: bounding_box,
      FontMatrix: matrix,
      CharProcs: char_procs,
      Encoding: encoding,
      FirstChar: 32,
      LastChar: 31 + glyphs.length,
      Widths: widths,
    },
    type: Pdfrb::Model::Type::Font
  )
end