thinreports 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +41 -0
  3. data/.travis.yml +4 -6
  4. data/CHANGELOG.md +21 -2
  5. data/Gemfile +2 -4
  6. data/README.md +4 -5
  7. data/Rakefile +7 -8
  8. data/examples/character_spacing/character_spacing.rb +1 -1
  9. data/examples/dynamic_image/dynamic_image.rb +3 -3
  10. data/examples/dynamic_style/dynamic_style.rb +1 -1
  11. data/examples/eudc/eudc.rb +1 -1
  12. data/examples/helper.rb +2 -2
  13. data/examples/hidden_shapes/hidden_shapes.rb +1 -1
  14. data/examples/list_events/list_events.rb +1 -1
  15. data/examples/list_manual_generation/list_manual_generation.rb +1 -1
  16. data/examples/list_page_number/list_page_number.rb +1 -1
  17. data/examples/page_number/page_number.rb +1 -1
  18. data/examples/palleted_png/palleted_png.rb +1 -3
  19. data/examples/password_setting/password_setting.rb +1 -1
  20. data/examples/report_callbacks/report_callbacks.rb +1 -1
  21. data/examples/single_line_tblock/single_line_tblock.rb +1 -1
  22. data/examples/tblock_overflow/tblock_overflow.rb +1 -1
  23. data/examples/tblock_styles/tblock_styles.rb +1 -1
  24. data/examples/text_align/text_align.rb +1 -1
  25. data/examples/typeB_page_size/typeB_page_size.rb +1 -1
  26. data/examples/word_wrap/word_wrap.rb +1 -1
  27. data/lib/thinreports.rb +16 -15
  28. data/lib/thinreports/config.rb +2 -24
  29. data/lib/thinreports/core/errors.rb +6 -17
  30. data/lib/thinreports/core/format/base.rb +62 -61
  31. data/lib/thinreports/core/shape.rb +13 -14
  32. data/lib/thinreports/core/shape/base.rb +3 -12
  33. data/lib/thinreports/core/shape/base/interface.rb +35 -32
  34. data/lib/thinreports/core/shape/base/internal.rb +45 -43
  35. data/lib/thinreports/core/shape/basic.rb +12 -12
  36. data/lib/thinreports/core/shape/basic/block_format.rb +8 -6
  37. data/lib/thinreports/core/shape/basic/block_interface.rb +24 -22
  38. data/lib/thinreports/core/shape/basic/block_internal.rb +28 -26
  39. data/lib/thinreports/core/shape/basic/format.rb +11 -9
  40. data/lib/thinreports/core/shape/basic/interface.rb +79 -77
  41. data/lib/thinreports/core/shape/basic/internal.rb +17 -14
  42. data/lib/thinreports/core/shape/image_block.rb +9 -9
  43. data/lib/thinreports/core/shape/image_block/format.rb +7 -5
  44. data/lib/thinreports/core/shape/image_block/interface.rb +15 -13
  45. data/lib/thinreports/core/shape/image_block/internal.rb +10 -8
  46. data/lib/thinreports/core/shape/list.rb +13 -13
  47. data/lib/thinreports/core/shape/list/format.rb +83 -77
  48. data/lib/thinreports/core/shape/list/manager.rb +182 -176
  49. data/lib/thinreports/core/shape/list/page.rb +94 -93
  50. data/lib/thinreports/core/shape/list/page_state.rb +42 -40
  51. data/lib/thinreports/core/shape/list/section_format.rb +28 -26
  52. data/lib/thinreports/core/shape/list/section_interface.rb +42 -40
  53. data/lib/thinreports/core/shape/list/section_internal.rb +21 -19
  54. data/lib/thinreports/core/shape/manager.rb +4 -13
  55. data/lib/thinreports/core/shape/manager/format.rb +21 -19
  56. data/lib/thinreports/core/shape/manager/internal.rb +88 -84
  57. data/lib/thinreports/core/shape/manager/target.rb +93 -95
  58. data/lib/thinreports/core/shape/page_number.rb +9 -9
  59. data/lib/thinreports/core/shape/page_number/format.rb +20 -18
  60. data/lib/thinreports/core/shape/page_number/interface.rb +20 -18
  61. data/lib/thinreports/core/shape/page_number/internal.rb +47 -44
  62. data/lib/thinreports/core/shape/style.rb +5 -14
  63. data/lib/thinreports/core/shape/style/base.rb +121 -120
  64. data/lib/thinreports/core/shape/style/basic.rb +12 -10
  65. data/lib/thinreports/core/shape/style/graphic.rb +35 -33
  66. data/lib/thinreports/core/shape/style/text.rb +113 -107
  67. data/lib/thinreports/core/shape/text.rb +9 -9
  68. data/lib/thinreports/core/shape/text/format.rb +10 -9
  69. data/lib/thinreports/core/shape/text/interface.rb +13 -11
  70. data/lib/thinreports/core/shape/text/internal.rb +14 -12
  71. data/lib/thinreports/core/shape/text_block.rb +10 -10
  72. data/lib/thinreports/core/shape/text_block/format.rb +35 -33
  73. data/lib/thinreports/core/shape/text_block/formatter.rb +26 -24
  74. data/lib/thinreports/core/shape/text_block/formatter/basic.rb +25 -25
  75. data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +15 -12
  76. data/lib/thinreports/core/shape/text_block/formatter/number.rb +45 -39
  77. data/lib/thinreports/core/shape/text_block/formatter/padding.rb +17 -14
  78. data/lib/thinreports/core/shape/text_block/interface.rb +25 -23
  79. data/lib/thinreports/core/shape/text_block/internal.rb +68 -68
  80. data/lib/thinreports/core/utils.rb +4 -3
  81. data/lib/thinreports/generator/pdf.rb +24 -25
  82. data/lib/thinreports/generator/pdf/document.rb +109 -110
  83. data/lib/thinreports/generator/pdf/document/draw_shape.rb +58 -51
  84. data/lib/thinreports/generator/pdf/document/draw_template_items.rb +51 -47
  85. data/lib/thinreports/generator/pdf/document/font.rb +62 -59
  86. data/lib/thinreports/generator/pdf/document/graphics.rb +32 -33
  87. data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +122 -121
  88. data/lib/thinreports/generator/pdf/document/graphics/basic.rb +113 -114
  89. data/lib/thinreports/generator/pdf/document/graphics/image.rb +58 -150
  90. data/lib/thinreports/generator/pdf/document/graphics/text.rb +152 -137
  91. data/lib/thinreports/generator/pdf/document/page.rb +63 -64
  92. data/lib/thinreports/generator/pdf/document/parse_color.rb +37 -37
  93. data/lib/thinreports/generator/pdf/drawer/base.rb +41 -41
  94. data/lib/thinreports/generator/pdf/drawer/list.rb +48 -45
  95. data/lib/thinreports/generator/pdf/drawer/list_section.rb +42 -40
  96. data/lib/thinreports/generator/pdf/drawer/page.rb +101 -96
  97. data/lib/thinreports/generator/pdf/prawn_ext.rb +4 -30
  98. data/lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb +34 -0
  99. data/lib/thinreports/generator/pdf/prawn_ext/width_of.rb +29 -0
  100. data/lib/thinreports/layout.rb +5 -7
  101. data/lib/thinreports/layout/base.rb +5 -7
  102. data/lib/thinreports/layout/format.rb +18 -13
  103. data/lib/thinreports/layout/legacy_schema.rb +18 -10
  104. data/lib/thinreports/layout/version.rb +22 -25
  105. data/lib/thinreports/report.rb +4 -6
  106. data/lib/thinreports/report/base.rb +24 -51
  107. data/lib/thinreports/report/internal.rb +9 -11
  108. data/lib/thinreports/report/page.rb +5 -6
  109. data/lib/thinreports/version.rb +2 -2
  110. data/test/schema_helper.rb +4 -3
  111. data/test/test_helper.rb +9 -3
  112. data/test/unit/core/format/test_base.rb +1 -1
  113. data/test/unit/core/shape/base/test_internal.rb +1 -1
  114. data/test/unit/core/shape/basic/test_block_format.rb +1 -1
  115. data/test/unit/core/shape/basic/test_block_interface.rb +1 -1
  116. data/test/unit/core/shape/basic/test_block_internal.rb +1 -1
  117. data/test/unit/core/shape/basic/test_format.rb +1 -1
  118. data/test/unit/core/shape/basic/test_interface.rb +1 -1
  119. data/test/unit/core/shape/basic/test_internal.rb +1 -1
  120. data/test/unit/core/shape/image_block/test_interface.rb +1 -1
  121. data/test/unit/core/shape/image_block/test_internal.rb +1 -1
  122. data/test/unit/core/shape/list/test_format.rb +1 -1
  123. data/test/unit/core/shape/list/test_manager.rb +4 -4
  124. data/test/unit/core/shape/list/test_page.rb +1 -1
  125. data/test/unit/core/shape/list/test_page_state.rb +1 -1
  126. data/test/unit/core/shape/list/test_section_format.rb +1 -1
  127. data/test/unit/core/shape/list/test_section_interface.rb +2 -2
  128. data/test/unit/core/shape/list/test_section_internal.rb +1 -1
  129. data/test/unit/core/shape/manager/test_format.rb +2 -2
  130. data/test/unit/core/shape/manager/test_internal.rb +3 -3
  131. data/test/unit/core/shape/manager/test_target.rb +1 -1
  132. data/test/unit/core/shape/page_number/test_format.rb +1 -1
  133. data/test/unit/core/shape/page_number/test_interface.rb +1 -1
  134. data/test/unit/core/shape/page_number/test_internal.rb +1 -1
  135. data/test/unit/core/shape/styles/test_base.rb +1 -1
  136. data/test/unit/core/shape/styles/test_basic.rb +1 -1
  137. data/test/unit/core/shape/styles/test_graphic.rb +1 -1
  138. data/test/unit/core/shape/styles/test_text.rb +1 -1
  139. data/test/unit/core/shape/text/test_format.rb +1 -1
  140. data/test/unit/core/shape/text/test_internal.rb +1 -1
  141. data/test/unit/core/shape/text_block/formatter/test_basic.rb +1 -1
  142. data/test/unit/core/shape/text_block/formatter/test_datetime.rb +1 -1
  143. data/test/unit/core/shape/text_block/formatter/test_number.rb +1 -1
  144. data/test/unit/core/shape/text_block/formatter/test_padding.rb +2 -2
  145. data/test/unit/core/shape/text_block/test_format.rb +1 -1
  146. data/test/unit/core/shape/text_block/test_formatter.rb +1 -1
  147. data/test/unit/core/shape/text_block/test_interface.rb +1 -1
  148. data/test/unit/core/shape/text_block/test_internal.rb +3 -3
  149. data/test/unit/core/test_shape.rb +1 -1
  150. data/test/unit/core/test_utils.rb +2 -2
  151. data/test/unit/generator/pdf/document/graphics/test_attributes.rb +1 -1
  152. data/test/unit/generator/pdf/document/graphics/test_basic.rb +2 -1
  153. data/test/unit/generator/pdf/document/graphics/test_image.rb +27 -131
  154. data/test/unit/generator/pdf/document/graphics/test_text.rb +1 -1
  155. data/test/unit/generator/pdf/document/test_font.rb +2 -2
  156. data/test/unit/generator/pdf/document/test_graphics.rb +1 -1
  157. data/test/unit/generator/pdf/document/test_page.rb +7 -7
  158. data/test/unit/generator/pdf/document/test_parse_color.rb +1 -1
  159. data/test/unit/generator/pdf/prawn_ext/test_calc_image_dimensions.rb +41 -0
  160. data/test/unit/generator/pdf/prawn_ext/test_width_of.rb +22 -0
  161. data/test/unit/generator/pdf/test_document.rb +8 -6
  162. data/test/unit/generator/test_pdf.rb +12 -10
  163. data/test/unit/layout/test_base.rb +1 -1
  164. data/test/unit/layout/test_format.rb +2 -2
  165. data/test/unit/layout/test_legacy_schema.rb +4 -4
  166. data/test/unit/layout/test_version.rb +11 -80
  167. data/test/unit/report/test_base.rb +45 -59
  168. data/test/unit/report/test_internal.rb +1 -1
  169. data/test/unit/test_config.rb +1 -16
  170. data/test/unit/test_layout.rb +1 -1
  171. data/test/unit/test_report.rb +1 -1
  172. data/thinreports.gemspec +11 -14
  173. metadata +42 -103
  174. data/lib/thinreports/generator.rb +0 -29
  175. data/lib/thinreports/generator/base.rb +0 -40
  176. data/lib/thinreports/generator/configuration.rb +0 -31
  177. data/lib/thinreports/report/events.rb +0 -32
  178. data/test/unit/generator/test_base.rb +0 -40
  179. data/test/unit/generator/test_configuration.rb +0 -21
