Class: HexaPDF::Font::Type1Wrapper

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(document, font, custom_encoding: false) ⇒ Type1Wrapper

Creates a new object wrapping the Type1 font for the PDF document.

The optional argument custom_encoding can be set to true so that a custom encoding instead of the WinAnsiEncoding is used.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hexapdf/font/type1_wrapper.rb', line 83

def initialize(document, font, custom_encoding: false)
  @document = document
  @wrapped_font = font

  @dict = build_font_dict
  @document.register_listener(:complete_objects, &method(:complete_font_dict))
  if @wrapped_font.metrics.character_set == 'Special' || custom_encoding
    @encoding = Encoding::Base.new
    @encoding.code_to_name[32] = :space
    @max_code = 32 # 32 = space
  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

#dictObject (readonly)

The PDF font dictionary representing the wrapped font.



77
78
79
# File 'lib/hexapdf/font/type1_wrapper.rb', line 77

def dict
  @dict
end

#wrapped_fontObject (readonly)

Returns the wrapped Type1 font object.



74
75
76
# File 'lib/hexapdf/font/type1_wrapper.rb', line 74

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.



116
117
118
119
120
121
122
123
124
125
# File 'lib/hexapdf/font/type1_wrapper.rb', line 116

def decode_utf8(str)
  str.each_codepoint.map do |c|
    @codepoint_to_glyph[c] ||=
      begin
        name = Encoding::GlyphList.unicode_to_name('' << c, @zapf_dingbats_opt)
        name = '' << c if name == :'.notdef'
        glyph(name)
      end
  end
end

#encode(glyph) ⇒ Object

Encodes the glyph and returns the code string.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hexapdf/font/type1_wrapper.rb', line 128

def encode(glyph)
  @encoded_glyphs[glyph.name] ||=
    begin
      code = @encoding.code_to_name.key(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

#glyph(name) ⇒ Object

Returns a Glyph object for the given glyph name.



105
106
107
108
109
110
111
112
113
# File 'lib/hexapdf/font/type1_wrapper.rb', line 105

def glyph(name)
  @name_to_glyph[name] ||=
    begin
      unless @wrapped_font.metrics.character_metrics.key?(name)
        name = @document.config['font.on_missing_glyph'].call(name, @wrapped_font)
      end
      Glyph.new(@wrapped_font, name)
    end
end