prawn 1.0.0.rc2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +9 -0
  3. data/COPYING +2 -2
  4. data/Gemfile +8 -15
  5. data/LICENSE +1 -1
  6. data/Rakefile +25 -16
  7. data/data/images/16bit.alpha +0 -0
  8. data/data/images/16bit.color +0 -0
  9. data/data/images/dice.alpha +0 -0
  10. data/data/images/dice.color +0 -0
  11. data/data/images/indexed_color.dat +0 -0
  12. data/data/images/indexed_color.png +0 -0
  13. data/data/images/license.md +8 -0
  14. data/data/images/page_white_text.alpha +0 -0
  15. data/data/images/page_white_text.color +0 -0
  16. data/lib/prawn.rb +85 -23
  17. data/lib/prawn/document.rb +134 -116
  18. data/lib/prawn/document/bounding_box.rb +33 -4
  19. data/lib/prawn/document/column_box.rb +18 -6
  20. data/lib/prawn/document/graphics_state.rb +11 -74
  21. data/lib/prawn/document/internals.rb +24 -23
  22. data/lib/prawn/document/span.rb +12 -10
  23. data/lib/prawn/encoding.rb +8 -9
  24. data/lib/prawn/errors.rb +13 -32
  25. data/lib/prawn/font.rb +137 -105
  26. data/lib/prawn/font/afm.rb +76 -32
  27. data/lib/prawn/font/dfont.rb +4 -3
  28. data/lib/prawn/font/ttf.rb +33 -25
  29. data/lib/prawn/font_metric_cache.rb +47 -0
  30. data/lib/prawn/graphics.rb +177 -57
  31. data/lib/prawn/graphics/cap_style.rb +4 -3
  32. data/lib/prawn/graphics/color.rb +5 -4
  33. data/lib/prawn/graphics/dash.rb +53 -31
  34. data/lib/prawn/graphics/join_style.rb +9 -7
  35. data/lib/prawn/graphics/patterns.rb +4 -15
  36. data/lib/prawn/graphics/transformation.rb +10 -9
  37. data/lib/prawn/graphics/transparency.rb +3 -1
  38. data/lib/prawn/{layout/grid.rb → grid.rb} +72 -54
  39. data/lib/prawn/image_handler.rb +42 -0
  40. data/lib/prawn/images.rb +58 -54
  41. data/lib/prawn/images/image.rb +6 -22
  42. data/lib/prawn/images/jpg.rb +20 -14
  43. data/lib/prawn/images/png.rb +58 -121
  44. data/lib/prawn/layout.rb +12 -15
  45. data/lib/prawn/measurement_extensions.rb +10 -6
  46. data/lib/prawn/measurements.rb +27 -21
  47. data/lib/prawn/outline.rb +108 -147
  48. data/lib/prawn/repeater.rb +10 -8
  49. data/lib/prawn/security.rb +59 -40
  50. data/lib/prawn/security/arcfour.rb +52 -0
  51. data/lib/prawn/soft_mask.rb +4 -4
  52. data/lib/prawn/stamp.rb +5 -3
  53. data/lib/prawn/table.rb +83 -60
  54. data/lib/prawn/table/cell.rb +17 -21
  55. data/lib/prawn/table/cell/image.rb +2 -3
  56. data/lib/prawn/table/cell/in_table.rb +8 -2
  57. data/lib/prawn/table/cell/span_dummy.rb +5 -0
  58. data/lib/prawn/table/cell/subtable.rb +3 -2
  59. data/lib/prawn/table/cell/text.rb +14 -12
  60. data/lib/prawn/table/cells.rb +58 -14
  61. data/lib/prawn/table/column_width_calculator.rb +61 -0
  62. data/lib/prawn/text.rb +27 -26
  63. data/lib/prawn/text/box.rb +12 -6
  64. data/lib/prawn/text/formatted.rb +5 -4
  65. data/lib/prawn/text/formatted/arranger.rb +290 -0
  66. data/lib/prawn/text/formatted/box.rb +85 -57
  67. data/lib/prawn/text/formatted/fragment.rb +11 -11
  68. data/lib/prawn/text/formatted/line_wrap.rb +266 -0
  69. data/lib/prawn/text/formatted/parser.rb +11 -4
  70. data/lib/prawn/text/formatted/wrap.rb +156 -0
  71. data/lib/prawn/utilities.rb +5 -3
  72. data/manual/document_and_page_options/document_and_page_options.rb +2 -1
  73. data/manual/document_and_page_options/metadata.rb +3 -3
  74. data/manual/document_and_page_options/page_size.rb +2 -2
  75. data/manual/document_and_page_options/print_scaling.rb +20 -0
  76. data/manual/example_file.rb +2 -7
  77. data/manual/example_helper.rb +62 -81
  78. data/manual/graphics/common_lines.rb +2 -0
  79. data/manual/graphics/helper.rb +11 -4
  80. data/manual/graphics/stroke_dash.rb +19 -14
  81. data/manual/manual/cover.rb +16 -0
  82. data/manual/manual/manual.rb +1 -5
  83. data/manual/text/fallback_fonts.rb +4 -4
  84. data/manual/text/formatted_text.rb +5 -5
  85. data/manual/text/inline.rb +2 -4
  86. data/manual/text/registering_families.rb +12 -12
  87. data/manual/text/single_usage.rb +4 -4
  88. data/manual/text/text.rb +0 -2
  89. data/prawn.gemspec +21 -13
  90. data/spec/acceptance/png.rb +23 -0
  91. data/spec/annotations_spec.rb +16 -32
  92. data/spec/bounding_box_spec.rb +22 -5
  93. data/spec/cell_spec.rb +49 -5
  94. data/spec/column_box_spec.rb +32 -0
  95. data/spec/destinations_spec.rb +5 -5
  96. data/spec/document_spec.rb +112 -118
  97. data/spec/extensions/encoding_helpers.rb +5 -2
  98. data/spec/font_metric_cache_spec.rb +52 -0
  99. data/spec/font_spec.rb +121 -120
  100. data/spec/formatted_text_arranger_spec.rb +24 -24
  101. data/spec/formatted_text_box_spec.rb +31 -32
  102. data/spec/formatted_text_fragment_spec.rb +2 -2
  103. data/spec/graphics_spec.rb +63 -45
  104. data/spec/grid_spec.rb +24 -13
  105. data/spec/image_handler_spec.rb +54 -0
  106. data/spec/images_spec.rb +34 -21
  107. data/spec/inline_formatted_text_parser_spec.rb +69 -20
  108. data/spec/jpg_spec.rb +3 -3
  109. data/spec/line_wrap_spec.rb +25 -14
  110. data/spec/measurement_units_spec.rb +5 -5
  111. data/spec/outline_spec.rb +68 -64
  112. data/spec/png_spec.rb +15 -18
  113. data/spec/reference_spec.rb +2 -82
  114. data/spec/repeater_spec.rb +1 -1
  115. data/spec/security_spec.rb +41 -9
  116. data/spec/soft_mask_spec.rb +0 -40
  117. data/spec/span_spec.rb +6 -11
  118. data/spec/spec_helper.rb +20 -2
  119. data/spec/stamp_spec.rb +19 -20
  120. data/spec/stroke_styles_spec.rb +31 -13
  121. data/spec/table/span_dummy_spec.rb +17 -0
  122. data/spec/table_spec.rb +268 -43
  123. data/spec/text_at_spec.rb +13 -27
  124. data/spec/text_box_spec.rb +35 -30
  125. data/spec/text_spec.rb +56 -40
  126. data/spec/transparency_spec.rb +5 -5
  127. metadata +214 -217
  128. data/README.md +0 -98
  129. data/data/fonts/Action Man.dfont +0 -0
  130. data/data/fonts/Activa.ttf +0 -0
  131. data/data/fonts/Chalkboard.ttf +0 -0
  132. data/data/fonts/DejaVuSans.ttf +0 -0
  133. data/data/fonts/Dustismo_Roman.ttf +0 -0
  134. data/data/fonts/comicsans.ttf +0 -0
  135. data/data/fonts/gkai00mp.ttf +0 -0
  136. data/data/images/16bit.dat +0 -0
  137. data/data/images/barcode_issue.png +0 -0
  138. data/data/images/dice.dat +0 -0
  139. data/data/images/page_white_text.dat +0 -0
  140. data/data/images/rails.dat +0 -0
  141. data/data/images/rails.png +0 -0
  142. data/lib/prawn/compatibility.rb +0 -87
  143. data/lib/prawn/core.rb +0 -87
  144. data/lib/prawn/core/annotations.rb +0 -61
  145. data/lib/prawn/core/byte_string.rb +0 -9
  146. data/lib/prawn/core/destinations.rb +0 -90
  147. data/lib/prawn/core/document_state.rb +0 -79
  148. data/lib/prawn/core/literal_string.rb +0 -16
  149. data/lib/prawn/core/name_tree.rb +0 -177
  150. data/lib/prawn/core/object_store.rb +0 -320
  151. data/lib/prawn/core/page.rb +0 -212
  152. data/lib/prawn/core/pdf_object.rb +0 -125
  153. data/lib/prawn/core/reference.rb +0 -119
  154. data/lib/prawn/core/text.rb +0 -268
  155. data/lib/prawn/core/text/formatted/arranger.rb +0 -294
  156. data/lib/prawn/core/text/formatted/line_wrap.rb +0 -288
  157. data/lib/prawn/core/text/formatted/wrap.rb +0 -153
  158. data/lib/prawn/document/page_geometry.rb +0 -136
  159. data/lib/prawn/document/snapshot.rb +0 -89
  160. data/manual/manual/foreword.rb +0 -13
  161. data/manual/templates/full_template.rb +0 -23
  162. data/manual/templates/page_template.rb +0 -47
  163. data/manual/templates/templates.rb +0 -26
  164. data/manual/text/group.rb +0 -29
  165. data/spec/name_tree_spec.rb +0 -112
  166. data/spec/object_store_spec.rb +0 -170
  167. data/spec/pdf_object_spec.rb +0 -172
  168. data/spec/snapshot_spec.rb +0 -186
  169. data/spec/template_spec.rb +0 -351
