thinreports 0.9.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +39 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +59 -2
- data/Dockerfile +12 -0
- data/Gemfile +7 -4
- data/README.md +44 -22
- data/Rakefile +14 -31
- data/lib/thinreports.rb +16 -15
- data/lib/thinreports/config.rb +2 -24
- data/lib/thinreports/core/errors.rb +6 -17
- data/lib/thinreports/core/format/base.rb +62 -61
- data/lib/thinreports/core/shape.rb +10 -12
- data/lib/thinreports/core/shape/base.rb +3 -12
- data/lib/thinreports/core/shape/base/interface.rb +34 -32
- data/lib/thinreports/core/shape/base/internal.rb +45 -43
- data/lib/thinreports/core/shape/basic.rb +12 -12
- data/lib/thinreports/core/shape/basic/block_format.rb +8 -6
- data/lib/thinreports/core/shape/basic/block_interface.rb +24 -22
- data/lib/thinreports/core/shape/basic/block_internal.rb +28 -26
- data/lib/thinreports/core/shape/basic/format.rb +11 -9
- data/lib/thinreports/core/shape/basic/interface.rb +79 -77
- data/lib/thinreports/core/shape/basic/internal.rb +17 -14
- data/lib/thinreports/core/shape/image_block.rb +9 -9
- data/lib/thinreports/core/shape/image_block/format.rb +7 -5
- data/lib/thinreports/core/shape/image_block/interface.rb +15 -13
- data/lib/thinreports/core/shape/image_block/internal.rb +10 -8
- data/lib/thinreports/core/shape/list.rb +13 -13
- data/lib/thinreports/core/shape/list/format.rb +83 -77
- data/lib/thinreports/core/shape/list/manager.rb +179 -176
- data/lib/thinreports/core/shape/list/page.rb +91 -93
- data/lib/thinreports/core/shape/list/page_state.rb +42 -40
- data/lib/thinreports/core/shape/list/section_format.rb +28 -26
- data/lib/thinreports/core/shape/list/section_interface.rb +42 -40
- data/lib/thinreports/core/shape/list/section_internal.rb +21 -19
- data/lib/thinreports/core/shape/manager.rb +4 -13
- data/lib/thinreports/core/shape/manager/format.rb +21 -19
- data/lib/thinreports/core/shape/manager/internal.rb +87 -84
- data/lib/thinreports/core/shape/manager/target.rb +93 -95
- data/lib/thinreports/core/shape/page_number.rb +9 -9
- data/lib/thinreports/core/shape/page_number/format.rb +20 -18
- data/lib/thinreports/core/shape/page_number/interface.rb +20 -18
- data/lib/thinreports/core/shape/page_number/internal.rb +46 -44
- data/lib/thinreports/core/shape/style.rb +5 -14
- data/lib/thinreports/core/shape/style/base.rb +121 -120
- data/lib/thinreports/core/shape/style/basic.rb +12 -10
- data/lib/thinreports/core/shape/style/graphic.rb +35 -33
- data/lib/thinreports/core/shape/style/text.rb +113 -107
- data/lib/thinreports/core/shape/text.rb +9 -9
- data/lib/thinreports/core/shape/text/format.rb +10 -9
- data/lib/thinreports/core/shape/text/interface.rb +13 -11
- data/lib/thinreports/core/shape/text/internal.rb +14 -12
- data/lib/thinreports/core/shape/text_block.rb +10 -10
- data/lib/thinreports/core/shape/text_block/format.rb +35 -33
- data/lib/thinreports/core/shape/text_block/formatter.rb +26 -24
- data/lib/thinreports/core/shape/text_block/formatter/basic.rb +25 -25
- data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +15 -12
- data/lib/thinreports/core/shape/text_block/formatter/number.rb +52 -34
- data/lib/thinreports/core/shape/text_block/formatter/padding.rb +17 -14
- data/lib/thinreports/core/shape/text_block/interface.rb +25 -23
- data/lib/thinreports/core/shape/text_block/internal.rb +66 -68
- data/lib/thinreports/core/utils.rb +7 -3
- data/lib/thinreports/generator/pdf.rb +24 -25
- data/lib/thinreports/generator/pdf/document.rb +109 -110
- data/lib/thinreports/generator/pdf/document/draw_shape.rb +57 -51
- data/lib/thinreports/generator/pdf/document/draw_template_items.rb +50 -47
- data/lib/thinreports/generator/pdf/document/font.rb +67 -58
- data/lib/thinreports/generator/pdf/document/graphics.rb +32 -33
- data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +121 -121
- data/lib/thinreports/generator/pdf/document/graphics/basic.rb +112 -114
- data/lib/thinreports/generator/pdf/document/graphics/image.rb +58 -150
- data/lib/thinreports/generator/pdf/document/graphics/text.rb +149 -137
- data/lib/thinreports/generator/pdf/document/page.rb +60 -64
- data/lib/thinreports/generator/pdf/document/parse_color.rb +37 -37
- data/lib/thinreports/generator/pdf/drawer/base.rb +41 -41
- data/lib/thinreports/generator/pdf/drawer/list.rb +56 -54
- data/lib/thinreports/generator/pdf/drawer/list_section.rb +42 -40
- data/lib/thinreports/generator/pdf/drawer/page.rb +101 -96
- data/lib/thinreports/generator/pdf/prawn_ext.rb +4 -30
- data/lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb +32 -0
- data/lib/thinreports/generator/pdf/prawn_ext/width_of.rb +35 -0
- data/lib/thinreports/layout.rb +5 -7
- data/lib/thinreports/layout/base.rb +5 -9
- data/lib/thinreports/layout/format.rb +17 -13
- data/lib/thinreports/layout/legacy_schema.rb +17 -13
- data/lib/thinreports/layout/version.rb +22 -25
- data/lib/thinreports/report.rb +4 -6
- data/lib/thinreports/report/base.rb +24 -51
- data/lib/thinreports/report/internal.rb +13 -15
- data/lib/thinreports/report/page.rb +5 -11
- data/lib/thinreports/version.rb +2 -2
- data/thinreports.gemspec +10 -19
- metadata +13 -288
- data/.travis.yml +0 -19
- data/examples/character_spacing/character_spacing.rb +0 -8
- data/examples/character_spacing/character_spacing.tlf +0 -293
- data/examples/dynamic_image/dynamic_image.rb +0 -31
- data/examples/dynamic_image/dynamic_image.tlf +0 -661
- data/examples/dynamic_image/img200x100.png +0 -0
- data/examples/dynamic_image/img50x50.png +0 -0
- data/examples/dynamic_style/dynamic_style.rb +0 -150
- data/examples/dynamic_style/dynamic_style.tlf +0 -1835
- data/examples/dynamic_style/dynamic_style_in_list.tlf +0 -344
- data/examples/dynamic_style/image.png +0 -0
- data/examples/eudc/eudc.rb +0 -20
- data/examples/eudc/eudc.tlf +0 -180
- data/examples/eudc/eudc.ttf +0 -0
- data/examples/helper.rb +0 -50
- data/examples/hidden_shapes/hidden_shapes.rb +0 -9
- data/examples/hidden_shapes/hidden_shapes.tlf +0 -449
- data/examples/list_events/list_events.rb +0 -32
- data/examples/list_events/list_events.tlf +0 -361
- data/examples/list_manual_generation/list_manual_generation.rb +0 -22
- data/examples/list_manual_generation/list_manual_generation.tlf +0 -132
- data/examples/list_page_number/list_page_number.rb +0 -17
- data/examples/list_page_number/list_page_number.tlf +0 -254
- data/examples/page_number/page_number.rb +0 -33
- data/examples/page_number/page_number.tlf +0 -215
- data/examples/palleted_png/palleted_png.png +0 -0
- data/examples/palleted_png/palleted_png.rb +0 -11
- data/examples/palleted_png/palleted_png.tlf +0 -47
- data/examples/password_setting/password_setting.rb +0 -10
- data/examples/password_setting/password_setting.tlf +0 -45
- data/examples/report_callbacks/report_callbacks.rb +0 -14
- data/examples/report_callbacks/report_callbacks.tlf +0 -147
- data/examples/single_line_tblock/single_line_tblock.rb +0 -13
- data/examples/single_line_tblock/single_line_tblock.tlf +0 -170
- data/examples/tblock_overflow/tblock_overflow.rb +0 -20
- data/examples/tblock_overflow/tblock_overflow.tlf +0 -538
- data/examples/tblock_styles/font_size.tlf +0 -383
- data/examples/tblock_styles/tblock_styles.rb +0 -43
- data/examples/tblock_styles/tblock_styles.tlf +0 -889
- data/examples/text_align/text_align.rb +0 -8
- data/examples/text_align/text_align.tlf +0 -241
- data/examples/typeB_page_size/B4_ISO.tlf +0 -45
- data/examples/typeB_page_size/B4_JIS.tlf +0 -45
- data/examples/typeB_page_size/typeB_page_size.rb +0 -17
- data/examples/word_wrap/word_wrap.rb +0 -26
- data/examples/word_wrap/word_wrap.tlf +0 -297
- data/lib/thinreports/generator.rb +0 -29
- data/lib/thinreports/generator/base.rb +0 -40
- data/lib/thinreports/generator/configuration.rb +0 -31
- data/lib/thinreports/report/events.rb +0 -32
- data/test/data/font.ttf +0 -0
- data/test/data/image_normal.jpg +0 -0
- data/test/data/image_normal.png +0 -0
- data/test/data/image_normal_jpg_noext +0 -0
- data/test/data/image_normal_png_noext +0 -0
- data/test/data/image_pallete_based.png +0 -0
- data/test/data/legacy_layout/all-items.tlf +0 -1
- data/test/schema_helper.rb +0 -121
- data/test/test_helper.rb +0 -42
- data/test/tmp/.gitkeep +0 -0
- data/test/unit/core/format/test_base.rb +0 -152
- data/test/unit/core/shape/base/test_internal.rb +0 -87
- data/test/unit/core/shape/basic/test_block_format.rb +0 -23
- data/test/unit/core/shape/basic/test_block_interface.rb +0 -29
- data/test/unit/core/shape/basic/test_block_internal.rb +0 -55
- data/test/unit/core/shape/basic/test_format.rb +0 -37
- data/test/unit/core/shape/basic/test_interface.rb +0 -108
- data/test/unit/core/shape/basic/test_internal.rb +0 -55
- data/test/unit/core/shape/image_block/test_interface.rb +0 -24
- data/test/unit/core/shape/image_block/test_internal.rb +0 -31
- data/test/unit/core/shape/list/test_format.rb +0 -111
- data/test/unit/core/shape/list/test_manager.rb +0 -67
- data/test/unit/core/shape/list/test_page.rb +0 -82
- data/test/unit/core/shape/list/test_page_state.rb +0 -31
- data/test/unit/core/shape/list/test_section_format.rb +0 -36
- data/test/unit/core/shape/list/test_section_interface.rb +0 -75
- data/test/unit/core/shape/list/test_section_internal.rb +0 -50
- data/test/unit/core/shape/manager/test_format.rb +0 -38
- data/test/unit/core/shape/manager/test_internal.rb +0 -132
- data/test/unit/core/shape/manager/test_target.rb +0 -140
- data/test/unit/core/shape/page_number/test_format.rb +0 -55
- data/test/unit/core/shape/page_number/test_interface.rb +0 -33
- data/test/unit/core/shape/page_number/test_internal.rb +0 -81
- data/test/unit/core/shape/styles/test_base.rb +0 -191
- data/test/unit/core/shape/styles/test_basic.rb +0 -24
- data/test/unit/core/shape/styles/test_graphic.rb +0 -50
- data/test/unit/core/shape/styles/test_text.rb +0 -97
- data/test/unit/core/shape/text/test_format.rb +0 -44
- data/test/unit/core/shape/text/test_internal.rb +0 -20
- data/test/unit/core/shape/text_block/formatter/test_basic.rb +0 -24
- data/test/unit/core/shape/text_block/formatter/test_datetime.rb +0 -49
- data/test/unit/core/shape/text_block/formatter/test_number.rb +0 -76
- data/test/unit/core/shape/text_block/formatter/test_padding.rb +0 -77
- data/test/unit/core/shape/text_block/test_format.rb +0 -169
- data/test/unit/core/shape/text_block/test_formatter.rb +0 -28
- data/test/unit/core/shape/text_block/test_interface.rb +0 -63
- data/test/unit/core/shape/text_block/test_internal.rb +0 -128
- data/test/unit/core/test_shape.rb +0 -52
- data/test/unit/core/test_utils.rb +0 -68
- data/test/unit/generator/pdf/document/graphics/test_attributes.rb +0 -135
- data/test/unit/generator/pdf/document/graphics/test_basic.rb +0 -45
- data/test/unit/generator/pdf/document/graphics/test_image.rb +0 -150
- data/test/unit/generator/pdf/document/graphics/test_text.rb +0 -171
- data/test/unit/generator/pdf/document/test_font.rb +0 -84
- data/test/unit/generator/pdf/document/test_graphics.rb +0 -42
- data/test/unit/generator/pdf/document/test_page.rb +0 -122
- data/test/unit/generator/pdf/document/test_parse_color.rb +0 -30
- data/test/unit/generator/pdf/test_document.rb +0 -20
- data/test/unit/generator/test_base.rb +0 -40
- data/test/unit/generator/test_configuration.rb +0 -21
- data/test/unit/generator/test_pdf.rb +0 -24
- data/test/unit/layout/test_base.rb +0 -41
- data/test/unit/layout/test_format.rb +0 -100
- data/test/unit/layout/test_legacy_schema.rb +0 -574
- data/test/unit/layout/test_version.rb +0 -95
- data/test/unit/report/test_base.rb +0 -262
- data/test/unit/report/test_internal.rb +0 -206
- data/test/unit/test_config.rb +0 -51
- data/test/unit/test_layout.rb +0 -12
- data/test/unit/test_report.rb +0 -18
@@ -1,147 +1,147 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Thinreports
|
4
4
|
module Generator
|
5
|
+
class PDF
|
6
|
+
module Graphics
|
7
|
+
# @param [Hash] style
|
8
|
+
# @yield [attrs]
|
9
|
+
# @yieldparam [Hash] attrs
|
10
|
+
# @return [Hash]
|
11
|
+
def build_graphic_attributes(style, &block)
|
12
|
+
graphic_attributes = {
|
13
|
+
stroke: style['border-color'],
|
14
|
+
stroke_width: style['border-width'],
|
15
|
+
stroke_type: style['border-style'],
|
16
|
+
fill: style['fill-color']
|
17
|
+
}
|
18
|
+
block.call(graphic_attributes) if block_given?
|
19
|
+
graphic_attributes
|
20
|
+
end
|
5
21
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
def build_text_attributes(style, &block)
|
27
|
-
text_attributes = {
|
28
|
-
font: font_family(style['font-family']),
|
29
|
-
size: style['font-size'],
|
30
|
-
color: style['color'],
|
31
|
-
align: text_align(style['text-align']),
|
32
|
-
valign: text_valign(style['vertical-align']),
|
33
|
-
styles: font_styles(style['font-style']),
|
34
|
-
letter_spacing: letter_spacing(style['letter-spacing']),
|
35
|
-
line_height: line_height(style['line-height']),
|
36
|
-
overflow: text_overflow(style['overflow']),
|
37
|
-
word_wrap: word_wrap(style['word-wrap'])
|
38
|
-
}
|
39
|
-
block.call(text_attributes) if block_given?
|
40
|
-
text_attributes
|
41
|
-
end
|
22
|
+
# @param [Hash] style
|
23
|
+
# @yield [attrs]
|
24
|
+
# @yieldparam [Hash] attrs
|
25
|
+
# @return [Hash]
|
26
|
+
def build_text_attributes(style, &block)
|
27
|
+
text_attributes = {
|
28
|
+
font: font_family(style['font-family']),
|
29
|
+
size: style['font-size'],
|
30
|
+
color: style['color'],
|
31
|
+
align: text_align(style['text-align']),
|
32
|
+
valign: text_valign(style['vertical-align']),
|
33
|
+
styles: font_styles(style['font-style']),
|
34
|
+
letter_spacing: letter_spacing(style['letter-spacing']),
|
35
|
+
line_height: line_height(style['line-height']),
|
36
|
+
overflow: text_overflow(style['overflow']),
|
37
|
+
word_wrap: word_wrap(style['word-wrap'])
|
38
|
+
}
|
39
|
+
block.call(text_attributes) if block_given?
|
40
|
+
text_attributes
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
43
|
+
# @param [Array<String>] font_names
|
44
|
+
# @return [String]
|
45
|
+
def font_family(font_names)
|
46
|
+
font_name = font_names.first
|
47
|
+
default_family_if_missing(font_name)
|
48
|
+
end
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
50
|
+
# @param [Array<String>] styles
|
51
|
+
# @return [Array<Symbol>]
|
52
|
+
def font_styles(styles)
|
53
|
+
styles.map do |font_style|
|
54
|
+
case font_style
|
55
|
+
when 'bold' then :bold
|
56
|
+
when 'italic' then :italic
|
57
|
+
when 'underline' then :underline
|
58
|
+
when 'linethrough' then :strikethrough
|
59
|
+
end
|
59
60
|
end
|
60
61
|
end
|
61
|
-
end
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
63
|
+
# @param [Float, "", nil] spacing
|
64
|
+
# @return [Float, nil]
|
65
|
+
def letter_spacing(spacing)
|
66
|
+
blank_value?(spacing) ? nil : spacing
|
67
|
+
end
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
69
|
+
# @param ["left", "center", "right", ""] align
|
70
|
+
# @return [:left, :center, :right]
|
71
|
+
def text_align(align)
|
72
|
+
case align
|
73
|
+
when 'left' then :left
|
74
|
+
when 'center' then :center
|
75
|
+
when 'right' then :right
|
76
|
+
when '' then :left
|
77
|
+
else :left
|
78
|
+
end
|
78
79
|
end
|
79
|
-
end
|
80
80
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
81
|
+
# @param ["top", "middle", "bottom", "", nil] valign
|
82
|
+
# @return [:top, :center, :bottom]
|
83
|
+
def text_valign(valign)
|
84
|
+
case valign
|
85
|
+
when 'top' then :top
|
86
|
+
when 'middle' then :center
|
87
|
+
when 'bottom' then :bottom
|
88
|
+
when '' then :top
|
89
|
+
else :top
|
90
|
+
end
|
90
91
|
end
|
91
|
-
end
|
92
92
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
93
|
+
# @param ["truncate", "fit", "expand", "", nil] overflow
|
94
|
+
# @return [:truncate, :shrink_to_fit, :expand]
|
95
|
+
def text_overflow(overflow)
|
96
|
+
case overflow
|
97
|
+
when 'truncate' then :truncate
|
98
|
+
when 'fit' then :shrink_to_fit
|
99
|
+
when 'expand' then :expand
|
100
|
+
when '' then :truncate
|
101
|
+
else :truncate
|
102
|
+
end
|
102
103
|
end
|
103
|
-
end
|
104
104
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
105
|
+
# @param ["break-word", "none", "", nil] word_wrap
|
106
|
+
# @return [:break_word, :none]
|
107
|
+
def word_wrap(word_wrap)
|
108
|
+
case word_wrap
|
109
|
+
when 'break-word' then :break_word
|
110
|
+
when 'none' then :none
|
111
|
+
else :none
|
112
|
+
end
|
112
113
|
end
|
113
|
-
end
|
114
114
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
115
|
+
# @param [Float, "", nil] height
|
116
|
+
# @return [Float, nil]
|
117
|
+
def line_height(height)
|
118
|
+
blank_value?(height) ? nil : height
|
119
|
+
end
|
120
120
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
121
|
+
# @param ["left", "center", "right", ""] position
|
122
|
+
# @return [:left, :center, :right]
|
123
|
+
def image_position_x(position)
|
124
|
+
case position
|
125
|
+
when 'left' then :left
|
126
|
+
when 'center' then :center
|
127
|
+
when 'right' then :right
|
128
|
+
when '' then :left
|
129
|
+
else :left
|
130
|
+
end
|
130
131
|
end
|
131
|
-
end
|
132
132
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
133
|
+
# @param ["top", "middle", "bottom", ""] position
|
134
|
+
# @return [:left, :center, :right]
|
135
|
+
def image_position_y(position)
|
136
|
+
case position
|
137
|
+
when 'top' then :top
|
138
|
+
when 'middle' then :center
|
139
|
+
when 'bottom' then :bottom
|
140
|
+
when '' then :top
|
141
|
+
else :top
|
142
|
+
end
|
142
143
|
end
|
143
144
|
end
|
144
145
|
end
|
145
|
-
|
146
146
|
end
|
147
147
|
end
|
@@ -1,137 +1,135 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Thinreports
|
4
4
|
module Generator
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
5
|
+
class PDF
|
6
|
+
module Graphics
|
7
|
+
STROKE_DASH = {
|
8
|
+
dashed: [2, 2],
|
9
|
+
dotted: [1, 2]
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
# @param [Numeric, String] x1
|
13
|
+
# @param [Numeric, String] y1
|
14
|
+
# @param [Numeric, String] x2
|
15
|
+
# @param [Numeric, String] y2
|
16
|
+
# @param [Hash] attrs ({})
|
17
|
+
# @option attrs [String] :stroke
|
18
|
+
# @option attrs [Numeric, String] :stroke_width
|
19
|
+
# @option attrs ["solid", "dashed", "dotted"] :stroke_type
|
20
|
+
def line(x1, y1, x2, y2, attrs = {})
|
21
|
+
with_graphic_styles(attrs) do
|
22
|
+
pdf.line(pos(x1, y1), pos(x2, y2))
|
23
|
+
end
|
23
24
|
end
|
24
|
-
end
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
26
|
+
# @param [Numeric, String] x
|
27
|
+
# @param [Numeric, String] y
|
28
|
+
# @param [Numeric, String] w width
|
29
|
+
# @param [Numeric, String] h height
|
30
|
+
# @param [Hash] attrs ({})
|
31
|
+
# @option attrs [Integer, String] :radius
|
32
|
+
# @option attrs [String] :stroke
|
33
|
+
# @option attrs [Numeric, String] :stroke_width
|
34
|
+
# @option attrs ["solid", "dashed", "dotted"] :stroke_type
|
35
|
+
# @option attrs [String] :fill
|
36
|
+
def rect(x, y, w, h, attrs = {})
|
37
|
+
w, h = s2f(w, h)
|
38
|
+
radius = s2f(attrs[:radius])
|
39
|
+
|
40
|
+
with_graphic_styles(attrs) do
|
41
|
+
if radius && !radius.zero?
|
42
|
+
pdf.rounded_rectangle(pos(x, y), w, h, radius)
|
43
|
+
else
|
44
|
+
pdf.rectangle(pos(x, y), w, h)
|
45
|
+
end
|
45
46
|
end
|
46
47
|
end
|
47
|
-
end
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
49
|
+
# @param [Numeric, String] x center-x
|
50
|
+
# @param [Numeric, String] y center-y
|
51
|
+
# @param [Numeric, String] rx
|
52
|
+
# @param [Numeric, String] ry
|
53
|
+
# @param [Hash] attrs ({})
|
54
|
+
# @option attrs [String] :stroke
|
55
|
+
# @option attrs [Numeric, String] :stroke_width
|
56
|
+
# @option attrs [Array<Integer, String>] :stroke_dash
|
57
|
+
# @option attrs ["solid", "dashed", "dotted"] :stroke_type
|
58
|
+
# @option attrs [String] :fill
|
59
|
+
def ellipse(x, y, rx, ry, attrs = {})
|
60
|
+
rx, ry = s2f(rx, ry)
|
61
|
+
|
62
|
+
with_graphic_styles(attrs) do
|
63
|
+
pdf.ellipse(pos(x, y), rx, ry)
|
64
|
+
end
|
64
65
|
end
|
65
|
-
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
# @param [Hash] attrs
|
68
|
+
def with_graphic_styles(attrs, &block)
|
69
|
+
stroke = build_stroke_styles(attrs)
|
70
|
+
fill = build_fill_styles(attrs)
|
71
|
+
|
72
|
+
# Do not draw if no colors given.
|
73
|
+
return unless fill || stroke
|
71
74
|
|
72
|
-
|
73
|
-
return unless fill || stroke
|
75
|
+
save_graphics_state
|
74
76
|
|
75
|
-
|
77
|
+
# Apply stroke-dashed.
|
78
|
+
if stroke && stroke[:dash]
|
79
|
+
length, space = stroke[:dash]
|
80
|
+
pdf.dash(length, space: space)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Draw with fill and stroke.
|
84
|
+
if fill && stroke
|
85
|
+
pdf.fill_and_stroke do
|
86
|
+
line_width(stroke[:width])
|
87
|
+
pdf.fill_color(fill[:color])
|
88
|
+
pdf.stroke_color(stroke[:color])
|
89
|
+
block.call
|
90
|
+
end
|
91
|
+
# Draw only with fill.
|
92
|
+
elsif fill
|
93
|
+
pdf.fill do
|
94
|
+
pdf.fill_color(fill[:color])
|
95
|
+
block.call
|
96
|
+
end
|
97
|
+
# Draw only with stroke.
|
98
|
+
elsif stroke
|
99
|
+
pdf.stroke do
|
100
|
+
line_width(stroke[:width])
|
101
|
+
pdf.stroke_color(stroke[:color])
|
102
|
+
block.call
|
103
|
+
end
|
104
|
+
end
|
76
105
|
|
77
|
-
|
78
|
-
if stroke && stroke[:dash]
|
79
|
-
length, space = stroke[:dash]
|
80
|
-
pdf.dash(length, space: space)
|
106
|
+
restore_graphics_state
|
81
107
|
end
|
82
108
|
|
83
|
-
|
84
|
-
#
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
pdf.fill_color(fill[:color])
|
96
|
-
block.call
|
97
|
-
}
|
98
|
-
# Draw only with stroke.
|
99
|
-
when stroke
|
100
|
-
pdf.stroke {
|
101
|
-
line_width(stroke[:width])
|
102
|
-
pdf.stroke_color(stroke[:color])
|
103
|
-
block.call
|
109
|
+
# @param [Hash] styles
|
110
|
+
# @return [Hash, nil]
|
111
|
+
def build_stroke_styles(styles)
|
112
|
+
color = styles[:stroke]
|
113
|
+
width = styles[:stroke_width]
|
114
|
+
return nil unless color && color != 'none'
|
115
|
+
return nil unless width && width != 0
|
116
|
+
|
117
|
+
{
|
118
|
+
color: parse_color(color),
|
119
|
+
width: s2f(width),
|
120
|
+
dash: STROKE_DASH[styles[:stroke_type].to_sym]
|
104
121
|
}
|
105
122
|
end
|
106
123
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
def build_stroke_styles(styles)
|
113
|
-
color = styles[:stroke]
|
114
|
-
width = styles[:stroke_width]
|
115
|
-
return nil unless color && color != 'none'
|
116
|
-
return nil unless width && width != 0
|
117
|
-
|
118
|
-
{
|
119
|
-
color: parse_color(color),
|
120
|
-
width: s2f(width),
|
121
|
-
dash: STROKE_DASH[styles[:stroke_type].to_sym]
|
122
|
-
}
|
123
|
-
end
|
124
|
+
# @param [Hash] styles
|
125
|
+
# @return [Hash, nil]
|
126
|
+
def build_fill_styles(styles)
|
127
|
+
color = styles[:fill]
|
128
|
+
return nil unless color && color != 'none'
|
124
129
|
|
125
|
-
|
126
|
-
|
127
|
-
def build_fill_styles(styles)
|
128
|
-
color = styles[:fill]
|
129
|
-
return nil unless color && color != 'none'
|
130
|
-
|
131
|
-
{ color: parse_color(color) }
|
130
|
+
{ color: parse_color(color) }
|
131
|
+
end
|
132
132
|
end
|
133
|
-
|
134
133
|
end
|
135
|
-
|
136
134
|
end
|
137
135
|
end
|