nurettin-prawn 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +2 -0
- data/GPLv2 +340 -0
- data/GPLv3 +674 -0
- data/Gemfile +18 -0
- data/LICENSE +56 -0
- data/README.md +98 -0
- data/Rakefile +45 -0
- data/data/encodings/win_ansi.txt +29 -0
- data/data/fonts/Action Man.dfont +0 -0
- data/data/fonts/Activa.ttf +0 -0
- data/data/fonts/Chalkboard.ttf +0 -0
- data/data/fonts/Courier-Bold.afm +342 -0
- data/data/fonts/Courier-BoldOblique.afm +342 -0
- data/data/fonts/Courier-Oblique.afm +342 -0
- data/data/fonts/Courier.afm +342 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/Helvetica-Bold.afm +2827 -0
- data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
- data/data/fonts/Helvetica-Oblique.afm +3051 -0
- data/data/fonts/Helvetica.afm +3051 -0
- data/data/fonts/MustRead.html +19 -0
- data/data/fonts/Symbol.afm +213 -0
- data/data/fonts/Times-Bold.afm +2588 -0
- data/data/fonts/Times-BoldItalic.afm +2384 -0
- data/data/fonts/Times-Italic.afm +2667 -0
- data/data/fonts/Times-Roman.afm +2419 -0
- data/data/fonts/ZapfDingbats.afm +225 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.dat +0 -0
- data/data/images/16bit.png +0 -0
- data/data/images/arrow.png +0 -0
- data/data/images/arrow2.png +0 -0
- data/data/images/barcode_issue.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/dice_interlaced.png +0 -0
- data/data/images/fractal.jpg +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/prawn.png +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/tru256.bmp +0 -0
- data/data/images/web-links.dat +1 -0
- data/data/images/web-links.png +0 -0
- data/data/pdfs/complex_template.pdf +0 -0
- data/data/pdfs/contains_ttf_font.pdf +0 -0
- data/data/pdfs/encrypted.pdf +0 -0
- data/data/pdfs/form.pdf +820 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/multipage_template.pdf +127 -0
- data/data/pdfs/nested_pages.pdf +118 -0
- data/data/pdfs/page_without_mediabox.pdf +193 -0
- data/data/pdfs/resources_as_indirect_object.pdf +83 -0
- data/data/pdfs/two_hexagons.pdf +90 -0
- data/data/pdfs/version_1_6.pdf +61 -0
- data/data/shift_jis_text.txt +1 -0
- data/lib/prawn.rb +29 -0
- data/lib/prawn/compatibility.rb +87 -0
- data/lib/prawn/core.rb +87 -0
- data/lib/prawn/core/annotations.rb +61 -0
- data/lib/prawn/core/byte_string.rb +9 -0
- data/lib/prawn/core/destinations.rb +90 -0
- data/lib/prawn/core/document_state.rb +79 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +177 -0
- data/lib/prawn/core/object_store.rb +320 -0
- data/lib/prawn/core/page.rb +212 -0
- data/lib/prawn/core/pdf_object.rb +125 -0
- data/lib/prawn/core/reference.rb +119 -0
- data/lib/prawn/core/text.rb +268 -0
- data/lib/prawn/core/text/formatted/arranger.rb +294 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
- data/lib/prawn/core/text/formatted/wrap.rb +153 -0
- data/lib/prawn/document.rb +707 -0
- data/lib/prawn/document/bounding_box.rb +510 -0
- data/lib/prawn/document/column_box.rb +132 -0
- data/lib/prawn/document/graphics_state.rb +136 -0
- data/lib/prawn/document/internals.rb +173 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +89 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +99 -0
- data/lib/prawn/font.rb +386 -0
- data/lib/prawn/font/afm.rb +203 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +343 -0
- data/lib/prawn/graphics.rb +523 -0
- data/lib/prawn/graphics/cap_style.rb +46 -0
- data/lib/prawn/graphics/color.rb +231 -0
- data/lib/prawn/graphics/dash.rb +82 -0
- data/lib/prawn/graphics/join_style.rb +47 -0
- data/lib/prawn/graphics/patterns.rb +137 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/images.rb +196 -0
- data/lib/prawn/images/image.rb +65 -0
- data/lib/prawn/images/jpg.rb +85 -0
- data/lib/prawn/images/png.rb +362 -0
- data/lib/prawn/layout.rb +20 -0
- data/lib/prawn/layout/grid.rb +259 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +326 -0
- data/lib/prawn/repeater.rb +122 -0
- data/lib/prawn/security.rb +269 -0
- data/lib/prawn/soft_mask.rb +94 -0
- data/lib/prawn/stamp.rb +134 -0
- data/lib/prawn/table.rb +609 -0
- data/lib/prawn/table/cell.rb +776 -0
- data/lib/prawn/table/cell/image.rb +70 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/span_dummy.rb +88 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +152 -0
- data/lib/prawn/table/cells.rb +260 -0
- data/lib/prawn/text.rb +420 -0
- data/lib/prawn/text/box.rb +141 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text/formatted/box.rb +563 -0
- data/lib/prawn/text/formatted/fragment.rb +253 -0
- data/lib/prawn/text/formatted/parser.rb +217 -0
- data/lib/prawn/utilities.rb +44 -0
- data/manual/basic_concepts/adding_pages.rb +27 -0
- data/manual/basic_concepts/basic_concepts.rb +34 -0
- data/manual/basic_concepts/creation.rb +39 -0
- data/manual/basic_concepts/cursor.rb +33 -0
- data/manual/basic_concepts/measurement.rb +25 -0
- data/manual/basic_concepts/origin.rb +38 -0
- data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
- data/manual/bounding_box/bounding_box.rb +39 -0
- data/manual/bounding_box/bounds.rb +49 -0
- data/manual/bounding_box/canvas.rb +24 -0
- data/manual/bounding_box/creation.rb +23 -0
- data/manual/bounding_box/indentation.rb +46 -0
- data/manual/bounding_box/nesting.rb +45 -0
- data/manual/bounding_box/russian_boxes.rb +40 -0
- data/manual/bounding_box/stretchy.rb +31 -0
- data/manual/document_and_page_options/background.rb +27 -0
- data/manual/document_and_page_options/document_and_page_options.rb +31 -0
- data/manual/document_and_page_options/metadata.rb +23 -0
- data/manual/document_and_page_options/page_margins.rb +38 -0
- data/manual/document_and_page_options/page_size.rb +34 -0
- data/manual/example_file.rb +116 -0
- data/manual/example_helper.rb +430 -0
- data/manual/example_package.rb +53 -0
- data/manual/example_section.rb +46 -0
- data/manual/graphics/circle_and_ellipse.rb +22 -0
- data/manual/graphics/color.rb +24 -0
- data/manual/graphics/common_lines.rb +28 -0
- data/manual/graphics/fill_and_stroke.rb +42 -0
- data/manual/graphics/fill_rules.rb +37 -0
- data/manual/graphics/gradients.rb +37 -0
- data/manual/graphics/graphics.rb +58 -0
- data/manual/graphics/helper.rb +17 -0
- data/manual/graphics/line_width.rb +35 -0
- data/manual/graphics/lines_and_curves.rb +41 -0
- data/manual/graphics/polygon.rb +29 -0
- data/manual/graphics/rectangle.rb +21 -0
- data/manual/graphics/rotate.rb +28 -0
- data/manual/graphics/scale.rb +41 -0
- data/manual/graphics/soft_masks.rb +46 -0
- data/manual/graphics/stroke_cap.rb +31 -0
- data/manual/graphics/stroke_dash.rb +43 -0
- data/manual/graphics/stroke_join.rb +30 -0
- data/manual/graphics/translate.rb +29 -0
- data/manual/graphics/transparency.rb +35 -0
- data/manual/images/absolute_position.rb +23 -0
- data/manual/images/fit.rb +21 -0
- data/manual/images/horizontal.rb +25 -0
- data/manual/images/images.rb +40 -0
- data/manual/images/plain_image.rb +18 -0
- data/manual/images/scale.rb +22 -0
- data/manual/images/vertical.rb +28 -0
- data/manual/images/width_and_height.rb +25 -0
- data/manual/layout/boxes.rb +27 -0
- data/manual/layout/content.rb +25 -0
- data/manual/layout/layout.rb +28 -0
- data/manual/layout/simple_grid.rb +23 -0
- data/manual/manual/cover.rb +26 -0
- data/manual/manual/foreword.rb +13 -0
- data/manual/manual/how_to_read_this_manual.rb +41 -0
- data/manual/manual/manual.rb +36 -0
- data/manual/outline/add_subsection_to.rb +61 -0
- data/manual/outline/insert_section_after.rb +47 -0
- data/manual/outline/outline.rb +32 -0
- data/manual/outline/sections_and_pages.rb +67 -0
- data/manual/repeatable_content/page_numbering.rb +54 -0
- data/manual/repeatable_content/repeatable_content.rb +31 -0
- data/manual/repeatable_content/repeater.rb +55 -0
- data/manual/repeatable_content/stamp.rb +41 -0
- data/manual/security/encryption.rb +31 -0
- data/manual/security/permissions.rb +38 -0
- data/manual/security/security.rb +28 -0
- data/manual/syntax_highlight.rb +52 -0
- data/manual/table/basic_block.rb +53 -0
- data/manual/table/before_rendering_page.rb +26 -0
- data/manual/table/cell_border_lines.rb +24 -0
- data/manual/table/cell_borders_and_bg.rb +31 -0
- data/manual/table/cell_dimensions.rb +30 -0
- data/manual/table/cell_text.rb +38 -0
- data/manual/table/column_widths.rb +30 -0
- data/manual/table/content_and_subtables.rb +39 -0
- data/manual/table/creation.rb +27 -0
- data/manual/table/filtering.rb +36 -0
- data/manual/table/flow_and_header.rb +17 -0
- data/manual/table/image_cells.rb +33 -0
- data/manual/table/position.rb +29 -0
- data/manual/table/row_colors.rb +20 -0
- data/manual/table/span.rb +30 -0
- data/manual/table/style.rb +22 -0
- data/manual/table/table.rb +52 -0
- data/manual/table/width.rb +27 -0
- data/manual/templates/full_template.rb +23 -0
- data/manual/templates/page_template.rb +47 -0
- data/manual/templates/templates.rb +26 -0
- data/manual/text/alignment.rb +44 -0
- data/manual/text/color.rb +24 -0
- data/manual/text/column_box.rb +32 -0
- data/manual/text/fallback_fonts.rb +37 -0
- data/manual/text/font.rb +41 -0
- data/manual/text/font_size.rb +45 -0
- data/manual/text/font_style.rb +23 -0
- data/manual/text/formatted_callbacks.rb +60 -0
- data/manual/text/formatted_text.rb +50 -0
- data/manual/text/free_flowing_text.rb +51 -0
- data/manual/text/group.rb +29 -0
- data/manual/text/inline.rb +43 -0
- data/manual/text/kerning_and_character_spacing.rb +39 -0
- data/manual/text/leading.rb +25 -0
- data/manual/text/line_wrapping.rb +41 -0
- data/manual/text/paragraph_indentation.rb +26 -0
- data/manual/text/positioned_text.rb +38 -0
- data/manual/text/registering_families.rb +48 -0
- data/manual/text/rendering_and_color.rb +37 -0
- data/manual/text/right_to_left_text.rb +43 -0
- data/manual/text/rotation.rb +43 -0
- data/manual/text/single_usage.rb +37 -0
- data/manual/text/text.rb +75 -0
- data/manual/text/text_box_excess.rb +32 -0
- data/manual/text/text_box_extensions.rb +45 -0
- data/manual/text/text_box_overflow.rb +44 -0
- data/manual/text/utf8.rb +28 -0
- data/manual/text/win_ansi_charset.rb +59 -0
- data/prawn.gemspec +46 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +493 -0
- data/spec/cell_spec.rb +584 -0
- data/spec/column_box_spec.rb +33 -0
- data/spec/data/curves.pdf +66 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +736 -0
- data/spec/extensions/encoding_helpers.rb +6 -0
- data/spec/extensions/mocha.rb +44 -0
- data/spec/font_spec.rb +433 -0
- data/spec/formatted_text_arranger_spec.rb +421 -0
- data/spec/formatted_text_box_spec.rb +640 -0
- data/spec/formatted_text_fragment_spec.rb +298 -0
- data/spec/graphics_spec.rb +651 -0
- data/spec/grid_spec.rb +85 -0
- data/spec/images_spec.rb +140 -0
- data/spec/inline_formatted_text_parser_spec.rb +515 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +333 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +170 -0
- data/spec/outline_spec.rb +426 -0
- data/spec/pdf_object_spec.rb +172 -0
- data/spec/png_spec.rb +240 -0
- data/spec/reference_spec.rb +105 -0
- data/spec/repeater_spec.rb +158 -0
- data/spec/security_spec.rb +126 -0
- data/spec/snapshot_spec.rb +186 -0
- data/spec/soft_mask_spec.rb +117 -0
- data/spec/span_spec.rb +49 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/stamp_spec.rb +159 -0
- data/spec/stroke_styles_spec.rb +193 -0
- data/spec/table_spec.rb +1209 -0
- data/spec/template_spec.rb +351 -0
- data/spec/text_at_spec.rb +129 -0
- data/spec/text_box_spec.rb +1029 -0
- data/spec/text_rendering_mode_spec.rb +45 -0
- data/spec/text_spacing_spec.rb +93 -0
- data/spec/text_spec.rb +421 -0
- data/spec/text_with_inline_formatting_spec.rb +35 -0
- data/spec/transparency_spec.rb +89 -0
- metadata +544 -0
@@ -0,0 +1,172 @@
|
|
1
|
+
# encoding: ASCII-8BIT
|
2
|
+
|
3
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
|
4
|
+
|
5
|
+
# See PDF Reference, Sixth Edition (1.7) pp51-60 for details
|
6
|
+
describe "PDF Object Serialization" do
|
7
|
+
|
8
|
+
it "should convert Ruby's nil to PDF null" do
|
9
|
+
Prawn::Core::PdfObject(nil).should == "null"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should convert Ruby booleans to PDF booleans" do
|
13
|
+
Prawn::Core::PdfObject(true).should == "true"
|
14
|
+
Prawn::Core::PdfObject(false).should == "false"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should convert a Ruby number to PDF number" do
|
18
|
+
Prawn::Core::PdfObject(1).should == "1"
|
19
|
+
Prawn::Core::PdfObject(1.214112421).should == "1.214112421"
|
20
|
+
# scientific notation is not valid in PDF
|
21
|
+
Prawn::Core::PdfObject(0.000005).should == "0.000005"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should convert a Ruby time object to a PDF timestamp" do
|
25
|
+
t = Time.now
|
26
|
+
Prawn::Core::PdfObject(t).should == t.strftime("(D:%Y%m%d%H%M%S%z").chop.chop + "'00')"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should convert a Ruby string to PDF string when inside a content stream" do
|
30
|
+
s = "I can has a string"
|
31
|
+
PDF::Inspector.parse(Prawn::Core::PdfObject(s, true)).should == s
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should convert a Ruby string to a UTF-16 PDF string when outside a content stream" do
|
35
|
+
s = "I can has a string"
|
36
|
+
s_utf16 = "\xFE\xFF" + s.unpack("U*").pack("n*")
|
37
|
+
PDF::Inspector.parse(Prawn::Core::PdfObject(s, false)).should == s_utf16
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should convert a Ruby string with characters outside the BMP to its " +
|
41
|
+
"UTF-16 representation with a BOM" do
|
42
|
+
# U+10192 ROMAN SEMUNCIA SIGN
|
43
|
+
semuncia = [65938].pack("U")
|
44
|
+
Prawn::Core::PdfObject(semuncia, false).upcase.should == "<FEFFD800DD92>"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should pass through bytes regardless of content stream status for ByteString" do
|
48
|
+
Prawn::Core::PdfObject(Prawn::Core::ByteString.new("\xDE\xAD\xBE\xEF")).upcase.
|
49
|
+
should == "<DEADBEEF>"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should escape parens when converting from Ruby string to PDF" do
|
53
|
+
s = 'I )(can has a string'
|
54
|
+
PDF::Inspector.parse(Prawn::Core::PdfObject(s, true)).should == s
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should handle ruby escaped parens when converting to PDF string" do
|
58
|
+
s = 'I can \\)( has string'
|
59
|
+
PDF::Inspector.parse(Prawn::Core::PdfObject(s, true)).should == s
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should escape various strings correctly when converting a LiteralString" do
|
63
|
+
ls = Prawn::Core::LiteralString.new("abc")
|
64
|
+
Prawn::Core::PdfObject(ls).should == "(abc)"
|
65
|
+
|
66
|
+
ls = Prawn::Core::LiteralString.new("abc\x0Ade") # should escape \n
|
67
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C\x0Ade)"
|
68
|
+
|
69
|
+
ls = Prawn::Core::LiteralString.new("abc\x0Dde") # should escape \r
|
70
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C\x0Dde)"
|
71
|
+
|
72
|
+
ls = Prawn::Core::LiteralString.new("abc\x09de") # should escape \t
|
73
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C\x09de)"
|
74
|
+
|
75
|
+
ls = Prawn::Core::LiteralString.new("abc\x08de") # should escape \b
|
76
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C\x08de)"
|
77
|
+
|
78
|
+
ls = Prawn::Core::LiteralString.new("abc\x0Cde") # should escape \f
|
79
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C\x0Cde)"
|
80
|
+
|
81
|
+
ls = Prawn::Core::LiteralString.new("abc(de") # should escape \(
|
82
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C(de)"
|
83
|
+
|
84
|
+
ls = Prawn::Core::LiteralString.new("abc)de") # should escape \)
|
85
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C)de)"
|
86
|
+
|
87
|
+
ls = Prawn::Core::LiteralString.new("abc\x5Cde") # should escape \\
|
88
|
+
Prawn::Core::PdfObject(ls).should == "(abc\x5C\x5Cde)"
|
89
|
+
Prawn::Core::PdfObject(ls).size.should == 9
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should escape strings correctly when converting a LiteralString that is not utf-8" do
|
93
|
+
data = "\x43\xaf\xc9\x7f\xef\xf\xe6\xa8\xcb\x5c\xaf\xd0"
|
94
|
+
ls = Prawn::Core::LiteralString.new(data)
|
95
|
+
Prawn::Core::PdfObject(ls).should == "(\x43\xaf\xc9\x7f\xef\xf\xe6\xa8\xcb\x5c\x5c\xaf\xd0)"
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should convert a Ruby symbol to PDF name" do
|
99
|
+
Prawn::Core::PdfObject(:my_symbol).should == "/my_symbol"
|
100
|
+
Prawn::Core::PdfObject(:"A;Name_With-Various***Characters?").should ==
|
101
|
+
"/A;Name_With-Various***Characters?"
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should convert a whitespace or delimiter containing Ruby symbol to a PDF name" do
|
105
|
+
Prawn::Core::PdfObject(:"my symbol").should == "/my#20symbol"
|
106
|
+
Prawn::Core::PdfObject(:"my#symbol").should == "/my#23symbol"
|
107
|
+
Prawn::Core::PdfObject(:"my/symbol").should == "/my#2Fsymbol"
|
108
|
+
Prawn::Core::PdfObject(:"my(symbol").should == "/my#28symbol"
|
109
|
+
Prawn::Core::PdfObject(:"my)symbol").should == "/my#29symbol"
|
110
|
+
Prawn::Core::PdfObject(:"my<symbol").should == "/my#3Csymbol"
|
111
|
+
Prawn::Core::PdfObject(:"my>symbol").should == "/my#3Esymbol"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should convert a Ruby array to PDF Array when inside a content stream" do
|
115
|
+
Prawn::Core::PdfObject([1,2,3]).should == "[1 2 3]"
|
116
|
+
PDF::Inspector.parse(Prawn::Core::PdfObject([[1,2],:foo,"Bar"], true)).should ==
|
117
|
+
[[1,2],:foo, "Bar"]
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should convert a Ruby array to PDF Array when outside a content stream" do
|
121
|
+
bar = "\xFE\xFF" + "Bar".unpack("U*").pack("n*")
|
122
|
+
Prawn::Core::PdfObject([1,2,3]).should == "[1 2 3]"
|
123
|
+
PDF::Inspector.parse(Prawn::Core::PdfObject([[1,2],:foo,"Bar"], false)).should ==
|
124
|
+
[[1,2],:foo, bar]
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should convert a Ruby hash to a PDF Dictionary when inside a content stream" do
|
128
|
+
dict = Prawn::Core::PdfObject( {:foo => :bar,
|
129
|
+
"baz" => [1,2,3],
|
130
|
+
:bang => {:a => "what", :b => [:you, :say] }}, true )
|
131
|
+
|
132
|
+
res = PDF::Inspector.parse(dict)
|
133
|
+
|
134
|
+
res[:foo].should == :bar
|
135
|
+
res[:baz].should == [1,2,3]
|
136
|
+
res[:bang].should == { :a => "what", :b => [:you, :say] }
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should convert a Ruby hash to a PDF Dictionary when outside a content stream" do
|
141
|
+
what = "\xFE\xFF" + "what".unpack("U*").pack("n*")
|
142
|
+
dict = Prawn::Core::PdfObject( {:foo => :bar,
|
143
|
+
"baz" => [1,2,3],
|
144
|
+
:bang => {:a => "what", :b => [:you, :say] }}, false )
|
145
|
+
|
146
|
+
res = PDF::Inspector.parse(dict)
|
147
|
+
|
148
|
+
res[:foo].should == :bar
|
149
|
+
res[:baz].should == [1,2,3]
|
150
|
+
res[:bang].should == { :a => what, :b => [:you, :say] }
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should not allow keys other than strings or symbols for PDF dicts" do
|
155
|
+
lambda { Prawn::Core::PdfObject(:foo => :bar, :baz => :bang, 1 => 4) }.
|
156
|
+
should raise_error(Prawn::Errors::FailedObjectConversion)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should convert a Prawn::Reference to a PDF indirect object reference" do
|
160
|
+
ref = Prawn::Core::Reference(1,true)
|
161
|
+
Prawn::Core::PdfObject(ref).should == ref.to_s
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should convert a NameTree::Node to a PDF hash" do
|
165
|
+
node = Prawn::Core::NameTree::Node.new(Prawn::Document.new, 10)
|
166
|
+
node.add "hello", 1.0
|
167
|
+
node.add "world", 2.0
|
168
|
+
data = Prawn::Core::PdfObject(node)
|
169
|
+
res = PDF::Inspector.parse(data)
|
170
|
+
res.should == {:Names => ["hello", 1.0, "world", 2.0]}
|
171
|
+
end
|
172
|
+
end
|
data/spec/png_spec.rb
ADDED
@@ -0,0 +1,240 @@
|
|
1
|
+
# encoding: ASCII-8BIT
|
2
|
+
|
3
|
+
# Spec'ing the PNG class. Not complete yet - still needs to check the
|
4
|
+
# contents of palette and transparency to ensure they're correct.
|
5
|
+
# Need to find files that have these sections first.
|
6
|
+
#
|
7
|
+
# see http://www.w3.org/TR/PNG/ for a detailed description of the PNG spec,
|
8
|
+
# particuarly Table 11.1 for the different color types
|
9
|
+
|
10
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
|
11
|
+
|
12
|
+
describe "When reading a greyscale PNG file (color type 0)" do
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
@filename = "#{Prawn::DATADIR}/images/web-links.png"
|
16
|
+
@data_filename = "#{Prawn::DATADIR}/images/web-links.dat"
|
17
|
+
@img_data = File.binread(@filename)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should read the attributes from the header chunk correctly" do
|
21
|
+
png = Prawn::Images::PNG.new(@img_data)
|
22
|
+
|
23
|
+
png.width.should == 21
|
24
|
+
png.height.should == 14
|
25
|
+
png.bits.should == 8
|
26
|
+
png.color_type.should == 0
|
27
|
+
png.compression_method.should == 0
|
28
|
+
png.filter_method.should == 0
|
29
|
+
png.interlace_method.should == 0
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should read the image data chunk correctly" do
|
33
|
+
png = Prawn::Images::PNG.new(@img_data)
|
34
|
+
data = File.binread(@data_filename)
|
35
|
+
png.img_data.should == data
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "When reading a greyscale PNG file with transparency (color type 0)" do
|
40
|
+
|
41
|
+
before(:each) do
|
42
|
+
@filename = "#{Prawn::DATADIR}/images/ruport_type0.png"
|
43
|
+
@img_data = File.binread(@filename)
|
44
|
+
end
|
45
|
+
|
46
|
+
# In a greyscale type 0 PNG image, the tRNS chunk should contain a single value
|
47
|
+
# that indicates the color that should be interpreted as transparent.
|
48
|
+
#
|
49
|
+
# http://www.w3.org/TR/PNG/#11tRNS
|
50
|
+
it "should read the tRNS chunk correctly" do
|
51
|
+
png = Prawn::Images::PNG.new(@img_data)
|
52
|
+
png.transparency[:grayscale].should == 255
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "When reading an RGB PNG file (color type 2)" do
|
57
|
+
|
58
|
+
before(:each) do
|
59
|
+
@filename = "#{Prawn::DATADIR}/images/ruport.png"
|
60
|
+
@data_filename = "#{Prawn::DATADIR}/images/ruport_data.dat"
|
61
|
+
@img_data = File.binread(@filename)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should read the attributes from the header chunk correctly" do
|
65
|
+
png = Prawn::Images::PNG.new(@img_data)
|
66
|
+
|
67
|
+
png.width.should == 258
|
68
|
+
png.height.should == 105
|
69
|
+
png.bits.should == 8
|
70
|
+
png.color_type.should == 2
|
71
|
+
png.compression_method.should == 0
|
72
|
+
png.filter_method.should == 0
|
73
|
+
png.interlace_method.should == 0
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should read the image data chunk correctly" do
|
77
|
+
png = Prawn::Images::PNG.new(@img_data)
|
78
|
+
data = File.binread(@data_filename)
|
79
|
+
png.img_data.should == data
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "When reading an RGB PNG file with transparency (color type 2)" do
|
84
|
+
|
85
|
+
before(:each) do
|
86
|
+
@filename = "#{Prawn::DATADIR}/images/arrow2.png"
|
87
|
+
@img_data = File.binread(@filename)
|
88
|
+
end
|
89
|
+
|
90
|
+
# In a RGB type 2 PNG image, the tRNS chunk should contain a single RGB value
|
91
|
+
# that indicates the color that should be interpreted as transparent. In this
|
92
|
+
# case it's green.
|
93
|
+
#
|
94
|
+
# http://www.w3.org/TR/PNG/#11tRNS
|
95
|
+
it "should read the tRNS chunk correctly" do
|
96
|
+
png = Prawn::Images::PNG.new(@img_data)
|
97
|
+
png.transparency[:rgb].should == [0, 255, 0]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# TODO: describe "When reading an indexed color PNG file wiih transparency (color type 3)"
|
102
|
+
|
103
|
+
describe "When reading an indexed color PNG file (color type 3)" do
|
104
|
+
|
105
|
+
before(:each) do
|
106
|
+
@filename = "#{Prawn::DATADIR}/images/rails.png"
|
107
|
+
@data_filename = "#{Prawn::DATADIR}/images/rails.dat"
|
108
|
+
@img_data = File.binread(@filename)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should read the attributes from the header chunk correctly" do
|
112
|
+
png = Prawn::Images::PNG.new(@img_data)
|
113
|
+
|
114
|
+
png.width.should == 50
|
115
|
+
png.height.should == 64
|
116
|
+
png.bits.should == 8
|
117
|
+
png.color_type.should == 3
|
118
|
+
png.compression_method.should == 0
|
119
|
+
png.filter_method.should == 0
|
120
|
+
png.interlace_method.should == 0
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should read the image data chunk correctly" do
|
124
|
+
png = Prawn::Images::PNG.new(@img_data)
|
125
|
+
data = File.binread(@data_filename)
|
126
|
+
png.img_data.should == data
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "When reading a greyscale+alpha PNG file (color type 4)" do
|
131
|
+
|
132
|
+
before(:each) do
|
133
|
+
@filename = "#{Prawn::DATADIR}/images/page_white_text.png"
|
134
|
+
@data_filename = "#{Prawn::DATADIR}/images/page_white_text.dat"
|
135
|
+
@alpha_data_filename = "#{Prawn::DATADIR}/images/page_white_text.alpha"
|
136
|
+
@img_data = File.binread(@filename)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should read the attributes from the header chunk correctly" do
|
140
|
+
png = Prawn::Images::PNG.new(@img_data)
|
141
|
+
|
142
|
+
png.width.should == 16
|
143
|
+
png.height.should == 16
|
144
|
+
png.bits.should == 8
|
145
|
+
png.color_type.should == 4
|
146
|
+
png.compression_method.should == 0
|
147
|
+
png.filter_method.should == 0
|
148
|
+
png.interlace_method.should == 0
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should correctly return the raw image data (with no alpha channel) from the image data chunk" do
|
152
|
+
png = Prawn::Images::PNG.new(@img_data)
|
153
|
+
png.split_alpha_channel!
|
154
|
+
data = File.binread(@data_filename)
|
155
|
+
png.img_data.should == data
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should correctly extract the alpha channel data from the image data chunk" do
|
159
|
+
png = Prawn::Images::PNG.new(@img_data)
|
160
|
+
png.split_alpha_channel!
|
161
|
+
data = File.binread(@alpha_data_filename)
|
162
|
+
png.alpha_channel.should == data
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "When reading an RGB+alpha PNG file (color type 6)" do
|
167
|
+
|
168
|
+
before(:each) do
|
169
|
+
@filename = "#{Prawn::DATADIR}/images/dice.png"
|
170
|
+
@data_filename = "#{Prawn::DATADIR}/images/dice.dat"
|
171
|
+
@alpha_data_filename = "#{Prawn::DATADIR}/images/dice.alpha"
|
172
|
+
@img_data = File.binread(@filename)
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should read the attributes from the header chunk correctly" do
|
176
|
+
png = Prawn::Images::PNG.new(@img_data)
|
177
|
+
|
178
|
+
png.width.should == 320
|
179
|
+
png.height.should == 240
|
180
|
+
png.bits.should == 8
|
181
|
+
png.color_type.should == 6
|
182
|
+
png.compression_method.should == 0
|
183
|
+
png.filter_method.should == 0
|
184
|
+
png.interlace_method.should == 0
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should correctly return the raw image data (with no alpha channel) from the image data chunk" do
|
188
|
+
png = Prawn::Images::PNG.new(@img_data)
|
189
|
+
png.split_alpha_channel!
|
190
|
+
data = File.binread(@data_filename)
|
191
|
+
# compare decompressed rather than compressed image data
|
192
|
+
# because JRuby's implementation of Zlib is different from MRI --
|
193
|
+
# both generate valid gzipped data, but not bit-identical to each other
|
194
|
+
Zlib::Inflate.inflate(png.img_data).should == Zlib::Inflate.inflate(data)
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should correctly extract the alpha channel data from the image data chunk" do
|
198
|
+
png = Prawn::Images::PNG.new(@img_data)
|
199
|
+
png.split_alpha_channel!
|
200
|
+
data = File.binread(@alpha_data_filename)
|
201
|
+
Zlib::Inflate.inflate(png.alpha_channel).should == Zlib::Inflate.inflate(data)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
describe "When reading a 16bit RGB+alpha PNG file (color type 6)" do
|
206
|
+
|
207
|
+
before(:each) do
|
208
|
+
@filename = "#{Prawn::DATADIR}/images/16bit.png"
|
209
|
+
@data_filename = "#{Prawn::DATADIR}/images/16bit.dat"
|
210
|
+
# alpha channel truncated to 8-bit
|
211
|
+
@alpha_data_filename = "#{Prawn::DATADIR}/images/16bit.alpha"
|
212
|
+
@img_data = File.binread(@filename)
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should read the attributes from the header chunk correctly" do
|
216
|
+
png = Prawn::Images::PNG.new(@img_data)
|
217
|
+
|
218
|
+
png.width.should == 32
|
219
|
+
png.height.should == 32
|
220
|
+
png.bits.should == 16
|
221
|
+
png.color_type.should == 6
|
222
|
+
png.compression_method.should == 0
|
223
|
+
png.filter_method.should == 0
|
224
|
+
png.interlace_method.should == 0
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should correctly return the raw image data (with no alpha channel) from the image data chunk" do
|
228
|
+
png = Prawn::Images::PNG.new(@img_data)
|
229
|
+
png.split_alpha_channel!
|
230
|
+
data = File.binread(@data_filename)
|
231
|
+
png.img_data.should == data
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should correctly extract the alpha channel data from the image data chunk" do
|
235
|
+
png = Prawn::Images::PNG.new(@img_data)
|
236
|
+
png.split_alpha_channel!
|
237
|
+
data = File.binread(@alpha_data_filename)
|
238
|
+
png.alpha_channel.should == data
|
239
|
+
end
|
240
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
|
4
|
+
|
5
|
+
describe "A Reference object" do
|
6
|
+
it "should produce a PDF reference on #to_s call" do
|
7
|
+
ref = Prawn::Core::Reference(1,true)
|
8
|
+
ref.to_s.should == "1 0 R"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should allow changing generation number" do
|
12
|
+
ref = Prawn::Core::Reference(1,true)
|
13
|
+
ref.gen = 1
|
14
|
+
ref.to_s.should == "1 1 R"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should generate a valid PDF object for the referenced data" do
|
18
|
+
ref = Prawn::Core::Reference(2,[1,"foo"])
|
19
|
+
ref.object.should == "2 0 obj\n#{Prawn::Core::PdfObject([1,"foo"])}\nendobj\n"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should automatically open a stream when #<< is used" do
|
23
|
+
ref = Prawn::Core::Reference(1, :Length => 41)
|
24
|
+
ref << "BT\n/F1 12 Tf\n72 712 Td\n( A stream ) Tj\nET"
|
25
|
+
ref.object.should == "1 0 obj\n<< /Length 41\n>>\nstream"+
|
26
|
+
"\nBT\n/F1 12 Tf\n72 712 Td\n( A stream ) Tj\nET" +
|
27
|
+
"\nendstream\nendobj\n"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should compress a stream upon request" do
|
31
|
+
ref = Prawn::Core::Reference(2,{})
|
32
|
+
ref << "Hi There " * 20
|
33
|
+
|
34
|
+
cref = Prawn::Core::Reference(2,{})
|
35
|
+
cref << "Hi There " * 20
|
36
|
+
cref.compress_stream
|
37
|
+
|
38
|
+
cref.stream.size.should be < ref.stream.size,
|
39
|
+
"compressed stream expected to be smaller than source but wasn't"
|
40
|
+
cref.data[:Filter].should == :FlateDecode
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should copy the data and stream from another ref on #replace" do
|
44
|
+
from = Prawn::Core::Reference(3, {:foo => 'bar'})
|
45
|
+
from << "has a stream too"
|
46
|
+
|
47
|
+
to = Prawn::Core::Reference(4, {:foo => 'baz'})
|
48
|
+
to.replace from
|
49
|
+
|
50
|
+
# should preserve identifier but copy data and stream
|
51
|
+
to.identifier.should == 4
|
52
|
+
to.data.should == from.data
|
53
|
+
to.stream.should == from.stream
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should copy a compressed stream from a compressed ref on #replace" do
|
57
|
+
from = Prawn::Core::Reference(5, {:foo => 'bar'})
|
58
|
+
from << "has a stream too " * 20
|
59
|
+
from.compress_stream
|
60
|
+
|
61
|
+
to = Prawn::Core::Reference(6, {:foo => 'baz'})
|
62
|
+
to.replace from
|
63
|
+
|
64
|
+
to.identifier.should == 6
|
65
|
+
to.data.should == from.data
|
66
|
+
to.stream.should == from.stream
|
67
|
+
to.compressed?.should == true
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should have Length if stream present" do
|
71
|
+
ref = Prawn::Core::Reference(7, {})
|
72
|
+
ref << "Hello"
|
73
|
+
|
74
|
+
ref.data[:Length].should == 5
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should update Length when stream is updated" do
|
78
|
+
ref = Prawn::Core::Reference(7, {})
|
79
|
+
ref << "Hello"
|
80
|
+
ref.data[:Length].should == 5
|
81
|
+
|
82
|
+
ref << " world"
|
83
|
+
ref.data[:Length].should == 11
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "generated via Prawn::Document" do
|
87
|
+
it "should return a proper reference on ref!" do
|
88
|
+
pdf = Prawn::Document.new
|
89
|
+
pdf.ref!({}).is_a?(Prawn::Core::Reference).should == true
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return an identifier on ref" do
|
93
|
+
pdf = Prawn::Document.new
|
94
|
+
r = pdf.ref({})
|
95
|
+
r.is_a?(Integer).should == true
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should have :Length of stream if it has one when compression disabled" do
|
99
|
+
pdf = Prawn::Document.new :compress => false
|
100
|
+
ref = pdf.ref!({})
|
101
|
+
ref << 'Hello'
|
102
|
+
ref.data[:Length].should == 5
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|