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,523 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# graphics.rb : Implements PDF drawing primitives
|
4
|
+
#
|
5
|
+
# Copyright April 2008, Gregory Brown. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
require "prawn/graphics/color"
|
10
|
+
require "prawn/graphics/dash"
|
11
|
+
require "prawn/graphics/cap_style"
|
12
|
+
require "prawn/graphics/join_style"
|
13
|
+
require "prawn/graphics/transparency"
|
14
|
+
require "prawn/graphics/transformation"
|
15
|
+
require "prawn/graphics/patterns"
|
16
|
+
|
17
|
+
module Prawn
|
18
|
+
|
19
|
+
# Implements the drawing facilities for Prawn::Document.
|
20
|
+
# Use this to draw the most beautiful imaginable things.
|
21
|
+
#
|
22
|
+
# This file lifts and modifies several of PDF::Writer's graphics functions
|
23
|
+
# ruby-pdf.rubyforge.org
|
24
|
+
#
|
25
|
+
module Graphics
|
26
|
+
|
27
|
+
include Color
|
28
|
+
include Dash
|
29
|
+
include CapStyle
|
30
|
+
include JoinStyle
|
31
|
+
include Transparency
|
32
|
+
include Transformation
|
33
|
+
include Patterns
|
34
|
+
|
35
|
+
#######################################################################
|
36
|
+
# Low level drawing operations must map the point to absolute coords! #
|
37
|
+
#######################################################################
|
38
|
+
|
39
|
+
# Moves the drawing position to a given point. The point can be
|
40
|
+
# specified as a tuple or a flattened argument list
|
41
|
+
#
|
42
|
+
# pdf.move_to [100,50]
|
43
|
+
# pdf.move_to(100,50)
|
44
|
+
#
|
45
|
+
def move_to(*point)
|
46
|
+
x,y = map_to_absolute(point)
|
47
|
+
add_content("%.3f %.3f m" % [ x, y ])
|
48
|
+
end
|
49
|
+
|
50
|
+
# Draws a line from the current drawing position to the specified point.
|
51
|
+
# The destination may be described as a tuple or a flattened list:
|
52
|
+
#
|
53
|
+
# pdf.line_to [50,50]
|
54
|
+
# pdf.line_to(50,50)
|
55
|
+
#
|
56
|
+
def line_to(*point)
|
57
|
+
x,y = map_to_absolute(point)
|
58
|
+
add_content("%.3f %.3f l" % [ x, y ])
|
59
|
+
end
|
60
|
+
|
61
|
+
# Draws a Bezier curve from the current drawing position to the
|
62
|
+
# specified point, bounded by two additional points.
|
63
|
+
#
|
64
|
+
# pdf.curve_to [100,100], :bounds => [[90,90],[75,75]]
|
65
|
+
#
|
66
|
+
def curve_to(dest,options={})
|
67
|
+
options[:bounds] or raise Prawn::Errors::InvalidGraphicsPath,
|
68
|
+
"Bounding points for bezier curve must be specified "+
|
69
|
+
"as :bounds => [[x1,y1],[x2,y2]]"
|
70
|
+
|
71
|
+
curve_points = (options[:bounds] << dest).map { |e| map_to_absolute(e) }
|
72
|
+
add_content("%.3f %.3f %.3f %.3f %.3f %.3f c" %
|
73
|
+
curve_points.flatten )
|
74
|
+
end
|
75
|
+
|
76
|
+
# Draws a rectangle given <tt>point</tt>, <tt>width</tt> and
|
77
|
+
# <tt>height</tt>. The rectangle is bounded by its upper-left corner.
|
78
|
+
#
|
79
|
+
# pdf.rectangle [300,300], 100, 200
|
80
|
+
#
|
81
|
+
def rectangle(point,width,height)
|
82
|
+
x,y = map_to_absolute(point)
|
83
|
+
add_content("%.3f %.3f %.3f %.3f re" % [ x, y - height, width, height ])
|
84
|
+
end
|
85
|
+
|
86
|
+
# Draws a rounded rectangle given <tt>point</tt>, <tt>width</tt> and
|
87
|
+
# <tt>height</tt> and <tt>radius</tt> for the rounded corner. The rectangle
|
88
|
+
# is bounded by its upper-left corner.
|
89
|
+
#
|
90
|
+
# pdf.rounded_rectangle [300,300], 100, 200, 10
|
91
|
+
#
|
92
|
+
def rounded_rectangle(point,width,height,radius)
|
93
|
+
x, y = point
|
94
|
+
rounded_polygon(radius, point, [x + width, y], [x + width, y - height], [x, y - height])
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
###########################################################
|
99
|
+
# Higher level functions: May use relative coords #
|
100
|
+
###########################################################
|
101
|
+
|
102
|
+
# Sets line thickness to the <tt>width</tt> specified.
|
103
|
+
#
|
104
|
+
def line_width=(width)
|
105
|
+
self.current_line_width = width
|
106
|
+
write_line_width
|
107
|
+
end
|
108
|
+
|
109
|
+
# When called without an argument, returns the current line thickness.
|
110
|
+
# When called with an argument, sets the line thickness to the specified
|
111
|
+
# value (in PDF points)
|
112
|
+
#
|
113
|
+
# pdf.line_width #=> 1
|
114
|
+
# pdf.line_width(5)
|
115
|
+
# pdf.line_width #=> 5
|
116
|
+
#
|
117
|
+
def line_width(width=nil)
|
118
|
+
if width
|
119
|
+
self.line_width = width
|
120
|
+
else
|
121
|
+
current_line_width
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Draws a line from one point to another. Points may be specified as
|
126
|
+
# tuples or flattened argument list:
|
127
|
+
#
|
128
|
+
# pdf.line [100,100], [200,250]
|
129
|
+
# pdf.line(100,100,200,250)
|
130
|
+
#
|
131
|
+
def line(*points)
|
132
|
+
x0,y0,x1,y1 = points.flatten
|
133
|
+
move_to(x0, y0)
|
134
|
+
line_to(x1, y1)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Draws a horizontal line from <tt>x1</tt> to <tt>x2</tt> at the
|
138
|
+
# current <tt>y</tt> position, or the position specified by the :at option.
|
139
|
+
#
|
140
|
+
# # draw a line from [25, 75] to [100, 75]
|
141
|
+
# horizontal_line 25, 100, :at => 75
|
142
|
+
#
|
143
|
+
def horizontal_line(x1,x2,options={})
|
144
|
+
if options[:at]
|
145
|
+
y1 = options[:at]
|
146
|
+
else
|
147
|
+
y1 = y - bounds.absolute_bottom
|
148
|
+
end
|
149
|
+
|
150
|
+
line(x1,y1,x2,y1)
|
151
|
+
end
|
152
|
+
|
153
|
+
# Draws a horizontal line from the left border to the right border of the
|
154
|
+
# bounding box at the current <tt>y</tt> position.
|
155
|
+
#
|
156
|
+
def horizontal_rule
|
157
|
+
horizontal_line(bounds.left, bounds.right)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Draws a vertical line at the x cooordinate given by :at from y1 to y2.
|
161
|
+
#
|
162
|
+
# # draw a line from [25, 100] to [25, 300]
|
163
|
+
# vertical_line 100, 300, :at => 25
|
164
|
+
#
|
165
|
+
def vertical_line(y1,y2,params)
|
166
|
+
line(params[:at],y1,params[:at],y2)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Draws a Bezier curve between two points, bounded by two additional
|
170
|
+
# points
|
171
|
+
#
|
172
|
+
# pdf.curve [50,100], [100,100], :bounds => [[90,90],[75,75]]
|
173
|
+
#
|
174
|
+
def curve(origin,dest, options={})
|
175
|
+
move_to(*origin)
|
176
|
+
curve_to(dest,options)
|
177
|
+
end
|
178
|
+
|
179
|
+
# This constant is used to approximate a symmetrical arc using a cubic
|
180
|
+
# Bezier curve.
|
181
|
+
#
|
182
|
+
KAPPA = 4.0 * ((Math.sqrt(2) - 1.0) / 3.0)
|
183
|
+
|
184
|
+
# <b>DEPRECATED:</b> Please use <tt>circle</tt> instead.
|
185
|
+
def circle_at(point, options)
|
186
|
+
warn "[DEPRECATION] 'circle_at' is deprecated in favor of 'circle'. " +
|
187
|
+
"'circle_at' will be removed in release 1.1"
|
188
|
+
circle(point, options[:radius])
|
189
|
+
end
|
190
|
+
|
191
|
+
# Draws a circle of radius <tt>radius</tt> with the centre-point at <tt>point</tt>
|
192
|
+
# as a complete subpath. The drawing point will be moved to the
|
193
|
+
# centre-point upon completion of the drawing the circle.
|
194
|
+
#
|
195
|
+
# pdf.circle [100,100], 25
|
196
|
+
#
|
197
|
+
def circle(center, radius)
|
198
|
+
ellipse(center, radius, radius)
|
199
|
+
end
|
200
|
+
|
201
|
+
# <b>DEPRECATED:</b> Please use <tt>ellipse</tt> instead.
|
202
|
+
def ellipse_at(point, r1, r2=r1)
|
203
|
+
warn "[DEPRECATION] 'ellipse_at' is deprecated in favor of 'ellipse'. " +
|
204
|
+
"'ellipse_at' will be removed in release 1.1"
|
205
|
+
ellipse(point, r1, r2)
|
206
|
+
end
|
207
|
+
|
208
|
+
# Draws an ellipse of +x+ radius <tt>r1</tt> and +y+ radius <tt>r2</tt>
|
209
|
+
# with the centre-point at <tt>point</tt> as a complete subpath. The
|
210
|
+
# drawing point will be moved to the centre-point upon completion of the
|
211
|
+
# drawing the ellipse.
|
212
|
+
#
|
213
|
+
# # draws an ellipse with x-radius 25 and y-radius 50
|
214
|
+
# pdf.ellipse [100,100], 25, 50
|
215
|
+
#
|
216
|
+
def ellipse(point, r1, r2 = r1)
|
217
|
+
x, y = point
|
218
|
+
l1 = r1 * KAPPA
|
219
|
+
l2 = r2 * KAPPA
|
220
|
+
|
221
|
+
move_to(x + r1, y)
|
222
|
+
|
223
|
+
# Upper right hand corner
|
224
|
+
curve_to [x, y + r2],
|
225
|
+
:bounds => [[x + r1, y + l2], [x + l1, y + r2]]
|
226
|
+
|
227
|
+
# Upper left hand corner
|
228
|
+
curve_to [x - r1, y],
|
229
|
+
:bounds => [[x - l1, y + r2], [x - r1, y + l2]]
|
230
|
+
|
231
|
+
# Lower left hand corner
|
232
|
+
curve_to [x, y - r2],
|
233
|
+
:bounds => [[x - r1, y - l2], [x - l1, y - r2]]
|
234
|
+
|
235
|
+
# Lower right hand corner
|
236
|
+
curve_to [x + r1, y],
|
237
|
+
:bounds => [[x + l1, y - r2], [x + r1, y - l2]]
|
238
|
+
|
239
|
+
move_to(x, y)
|
240
|
+
end
|
241
|
+
|
242
|
+
# Draws a polygon from the specified points.
|
243
|
+
#
|
244
|
+
# # draws a snazzy triangle
|
245
|
+
# pdf.polygon [100,100], [100,200], [200,200]
|
246
|
+
#
|
247
|
+
def polygon(*points)
|
248
|
+
move_to points[0]
|
249
|
+
(points[1..-1] << points[0]).each do |point|
|
250
|
+
line_to(*point)
|
251
|
+
end
|
252
|
+
# close the path
|
253
|
+
add_content "h"
|
254
|
+
end
|
255
|
+
|
256
|
+
# Draws a rounded polygon from specified points using the radius to define bezier curves
|
257
|
+
#
|
258
|
+
# # draws a rounded filled in polygon
|
259
|
+
# pdf.fill_and_stroke_rounded_polygon(10, [100, 250], [200, 300], [300, 250],
|
260
|
+
# [300, 150], [200, 100], [100, 150])
|
261
|
+
def rounded_polygon(radius, *points)
|
262
|
+
move_to point_on_line(radius, points[1], points[0])
|
263
|
+
sides = points.size
|
264
|
+
points << points[0] << points[1]
|
265
|
+
(sides).times do |i|
|
266
|
+
rounded_vertex(radius, points[i], points[i + 1], points[i + 2])
|
267
|
+
end
|
268
|
+
# close the path
|
269
|
+
add_content "h"
|
270
|
+
end
|
271
|
+
|
272
|
+
|
273
|
+
# Creates a rounded vertex for a line segment used for building a rounded polygon
|
274
|
+
# requires a radius to define bezier curve and three points. The first two points define
|
275
|
+
# the line segment and the third point helps define the curve for the vertex.
|
276
|
+
def rounded_vertex(radius, *points)
|
277
|
+
x0,y0,x1,y1,x2,y2 = points.flatten
|
278
|
+
radial_point_1 = point_on_line(radius, points[0], points[1])
|
279
|
+
bezier_point_1 = point_on_line((radius - radius*KAPPA), points[0], points[1] )
|
280
|
+
radial_point_2 = point_on_line(radius, points[2], points[1])
|
281
|
+
bezier_point_2 = point_on_line((radius - radius*KAPPA), points[2], points[1])
|
282
|
+
line_to(radial_point_1)
|
283
|
+
curve_to(radial_point_2, :bounds => [bezier_point_1, bezier_point_2])
|
284
|
+
end
|
285
|
+
|
286
|
+
# Strokes the current path. If a block is provided, yields to the block
|
287
|
+
# before closing the path. See Graphics::Color for color details.
|
288
|
+
#
|
289
|
+
def stroke
|
290
|
+
yield if block_given?
|
291
|
+
add_content "S"
|
292
|
+
end
|
293
|
+
|
294
|
+
# Closes and strokes the current path. If a block is provided, yields to
|
295
|
+
# the block before closing the path. See Graphics::Color for color details.
|
296
|
+
#
|
297
|
+
def close_and_stroke
|
298
|
+
yield if block_given?
|
299
|
+
add_content "s"
|
300
|
+
end
|
301
|
+
|
302
|
+
# Draws and strokes a rectangle represented by the current bounding box
|
303
|
+
#
|
304
|
+
def stroke_bounds
|
305
|
+
stroke_rectangle bounds.top_left, bounds.width, bounds.height
|
306
|
+
end
|
307
|
+
|
308
|
+
# Closes and fills the current path. See Graphics::Color for color details.
|
309
|
+
#
|
310
|
+
# If the option :fill_rule => :even_odd is specified, Prawn will use the
|
311
|
+
# even-odd rule to fill the path. Otherwise, the nonzero winding number rule
|
312
|
+
# will be used. See the PDF reference, "Graphics -> Path Construction and
|
313
|
+
# Painting -> Clipping Path Operators" for details on the difference.
|
314
|
+
#
|
315
|
+
def fill(options={})
|
316
|
+
yield if block_given?
|
317
|
+
add_content(options[:fill_rule] == :even_odd ? "f*" : "f")
|
318
|
+
end
|
319
|
+
|
320
|
+
# Closes, fills, and strokes the current path. If a block is provided,
|
321
|
+
# yields to the block before closing the path. See Graphics::Color for
|
322
|
+
# color details.
|
323
|
+
#
|
324
|
+
# If the option :fill_rule => :even_odd is specified, Prawn will use the
|
325
|
+
# even-odd rule to fill the path. Otherwise, the nonzero winding number rule
|
326
|
+
# will be used. See the PDF reference, "Graphics -> Path Construction and
|
327
|
+
# Painting -> Clipping Path Operators" for details on the difference.
|
328
|
+
#
|
329
|
+
def fill_and_stroke(options={})
|
330
|
+
yield if block_given?
|
331
|
+
add_content(options[:fill_rule] == :even_odd ? "b*" : "b")
|
332
|
+
end
|
333
|
+
|
334
|
+
# Closes the current path.
|
335
|
+
#
|
336
|
+
def close_path
|
337
|
+
add_content "h"
|
338
|
+
end
|
339
|
+
|
340
|
+
##
|
341
|
+
# :method: stroke_rectangle
|
342
|
+
# Draws and strokes a rectangle given +point+, +width+ and +height+. The rectangle is bounded by its upper-left corner.
|
343
|
+
# :call-seq:
|
344
|
+
# stroke_rectangle(point,width,height)
|
345
|
+
|
346
|
+
##
|
347
|
+
# :method: fill_rectangle
|
348
|
+
# Draws and fills ills a rectangle given +point+, +width+ and +height+. The rectangle is bounded by its upper-left corner.
|
349
|
+
# :call-seq:
|
350
|
+
# fill_rectangle(point,width,height)
|
351
|
+
|
352
|
+
##
|
353
|
+
# :method: fill_and_stroke_rectangle
|
354
|
+
# Draws, fills, and strokes a rectangle given +point+, +width+ and +height+. The rectangle is bounded by its upper-left corner.
|
355
|
+
# :call-seq:
|
356
|
+
# fill_and_stroke_rectangle(point,width,height)
|
357
|
+
|
358
|
+
##
|
359
|
+
# :method: stroke_rounded_rectangle
|
360
|
+
# Draws and strokes a rounded rectangle given +point+, +width+ and +height+ and +radius+ for the rounded corner. The rectangle is bounded by its upper-left corner.
|
361
|
+
# :call-seq:
|
362
|
+
# stroke_rounded_rectangle(point,width,height,radius)
|
363
|
+
|
364
|
+
##
|
365
|
+
# :method: fill_rounded_rectangle
|
366
|
+
# Draws and fills a rounded rectangle given +point+, +width+ and +height+ and +radius+ for the rounded corner. The rectangle is bounded by its upper-left corner.
|
367
|
+
# :call-seq:
|
368
|
+
# fill_rounded_rectangle(point,width,height,radius)
|
369
|
+
|
370
|
+
##
|
371
|
+
# :method: stroke_and_fill_rounded_rectangle
|
372
|
+
# Draws, fills, and strokes a rounded rectangle given +point+, +width+ and +height+ and +radius+ for the rounded corner. The rectangle is bounded by its upper-left corner.
|
373
|
+
# :call-seq:
|
374
|
+
# stroke_and_fill_rounded_rectangle(point,width,height,radius)
|
375
|
+
|
376
|
+
##
|
377
|
+
# :method: stroke_line
|
378
|
+
# Strokes a line from one point to another. Points may be specified as tuples or flattened argument list.
|
379
|
+
# :call-seq:
|
380
|
+
# stroke_line(*points)
|
381
|
+
|
382
|
+
##
|
383
|
+
# :method: stroke_horizontal_line
|
384
|
+
# Strokes a horizontal line from +x1+ to +x2+ at the current y position, or the position specified by the :at option.
|
385
|
+
# :call-seq:
|
386
|
+
# stroke_horizontal_line(x1,x2,options={})
|
387
|
+
|
388
|
+
##
|
389
|
+
# :method: stroke_horizontal_rule
|
390
|
+
# Strokes a horizontal line from the left border to the right border of the bounding box at the current y position.
|
391
|
+
|
392
|
+
##
|
393
|
+
# :method: stroke_vertical_line
|
394
|
+
# Strokes a vertical line at the x coordinate given by :at from y1 to y2.
|
395
|
+
# :call-seq:
|
396
|
+
# stroke_vertical_line(y1,y2,params)
|
397
|
+
|
398
|
+
##
|
399
|
+
# :method: stroke_curve
|
400
|
+
# Strokes a Bezier curve between two points, bounded by two additional points.
|
401
|
+
# :call-seq:
|
402
|
+
# stroke_curve(origin,dest,options={})
|
403
|
+
|
404
|
+
##
|
405
|
+
# :method: stroke_circle
|
406
|
+
# Draws and strokes a circle of radius +radius+ with the centre-point at +point+.
|
407
|
+
# :call-seq:
|
408
|
+
# stroke_circle(center,radius)
|
409
|
+
|
410
|
+
##
|
411
|
+
# :method: fill_circle
|
412
|
+
# Draws and fills a circle of radius +radius+ with the centre-point at +point+.
|
413
|
+
# :call-seq:
|
414
|
+
# fill_circle(center,radius)
|
415
|
+
|
416
|
+
##
|
417
|
+
# :method: fill_and_stroke_circle
|
418
|
+
# Draws, strokes, and fills a circle of radius +radius+ with the centre-point at +point+.
|
419
|
+
# :call-seq:
|
420
|
+
# fill_and_stroke_circle(center,radius)
|
421
|
+
|
422
|
+
##
|
423
|
+
# :method: stroke_ellipse
|
424
|
+
# Draws and strokes an ellipse of x radius +r1+ and y radius +r2+ with the centre-point at +point+.
|
425
|
+
# :call-seq:
|
426
|
+
# stroke_ellipse(point, r1, r2 = r1)
|
427
|
+
|
428
|
+
##
|
429
|
+
# :method: fill_ellipse
|
430
|
+
# Draws and fills an ellipse of x radius +r1+ and y radius +r2+ with the centre-point at +point+.
|
431
|
+
# :call-seq:
|
432
|
+
# fill_ellipse(point, r1, r2 = r1)
|
433
|
+
|
434
|
+
##
|
435
|
+
# :method: fill_and_stroke_ellipse
|
436
|
+
# Draws, strokes, and fills an ellipse of x radius +r1+ and y radius +r2+ with the centre-point at +point+.
|
437
|
+
# :call-seq:
|
438
|
+
# fill_and_stroke_ellipse(point, r1, r2 = r1)
|
439
|
+
|
440
|
+
##
|
441
|
+
# :method: stroke_polygon
|
442
|
+
# Draws and strokes a polygon from the specified points.
|
443
|
+
# :call-seq:
|
444
|
+
# stroke_polygon(*points)
|
445
|
+
|
446
|
+
##
|
447
|
+
# :method: fill_polygon
|
448
|
+
# Draws and fills a polygon from the specified points.
|
449
|
+
# :call-seq:
|
450
|
+
# fill_polygon(*points)
|
451
|
+
|
452
|
+
##
|
453
|
+
# :method: fill_and_stroke_polygon
|
454
|
+
# Draws, strokes, and fills a polygon from the specified points.
|
455
|
+
# :call-seq:
|
456
|
+
# fill_and_stroke_polygon(*points)
|
457
|
+
|
458
|
+
##
|
459
|
+
# :method: stroke_rounded_polygon
|
460
|
+
# Draws and strokes a rounded polygon from specified points, using +radius+ to define Bezier curves.
|
461
|
+
# :call-seq:
|
462
|
+
# stroke_rounded_polygon(radius, *points)
|
463
|
+
|
464
|
+
##
|
465
|
+
# :method: fill_rounded_polygon
|
466
|
+
# Draws and fills a rounded polygon from specified points, using +radius+ to define Bezier curves.
|
467
|
+
# :call-seq:
|
468
|
+
# fill_rounded_polygon(radius, *points)
|
469
|
+
|
470
|
+
##
|
471
|
+
# :method: fill_and_stroke_rounded_polygon
|
472
|
+
# Draws, strokes, and fills a rounded polygon from specified points, using +radius+ to define Bezier curves.
|
473
|
+
# :call-seq:
|
474
|
+
# fill_and_stroke_rounded_polygon(radius, *points)
|
475
|
+
|
476
|
+
ops = %w{fill stroke fill_and_stroke}
|
477
|
+
shapes = %w{line_to curve_to rectangle rounded_rectangle line horizontal_line horizontal_rule vertical_line
|
478
|
+
curve circle_at circle ellipse_at ellipse polygon rounded_polygon rounded_vertex}
|
479
|
+
|
480
|
+
ops.product(shapes).each do |operation,shape|
|
481
|
+
class_eval "def #{operation}_#{shape}(*args); #{shape}(*args); #{operation}; end"
|
482
|
+
end
|
483
|
+
|
484
|
+
private
|
485
|
+
|
486
|
+
def current_line_width
|
487
|
+
graphic_state.line_width
|
488
|
+
end
|
489
|
+
|
490
|
+
def current_line_width=(width)
|
491
|
+
graphic_state.line_width = width
|
492
|
+
end
|
493
|
+
|
494
|
+
def write_line_width
|
495
|
+
add_content("#{current_line_width} w")
|
496
|
+
end
|
497
|
+
|
498
|
+
def map_to_absolute(*point)
|
499
|
+
x,y = point.flatten
|
500
|
+
[@bounding_box.absolute_left + x, @bounding_box.absolute_bottom + y]
|
501
|
+
end
|
502
|
+
|
503
|
+
def map_to_absolute!(point)
|
504
|
+
point.replace(map_to_absolute(point))
|
505
|
+
end
|
506
|
+
|
507
|
+
def degree_to_rad(angle)
|
508
|
+
angle * Math::PI / 180
|
509
|
+
end
|
510
|
+
|
511
|
+
# Returns the coordinates for a point on a line that is a given distance away from the second
|
512
|
+
# point defining the line segement
|
513
|
+
def point_on_line(distance_from_end, *points)
|
514
|
+
x0,y0,x1,y1 = points.flatten
|
515
|
+
length = Math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
|
516
|
+
p = (length - distance_from_end) / length
|
517
|
+
xr = x0 + p*(x1 - x0)
|
518
|
+
yr = y0 + p*(y1 - y0)
|
519
|
+
[xr, yr]
|
520
|
+
end
|
521
|
+
|
522
|
+
end
|
523
|
+
end
|