thinreports 0.9.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +39 -0
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +59 -2
  5. data/Dockerfile +12 -0
  6. data/Gemfile +7 -4
  7. data/README.md +44 -22
  8. data/Rakefile +14 -31
  9. data/lib/thinreports.rb +16 -15
  10. data/lib/thinreports/config.rb +2 -24
  11. data/lib/thinreports/core/errors.rb +6 -17
  12. data/lib/thinreports/core/format/base.rb +62 -61
  13. data/lib/thinreports/core/shape.rb +10 -12
  14. data/lib/thinreports/core/shape/base.rb +3 -12
  15. data/lib/thinreports/core/shape/base/interface.rb +34 -32
  16. data/lib/thinreports/core/shape/base/internal.rb +45 -43
  17. data/lib/thinreports/core/shape/basic.rb +12 -12
  18. data/lib/thinreports/core/shape/basic/block_format.rb +8 -6
  19. data/lib/thinreports/core/shape/basic/block_interface.rb +24 -22
  20. data/lib/thinreports/core/shape/basic/block_internal.rb +28 -26
  21. data/lib/thinreports/core/shape/basic/format.rb +11 -9
  22. data/lib/thinreports/core/shape/basic/interface.rb +79 -77
  23. data/lib/thinreports/core/shape/basic/internal.rb +17 -14
  24. data/lib/thinreports/core/shape/image_block.rb +9 -9
  25. data/lib/thinreports/core/shape/image_block/format.rb +7 -5
  26. data/lib/thinreports/core/shape/image_block/interface.rb +15 -13
  27. data/lib/thinreports/core/shape/image_block/internal.rb +10 -8
  28. data/lib/thinreports/core/shape/list.rb +13 -13
  29. data/lib/thinreports/core/shape/list/format.rb +83 -77
  30. data/lib/thinreports/core/shape/list/manager.rb +179 -176
  31. data/lib/thinreports/core/shape/list/page.rb +91 -93
  32. data/lib/thinreports/core/shape/list/page_state.rb +42 -40
  33. data/lib/thinreports/core/shape/list/section_format.rb +28 -26
  34. data/lib/thinreports/core/shape/list/section_interface.rb +42 -40
  35. data/lib/thinreports/core/shape/list/section_internal.rb +21 -19
  36. data/lib/thinreports/core/shape/manager.rb +4 -13
  37. data/lib/thinreports/core/shape/manager/format.rb +21 -19
  38. data/lib/thinreports/core/shape/manager/internal.rb +87 -84
  39. data/lib/thinreports/core/shape/manager/target.rb +93 -95
  40. data/lib/thinreports/core/shape/page_number.rb +9 -9
  41. data/lib/thinreports/core/shape/page_number/format.rb +20 -18
  42. data/lib/thinreports/core/shape/page_number/interface.rb +20 -18
  43. data/lib/thinreports/core/shape/page_number/internal.rb +46 -44
  44. data/lib/thinreports/core/shape/style.rb +5 -14
  45. data/lib/thinreports/core/shape/style/base.rb +121 -120
  46. data/lib/thinreports/core/shape/style/basic.rb +12 -10
  47. data/lib/thinreports/core/shape/style/graphic.rb +35 -33
  48. data/lib/thinreports/core/shape/style/text.rb +113 -107
  49. data/lib/thinreports/core/shape/text.rb +9 -9
  50. data/lib/thinreports/core/shape/text/format.rb +10 -9
  51. data/lib/thinreports/core/shape/text/interface.rb +13 -11
  52. data/lib/thinreports/core/shape/text/internal.rb +14 -12
  53. data/lib/thinreports/core/shape/text_block.rb +10 -10
  54. data/lib/thinreports/core/shape/text_block/format.rb +35 -33
  55. data/lib/thinreports/core/shape/text_block/formatter.rb +26 -24
  56. data/lib/thinreports/core/shape/text_block/formatter/basic.rb +25 -25
  57. data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +15 -12
  58. data/lib/thinreports/core/shape/text_block/formatter/number.rb +52 -34
  59. data/lib/thinreports/core/shape/text_block/formatter/padding.rb +17 -14
  60. data/lib/thinreports/core/shape/text_block/interface.rb +25 -23
  61. data/lib/thinreports/core/shape/text_block/internal.rb +66 -68
  62. data/lib/thinreports/core/utils.rb +7 -3
  63. data/lib/thinreports/generator/pdf.rb +24 -25
  64. data/lib/thinreports/generator/pdf/document.rb +109 -110
  65. data/lib/thinreports/generator/pdf/document/draw_shape.rb +57 -51
  66. data/lib/thinreports/generator/pdf/document/draw_template_items.rb +50 -47
  67. data/lib/thinreports/generator/pdf/document/font.rb +67 -58
  68. data/lib/thinreports/generator/pdf/document/graphics.rb +32 -33
  69. data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +121 -121
  70. data/lib/thinreports/generator/pdf/document/graphics/basic.rb +112 -114
  71. data/lib/thinreports/generator/pdf/document/graphics/image.rb +58 -150
  72. data/lib/thinreports/generator/pdf/document/graphics/text.rb +149 -137
  73. data/lib/thinreports/generator/pdf/document/page.rb +60 -64
  74. data/lib/thinreports/generator/pdf/document/parse_color.rb +37 -37
  75. data/lib/thinreports/generator/pdf/drawer/base.rb +41 -41
  76. data/lib/thinreports/generator/pdf/drawer/list.rb +56 -54
  77. data/lib/thinreports/generator/pdf/drawer/list_section.rb +42 -40
  78. data/lib/thinreports/generator/pdf/drawer/page.rb +101 -96
  79. data/lib/thinreports/generator/pdf/prawn_ext.rb +4 -30
  80. data/lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb +32 -0
  81. data/lib/thinreports/generator/pdf/prawn_ext/width_of.rb +35 -0
  82. data/lib/thinreports/layout.rb +5 -7
  83. data/lib/thinreports/layout/base.rb +5 -9
  84. data/lib/thinreports/layout/format.rb +17 -13
  85. data/lib/thinreports/layout/legacy_schema.rb +17 -13
  86. data/lib/thinreports/layout/version.rb +22 -25
  87. data/lib/thinreports/report.rb +4 -6
  88. data/lib/thinreports/report/base.rb +24 -51
  89. data/lib/thinreports/report/internal.rb +13 -15
  90. data/lib/thinreports/report/page.rb +5 -11
  91. data/lib/thinreports/version.rb +2 -2
  92. data/thinreports.gemspec +10 -19
  93. metadata +13 -288
  94. data/.travis.yml +0 -19
  95. data/examples/character_spacing/character_spacing.rb +0 -8
  96. data/examples/character_spacing/character_spacing.tlf +0 -293
  97. data/examples/dynamic_image/dynamic_image.rb +0 -31
  98. data/examples/dynamic_image/dynamic_image.tlf +0 -661
  99. data/examples/dynamic_image/img200x100.png +0 -0
  100. data/examples/dynamic_image/img50x50.png +0 -0
  101. data/examples/dynamic_style/dynamic_style.rb +0 -150
  102. data/examples/dynamic_style/dynamic_style.tlf +0 -1835
  103. data/examples/dynamic_style/dynamic_style_in_list.tlf +0 -344
  104. data/examples/dynamic_style/image.png +0 -0
  105. data/examples/eudc/eudc.rb +0 -20
  106. data/examples/eudc/eudc.tlf +0 -180
  107. data/examples/eudc/eudc.ttf +0 -0
  108. data/examples/helper.rb +0 -50
  109. data/examples/hidden_shapes/hidden_shapes.rb +0 -9
  110. data/examples/hidden_shapes/hidden_shapes.tlf +0 -449
  111. data/examples/list_events/list_events.rb +0 -32
  112. data/examples/list_events/list_events.tlf +0 -361
  113. data/examples/list_manual_generation/list_manual_generation.rb +0 -22
  114. data/examples/list_manual_generation/list_manual_generation.tlf +0 -132
  115. data/examples/list_page_number/list_page_number.rb +0 -17
  116. data/examples/list_page_number/list_page_number.tlf +0 -254
  117. data/examples/page_number/page_number.rb +0 -33
  118. data/examples/page_number/page_number.tlf +0 -215
  119. data/examples/palleted_png/palleted_png.png +0 -0
  120. data/examples/palleted_png/palleted_png.rb +0 -11
  121. data/examples/palleted_png/palleted_png.tlf +0 -47
  122. data/examples/password_setting/password_setting.rb +0 -10
  123. data/examples/password_setting/password_setting.tlf +0 -45
  124. data/examples/report_callbacks/report_callbacks.rb +0 -14
  125. data/examples/report_callbacks/report_callbacks.tlf +0 -147
  126. data/examples/single_line_tblock/single_line_tblock.rb +0 -13
  127. data/examples/single_line_tblock/single_line_tblock.tlf +0 -170
  128. data/examples/tblock_overflow/tblock_overflow.rb +0 -20
  129. data/examples/tblock_overflow/tblock_overflow.tlf +0 -538
  130. data/examples/tblock_styles/font_size.tlf +0 -383
  131. data/examples/tblock_styles/tblock_styles.rb +0 -43
  132. data/examples/tblock_styles/tblock_styles.tlf +0 -889
  133. data/examples/text_align/text_align.rb +0 -8
  134. data/examples/text_align/text_align.tlf +0 -241
  135. data/examples/typeB_page_size/B4_ISO.tlf +0 -45
  136. data/examples/typeB_page_size/B4_JIS.tlf +0 -45
  137. data/examples/typeB_page_size/typeB_page_size.rb +0 -17
  138. data/examples/word_wrap/word_wrap.rb +0 -26
  139. data/examples/word_wrap/word_wrap.tlf +0 -297
  140. data/lib/thinreports/generator.rb +0 -29
  141. data/lib/thinreports/generator/base.rb +0 -40
  142. data/lib/thinreports/generator/configuration.rb +0 -31
  143. data/lib/thinreports/report/events.rb +0 -32
  144. data/test/data/font.ttf +0 -0
  145. data/test/data/image_normal.jpg +0 -0
  146. data/test/data/image_normal.png +0 -0
  147. data/test/data/image_normal_jpg_noext +0 -0
  148. data/test/data/image_normal_png_noext +0 -0
  149. data/test/data/image_pallete_based.png +0 -0
  150. data/test/data/legacy_layout/all-items.tlf +0 -1
  151. data/test/schema_helper.rb +0 -121
  152. data/test/test_helper.rb +0 -42
  153. data/test/tmp/.gitkeep +0 -0
  154. data/test/unit/core/format/test_base.rb +0 -152
  155. data/test/unit/core/shape/base/test_internal.rb +0 -87
  156. data/test/unit/core/shape/basic/test_block_format.rb +0 -23
  157. data/test/unit/core/shape/basic/test_block_interface.rb +0 -29
  158. data/test/unit/core/shape/basic/test_block_internal.rb +0 -55
  159. data/test/unit/core/shape/basic/test_format.rb +0 -37
  160. data/test/unit/core/shape/basic/test_interface.rb +0 -108
  161. data/test/unit/core/shape/basic/test_internal.rb +0 -55
  162. data/test/unit/core/shape/image_block/test_interface.rb +0 -24
  163. data/test/unit/core/shape/image_block/test_internal.rb +0 -31
  164. data/test/unit/core/shape/list/test_format.rb +0 -111
  165. data/test/unit/core/shape/list/test_manager.rb +0 -67
  166. data/test/unit/core/shape/list/test_page.rb +0 -82
  167. data/test/unit/core/shape/list/test_page_state.rb +0 -31
  168. data/test/unit/core/shape/list/test_section_format.rb +0 -36
  169. data/test/unit/core/shape/list/test_section_interface.rb +0 -75
  170. data/test/unit/core/shape/list/test_section_internal.rb +0 -50
  171. data/test/unit/core/shape/manager/test_format.rb +0 -38
  172. data/test/unit/core/shape/manager/test_internal.rb +0 -132
  173. data/test/unit/core/shape/manager/test_target.rb +0 -140
  174. data/test/unit/core/shape/page_number/test_format.rb +0 -55
  175. data/test/unit/core/shape/page_number/test_interface.rb +0 -33
  176. data/test/unit/core/shape/page_number/test_internal.rb +0 -81
  177. data/test/unit/core/shape/styles/test_base.rb +0 -191
  178. data/test/unit/core/shape/styles/test_basic.rb +0 -24
  179. data/test/unit/core/shape/styles/test_graphic.rb +0 -50
  180. data/test/unit/core/shape/styles/test_text.rb +0 -97
  181. data/test/unit/core/shape/text/test_format.rb +0 -44
  182. data/test/unit/core/shape/text/test_internal.rb +0 -20
  183. data/test/unit/core/shape/text_block/formatter/test_basic.rb +0 -24
  184. data/test/unit/core/shape/text_block/formatter/test_datetime.rb +0 -49
  185. data/test/unit/core/shape/text_block/formatter/test_number.rb +0 -76
  186. data/test/unit/core/shape/text_block/formatter/test_padding.rb +0 -77
  187. data/test/unit/core/shape/text_block/test_format.rb +0 -169
  188. data/test/unit/core/shape/text_block/test_formatter.rb +0 -28
  189. data/test/unit/core/shape/text_block/test_interface.rb +0 -63
  190. data/test/unit/core/shape/text_block/test_internal.rb +0 -128
  191. data/test/unit/core/test_shape.rb +0 -52
  192. data/test/unit/core/test_utils.rb +0 -68
  193. data/test/unit/generator/pdf/document/graphics/test_attributes.rb +0 -135
  194. data/test/unit/generator/pdf/document/graphics/test_basic.rb +0 -45
  195. data/test/unit/generator/pdf/document/graphics/test_image.rb +0 -150
  196. data/test/unit/generator/pdf/document/graphics/test_text.rb +0 -171
  197. data/test/unit/generator/pdf/document/test_font.rb +0 -84
  198. data/test/unit/generator/pdf/document/test_graphics.rb +0 -42
  199. data/test/unit/generator/pdf/document/test_page.rb +0 -122
  200. data/test/unit/generator/pdf/document/test_parse_color.rb +0 -30
  201. data/test/unit/generator/pdf/test_document.rb +0 -20
  202. data/test/unit/generator/test_base.rb +0 -40
  203. data/test/unit/generator/test_configuration.rb +0 -21
  204. data/test/unit/generator/test_pdf.rb +0 -24
  205. data/test/unit/layout/test_base.rb +0 -41
  206. data/test/unit/layout/test_format.rb +0 -100
  207. data/test/unit/layout/test_legacy_schema.rb +0 -574
  208. data/test/unit/layout/test_version.rb +0 -95
  209. data/test/unit/report/test_base.rb +0 -262
  210. data/test/unit/report/test_internal.rb +0 -206
  211. data/test/unit/test_config.rb +0 -51
  212. data/test/unit/test_layout.rb +0 -12
  213. data/test/unit/test_report.rb +0 -18
@@ -1,147 +1,147 @@
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
+ 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
- # @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
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
- # @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
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
- # @param [Float, "", nil] spacing
64
- # @return [Float, nil]
65
- def letter_spacing(spacing)
66
- blank_value?(spacing) ? nil : spacing
67
- end
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
- # @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
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
- # @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
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
- # @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
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
- # @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
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
- # @param [Float, "", nil] height
116
- # @return [Float, nil]
117
- def line_height(height)
118
- blank_value?(height) ? nil : height
119
- end
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
- # @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
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
- # @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
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
- # 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
+ 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
- # Do not draw if no colors given.
73
- return unless fill || stroke
75
+ save_graphics_state
74
76
 
75
- save_graphics_state
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
- # Apply stroke-dashed.
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
- 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
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
- 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
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
- # @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) }
130
+ { color: parse_color(color) }
131
+ end
132
132
  end
133
-
134
133
  end
135
-
136
134
  end
137
135
  end