@@ -1,147 +1,148 @@
1
- # coding: utf-8
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
- module PDF::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
21
-
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
22
+ # @param [Hash] style
23
+ # @yield [attrs]
24
+ # @yieldparam [Hash] attrs
25
+ # @return [Hash]
26
+ # rubocop:disable Metrics/AbcSize
27
+ def build_text_attributes(style, &block)
28
+ text_attributes = {
29
+ font: font_family(style['font-family']),
30
+ size: style['font-size'],
31
+ color: style['color'],
32
+ align: text_align(style['text-align']),
33
+ valign: text_valign(style['vertical-align']),
34
+ styles: font_styles(style['font-style']),
35
+ letter_spacing: letter_spacing(style['letter-spacing']),
36
+ line_height: line_height(style['line-height']),
37
+ overflow: text_overflow(style['overflow']),
38
+ word_wrap: word_wrap(style['word-wrap'])
39
+ }
40
+ block.call(text_attributes) if block_given?
41
+ text_attributes
42
+ end
42
43
 
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
44
+ # @param [Array<String>] font_names
45
+ # @return [String]
46
+ def font_family(font_names)
47
+ font_name = font_names.first
48
+ default_family_if_missing(font_name)
49
+ end
49
50
 
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
51
+ # @param [Array<String>] styles
52
+ # @return [Array<Symbol>]
53
+ def font_styles(styles)
54
+ styles.map do |font_style|
55
+ case font_style
56
+ when 'bold' then :bold
57
+ when 'italic' then :italic
58
+ when 'underline' then :underline
59
+ when 'linethrough' then :strikethrough
60
+ end
59
61
  end
