File: afm.rb

package info (click to toggle)
ruby-prawn 2.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,380 kB
  • sloc: ruby: 15,820; sh: 43; makefile: 20
file content (288 lines) | stat: -rw-r--r-- 8,709 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# frozen_string_literal: true

# Implements AFM font support for Prawn
#
# Copyright May 2008, Gregory Brown / James Healy.  All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.

require_relative '../encoding'

module Prawn
  module Fonts
    # @private

    class AFM < Font
      class << self
        attr_accessor :hide_m17n_warning
      end

      self.hide_m17n_warning = false

      BUILT_INS = %w[
        Courier Helvetica Times-Roman Symbol ZapfDingbats
        Courier-Bold Courier-Oblique Courier-BoldOblique
        Times-Bold Times-Italic Times-BoldItalic
        Helvetica-Bold Helvetica-Oblique Helvetica-BoldOblique
      ].freeze

      def unicode?
        false
      end

      def self.metrics_path
        @metrics_path ||= if ENV['METRICS']
                            ENV['METRICS'].split(':')
                          else
                            [
                              # use Prawn's font directories (Debian patch)
                              Prawn::DATADIR + '/fonts'
                            ]
                          end
      end

      attr_reader :attributes #:nodoc:

      # parse each ATM font file once only
      def self.font_data
        @font_data ||= SynchronizedCache.new
      end

      def initialize(document, name, options = {}) #:nodoc:
        name ||= options[:family]
        unless BUILT_INS.include?(name)
          raise Prawn::Errors::UnknownFont,
            "#{name} (#{options[:style] || 'normal'}) is not a known font."
        end

        super

        file_name = @name.dup
        file_name << '.afm' unless /\.afm$/.match?(file_name)
        file_name = file_name[0] == '/' ? file_name : find_font(file_name)

        font_data = self.class.font_data[file_name] ||= parse_afm(file_name)
        @glyph_widths = font_data[:glyph_widths]
        @glyph_table = font_data[:glyph_table]
        @bounding_boxes = font_data[:bounding_boxes]
        @kern_pairs = font_data[:kern_pairs]
        @kern_pair_table = font_data[:kern_pair_table]
        @attributes = font_data[:attributes]

        @ascender = @attributes['ascender'].to_i
        @descender = @attributes['descender'].to_i
        @line_gap = Float(bbox[3] - bbox[1]) - (@ascender - @descender)
      end

      # The font bbox, as an array of integers
      #
      def bbox
        @bbox ||= @attributes['fontbbox'].split(/\s+/).map { |e| Integer(e) }
      end

      # NOTE: String *must* be encoded as WinAnsi
      def compute_width_of(string, options = {}) #:nodoc:
        scale = (options[:size] || size) / 1000.0

        if options[:kerning]
          strings, numbers = kern(string).partition { |e| e.is_a?(String) }
          total_kerning_offset = numbers.inject(0.0) { |a, e| a + e }
          (unscaled_width_of(strings.join) - total_kerning_offset) * scale
        else
          unscaled_width_of(string) * scale
        end
      end

      # Returns true if the font has kerning data, false otherwise
      #
      # rubocop: disable Naming/PredicateName
      def has_kerning_data?
        @kern_pairs.any?
      end
      # rubocop: enable Naming/PredicateName

      # built-in fonts only work with winansi encoding, so translate the
      # string. Changes the encoding in-place, so the argument itself
      # is replaced with a string in WinAnsi encoding.
      #
      def normalize_encoding(text)
        text.encode('windows-1252')
      rescue ::Encoding::InvalidByteSequenceError,
             ::Encoding::UndefinedConversionError

        raise Prawn::Errors::IncompatibleStringEncoding,
          "Your document includes text that's not compatible with the " \
          "Windows-1252 character set.\n" \
          'If you need full UTF-8 support, use external fonts instead of ' \
          "PDF's built-in fonts.\n"
      end

      def to_utf8(text)
        text.encode('UTF-8')
      end

      # Returns the number of characters in +str+ (a WinAnsi-encoded string).
      #
      def character_count(str)
        str.length
      end

      # Perform any changes to the string that need to happen
      # before it is rendered to the canvas. Returns an array of
      # subset "chunks", where each chunk is an array of two elements.
      # The first element is the font subset number, and the second
      # is either a string or an array (for kerned text).
      #
      # For Adobe fonts, there is only ever a single subset, so
      # the first element of the array is "0", and the second is
      # the string itself (or an array, if kerning is performed).
      #
      # The +text+ parameter must be in WinAnsi encoding (cp1252).
      #
      def encode_text(text, options = {})
        [[0, options[:kerning] ? kern(text) : text]]
      end

      def glyph_present?(char)
        !normalize_encoding(char).nil?
      rescue Prawn::Errors::IncompatibleStringEncoding
        false
      end

      private

      def register(_subset)
        font_dict = {
          Type: :Font,
          Subtype: :Type1,
          BaseFont: name.to_sym
        }

        # Symbolic AFM fonts (Symbol, ZapfDingbats) have their own encodings
        font_dict[:Encoding] = :WinAnsiEncoding unless symbolic?

        @document.ref!(font_dict)
      end

      def symbolic?
        attributes['characterset'] == 'Special'
      end

      def find_font(file)
        self.class.metrics_path.find { |f| File.exist? "#{f}/#{file}" } +
          "/#{file}"
      rescue NoMethodError
        raise Prawn::Errors::UnknownFont,
          "Couldn't find the font: #{file} in any of:\n" +
          self.class.metrics_path.join("\n")
      end

      def parse_afm(file_name)
        data = {
          glyph_widths: {},
          bounding_boxes: {},
          kern_pairs: {},
          attributes: {}
        }
        section = []

        File.foreach(file_name) do |line|
          case line
          when /^Start(\w+)/
            section.push Regexp.last_match(1)
            next
          when /^End(\w+)/
            section.pop
            next
          end

          case section
          when %w[FontMetrics CharMetrics]
            next unless /^CH?\s/.match?(line)

            name = line[/\bN\s+(\.?\w+)\s*;/, 1]
            data[:glyph_widths][name] = line[/\bWX\s+(\d+)\s*;/, 1].to_i
            data[:bounding_boxes][name] = line[/\bB\s+([^;]+);/, 1].to_s.rstrip
          when %w[FontMetrics KernData KernPairs]
            next unless line =~ /^KPX\s+(\.?\w+)\s+(\.?\w+)\s+(-?\d+)/

            data[:kern_pairs][[Regexp.last_match(1), Regexp.last_match(2)]] =
              Regexp.last_match(3).to_i
          when %w[FontMetrics KernData TrackKern],
            %w[FontMetrics Composites]
            next
          else
            parse_generic_afm_attribute(line, data)
          end
        end

        # process data parsed from AFM file to build tables which
        #   will be used when measuring and kerning text
        data[:glyph_table] = (0..255).map do |i|
          data[:glyph_widths][Encoding::WinAnsi::CHARACTERS[i]].to_i
        end

        character_hash = Hash[
          Encoding::WinAnsi::CHARACTERS.zip(
            (0..Encoding::WinAnsi::CHARACTERS.size).to_a
          )
        ]
        data[:kern_pair_table] =
          data[:kern_pairs].each_with_object({}) do |p, h|
            h[p[0].map { |n| character_hash[n] }] = p[1]
          end

        data.each_value(&:freeze)
        data.freeze
      end

      def parse_generic_afm_attribute(line, hash)
        line =~ /(^\w+)\s+(.*)/
        key = Regexp.last_match(1).to_s.downcase
        value = Regexp.last_match(2)

        hash[:attributes][key] =
          if hash[:attributes][key]
            Array(hash[:attributes][key]) << value
          else
            value
          end
      end

      # converts a string into an array with spacing offsets
      # bewteen characters that need to be kerned
      #
      # String *must* be encoded as WinAnsi
      #
      def kern(string)
        kerned = [[]]
        last_byte = nil

        string.each_byte do |byte|
          k = last_byte && @kern_pair_table[[last_byte, byte]]
          if k
            kerned << -k << [byte]
          else
            kerned.last << byte
          end
          last_byte = byte
        end

        kerned.map do |e|
          e = e.is_a?(Array) ? e.pack('C*') : e
          if e.respond_to?(:force_encoding)
            e.force_encoding(::Encoding::Windows_1252)
          else
            e
          end
        end
      end

      def unscaled_width_of(string)
        string.bytes.inject(0) do |s, r|
          s + @glyph_table[r]
        end
      end
    end
  end
end