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.
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:.
- #inspect ⇒ Object
- #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:
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/prawn/font.rb', line 131 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}" @reference = nil end |
Instance Attribute Details
#family ⇒ Object (readonly)
The current font family
121 122 123 |
# File 'lib/prawn/font.rb', line 121 def family @family end |
#identifier ⇒ Object (readonly)
:nodoc:
123 124 125 |
# File 'lib/prawn/font.rb', line 123 def identifier @identifier end |
#metrics ⇒ Object (readonly)
The font metrics object
115 116 117 |
# File 'lib/prawn/font.rb', line 115 def metrics @metrics end |
#name ⇒ Object (readonly)
The current font name
118 119 120 |
# File 'lib/prawn/font.rb', line 118 def name @name end |
#reference ⇒ Object (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_page ⇒ Object
:nodoc:
230 231 232 233 |
# File 'lib/prawn/font.rb', line 230 def add_to_current_page #:nodoc: unless @reference @document.page_fonts.merge!(@identifier => @reference) end |
#ascender ⇒ Object
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 |
#descender ⇒ Object
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 |
#height ⇒ Object
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,={}) @metrics.string_height( text, :font_size => @size, :line_width => [:line_width] ) end |
#inspect ⇒ Object
145 146 147 |
# File 'lib/prawn/font.rb', line 145 def inspect "Prawn::Font< #{name}: #{size} >" end |
#line_gap ⇒ Object
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 |