60
62
  end
61
- end
62
63
 
63
- # @param [Float, "", nil] spacing
64
- # @return [Float, nil]
65
- def letter_spacing(spacing)
66
- blank_value?(spacing) ? nil : spacing
67
- end
64
+ # @param [Float, "", nil] spacing
65
+ # @return [Float, nil]
66
+ def letter_spacing(spacing)
67
+ blank_value?(spacing) ? nil : spacing
68
+ end
68
69
 
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
70
+ # @param ["left", "center", "right", ""] align
71
+ # @return [:left, :center, :right]
72
+ def text_align(align)
73
+ case align
74
+ when 'left' then :left
75
+ when 'center' then :center
76
+ when 'right' then :right
77
+ when '' then :left
78
+ else :left
79
+ end
78
80
  end
79
- end
80
81
 
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
82
+ # @param ["top", "middle", "bottom", "", nil] valign
83
+ # @return [:top, :center, :bottom]
84
+ def text_valign(valign)
85
+ case valign
86
+ when 'top' then :top
87
+ when 'middle' then :center
88
+ when 'bottom' then :bottom
89
+ when '' then :top
90
+ else :top
91
+ end
90
92
  end
91
- end
92
93
 
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
94
+ # @param ["truncate", "fit", "expand", "", nil] overflow
95
+ # @return [:truncate, :shrink_to_fit, :expand]
96
+ def text_overflow(overflow)
97
+ case overflow
98
+ when 'truncate' then :truncate
99
+ when 'fit' then :shrink_to_fit
100
+ when 'expand' then :expand
101
+ when '' then :truncate
102
+ else :truncate
103
+ end
102
104
  end