@@ -5,7 +5,7 @@
5
5
  # This is free software. Please see the LICENSE and COPYING files for details.
6
6
  #
7
7
  module Prawn
8
- module Encoding
8
+ module Encoding # @private
9
9
  # Map between unicode and WinAnsiEnoding
10
10
  #
11
11
  class WinAnsi #:nodoc:
@@ -18,7 +18,7 @@ module Prawn
18
18
  .notdef .notdef .notdef .notdef
19
19
  .notdef .notdef .notdef .notdef
20
20
  .notdef .notdef .notdef .notdef
21
-
21
+
22
22
  space exclam quotedbl numbersign
23
23
  dollar percent ampersand quotesingle
24
24
  parenleft parenright asterisk plus
@@ -54,7 +54,7 @@ module Prawn
54
54
  quotedblright bullet endash emdash
55
55
  tilde trademark scaron guilsinglright
56
56
  oe .notdef zcaron ydieresis
57
-
57
+
58
58
  space exclamdown cent sterling
59
59
  currency yen brokenbar section
60
60
  dieresis copyright ordfeminine guillemotleft
@@ -81,8 +81,8 @@ module Prawn
81
81
  ocircumflex otilde odieresis divide
82
82
  oslash ugrave uacute ucircumflex
83
83
  udieresis yacute thorn ydieresis
