File: ttfunk.rb

package info (click to toggle)
ruby-ttfunk 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,472 kB
  • sloc: ruby: 7,954; makefile: 7
file content (332 lines) | stat: -rw-r--r-- 9,958 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# frozen_string_literal: true

require 'stringio'
require 'pathname'

require_relative 'ttfunk/aggregate'
require_relative 'ttfunk/directory'
require_relative 'ttfunk/resource_file'
require_relative 'ttfunk/collection'
require_relative 'ttfunk/ttf_encoder'
require_relative 'ttfunk/encoded_string'
require_relative 'ttfunk/placeholder'
require_relative 'ttfunk/otf_encoder'
require_relative 'ttfunk/sci_form'
require_relative 'ttfunk/bit_field'
require_relative 'ttfunk/bin_utils'
require_relative 'ttfunk/sub_table'
require_relative 'ttfunk/min'
require_relative 'ttfunk/max'
require_relative 'ttfunk/sum'
require_relative 'ttfunk/one_based_array'

# TTFunk is a TrueType and OpenType font library written in pure ruby. It
# supports both parsing and encoding of fonts. Also provides limited font
# subsetting.
#
# It supports a veriety of SFNT-based formats:
# * TrueType fonts (ttf)
# * OpenType fonts (otf), with both TrueType and CFF glyph outlines
# * DFont resources (dfont)
# * TrueType Collections (ttc)
#
# While not all TrueType and OpenType tables are implemented the most common
# ones are.
module TTFunk
  # TTFunk-specific exceptions
  class Error < StandardError; end

  # File represents an individual font. It can represents both TrueType and
  # OpenType fonts.
  class File
    # Raw content of the font.
    # @return [String]
    attr_reader :contents

    # Font tables directory.
    # @return [TTFunk::Directory]
    attr_reader :directory

    # Open font file
    #
    # @overload open(io)
    #   @param io [IO] IO to read font content from. IO position and binmode
    #     might change.
    #   @return [TTFunk::File]
    # @overload open(path)
    #   @param path [String, Pathname] Path to file to read the font from.
    #   @return [TTFunk::File]
    def self.open(io_or_path)
      new(verify_and_read(io_or_path))
    end

    # Load a font from a resource file.
    #
    # @param file [String, Pathname] Path to the resource file.
    # @param which [Integer, String] index or name of the font to load
    # @return [TTFunk::File]]
    def self.from_dfont(file, which = 0)
      new(ResourceFile.open(file) { |dfont| dfont['sfnt', which] })
    end

    # Load a font from a TrueType collection.
    #
    # @overload from_ttc(io, which = 0)
    #   @param file [IO] IO to read the collection from.
    #   @param which [Integer] index of the font to load
    #   @return [TTFunk::File]
    # @overload from_ttc(file_path, which = 0)
    #   @param file_path [String, Pathname] Path to the resource file.
    #   @param which [Integer] index of the font to load
    #   @return [TTFunk::File]
    def self.from_ttc(file, which = 0)
      Collection.open(file) { |ttc| ttc[which] }
    end

    # Turn a path or IO into an IO convenient for TTFunk. The resulting IO is
    # going to be in bin mode and its position set to the beginning.
    #
    # @overload verify_and_open(io)
    #   @param io [IO] IO to prepare. Its position and binmode might
    #     change.
    #   @return [io]
    # @overload verify_and_open(path)
    #   @param path [String, Pathname] path of the file to turn into an IO.
    #   @return [IO] newly opened IO for the path
    # @deprecated This method might retain open files for longer than necessary.
    # @see .verify_and_read
    def self.verify_and_open(io_or_path)
      # File or IO
      if io_or_path.respond_to?(:rewind)
        io = io_or_path
        # Rewind if the object we're passed is an IO, so that multiple embeds of
        # the same IO object will work
        io.rewind
        # read the file as binary so the size is calculated correctly
        # guard binmode because some objects acting io-like don't implement it
        io.binmode if io.respond_to?(:binmode)
        return io
      end
      # String or Pathname
      io_or_path = Pathname.new(io_or_path)
      raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?

      io_or_path.open('rb')
    end

    # Read contents of a path or IO.
    #
    # @overload verify_and_read(io)
    #   @param io [IO] IO to read from. Its position and binmode might
    #     change. IO is read from the beginning regardless of its initial
    #     position.
    #   @return [String]
    # @overload verify_and_read(path)
    #   @param path [String, Pathname] path of the file to read.
    #   @return [String]
    def self.verify_and_read(io_or_path)
      # File or IO
      if io_or_path.respond_to?(:rewind)
        io = io_or_path
        # Rewind if the object we're passed is an IO, so that multiple embeds of
        # the same IO object will work
        io.rewind
        # read the file as binary so the size is calculated correctly
        # guard binmode because some objects acting io-like don't implement it
        io.binmode if io.respond_to?(:binmode)
        return io.read
      end
      # String or Pathname
      io_or_path = Pathname.new(io_or_path)
      raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?

      io_or_path.binread
    end

    # @param contents [String] binary string containg the font data
    # @param offset [Integer] offset at which the font data starts
    def initialize(contents, offset = 0)
      @contents = StringIO.new(contents)
      @directory = Directory.new(@contents, offset)
    end

    # Glyphs ascent as defined for in the font.
    #
    # @return [Integer]
    def ascent
      @ascent ||= (os2.exists? && os2.ascent && os2.ascent.nonzero?) ||
        horizontal_header.ascent
    end

    # Glyphs descent as defined in the font.
    #
    # @return [Integer]
    def descent
      @descent ||= (os2.exists? && os2.descent && os2.descent.nonzero?) ||
        horizontal_header.descent
    end

    # Line gap as defined in the font.
    #
    # @return [Integer]
    def line_gap
      @line_gap ||= (os2.exists? && os2.line_gap && os2.line_gap.nonzero?) ||
        horizontal_header.line_gap
    end

    # Glyps bounding box as defined in the font.
    #
    # @return [Array(Integer, Integer, Integer, Integer)]
    def bbox
      [header.x_min, header.y_min, header.x_max, header.y_max]
    end

    # Font directory entry for the table with the provided tag.
    #
    # @param tag [String] table tab
    # @return [Hash, nil]
    def directory_info(tag)
      directory.tables[tag.to_s]
    end

    # Font Header (`head`) table
    #
    # @return [TTFunk::Table::Head, nil]
    def header
      @header ||= TTFunk::Table::Head.new(self)
    end

    # Character to Glyph Index Mapping (`cmap`) table
    #
    # @return [TTFunk::Tbale::Cmap, nil]
    def cmap
      @cmap ||= TTFunk::Table::Cmap.new(self)
    end

    # Horizontal Header (`hhea`) table
    #
    # @return [TTFunk::Table::Hhea, nil]
    def horizontal_header
      @horizontal_header ||= TTFunk::Table::Hhea.new(self)
    end

    # Horizontal Metrics (`hmtx`) table
    #
    # @return [TTFunk::Table::Hmtx, nil]
    def horizontal_metrics
      @horizontal_metrics ||= TTFunk::Table::Hmtx.new(self)
    end

    # Maximum Profile (`maxp`) table
    #
    # @return [TTFunk::Table::Maxp, nil]
    def maximum_profile
      @maximum_profile ||= TTFunk::Table::Maxp.new(self)
    end

    # Kerning (`kern`) table
    #
    # @return [TTFunk::Table::Kern, nil]
    def kerning
      @kerning ||= TTFunk::Table::Kern.new(self)
    end

    # Naming (`name`) table
    #
    # @return [TTFunk::Table::Name, nil]
    def name
      @name ||= TTFunk::Table::Name.new(self)
    end

    # OS/2 and Windows Metrics (`OS/2`) table
    #
    # @return [TTFunk::Table:OS2, nil]
    def os2
      @os2 ||= TTFunk::Table::OS2.new(self)
    end

    # PostScript (`post`) table
    #
    # @return [TTFunk::Table::Post, nil]
    def postscript
      @postscript ||= TTFunk::Table::Post.new(self)
    end

    # Index to Location (`loca`) table
    #
    # @return [TTFunk::Table::Loca, nil]
    def glyph_locations
      @glyph_locations ||= TTFunk::Table::Loca.new(self)
    end

    # Glyph Data (`glyf`) table
    #
    # @return [TTFunk::Table::Glyf, nil]
    def glyph_outlines
      @glyph_outlines ||= TTFunk::Table::Glyf.new(self)
    end

    # Standard Bitmap Graphics (`sbix`) table
    #
    # @return [TTFunk::Table::Sbix, nil]
    def sbix
      @sbix ||= TTFunk::Table::Sbix.new(self)
    end

    # Compact Font Format (`CFF `) table
    #
    # @return [Table::Table::Cff, nil]
    def cff
      @cff ||= TTFunk::Table::Cff.new(self)
    end

    # Vertical Origin (`VORG`) table
    #
    # @return [TTFunk::Table::Vorg, nil]
    def vertical_origins
      @vertical_origins ||=
        if directory.tables.include?(TTFunk::Table::Vorg::TAG)
          TTFunk::Table::Vorg.new(self)
        end
    end

    # Digital Signature (`DSIG`) table
    #
    # @return [TTFunk::Table::Dsig, nil]
    def digital_signature
      @digital_signature ||=
        if directory.tables.include?(TTFunk::Table::Dsig::TAG)
          TTFunk::Table::Dsig.new(self)
        end
    end

    # Find glyph by its index.
    #
    # @return [TTFunk::Table::Cff::Charstring] if it's a CFF-based OpenType font
    # @return [TTFunk::Table::Glyf::Simple, TTFunk::Table::Glyf::Compound]
    #   if it's a TrueType font
    def find_glyph(glyph_id)
      if cff.exists?
        cff.top_index[0].charstrings_index[glyph_id].glyph
      else
        glyph_outlines.for(glyph_id)
      end
    end
  end
end

require_relative 'ttfunk/table/cff'
require_relative 'ttfunk/table/cmap'
require_relative 'ttfunk/table/dsig'
require_relative 'ttfunk/table/glyf'
require_relative 'ttfunk/table/head'
require_relative 'ttfunk/table/hhea'
require_relative 'ttfunk/table/hmtx'
require_relative 'ttfunk/table/kern'
require_relative 'ttfunk/table/loca'
require_relative 'ttfunk/table/maxp'
require_relative 'ttfunk/table/name'
require_relative 'ttfunk/table/os2'
require_relative 'ttfunk/table/post'
require_relative 'ttfunk/table/sbix'
require_relative 'ttfunk/table/vorg'