Class: CombinePDF::Fonts::Font

Inherits:
Hash
  • Object
show all
Defined in:
lib/combine_pdf/fonts.rb

Overview

the internal class for the Fonts model

this is an internal class, used by PDFWriter and PDF. you don’t normally need to use this.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, font_metrics = { 32 => { wx: 250, boundingbox: [0, 0, 0, 0] } }, object = nil, c_map = nil) ⇒ Font

internelized a new Font object, setting it’s name, it’s metrics Hash and the object Hash. Optionally set a CMap, if the glyph ID and character codes aren’t the same.



39
40
41
42
43
44
45
# File 'lib/combine_pdf/fonts.rb', line 39

def initialize(name = nil, font_metrics = { 32 => { wx: 250, boundingbox: [0, 0, 0, 0] } }, object = nil, c_map = nil)
  self.name = name
  self.cmap = c_map
  self.metrics = font_metrics
  self[:is_reference_only] = true
  self[:referenced_object] = object
end

Instance Attribute Details

#cmapObject

set/get a character/Glyph mapping for the font (the equivilant of a CMap)

the cmap is a Hash were each key is the unicode character code and the value is the glyph ID for the font.



28
29
30
# File 'lib/combine_pdf/fonts.rb', line 28

def cmap
  @cmap
end

#metricsObject

get the metrics dictionary for the font



30
31
32
# File 'lib/combine_pdf/fonts.rb', line 30

def metrics
  @metrics
end

#nameObject

set/get the name of the font



24
25
26
# File 'lib/combine_pdf/fonts.rb', line 24

def name
  @name
end

Instance Method Details

#encode(text) ⇒ Object

This function translate a unicode string, to a character glyph ID stream.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/combine_pdf/fonts.rb', line 48

def encode(text)
  # FIXME: embed RTL text convertion
  return format_string_to_pdf(text) unless cmap
  coded_array = text.chars.map do |c|
    if cmap[c]
      cmap[c]
    else
      warn "CombinePDF ERROR: couldn't encode string - characters not supported by the chosen font."
      ''
    end
  end
  coded_array.unshift '<'
  coded_array.push '>'
  coded_array.join
end