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

Instance Method Summary collapse

Constructor Details

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

:nodoc:



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/prawn/font.rb', line 131

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}"  

  @reference = nil
end

Instance Attribute Details

#familyObject (readonly)

The current font family



121
122
123
# File 'lib/prawn/font.rb', line 121

def family
  @family
end

#identifierObject (readonly)

:nodoc:



123
124
125
# File 'lib/prawn/font.rb', line 123

def identifier
  @identifier
end

#metricsObject (readonly)

The font metrics object



115
116
117
# File 'lib/prawn/font.rb', line 115

def metrics
  @metrics
end

#nameObject (readonly)

The current font name



118
119
120
# File 'lib/prawn/font.rb', line 118

def name
  @name
end

#referenceObject (readonly)

:nodoc:



123
124
125
# File 'lib/prawn/font.rb', line 123

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.



169
170
171
172
173
174
175
# File 'lib/prawn/font.rb', line 169

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

Instance Method Details

#add_to_current_pageObject

:nodoc:



230
231
232
233
# File 'lib/prawn/font.rb', line 230

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

#ascenderObject

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



205
206
207
# File 'lib/prawn/font.rb', line 205

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

#descenderObject

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



211
212
213
# File 'lib/prawn/font.rb', line 211

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

#heightObject

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



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

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.

If using an AFM, string must be encoded as WinAnsi (Use normalize_encoding to convert)



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

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

#inspectObject



145
146
147
# File 'lib/prawn/font.rb', line 145

def inspect
  "Prawn::Font< #{name}: #{size} >"
end

#line_gapObject



215
216
217
# File 'lib/prawn/font.rb', line 215

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

#normalize_encoding(text) ⇒ Object

:nodoc:



219
220
221
222
223
224
225
226
227
228
# File 'lib/prawn/font.rb', line 219

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

If using an AFM, string must be encoded as WinAnsi (Use normalize_encoding to convert)



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

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