Class: Prawn::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/font.rb,
lib/prawn/font/cmap.rb,
lib/prawn/font/metrics.rb,
lib/prawn/font/wrapping.rb

Overview

Provides font information and helper functions.

Defined Under Namespace

Modules: Wrapping Classes: CMap, Metrics

Constant Summary collapse

BUILT_INS =
%w[ Courier Helvetica Times-Roman Symbol ZapfDingbats 
Courier-Bold Courier-Oblique Courier-BoldOblique
Times-Bold Times-Italic Times-BoldItalic
Helvetica-Bold Helvetica-Oblique Helvetica-BoldOblique ]
DEFAULT_SIZE =
12

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Font

:nodoc:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/prawn/font.rb', line 118

def initialize(name,options={}) #:nodoc:
  @name       = name   
  @family     = options[:family]      
          
  @metrics    = Prawn::Font::Metrics[name] 
  @document   = options[:for]  
  
  @document.proc_set :PDF, :Text  
  @size       = DEFAULT_SIZE
  @identifier = :"F#{@document.font_registry.size + 1}"  
  
  case(name)
  when /\.ttf$/i
    embed_ttf(name)
  else
    register_builtin(name)
  end  
  
  add_to_current_page    
end

Instance Attribute Details

#familyObject (readonly)

The current font family



108
109
110
# File 'lib/prawn/font.rb', line 108

def family
  @family
end

#identifierObject (readonly)

:nodoc:



110
111
112
# File 'lib/prawn/font.rb', line 110

def identifier
  @identifier
end

#metricsObject (readonly)

The font metrics object



102
103
104
# File 'lib/prawn/font.rb', line 102

def metrics
  @metrics
end

#nameObject (readonly)

The current font name



105
106
107
# File 'lib/prawn/font.rb', line 105

def name
  @name
end

#referenceObject (readonly)

:nodoc:



110
111
112
# File 'lib/prawn/font.rb', line 110

def reference
  @reference
end

#size(points = nil) ⇒ Object

Sets the default font size for use within a block. Individual overrides can be used as desired. The previous font size will be restored after the block.

Prawn::Document.generate(“font_size.pdf”) do

font.size = 16
text "At size 16"

font.size(10) do
  text "At size 10"
  text "At size 6", :size => 6
  text "At size 10"
end

text "At size 16"

end

When called without an argument, this method returns the current font size.



159
160
161
162
163
164
165
# File 'lib/prawn/font.rb', line 159

def size(points=nil)      
  return @size unless points
  size_before_yield = @size
  @size = points
  yield
  @size = size_before_yield
end

Class Method Details

.register(name, options = {}) ⇒ Object

:nodoc:



97
98
99
# File 'lib/prawn/font.rb', line 97

def self.register(name,options={})  #:nodoc:      
   options[:for].font_registry[name] = Font.new(name,options)
end

Instance Method Details

#add_to_current_pageObject

:nodoc:



213
214
215
# File 'lib/prawn/font.rb', line 213

def add_to_current_page #:nodoc:
  @document.page_fonts.merge!(@identifier => @reference)
end

#ascenderObject

The height of the ascender at the current font size in PDF points



188
189
190
# File 'lib/prawn/font.rb', line 188

def ascender 
  @metrics.ascender / 1000.0 * @size
end

#descenderObject

The height of the descender at the current font size in PDF points



194
195
196
# File 'lib/prawn/font.rb', line 194

def descender 
  @metrics.descender / 1000.0 * @size
end

#heightObject

Gets height of current font in PDF points at current font size



182
183
184
# File 'lib/prawn/font.rb', line 182

def height
  @metrics.font_height(@size)       
end

#height_of(text, options = {}) ⇒ Object

Gets height of text in PDF points at current font size. Text :line_width must be specified in PDF points.



176
177
178
# File 'lib/prawn/font.rb', line 176

def height_of(text,options={})
  @metrics.string_height( text, options.merge(:font_size  => @size) ) 
end

#line_gapObject



198
199
200
# File 'lib/prawn/font.rb', line 198

def line_gap
  @metrics.line_gap / 1000.0 * @size
end

#normalize_encoding(text) ⇒ Object

:nodoc:



202
203
204
205
206
207
208
209
210
211
# File 'lib/prawn/font.rb', line 202

def normalize_encoding(text) # :nodoc:
  # check the string is encoded sanely
  # - UTF-8 for TTF fonts
  # - ISO-8859-1 for Built-In fonts
  if @metrics.type0?
    normalize_ttf_encoding(text) 
  else
    normalize_builtin_encoding(text) 
  end 
end

#width_of(string) ⇒ Object

Gets width of string in PDF points at current font size



169
170
171
# File 'lib/prawn/font.rb', line 169

def width_of(string)
  @metrics.string_width(string,@size)
end