84
- ]
85
-
84
+ ]
85
+
86
86
  def initialize
87
87
  @mapping_file = "#{Prawn::DATADIR}/encodings/win_ansi.txt"
88
88
  load_mapping if self.class.mapping.empty?
@@ -100,7 +100,7 @@ module Prawn
100
100
  # Replace anything else with an underscore
101
101
  self.class.mapping[codepoint] || 95
102
102
  end
103
-
103
+
104
104
  def self.mapping
105
105
  @mapping ||= {}
106
106
  end
@@ -108,10 +108,9 @@ module Prawn
108
108
  private
109
109
 
110
110
  def load_mapping
111
- RUBY_VERSION >= "1.9" ? mode = "r:BINARY" : mode = "r"
112
- File.open(@mapping_file, mode) do |f|
111
+ File.open(@mapping_file, "r:BINARY") do |f|
113
112
  f.each do |l|
114
- m, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
113
+ _, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
115
114
  self.class.mapping["0x#{unicode}".hex] = "0x#{single_byte}".hex if single_byte
116
115
  end
117
116
  end
@@ -8,29 +8,18 @@
8
8
  #
9
9
  module Prawn
10
10
  module Errors
11
-
12
- # This error is raised when Prawn::PdfObject() encounters a Ruby object it
13
- # cannot convert to PDF
14
- #
15
- FailedObjectConversion = Class.new(StandardError)
16
-
17
- # This error is raised when Document#page_layout is set to anything
18
- # other than :portrait or :landscape
19
- #
20
- InvalidPageLayout = Class.new(StandardError)
21
-
22
11
  # Raised when a table is spanned in an impossible way.
