prawn 0.8.4 → 0.11.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/HACKING +50 -0
- data/LICENSE +56 -0
- data/README +141 -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/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/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 +8 -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 +66 -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/grid/bounding_boxes.rb +21 -0
- data/examples/grid/column_gutter_grid.rb +20 -0
- data/examples/grid/multi_boxes.rb +51 -0
- data/examples/grid/show_grid.rb +13 -0
- data/examples/grid/simple_grid.rb +20 -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/character_spacing.rb +12 -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 +22 -0
- data/examples/text/inline_format.rb +103 -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/span.rb +29 -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.rb +25 -1
- data/lib/prawn/compatibility.rb +51 -0
- data/lib/prawn/core.rb +85 -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 +78 -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 +236 -0
- data/lib/prawn/core/page.rb +199 -0
- data/lib/prawn/core/pdf_object.rb +108 -0
- data/lib/prawn/core/reference.rb +112 -0
- data/lib/prawn/core/text.rb +140 -0
- data/lib/prawn/core/text/formatted/arranger.rb +266 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
- data/lib/prawn/core/text/formatted/wrap.rb +112 -0
- data/lib/prawn/core/text/line_wrap.rb +211 -0
- data/lib/prawn/core/text/wrap.rb +82 -0
- data/lib/prawn/document.rb +574 -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 +170 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +87 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +86 -0
- data/lib/prawn/font.rb +368 -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/graphics.rb +325 -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/images.rb +361 -0
- data/lib/prawn/images/jpg.rb +46 -0
- data/lib/prawn/images/png.rb +226 -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 +129 -0
- data/lib/prawn/security.rb +262 -0
- data/lib/prawn/security/arcfour.rb +51 -0
- data/lib/prawn/stamp.rb +126 -0
- data/lib/prawn/table.rb +421 -0
- data/lib/prawn/table/cell.rb +360 -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 +125 -0
- data/lib/prawn/table/cells.rb +189 -0
- data/lib/prawn/text.rb +449 -0
- data/lib/prawn/text/box.rb +385 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text/formatted/box.rb +222 -0
- data/lib/prawn/text/formatted/fragment.rb +181 -0
- data/lib/prawn/text/formatted/parser.rb +213 -0
- data/prawn.gemspec +28 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +190 -0
- data/spec/cell_spec.rb +359 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +476 -0
- data/spec/font_spec.rb +324 -0
- data/spec/formatted_text_arranger_spec.rb +426 -0
- data/spec/formatted_text_box_spec.rb +756 -0
- data/spec/formatted_text_fragment_spec.rb +211 -0
- data/spec/graphics_spec.rb +446 -0
- data/spec/grid_spec.rb +85 -0
- data/spec/images_spec.rb +117 -0
- data/spec/inline_formatted_text_parser_spec.rb +502 -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 +404 -0
- data/spec/pdf_object_spec.rb +170 -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 +154 -0
- data/spec/span_spec.rb +49 -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 +598 -0
- data/spec/template_spec.rb +165 -0
- data/spec/text_at_spec.rb +119 -0
- data/spec/text_box_spec.rb +742 -0
- data/spec/text_spacing_spec.rb +75 -0
- data/spec/text_spec.rb +342 -0
- data/spec/text_with_inline_formatting_spec.rb +193 -0
- data/spec/transparency_spec.rb +89 -0
- data/vendor/pdf-inspector/README +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -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/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.rb +102 -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.rb +18 -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_collection.rb +72 -0
- data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -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/glyf.rb +64 -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/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.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -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.rb +91 -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/simple.rb +14 -0
- metadata +337 -48
@@ -0,0 +1,360 @@
|
|
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
|
+
# Set up, but do not draw, a cell. Useful for creating cells with
|
25
|
+
# formatting options to be inserted into a Table. Call +draw+ on the
|
26
|
+
# resulting Cell to ink it.
|
27
|
+
#
|
28
|
+
# See the documentation on Prawn::Cell for details on the arguments.
|
29
|
+
#
|
30
|
+
def make_cell(content, options={})
|
31
|
+
Prawn::Table::Cell.make(self, content, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
class Table
|
37
|
+
|
38
|
+
# A Cell is a rectangular area of the page into which content is drawn. It
|
39
|
+
# has a framework for sizing itself and adding padding and simple styling.
|
40
|
+
# There are several standard Cell subclasses that handle things like text,
|
41
|
+
# Tables, and (in the future) stamps, images, and arbitrary content.
|
42
|
+
#
|
43
|
+
# Cells are a basic building block for table support (see Prawn::Table).
|
44
|
+
#
|
45
|
+
# Please subclass me if you want new content types! I'm designed to be very
|
46
|
+
# extensible. See the different standard Cell subclasses in
|
47
|
+
# lib/prawn/table/cell/*.rb for a template.
|
48
|
+
#
|
49
|
+
class Cell
|
50
|
+
|
51
|
+
# Amount of dead space (in PDF points) inside the borders but outside the
|
52
|
+
# content. Padding defaults to 5pt.
|
53
|
+
#
|
54
|
+
attr_reader :padding
|
55
|
+
|
56
|
+
# If provided, the minimum width that this cell will permit.
|
57
|
+
#
|
58
|
+
attr_reader :min_width
|
59
|
+
|
60
|
+
# If provided, the maximum width that this cell can be drawn in.
|
61
|
+
#
|
62
|
+
attr_reader :max_width
|
63
|
+
|
64
|
+
# Manually specify the cell's height.
|
65
|
+
#
|
66
|
+
attr_writer :height
|
67
|
+
|
68
|
+
# Specifies which borders to enable. Must be an array of zero or more of:
|
69
|
+
# <tt>[:left, :right, :top, :bottom]</tt>.
|
70
|
+
#
|
71
|
+
attr_accessor :borders
|
72
|
+
|
73
|
+
# Specifies the width, in PDF points, of the cell's borders.
|
74
|
+
#
|
75
|
+
attr_accessor :border_width
|
76
|
+
|
77
|
+
# Specifies the color of the cell borders. Given in HTML RGB format, e.g.,
|
78
|
+
# "ccffff".
|
79
|
+
#
|
80
|
+
attr_accessor :border_color
|
81
|
+
|
82
|
+
# Specifies the content for the cell. Must be a "cellable" object. See the
|
83
|
+
# "Data" section of the Prawn::Table documentation for details on cellable
|
84
|
+
# objects.
|
85
|
+
#
|
86
|
+
attr_accessor :content
|
87
|
+
|
88
|
+
# The background color, if any, for this cell. Specified in HTML RGB
|
89
|
+
# format, e.g., "ccffff". The background is drawn under the whole cell,
|
90
|
+
# including any padding.
|
91
|
+
#
|
92
|
+
attr_accessor :background_color
|
93
|
+
|
94
|
+
# Instantiates a Cell based on the given options. The particular class of
|
95
|
+
# cell returned depends on the :content argument. See the Prawn::Table
|
96
|
+
# documentation under "Data" for allowable content types.
|
97
|
+
#
|
98
|
+
def self.make(pdf, content, options={})
|
99
|
+
at = options.delete(:at) || [0, pdf.cursor]
|
100
|
+
content = "" if content.nil?
|
101
|
+
options[:content] = content
|
102
|
+
|
103
|
+
case content
|
104
|
+
when Prawn::Table::Cell
|
105
|
+
content
|
106
|
+
when String
|
107
|
+
Cell::Text.new(pdf, at, options)
|
108
|
+
when Prawn::Table
|
109
|
+
Cell::Subtable.new(pdf, at, options)
|
110
|
+
when Array
|
111
|
+
subtable = Prawn::Table.new(options[:content], pdf, {})
|
112
|
+
Cell::Subtable.new(pdf, at, options.merge(:content => subtable))
|
113
|
+
else
|
114
|
+
# TODO: other types of content
|
115
|
+
raise ArgumentError, "Content type not recognized: #{content.inspect}"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# A small amount added to the bounding box width to cover over floating-
|
120
|
+
# point errors when round-tripping from content_width to width and back.
|
121
|
+
# This does not change cell positioning; it only slightly expands each
|
122
|
+
# cell's bounding box width so that rounding error does not prevent a cell
|
123
|
+
# from rendering.
|
124
|
+
#
|
125
|
+
FPTolerance = 1
|
126
|
+
|
127
|
+
# Sets up a cell on the document +pdf+, at the given x/y location +point+,
|
128
|
+
# with the given +options+. Cell, like Table, follows the "options set
|
129
|
+
# accessors" paradigm (see "Options" under the Table documentation), so
|
130
|
+
# any cell accessor <tt>cell.foo = :bar</tt> can be set by providing the
|
131
|
+
# option <tt>:foo => :bar</tt> here.
|
132
|
+
#
|
133
|
+
def initialize(pdf, point, options={})
|
134
|
+
@pdf = pdf
|
135
|
+
@point = point
|
136
|
+
|
137
|
+
# Set defaults; these can be changed by options
|
138
|
+
@padding = [5, 5, 5, 5]
|
139
|
+
@borders = [:top, :bottom, :left, :right]
|
140
|
+
@border_width = 1
|
141
|
+
@border_color = '000000'
|
142
|
+
|
143
|
+
options.each { |k, v| send("#{k}=", v) }
|
144
|
+
|
145
|
+
# Sensible defaults for min / max.
|
146
|
+
@min_width = padding_left + padding_right
|
147
|
+
@max_width = @pdf.bounds.width
|
148
|
+
end
|
149
|
+
|
150
|
+
# Returns the cell's width in points, inclusive of padding.
|
151
|
+
#
|
152
|
+
def width
|
153
|
+
# We can't ||= here because the FP error accumulates on the round-trip
|
154
|
+
# from #content_width.
|
155
|
+
@width || (content_width + padding_left + padding_right)
|
156
|
+
end
|
157
|
+
|
158
|
+
# Manually sets the cell's width, inclusive of padding.
|
159
|
+
#
|
160
|
+
def width=(w)
|
161
|
+
@width = @min_width = @max_width = w
|
162
|
+
end
|
163
|
+
|
164
|
+
# Returns the width of the bare content in the cell, excluding padding.
|
165
|
+
#
|
166
|
+
def content_width
|
167
|
+
if @width # manually set
|
168
|
+
return @width - padding_left - padding_right
|
169
|
+
end
|
170
|
+
|
171
|
+
natural_content_width
|
172
|
+
end
|
173
|
+
|
174
|
+
# Returns the width this cell would naturally take on, absent other
|
175
|
+
# constraints. Must be implemented in subclasses.
|
176
|
+
#
|
177
|
+
def natural_content_width
|
178
|
+
raise NotImplementedError,
|
179
|
+
"subclasses must implement natural_content_width"
|
180
|
+
end
|
181
|
+
|
182
|
+
# Returns the cell's height in points, inclusive of padding.
|
183
|
+
#
|
184
|
+
def height
|
185
|
+
# We can't ||= here because the FP error accumulates on the round-trip
|
186
|
+
# from #content_height.
|
187
|
+
@height || (content_height + padding_top + padding_bottom)
|
188
|
+
end
|
189
|
+
|
190
|
+
# Returns the height of the bare content in the cell, excluding padding.
|
191
|
+
#
|
192
|
+
def content_height
|
193
|
+
if @height # manually set
|
194
|
+
return @height - padding_top - padding_bottom
|
195
|
+
end
|
196
|
+
|
197
|
+
natural_content_height
|
198
|
+
end
|
199
|
+
|
200
|
+
# Returns the height this cell would naturally take on, absent
|
201
|
+
# constraints. Must be implemented in subclasses.
|
202
|
+
#
|
203
|
+
def natural_content_height
|
204
|
+
raise NotImplementedError,
|
205
|
+
"subclasses must implement natural_content_height"
|
206
|
+
end
|
207
|
+
|
208
|
+
# Draws the cell onto the document. Pass in a point [x,y] to override the
|
209
|
+
# location at which the cell is drawn.
|
210
|
+
#
|
211
|
+
def draw(pt=[x, y])
|
212
|
+
draw_background(pt)
|
213
|
+
@pdf.bounding_box([pt[0] + padding_left, pt[1] - padding_top],
|
214
|
+
:width => content_width + FPTolerance,
|
215
|
+
:height => content_height + FPTolerance) do
|
216
|
+
draw_content
|
217
|
+
end
|
218
|
+
draw_borders(pt)
|
219
|
+
end
|
220
|
+
|
221
|
+
# x-position of the cell within the parent bounds.
|
222
|
+
#
|
223
|
+
def x
|
224
|
+
@point[0]
|
225
|
+
end
|
226
|
+
|
227
|
+
# Set the x-position of the cell within the parent bounds.
|
228
|
+
#
|
229
|
+
def x=(val)
|
230
|
+
@point[0] = val
|
231
|
+
end
|
232
|
+
|
233
|
+
# y-position of the cell within the parent bounds.
|
234
|
+
#
|
235
|
+
def y
|
236
|
+
@point[1]
|
237
|
+
end
|
238
|
+
|
239
|
+
# Set the y-position of the cell within the parent bounds.
|
240
|
+
#
|
241
|
+
def y=(val)
|
242
|
+
@point[1] = val
|
243
|
+
end
|
244
|
+
|
245
|
+
# Sets padding on this cell. The argument can be one of:
|
246
|
+
#
|
247
|
+
# * an integer (sets all padding)
|
248
|
+
# * a two-element array [vertical, horizontal]
|
249
|
+
# * a three-element array [top, horizontal, bottom]
|
250
|
+
# * a four-element array [top, right, bottom, left]
|
251
|
+
#
|
252
|
+
def padding=(pad)
|
253
|
+
@padding = case
|
254
|
+
when pad.nil?
|
255
|
+
[0, 0, 0, 0]
|
256
|
+
when Numeric === pad # all padding
|
257
|
+
[pad, pad, pad, pad]
|
258
|
+
when pad.length == 2 # vert, horiz
|
259
|
+
[pad[0], pad[1], pad[0], pad[1]]
|
260
|
+
when pad.length == 3 # top, horiz, bottom
|
261
|
+
[pad[0], pad[1], pad[2], pad[1]]
|
262
|
+
when pad.length == 4 # top, right, bottom, left
|
263
|
+
[pad[0], pad[1], pad[2], pad[3]]
|
264
|
+
else
|
265
|
+
raise ArgumentError, ":padding must be a number or an array [v,h] " +
|
266
|
+
"or [t,r,b,l]"
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
protected
|
271
|
+
|
272
|
+
# Draws the cell's background color.
|
273
|
+
#
|
274
|
+
def draw_background(pt)
|
275
|
+
x, y = pt
|
276
|
+
margin = @border_width / 2
|
277
|
+
if @background_color
|
278
|
+
@pdf.mask(:fill_color) do
|
279
|
+
@pdf.fill_color @background_color
|
280
|
+
h = @borders.include?(:bottom) ? height - (2*margin) :
|
281
|
+
height + margin
|
282
|
+
@pdf.fill_rectangle [x, y], width, h
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
287
|
+
# Draws borders around the cell. Borders are centered on the bounds of
|
288
|
+
# the cell outside of any padding, so the caller is responsible for
|
289
|
+
# setting appropriate padding to ensure the border does not overlap with
|
290
|
+
# cell content.
|
291
|
+
#
|
292
|
+
def draw_borders(pt)
|
293
|
+
x, y = pt
|
294
|
+
return if @border_width <= 0
|
295
|
+
# Draw left / right borders one-half border width beyond the center of
|
296
|
+
# the corner, so that the corners end up square.
|
297
|
+
margin = @border_width / 2.0
|
298
|
+
|
299
|
+
@pdf.mask(:line_width, :stroke_color) do
|
300
|
+
@pdf.line_width = @border_width
|
301
|
+
@pdf.stroke_color = @border_color if @border_color
|
302
|
+
|
303
|
+
@borders.each do |border|
|
304
|
+
from, to = case border
|
305
|
+
when :top
|
306
|
+
[[x, y], [x+width, y]]
|
307
|
+
when :bottom
|
308
|
+
[[x, y-height], [x+width, y-height]]
|
309
|
+
when :left
|
310
|
+
[[x, y+margin], [x, y-height-margin]]
|
311
|
+
when :right
|
312
|
+
[[x+width, y+margin], [x+width, y-height-margin]]
|
313
|
+
end
|
314
|
+
@pdf.stroke_line(from, to)
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
# Draws cell content within the cell's bounding box. Must be implemented
|
320
|
+
# in subclasses.
|
321
|
+
#
|
322
|
+
def draw_content
|
323
|
+
raise NotImplementedError, "subclasses must implement draw_content"
|
324
|
+
end
|
325
|
+
|
326
|
+
def padding_top
|
327
|
+
@padding[0]
|
328
|
+
end
|
329
|
+
|
330
|
+
def padding_top=(val)
|
331
|
+
@padding[0] = val
|
332
|
+
end
|
333
|
+
|
334
|
+
def padding_right
|
335
|
+
@padding[1]
|
336
|
+
end
|
337
|
+
|
338
|
+
def padding_right=(val)
|
339
|
+
@padding[1] = val
|
340
|
+
end
|
341
|
+
|
342
|
+
def padding_bottom
|
343
|
+
@padding[2]
|
344
|
+
end
|
345
|
+
|
346
|
+
def padding_bottom=(val)
|
347
|
+
@padding[2] = val
|
348
|
+
end
|
349
|
+
|
350
|
+
def padding_left
|
351
|
+
@padding[3]
|
352
|
+
end
|
353
|
+
|
354
|
+
def padding_left=(val)
|
355
|
+
@padding[3] = val
|
356
|
+
end
|
357
|
+
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Accessors for using a Cell inside a Table.
|
4
|
+
|
5
|
+
module Prawn
|
6
|
+
class Table
|
7
|
+
class Cell
|
8
|
+
|
9
|
+
# This module extends Cell objects when they are used in a table (as
|
10
|
+
# opposed to standalone). Its properties apply to cells-in-tables but not
|
11
|
+
# cells themselves.
|
12
|
+
#
|
13
|
+
module InTable
|
14
|
+
|
15
|
+
# Row number (0-based).
|
16
|
+
#
|
17
|
+
attr_accessor :row
|
18
|
+
|
19
|
+
# Column number (0-based).
|
20
|
+
#
|
21
|
+
attr_accessor :column
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# subtable.rb: Yo dawg.
|
4
|
+
#
|
5
|
+
# Copyright January 2010, 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 another table.
|
13
|
+
#
|
14
|
+
class Subtable < Cell
|
15
|
+
|
16
|
+
attr_reader :subtable
|
17
|
+
|
18
|
+
def initialize(pdf, point, options={})
|
19
|
+
super
|
20
|
+
@subtable = options[:content]
|
21
|
+
|
22
|
+
# Subtable padding defaults to zero
|
23
|
+
@padding = [0, 0, 0, 0]
|
24
|
+
end
|
25
|
+
|
26
|
+
# Sets the text color of the entire subtable.
|
27
|
+
#
|
28
|
+
def text_color=(color)
|
29
|
+
@subtable.cells.text_color = color
|
30
|
+
end
|
31
|
+
|
32
|
+
# Proxied to subtable.
|
33
|
+
#
|
34
|
+
def natural_content_width
|
35
|
+
@subtable.cells.width
|
36
|
+
end
|
37
|
+
|
38
|
+
# Proxied to subtable.
|
39
|
+
#
|
40
|
+
def min_width
|
41
|
+
@subtable.cells.min_width
|
42
|
+
end
|
43
|
+
|
44
|
+
# Proxied to subtable.
|
45
|
+
#
|
46
|
+
def max_width
|
47
|
+
@subtable.cells.max_width
|
48
|
+
end
|
49
|
+
|
50
|
+
# Proxied to subtable.
|
51
|
+
#
|
52
|
+
def natural_content_height
|
53
|
+
@subtable.cells.height
|
54
|
+
end
|
55
|
+
|
56
|
+
# Draws the subtable.
|
57
|
+
#
|
58
|
+
def draw_content
|
59
|
+
@subtable.draw
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|