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.



46
47
48
49
50
51
52
# File 'lib/combine_pdf/fonts.rb', line 46

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.



36
37
38
# File 'lib/combine_pdf/fonts.rb', line 36

def cmap
  @cmap
end

#metricsObject

get the metrics dictionary for the font



38
39
40
# File 'lib/combine_pdf/fonts.rb', line 38

def metrics
  @metrics
end

#nameObject

set/get the name of the font



32
33
34
# File 'lib/combine_pdf/fonts.rb', line 32

def name
  @name
end

Instance Method Details

#encode(text) ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/combine_pdf/fonts.rb', line 55

def encode text
	# FixMe: embed RTL text convertion
	return format_string_to_pdf(text) unless self.cmap
	coded_array = text.chars.map do |c|
		if self.cmap[c]
			self.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