lib/prawn/font/afm.rb in prawn-1.3.0 vs lib/prawn/font/afm.rb in prawn-2.0.0
- old
+ new
@@ -4,18 +4,24 @@
#
# 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 '../../prawn/encoding'
+require_relative "../encoding"
module Prawn
class Font
# @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 ]
@@ -42,11 +48,10 @@
raise Prawn::Errors::UnknownFont, "#{name} is not a known font."
end
super
- @@winansi ||= Prawn::Encoding::WinAnsi.new # parse data/encodings/win_ansi.txt once only
@@font_data ||= SynchronizedCache.new # parse each ATM font file once only
file_name = @name.dup
file_name << ".afm" unless file_name =~ /\.afm$/
file_name = file_name[0] == ?/ ? file_name : find_font(file_name)
@@ -92,17 +97,23 @@
# 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)
- enc = @@winansi
- text.unpack("U*").collect { |i| enc[i] }.pack("C*")
- rescue ArgumentError
+ text.encode("windows-1252")
+ rescue ::Encoding::InvalidByteSequenceError,
+ ::Encoding::UndefinedConversionError
+
raise Prawn::Errors::IncompatibleStringEncoding,
- "Arguments to text methods must be UTF-8 encoded"
+ "Your document includes text that's not compatible with the Windows-1252 character set.\n"+
+ "If you need full UTF-8 support, use TTF 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
@@ -122,14 +133,12 @@
def encode_text(text, options={})
[[0, options[:kerning] ? kern(text) : text]]
end
def glyph_present?(char)
- if char == "_"
- true
- else
- normalize_encoding(char) != "_"
- end
+ !!normalize_encoding(char)
+ rescue Prawn::Errors::IncompatibleStringEncoding
+ false
end
private
def register(subset)