Class: HexaPDF::Font::Type1Wrapper
- Inherits:
-
Object
- Object
- HexaPDF::Font::Type1Wrapper
- Defined in:
- lib/hexapdf/font/type1_wrapper.rb
Overview
This class wraps a generic Type1 font object and provides the methods needed for working with the font in a PDF context.
Instance Attribute Summary collapse
-
#pdf_object ⇒ Object
readonly
Returns the PDF object associated with the wrapper.
-
#wrapped_font ⇒ Object
readonly
Returns the wrapped Type1 font object.
Instance Method Summary collapse
-
#decode_utf8(str) ⇒ Object
Returns an array of glyph objects representing the characters in the UTF-8 encoded string.
-
#encode(glyph) ⇒ Object
Encodes the glyph and returns the code string.
-
#font_type ⇒ Object
Returns the type of the font, i.e.
-
#glyph(name) ⇒ Object
Returns a Glyph object for the given glyph name.
-
#initialize(document, font, pdf_object: nil, custom_encoding: false) ⇒ Type1Wrapper
constructor
Creates a new Type1Wrapper object wrapping the Type1 font.
-
#scaling_factor ⇒ Object
Returns 1 since all Type1 fonts use 1000 units for the em-square.
Constructor Details
#initialize(document, font, pdf_object: nil, custom_encoding: false) ⇒ Type1Wrapper
Creates a new Type1Wrapper object wrapping the Type1 font.
The optional argument pdf_object
can be used to set the PDF font object that this wrapper should be associated with. If no object is set, a suitable one is automatically created.
If pdf_object
is provided, the PDF object’s encoding is used. Otherwise, the WinAnsiEncoding or, for ‘Special’ fonts, the font’s internal encoding is used. The optional argument custom_encoding
can be set to true
so that a custom encoding is used (only respected if pdf_object
is not provided).
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 119 def initialize(document, font, pdf_object: nil, custom_encoding: false) @wrapped_font = font @pdf_object = pdf_object || create_pdf_object(document) @missing_glyph_callable = document.config['font.on_missing_glyph'] if pdf_object @encoding = pdf_object.encoding @max_code = 255 # Encoding is not modified elsif custom_encoding @encoding = Encoding::Base.new @encoding.code_to_name[32] = :space @max_code = 32 # 32 = space elsif @wrapped_font.metrics.character_set == 'Special' @encoding = @wrapped_font.encoding @max_code = 255 # Encoding is not modified else @encoding = Encoding.for_name(:WinAnsiEncoding) @max_code = 255 # Encoding is not modified end @zapf_dingbats_opt = {zapf_dingbats: (@wrapped_font.font_name == 'ZapfDingbats')} @name_to_glyph = {} @codepoint_to_glyph = {} @encoded_glyphs = {} end |
Instance Attribute Details
#pdf_object ⇒ Object (readonly)
Returns the PDF object associated with the wrapper.
108 109 110 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 108 def pdf_object @pdf_object end |
#wrapped_font ⇒ Object (readonly)
Returns the wrapped Type1 font object.
105 106 107 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 105 def wrapped_font @wrapped_font end |
Instance Method Details
#decode_utf8(str) ⇒ Object
Returns an array of glyph objects representing the characters in the UTF-8 encoded string.
If a Unicode codepoint is not available as glyph object, it is tried to map the codepoint using the font’s internal encoding. This is useful, for example, for the ZapfDingbats font to use ASCII characters for accessing the glyphs.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 173 def decode_utf8(str) str.codepoints.map! do |c| @codepoint_to_glyph[c] ||= begin name = Encoding::GlyphList.unicode_to_name(+'' << c, **@zapf_dingbats_opt) if @wrapped_font.metrics.character_set == 'Special' && (name == :'.notdef' || !@wrapped_font.metrics.character_metrics.key?(name)) name = @encoding.name(c) end name = +"u" << c.to_s(16).rjust(6, '0') if name == :'.notdef' glyph(name) end end end |
#encode(glyph) ⇒ Object
Encodes the glyph and returns the code string.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 189 def encode(glyph) @encoded_glyphs[glyph.name] ||= begin if glyph.name == @wrapped_font.missing_glyph_id raise HexaPDF::Error, "Glyph for #{glyph.str.inspect} missing" end code = @encoding.code(glyph.name) if code code.chr.freeze elsif @max_code < 255 @max_code += 1 @encoding.code_to_name[@max_code] = glyph.name @max_code.chr.freeze else raise HexaPDF::Error, "Type1 encoding has no codepoint for #{glyph.name}" end end end |
#font_type ⇒ Object
Returns the type of the font, i.e. :Type1.
146 147 148 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 146 def font_type :Type1 end |
#glyph(name) ⇒ Object
Returns a Glyph object for the given glyph name.
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 156 def glyph(name) @name_to_glyph[name] ||= begin str = Encoding::GlyphList.name_to_unicode(name, **@zapf_dingbats_opt) if @wrapped_font.metrics.character_metrics.key?(name) Glyph.new(@wrapped_font, name, str) else @missing_glyph_callable.call(str, font_type, @wrapped_font) end end end |
#scaling_factor ⇒ Object
Returns 1 since all Type1 fonts use 1000 units for the em-square.
151 152 153 |
# File 'lib/hexapdf/font/type1_wrapper.rb', line 151 def scaling_factor 1 end |