Class: TTFunk::Table::Cff::Charstring

Inherits:
Object
  • Object
show all
Defined in:
lib/ttfunk/table/cff/charstring.rb

Overview

CFF Charstring.

Constant Summary collapse

CODE_MAP =

Type 2 charstring operators

{
  1 => :hstem,
  3 => :vstem,
  4 => :vmoveto,
  5 => :rlineto,
  6 => :hlineto,
  7 => :vlineto,
  8 => :rrcurveto,
  10 => :callsubr,
  12 => :flex_select,
  14 => :endchar,
  18 => :hstemhm,
  19 => :hintmask,
  20 => :cntrmask,
  21 => :rmoveto,
  22 => :hmoveto,
  23 => :vstemhm,
  24 => :rcurveline,
  25 => :rlinecurve,
  26 => :vvcurveto,
  27 => :hhcurveto,
  28 => :shortint,
  29 => :callgsubr,
  30 => :vhcurveto,
  31 => :hvcurveto,
}.freeze
FLEX_CODE_MAP =

Type 2 Flex operators.

{
  35 => :flex,
  34 => :hflex,
  36 => :hflex1,
  37 => :flex1,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glyph_id, top_dict, font_dict, raw) ⇒ Charstring

Returns a new instance of Charstring.

Parameters:

  • glyph_id (Integer)
  • top_dict (TTFunk::Table:Cff::TopDict)
  • font_dict (TTFunk::Table:Cff::FontDict)
  • raw (String)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ttfunk/table/cff/charstring.rb', line 56

def initialize(glyph_id, top_dict, font_dict, raw)
  @glyph_id = glyph_id
  @top_dict = top_dict
  @font_dict = font_dict
  @raw = raw

  default_width_x = (@font_dict || @top_dict).private_dict.default_width_x

  @nominal_width_x = (@font_dict || @top_dict).private_dict.nominal_width_x

  @subrs = (@font_dict || @top_dict).private_dict.subr_index
  @gsubrs = @top_dict.cff.global_subr_index
  @subrs_bias = @subrs.bias if @subrs
  @gsubrs_bias = @gsubrs.bias if @gsubrs

  @stack = []
  @data = raw.bytes
  @index = 0
  @n_stems = 0
  @have_width = false
  @open = false
  @width = default_width_x
  @x = 0
  @y = 0
end

Instance Attribute Details

#glyph_idInteger (readonly)

Glyph ID.

Returns:

  • (Integer)


46
47
48
# File 'lib/ttfunk/table/cff/charstring.rb', line 46

def glyph_id
  @glyph_id
end

#rawString (readonly)

Encoded charstring.

Returns:

  • (String)


50
51
52
# File 'lib/ttfunk/table/cff/charstring.rb', line 50

def raw
  @raw
end

Instance Method Details

#glyphTTFunk::Table::Glyf::PathBased

Get a TrueType-compatible glyph representation of this charstring.



96
97
98
99
100
101
102
# File 'lib/ttfunk/table/cff/charstring.rb', line 96

def glyph
  @glyph ||=
    begin
      horizontal_metrics = @top_dict.file.horizontal_metrics.for(glyph_id)
      Glyf::PathBased.new(path, horizontal_metrics)
    end
end

#pathTTFunk::Table::Cff::Path

Get path representation of this charstring.



85
86
87
88
89
90
91
# File 'lib/ttfunk/table/cff/charstring.rb', line 85

def path
  @path || begin
    @path = Path.new
    parse!
    @path
  end
end

#render(x: 0, y: 0, font_size: 72) ⇒ TTFunk::Table::Cff::Path

Get path representation of this charstring at the specified font size.

Parameters:

  • x (Integer, Float) (defaults to: 0)

    new horizontal position.

  • y (Integer, Float) (defaults to: 0)

    new vertical position.

  • font_size (Integer, Float) (defaults to: 72)

    font size.

Returns:



110
111
112
113
114
115
116
117
# File 'lib/ttfunk/table/cff/charstring.rb', line 110

def render(x: 0, y: 0, font_size: 72)
  path.render(
    x: x,
    y: y,
    font_size: font_size,
    units_per_em: @top_dict.file.header.units_per_em,
  )
end