23
12
  #
24
13
  InvalidTableSpan = Class.new(StandardError)
25
-
26
- # This error is raised when a method requiring a current page is called
14
+
15
+ # This error is raised when a method requiring a current page is called
27
16
  # without being on a page.
28
17
  #
29
18
  NotOnPage = Class.new(StandardError)
30
-
31
- # This error is raised when Prawn cannot find a specified font
19
+
20
+ # This error is raised when Prawn cannot find a specified font
32
21
  #
33
- UnknownFont = Class.new(StandardError)
22
+ UnknownFont = Class.new(StandardError)
34
23
 
35
24
  # Raised when Prawn is asked to draw something into a too-small box
36
25
  #
@@ -39,18 +28,18 @@ module Prawn
39
28
  # Raised if group() is called with a block that is too big to be
40
29
  # rendered in the current context.
41
30
  #
42
- CannotGroup = Class.new(StandardError)
31
+ CannotGroup = Class.new(StandardError)
43
32
 
44
33
  # This error is raised when Prawn is being used on a M17N aware VM,
45
34
  # and the user attempts to add text that isn't compatible with UTF-8
46
35
  # to their document
47
36
  #
48
37
  IncompatibleStringEncoding = Class.new(StandardError)
49
-
38
+
50
39
  # This error is raised when Prawn encounters an unknown key in functions
51
40
  # that accept an options hash. This usually means there is a typo in your
52
41
  # code or that the option you are trying to use has a different name than
53
- # what you have specified.
42
+ # what you have specified.
54
43
  #
55
44
  UnknownOption = Class.new(StandardError)
56
45
 
@@ -70,30 +59,22 @@ module Prawn
70
59
  # This error is raised when an object is attempted to be
71
60
  # referenced by name, but no such name is associated with an object
72
61
  UndefinedObjectName = Class.new(StandardError)
73
-
62
+
74
63
  # This error is raised when a required option has not been set
75
64
  RequiredOption = Class.new(StandardError)
76
-
65
+
77
66
  # This error is raised when a requested outline item with a given title does not exist
78
67
  UnknownOutlineTitle = Class.new(StandardError)
79
68
 
80
69
  # This error is raised when a block is required, but not provided
81
70
  BlockRequired = Class.new(StandardError)
82
-
71
+
83
72
  # This error is rased when a graphics method is called with improper arguments
84
73
  InvalidGraphicsPath = Class.new(StandardError)
85
-
86
- # This error is raised when Prawn fails to load a template file
87
- #
88
- TemplateError = Class.new(StandardError)
89
-
90
- # This error is raise when trying to restore a graphic state that
91
- #
92
- EmptyGraphicStateStack = Class.new(StandardError)
93
74
 
94
75
  # Raised when unrecognized content is provided for a table cell.
95
76
  #
96
77
  UnrecognizedTableContent = Class.new(StandardError)
97
-
78
+
98
79
  end
99
- end
80
+ end
@@ -6,13 +6,16 @@
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
  #
9
- require "prawn/font/afm"
10
- require "prawn/font/ttf"
11
- require "prawn/font/dfont"
9
+ require_relative "font/afm"
10
+ require_relative "font/ttf"
11
+ require_relative "font/dfont"
12
+ require_relative "font_metric_cache"
12
13
 
13
14
  module Prawn
14
15
 
15
16
  class Document
17
+ # @group Stable API
18
+
16
19
  # Without arguments, this returns the currently selected font. Otherwise,
17
20
  # it sets the current font. When a block is used, the font is applied
18
21
  # transactionally and is rolled back when the block exits.