103
- end
104
105
 
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
106
+ # @param ["break-word", "none", "", nil] word_wrap
107
+ # @return [:break_word, :none]
108
+ def word_wrap(word_wrap)
109
+ case word_wrap
110
+ when 'break-word' then :break_word
111
+ when 'none' then :none
112
+ else :none
113
+ end
112
114
  end
113
- end
114
115
 
115
- # @param [Float, "", nil] height
116
- # @return [Float, nil]
117
- def line_height(height)
118
- blank_value?(height) ? nil : height
119
- end
116
+ # @param [Float, "", nil] height
117
+ # @return [Float, nil]
118
+ def line_height(height)
119
+ blank_value?(height) ? nil : height
120
+ end
120
121
 
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
122
+ # @param ["left", "center", "right", ""] position
123
+ # @return [:left, :center, :right]
124
+ def image_position_x(position)
125
+ case position
126
+ when 'left' then :left
127
+ when 'center' then :center
128
+ when 'right' then :right
129
+ when '' then :left
130
+ else :left
131
+ end
130
132
  end
131
- end
132
133
 
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
134
+ # @param ["top", "middle", "bottom", ""] position
135
+ # @return [:left, :center, :right]
136
+ def image_position_y(position)
137
+ case position
138
+ when 'top' then :top
139
+ when 'middle' then :center
140
+ when 'bottom' then :bottom
141
+ when '' then :top
142
+ else :top
143
+ end
142
144
  end
