nurettin-prawn 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/COPYING +2 -0
  2. data/GPLv2 +340 -0
  3. data/GPLv3 +674 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +56 -0
  6. data/README.md +98 -0
  7. data/Rakefile +45 -0
  8. data/data/encodings/win_ansi.txt +29 -0
  9. data/data/fonts/Action Man.dfont +0 -0
  10. data/data/fonts/Activa.ttf +0 -0
  11. data/data/fonts/Chalkboard.ttf +0 -0
  12. data/data/fonts/Courier-Bold.afm +342 -0
  13. data/data/fonts/Courier-BoldOblique.afm +342 -0
  14. data/data/fonts/Courier-Oblique.afm +342 -0
  15. data/data/fonts/Courier.afm +342 -0
  16. data/data/fonts/DejaVuSans.ttf +0 -0
  17. data/data/fonts/Dustismo_Roman.ttf +0 -0
  18. data/data/fonts/Helvetica-Bold.afm +2827 -0
  19. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  20. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  21. data/data/fonts/Helvetica.afm +3051 -0
  22. data/data/fonts/MustRead.html +19 -0
  23. data/data/fonts/Symbol.afm +213 -0
  24. data/data/fonts/Times-Bold.afm +2588 -0
  25. data/data/fonts/Times-BoldItalic.afm +2384 -0
  26. data/data/fonts/Times-Italic.afm +2667 -0
  27. data/data/fonts/Times-Roman.afm +2419 -0
  28. data/data/fonts/ZapfDingbats.afm +225 -0
  29. data/data/fonts/comicsans.ttf +0 -0
  30. data/data/fonts/gkai00mp.ttf +0 -0
  31. data/data/images/16bit.alpha +0 -0
  32. data/data/images/16bit.dat +0 -0
  33. data/data/images/16bit.png +0 -0
  34. data/data/images/arrow.png +0 -0
  35. data/data/images/arrow2.png +0 -0
  36. data/data/images/barcode_issue.png +0 -0
  37. data/data/images/dice.alpha +0 -0
  38. data/data/images/dice.dat +0 -0
  39. data/data/images/dice.png +0 -0
  40. data/data/images/dice_interlaced.png +0 -0
  41. data/data/images/fractal.jpg +0 -0
  42. data/data/images/letterhead.jpg +0 -0
  43. data/data/images/page_white_text.alpha +0 -0
  44. data/data/images/page_white_text.dat +0 -0
  45. data/data/images/page_white_text.png +0 -0
  46. data/data/images/pigs.jpg +0 -0
  47. data/data/images/prawn.png +0 -0
  48. data/data/images/rails.dat +0 -0
  49. data/data/images/rails.png +0 -0
  50. data/data/images/ruport.png +0 -0
  51. data/data/images/ruport_data.dat +0 -0
  52. data/data/images/ruport_transparent.png +0 -0
  53. data/data/images/ruport_type0.png +0 -0
  54. data/data/images/stef.jpg +0 -0
  55. data/data/images/tru256.bmp +0 -0
  56. data/data/images/web-links.dat +1 -0
  57. data/data/images/web-links.png +0 -0
  58. data/data/pdfs/complex_template.pdf +0 -0
  59. data/data/pdfs/contains_ttf_font.pdf +0 -0
  60. data/data/pdfs/encrypted.pdf +0 -0
  61. data/data/pdfs/form.pdf +820 -0
  62. data/data/pdfs/hexagon.pdf +61 -0
  63. data/data/pdfs/indirect_reference.pdf +86 -0
  64. data/data/pdfs/multipage_template.pdf +127 -0
  65. data/data/pdfs/nested_pages.pdf +118 -0
  66. data/data/pdfs/page_without_mediabox.pdf +193 -0
  67. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  68. data/data/pdfs/two_hexagons.pdf +90 -0
  69. data/data/pdfs/version_1_6.pdf +61 -0
  70. data/data/shift_jis_text.txt +1 -0
  71. data/lib/prawn.rb +29 -0
  72. data/lib/prawn/compatibility.rb +87 -0
  73. data/lib/prawn/core.rb +87 -0
  74. data/lib/prawn/core/annotations.rb +61 -0
  75. data/lib/prawn/core/byte_string.rb +9 -0
  76. data/lib/prawn/core/destinations.rb +90 -0
  77. data/lib/prawn/core/document_state.rb +79 -0
  78. data/lib/prawn/core/literal_string.rb +16 -0
  79. data/lib/prawn/core/name_tree.rb +177 -0
  80. data/lib/prawn/core/object_store.rb +320 -0
  81. data/lib/prawn/core/page.rb +212 -0
  82. data/lib/prawn/core/pdf_object.rb +125 -0
  83. data/lib/prawn/core/reference.rb +119 -0
  84. data/lib/prawn/core/text.rb +268 -0
  85. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  86. data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
  87. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  88. data/lib/prawn/document.rb +707 -0
  89. data/lib/prawn/document/bounding_box.rb +510 -0
  90. data/lib/prawn/document/column_box.rb +132 -0
  91. data/lib/prawn/document/graphics_state.rb +136 -0
  92. data/lib/prawn/document/internals.rb +173 -0
  93. data/lib/prawn/document/page_geometry.rb +136 -0
  94. data/lib/prawn/document/snapshot.rb +89 -0
  95. data/lib/prawn/document/span.rb +55 -0
  96. data/lib/prawn/encoding.rb +121 -0
  97. data/lib/prawn/errors.rb +99 -0
  98. data/lib/prawn/font.rb +386 -0
  99. data/lib/prawn/font/afm.rb +203 -0
  100. data/lib/prawn/font/dfont.rb +42 -0
  101. data/lib/prawn/font/ttf.rb +343 -0
  102. data/lib/prawn/graphics.rb +523 -0
  103. data/lib/prawn/graphics/cap_style.rb +46 -0
  104. data/lib/prawn/graphics/color.rb +231 -0
  105. data/lib/prawn/graphics/dash.rb +82 -0
  106. data/lib/prawn/graphics/join_style.rb +47 -0
  107. data/lib/prawn/graphics/patterns.rb +137 -0
  108. data/lib/prawn/graphics/transformation.rb +156 -0
  109. data/lib/prawn/graphics/transparency.rb +99 -0
  110. data/lib/prawn/images.rb +196 -0
  111. data/lib/prawn/images/image.rb +65 -0
  112. data/lib/prawn/images/jpg.rb +85 -0
  113. data/lib/prawn/images/png.rb +362 -0
  114. data/lib/prawn/layout.rb +20 -0
  115. data/lib/prawn/layout/grid.rb +259 -0
  116. data/lib/prawn/measurement_extensions.rb +46 -0
  117. data/lib/prawn/measurements.rb +71 -0
  118. data/lib/prawn/outline.rb +326 -0
  119. data/lib/prawn/repeater.rb +122 -0
  120. data/lib/prawn/security.rb +269 -0
  121. data/lib/prawn/soft_mask.rb +94 -0
  122. data/lib/prawn/stamp.rb +134 -0
  123. data/lib/prawn/table.rb +609 -0
  124. data/lib/prawn/table/cell.rb +776 -0
  125. data/lib/prawn/table/cell/image.rb +70 -0
  126. data/lib/prawn/table/cell/in_table.rb +27 -0
  127. data/lib/prawn/table/cell/span_dummy.rb +88 -0
  128. data/lib/prawn/table/cell/subtable.rb +65 -0
  129. data/lib/prawn/table/cell/text.rb +152 -0
  130. data/lib/prawn/table/cells.rb +260 -0
  131. data/lib/prawn/text.rb +420 -0
  132. data/lib/prawn/text/box.rb +141 -0
  133. data/lib/prawn/text/formatted.rb +4 -0
  134. data/lib/prawn/text/formatted/box.rb +563 -0
  135. data/lib/prawn/text/formatted/fragment.rb +253 -0
  136. data/lib/prawn/text/formatted/parser.rb +217 -0
  137. data/lib/prawn/utilities.rb +44 -0
  138. data/manual/basic_concepts/adding_pages.rb +27 -0
  139. data/manual/basic_concepts/basic_concepts.rb +34 -0
  140. data/manual/basic_concepts/creation.rb +39 -0
  141. data/manual/basic_concepts/cursor.rb +33 -0
  142. data/manual/basic_concepts/measurement.rb +25 -0
  143. data/manual/basic_concepts/origin.rb +38 -0
  144. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  145. data/manual/bounding_box/bounding_box.rb +39 -0
  146. data/manual/bounding_box/bounds.rb +49 -0
  147. data/manual/bounding_box/canvas.rb +24 -0
  148. data/manual/bounding_box/creation.rb +23 -0
  149. data/manual/bounding_box/indentation.rb +46 -0
  150. data/manual/bounding_box/nesting.rb +45 -0
  151. data/manual/bounding_box/russian_boxes.rb +40 -0
  152. data/manual/bounding_box/stretchy.rb +31 -0
  153. data/manual/document_and_page_options/background.rb +27 -0
  154. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  155. data/manual/document_and_page_options/metadata.rb +23 -0
  156. data/manual/document_and_page_options/page_margins.rb +38 -0
  157. data/manual/document_and_page_options/page_size.rb +34 -0
  158. data/manual/example_file.rb +116 -0
  159. data/manual/example_helper.rb +430 -0
  160. data/manual/example_package.rb +53 -0
  161. data/manual/example_section.rb +46 -0
  162. data/manual/graphics/circle_and_ellipse.rb +22 -0
  163. data/manual/graphics/color.rb +24 -0
  164. data/manual/graphics/common_lines.rb +28 -0
  165. data/manual/graphics/fill_and_stroke.rb +42 -0
  166. data/manual/graphics/fill_rules.rb +37 -0
  167. data/manual/graphics/gradients.rb +37 -0
  168. data/manual/graphics/graphics.rb +58 -0
  169. data/manual/graphics/helper.rb +17 -0
  170. data/manual/graphics/line_width.rb +35 -0
  171. data/manual/graphics/lines_and_curves.rb +41 -0
  172. data/manual/graphics/polygon.rb +29 -0
  173. data/manual/graphics/rectangle.rb +21 -0
  174. data/manual/graphics/rotate.rb +28 -0
  175. data/manual/graphics/scale.rb +41 -0
  176. data/manual/graphics/soft_masks.rb +46 -0
  177. data/manual/graphics/stroke_cap.rb +31 -0
  178. data/manual/graphics/stroke_dash.rb +43 -0
  179. data/manual/graphics/stroke_join.rb +30 -0
  180. data/manual/graphics/translate.rb +29 -0
  181. data/manual/graphics/transparency.rb +35 -0
  182. data/manual/images/absolute_position.rb +23 -0
  183. data/manual/images/fit.rb +21 -0
  184. data/manual/images/horizontal.rb +25 -0
  185. data/manual/images/images.rb +40 -0
  186. data/manual/images/plain_image.rb +18 -0
  187. data/manual/images/scale.rb +22 -0
  188. data/manual/images/vertical.rb +28 -0
  189. data/manual/images/width_and_height.rb +25 -0
  190. data/manual/layout/boxes.rb +27 -0
  191. data/manual/layout/content.rb +25 -0
  192. data/manual/layout/layout.rb +28 -0
  193. data/manual/layout/simple_grid.rb +23 -0
  194. data/manual/manual/cover.rb +26 -0
  195. data/manual/manual/foreword.rb +13 -0
  196. data/manual/manual/how_to_read_this_manual.rb +41 -0
  197. data/manual/manual/manual.rb +36 -0
  198. data/manual/outline/add_subsection_to.rb +61 -0
  199. data/manual/outline/insert_section_after.rb +47 -0
  200. data/manual/outline/outline.rb +32 -0
  201. data/manual/outline/sections_and_pages.rb +67 -0
  202. data/manual/repeatable_content/page_numbering.rb +54 -0
  203. data/manual/repeatable_content/repeatable_content.rb +31 -0
  204. data/manual/repeatable_content/repeater.rb +55 -0
  205. data/manual/repeatable_content/stamp.rb +41 -0
  206. data/manual/security/encryption.rb +31 -0
  207. data/manual/security/permissions.rb +38 -0
  208. data/manual/security/security.rb +28 -0
  209. data/manual/syntax_highlight.rb +52 -0
  210. data/manual/table/basic_block.rb +53 -0
  211. data/manual/table/before_rendering_page.rb +26 -0
  212. data/manual/table/cell_border_lines.rb +24 -0
  213. data/manual/table/cell_borders_and_bg.rb +31 -0
  214. data/manual/table/cell_dimensions.rb +30 -0
  215. data/manual/table/cell_text.rb +38 -0
  216. data/manual/table/column_widths.rb +30 -0
  217. data/manual/table/content_and_subtables.rb +39 -0
  218. data/manual/table/creation.rb +27 -0
  219. data/manual/table/filtering.rb +36 -0
  220. data/manual/table/flow_and_header.rb +17 -0
  221. data/manual/table/image_cells.rb +33 -0
  222. data/manual/table/position.rb +29 -0
  223. data/manual/table/row_colors.rb +20 -0
  224. data/manual/table/span.rb +30 -0
  225. data/manual/table/style.rb +22 -0
  226. data/manual/table/table.rb +52 -0
  227. data/manual/table/width.rb +27 -0
  228. data/manual/templates/full_template.rb +23 -0
  229. data/manual/templates/page_template.rb +47 -0
  230. data/manual/templates/templates.rb +26 -0
  231. data/manual/text/alignment.rb +44 -0
  232. data/manual/text/color.rb +24 -0
  233. data/manual/text/column_box.rb +32 -0
  234. data/manual/text/fallback_fonts.rb +37 -0
  235. data/manual/text/font.rb +41 -0
  236. data/manual/text/font_size.rb +45 -0
  237. data/manual/text/font_style.rb +23 -0
  238. data/manual/text/formatted_callbacks.rb +60 -0
  239. data/manual/text/formatted_text.rb +50 -0
  240. data/manual/text/free_flowing_text.rb +51 -0
  241. data/manual/text/group.rb +29 -0
  242. data/manual/text/inline.rb +43 -0
  243. data/manual/text/kerning_and_character_spacing.rb +39 -0
  244. data/manual/text/leading.rb +25 -0
  245. data/manual/text/line_wrapping.rb +41 -0
  246. data/manual/text/paragraph_indentation.rb +26 -0
  247. data/manual/text/positioned_text.rb +38 -0
  248. data/manual/text/registering_families.rb +48 -0
  249. data/manual/text/rendering_and_color.rb +37 -0
  250. data/manual/text/right_to_left_text.rb +43 -0
  251. data/manual/text/rotation.rb +43 -0
  252. data/manual/text/single_usage.rb +37 -0
  253. data/manual/text/text.rb +75 -0
  254. data/manual/text/text_box_excess.rb +32 -0
  255. data/manual/text/text_box_extensions.rb +45 -0
  256. data/manual/text/text_box_overflow.rb +44 -0
  257. data/manual/text/utf8.rb +28 -0
  258. data/manual/text/win_ansi_charset.rb +59 -0
  259. data/prawn.gemspec +46 -0
  260. data/spec/annotations_spec.rb +90 -0
  261. data/spec/bounding_box_spec.rb +493 -0
  262. data/spec/cell_spec.rb +584 -0
  263. data/spec/column_box_spec.rb +33 -0
  264. data/spec/data/curves.pdf +66 -0
  265. data/spec/destinations_spec.rb +15 -0
  266. data/spec/document_spec.rb +736 -0
  267. data/spec/extensions/encoding_helpers.rb +6 -0
  268. data/spec/extensions/mocha.rb +44 -0
  269. data/spec/font_spec.rb +433 -0
  270. data/spec/formatted_text_arranger_spec.rb +421 -0
  271. data/spec/formatted_text_box_spec.rb +640 -0
  272. data/spec/formatted_text_fragment_spec.rb +298 -0
  273. data/spec/graphics_spec.rb +651 -0
  274. data/spec/grid_spec.rb +85 -0
  275. data/spec/images_spec.rb +140 -0
  276. data/spec/inline_formatted_text_parser_spec.rb +515 -0
  277. data/spec/jpg_spec.rb +25 -0
  278. data/spec/line_wrap_spec.rb +333 -0
  279. data/spec/measurement_units_spec.rb +23 -0
  280. data/spec/name_tree_spec.rb +112 -0
  281. data/spec/object_store_spec.rb +170 -0
  282. data/spec/outline_spec.rb +426 -0
  283. data/spec/pdf_object_spec.rb +172 -0
  284. data/spec/png_spec.rb +240 -0
  285. data/spec/reference_spec.rb +105 -0
  286. data/spec/repeater_spec.rb +158 -0
  287. data/spec/security_spec.rb +126 -0
  288. data/spec/snapshot_spec.rb +186 -0
  289. data/spec/soft_mask_spec.rb +117 -0
  290. data/spec/span_spec.rb +49 -0
  291. data/spec/spec_helper.rb +36 -0
  292. data/spec/stamp_spec.rb +159 -0
  293. data/spec/stroke_styles_spec.rb +193 -0
  294. data/spec/table_spec.rb +1209 -0
  295. data/spec/template_spec.rb +351 -0
  296. data/spec/text_at_spec.rb +129 -0
  297. data/spec/text_box_spec.rb +1029 -0
  298. data/spec/text_rendering_mode_spec.rb +45 -0
  299. data/spec/text_spacing_spec.rb +93 -0
  300. data/spec/text_spec.rb +421 -0
  301. data/spec/text_with_inline_formatting_spec.rb +35 -0
  302. data/spec/transparency_spec.rb +89 -0
  303. metadata +544 -0