@@ -23,8 +26,8 @@ module Prawn
23
26
  # font "Times-Roman"
24
27
  # text "Now using Times-Roman"
25
28
  #
26
- # font("Chalkboard.ttf") do
27
- # text "Using TTF font from file Chalkboard.ttf"
29
+ # font("DejaVuSans.ttf") do
30
+ # text "Using TTF font from file DejaVuSans.ttf"
28
31
  # font "Courier", :style => :bold
29
32
  # text "You see this in bold Courier"
30
33
  # end
@@ -38,7 +41,7 @@ module Prawn
38
41
  #
39
42
  # If a ttf font is specified, the glyphs necessary to render your document
40
43
  # will be embedded in the rendered PDF. This should be your preferred option
41
- # in most cases. It will increase the size of the resulting file, but also
44
+ # in most cases. It will increase the size of the resulting file, but also
42
45
  # make it more portable.
43
46
  #
44
47
  # The options parameter is an optional hash providing size and style. To use
@@ -49,9 +52,9 @@ module Prawn
49
52
  return((defined?(@font) && @font) || font("Helvetica")) if name.nil?
50
53
 
51
54
  if state.pages.empty? && !state.page.in_stamp_stream?
52
- raise Prawn::Errors::NotOnPage
55
+ raise Prawn::Errors::NotOnPage
53
56
  end
54
-
57
+
55
58
  new_font = find_font(name.to_s, options)
56
59
 
57
60
  if block_given?
@@ -66,7 +69,10 @@ module Prawn
66
69
  @font
67
70
  end
68
71
 
72
+ # @method font_size(points=nil)
73
+ #
69
74
  # When called with no argument, returns the current font size.
75
+ #
70
76
  # When called with a single argument but no block, sets the current font
71
77
  # size. When a block is used, the font size is applied transactionally and
72
78
  # is rolled back when the block exits. You may still change the font size
@@ -97,59 +103,44 @@ module Prawn
97
103
  @font_size = size_before_yield
98
104
  end
99
105
 
100
- # Sets the font directly, given an actual Font object
101
- # and size.
102
- #
103
- def set_font(font, size=nil) # :nodoc:
104
- @font = font
105
- @font_size = size if size
106
+ # Sets the font size
107
+ def font_size=(size)
108
+ font_size(size)
106
109
  end
107
110
 
108
- # Saves the current font, and then yields. When the block
109
- # finishes, the original font is restored.
111
+ # Returns the width of the given string using the given font. If :size is not
112
+ # specified as one of the options, the string is measured using the current
113
+ # font size. You can also pass :kerning as an option to indicate whether
114
+ # kerning should be used when measuring the width (defaults to +false+).
110
115
  #
111
- def save_font
112
- @font ||= find_font("Helvetica")
113
- original_font = @font
114
- original_size = @font_size
115
-
116
- yield
117
- ensure
118
- set_font(original_font, original_size) if original_font
119
- end
120
-
121
- # Looks up the given font using the given criteria. Once a font has been
122
- # found by that matches the criteria, it will be cached to subsequent lookups
123
- # for that font will return the same object.
116
+ # Note that the string _must_ be encoded properly for the font being used.
117
+ # For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
118
+ # UTF-8. You can use the Font#normalize_encoding method to make sure strings
119
+ # are in an encoding appropriate for the current font.
124
120
  #--
125
- # Challenges involved: the name alone is not sufficient to uniquely identify
126
- # a font (think dfont suitcases that can hold multiple different fonts in a
127
- # single file). Thus, the :name key is included in the cache key.
128
- #
129
- # It is further complicated, however, since fonts in some formats (like the
130
- # dfont suitcases) can be identified either by numeric index, OR by their
131
- # name within the suitcase, and both should hash to the same font object
132
- # (to avoid the font being embedded multiple times). This is not yet implemented,
133
- # which means if someone selects a font both by name, and by index, the
134
- # font will be embedded twice. Since we do font subsetting, this double
135
- # embedding won't be catastrophic, just annoying.
136
- # ++
137
- def find_font(name, options={}) #:nodoc:
138
- if font_families.key?(name)
139
- family, name = name, font_families[name][options[:style] || :normal]
140
- if name.is_a?(Hash)
141
- options = options.merge(name)
142
- name = options[:file]
143
- end
144
- end
145
- key = "#{name}:#{options[:font] || 0}"
146
- font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
147
- end
148
-
149
- # Hash of Font objects keyed by names
121
+ # For the record, this method used to be a method of Font (and still delegates
122
+ # to width computations on Font). However, having the primary interface for
123
+ # calculating string widths exist on Font made it tricky to write extensions
124
+ # for Prawn in which widths are computed differently (e.g., taking formatting
125
+ # tags into account, or the like).
150
126
  #
