Class: PDF::Reader::Font
- Inherits:
-
Object
- Object
- PDF::Reader::Font
- Defined in:
- lib/pdf/reader/font.rb
Overview
Represents a single font PDF object and provides some useful methods for extracting info. Mainly used for converting text to UTF-8.
Instance Attribute Summary collapse
-
#basefont ⇒ Object
readonly
: Symbol?.
-
#cid_default_width ⇒ Object
readonly
: Numeric.
-
#cid_widths ⇒ Object
readonly
: Array.
-
#descendantfonts ⇒ Object
: Array.
-
#encoding ⇒ Object
: PDF::Reader::Encoding.
-
#first_char ⇒ Object
readonly
: Integer?.
-
#font_descriptor ⇒ Object
readonly
: PDF::Reader::FontDescriptor?.
-
#last_char ⇒ Object
readonly
: Integer?.
-
#subtype ⇒ Object
: Symbol?.
-
#tounicode ⇒ Object
: PDF::Reader::CMap | nil.
-
#widths ⇒ Object
readonly
: Array.
Instance Method Summary collapse
-
#glyph_width(code_point) ⇒ Object
looks up the specified codepoint and returns a value that is in (pdf) glyph space, which is 1000 glyph units = 1 text space unit : (Integer | String) -> Numeric.
-
#glyph_width_in_text_space(code_point) ⇒ Object
In most cases glyph width is converted into text space with a simple divide by 1000.
-
#initialize(ohash, obj) ⇒ Font
constructor
: (PDF::Reader::ObjectHash, Hash[Symbol, untyped]) -> void.
-
#to_utf8(params) ⇒ Object
: (Integer | String | Array[Integer | String]) -> String.
-
#unpack(data) ⇒ Object
: (String) -> (Array[Integer | Float | String | nil] | nil).
Constructor Details
#initialize(ohash, obj) ⇒ Font
: (PDF::Reader::ObjectHash, Hash[Symbol, untyped]) -> void
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/pdf/reader/font.rb', line 80 def initialize(ohash, obj) @ohash = ohash @tounicode = nil #: PDF::Reader::CMap | nil @descendantfonts = [] #: Array[PDF::Reader::Font] @widths = [] #: Array[Numeric] @first_char = nil #: Integer? @last_char = nil #: Integer? @basefont = nil #: Symbol? @font_descriptor = nil #: PDF::Reader::FontDescriptor? @cid_widths = [] #: Array[Numeric] @cid_default_width = 0 #: Numeric @encoding = PDF::Reader::Encoding.new(:StandardEncoding) #: PDF::Reader::Encoding @cached_widths = {} #: Hash[Integer, Numeric] @font_matrix = nil #: Array[Numeric] | nil extract_base_info(obj) extract_type3_info(obj) extract_descriptor(obj) extract_descendants(obj) @width_calc = build_width_calculator #: widthCalculator end |
Instance Attribute Details
#basefont ⇒ Object (readonly)
: Symbol?
68 69 70 |
# File 'lib/pdf/reader/font.rb', line 68 def basefont @basefont end |
#cid_default_width ⇒ Object (readonly)
: Numeric
77 78 79 |
# File 'lib/pdf/reader/font.rb', line 77 def cid_default_width @cid_default_width end |
#cid_widths ⇒ Object (readonly)
: Array
74 75 76 |
# File 'lib/pdf/reader/font.rb', line 74 def cid_widths @cid_widths end |
#descendantfonts ⇒ Object
: Array
53 54 55 |
# File 'lib/pdf/reader/font.rb', line 53 def descendantfonts @descendantfonts end |
#encoding ⇒ Object
: PDF::Reader::Encoding
50 51 52 |
# File 'lib/pdf/reader/font.rb', line 50 def encoding @encoding end |
#first_char ⇒ Object (readonly)
: Integer?
62 63 64 |
# File 'lib/pdf/reader/font.rb', line 62 def first_char @first_char end |
#font_descriptor ⇒ Object (readonly)
: PDF::Reader::FontDescriptor?
71 72 73 |
# File 'lib/pdf/reader/font.rb', line 71 def font_descriptor @font_descriptor end |
#last_char ⇒ Object (readonly)
: Integer?
65 66 67 |
# File 'lib/pdf/reader/font.rb', line 65 def last_char @last_char end |
#subtype ⇒ Object
: Symbol?
47 48 49 |
# File 'lib/pdf/reader/font.rb', line 47 def subtype @subtype end |
#tounicode ⇒ Object
: PDF::Reader::CMap | nil
56 57 58 |
# File 'lib/pdf/reader/font.rb', line 56 def tounicode @tounicode end |
#widths ⇒ Object (readonly)
: Array
59 60 61 |
# File 'lib/pdf/reader/font.rb', line 59 def widths @widths end |
Instance Method Details
#glyph_width(code_point) ⇒ Object
looks up the specified codepoint and returns a value that is in (pdf) glyph space, which is 1000 glyph units = 1 text space unit : (Integer | String) -> Numeric
119 120 121 122 123 124 125 126 |
# File 'lib/pdf/reader/font.rb', line 119 def glyph_width(code_point) if code_point.is_a?(String) code_point = unpack_string_to_array_of_ints(code_point, encoding.unpack).first raise MalformedPDFError, "code point missing" if code_point.nil? end @cached_widths[code_point] ||= @width_calc.glyph_width(code_point) end |
#glyph_width_in_text_space(code_point) ⇒ Object
In most cases glyph width is converted into text space with a simple divide by 1000.
However, Type3 fonts provide their own FontMatrix that’s used for the transformation.
: (Integer | String) -> Numeric
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/pdf/reader/font.rb', line 133 def glyph_width_in_text_space(code_point) glyph_width_in_glyph_space = glyph_width(code_point) if @subtype == :Type3 x1, _y1 = font_matrix_transform(0,0) x2, _y2 = font_matrix_transform(glyph_width_in_glyph_space, 0) (x2 - x1).abs.round(2) else glyph_width_in_glyph_space / 1000.0 end end |
#to_utf8(params) ⇒ Object
: (Integer | String | Array[Integer | String]) -> String
103 104 105 106 107 108 109 |
# File 'lib/pdf/reader/font.rb', line 103 def to_utf8(params) if @tounicode to_utf8_via_cmap(params, @tounicode) else to_utf8_via_encoding(params) end end |
#unpack(data) ⇒ Object
: (String) -> (Array[Integer | Float | String | nil] | nil)
112 113 114 |
# File 'lib/pdf/reader/font.rb', line 112 def unpack(data) data.unpack(encoding.unpack) end |