@@ -0,0 +1,6 @@
1
+ module EncodingHelpers
2
+ def win1252_string(str)
3
+ ruby_19 { str.force_encoding("Windows-1252") }
4
+ str
5
+ end
6
+ end
@@ -0,0 +1,44 @@
1
+ # Allow speccing things when an expectation matcher runs. Similar to #with, but
2
+ # always succeeds.
3
+ #
4
+ # @pdf.expects(:stroke_line).checking do |from, to|
5
+ # @pdf.map_to_absolute(from).should == [0, 0]
6
+ # end
7
+ #
8
+ # Note that the outer expectation does *not* fail only because the inner one
9
+ # does; in the above example, the outer expectation would only fail if
10
+ # stroke_line were not called.
11
+
12
+ class ParameterChecker < Mocha::ParametersMatcher
13
+ def initialize(&matching_block)
14
+ @expected_parameters = [Mocha::ParameterMatchers::AnyParameters.new]
15
+ @matching_block = matching_block
16
+ end
17
+
18
+ def match?(actual_parameters = [])
19
+ @matching_block.call(*actual_parameters)
20
+
21
+ true # always succeed
22
+ end
23
+ end
24
+
25
+ class Mocha::Expectation
26
+ def checking(&block)
27
+ @parameters_matcher = ParameterChecker.new(&block)
28
+ self
29
+ end
30
+ end
31
+
32
+
33
+ # Equivalent to expects(method_name).at_least(0). More useful when combined
34
+ # with parameter matchers to ignore certain calls for the sake of parameter
35
+ # matching.
36
+ #
37
+ # @pdf.ignores(:stroke_color=).with("000000")
38
+ # @pdf.expects(:stroke_color=).with("ff0000")
39
+ #
40
+ module Mocha::ObjectMethods
41
+ def ignores(method_name)
42
+ expects(method_name).at_least(0)
43
+ end
44
+ end
@@ -0,0 +1,433 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+ require 'pathname'
5
+
6
+ describe "Font behavior" do
7
+
8
+ it "should default to Helvetica if no font is specified" do
9
+ @pdf = Prawn::Document.new
10
+ @pdf.font.name.should == "Helvetica"
11
+ end
12
+
13
+ end
14
+
15
+ describe "#width_of" do
16
+ it "should take character spacing into account" do
17
+ create_pdf
18
+ original_width = @pdf.width_of("hello world")
19
+ @pdf.character_spacing(7) do
20
+ @pdf.width_of("hello world").should == original_width + 11 * 7
21
+ end
22
+ end
23
+
24
+ it "should exclude newlines" do
25
+ create_pdf
26
+ # Use a TTF font that has a non-zero width for \n
27
+ @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf")
28
+
29
+ @pdf.width_of("\nhello world\n").should ==
30
+ @pdf.width_of("hello world")
31
+ end
32
+
33
+ it "should take formatting into account" do
34
+ create_pdf
35
+
36
+ normal_hello = @pdf.width_of("hello")
37
+ inline_bold_hello = @pdf.width_of("<b>hello</b>", :inline_format => true)
38
+ @pdf.font("Helvetica", :style => :bold) {
39
+ @bold_hello = @pdf.width_of("hello")
40
+ }
41
+
42
+ inline_bold_hello.should be > normal_hello
43
+ inline_bold_hello.should == @bold_hello
44
+ end
45
+
46
+ it "should accept :style as an argument" do
47
+ create_pdf
48
+
49
+ styled_bold_hello = @pdf.width_of("hello", :style => :bold)
50
+ @pdf.font("Helvetica", :style => :bold) {
51
+ @bold_hello = @pdf.width_of("hello")
52
+ }
53
+
54
+ styled_bold_hello.should == @bold_hello
55
+ end
56
+ end
57
+
58
+ describe "#font_size" do
59
+ it "should allow setting font size in DSL style" do
60
+ create_pdf
61
+ @pdf.font_size 20
62
+ @pdf.font_size.should == 20
63
+ end
64
+ end
65
+
66
+ describe "font style support" do
67
+ before(:each) { create_pdf }
68
+
69
+ it "should complain if there is no @current_page" do
70
+ pdf_without_page = Prawn::Document.new(:skip_page_creation => true)
71
+
72
+ lambda{ pdf_without_page.font "Helvetica" }.
73
+ should raise_error(Prawn::Errors::NotOnPage)
74
+ end
75
+
76
+ it "should allow specifying font style by style name and font family" do
77
+ @pdf.font "Courier", :style => :bold
78
+ @pdf.text "In Courier bold"
79
+
80
+ @pdf.font "Courier", :style => :bold_italic
81
+ @pdf.text "In Courier bold-italic"
82
+
83
+ @pdf.font "Courier", :style => :italic
84
+ @pdf.text "In Courier italic"
85
+
86
+ @pdf.font "Courier", :style => :normal
87
+ @pdf.text "In Normal Courier"
88
+
89
+ @pdf.font "Helvetica"
90
+ @pdf.text "In Normal Helvetica"
91
+
92
+ text = PDF::Inspector::Text.analyze(@pdf.render)
93
+ text.font_settings.map { |e| e[:name] }.should ==
94
+ [:"Courier-Bold", :"Courier-BoldOblique", :"Courier-Oblique",
95
+ :Courier, :Helvetica]
96
+ end
97
+
98
+ it "should allow font familes to be defined in a single dfont" do
99
+ file = "#{Prawn::DATADIR}/fonts/Action Man.dfont"
100
+ @pdf.font_families["Action Man"] = {
101
+ :normal => { :file => file, :font => "ActionMan" },
102
+ :italic => { :file => file, :font => "ActionMan-Italic" },
103
+ :bold => { :file => file, :font => "ActionMan-Bold" },
104
+ :bold_italic => { :file => file, :font => "ActionMan-BoldItalic" }
105
+ }
106
+
107
+ @pdf.font "Action Man", :style => :italic
108
+ @pdf.text "In ActionMan-Italic"
109
+
110
+ text = PDF::Inspector::Text.analyze(@pdf.render)
111
+ name = text.font_settings.map { |e| e[:name] }.first.to_s
112
+ name = name.sub(/\w+\+/, "subset+")
113
+ name.should == "subset+ActionMan-Italic"
114
+ end
115
+
116
+ it "should accept Pathname objects for font files" do
117
+ file = Pathname.new( "#{Prawn::DATADIR}/fonts/Chalkboard.ttf" )
118
+ @pdf.font_families["Chalkboard"] = {
119
+ :normal => file
120
+ }
121
+
122
+ @pdf.font "Chalkboard"
123
+ @pdf.text "In Chalkboard"
124
+
125
+ text = PDF::Inspector::Text.analyze(@pdf.render)
126
+ name = text.font_settings.map { |e| e[:name] }.first.to_s
127
+ name = name.sub(/\w+\+/, "subset+")
128
+ name.should == "subset+Chalkboard"
129
+ end
130
+ end
131
+
132
+ describe "Transactional font handling" do
133
+ before(:each) { create_pdf }
134
+
135
+ it "should allow setting of size directly when font is created" do
136
+ @pdf.font "Courier", :size => 16
137
+ @pdf.font_size.should == 16
138
+ end
139
+
140
+ it "should allow temporary setting of a new font using a transaction" do
141
+ @pdf.font "Helvetica", :size => 12
142
+
143
+ @pdf.font "Courier", :size => 16 do
144
+ @pdf.font.name.should == "Courier"
145
+ @pdf.font_size.should == 16
146
+ end
147
+
148
+ @pdf.font.name.should == "Helvetica"
149
+ @pdf.font_size.should == 12
150
+ end
151
+
152
+ it "should mask font size when using a transacation" do
153
+ @pdf.font "Courier", :size => 16 do
154
+ @pdf.font_size.should == 16
155
+ end
156
+
157
+ @pdf.font "Times-Roman"
158
+ @pdf.font "Courier"
159
+
160
+ @pdf.font_size.should == 12
161
+ end
162
+
163
+ end
164
+
165
+ describe "Document#page_fonts" do
166
+ before(:each) { create_pdf }
167
+
168
+ it "should register fonts properly by page" do
169
+ @pdf.font "Courier"; @pdf.text("hello")
170
+ @pdf.font "Helvetica"; @pdf.text("hello")
171
+ @pdf.font "Times-Roman"; @pdf.text("hello")
172
+ ["Courier","Helvetica","Times-Roman"].each { |f|
173
+ page_should_include_font(f)
174
+ }
175
+
176
+ @pdf.start_new_page
177
+ @pdf.font "Helvetica"; @pdf.text("hello")
178
+ page_should_include_font("Helvetica")
179
+ page_should_not_include_font("Courier")
180
+ page_should_not_include_font("Times-Roman")
181
+ end
182
+
183
+ def page_includes_font?(font)
184
+ @pdf.page.fonts.values.map { |e| e.data[:BaseFont] }.include?(font.to_sym)
185
+ end
186
+
187
+ def page_should_include_font(font)
188
+ page_includes_font?(font).should be_true
189
+ end
190
+
191
+ def page_should_not_include_font(font)
192
+ page_includes_font?(font).should be_false
193
+ end
194
+
195
+ end
196
+
197
+ describe "AFM fonts" do
198
+
199
+ before do
200
+ create_pdf
201
+ @times = @pdf.find_font "Times-Roman"
202
+ end
203
+
204
+ it "should calculate string width taking into account accented characters" do
205
+ input = win1252_string("\xE9")# é in win-1252
206
+ @times.compute_width_of(input, :size => 12).should == @times.compute_width_of("e", :size => 12)
207
+ end
208
+
209
+ it "should calculate string width taking into account kerning pairs" do
210
+ @times.compute_width_of(win1252_string("To"), :size => 12).should == 13.332
211
+ @times.compute_width_of(win1252_string("To"), :size => 12, :kerning => true).should == 12.372
212
+
213
+ input = win1252_string("T\xF6") # Tö in win-1252
214
+ @times.compute_width_of(input, :size => 12, :kerning => true).should == 12.372
215
+ end
216
+
217
+ it "should encode text without kerning by default" do
218
+ @times.encode_text(win1252_string("To")).should == [[0, "To"]]
219
+ input = win1252_string("T\xE9l\xE9") # Télé in win-1252
220
+ @times.encode_text(input).should == [[0, input]]
221
+ @times.encode_text(win1252_string("Technology")).should == [[0, "Technology"]]
222
+ @times.encode_text(win1252_string("Technology...")).should == [[0, "Technology..."]]
223
+ end
224
+
225
+ it "should encode text with kerning if requested" do
226
+ @times.encode_text(win1252_string("To"), :kerning => true).should == [[0, ["T", 80, "o"]]]
227
+ input = win1252_string("T\xE9l\xE9") # Télé in win-1252
228
+ output = win1252_string("\xE9l\xE9") # élé in win-1252
229
+ @times.encode_text(input, :kerning => true).should == [[0, ["T", 70, output]]]
230
+ @times.encode_text(win1252_string("Technology"), :kerning => true).should == [[0, ["T", 70, "echnology"]]]
231
+ @times.encode_text(win1252_string("Technology..."), :kerning => true).should == [[0, ["T", 70, "echnology", 65, "..."]]]
232
+ end
233
+
234
+ describe "when normalizing encoding" do
235
+
236
+ it "should not modify the original string when normalize_encoding() is used" do
237
+ original = "Foo"
238
+ normalized = @times.normalize_encoding(original)
239
+ original.equal?(normalized).should be_false
240
+ end
241
+
242
+ it "should modify the original string when normalize_encoding!() is used" do
243
+ original = "Foo"
244
+ normalized = @times.normalize_encoding!(original)
245
+ original.equal?(normalized).should be_true
246
+ end
247
+
248
+ end
249
+
250
+ it "should omit /Encoding for symbolic fonts" do
251
+ zapf = @pdf.find_font "ZapfDingbats"
252
+ font_dict = zapf.send(:register, nil)
253
+ font_dict.data[:Encoding].should == nil
254
+ end
255
+
256
+ end
257
+
258
+ describe "#glyph_present" do
259
+ before(:each) { create_pdf }
260
+
261
+ it "should return true when present in an AFM font" do
262
+ font = @pdf.find_font("Helvetica")
263
+ font.glyph_present?("H").should be_true
264
+ end
265
+
266
+ it "should return false when absent in an AFM font" do
267
+ font = @pdf.find_font("Helvetica")
268
+ font.glyph_present?("再").should be_false
269
+ end
270
+
271
+ it "should return true when present in a TTF font" do
272
+ font = @pdf.find_font("#{Prawn::DATADIR}/fonts/Activa.ttf")
273
+ font.glyph_present?("H").should be_true
274
+ end
275
+
276
+ it "should return false when absent in a TTF font" do
277
+ font = @pdf.find_font("#{Prawn::DATADIR}/fonts/Activa.ttf")
278
+ font.glyph_present?("再").should be_false
279
+
280
+ font = @pdf.find_font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf")
281
+ font.glyph_present?("€").should be_false
282
+ end
283
+ end
284
+
285
+ describe "TTF fonts" do
286
+
287
+ before do
288
+ create_pdf
289
+ @activa = @pdf.find_font "#{Prawn::DATADIR}/fonts/Activa.ttf"
290
+ end
291
+
292
+ it "should calculate string width taking into account accented characters" do
293
+ @activa.compute_width_of("é", :size => 12).should == @activa.compute_width_of("e", :size => 12)
294
+ end
295
+
296
+ it "should calculate string width taking into account kerning pairs" do
297
+ @activa.compute_width_of("To", :size => 12).should == 15.228
298
+ @activa.compute_width_of("To", :size => 12, :kerning => true).should == 12.996
299
+ end
300
+
301
+ it "should encode text without kerning by default" do
302
+ @activa.encode_text("To").should == [[0, "To"]]
303
+
304
+ tele = "T\216l\216"
305
+ result = @activa.encode_text("Télé")
306
+ result.length.should == 1
307
+ result[0][0].should == 0
308
+ result[0][1].bytes.to_a.should == tele.bytes.to_a
309
+
310
+ @activa.encode_text("Technology").should == [[0, "Technology"]]
311
+ @activa.encode_text("Technology...").should == [[0, "Technology..."]]
312
+ @activa.encode_text("Teχnology...").should == [[0, "Te"], [1, "!"], [0, "nology..."]]
313
+ end
314
+
315
+ it "should encode text with kerning if requested" do
316
+ @activa.encode_text("To", :kerning => true).should == [[0, ["T", 186.0, "o"]]]
317
+ @activa.encode_text("To", :kerning => true).should == [[0, ["T", 186.0, "o"]]]
318
+ @activa.encode_text("Technology", :kerning => true).should == [[0, ["T", 186.0, "echnology"]]]
319
+ @activa.encode_text("Technology...", :kerning => true).should == [[0, ["T", 186.0, "echnology", 88.0, "..."]]]
320
+ @activa.encode_text("Teχnology...", :kerning => true).should == [[0, ["T", 186.0, "e"]], [1, "!"], [0, ["nology", 88.0, "..."]]]
321
+ end
322
+
323
+ it "should use the ascender, descender, and cap height from the TTF verbatim" do
324
+ # These metrics are relative to the font's own bbox. They should not be
325
+ # scaled with font size.
326
+ ref = @pdf.ref!({})
327
+ @activa.send :embed, ref, 0
328
+
329
+ # Pull out the embedded font descriptor
330
+ descriptor = ref.data[:FontDescriptor].data
331
+ descriptor[:Ascent].should == 804
332
+ descriptor[:Descent].should == -195
333
+ descriptor[:CapHeight].should == 804
334
+ end
335
+
336
+ describe "when normalizing encoding" do
337
+
338
+ it "should not modify the original string when normalize_encoding() is used" do
339
+ original = "Foo"
340
+ normalized = @activa.normalize_encoding(original)
341
+ original.equal?(normalized).should be_false
342
+ end
343
+
344
+ it "should modify the original string when normalize_encoding!() is used" do
345
+ original = "Foo"
346
+ normalized = @activa.normalize_encoding!(original)
347
+ original.equal?(normalized).should be_true
348
+ end
349
+
350
+ end
351
+
352
+ describe "when used with snapshots or transactions" do
353
+
354
+ it "should allow TTF fonts to be used alongside document transactions" do
355
+ lambda {
356
+ Prawn::Document.new do
357
+ font "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf"
358
+ text "Hi there"
359
+ transaction { text "Nice, thank you" }
360
+ end
361
+ }.should_not raise_error
362
+ end
363
+
364
+ it "should allow TTF fonts to be used inside transactions" do
365
+ pdf = Prawn::Document.new do
366
+ transaction do
367
+ font "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf"
368
+ text "Hi there"
369
+ end
370
+ end
371
+
372
+ text = PDF::Inspector::Text.analyze(pdf.render)
373
+ name = text.font_settings.map { |e| e[:name] }.first.to_s
374
+ name = name.sub(/\w+\+/, "subset+")
375
+ name.should == "subset+DejaVuSans"
376
+ end
377
+
378
+ end
379
+
380
+ end
381
+
382
+ describe "DFont fonts" do
383
+ before do
384
+ create_pdf
385
+ @file = "#{Prawn::DATADIR}/fonts/Action Man.dfont"
386
+ end
387
+
388
+ it "should list all named fonts" do
389
+ list = Prawn::Font::DFont.named_fonts(@file)
390
+ list.sort.should == %w(ActionMan ActionMan-Italic ActionMan-Bold ActionMan-BoldItalic).sort
391
+ end
392
+
393
+ it "should count the number of fonts in the file" do
394
+ Prawn::Font::DFont.font_count(@file).should == 4
395
+ end
396
+
397
+ it "should default selected font to the first one if not specified" do
398
+ font = @pdf.find_font(@file)
399
+ font.basename.should == "ActionMan"
400
+ end
401
+
402
+ it "should allow font to be selected by index" do
403
+ font = @pdf.find_font(@file, :font => 2)
404
+ font.basename.should == "ActionMan-Italic"
405
+ end
406
+
407
+ it "should allow font to be selected by name" do
408
+ font = @pdf.find_font(@file, :font => "ActionMan-BoldItalic")
409
+ font.basename.should == "ActionMan-BoldItalic"
410
+ end
411
+
412
+ it "should cache font object based on selected font" do
413
+ f1 = @pdf.find_font(@file, :font => "ActionMan")
414
+ f2 = @pdf.find_font(@file, :font => "ActionMan-Bold")
415
+ f2.object_id.should_not == f1.object_id
416
+ @pdf.find_font(@file, :font => "ActionMan").object_id.should == f1.object_id
417
+ @pdf.find_font(@file, :font => "ActionMan-Bold").object_id.should == f2.object_id
418
+ end
419
+ end
420
+
421
+ describe "#character_count(text)" do
422
+ it "should work on TTF fonts" do
423
+ create_pdf
424
+ @pdf.font("#{Prawn::DATADIR}/fonts/gkai00mp.ttf")
425
+ @pdf.font.character_count("こんにちは世界").should == 7
426
+ @pdf.font.character_count("Hello, world!").should == 13
427
+ end
428
+
429
+ it "should work on AFM fonts" do
430
+ create_pdf
431
+ @pdf.font.character_count("Hello, world!").should == 13
432
+ end
433
+ end