Class: Fontisan::Type1::CharStrings
- Inherits:
-
Object
- Object
- Fontisan::Type1::CharStrings
- Defined in:
- lib/fontisan/type1/charstrings.rb
Overview
Type 1 CharStrings parser
CharStrings parses and stores
the CharStrings dictionary from a Type 1 font, which contains
glyph outline descriptions.
CharStrings in Type 1 fonts use a stack-based language with commands for drawing curves, lines, and composite glyphs (via the seac operator).
Defined Under Namespace
Classes: CharStringParser
Instance Attribute Summary collapse
-
#charstrings ⇒ Hash
readonly
Glyph name to CharString data mapping.
-
#private_dict ⇒ PrivateDict
readonly
Private dictionary for decryption.
Class Method Summary collapse
-
.from_hash(charstrings_hash, encoding: nil, private_dict: nil) ⇒ CharStrings
Build a CharStrings from a pre-built glyph-name → charstring-bytecode hash.
-
.parse(data, private_dict = nil) ⇒ CharStrings
Parse CharStrings dictionary from decrypted Type 1 font data.
Instance Method Summary collapse
-
#[](glyph_name) ⇒ String?
Get CharString data for glyph.
-
#charstring(glyph_name) ⇒ String?
Alias for #[].
-
#components_for(glyph_name) ⇒ Hash?
Get components for composite glyph.
-
#composite?(glyph_name) ⇒ Boolean
Check if glyph is composite (uses seac).
-
#count ⇒ Integer
Get the number of charstrings.
-
#each_charstring {|glyph_name, charstring_data| ... } ⇒ Enumerator
Iterate over all charstrings.
-
#encoding ⇒ Hash
Get encoding map.
-
#glyph_names ⇒ Array<String>
Get list of glyph names.
-
#has_glyph?(glyph_name) ⇒ Boolean
Check if glyph exists.
-
#initialize(private_dict = nil) ⇒ CharStrings
constructor
Initialize a new CharStrings parser.
-
#outline_for(glyph_name) ⇒ Array
Get outline for glyph by name.
-
#parse(data) ⇒ CharStrings
Parse CharStrings dictionary from decrypted Type 1 font data.
-
#register(glyph_name, charstring_data) ⇒ Object
Register (or replace) a glyph's charstring data.
-
#set_encoding(encoding) ⇒ Object
Override the default Adobe Standard Encoding with a custom
char_code → glyph_namemap.
Constructor Details
#initialize(private_dict = nil) ⇒ CharStrings
Initialize a new CharStrings parser
42 43 44 45 46 |
# File 'lib/fontisan/type1/charstrings.rb', line 42 def initialize(private_dict = nil) @private_dict = private_dict || PrivateDict.new @charstrings = {} @encoding_override = nil end |
Instance Attribute Details
#charstrings ⇒ Hash (readonly)
Returns Glyph name to CharString data mapping.
21 22 23 |
# File 'lib/fontisan/type1/charstrings.rb', line 21 def charstrings @charstrings end |
#private_dict ⇒ PrivateDict (readonly)
Returns Private dictionary for decryption.
24 25 26 |
# File 'lib/fontisan/type1/charstrings.rb', line 24 def private_dict @private_dict end |
Class Method Details
.from_hash(charstrings_hash, encoding: nil, private_dict: nil) ⇒ CharStrings
Build a CharStrings from a pre-built glyph-name → charstring-bytecode
hash. Useful for constructing test fixtures without parsing a full
Type 1 font. Optionally accepts encoding: to override the default
Adobe Standard Encoding (char code → glyph name).
57 58 59 60 61 62 |
# File 'lib/fontisan/type1/charstrings.rb', line 57 def self.from_hash(charstrings_hash, encoding: nil, private_dict: nil) cs = new(private_dict) charstrings_hash.each { |name, data| cs.register(name, data) } cs.set_encoding(encoding) if encoding cs end |
.parse(data, private_dict = nil) ⇒ CharStrings
Parse CharStrings dictionary from decrypted Type 1 font data
35 36 37 |
# File 'lib/fontisan/type1/charstrings.rb', line 35 def self.parse(data, private_dict = nil) new(private_dict).parse(data) end |
Instance Method Details
#[](glyph_name) ⇒ String?
Get CharString data for glyph
133 134 135 |
# File 'lib/fontisan/type1/charstrings.rb', line 133 def [](glyph_name) @charstrings[glyph_name] end |
#charstring(glyph_name) ⇒ String?
Alias for #[]
141 142 143 |
# File 'lib/fontisan/type1/charstrings.rb', line 141 def charstring(glyph_name) @charstrings[glyph_name] end |
#components_for(glyph_name) ⇒ Hash?
Get components for composite glyph
174 175 176 177 178 179 180 181 182 |
# File 'lib/fontisan/type1/charstrings.rb', line 174 def components_for(glyph_name) return nil unless composite?(glyph_name) charstring = @charstrings[glyph_name] parser = CharStringParser.new(@private_dict) parser.parse(charstring) parser.seac_components end |
#composite?(glyph_name) ⇒ Boolean
Check if glyph is composite (uses seac)
163 164 165 166 167 168 |
# File 'lib/fontisan/type1/charstrings.rb', line 163 def composite?(glyph_name) charstring = @charstrings[glyph_name] return false if charstring.nil? charstring.include?(SEAC_OPCODE) end |
#count ⇒ Integer
Get the number of charstrings
100 101 102 |
# File 'lib/fontisan/type1/charstrings.rb', line 100 def count @charstrings.size end |
#each_charstring {|glyph_name, charstring_data| ... } ⇒ Enumerator
Iterate over all charstrings
115 116 117 118 119 |
# File 'lib/fontisan/type1/charstrings.rb', line 115 def each_charstring(&) return enum_for(:each_charstring) unless block_given? @charstrings.each(&) end |
#encoding ⇒ Hash
Get encoding map
107 108 109 |
# File 'lib/fontisan/type1/charstrings.rb', line 107 def encoding @encoding_override || (@encoding ||= build_standard_encoding) end |
#glyph_names ⇒ Array<String>
Get list of glyph names
93 94 95 |
# File 'lib/fontisan/type1/charstrings.rb', line 93 def glyph_names @charstrings.keys end |
#has_glyph?(glyph_name) ⇒ Boolean
Check if glyph exists
125 126 127 |
# File 'lib/fontisan/type1/charstrings.rb', line 125 def has_glyph?(glyph_name) @charstrings.key?(glyph_name) end |
#outline_for(glyph_name) ⇒ Array
Get outline for glyph by name
Parses the CharString and returns outline commands.
151 152 153 154 155 156 157 |
# File 'lib/fontisan/type1/charstrings.rb', line 151 def outline_for(glyph_name) charstring = @charstrings[glyph_name] return nil if charstring.nil? parser = CharStringParser.new(@private_dict) parser.parse(charstring) end |
#parse(data) ⇒ CharStrings
Parse CharStrings dictionary from decrypted Type 1 font data
84 85 86 87 88 |
# File 'lib/fontisan/type1/charstrings.rb', line 84 def parse(data) extract_charstrings(data) decrypt_charstrings self end |
#register(glyph_name, charstring_data) ⇒ Object
Register (or replace) a glyph's charstring data.
68 69 70 |
# File 'lib/fontisan/type1/charstrings.rb', line 68 def register(glyph_name, charstring_data) @charstrings[glyph_name] = charstring_data end |
#set_encoding(encoding) ⇒ Object
Override the default Adobe Standard Encoding with a custom
char_code → glyph_name map.
76 77 78 |
# File 'lib/fontisan/type1/charstrings.rb', line 76 def set_encoding(encoding) @encoding_override = encoding end |