piglop-prawn 0.10.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/HACKING +49 -0
- data/LICENSE +56 -0
- data/README +140 -0
- data/Rakefile +52 -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/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/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/nested_pages.pdf +118 -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/examples/bounding_box/bounding_boxes.rb +43 -0
- data/examples/bounding_box/indentation.rb +34 -0
- data/examples/bounding_box/russian_boxes.rb +36 -0
- data/examples/bounding_box/stretched_nesting.rb +67 -0
- data/examples/example_helper.rb +4 -0
- data/examples/general/background.rb +23 -0
- data/examples/general/canvas.rb +15 -0
- data/examples/general/context_sensitive_headers.rb +37 -0
- data/examples/general/float.rb +11 -0
- data/examples/general/margin.rb +36 -0
- data/examples/general/measurement_units.rb +51 -0
- data/examples/general/metadata-info.rb +16 -0
- data/examples/general/multi_page_layout.rb +18 -0
- data/examples/general/outlines.rb +50 -0
- data/examples/general/page_geometry.rb +31 -0
- data/examples/general/page_numbering.rb +15 -0
- data/examples/general/repeaters.rb +47 -0
- data/examples/general/stamp.rb +41 -0
- data/examples/general/templates.rb +13 -0
- data/examples/graphics/basic_images.rb +23 -0
- data/examples/graphics/cmyk.rb +12 -0
- data/examples/graphics/curves.rb +11 -0
- data/examples/graphics/hexagon.rb +13 -0
- data/examples/graphics/image_fit.rb +15 -0
- data/examples/graphics/image_flow.rb +37 -0
- data/examples/graphics/image_position.rb +17 -0
- data/examples/graphics/line.rb +32 -0
- data/examples/graphics/png_types.rb +22 -0
- data/examples/graphics/polygons.rb +16 -0
- data/examples/graphics/remote_images.rb +12 -0
- data/examples/graphics/rounded_polygons.rb +19 -0
- data/examples/graphics/rounded_rectangle.rb +20 -0
- data/examples/graphics/ruport_style_helpers.rb +19 -0
- data/examples/graphics/stroke_bounds.rb +20 -0
- data/examples/graphics/stroke_cap_and_join.rb +45 -0
- data/examples/graphics/stroke_dash.rb +42 -0
- data/examples/graphics/transformations.rb +52 -0
- data/examples/graphics/transparency.rb +26 -0
- data/examples/m17n/chinese_text_wrapping.rb +17 -0
- data/examples/m17n/euro.rb +15 -0
- data/examples/m17n/sjis.rb +28 -0
- data/examples/m17n/utf8.rb +13 -0
- data/examples/m17n/win_ansi_charset.rb +54 -0
- data/examples/security/hello_foo.rb +8 -0
- data/examples/table/bill.rb +53 -0
- data/examples/table/cell.rb +12 -0
- data/examples/table/checkerboard.rb +22 -0
- data/examples/table/header.rb +14 -0
- data/examples/table/inline_format_table.rb +12 -0
- data/examples/table/multi_page_table.rb +9 -0
- data/examples/table/simple_table.rb +24 -0
- data/examples/table/subtable.rb +12 -0
- data/examples/table/widths.rb +20 -0
- data/examples/text/alignment.rb +18 -0
- data/examples/text/dfont.rb +48 -0
- data/examples/text/family_based_styling.rb +24 -0
- data/examples/text/font_calculations.rb +91 -0
- data/examples/text/font_size.rb +33 -0
- data/examples/text/hyphenation.rb +45 -0
- data/examples/text/indent_paragraphs.rb +18 -0
- data/examples/text/inline_format.rb +72 -0
- data/examples/text/kerning.rb +30 -0
- data/examples/text/rotated.rb +98 -0
- data/examples/text/shaped_text_box.rb +31 -0
- data/examples/text/simple_text.rb +17 -0
- data/examples/text/simple_text_ttf.rb +17 -0
- data/examples/text/text_box.rb +88 -0
- data/examples/text/text_box_returning_excess.rb +51 -0
- data/examples/text/text_flow.rb +67 -0
- data/lib/prawn/compatibility.rb +51 -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 +84 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +165 -0
- data/lib/prawn/core/object_store.rb +219 -0
- data/lib/prawn/core/page.rb +179 -0
- data/lib/prawn/core/pdf_object.rb +83 -0
- data/lib/prawn/core/reference.rb +112 -0
- data/lib/prawn/core/text/formatted/arranger.rb +260 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
- data/lib/prawn/core/text/formatted/wrap.rb +113 -0
- data/lib/prawn/core/text/line_wrap.rb +198 -0
- data/lib/prawn/core/text/wrap.rb +80 -0
- data/lib/prawn/core/text.rb +115 -0
- data/lib/prawn/core.rb +89 -0
- data/lib/prawn/document/bounding_box.rb +425 -0
- data/lib/prawn/document/graphics_state.rb +48 -0
- data/lib/prawn/document/internals.rb +176 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +87 -0
- data/lib/prawn/document.rb +573 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +94 -0
- data/lib/prawn/font/afm.rb +225 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +350 -0
- data/lib/prawn/font.rb +342 -0
- data/lib/prawn/graphics/cap_style.rb +38 -0
- data/lib/prawn/graphics/color.rb +205 -0
- data/lib/prawn/graphics/dash.rb +71 -0
- data/lib/prawn/graphics/join_style.rb +38 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/graphics.rb +321 -0
- data/lib/prawn/images/jpg.rb +46 -0
- data/lib/prawn/images/png.rb +226 -0
- data/lib/prawn/images.rb +348 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +278 -0
- data/lib/prawn/repeater.rb +129 -0
- data/lib/prawn/security/arcfour.rb +51 -0
- data/lib/prawn/security.rb +262 -0
- data/lib/prawn/stamp.rb +126 -0
- data/lib/prawn/table/accessors.rb +180 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +128 -0
- data/lib/prawn/table/cell.rb +330 -0
- data/lib/prawn/table.rb +421 -0
- data/lib/prawn/text/box.rb +380 -0
- data/lib/prawn/text/formatted/box.rb +215 -0
- data/lib/prawn/text/formatted/fragment.rb +127 -0
- data/lib/prawn/text/formatted/parser.rb +202 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text.rb +442 -0
- data/lib/prawn.rb +25 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +190 -0
- data/spec/cell_spec.rb +337 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +461 -0
- data/spec/font_spec.rb +314 -0
- data/spec/formatted_text_arranger_spec.rb +400 -0
- data/spec/formatted_text_box_spec.rb +659 -0
- data/spec/formatted_text_fragment_spec.rb +211 -0
- data/spec/graphics_spec.rb +446 -0
- data/spec/images_spec.rb +96 -0
- data/spec/inline_formatted_text_parser_spec.rb +446 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +341 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +160 -0
- data/spec/outline_spec.rb +229 -0
- data/spec/pdf_object_spec.rb +127 -0
- data/spec/png_spec.rb +237 -0
- data/spec/reference_spec.rb +82 -0
- data/spec/repeater_spec.rb +96 -0
- data/spec/security_spec.rb +120 -0
- data/spec/snapshot_spec.rb +138 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/stamp_spec.rb +108 -0
- data/spec/stroke_styles_spec.rb +163 -0
- data/spec/table_spec.rb +583 -0
- data/spec/template_spec.rb +158 -0
- data/spec/text_at_spec.rb +119 -0
- data/spec/text_box_spec.rb +675 -0
- data/spec/text_spacing_spec.rb +57 -0
- data/spec/text_spec.rb +328 -0
- data/spec/text_with_inline_formatting_spec.rb +193 -0
- data/spec/transparency_spec.rb +75 -0
- data/vendor/pdf-inspector/README +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
- data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
- data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
- data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
- data/vendor/ttfunk/example.rb +45 -0
- data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
- data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
- data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
- data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
- data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
- data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
- data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
- data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
- data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
- data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
- data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
- data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
- data/vendor/ttfunk/lib/ttfunk.rb +102 -0
- metadata +364 -0
@@ -0,0 +1,128 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# text.rb: Text table cells.
|
4
|
+
#
|
5
|
+
# Copyright December 2009, Gregory Brown and Brad Ediger. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
module Prawn
|
9
|
+
class Table
|
10
|
+
class Cell
|
11
|
+
|
12
|
+
# A Cell that contains text. Has some limited options to set font family,
|
13
|
+
# size, and style.
|
14
|
+
#
|
15
|
+
class Text < Cell
|
16
|
+
|
17
|
+
TextOptions = [:inline_format, :kerning, :size, :style,
|
18
|
+
:align, :valign, :rotate, :rotate_around, :leading, :single_line,
|
19
|
+
:skip_encoding, :overflow, :min_font_size]
|
20
|
+
|
21
|
+
TextOptions.each do |option|
|
22
|
+
define_method("#{option}=") { |v| @text_options[option] = v }
|
23
|
+
define_method(option) { @text_options[option] }
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_writer :font, :text_color
|
27
|
+
|
28
|
+
def initialize(pdf, point, options={})
|
29
|
+
@text_options = {}
|
30
|
+
super
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns the font that will be used to draw this cell.
|
34
|
+
#
|
35
|
+
def font
|
36
|
+
with_font { @pdf.font }
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns the width of this text with no wrapping. This will be far off
|
40
|
+
# from the final width if the text is long.
|
41
|
+
#
|
42
|
+
def natural_content_width
|
43
|
+
[styled_width_of(@content), @pdf.bounds.width].min
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns a reasonable minimum width. If the cell has any content, make
|
47
|
+
# sure we have enough width to be at least one character wide. This is
|
48
|
+
# a bit of a hack, but it should work well enough.
|
49
|
+
#
|
50
|
+
def min_width
|
51
|
+
min_content_width = [natural_content_width, styled_width_of("M")].min
|
52
|
+
left_padding + right_padding + min_content_width
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns the natural height of this block of text, wrapped to the
|
56
|
+
# preset width.
|
57
|
+
#
|
58
|
+
def natural_content_height
|
59
|
+
with_font do
|
60
|
+
b = text_box(:width => content_width + FPTolerance)
|
61
|
+
b.render(:dry_run => true)
|
62
|
+
b.height
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Draws the text content into its bounding box.
|
67
|
+
#
|
68
|
+
def draw_content
|
69
|
+
with_font do
|
70
|
+
@pdf.move_down((@pdf.font.line_gap + @pdf.font.descender)/2)
|
71
|
+
with_text_color do
|
72
|
+
text_box(:width => content_width + FPTolerance,
|
73
|
+
:height => content_height + FPTolerance,
|
74
|
+
:at => [0, @pdf.cursor]).render
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def with_font
|
82
|
+
@pdf.save_font do
|
83
|
+
options = {}
|
84
|
+
options[:style] = @text_options[:style] if @text_options[:style]
|
85
|
+
|
86
|
+
@pdf.font(@font || @pdf.font.name, options)
|
87
|
+
|
88
|
+
yield
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def with_text_color
|
93
|
+
old_color = @pdf.fill_color || '000000'
|
94
|
+
@pdf.fill_color(@text_color) if @text_color
|
95
|
+
yield
|
96
|
+
ensure
|
97
|
+
@pdf.fill_color(old_color)
|
98
|
+
end
|
99
|
+
|
100
|
+
def text_box(extra_options={})
|
101
|
+
if @text_options[:inline_format]
|
102
|
+
options = @text_options.dup
|
103
|
+
options.delete(:inline_format)
|
104
|
+
|
105
|
+
array = ::Prawn::Text::Formatted::Parser.to_array(@content)
|
106
|
+
::Prawn::Text::Formatted::Box.new(array,
|
107
|
+
options.merge(extra_options).merge(:document => @pdf))
|
108
|
+
else
|
109
|
+
::Prawn::Text::Box.new(@content, @text_options.merge(extra_options).
|
110
|
+
merge(:document => @pdf))
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns the width of +text+ under the given text options.
|
115
|
+
#
|
116
|
+
def styled_width_of(text)
|
117
|
+
with_font do
|
118
|
+
options = {}
|
119
|
+
options[:size] = @text_options[:size] if @text_options[:size]
|
120
|
+
|
121
|
+
@pdf.font.compute_width_of(text, options)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,330 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# cell.rb: Table cell drawing.
|
4
|
+
#
|
5
|
+
# Copyright December 2009, Gregory Brown and Brad Ediger. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
module Prawn
|
10
|
+
class Document
|
11
|
+
|
12
|
+
# Instantiates and draws a cell on the document.
|
13
|
+
#
|
14
|
+
# cell(:content => "Hello world!", :at => [12, 34])
|
15
|
+
#
|
16
|
+
# See Prawn::Table::Cell.make for full options.
|
17
|
+
#
|
18
|
+
def cell(options={})
|
19
|
+
cell = Table::Cell.make(self, options.delete(:content), options)
|
20
|
+
cell.draw
|
21
|
+
cell
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
class Table
|
27
|
+
|
28
|
+
# A Cell is a rectangular area of the page into which content is drawn. It
|
29
|
+
# has a framework for sizing itself and adding padding and simple styling.
|
30
|
+
# There are several standard Cell subclasses that handle things like text,
|
31
|
+
# Tables, and (in the future) stamps, images, and arbitrary content.
|
32
|
+
#
|
33
|
+
# Cells are a basic building block for table support (see Prawn::Table).
|
34
|
+
#
|
35
|
+
# Please subclass me if you want new content types! I'm designed to be very
|
36
|
+
# extensible. See the different standard Cell subclasses in
|
37
|
+
# lib/prawn/table/cell/*.rb for a template.
|
38
|
+
#
|
39
|
+
class Cell
|
40
|
+
|
41
|
+
# Amount of dead space (in PDF points) inside the borders but outside the
|
42
|
+
# content. Padding defaults to 5pt.
|
43
|
+
#
|
44
|
+
attr_reader :padding
|
45
|
+
|
46
|
+
# If provided, the minimum width that this cell will permit.
|
47
|
+
#
|
48
|
+
attr_reader :min_width
|
49
|
+
|
50
|
+
# If provided, the maximum width that this cell can be drawn in.
|
51
|
+
#
|
52
|
+
attr_reader :max_width
|
53
|
+
|
54
|
+
# Manually specify the cell's height.
|
55
|
+
#
|
56
|
+
attr_writer :height
|
57
|
+
|
58
|
+
# Specifies which borders to enable. Must be an array of zero or more of:
|
59
|
+
# <tt>[:left, :right, :top, :bottom]</tt>.
|
60
|
+
#
|
61
|
+
attr_accessor :borders
|
62
|
+
|
63
|
+
# Specifies the width, in PDF points, of the cell's borders.
|
64
|
+
#
|
65
|
+
attr_accessor :border_width
|
66
|
+
|
67
|
+
# Specifies the color of the cell borders. Given in HTML RGB format, e.g.,
|
68
|
+
# "ccffff".
|
69
|
+
#
|
70
|
+
attr_accessor :border_color
|
71
|
+
|
72
|
+
# Specifies the content for the cell. Must be a "cellable" object. See the
|
73
|
+
# "Data" section of the Prawn::Table documentation for details on cellable
|
74
|
+
# objects.
|
75
|
+
#
|
76
|
+
attr_accessor :content
|
77
|
+
|
78
|
+
# The background color, if any, for this cell. Specified in HTML RGB
|
79
|
+
# format, e.g., "ccffff". The background is drawn under the whole cell,
|
80
|
+
# including any padding.
|
81
|
+
#
|
82
|
+
attr_accessor :background_color
|
83
|
+
|
84
|
+
# Instantiates a Cell based on the given options. The particular class of
|
85
|
+
# cell returned depends on the :content argument. See the Prawn::Table
|
86
|
+
# documentation under "Data" for allowable content types.
|
87
|
+
#
|
88
|
+
def self.make(pdf, content, options={})
|
89
|
+
at = options.delete(:at) || [0, pdf.cursor]
|
90
|
+
options[:content] = content
|
91
|
+
|
92
|
+
case content
|
93
|
+
when Prawn::Table::Cell
|
94
|
+
content
|
95
|
+
when String
|
96
|
+
Cell::Text.new(pdf, at, options)
|
97
|
+
when Prawn::Table
|
98
|
+
Cell::Subtable.new(pdf, at, options)
|
99
|
+
when Array
|
100
|
+
subtable = Prawn::Table.new(options[:content], pdf, {})
|
101
|
+
Cell::Subtable.new(pdf, at, options.merge(:content => subtable))
|
102
|
+
else
|
103
|
+
# TODO: other types of content
|
104
|
+
raise ArgumentError, "Content type not recognized: #{content.inspect}"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# A small amount added to the bounding box width to cover over floating-
|
109
|
+
# point errors when round-tripping from content_width to width and back.
|
110
|
+
# This does not change cell positioning; it only slightly expands each
|
111
|
+
# cell's bounding box width so that rounding error does not prevent a cell
|
112
|
+
# from rendering.
|
113
|
+
#
|
114
|
+
FPTolerance = 1
|
115
|
+
|
116
|
+
# Sets up a cell on the document +pdf+, at the given x/y location +point+,
|
117
|
+
# with the given +options+. Cell, like Table, follows the "options set
|
118
|
+
# accessors" paradigm (see "Options" under the Table documentation), so
|
119
|
+
# any cell accessor <tt>cell.foo = :bar</tt> can be set by providing the
|
120
|
+
# option <tt>:foo => :bar</tt> here.
|
121
|
+
#
|
122
|
+
def initialize(pdf, point, options={})
|
123
|
+
@pdf = pdf
|
124
|
+
@point = point
|
125
|
+
|
126
|
+
# Set defaults; these can be changed by options
|
127
|
+
@padding = [5, 5, 5, 5]
|
128
|
+
@borders = [:top, :bottom, :left, :right]
|
129
|
+
@border_width = 1
|
130
|
+
@border_color = '000000'
|
131
|
+
|
132
|
+
options.each { |k, v| send("#{k}=", v) }
|
133
|
+
|
134
|
+
# Sensible defaults for min / max.
|
135
|
+
@min_width = left_padding + right_padding
|
136
|
+
@max_width = @pdf.bounds.width
|
137
|
+
end
|
138
|
+
|
139
|
+
# Returns the cell's width in points, inclusive of padding.
|
140
|
+
#
|
141
|
+
def width
|
142
|
+
# We can't ||= here because the FP error accumulates on the round-trip
|
143
|
+
# from #content_width.
|
144
|
+
@width || (content_width + left_padding + right_padding)
|
145
|
+
end
|
146
|
+
|
147
|
+
# Manually sets the cell's width, inclusive of padding.
|
148
|
+
#
|
149
|
+
def width=(w)
|
150
|
+
@width = @min_width = @max_width = w
|
151
|
+
end
|
152
|
+
|
153
|
+
# Returns the width of the bare content in the cell, excluding padding.
|
154
|
+
#
|
155
|
+
def content_width
|
156
|
+
if @width # manually set
|
157
|
+
return @width - left_padding - right_padding
|
158
|
+
end
|
159
|
+
|
160
|
+
natural_content_width
|
161
|
+
end
|
162
|
+
|
163
|
+
# Returns the width this cell would naturally take on, absent other
|
164
|
+
# constraints. Must be implemented in subclasses.
|
165
|
+
#
|
166
|
+
def natural_content_width
|
167
|
+
raise NotImplementedError,
|
168
|
+
"subclasses must implement natural_content_width"
|
169
|
+
end
|
170
|
+
|
171
|
+
# Returns the cell's height in points, inclusive of padding.
|
172
|
+
#
|
173
|
+
def height
|
174
|
+
# We can't ||= here because the FP error accumulates on the round-trip
|
175
|
+
# from #content_height.
|
176
|
+
@height || (content_height + top_padding + bottom_padding)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Returns the height of the bare content in the cell, excluding padding.
|
180
|
+
#
|
181
|
+
def content_height
|
182
|
+
if @height # manually set
|
183
|
+
return @height - top_padding - bottom_padding
|
184
|
+
end
|
185
|
+
|
186
|
+
natural_content_height
|
187
|
+
end
|
188
|
+
|
189
|
+
# Returns the height this cell would naturally take on, absent
|
190
|
+
# constraints. Must be implemented in subclasses.
|
191
|
+
#
|
192
|
+
def natural_content_height
|
193
|
+
raise NotImplementedError,
|
194
|
+
"subclasses must implement natural_content_height"
|
195
|
+
end
|
196
|
+
|
197
|
+
# Draws the cell onto the document. Pass in a point [x,y] to override the
|
198
|
+
# location at which the cell is drawn.
|
199
|
+
#
|
200
|
+
def draw(pt=[x, y])
|
201
|
+
draw_background(pt)
|
202
|
+
draw_borders(pt)
|
203
|
+
@pdf.bounding_box([pt[0] + left_padding, pt[1] - top_padding],
|
204
|
+
:width => content_width + FPTolerance,
|
205
|
+
:height => content_height + FPTolerance) do
|
206
|
+
draw_content
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
# x-position of the cell within the parent bounds.
|
211
|
+
#
|
212
|
+
def x
|
213
|
+
@point[0]
|
214
|
+
end
|
215
|
+
|
216
|
+
# Set the x-position of the cell within the parent bounds.
|
217
|
+
#
|
218
|
+
def x=(val)
|
219
|
+
@point[0] = val
|
220
|
+
end
|
221
|
+
|
222
|
+
# y-position of the cell within the parent bounds.
|
223
|
+
#
|
224
|
+
def y
|
225
|
+
@point[1]
|
226
|
+
end
|
227
|
+
|
228
|
+
# Set the y-position of the cell within the parent bounds.
|
229
|
+
#
|
230
|
+
def y=(val)
|
231
|
+
@point[1] = val
|
232
|
+
end
|
233
|
+
|
234
|
+
# Sets padding on this cell. The argument can be one of:
|
235
|
+
#
|
236
|
+
# * an integer (sets all padding)
|
237
|
+
# * a two-element array [vertical_padding, horizontal_padding]
|
238
|
+
# * a four-element array [top, right, bottom, left]
|
239
|
+
#
|
240
|
+
def padding=(pad)
|
241
|
+
@padding = case
|
242
|
+
when pad.nil?
|
243
|
+
[0, 0, 0, 0]
|
244
|
+
when Numeric === pad # all padding
|
245
|
+
[pad, pad, pad, pad]
|
246
|
+
when pad.length == 2 # vert, horiz
|
247
|
+
[pad[0], pad[1], pad[0], pad[1]]
|
248
|
+
when pad.length == 4 # top, right, bottom, left
|
249
|
+
[pad[0], pad[1], pad[2], pad[3]]
|
250
|
+
else
|
251
|
+
raise ArgumentError, ":padding must be a number or an array [v,h] " +
|
252
|
+
"or [t,r,b,l]"
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
protected
|
257
|
+
|
258
|
+
# Draws the cell's background color.
|
259
|
+
#
|
260
|
+
def draw_background(pt)
|
261
|
+
x, y = pt
|
262
|
+
margin = @border_width / 2
|
263
|
+
if @background_color
|
264
|
+
@pdf.mask(:fill_color) do
|
265
|
+
@pdf.fill_color @background_color
|
266
|
+
h = @borders.include?(:bottom) ? height - (2*margin) :
|
267
|
+
height + margin
|
268
|
+
@pdf.fill_rectangle [x, y], width, h
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
# Draws borders around the cell. Borders are centered on the bounds of
|
274
|
+
# the cell outside of any padding, so the caller is responsible for
|
275
|
+
# setting appropriate padding to ensure the border does not overlap with
|
276
|
+
# cell content.
|
277
|
+
#
|
278
|
+
def draw_borders(pt)
|
279
|
+
x, y = pt
|
280
|
+
return if @border_width <= 0
|
281
|
+
# Draw left / right borders one-half border width beyond the center of
|
282
|
+
# the corner, so that the corners end up square.
|
283
|
+
margin = @border_width / 2.0
|
284
|
+
|
285
|
+
@pdf.mask(:line_width, :stroke_color) do
|
286
|
+
@pdf.line_width = @border_width
|
287
|
+
@pdf.stroke_color = @border_color if @border_color
|
288
|
+
|
289
|
+
@borders.each do |border|
|
290
|
+
from, to = case border
|
291
|
+
when :top
|
292
|
+
[[x, y], [x+width, y]]
|
293
|
+
when :bottom
|
294
|
+
[[x, y-height], [x+width, y-height]]
|
295
|
+
when :left
|
296
|
+
[[x, y+margin], [x, y-height-margin]]
|
297
|
+
when :right
|
298
|
+
[[x+width, y+margin], [x+width, y-height-margin]]
|
299
|
+
end
|
300
|
+
@pdf.stroke_line(from, to)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
|
305
|
+
# Draws cell content within the cell's bounding box. Must be implemented
|
306
|
+
# in subclasses.
|
307
|
+
#
|
308
|
+
def draw_content
|
309
|
+
raise NotImplementedError, "subclasses must implement draw_content"
|
310
|
+
end
|
311
|
+
|
312
|
+
def top_padding
|
313
|
+
@padding[0]
|
314
|
+
end
|
315
|
+
|
316
|
+
def right_padding
|
317
|
+
@padding[1]
|
318
|
+
end
|
319
|
+
|
320
|
+
def bottom_padding
|
321
|
+
@padding[2]
|
322
|
+
end
|
323
|
+
|
324
|
+
def left_padding
|
325
|
+
@padding[3]
|
326
|
+
end
|
327
|
+
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|