143
145
  end
144
146
  end
145
-
146
147
  end
147
148
  end
@@ -1,137 +1,136 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
4
  module Generator
5
-
6
- module PDF::Graphics
7
- STROKE_DASH = {
8
- dashed: [2, 2],
9
- dotted: [1, 2]
10
- }
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))
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
- # @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)
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
- # @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)
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
- # @param [Hash] attrs
68
- def with_graphic_styles(attrs, &block)
69
- stroke = build_stroke_styles(attrs)
70
- fill = build_fill_styles(attrs)
67
+ # @param [Hash] attrs
68
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/PerceivedComplexity
69
+ def with_graphic_styles(attrs, &block)
70
+ stroke = build_stroke_styles(attrs)
71
+ fill = build_fill_styles(attrs)
72
+
73
+ # Do not draw if no colors given.
74
+ return unless fill || stroke
71
75
 
72
- # Do not draw if no colors given.
73
- return unless fill || stroke
76
+ save_graphics_state
74
77
 
75
- save_graphics_state
78
+ # Apply stroke-dashed.
79
+ if stroke && stroke[:dash]
80
+ length, space = stroke[:dash]
81
+ pdf.dash(length, space: space)
82
+ end
83
+
84
+ # Draw with fill and stroke.
85
+ if fill && stroke
86
+ pdf.fill_and_stroke do
87
+ line_width(stroke[:width])
88
+ pdf.fill_color(fill[:color])
89
+ pdf.stroke_color(stroke[:color])
90
+ block.call
91
+ end
92
+ # Draw only with fill.
93
+ elsif fill
94
+ pdf.fill do
95
+ pdf.fill_color(fill[:color])
96
+ block.call
97
+ end
98
+ # Draw only with stroke.
99
+ elsif stroke
100
+ pdf.stroke do
101
+ line_width(stroke[:width])
102
+ pdf.stroke_color(stroke[:color])
103
+ block.call
104
+ end
105
+ end
76
106
 
77
- # Apply stroke-dashed.
78
- if stroke && stroke[:dash]
79
- length, space = stroke[:dash]
80
- pdf.dash(length, space: space)
107
+ restore_graphics_state
81
108
  end
82
109
 
83
- case
84
- # Draw with fill and stroke.
85
- when fill && stroke
86
- pdf.fill_and_stroke {
87
- line_width(stroke[:width])
88
- pdf.fill_color(fill[:color])
89
- pdf.stroke_color(stroke[:color])
90
- block.call
91
- }
92
- # Draw only with fill.
93
- when fill
94
- pdf.fill {
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
110
+ # @param [Hash] styles
111
+ # @return [Hash, nil]
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]
104
122
  }
105
123
  end
106
124
 
107
- restore_graphics_state
108
- end
109
-
110
- # @param [Hash] styles
111
- # @return [Hash, nil]
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
125
+ # @param [Hash] styles
126
+ # @return [Hash, nil]
127
+ def build_fill_styles(styles)
128
+ color = styles[:fill]
129
+ return nil unless color && color != 'none'
124
130
 
125
- # @param [Hash] styles
126
- # @return [Hash, nil]
127
- def build_fill_styles(styles)
128
- color = styles[:fill]
129
- return nil unless color && color != 'none'
130
-
131
- { color: parse_color(color) }
131
+ { color: parse_color(color) }
132
+ end
132
133
  end
133
-
134
134
  end
135
-
136
135
  end
137
136
  end