Class: HexaPDF::Type::FontSimple

Inherits:
Font show all
Defined in:
lib/hexapdf/type/font_simple.rb

Overview

Represents a simple PDF font.

A simple font has only single-byte character codes and only supports horizontal metrics.

See: PDF1.7 s9.6

Direct Known Subclasses

FontTrueType, FontType1

Constant Summary

Constants included from DictionaryFields

DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate

Constants inherited from Object

Object::NOT_DUPLICATABLE_CLASSES

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Font

#must_be_indirect?

Methods inherited from Dictionary

#[], #[]=, define_field, #delete, #each, each_field, #empty?, field, #key?, #to_hash, #type

Methods inherited from Object

#<=>, #==, deep_copy, #deep_copy, #document?, #eql?, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#bounding_boxObject

Returns the bounding box of the font or nil if it is not found.



114
115
116
117
118
119
120
# File 'lib/hexapdf/type/font_simple.rb', line 114

def bounding_box
  if key?(:FontDescriptor) && self[:FontDescriptor].key?(:FontBBox)
    self[:FontDescriptor][:FontBBox].value
  else
    nil
  end
end

#decode(string) ⇒ Object

Decodes the given string into an array of character codes.



85
86
87
# File 'lib/hexapdf/type/font_simple.rb', line 85

def decode(string)
  string.bytes
end

#embedded?Boolean

Returns true if the font is embedded.

Returns:



128
129
130
131
# File 'lib/hexapdf/type/font_simple.rb', line 128

def embedded?
  dict = self[:FontDescriptor]
  dict && (dict[:FontFile] || dict[:FontFile2] || dict[:FontFile3])
end

#encodingObject

Returns the encoding object used for this font.

Note that the encoding is cached internally when accessed the first time.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hexapdf/type/font_simple.rb', line 56

def encoding
  @encoding ||=
    begin
      case (val = self[:Encoding])
      when Symbol
        encoding = HexaPDF::Font::Encoding.for_name(val)
        encoding = encoding_from_font if encoding.nil?
        encoding
      when HexaPDF::Dictionary, Hash
        encoding = val[:BaseEncoding]
        encoding = HexaPDF::Font::Encoding.for_name(encoding) if encoding
        unless encoding
          if embedded? || symbolic?
            encoding = encoding_from_font
          else
            encoding = HexaPDF::Font::Encoding.for_name(:StandardEncoding)
          end
        end
        encoding = difference_encoding(encoding, val[:Differences]) if val.key?(:Differences)
        encoding
      when nil
        encoding_from_font
      else
        raise HexaPDF::Error, "Unknown value for font's encoding: #{self[:Encoding]}"
      end
    end
end

#symbolic?Boolean

Returns true if the font is a symbolic font, false if it is not, and nil if it is not known.

Returns:



135
136
137
# File 'lib/hexapdf/type/font_simple.rb', line 135

def symbolic?
  self[:FontDescriptor] && self[:FontDescriptor].flagged?(:symbolic) || nil
end

#to_utf8(code) ⇒ Object

Returns the UTF-8 string for the given character code, or an empty string if no mapping was found.



91
92
93
94
95
# File 'lib/hexapdf/type/font_simple.rb', line 91

def to_utf8(code)
  str = super
  str = encoding.unicode(code) if str.empty?
  str
end

#width(code) ⇒ Object

Returns the unscaled width of the given code point in glyph units, or 0 if the width for the code point is missing.



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hexapdf/type/font_simple.rb', line 99

def width(code)
  widths = self[:Widths]
  first_char = self[:FirstChar] || -1
  last_char = self[:LastChar] || -1

  if widths && code >= first_char && code <= last_char
    widths[code - first_char]
  elsif widths && key?(:FontDescriptor)
    self[:FontDescriptor][:MissingWidth]
  else
    0
  end
end

#writing_modeObject

Returns the writing mode which is always :horizontal for simple fonts like Type1.



123
124
125
# File 'lib/hexapdf/type/font_simple.rb', line 123

def writing_mode
  :horizontal
end