glyph_imager 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +17 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/glyph_imager.gemspec +90 -0
  8. data/lib/glyph_imager.rb +102 -0
  9. data/test/fonts/DejaVuSerif.ttf +0 -0
  10. data/test/helper.rb +10 -0
  11. data/test/test_glyph_imager.rb +62 -0
  12. data/vendor/graphics_utf +157 -0
  13. data/vendor/ttf-ruby-0.1/AUTHORS +1 -0
  14. data/vendor/ttf-ruby-0.1/COPYING +340 -0
  15. data/vendor/ttf-ruby-0.1/README +49 -0
  16. data/vendor/ttf-ruby-0.1/TODO +23 -0
  17. data/vendor/ttf-ruby-0.1/VERSION +1 -0
  18. data/vendor/ttf-ruby-0.1/lib/ttf/datatypes.rb +189 -0
  19. data/vendor/ttf-ruby-0.1/lib/ttf/encodings.rb +140 -0
  20. data/vendor/ttf-ruby-0.1/lib/ttf/exceptions.rb +28 -0
  21. data/vendor/ttf-ruby-0.1/lib/ttf/font_loader.rb +290 -0
  22. data/vendor/ttf-ruby-0.1/lib/ttf/fontchunk.rb +77 -0
  23. data/vendor/ttf-ruby-0.1/lib/ttf/table/cmap.rb +408 -0
  24. data/vendor/ttf-ruby-0.1/lib/ttf/table/cvt.rb +49 -0
  25. data/vendor/ttf-ruby-0.1/lib/ttf/table/fpgm.rb +48 -0
  26. data/vendor/ttf-ruby-0.1/lib/ttf/table/gasp.rb +88 -0
  27. data/vendor/ttf-ruby-0.1/lib/ttf/table/glyf.rb +452 -0
  28. data/vendor/ttf-ruby-0.1/lib/ttf/table/head.rb +86 -0
  29. data/vendor/ttf-ruby-0.1/lib/ttf/table/hhea.rb +96 -0
  30. data/vendor/ttf-ruby-0.1/lib/ttf/table/hmtx.rb +98 -0
  31. data/vendor/ttf-ruby-0.1/lib/ttf/table/kern.rb +186 -0
  32. data/vendor/ttf-ruby-0.1/lib/ttf/table/loca.rb +75 -0
  33. data/vendor/ttf-ruby-0.1/lib/ttf/table/maxp.rb +81 -0
  34. data/vendor/ttf-ruby-0.1/lib/ttf/table/name.rb +222 -0
  35. data/vendor/ttf-ruby-0.1/lib/ttf/table/os2.rb +172 -0
  36. data/vendor/ttf-ruby-0.1/lib/ttf/table/post.rb +120 -0
  37. data/vendor/ttf-ruby-0.1/lib/ttf/table/prep.rb +27 -0
  38. data/vendor/ttf-ruby-0.1/lib/ttf/table/vhea.rb +45 -0
  39. data/vendor/ttf-ruby-0.1/lib/ttf/table/vmtx.rb +36 -0
  40. data/vendor/ttf-ruby-0.1/lib/ttf.rb +20 -0
  41. data/vendor/ttf-ruby-0.1/setup.rb +1558 -0
  42. data/vendor/ttf-ruby-0.1/tools/README +44 -0
  43. data/vendor/ttf-ruby-0.1/tools/ttfcairoglyphviewer +229 -0
  44. data/vendor/ttf-ruby-0.1/tools/ttfdump +396 -0
  45. data/vendor/ttf-ruby-0.1/tools/ttfglyph2svg +144 -0
  46. data/vendor/ttf-ruby-0.1/tools/ttfsubset +273 -0
  47. metadata +120 -0
