lib/prawn/font/ttf.rb in prawn-0.4.0 vs lib/prawn/font/ttf.rb in prawn-0.4.1
- old
+ new
@@ -227,12 +227,15 @@
# FIXME: we need postscript_name and glyph widths from the font
# subset. Perhaps this could be done by querying the subset,
# rather than by parsing the font that the subset produces?
font = TTFunk::File.new(font_content)
- basename = font.name.postscript_name
+ # empirically, it looks like Adobe Reader will not display fonts
+ # if their font name is more than 33 bytes long. Strange. But true.
+ basename = font.name.postscript_name[0, 33]
+
raise "Can't detect a postscript name for #{file}" if basename.nil?
compressed_font = Zlib::Deflate.deflate(font_content)
fontfile = @document.ref(:Length => compressed_font.size,
@@ -252,42 +255,47 @@
:CapHeight => cap_height,
:XHeight => x_height)
hmtx = font.horizontal_metrics
widths = font.cmap.tables.first.code_map.map { |gid|
- Integer(hmtx.widths[gid] * scale_factor) }
+ Integer(hmtx.widths[gid] * scale_factor) }[32..-1]
- reference.data.update(:Subtype => :TrueType,
- :BaseFont => basename,
- :FontDescriptor => descriptor,
- :FirstChar => 0,
- :LastChar => 255,
- :Widths => @document.ref(widths))
+ # It would be nice to have Encoding set for the macroman subsets,
+ # and only do a ToUnicode cmap for non-encoded unicode subsets.
+ # However, apparently Adobe Reader won't render MacRoman encoded
+ # subsets if original font contains unicode characters. (It has to
+ # be some flag or something that ttfunk is simply copying over...
+ # but I can't figure out which flag that is.)
+ #
+ # For now, it's simplest to just create a unicode cmap for every font.
+ # It offends my inner purist, but it'll do.
- if @subsets[subset].unicode?
- map = @subsets[subset].to_unicode_map
+ map = @subsets[subset].to_unicode_map
- ranges = [[]]
- lines = map.keys.sort.inject("") do |s, code|
- ranges << [] if ranges.last.length >= 100
- unicode = map[code]
- ranges.last << "<%02x><%04x>" % [code, unicode]
- end
+ ranges = [[]]
+ lines = map.keys.sort.inject("") do |s, code|
+ ranges << [] if ranges.last.length >= 100
+ unicode = map[code]
+ ranges.last << "<%02x><%04x>" % [code, unicode]
+ end
- range_blocks = ranges.inject("") do |s, list|
- s << "%d beginbfchar\n%s\nendbfchar\n" % [list.length, list.join("\n")]
- end
+ range_blocks = ranges.inject("") do |s, list|
+ s << "%d beginbfchar\n%s\nendbfchar\n" % [list.length, list.join("\n")]
+ end
- to_unicode_cmap = UNICODE_CMAP_TEMPLATE % range_blocks.strip
+ to_unicode_cmap = UNICODE_CMAP_TEMPLATE % range_blocks.strip
- cmap = @document.ref({})
- cmap << to_unicode_cmap
- cmap.compress_stream
+ cmap = @document.ref({})
+ cmap << to_unicode_cmap
+ cmap.compress_stream
- @references[subset].data[:ToUnicode] = cmap
- else
- @references[subset].data[:Encoding] = :MacRomanEncoding
- end
+ reference.data.update(:Subtype => :TrueType,
+ :BaseFont => basename,
+ :FontDescriptor => descriptor,
+ :FirstChar => 32,
+ :LastChar => 255,
+ :Widths => @document.ref(widths),
+ :ToUnicode => cmap)
end
UNICODE_CMAP_TEMPLATE = <<-STR.strip.gsub(/^\s*/, "")
/CIDInit /ProcSet findresource begin
12 dict begin