151
- def font_registry #:nodoc:
152
- @font_registry ||= {}
127
+ # By putting width_of here, on Document itself, extensions may easily override
128
+ # it and redefine the width calculation behavior.
129
+ #++
130
+ def width_of(string, options={})
131
+ if p = options[:inline_format]
132
+ p = [] unless p.is_a?(Array)
133
+
134
+ # Build up an Arranger with the entire string on one line, finalize it,
135
+ # and find its width.
136
+ arranger = Prawn::Text::Formatted::Arranger.new(self, options)
137
+ arranger.consumed = self.text_formatter.format(string, *p)
138
+ arranger.finalize_line
139
+
140
+ arranger.line_width
141
+ else
142
+ width_of_string(string, options)
143
+ end
153
144
  end
154
145
 
155
146
  # Hash that maps font family names to their styled individual font names.
@@ -173,13 +164,12 @@ module Prawn
173
164
  # wish to support.
174
165
  #
175
166
  # By default the styles :bold, :italic, :bold_italic, and :normal are
176
- # defined for fonts "Courier", "Times-Roman" and "Helvetica".
177
- #
178
- # You probably want to provide those four styles, but are free to define
179
- # custom ones, like :thin, and use them in font calls.
167
+ # defined for fonts "Courier", "Times-Roman" and "Helvetica". When
168
+ # defining your own font families, you can map any or all of these
169
+ # styles to whatever font files you'd like.
180
170
  #
181
171
  def font_families