@@ -0,0 +1,140 @@
1
+ # TTF/Ruby, a library to read and write TrueType fonts in Ruby.
2
+ # Copyright (C) 2006 Mathieu Blondel
3
+ #
4
+ # This program is free software; you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation; either version 2 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Font
19
+ module TTF
20
+ module Encodings
21
+
22
+ module Platform
23
+ UNICODE = 0
24
+ MACINTOSH = 1
25
+ ISO = 2
26
+ MICROSOFT = 3
27
+
28
+ ID2NAME = {
29
+ UNICODE => "Unicode",
30
+ MACINTOSH => "Macintosh",
31
+ ISO => "ISO",
32
+ MICROSOFT => "Microsoft"
33
+ }
34
+ end
35
+
36
+ module UnicodeEncoding
37
+ # When Platform == 0
38
+ DEFAULT_SEMANTICS = 0
39
+ VERSION_1_1_SEMANTICS = 1
40
+ ISO_10646_1993_SEMANTICS = 2
41
+ UNICODE = 3
42
+ end
43
+
44
+ module MicrosoftEncoding
45
+ # When Platform == 3
46
+ SYMBOL = 0
47
+ UNICODE = 1
48
+ SHIFTJIS = 2
49
+ BIG5 = 3
50
+ PRC = 4
51
+ WASUNG = 5
52
+ JOHAB = 6
53
+
54
+ ID2NAME = {
55
+ SYMBOL => "Symbol",
56
+ UNICODE => "Unicode",
57
+ SHIFTJIS => "ShiftJIS",
58
+ BIG5 => "Big5",
59
+ PRC => "PRC",
60
+ WASUNG => "Wasung",
61
+ JOHAB => "Johab"
62
+ }
63
+ end
64
+
65
+ module MacintoshEncoding
66
+ # When Platform == 1
67
+ ROMAN = 0
68
+ JAPANESE = 1
69
+ TRADITIONAL_CHINESE = 2
70
+ KOREAN = 3
71
+ ARABIC = 4
72
+ HEBREW = 5
73
+ GREEK = 6
74
+ RUSSIAN = 7
75
+ RSYMBOL = 8
76
+ DEVANAGARI = 9
77
+ GURMUKHI = 10
78
+ GUJARATI = 11
79
+ ORIYA = 12
80
+ BENGALI = 13
81
+ TAMIL = 14
82
+ TELUGU = 15
83
+ KANNADA = 16
84
+ MALAYALAM = 17
85
+ SINHALESE = 18
86
+ BURMESE = 19
87
+ KHMER = 20
88
+ THAI = 21
89
+ LAOTIAN = 22
90
+ GEORGIAN = 23
91
+ ARMENIAN = 24
92
+ SIMPLIFIED_CHINESE = 25
93
+ TIBETAN = 26
94
+ MONGOLIAN = 27
95
+ GEEZ = 28
96
+ SLAVIC = 29
97
+ VIETNAMESE = 30
98
+ SINDHI = 31
99
+ UNINTERPRETED = 32
100
+
101
+ ID2NAME = {
102
+ ROMAN => "Roman",
103
+ JAPANESE => "Japanese",
104
+ TRADITIONAL_CHINESE => "Traditional Chinese",
105
+ KOREAN => "Korean",
106
+ ARABIC => "Arabic",
107
+ HEBREW => "Hebrew",
108
+ GREEK => "Greek",
109
+ RUSSIAN => "Russian",
110
+ RSYMBOL => "RSymbol",
111
+ DEVANAGARI => "Devanagari",
112
+ GURMUKHI => "Gurmukhi",
113
+ GUJARATI => "Gujarati",
114
+ ORIYA => "Orya",
115
+ BENGALI => "Bengali",
116
+ TAMIL => "Tamil",
117
+ TELUGU => "Telugu",
118
+ KANNADA => "Kannada",
119
+ MALAYALAM => "Malayalam",
120
+ SINHALESE => "Sinhalese",
121
+ BURMESE => "Burmese",
122
+ KHMER => "Khmer",
123
+ THAI => "Thai",
124
+ LAOTIAN => "Laotian",
125
+ GEORGIAN => "Georgian",
126
+ ARMENIAN => "Armenian",
127
+ SIMPLIFIED_CHINESE => "Simplified Chinese",
128
+ TIBETAN => "Tibetan",
129
+ MONGOLIAN => "Mongolian",
130
+ GEEZ => "Geez",
131
+ SLAVIC => "Slavic",
132
+ VIETNAMESE => "Vietnamese",
133
+ SINDHI => "Sindhi",
134
+ UNINTERPRETED => "Uninterpreted"
135
+ }
136
+ end
137
+
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,28 @@
1
+ # TTF/Ruby, a library to read and write TrueType fonts in Ruby.
2
+ # Copyright (C) 2006 Mathieu Blondel
3
+ #
4
+ # This program is free software; you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation; either version 2 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Font
19
+ module TTF
20
+
21
+ class MalformedFont < Exception
22
+ end
23
+
24
+ class TableMissing < Exception
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,290 @@
1
+ # TTF/Ruby, a library to read and write TrueType fonts in Ruby.
2
+ # Copyright (C) 2006 Mathieu Blondel
3
+ #
4
+ # This program is free software; you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation; either version 2 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ require 'ttf/datatypes'
19
+ require 'ttf/exceptions'
20
+ require 'ttf/fontchunk'
21
+ require 'ttf/encodings'
22
+
23
+ require 'ttf/table/cmap'
24
+ require 'ttf/table/cvt'
25
+ require 'ttf/table/fpgm'
26
+ require 'ttf/table/gasp'
27
+ require 'ttf/table/glyf'
28
+ require 'ttf/table/head'
29
+ require 'ttf/table/hhea'
30
+ require 'ttf/table/hmtx'
31
+ require 'ttf/table/kern'
32
+ require 'ttf/table/loca'
33
+ require 'ttf/table/maxp'
34
+ require 'ttf/table/name'
35
+ require 'ttf/table/os2'
36
+ require 'ttf/table/post'
37
+ require 'ttf/table/prep'
38
+ require 'ttf/table/vhea'
39
+ require 'ttf/table/vmtx'
40
+
41
+ module Font
42
+ module TTF
43
+ # TTF/Ruby is a library to read and write TrueType fonts in Ruby.
44
+ #
45
+ # Author:: Mathieu Blondel
46
+ # Copyright:: Copyright (c) 2006 Mathieu Blondel
47
+ # License:: GPL
48
+ #
49
+ # This Font::TTF::File class is TTF/Ruby's main class and is a subclass
50
+ # of Ruby's File class. Here is some sample code:
51
+ #
52
+ # require 'ttf'
53
+ #
54
+ # font = Font::TTF::File.new("copy.ttf")
55
+ #
56
+ # cmap_tbl = font.get_table(:cmap)
57
+ # enc_tbl4 = cmap_tbl.encoding_tables.find { |t| t.format == 4 }
58
+ # m_unicode = "m".unpack("U")[0]
59
+ # glyph_id = enc_tbl4.charmaps[m_unicode]
60
+ #
61
+ # loca_tbl = font.get_table(:loca)
62
+ # glyph_offset = loca_tbl.glyph_offsets[glyph_id]
63
+ #
64
+ # glyf_tbl = font.get_table(:glyf)
65
+ # glyph = glyf_tbl.get_glyph_at_offset(glyph_offset)
66
+ # unless glyph.composite?
67
+ # glyph.abs_coordinates.each { |x, y| puts x, y }
68
+ # end
69
+ #
70
+ # Here are the tables and their associated Symbol:
71
+ #
72
+ # * :cmap => Font::TTF::Table::Cmap
73
+ # * :cvt => Font::TTF::Table::Cvt
74
+ # * :fpgm => Font::TTF::Table::Fpgm
75
+ # * :gasp => Font::TTF::Table::Gasp
76
+ # * :glyf => Font::TTF::Table::Glyf
77
+ # * :head => Font::TTF::Table::Head
78
+ # * :hhea => Font::TTF::Table::Hhea
79
+ # * :hmtx => Font::TTF::Table::Hmtx
80
+ # * :kern => Font::TTF::Table::Kern
81
+ # * :loca => Font::TTF::Table::Loca
82
+ # * :maxp => Font::TTF::Table::Maxp
83
+ # * :name => Font::TTF::Table::Name
84
+ # * :"OS/2" => Font::TTF::Table::OS2
85
+ # * :post => Font::TTF::Table::Post
86
+ # * :prep => Font::TTF::Table::Prep
87
+ # * :vhea => Font::TTF::Table::Vhea
88
+ # * :vmtx => Font::TTF::Table::Vmtx
89
+ #
90
+ # Of course, you may modify attributes and generate a new font file.
91
+ #
92
+ # require "ttf"
93
+ #
94
+ # font = Font::TTF::File.new("file.ttf", "w")
95
+ # name_tbl = font.get_table(:name)
96
+ # nr = name_tbl.name_records[0]
97
+ # nr.utf8_str = "blablabla"
98
+ # font.write(font.dump)
99
+ #
100
+ class FontLoader < File
101
+
102
+ TABLES = {:cmap => Font::TTF::Table::Cmap,
103
+ :cvt => Font::TTF::Table::Cvt,
104
+ :fpgm => Font::TTF::Table::Fpgm,
105
+ :gasp => Font::TTF::Table::Gasp,
106
+ :glyf => Font::TTF::Table::Glyf,
107
+ :head => Font::TTF::Table::Head,
108
+ :hhea => Font::TTF::Table::Hhea,
109
+ :hmtx => Font::TTF::Table::Hmtx,
110
+ :kern => Font::TTF::Table::Kern,
111
+ :loca => Font::TTF::Table::Loca,
112
+ :maxp => Font::TTF::Table::Maxp,
113
+ :name => Font::TTF::Table::Name,
114
+ :"OS/2" => Font::TTF::Table::OS2,
115
+ :post => Font::TTF::Table::Post,
116
+ :prep => Font::TTF::Table::Prep,
117
+ :vhea => Font::TTF::Table::Vhea,
118
+ :vmtx => Font::TTF::Table::Vmtx}
119
+
120
+ DIR_ENTRY_SIZE = 4 * IO::SIZEOF_ULONG
121
+
122
+ attr_reader :filename, :version, :search_range,
123
+ :entry_selector, :range_shift, :table_list, :tables_infos
124
+
125
+ attr_writer :version, :search_range, :entry_selector, :range_shift
126
+
127
+ # Font::TTF::File being a subclass of Ruby's File class, you may
128
+ # create new objects with the same parameters as Ruby's File class.
129
+ #
130
+ # But you may also create new objects without parameters in case you want
131
+ # to create a font from scratch and you don't need to write it to a file.
132
+ def initialize(*args)
133
+ if args.length == 0
134
+ @filename = nil
135
+ @version = 0x00010000 # 1.0
136
+ else
137
+ super(*args)
138
+ @filename = args[0]
139
+ end
140
+
141
+ @table_list = [] # ordered list
142
+ @tables = {} # Tables are kept in this arr so we don't
143
+ # have to create new objects everytime
144
+ @tables_infos = {} # infos about tables present in file
145
+
146
+ if not @filename.nil? and FileTest.exists? @filename
147
+ begin
148
+ at_offset(0) do
149
+ @version = read_fixed
150
+ table_num = read_ushort
151
+ @search_range = read_ushort
152
+ @entry_selector = read_ushort
153
+ @range_shift = read_ushort
154
+
155
+ table_num.times do
156
+ tag = read_ulong_as_text.strip.to_sym
157
+ @table_list << tag
158
+ @tables_infos[tag] = {:checksum => read_ulong,
159
+ :offset => read_ulong,
160
+ :len => read_ulong}
161
+ end
162
+ end
163
+ rescue
164
+ raise MalformedFont
165
+ end
166
+ end
167
+ end
168
+
169
+ # Returns table associated with tag tbl_tag. It may return one of
170
+ # Font::TTF::Table::* object or a Font::TTF::FontChunk object if the
171
+ # table is not implemented yet by ttf-ruby.
172
+ #
173
+ # The table returned is kept internally so that every future call to
174
+ # get_table with the same tbl_tag will return the same object.
175
+ def get_table(tbl_tag)
176
+ tbli = @tables_infos[tbl_tag]
177
+
178
+ if @tables.include? tbl_tag
179
+ @tables[tbl_tag]
180
+ elsif tbli.nil?
181
+ raise TableMissing, "Table #{tbl_tag.to_s} neither exists " + \
182
+ "in file nor was defined by user!"
183
+
184
+ elsif TABLES.include? tbl_tag
185
+ @tables[tbl_tag] = \
186
+ TABLES[tbl_tag].new(self, tbli[:offset], tbli[:len])
187
+ else
188
+ Font::TTF::FontChunk.new(self, tbli[:offset], tbli[:len])
189
+ end
190
+ end
191
+
192
+ # Returns whether table with tag tbl_tag is already in font or not.
193
+ def tables_include?(tbl_tag)
194
+ @table_list.include? tbl_tag
195
+ end
196
+
197
+ # Gets a new empty table that then may be set with set_table.
198
+ # tbl_tag is a Symbol.
199
+ def get_new_table(tbl_tag)
200
+ if TABLES.include? tbl_tag
201
+ TABLES[tbl_tag].new(self)
202
+ else
203
+ Font::TTF::FontChunk.new(self)
204
+ end
205
+ end
206
+
207
+ # Updates some member variables that change everytime a table is added
208
+ # to the font.
209
+ def table_list_update!
210
+ @table_list.sort!
211
+
212
+ res = 1
213
+ power = 0
214
+ while res <= @table_list.length
215
+ res *= 2
216
+ power += 1
217
+ end
218
+ @search_range = res * 16
219
+ @entry_selector = power - 1
220
+ @range_shift = @table_list.length * 16 - @search_range
221
+ end
222
+ private :table_list_update!
223
+
224
+ # Adds tbl to font. tbl is a table object
225
+ # (e.g. an instance of Font::TTF::Table::Loca).
226
+ def set_table(tbl)
227
+ tbl_tag = tbl.tag
228
+
229
+ unless tables_include? tbl_tag
230
+ @table_list << tbl_tag
231
+ table_list_update!
232
+ end
233
+ @tables[tbl_tag] = tbl
234
+ end
235
+
236
+ # Removes tbl from font. tbl may be a Symbol (e.g. :loca for the loca table)
237
+ # or a table object (e.g. an instance of Font::TTF::Table::Loca).
238
+ def unset_table(tbl)
239
+ if tbl.kind_of? Symbol
240
+ tbl_tag = tbl
241
+ else
242
+ tbl_tag = tbl.tag
243
+ end
244
+
245
+ @table_list.delete(tbl_tag)
246
+ @tables.delete(tbl_tag)
247
+ table_list_update!
248
+ end
249
+
250
+ # Dumps the whole font in binary raw format as found in a font file.
251
+ def dump
252
+ raw = ""
253
+ raw += (@version || 0).to_fixed
254
+ raw += (@table_list || []).length.to_ushort
255
+ raw += (@search_range || 0).to_ushort
256
+ raw += (@entry_selector || 0).to_ushort
257
+ raw += (@range_shift || 0).to_ushort
258
+
259
+
260
+ offs = IO::SIZEOF_FIXED + 4 * IO::SIZEOF_USHORT + \
261
+ @table_list.length * DIR_ENTRY_SIZE
262
+
263
+ dumps = []
264
+ @table_list.each do |tbl_tag|
265
+ tbl = get_table(tbl_tag)
266
+ tag = tbl_tag.to_s
267
+ tag.four_chars!
268
+ raw += tag
269
+ # FIXME: FontChunk#checksum method is buggy
270
+ # For now, I set it to 0
271
+ raw += 0.to_ulong
272
+ raw += offs.to_ulong
273
+ dump = tbl.dump
274
+ dumps << dump
275
+ len = dump.length
276
+ raw += len.to_ulong
277
+ offs += len
278
+ end
279
+
280
+ dumps.each do |dump|
281
+ raw += dump
282
+ end
283
+
284
+ raw
285
+ end
286
+
287
+ end
288
+
289
+ end
290
+ end