Class: Fontisan::Type1::TTFToType1Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/fontisan/type1/ttf_to_type1_converter.rb

Overview

TTF to Type 1 CharString Converter

[‘TTFToType1Converter`](lib/fontisan/type1/ttf_to_type1_converter.rb) converts TrueType glyphs to Type 1 CharStrings.

The conversion involves:

  • Converting quadratic curves (TrueType) to cubic curves (Type 1)

  • Scaling coordinates if needed

  • Generating Type 1 CharString commands

Examples:

Convert TTF font to Type 1 CharStrings

scaler = Fontisan::Type1::UPMScaler.type1_standard(font)
converter = Fontisan::Type1::TTFToType1Converter.new(font, scaler, encoding)
charstrings = converter.convert

See Also:

Constant Summary collapse

HSTEM =

Type 1 CharString command codes

1
VSTEM =
3
VMOVETO =
4
RLINETO =
5
HLINETO =
6
VLINETO =
7
RRCURVETO =
8
CLOSEPATH =
9
CALLSUBR =
10
RETURN =
11
ESCAPE =
12
HSBW =
13
ENDCHAR =
14
RMOVETO =
21
HMOVETO =
22
VHCURVETO =
30
HVCURVETO =
31

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font, scaler, encoding) ⇒ TTFToType1Converter

Returns a new instance of TTFToType1Converter.



51
52
53
54
55
56
# File 'lib/fontisan/type1/ttf_to_type1_converter.rb', line 51

def initialize(font, scaler, encoding)
  @font = font
  @scaler = scaler
  @encoding = encoding
  @charstrings = {}
end

Class Method Details

.convert(font, scaler, encoding) ⇒ Hash<Integer, String>

Convert TTF font to Type 1 CharStrings

Parameters:

  • font (Fontisan::Font)

    Source TTF font

  • scaler (UPMScaler)

    UPM scaler

  • encoding (Class)

    Encoding class

Returns:

  • (Hash<Integer, String>)

    Glyph ID to CharString mapping



47
48
49
# File 'lib/fontisan/type1/ttf_to_type1_converter.rb', line 47

def self.convert(font, scaler, encoding)
  new(font, scaler, encoding).convert
end

Instance Method Details

#convertHash<Integer, String>

Convert all glyphs to CharStrings

Returns:

  • (Hash<Integer, String>)

    Glyph ID to CharString mapping



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fontisan/type1/ttf_to_type1_converter.rb', line 61

def convert
  glyf_table = @font.table(Constants::GLYF_TAG)
  return {} unless glyf_table

  loca_table = @font.table(Constants::LOCA_TAG)
  head_table = @font.table(Constants::HEAD_TAG)

  maxp = @font.table(Constants::MAXP_TAG)
  num_glyphs = maxp&.num_glyphs || 0

  num_glyphs.times do |gid|
    @charstrings[gid] =
      convert_glyph(glyf_table, loca_table, head_table, gid)
  end

  @charstrings
end