prawn-git 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +10 -0
- data/COPYING +2 -0
- data/GPLv2 +340 -0
- data/GPLv3 +674 -0
- data/Gemfile +11 -0
- data/LICENSE +56 -0
- data/Rakefile +55 -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/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/images/16bit.alpha +0 -0
- data/data/images/16bit.color +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/dice.alpha +0 -0
- data/data/images/dice.color +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/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/license.md +8 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.color +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pal_bk.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/prawn.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 +89 -0
- data/lib/prawn/document.rb +706 -0
- data/lib/prawn/document/bounding_box.rb +539 -0
- data/lib/prawn/document/column_box.rb +144 -0
- data/lib/prawn/document/internals.rb +58 -0
- data/lib/prawn/document/span.rb +57 -0
- data/lib/prawn/encoding.rb +87 -0
- data/lib/prawn/errors.rb +80 -0
- data/lib/prawn/font.rb +413 -0
- data/lib/prawn/font/afm.rb +256 -0
- data/lib/prawn/font/dfont.rb +43 -0
- data/lib/prawn/font/ttf.rb +355 -0
- data/lib/prawn/font_metric_cache.rb +46 -0
- data/lib/prawn/graphics.rb +646 -0
- data/lib/prawn/graphics/cap_style.rb +47 -0
- data/lib/prawn/graphics/color.rb +232 -0
- data/lib/prawn/graphics/dash.rb +109 -0
- data/lib/prawn/graphics/join_style.rb +49 -0
- data/lib/prawn/graphics/patterns.rb +126 -0
- data/lib/prawn/graphics/transformation.rb +157 -0
- data/lib/prawn/graphics/transparency.rb +101 -0
- data/lib/prawn/grid.rb +279 -0
- data/lib/prawn/image_handler.rb +44 -0
- data/lib/prawn/images.rb +199 -0
- data/lib/prawn/images/image.rb +49 -0
- data/lib/prawn/images/jpg.rb +91 -0
- data/lib/prawn/images/png.rb +290 -0
- data/lib/prawn/measurement_extensions.rb +50 -0
- data/lib/prawn/measurements.rb +77 -0
- data/lib/prawn/outline.rb +289 -0
- data/lib/prawn/repeater.rb +124 -0
- data/lib/prawn/security.rb +288 -0
- data/lib/prawn/security/arcfour.rb +54 -0
- data/lib/prawn/soft_mask.rb +94 -0
- data/lib/prawn/stamp.rb +136 -0
- data/lib/prawn/text.rb +437 -0
- data/lib/prawn/text/box.rb +141 -0
- data/lib/prawn/text/formatted.rb +7 -0
- data/lib/prawn/text/formatted/arranger.rb +290 -0
- data/lib/prawn/text/formatted/box.rb +614 -0
- data/lib/prawn/text/formatted/fragment.rb +264 -0
- data/lib/prawn/text/formatted/line_wrap.rb +277 -0
- data/lib/prawn/text/formatted/parser.rb +224 -0
- data/lib/prawn/text/formatted/wrap.rb +160 -0
- data/lib/prawn/utilities.rb +46 -0
- data/lib/prawn/version.rb +5 -0
- data/lib/prawn/view.rb +91 -0
- data/manual/absolute_position.pdf +0 -0
- data/manual/basic_concepts/adding_pages.rb +27 -0
- data/manual/basic_concepts/basic_concepts.rb +36 -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/basic_concepts/view.rb +42 -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/contents.rb +29 -0
- data/manual/cover.rb +39 -0
- data/manual/document_and_page_options/background.rb +27 -0
- data/manual/document_and_page_options/document_and_page_options.rb +32 -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/document_and_page_options/print_scaling.rb +20 -0
- data/manual/example_helper.rb +7 -0
- data/manual/graphics/circle_and_ellipse.rb +22 -0
- data/manual/graphics/color.rb +24 -0
- data/manual/graphics/common_lines.rb +30 -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 +24 -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 +48 -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/how_to_read_this_manual.rb +40 -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/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/alternate_page_numbering.rb +32 -0
- data/manual/repeatable_content/page_numbering.rb +54 -0
- data/manual/repeatable_content/repeatable_content.rb +32 -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/table.rb +16 -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/inline.rb +41 -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 +34 -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 +47 -0
- data/manual/text/rotation.rb +43 -0
- data/manual/text/single_usage.rb +37 -0
- data/manual/text/text.rb +73 -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 +48 -0
- data/manual/text/utf8.rb +28 -0
- data/manual/text/win_ansi_charset.rb +60 -0
- data/prawn.gemspec +45 -0
- data/spec/acceptance/png.rb +25 -0
- data/spec/annotations_spec.rb +74 -0
- data/spec/bounding_box_spec.rb +510 -0
- data/spec/column_box_spec.rb +65 -0
- data/spec/data/curves.pdf +66 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +748 -0
- data/spec/extensions/encoding_helpers.rb +11 -0
- data/spec/extensions/mocha.rb +46 -0
- data/spec/font_metric_cache_spec.rb +52 -0
- data/spec/font_spec.rb +474 -0
- data/spec/formatted_text_arranger_spec.rb +421 -0
- data/spec/formatted_text_box_spec.rb +705 -0
- data/spec/formatted_text_fragment_spec.rb +298 -0
- data/spec/graphics_spec.rb +683 -0
- data/spec/grid_spec.rb +96 -0
- data/spec/image_handler_spec.rb +54 -0
- data/spec/images_spec.rb +153 -0
- data/spec/inline_formatted_text_parser_spec.rb +564 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +367 -0
- data/spec/measurement_units_spec.rb +25 -0
- data/spec/outline_spec.rb +430 -0
- data/spec/png_spec.rb +245 -0
- data/spec/reference_spec.rb +25 -0
- data/spec/repeater_spec.rb +160 -0
- data/spec/security_spec.rb +158 -0
- data/spec/soft_mask_spec.rb +79 -0
- data/spec/span_spec.rb +44 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/stamp_spec.rb +160 -0
- data/spec/stroke_styles_spec.rb +211 -0
- data/spec/text_at_spec.rb +143 -0
- data/spec/text_box_spec.rb +1043 -0
- data/spec/text_rendering_mode_spec.rb +45 -0
- data/spec/text_spacing_spec.rb +93 -0
- data/spec/text_spec.rb +557 -0
- data/spec/text_with_inline_formatting_spec.rb +35 -0
- data/spec/transparency_spec.rb +91 -0
- data/spec/view_spec.rb +43 -0
- metadata +509 -0
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# cap_style.rb : Implements stroke cap styling
|
4
|
+
#
|
5
|
+
# Contributed by Daniel Nelson. October, 2009
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
#
|
9
|
+
module Prawn
|
10
|
+
module Graphics
|
11
|
+
module CapStyle
|
12
|
+
# @group Stable API
|
13
|
+
|
14
|
+
CAP_STYLES = { :butt => 0, :round => 1, :projecting_square => 2 }
|
15
|
+
|
16
|
+
# Sets the cap style for stroked lines and curves
|
17
|
+
#
|
18
|
+
# style is one of :butt, :round, or :projecting_square
|
19
|
+
#
|
20
|
+
# NOTE: If this method is never called, :butt will be used by default.
|
21
|
+
#
|
22
|
+
def cap_style(style=nil)
|
23
|
+
return current_cap_style || :butt if style.nil?
|
24
|
+
|
25
|
+
self.current_cap_style = style
|
26
|
+
|
27
|
+
write_stroke_cap_style
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :cap_style=, :cap_style
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def current_cap_style
|
35
|
+
graphic_state.cap_style
|
36
|
+
end
|
37
|
+
|
38
|
+
def current_cap_style=(style)
|
39
|
+
graphic_state.cap_style = style
|
40
|
+
end
|
41
|
+
|
42
|
+
def write_stroke_cap_style
|
43
|
+
renderer.add_content "#{CAP_STYLES[current_cap_style]} J"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,232 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# color.rb : Implements color handling
|
4
|
+
#
|
5
|
+
# Copyright June 2008, Gregory Brown. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
module Prawn
|
10
|
+
module Graphics
|
11
|
+
module Color
|
12
|
+
# @group Stable API
|
13
|
+
|
14
|
+
# Sets or returns the fill color.
|
15
|
+
#
|
16
|
+
# When called with no argument, it returns the current fill color.
|
17
|
+
#
|
18
|
+
# If a single argument is provided, it should be a 6 digit HTML color
|
19
|
+
# code.
|
20
|
+
#
|
21
|
+
# pdf.fill_color "f0ffc1"
|
22
|
+
#
|
23
|
+
# If 4 arguments are provided, the color is assumed to be a CMYK value
|
24
|
+
# Values range from 0 - 100.
|
25
|
+
#
|
26
|
+
# pdf.fill_color 0, 99, 95, 0
|
27
|
+
#
|
28
|
+
def fill_color(*color)
|
29
|
+
return current_fill_color if color.empty?
|
30
|
+
self.current_fill_color = process_color(*color)
|
31
|
+
set_fill_color
|
32
|
+
end
|
33
|
+
|
34
|
+
alias_method :fill_color=, :fill_color
|
35
|
+
|
36
|
+
# Sets or returns the line stroking color.
|
37
|
+
#
|
38
|
+
# When called with no argument, it returns the current stroking color.
|
39
|
+
#
|
40
|
+
# If a single argument is provided, it should be a 6 digit HTML color
|
41
|
+
# code.
|
42
|
+
#
|
43
|
+
# pdf.stroke_color "f0ffc1"
|
44
|
+
#
|
45
|
+
# If 4 arguments are provided, the color is assumed to be a CMYK value
|
46
|
+
# Values range from 0 - 100.
|
47
|
+
#
|
48
|
+
# pdf.stroke_color 0, 99, 95, 0
|
49
|
+
#
|
50
|
+
def stroke_color(*color)
|
51
|
+
return current_stroke_color if color.empty?
|
52
|
+
color = process_color(*color)
|
53
|
+
self.current_stroke_color = color
|
54
|
+
set_stroke_color(color)
|
55
|
+
end
|
56
|
+
|
57
|
+
alias_method :stroke_color=, :stroke_color
|
58
|
+
|
59
|
+
module_function
|
60
|
+
|
61
|
+
# Converts RGB value array to hex string suitable for use with fill_color
|
62
|
+
# and stroke_color
|
63
|
+
#
|
64
|
+
# >> Prawn::Graphics::Color.rgb2hex([255,120,8])
|
65
|
+
# => "ff7808"
|
66
|
+
#
|
67
|
+
def rgb2hex(rgb)
|
68
|
+
rgb.map { |e| "%02x" % e }.join
|
69
|
+
end
|
70
|
+
|
71
|
+
# Converts hex string into RGB value array:
|
72
|
+
#
|
73
|
+
# >> Prawn::Graphics::Color.hex2rgb("ff7808")
|
74
|
+
# => [255, 120, 8]
|
75
|
+
#
|
76
|
+
def hex2rgb(hex)
|
77
|
+
r,g,b = hex[0..1], hex[2..3], hex[4..5]
|
78
|
+
[r,g,b].map { |e| e.to_i(16) }
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def process_color(*color)
|
84
|
+
case(color.size)
|
85
|
+
when 1
|
86
|
+
color[0]
|
87
|
+
when 4
|
88
|
+
color
|
89
|
+
else
|
90
|
+
raise ArgumentError, 'wrong number of arguments supplied'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def color_type(color)
|
95
|
+
case color
|
96
|
+
when String
|
97
|
+
:RGB
|
98
|
+
when Array
|
99
|
+
case color.length
|
100
|
+
when 3
|
101
|
+
:RGB
|
102
|
+
when 4
|
103
|
+
:CMYK
|
104
|
+
else
|
105
|
+
raise ArgumentError, "Unknown type of color: #{color.inspect}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def normalize_color(color)
|
111
|
+
case color_type(color)
|
112
|
+
when :RGB
|
113
|
+
r,g,b = hex2rgb(color)
|
114
|
+
[r / 255.0, g / 255.0, b / 255.0]
|
115
|
+
when :CMYK
|
116
|
+
c,m,y,k = *color
|
117
|
+
[c / 100.0, m / 100.0, y / 100.0, k / 100.0]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def color_to_s(color)
|
122
|
+
normalize_color(color).map { |c| '%.3f' % c }.join(' ')
|
123
|
+
end
|
124
|
+
|
125
|
+
def color_space(color)
|
126
|
+
case color_type(color)
|
127
|
+
when :RGB
|
128
|
+
:DeviceRGB
|
129
|
+
when :CMYK
|
130
|
+
:DeviceCMYK
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
COLOR_SPACES = [:DeviceRGB, :DeviceCMYK, :Pattern]
|
135
|
+
|
136
|
+
def set_color_space(type, color_space)
|
137
|
+
# don't set the same color space again
|
138
|
+
return if current_color_space(type) == color_space && !state.page.in_stamp_stream?
|
139
|
+
set_current_color_space(color_space, type)
|
140
|
+
|
141
|
+
unless COLOR_SPACES.include?(color_space)
|
142
|
+
raise ArgumentError, "unknown color space: '#{color_space}'"
|
143
|
+
end
|
144
|
+
|
145
|
+
operator = case type
|
146
|
+
when :fill
|
147
|
+
'cs'
|
148
|
+
when :stroke
|
149
|
+
'CS'
|
150
|
+
else
|
151
|
+
raise ArgumentError, "unknown type '#{type}'"
|
152
|
+
end
|
153
|
+
|
154
|
+
renderer.add_content "/#{color_space} #{operator}"
|
155
|
+
end
|
156
|
+
|
157
|
+
def set_color(type, color, options = {})
|
158
|
+
operator = case type
|
159
|
+
when :fill
|
160
|
+
'scn'
|
161
|
+
when :stroke
|
162
|
+
'SCN'
|
163
|
+
else
|
164
|
+
raise ArgumentError, "unknown type '#{type}'"
|
165
|
+
end
|
166
|
+
|
167
|
+
if options[:pattern]
|
168
|
+
set_color_space type, :Pattern
|
169
|
+
renderer.add_content "/#{color} #{operator}"
|
170
|
+
else
|
171
|
+
set_color_space type, color_space(color)
|
172
|
+
color = color_to_s(color)
|
173
|
+
write_color(color, operator)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def set_fill_color(color = nil)
|
178
|
+
set_color :fill, color || current_fill_color
|
179
|
+
end
|
180
|
+
|
181
|
+
def set_stroke_color(color = nil)
|
182
|
+
set_color :stroke, color || current_stroke_color
|
183
|
+
end
|
184
|
+
|
185
|
+
def update_colors
|
186
|
+
set_fill_color
|
187
|
+
set_stroke_color
|
188
|
+
end
|
189
|
+
|
190
|
+
private
|
191
|
+
|
192
|
+
def current_color_space(type)
|
193
|
+
graphic_state.color_space[type]
|
194
|
+
end
|
195
|
+
|
196
|
+
def set_current_color_space(color_space, type)
|
197
|
+
save_graphics_state if graphic_state.nil?
|
198
|
+
graphic_state.color_space[type] = color_space
|
199
|
+
end
|
200
|
+
|
201
|
+
def current_fill_color
|
202
|
+
graphic_state.fill_color
|
203
|
+
end
|
204
|
+
|
205
|
+
def current_fill_color=(color)
|
206
|
+
graphic_state.fill_color = color
|
207
|
+
end
|
208
|
+
|
209
|
+
def current_stroke_color
|
210
|
+
graphic_state.stroke_color
|
211
|
+
end
|
212
|
+
|
213
|
+
def current_stroke_color=(color)
|
214
|
+
graphic_state.stroke_color = color
|
215
|
+
end
|
216
|
+
|
217
|
+
def write_fill_color
|
218
|
+
write_color(current_fill_color, 'scn')
|
219
|
+
end
|
220
|
+
|
221
|
+
def write_stroke_color
|
222
|
+
write_color(current_fill_color, 'SCN')
|
223
|
+
end
|
224
|
+
|
225
|
+
def write_color(color, operator)
|
226
|
+
renderer.add_content "#{color} #{operator}"
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# dash.rb : Implements stroke dashing
|
4
|
+
#
|
5
|
+
# Contributed by Daniel Nelson. October, 2009
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
#
|
9
|
+
module Prawn
|
10
|
+
module Graphics
|
11
|
+
module Dash
|
12
|
+
# @group Stable API
|
13
|
+
|
14
|
+
# Sets the dash pattern for stroked lines and curves or return the
|
15
|
+
# current dash pattern setting if +length+ is nil.
|
16
|
+
#
|
17
|
+
# There are two ways to set the dash pattern:
|
18
|
+
#
|
19
|
+
# * If the parameter +length+ is an Integer/Float, it specifies
|
20
|
+
# the length of the dash and of the gap. The length of the gap
|
21
|
+
# can be customized by setting the :space option.
|
22
|
+
#
|
23
|
+
# Examples:
|
24
|
+
#
|
25
|
+
# length = 3
|
26
|
+
# 3 on, 3 off, 3 on, 3 off, ...
|
27
|
+
# length = 3, :space =2
|
28
|
+
# 3 on, 2 off, 3 on, 2 off, ...
|
29
|
+
#
|
30
|
+
# * If the parameter +length+ is an array, it specifies the
|
31
|
+
# lengths of alternating dashes and gaps. The :space option is
|
32
|
+
# ignored in this case.
|
33
|
+
#
|
34
|
+
# Examples:
|
35
|
+
#
|
36
|
+
# length = [2, 1]
|
37
|
+
# 2 on, 1 off, 2 on, 1 off, ...
|
38
|
+
# length = [3, 1, 2, 3]
|
39
|
+
# 3 on, 1 off, 2 on, 3 off, 3 on, 1 off, ...
|
40
|
+
#
|
41
|
+
# Options may contain the keys :space and :phase
|
42
|
+
#
|
43
|
+
# :space:: The space between the dashes (only used when +length+
|
44
|
+
# is not an array)
|
45
|
+
#
|
46
|
+
# :phase:: The distance into the dash pattern at which to start
|
47
|
+
# the dash. For example, a phase of 0 starts at the
|
48
|
+
# beginning of the dash; whereas, if the phase is equal
|
49
|
+
# to the length of the dash, then stroking will begin at
|
50
|
+
# the beginning of the space. Default is 0.
|
51
|
+
#
|
52
|
+
# Integers or Floats may be used for length and the option values.
|
53
|
+
# Dash units are in PDF points (1/72 inch).
|
54
|
+
#
|
55
|
+
def dash(length=nil, options={})
|
56
|
+
return current_dash_state if length.nil?
|
57
|
+
|
58
|
+
if length == 0 || length.kind_of?(Array) && length.any? { |e| e == 0 }
|
59
|
+
raise ArgumentError,
|
60
|
+
"Zero length dashes are invalid. Call #undash to disable dashes."
|
61
|
+
end
|
62
|
+
|
63
|
+
self.current_dash_state = { :dash => length,
|
64
|
+
:space => length.kind_of?(Array) ? nil : options[:space] || length,
|
65
|
+
:phase => options[:phase] || 0 }
|
66
|
+
|
67
|
+
write_stroke_dash
|
68
|
+
end
|
69
|
+
|
70
|
+
alias_method :dash=, :dash
|
71
|
+
|
72
|
+
# Stops dashing, restoring solid stroked lines and curves
|
73
|
+
#
|
74
|
+
def undash
|
75
|
+
self.current_dash_state = undashed_setting
|
76
|
+
write_stroke_dash
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns when stroke is dashed, false otherwise
|
80
|
+
#
|
81
|
+
def dashed?
|
82
|
+
current_dash_state != undashed_setting
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def write_stroke_dash
|
88
|
+
renderer.add_content dash_setting
|
89
|
+
end
|
90
|
+
|
91
|
+
def undashed_setting
|
92
|
+
{ :dash => nil, :space => nil, :phase => 0 }
|
93
|
+
end
|
94
|
+
|
95
|
+
def current_dash_state=(dash_options)
|
96
|
+
graphic_state.dash = dash_options
|
97
|
+
end
|
98
|
+
|
99
|
+
def current_dash_state
|
100
|
+
graphic_state.dash
|
101
|
+
end
|
102
|
+
|
103
|
+
def dash_setting
|
104
|
+
graphic_state.dash_setting
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# join_style.rb : Implements stroke join styling
|
4
|
+
#
|
5
|
+
# Contributed by Daniel Nelson. October, 2009
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
#
|
9
|
+
module Prawn
|
10
|
+
module Graphics
|
11
|
+
module JoinStyle
|
12
|
+
JOIN_STYLES = { :miter => 0, :round => 1, :bevel => 2 }
|
13
|
+
|
14
|
+
# @group Stable API
|
15
|
+
|
16
|
+
# Sets the join style for stroked lines and curves
|
17
|
+
#
|
18
|
+
# style is one of :miter, :round, or :bevel
|
19
|
+
#
|
20
|
+
# NOTE: if this method is never called, :miter will be used for join style
|
21
|
+
# throughout the document
|
22
|
+
#
|
23
|
+
def join_style(style=nil)
|
24
|
+
return current_join_style || :miter if style.nil?
|
25
|
+
|
26
|
+
self.current_join_style = style
|
27
|
+
|
28
|
+
write_stroke_join_style
|
29
|
+
end
|
30
|
+
|
31
|
+
alias_method :join_style=, :join_style
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def current_join_style
|
36
|
+
graphic_state.join_style
|
37
|
+
end
|
38
|
+
|
39
|
+
def current_join_style=(style)
|
40
|
+
graphic_state.join_style = style
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def write_stroke_join_style
|
45
|
+
renderer.add_content "#{JOIN_STYLES[current_join_style]} j"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# patterns.rb : Implements axial & radial gradients
|
4
|
+
#
|
5
|
+
# Originally implemented by Wojciech Piekutowski. November, 2009
|
6
|
+
# Copyright September 2012, Alexander Mankuta. All Rights Reserved.
|
7
|
+
#
|
8
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
9
|
+
#
|
10
|
+
module Prawn
|
11
|
+
module Graphics
|
12
|
+
module Patterns
|
13
|
+
# @group Stable API
|
14
|
+
|
15
|
+
# Sets the fill gradient from color1 to color2.
|
16
|
+
# old arguments: point, width, height, color1, color2, options = {}
|
17
|
+
# new arguments: from, to, color1, color1
|
18
|
+
# or from, r1, to, r2, color1, color2
|
19
|
+
def fill_gradient(*args)
|
20
|
+
set_gradient(:fill, *args)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Sets the stroke gradient from color1 to color2.
|
24
|
+
# old arguments: point, width, height, color1, color2, options = {}
|
25
|
+
# new arguments: from, to, color1, color2
|
26
|
+
# or from, r1, to, r2, color1, color2
|
27
|
+
def stroke_gradient(*args)
|
28
|
+
set_gradient(:stroke, *args)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def set_gradient(type, *grad)
|
34
|
+
patterns = page.resources[:Pattern] ||= {}
|
35
|
+
|
36
|
+
registry_key = gradient_registry_key grad
|
37
|
+
|
38
|
+
if patterns["SP#{registry_key}"]
|
39
|
+
shading = patterns["SP#{registry_key}"]
|
40
|
+
else
|
41
|
+
unless shading = gradient_registry[registry_key]
|
42
|
+
shading = gradient(*grad)
|
43
|
+
gradient_registry[registry_key] = shading
|
44
|
+
end
|
45
|
+
|
46
|
+
patterns["SP#{registry_key}"] = shading
|
47
|
+
end
|
48
|
+
|
49
|
+
operator = case type
|
50
|
+
when :fill
|
51
|
+
'scn'
|
52
|
+
when :stroke
|
53
|
+
'SCN'
|
54
|
+
else
|
55
|
+
raise ArgumentError, "unknown type '#{type}'"
|
56
|
+
end
|
57
|
+
|
58
|
+
set_color_space type, :Pattern
|
59
|
+
renderer.add_content "/SP#{registry_key} #{operator}"
|
60
|
+
end
|
61
|
+
|
62
|
+
def gradient_registry_key(gradient)
|
63
|
+
if gradient[1].is_a?(Array) # axial
|
64
|
+
[
|
65
|
+
map_to_absolute(gradient[0]),
|
66
|
+
map_to_absolute(gradient[1]),
|
67
|
+
gradient[2], gradient[3]
|
68
|
+
]
|
69
|
+
else # radial
|
70
|
+
[
|
71
|
+
map_to_absolute(gradient[0]),
|
72
|
+
gradient[1],
|
73
|
+
map_to_absolute(gradient[2]),
|
74
|
+
gradient[3],
|
75
|
+
gradient[4], gradient[5]
|
76
|
+
]
|
77
|
+
end.hash
|
78
|
+
end
|
79
|
+
|
80
|
+
def gradient_registry
|
81
|
+
@gradient_registry ||= {}
|
82
|
+
end
|
83
|
+
|
84
|
+
def gradient(*args)
|
85
|
+
if args.length != 4 && args.length != 6
|
86
|
+
raise ArgumentError, "Unknown type of gradient: #{args.inspect}"
|
87
|
+
end
|
88
|
+
|
89
|
+
color1 = normalize_color(args[-2]).dup.freeze
|
90
|
+
color2 = normalize_color(args[-1]).dup.freeze
|
91
|
+
|
92
|
+
if color_type(color1) != color_type(color2)
|
93
|
+
raise ArgumentError, "Both colors must be of the same color space: #{color1.inspect} and #{color2.inspect}"
|
94
|
+
end
|
95
|
+
|
96
|
+
process_color color1
|
97
|
+
process_color color2
|
98
|
+
|
99
|
+
shader = ref!({
|
100
|
+
:FunctionType => 2,
|
101
|
+
:Domain => [0.0, 1.0],
|
102
|
+
:C0 => color1,
|
103
|
+
:C1 => color2,
|
104
|
+
:N => 1.0,
|
105
|
+
})
|
106
|
+
|
107
|
+
shading = ref!({
|
108
|
+
:ShadingType => args.length == 4 ? 2 : 3, # axial : radial shading
|
109
|
+
:ColorSpace => color_space(color1),
|
110
|
+
:Coords => args.length == 4 ?
|
111
|
+
[0, 0, args[1].first - args[0].first, args[1].last - args[0].last] :
|
112
|
+
[0, 0, args[1], args[2].first - args[0].first, args[2].last - args[0].last, args[3]],
|
113
|
+
:Function => shader,
|
114
|
+
:Extend => [true, true],
|
115
|
+
})
|
116
|
+
|
117
|
+
ref!({
|
118
|
+
:PatternType => 2, # shading pattern
|
119
|
+
:Shading => shading,
|
120
|
+
:Matrix => [1, 0,
|
121
|
+
0, 1] + map_to_absolute(args[0]),
|
122
|
+
})
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|