Class: Prawn::Font
- Inherits:
-
Object
- Object
- Prawn::Font
- 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
-
#family ⇒ Object
readonly
The current font family.
-
#identifier ⇒ Object
readonly
:nodoc:.
-
#metrics ⇒ Object
readonly
The font metrics object.
-
#name ⇒ Object
readonly
The current font name.
-
#reference ⇒ Object
readonly
:nodoc:.
-
#size(points = nil) ⇒ Object
Sets the default font size for use within a block.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_to_current_page ⇒ Object
:nodoc:.
-
#ascender ⇒ Object
The height of the ascender at the current font size in PDF points.
-
#descender ⇒ Object
The height of the descender at the current font size in PDF points.
-
#height ⇒ Object
Gets height of current font in PDF points at current font size.
-
#height_of(text, options = {}) ⇒ Object
Gets height of text in PDF points at current font size.
-
#initialize(name, options = {}) ⇒ Font
constructor
:nodoc:.
- #line_gap ⇒ Object
-
#normalize_encoding(text) ⇒ Object
:nodoc:.
-
#width_of(string) ⇒ Object
Gets width of string in PDF points at current font size.
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,={}) #:nodoc: @name = name @family = [:family] @metrics = Prawn::Font::Metrics[name] @document = [:for] @document.proc_set :PDF, :Text @size = DEFAULT_SIZE @identifier = :"F#{@document.font_registry.size + 1}" case(name) when /\.ttf$/i (name) else register_builtin(name) end add_to_current_page end |
Instance Attribute Details
#family ⇒ Object (readonly)
The current font family
108 109 110 |
# File 'lib/prawn/font.rb', line 108 def family @family end |
#identifier ⇒ Object (readonly)
:nodoc:
110 111 112 |
# File 'lib/prawn/font.rb', line 110 def identifier @identifier end |
#metrics ⇒ Object (readonly)
The font metrics object
102 103 104 |
# File 'lib/prawn/font.rb', line 102 def metrics @metrics end |
#name ⇒ Object (readonly)
The current font name
105 106 107 |
# File 'lib/prawn/font.rb', line 105 def name @name end |
#reference ⇒ Object (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
Instance Method Details
#add_to_current_page ⇒ Object
:nodoc:
213 214 215 |
# File 'lib/prawn/font.rb', line 213 def add_to_current_page #:nodoc: @document.page_fonts.merge!(@identifier => @reference) end |
#ascender ⇒ Object
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 |
#descender ⇒ Object
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 |
#height ⇒ Object
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,={}) @metrics.string_height( text, .merge(:font_size => @size) ) end |
#line_gap ⇒ Object
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 |