Class: PDF::Reader::FontDescriptor
- Inherits:
-
Object
- Object
- PDF::Reader::FontDescriptor
- Defined in:
- lib/pdf/reader/font_descriptor.rb
Overview
Font descriptors are outlined in Section 9.8, PDF 32000-1:2008, pp 281-288
Instance Attribute Summary collapse
-
#ascent ⇒ Object
readonly
Returns the value of attribute ascent.
-
#avg_width ⇒ Object
readonly
Returns the value of attribute avg_width.
-
#cap_height ⇒ Object
readonly
Returns the value of attribute cap_height.
-
#descent ⇒ Object
readonly
Returns the value of attribute descent.
-
#font_bounding_box ⇒ Object
readonly
Returns the value of attribute font_bounding_box.
-
#font_family ⇒ Object
readonly
Returns the value of attribute font_family.
-
#font_flags ⇒ Object
readonly
Returns the value of attribute font_flags.
-
#font_name ⇒ Object
readonly
Returns the value of attribute font_name.
-
#font_stretch ⇒ Object
readonly
Returns the value of attribute font_stretch.
-
#font_weight ⇒ Object
readonly
Returns the value of attribute font_weight.
-
#italic_angle ⇒ Object
readonly
Returns the value of attribute italic_angle.
-
#leading ⇒ Object
readonly
Returns the value of attribute leading.
-
#max_width ⇒ Object
readonly
Returns the value of attribute max_width.
-
#missing_width ⇒ Object
readonly
Returns the value of attribute missing_width.
-
#stem_v ⇒ Object
readonly
Returns the value of attribute stem_v.
-
#x_height ⇒ Object
readonly
Returns the value of attribute x_height.
Instance Method Summary collapse
-
#glyph_to_pdf_scale_factor ⇒ Object
PDF states that a glyph is 1000 units wide, true type doesn’t enforce any behavior, but uses units/em to define how wide the ‘M’ is (the widest letter).
- #glyph_width(char_code) ⇒ Object
-
#initialize(ohash, fd_hash) ⇒ FontDescriptor
constructor
A new instance of FontDescriptor.
Constructor Details
#initialize(ohash, fd_hash) ⇒ FontDescriptor
Returns a new instance of FontDescriptor.
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 |
# File 'lib/pdf/reader/font_descriptor.rb', line 17 def initialize(ohash, fd_hash) # TODO change these to typed derefs @ascent = ohash.deref_number(fd_hash[:Ascent]) || 0 @descent = ohash.deref_number(fd_hash[:Descent]) || 0 @missing_width = ohash.deref_number(fd_hash[:MissingWidth]) || 0 @font_bounding_box = ohash.deref_array_of_numbers(fd_hash[:FontBBox]) || [0,0,0,0] @avg_width = ohash.deref_number(fd_hash[:AvgWidth]) || 0 @cap_height = ohash.deref_number(fd_hash[:CapHeight]) || 0 @font_flags = ohash.deref_integer(fd_hash[:Flags]) || 0 @italic_angle = ohash.deref_number(fd_hash[:ItalicAngle]) @font_name = ohash.deref_name(fd_hash[:FontName]).to_s @leading = ohash.deref_number(fd_hash[:Leading]) || 0 @max_width = ohash.deref_number(fd_hash[:MaxWidth]) || 0 @stem_v = ohash.deref_number(fd_hash[:StemV]) @x_height = ohash.deref_number(fd_hash[:XHeight]) @font_stretch = ohash.deref_name(fd_hash[:FontStretch]) || :Normal @font_weight = ohash.deref_number(fd_hash[:FontWeight]) || 400 @font_family = ohash.deref_string(fd_hash[:FontFamily]) # A FontDescriptor may have an embedded font program in FontFile # (Type 1 Font Program), FontFile2 (TrueType font program), or # FontFile3 (Other font program as defined by Subtype entry) # Subtype entries: # 1) Type1C: Type 1 Font Program in Compact Font Format # 2) CIDFontType0C: Type 0 Font Program in Compact Font Format # 3) OpenType: OpenType Font Program # see Section 9.9, PDF 32000-1:2008, pp 288-292 @font_program_stream = ohash.deref_stream(fd_hash[:FontFile2]) #TODO handle FontFile and FontFile3 @is_ttf = true if @font_program_stream end |
Instance Attribute Details
#ascent ⇒ Object (readonly)
Returns the value of attribute ascent.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def ascent @ascent end |
#avg_width ⇒ Object (readonly)
Returns the value of attribute avg_width.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def avg_width @avg_width end |
#cap_height ⇒ Object (readonly)
Returns the value of attribute cap_height.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def cap_height @cap_height end |
#descent ⇒ Object (readonly)
Returns the value of attribute descent.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def descent @descent end |
#font_bounding_box ⇒ Object (readonly)
Returns the value of attribute font_bounding_box.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def font_bounding_box @font_bounding_box end |
#font_family ⇒ Object (readonly)
Returns the value of attribute font_family.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def font_family @font_family end |
#font_flags ⇒ Object (readonly)
Returns the value of attribute font_flags.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def font_flags @font_flags end |
#font_name ⇒ Object (readonly)
Returns the value of attribute font_name.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def font_name @font_name end |
#font_stretch ⇒ Object (readonly)
Returns the value of attribute font_stretch.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def font_stretch @font_stretch end |
#font_weight ⇒ Object (readonly)
Returns the value of attribute font_weight.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def font_weight @font_weight end |
#italic_angle ⇒ Object (readonly)
Returns the value of attribute italic_angle.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def italic_angle @italic_angle end |
#leading ⇒ Object (readonly)
Returns the value of attribute leading.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def leading @leading end |
#max_width ⇒ Object (readonly)
Returns the value of attribute max_width.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def max_width @max_width end |
#missing_width ⇒ Object (readonly)
Returns the value of attribute missing_width.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def missing_width @missing_width end |
#stem_v ⇒ Object (readonly)
Returns the value of attribute stem_v.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def stem_v @stem_v end |
#x_height ⇒ Object (readonly)
Returns the value of attribute x_height.
12 13 14 |
# File 'lib/pdf/reader/font_descriptor.rb', line 12 def x_height @x_height end |
Instance Method Details
#glyph_to_pdf_scale_factor ⇒ Object
PDF states that a glyph is 1000 units wide, true type doesn’t enforce any behavior, but uses units/em to define how wide the ‘M’ is (the widest letter)
68 69 70 71 72 73 74 75 |
# File 'lib/pdf/reader/font_descriptor.rb', line 68 def glyph_to_pdf_scale_factor if @is_ttf @glyph_to_pdf_sf ||= (1.0 / ttf_program_stream.header.units_per_em) * 1000.0 else @glyph_to_pdf_sf ||= 1.0 end @glyph_to_pdf_sf end |
#glyph_width(char_code) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pdf/reader/font_descriptor.rb', line 50 def glyph_width(char_code) if @is_ttf if ttf_program_stream.cmap.unicode.length > 0 glyph_id = ttf_program_stream.cmap.unicode.first[char_code] else glyph_id = char_code end char_metric = ttf_program_stream.horizontal_metrics.metrics[glyph_id] if char_metric char_metric.advance_width else 0 end end end |