Class: TTFunk::Table::Glyf::Compound

Inherits:
Object
  • Object
show all
Includes:
Reader
Defined in:
lib/ttfunk/table/glyf/compound.rb

Defined Under Namespace

Classes: Component

Constant Summary collapse

ARG_1_AND_2_ARE_WORDS =
0x0001
WE_HAVE_A_SCALE =
0x0008
MORE_COMPONENTS =
0x0020
WE_HAVE_AN_X_AND_Y_SCALE =
0x0040
WE_HAVE_A_TWO_BY_TWO =
0x0080
WE_HAVE_INSTRUCTIONS =
0x0100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, x_min, y_min, x_max, y_max) ⇒ Compound

Returns a new instance of Compound.



22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/ttfunk/table/glyf/compound.rb', line 22

def initialize(raw, x_min, y_min, x_max, y_max)
  @raw = raw
  @x_min, @y_min, @x_max, @y_max = x_min, y_min, x_max, y_max

  # Because TTFunk only cares about glyphs insofar as they (1) provide
  # a bounding box for each glyph, and (2) can be rewritten into a
  # font subset, we don't really care about the rest of the glyph data
  # except as a whole. Thus, we don't actually decompose the glyph
  # into it's parts--all we really care about are the locations within
  # the raw string where the component glyph ids are stored, so that
  # when we rewrite this glyph into a subset we can rewrite the
  # component glyph-ids so they are correct for the subset.

  @glyph_ids = []
  @glyph_id_offsets = []
  offset = 10 # 2 bytes for each of num-contours, min x/y, max x/y

  loop do
    flags, glyph_id = @raw[offset, 4].unpack("n*")
    @glyph_ids << glyph_id
    @glyph_id_offsets << offset + 2

    break unless flags & MORE_COMPONENTS != 0
    offset += 4

    if flags & ARG_1_AND_2_ARE_WORDS != 0
      offset += 4
    else
      offset += 2
    end

    if flags & WE_HAVE_A_TWO_BY_TWO != 0
      offset += 8
    elsif flags & WE_HAVE_AN_X_AND_Y_SCALE != 0
      offset += 4
    elsif flags & WE_HAVE_A_SCALE != 0
      offset += 2
    end
  end
end

Instance Attribute Details

#glyph_idsObject (readonly)

Returns the value of attribute glyph_ids.



18
19
20
# File 'lib/ttfunk/table/glyf/compound.rb', line 18

def glyph_ids
  @glyph_ids
end

#rawObject (readonly)

Returns the value of attribute raw.



16
17
18
# File 'lib/ttfunk/table/glyf/compound.rb', line 16

def raw
  @raw
end

#x_maxObject (readonly)

Returns the value of attribute x_max.



17
18
19
# File 'lib/ttfunk/table/glyf/compound.rb', line 17

def x_max
  @x_max
end

#x_minObject (readonly)

Returns the value of attribute x_min.



17
18
19
# File 'lib/ttfunk/table/glyf/compound.rb', line 17

def x_min
  @x_min
end

#y_maxObject (readonly)

Returns the value of attribute y_max.



17
18
19
# File 'lib/ttfunk/table/glyf/compound.rb', line 17

def y_max
  @y_max
end

#y_minObject (readonly)

Returns the value of attribute y_min.



17
18
19
# File 'lib/ttfunk/table/glyf/compound.rb', line 17

def y_min
  @y_min
end

Instance Method Details

#compound?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ttfunk/table/glyf/compound.rb', line 63

def compound?
  true
end

#recode(mapping) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/ttfunk/table/glyf/compound.rb', line 67

def recode(mapping)
  result = @raw.dup
  new_ids = glyph_ids.map { |id| mapping[id] }

  new_ids.zip(@glyph_id_offsets).each do |new_id, offset|
    result[offset, 2] = [new_id].pack("n")
  end

  return result
end