Class: Pdfrb::Document::Fonts

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/fonts.rb

Overview

Font facade. add registers a font in the catalog /Resources (so the canvas can reference it via Tf) and returns the resource name to use with the canvas.

Supports the 14 PDF standard Type1 fonts by name (Helvetica, Times-Roman, Courier, etc.) without embedding. Future TTF/OTF loaders (TODO 111/112) plug in via register_loader.

Constant Summary collapse

STANDARDS =
%w[
  Helvetica Helvetica-Bold Helvetica-Oblique Helvetica-BoldOblique
  Times-Roman Times-Bold Times-Italic Times-BoldItalic
  Courier Courier-Bold Courier-Oblique Courier-BoldOblique
  Symbol ZapfDingbats
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Fonts

Returns a new instance of Fonts.



22
23
24
25
26
# File 'lib/pdfrb/document/fonts.rb', line 22

def initialize(document)
  @document = document
  @next_id = 1
  @registry = {} # name -> resource Symbol
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



20
21
22
# File 'lib/pdfrb/document/fonts.rb', line 20

def document
  @document
end

Class Method Details

.loadersObject



62
63
64
# File 'lib/pdfrb/document/fonts.rb', line 62

def loaders
  @loaders ||= []
end

.register_loader(loader) ⇒ Object



66
67
68
# File 'lib/pdfrb/document/fonts.rb', line 66

def register_loader(loader)
  loaders.unshift(loader)
end

Instance Method Details

#[](name) ⇒ Object

Look up the resource name registered for a font name.



45
46
47
# File 'lib/pdfrb/document/fonts.rb', line 45

def [](name)
  @registry[name]
end

#add(name_or_io, **opts) ⇒ Object

Register a font and return the resource name (e.g. :F1).

For the 14 standard fonts: pass the font name; no embedding is required. For everything else, use the loader protocol (TODO 111).



33
34
35
36
37
38
39
40
41
42
# File 'lib/pdfrb/document/fonts.rb', line 33

def add(name_or_io, **opts)
  name = font_name_for(name_or_io)
  cached = @registry[name]
  return cached if cached

  resource = next_resource_name
  register_font(resource, name, **opts)
  @registry[name] = resource
  resource
end

#each(&block) ⇒ Object



49
50
51
52
53
54
# File 'lib/pdfrb/document/fonts.rb', line 49

def each(&block)
  return enum_for(:each) unless block_given?

  @registry.each(&block)
  self
end