182
- @font_families ||= Hash.new.merge!(
172
+ @font_families ||= {}.merge!(
183
173
  { "Courier" => { :bold => "Courier-Bold",
184
174
  :italic => "Courier-Oblique",
185
175
  :bold_italic => "Courier-BoldOblique",
@@ -197,45 +187,79 @@ module Prawn
197
187
  })
198
188
  end
199
189
 
200
- # Returns the width of the given string using the given font. If :size is not
201
- # specified as one of the options, the string is measured using the current
202
- # font size. You can also pass :kerning as an option to indicate whether
203
- # kerning should be used when measuring the width (defaults to +false+).
190
+ # @group Experimental API
191
+
192
+ # Sets the font directly, given an actual Font object
193
+ # and size.
204
194
  #
205
- # Note that the string _must_ be encoded properly for the font being used.
206
- # For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
207
- # UTF-8. You can use the Font#normalize_encoding method to make sure strings
208
- # are in an encoding appropriate for the current font.
209
- #--
210
- # For the record, this method used to be a method of Font (and still delegates
211
- # to width computations on Font). However, having the primary interface for
212
- # calculating string widths exist on Font made it tricky to write extensions
213
- # for Prawn in which widths are computed differently (e.g., taking formatting
214
- # tags into account, or the like).
195
+ def set_font(font, size=nil) # :nodoc:
196
+ @font = font
197
+ @font_size = size if size
198
+ end
199
+
200
+ # Saves the current font, and then yields. When the block
201
+ # finishes, the original font is restored.
215
202
  #
216
- # By putting width_of here, on Document itself, extensions may easily override
217
- # it and redefine the width calculation behavior.
218
- #++
219
- def width_of(string, options={})
220
- if options[:inline_format]
221
- # Build up an Arranger with the entire string on one line, finalize it,
222
- # and find its width.
223
- arranger = Core::Text::Formatted::Arranger.new(self, options)
224
- arranger.consumed = Text::Formatted::Parser.to_array(string)
225
- arranger.finalize_line
203
+ def save_font
204
+ @font ||= find_font("Helvetica")
205
+ original_font = @font
206
+ original_size = @font_size
226
207
 
227
- arranger.line_width
228
- else
229
- f = if options[:style]
230
- # override style with :style => :bold
231
- find_font(@font ? @font.name : 'Helvetica',
232
- :style => options[:style])
233
- else
234
- font
235
- end
236
- f.compute_width_of(string, options) +
237
- (character_spacing * font.character_count(string))
208
+ yield
209
+ ensure
210
+ set_font(original_font, original_size) if original_font
211
+ end
212
+
213
+ # Looks up the given font using the given criteria. Once a font has been
214
+ # found by that matches the criteria, it will be cached to subsequent lookups
215
+ # for that font will return the same object.
216
+ #--
217
+ # Challenges involved: the name alone is not sufficient to uniquely identify
218
+ # a font (think dfont suitcases that can hold multiple different fonts in a
219
+ # single file). Thus, the :name key is included in the cache key.
220
+ #
221
+ # It is further complicated, however, since fonts in some formats (like the
222
+ # dfont suitcases) can be identified either by numeric index, OR by their
223
+ # name within the suitcase, and both should hash to the same font object
224
+ # (to avoid the font being embedded multiple times). This is not yet implemented,
225
+ # which means if someone selects a font both by name, and by index, the
226
+ # font will be embedded twice. Since we do font subsetting, this double
227
+ # embedding won't be catastrophic, just annoying.
228
+ # ++
229
+ #
230
+ # @private
231
+ def find_font(name, options={}) #:nodoc:
232
+ if font_families.key?(name)
233
+ family, name = name, font_families[name][options[:style] || :normal]
234
+ if name.is_a?(::Hash)
235
+ options = options.merge(name)
236
+ name = options[:file]
237
+ end
238
238
  end
239
+ key = "#{name}:#{options[:font] || 0}"
240
+ font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
241
+ end
242
+
243
+ # Hash of Font objects keyed by names
244
+ #
245
+ def font_registry #:nodoc:
246
+ @font_registry ||= {}
247
+ end
248
+
249
+ private
250
+
251
+ def width_of_inline_formatted_string(string, options={})
252
+ # Build up an Arranger with the entire string on one line, finalize it,
253
+ # and find its width.
254
+ arranger = Prawn::Text::Formatted::Arranger.new(self, options)
255
+ arranger.consumed = Text::Formatted::Parser.format(string)
256
+ arranger.finalize_line
257
+
258
+ arranger.line_width
259
+ end
260
+
261
+ def width_of_string(string, options={})
262
+ font_metric_cache.width_of( string, options )
239
263
  end
240
264
  end
241
265
 
@@ -277,7 +301,7 @@ module Prawn
277
301
  @references = {}
278
302
  end
279
303
 
280
- # The size of the font ascender in PDF points
304
+ # The size of the font ascender in PDF points
281
305
  #
282
306
  def ascender
283
307
  @ascender / 1000.0 * size
@@ -295,14 +319,6 @@ module Prawn
295
319
  @line_gap / 1000.0 * size
296
320
  end
297
321
 
298
- def identifier_for(subset)
299
- "#{@identifier}.#{subset}".to_sym
300
- end
301
-
302
- def inspect
303
- "#{self.class.name}< #{name}: #{size} >"
304
- end
305
-
306
322
  # Normalizes the encoding of the string to an encoding supported by the
307
323
  # font. The string is expected to be UTF-8 going in. It will be re-encoded
308
324
  # and the new string will be returned. For an in-place (destructive)
@@ -348,6 +364,22 @@ module Prawn
348
364
  "#{self.class.name}< #{name}: #{size} >"
349
365
  end
350
366
 
367
+ # Return a hash (as in Object#hash) for the font based on the output of
368
+ # #inspect. This is required since font objects are used as keys in hashes
369
+ # that cache certain values (See
370
+ # Prawn::Table::Text#styled_with_of_single_character)
371
+ #
372
+ def hash #:nodoc:
373
+ [ self.class, self.name, self.family, size ].hash
374
+ end
375
+
376
+ # Compliments the #hash implementation above
377
+ #
378
+ def eql?( other ) #:nodoc:
379
+ self.class == other.class && self.name == other.name &&
380
+ self.family == other.family && size == other.send(:size)
381
+ end
382
+
351
383
  private
352
384
 
353
385
  # generate a font identifier that hasn't been used on the curretn page yet