squib 0.15.1 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/tests.yml +22 -0
  3. data/.gitignore +41 -41
  4. data/CHANGELOG.md +422 -387
  5. data/CONTRIBUTING.md +40 -40
  6. data/Dockerfile +27 -25
  7. data/Gemfile +2 -2
  8. data/Guardfile +8 -0
  9. data/LICENSE.txt +22 -22
  10. data/README.md +139 -121
  11. data/RELEASE TODO.md +22 -21
  12. data/Rakefile +51 -48
  13. data/bin/squib +4 -4
  14. data/lib/squib.rb +34 -32
  15. data/lib/squib/api/settings.rb +21 -21
  16. data/lib/squib/args/arg_loader.rb +129 -126
  17. data/lib/squib/args/box.rb +59 -55
  18. data/lib/squib/args/card_range.rb +34 -32
  19. data/lib/squib/args/color_validator.rb +7 -12
  20. data/lib/squib/args/coords.rb +49 -35
  21. data/lib/squib/args/csv_opts.rb +22 -25
  22. data/lib/squib/args/dir_validator.rb +11 -16
  23. data/lib/squib/args/draw.rb +93 -92
  24. data/lib/squib/args/embed_adjust.rb +22 -25
  25. data/lib/squib/args/embed_key.rb +12 -17
  26. data/lib/squib/args/hand_special.rb +37 -37
  27. data/lib/squib/args/import.rb +67 -40
  28. data/lib/squib/args/input_file.rb +33 -37
  29. data/lib/squib/args/paint.rb +43 -44
  30. data/lib/squib/args/paragraph.rb +118 -116
  31. data/lib/squib/args/save_batch.rb +65 -63
  32. data/lib/squib/args/scale_box.rb +57 -53
  33. data/lib/squib/args/sheet.rb +170 -172
  34. data/lib/squib/args/showcase_special.rb +41 -41
  35. data/lib/squib/args/sprue_file.rb +44 -44
  36. data/lib/squib/args/svg_special.rb +37 -37
  37. data/lib/squib/args/transform.rb +55 -61
  38. data/lib/squib/args/typographer.rb +115 -119
  39. data/lib/squib/args/unit_conversion.rb +27 -29
  40. data/lib/squib/args/xywh_shorthands.rb +51 -0
  41. data/lib/squib/builtin/layouts/economy.yml +85 -85
  42. data/lib/squib/builtin/layouts/fantasy.yml +101 -101
  43. data/lib/squib/builtin/layouts/hand.yml +62 -62
  44. data/lib/squib/builtin/layouts/party.yml +94 -94
  45. data/lib/squib/builtin/layouts/playing-card.yml +35 -35
  46. data/lib/squib/builtin/layouts/tuck_box.yml +46 -46
  47. data/lib/squib/builtin/projects/advanced/.gitignore +4 -4
  48. data/lib/squib/builtin/projects/advanced/ABOUT.md +19 -19
  49. data/lib/squib/builtin/projects/advanced/Gemfile +11 -11
  50. data/lib/squib/builtin/projects/advanced/Guardfile +21 -21
  51. data/lib/squib/builtin/projects/advanced/IDEAS.md +22 -22
  52. data/lib/squib/builtin/projects/advanced/PLAYTESTING.md +26 -26
  53. data/lib/squib/builtin/projects/advanced/Rakefile +27 -27
  54. data/lib/squib/builtin/projects/advanced/config.yml +50 -49
  55. data/lib/squib/builtin/projects/advanced/docs/PNP NOTES.md +3 -3
  56. data/lib/squib/builtin/projects/advanced/docs/RULES.md +21 -21
  57. data/lib/squib/builtin/projects/advanced/img/example.svg +60 -60
  58. data/lib/squib/builtin/projects/advanced/layouts/deck.yml +27 -27
  59. data/lib/squib/builtin/projects/advanced/src/deck.rb +34 -34
  60. data/lib/squib/builtin/projects/advanced/src/version.rb +3 -3
  61. data/lib/squib/builtin/projects/basic/.gitignore +4 -4
  62. data/lib/squib/builtin/projects/basic/ABOUT.md +19 -19
  63. data/lib/squib/builtin/projects/basic/Gemfile +3 -3
  64. data/lib/squib/builtin/projects/basic/IDEAS.md +22 -22
  65. data/lib/squib/builtin/projects/basic/PLAYTESTING.md +26 -26
  66. data/lib/squib/builtin/projects/basic/PNP NOTES.md +3 -3
  67. data/lib/squib/builtin/projects/basic/RULES.md +21 -21
  68. data/lib/squib/builtin/projects/basic/Rakefile +7 -7
  69. data/lib/squib/builtin/projects/basic/config.yml +50 -49
  70. data/lib/squib/builtin/projects/basic/deck.rb +6 -6
  71. data/lib/squib/builtin/sprues/a4_euro_card.yml +42 -42
  72. data/lib/squib/builtin/sprues/a4_poker_card_8up.yml +40 -40
  73. data/lib/squib/builtin/sprues/a4_poker_card_9up.yml +42 -42
  74. data/lib/squib/builtin/sprues/a4_usa_card.yml +42 -42
  75. data/lib/squib/builtin/sprues/drivethrucards_1up.yml +10 -10
  76. data/lib/squib/builtin/sprues/letter_poker_card_9up.yml +25 -25
  77. data/lib/squib/builtin/sprues/letter_poker_foldable_8up.yml +52 -52
  78. data/lib/squib/builtin/sprues/printplaygames_18up.yml +68 -68
  79. data/lib/squib/card.rb +75 -75
  80. data/lib/squib/commands/cli.rb +39 -39
  81. data/lib/squib/commands/data/template_option.rb +109 -109
  82. data/lib/squib/commands/make_sprue.rb +277 -277
  83. data/lib/squib/commands/new.rb +77 -77
  84. data/lib/squib/conf.rb +144 -139
  85. data/lib/squib/constants.rb +17 -17
  86. data/lib/squib/deck.rb +138 -116
  87. data/lib/squib/dsl/background.rb +35 -0
  88. data/lib/squib/dsl/circle.rb +39 -0
  89. data/lib/squib/dsl/csv.rb +42 -0
  90. data/lib/squib/dsl/curve.rb +35 -0
  91. data/lib/squib/dsl/cut_zone.rb +47 -0
  92. data/lib/squib/dsl/ellipse.rb +37 -0
  93. data/lib/squib/dsl/grid.rb +35 -0
  94. data/lib/squib/{api → dsl}/groups.rb +54 -54
  95. data/lib/squib/dsl/hand.rb +42 -0
  96. data/lib/squib/dsl/line.rb +35 -0
  97. data/lib/squib/dsl/png.rb +56 -0
  98. data/lib/squib/dsl/polygon.rb +36 -0
  99. data/lib/squib/dsl/rect.rb +37 -0
  100. data/lib/squib/dsl/safe_zone.rb +48 -0
  101. data/lib/squib/dsl/save.rb +21 -0
  102. data/lib/squib/dsl/save_pdf.rb +50 -0
  103. data/lib/squib/dsl/save_png.rb +44 -0
  104. data/lib/squib/dsl/save_sheet.rb +53 -0
  105. data/lib/squib/dsl/showcase.rb +43 -0
  106. data/lib/squib/dsl/star.rb +37 -0
  107. data/lib/squib/dsl/svg.rb +62 -0
  108. data/lib/squib/dsl/text.rb +54 -0
  109. data/lib/squib/dsl/text_embed.rb +78 -0
  110. data/lib/squib/dsl/triangle.rb +35 -0
  111. data/lib/squib/{api → dsl}/units.rb +37 -27
  112. data/lib/squib/dsl/xlsx.rb +40 -0
  113. data/lib/squib/dsl/yaml.rb +40 -0
  114. data/lib/squib/errors_warnings/warn_unexpected_params.rb +14 -0
  115. data/lib/squib/graphics/background.rb +14 -14
  116. data/lib/squib/graphics/cairo_context_wrapper.rb +113 -113
  117. data/lib/squib/graphics/embedding_utils.rb +28 -28
  118. data/lib/squib/graphics/gradient_regex.rb +47 -47
  119. data/lib/squib/graphics/hand.rb +42 -42
  120. data/lib/squib/graphics/image.rb +123 -123
  121. data/lib/squib/graphics/save_doc.rb +61 -61
  122. data/lib/squib/graphics/save_images.rb +52 -52
  123. data/lib/squib/graphics/save_pdf.rb +90 -90
  124. data/lib/squib/graphics/save_sprue.rb +231 -219
  125. data/lib/squib/graphics/shapes.rb +143 -143
  126. data/lib/squib/graphics/showcase.rb +85 -85
  127. data/lib/squib/graphics/text.rb +202 -174
  128. data/lib/squib/import/csv_importer.rb +45 -0
  129. data/lib/squib/import/data_frame.rb +108 -108
  130. data/lib/squib/import/quantity_exploder.rb +18 -0
  131. data/lib/squib/import/xlsx_importer.rb +28 -0
  132. data/lib/squib/import/yaml_importer.rb +30 -0
  133. data/lib/squib/layout_parser.rb +155 -138
  134. data/lib/squib/progress.rb +38 -38
  135. data/lib/squib/sample_helpers.rb +34 -34
  136. data/lib/squib/sprues/crop_line.rb +28 -28
  137. data/lib/squib/sprues/crop_line_dash.rb +35 -35
  138. data/lib/squib/sprues/invalid_sprue_definition.rb +9 -9
  139. data/lib/squib/sprues/sprue.rb +208 -206
  140. data/lib/squib/sprues/sprue_schema.rb +50 -50
  141. data/lib/squib/version.rb +11 -10
  142. data/samples/autoscale_font/_autoscale_font.rb +98 -29
  143. data/samples/backend/_backend.rb +26 -26
  144. data/samples/basic.rb +19 -19
  145. data/samples/build_groups/build_groups.rb +36 -36
  146. data/samples/colors/_colors.rb +44 -44
  147. data/samples/colors/_gradients.rb +34 -34
  148. data/samples/colors/_switch_color.rb +33 -33
  149. data/samples/config/config_text_markup.rb +20 -20
  150. data/samples/config/custom_config.rb +18 -18
  151. data/samples/data/_csv.rb +33 -33
  152. data/samples/data/_excel.rb +55 -55
  153. data/samples/data/_yaml.rb +12 -12
  154. data/samples/hello_world.rb +6 -6
  155. data/samples/images/_cairo_access.rb +39 -39
  156. data/samples/images/_images.rb +104 -104
  157. data/samples/images/_more_load_images.rb +102 -102
  158. data/samples/intro/01_hello.rb +8 -8
  159. data/samples/intro/02_options.rb +14 -14
  160. data/samples/intro/03_layout.rb +11 -11
  161. data/samples/intro/04_arrays.rb +15 -15
  162. data/samples/intro/05_excel.rb +14 -14
  163. data/samples/layouts/builtin_layouts.rb +97 -97
  164. data/samples/layouts/layouts.rb +71 -71
  165. data/samples/project/src/characters.rb +8 -8
  166. data/samples/project/src/skills.rb +7 -7
  167. data/samples/proofs/_tgc_proofs.rb +16 -16
  168. data/samples/ranges/_ranges.rb +64 -64
  169. data/samples/saves/_hand.rb +23 -23
  170. data/samples/saves/_portrait_landscape.rb +23 -23
  171. data/samples/saves/_save_filenames.rb +28 -24
  172. data/samples/saves/_save_pdf.rb +29 -29
  173. data/samples/saves/_saves.rb +52 -51
  174. data/samples/saves/_showcase.rb +25 -25
  175. data/samples/shapes/_draw_shapes.rb +60 -60
  176. data/samples/shapes/_proofs.rb +22 -22
  177. data/samples/sprues/_advanced_sprues.rb +25 -25
  178. data/samples/sprues/_builtin_sprues.rb +22 -21
  179. data/samples/sprues/_fold_sheet.rb +30 -27
  180. data/samples/sprues/_hex_tiles.rb +15 -15
  181. data/samples/sprues/_mints.rb +11 -11
  182. data/samples/sprues/_sprue_example.rb +11 -11
  183. data/samples/text/_embed_text.rb +128 -128
  184. data/samples/text/_text.rb +52 -47
  185. data/samples/text/_text_options.rb +103 -102
  186. data/samples/text/bug134.rb +14 -14
  187. data/samples/units/_cells.rb +51 -0
  188. data/samples/units/_shorthands.rb +48 -0
  189. data/samples/units/_units.rb +39 -32
  190. data/squib.gemspec +58 -52
  191. metadata +177 -91
  192. data/.travis.yml +0 -17
  193. data/appveyor.yml +0 -25
  194. data/lib/squib/api/background.rb +0 -15
  195. data/lib/squib/api/data.rb +0 -137
  196. data/lib/squib/api/image.rb +0 -49
  197. data/lib/squib/api/save.rb +0 -83
  198. data/lib/squib/api/shapes.rb +0 -124
  199. data/lib/squib/api/text.rb +0 -25
  200. data/lib/squib/api/text_embed.rb +0 -71
@@ -1,47 +1,47 @@
1
- module Squib
2
- # @api private
3
- module Graphics
4
- STOPS = / # used to capture the stops
5
- \s* # leading whitespace is ok
6
- (\#?[\w]+) # color
7
- @ # no spaces here
8
- (\d+\.?\d*) # offset number
9
- /x
10
-
11
- LINEAR_GRADIENT = /
12
- \( \s* # coordinate 1
13
- (\d+\.?\d*) \s* # x1 number
14
- ,\s* # whitespace after comma is ok
15
- (\d+\.?\d*) \s* # y1 number
16
- \)
17
- \s* # space between coordinates is ok
18
- \( \s* # coordinate 2
19
- (\d+\.?\d*) \s* # x2 number
20
- ,\s* # whitespace after comma is ok
21
- (\d+\.?\d*) \s* # y2 number
22
- \)
23
- (#{STOPS})+ # stops
24
- \s* # trailing whitespace is ok
25
- /x
26
-
27
- RADIAL_GRADIENT = /
28
- \( \s* # coordinate 1
29
- (\d+\.?\d*) \s* # x1 number
30
- ,\s* # whitespace after comma is ok
31
- (\d+\.?\d*) \s* # y1 number
32
- ,\s* # whitespace after comma is ok
33
- (\d+\.?\d*) \s* # r1 number
34
- \)
35
- \s* # space between coordinates is ok
36
- \( \s* # coordinate 2
37
- (\d+\.?\d*) \s* # x2 number
38
- ,\s* # whitespace after comma is ok
39
- (\d+\.?\d*) \s* # y2 number
40
- ,\s* # whitespace after comma is ok
41
- (\d+\.?\d*) \s* # r2 number
42
- \)
43
- (#{STOPS})+ # stops
44
- \s* # trailing whitespace is ok
45
- /x
46
- end
47
- end
1
+ module Squib
2
+ # @api private
3
+ module Graphics
4
+ STOPS = / # used to capture the stops
5
+ \s* # leading whitespace is ok
6
+ (\#?[\w]+) # color
7
+ @ # no spaces here
8
+ (\d+\.?\d*) # offset number
9
+ /x
10
+
11
+ LINEAR_GRADIENT = /
12
+ \( \s* # coordinate 1
13
+ (\d+\.?\d*) \s* # x1 number
14
+ ,\s* # whitespace after comma is ok
15
+ (\d+\.?\d*) \s* # y1 number
16
+ \)
17
+ \s* # space between coordinates is ok
18
+ \( \s* # coordinate 2
19
+ (\d+\.?\d*) \s* # x2 number
20
+ ,\s* # whitespace after comma is ok
21
+ (\d+\.?\d*) \s* # y2 number
22
+ \)
23
+ (#{STOPS})+ # stops
24
+ \s* # trailing whitespace is ok
25
+ /x
26
+
27
+ RADIAL_GRADIENT = /
28
+ \( \s* # coordinate 1
29
+ (\d+\.?\d*) \s* # x1 number
30
+ ,\s* # whitespace after comma is ok
31
+ (\d+\.?\d*) \s* # y1 number
32
+ ,\s* # whitespace after comma is ok
33
+ (\d+\.?\d*) \s* # r1 number
34
+ \)
35
+ \s* # space between coordinates is ok
36
+ \( \s* # coordinate 2
37
+ (\d+\.?\d*) \s* # x2 number
38
+ ,\s* # whitespace after comma is ok
39
+ (\d+\.?\d*) \s* # y2 number
40
+ ,\s* # whitespace after comma is ok
41
+ (\d+\.?\d*) \s* # r2 number
42
+ \)
43
+ (#{STOPS})+ # stops
44
+ \s* # trailing whitespace is ok
45
+ /x
46
+ end
47
+ end
@@ -1,42 +1,42 @@
1
- require_relative 'cairo_context_wrapper'
2
-
3
- module Squib
4
- class Deck
5
-
6
- # Draw cards in a fan.
7
- # @api private
8
- def render_hand(range, sheet, hand)
9
- cards = range.collect { |i| @cards[i] }
10
- center_x = width / 2.0
11
- center_y = hand.radius + height
12
- out_size = 3.0 * center_y
13
- angle_delta = (hand.angle_range.last - hand.angle_range.first) / cards.size
14
- cxt = Cairo::Context.new(Cairo::RecordingSurface.new(0, 0, out_size, out_size))
15
- cxt.translate(out_size / 2.0, out_size / 2.0)
16
- cxt.rotate(hand.angle_range.first)
17
- cxt.translate(-width, -width)
18
- cards.each_with_index do |card, i|
19
- cxt.translate(center_x, center_y)
20
- cxt.rotate(angle_delta)
21
- cxt.translate(-center_x, -center_y)
22
- card.use_cairo do |card_cxt|
23
- cxt.rounded_rectangle(sheet.trim, sheet.trim,
24
- width - (2 * sheet.trim), height - (2 * sheet.trim),
25
- sheet.trim_radius, sheet.trim_radius)
26
- cxt.clip
27
- cxt.set_source(card_cxt.target)
28
- cxt.paint
29
- cxt.reset_clip
30
- end
31
- end
32
- x, y, w, h = cxt.target.ink_extents # I love Ruby assignment ;)
33
- png_cxt = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(Cairo::ImageSurface.new(w + 2 * sheet.margin, h + 2 * sheet.margin)))
34
- png_cxt.set_source_squibcolor(sheet.fill_color)
35
- png_cxt.paint
36
- png_cxt.translate(-x + sheet.margin, -y + sheet.margin)
37
- png_cxt.set_source(cxt.target)
38
- png_cxt.paint
39
- png_cxt.target.write_to_png sheet.full_filename
40
- end
41
- end
42
- end
1
+ require_relative 'cairo_context_wrapper'
2
+
3
+ module Squib
4
+ class Deck
5
+
6
+ # Draw cards in a fan.
7
+ # @api private
8
+ def render_hand(range, sheet, hand)
9
+ cards = range.collect { |i| @cards[i] }
10
+ center_x = width / 2.0
11
+ center_y = hand.radius + height
12
+ out_size = 3.0 * center_y
13
+ angle_delta = (hand.angle_range.last - hand.angle_range.first) / cards.size
14
+ cxt = Cairo::Context.new(Cairo::RecordingSurface.new(0, 0, out_size, out_size))
15
+ cxt.translate(out_size / 2.0, out_size / 2.0)
16
+ cxt.rotate(hand.angle_range.first)
17
+ cxt.translate(-width, -width)
18
+ cards.each_with_index do |card, i|
19
+ cxt.translate(center_x, center_y)
20
+ cxt.rotate(angle_delta)
21
+ cxt.translate(-center_x, -center_y)
22
+ card.use_cairo do |card_cxt|
23
+ cxt.rounded_rectangle(sheet.trim, sheet.trim,
24
+ width - (2 * sheet.trim), height - (2 * sheet.trim),
25
+ sheet.trim_radius, sheet.trim_radius)
26
+ cxt.clip
27
+ cxt.set_source(card_cxt.target)
28
+ cxt.paint
29
+ cxt.reset_clip
30
+ end
31
+ end
32
+ x, y, w, h = cxt.target.ink_extents # I love Ruby assignment ;)
33
+ png_cxt = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(Cairo::ImageSurface.new(w + 2 * sheet.margin, h + 2 * sheet.margin)))
34
+ png_cxt.set_source_squibcolor(sheet.fill_color)
35
+ png_cxt.paint
36
+ png_cxt.translate(-x + sheet.margin, -y + sheet.margin)
37
+ png_cxt.set_source(cxt.target)
38
+ png_cxt.paint
39
+ png_cxt.target.write_to_png sheet.full_filename
40
+ end
41
+ end
42
+ end
@@ -1,123 +1,123 @@
1
- module Squib
2
-
3
- # Cache all pngs we've already loaded
4
- #
5
- # :nodoc:
6
- # @api private
7
- def cache_load_image(file)
8
- @img_cache ||= {}
9
- @img_cache[file] ||= open_png file
10
- end
11
- module_function :cache_load_image
12
-
13
- # Open a PNG file, checking magic bytes if it's a real PNG
14
- # Magic bytes taken from:
15
- # https://en.wikipedia.org/wiki/List_of_file_signatures
16
- # :nodoc:
17
- # @api private
18
- PNG_MAGIC = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
19
- def open_png(file)
20
- if PNG_MAGIC == File.read(file, 8).bytes
21
- return Cairo::ImageSurface.from_png(file)
22
- else
23
- raise ArgumentError.new("ERROR: #{file} is not a PNG file")
24
- end
25
- end
26
- module_function :open_png
27
-
28
- class Card
29
-
30
- # :nodoc:
31
- # @api private
32
- def png(file, box, paint, trans)
33
- Squib.logger.debug {"RENDERING PNG: \n file: #{file}\n box: #{box}\n paint: #{paint}\n trans: #{trans}"}
34
- return if file.nil? or file.eql? ''
35
- png = Squib.cache_load_image(file)
36
- use_cairo do |cc|
37
- cc.translate(box.x, box.y)
38
- box.width = png.width.to_f if box.width == :native
39
- box.height = png.height.to_f if box.height == :native
40
- box.width = png.width.to_f * box.height.to_f / png.height.to_f if box.width == :scale
41
- box.height = png.height.to_f * box.width.to_f / png.width.to_f if box.height == :scale
42
-
43
- scale_width = box.width.to_f / png.width.to_f
44
- scale_height = box.height.to_f / png.height.to_f
45
- warn_png_scale(file, scale_width, scale_height)
46
- cc.scale(scale_width, scale_height)
47
-
48
- cc.rotate(trans.angle)
49
- cc.flip(trans.flip_vertical, trans.flip_horizontal, box.width / 2, box.height / 2)
50
- cc.translate(-box.x, -box.y)
51
-
52
- trans.crop_width = png.width.to_f if trans.crop_width == :native
53
- trans.crop_height = png.height.to_f if trans.crop_height == :native
54
- cc.rounded_rectangle(box.x, box.y, trans.crop_width, trans.crop_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius)
55
- cc.clip
56
- cc.translate(-trans.crop_x, -trans.crop_y)
57
-
58
-
59
- cc.set_source(png, box.x, box.y)
60
- cc.operator = paint.blend unless paint.blend == :none
61
- if paint.mask.empty?
62
- cc.paint(paint.alpha)
63
- else
64
- cc.set_source_squibcolor(paint.mask)
65
- cc.mask(png, box.x, box.y)
66
- end
67
- end
68
- end
69
-
70
- # :nodoc:
71
- # @api private
72
- def warn_png_scale(file, scale_width, scale_height)
73
- if @deck.conf.warn_png_scale? && (scale_width > 1.0 || scale_height > 1.0)
74
- Squib.logger.warn "PNG is being upscaled - antialiasing could result: #{file}"
75
- end
76
- end
77
-
78
- # :nodoc:
79
- # @api private
80
- def svg(file, svg_args, box, paint, trans)
81
- Squib.logger.debug {"Rendering: #{file}, id: #{id} @#{x},#{y} #{width}x#{height}, alpha: #{alpha}, blend: #{blend}, angle: #{angle}, mask: #{mask}"}
82
- Squib.logger.warn 'Both an SVG file and SVG data were specified' unless file.to_s.empty? || svg_args.data.to_s.empty?
83
- return if (file.nil? or file.eql? '') and svg_args.data.nil? # nothing specified TODO Move this out to arg validator
84
- svg_args.data = File.read(file) if svg_args.data.to_s.empty?
85
- begin
86
- svg = Rsvg::Handle.new_from_data(svg_args.data)
87
- rescue Rsvg::Error::Failed
88
- Squib.logger.error "Invalid SVG data. Is '#{file}' a valid svg file?"
89
- return
90
- end
91
- box.width = svg.width if box.width == :native
92
- box.height = svg.height if box.height == :native
93
- box.width = svg.width.to_f * box.height.to_f / svg.height.to_f if box.width == :scale
94
- box.height = svg.height.to_f * box.width.to_f / svg.width.to_f if box.height == :scale
95
- scale_width = box.width.to_f / svg.width.to_f
96
- scale_height = box.height.to_f / svg.height.to_f
97
- use_cairo do |cc|
98
- cc.translate(box.x, box.y)
99
- cc.flip(trans.flip_vertical, trans.flip_horizontal, box.width / 2, box.height / 2)
100
- cc.rotate(trans.angle)
101
- cc.scale(scale_width, scale_height)
102
-
103
- trans.crop_width = box.width if trans.crop_width == :native
104
- trans.crop_height = box.height if trans.crop_height == :native
105
- cc.rounded_rectangle(0, 0, trans.crop_width / scale_width, trans.crop_height / scale_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius)
106
- cc.clip
107
- cc.translate(-trans.crop_x, -trans.crop_y)
108
-
109
- cc.operator = paint.blend unless paint.blend == :none
110
- if paint.mask.to_s.empty?
111
- cc.render_rsvg_handle(svg, id: svg_args.id)
112
- else
113
- tmp = Cairo::ImageSurface.new(box.width / scale_width, box.height / scale_height)
114
- tmp_cc = Cairo::Context.new(tmp)
115
- tmp_cc.render_rsvg_handle(svg, id: svg_args.id)
116
- cc.set_source_squibcolor(paint.mask)
117
- cc.mask(tmp, 0, 0)
118
- end
119
- end
120
- end
121
-
122
- end
123
- end
1
+ module Squib
2
+
3
+ # Cache all pngs we've already loaded
4
+ #
5
+ # :nodoc:
6
+ # @api private
7
+ def cache_load_image(file)
8
+ @img_cache ||= {}
9
+ @img_cache[file] ||= open_png file
10
+ end
11
+ module_function :cache_load_image
12
+
13
+ # Open a PNG file, checking magic bytes if it's a real PNG
14
+ # Magic bytes taken from:
15
+ # https://en.wikipedia.org/wiki/List_of_file_signatures
16
+ # :nodoc:
17
+ # @api private
18
+ PNG_MAGIC = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]
19
+ def open_png(file)
20
+ if PNG_MAGIC == File.read(file, 8).bytes
21
+ return Cairo::ImageSurface.from_png(file)
22
+ else
23
+ raise ArgumentError.new("ERROR: #{file} is not a PNG file")
24
+ end
25
+ end
26
+ module_function :open_png
27
+
28
+ class Card
29
+
30
+ # :nodoc:
31
+ # @api private
32
+ def png(file, box, paint, trans)
33
+ Squib.logger.debug {"RENDERING PNG: \n file: #{file}\n box: #{box}\n paint: #{paint}\n trans: #{trans}"}
34
+ return if file.nil? or file.eql? ''
35
+ png = Squib.cache_load_image(file)
36
+ use_cairo do |cc|
37
+ cc.translate(box.x, box.y)
38
+ box.width = png.width.to_f if box.width == :native
39
+ box.height = png.height.to_f if box.height == :native
40
+ box.width = png.width.to_f * box.height.to_f / png.height.to_f if box.width == :scale
41
+ box.height = png.height.to_f * box.width.to_f / png.width.to_f if box.height == :scale
42
+
43
+ scale_width = box.width.to_f / png.width.to_f
44
+ scale_height = box.height.to_f / png.height.to_f
45
+ warn_png_scale(file, scale_width, scale_height)
46
+ cc.scale(scale_width, scale_height)
47
+
48
+ cc.rotate(trans.angle)
49
+ cc.flip(trans.flip_vertical, trans.flip_horizontal, box.width / 2, box.height / 2)
50
+ cc.translate(-box.x, -box.y)
51
+
52
+ trans.crop_width = png.width.to_f if trans.crop_width == :native
53
+ trans.crop_height = png.height.to_f if trans.crop_height == :native
54
+ cc.rounded_rectangle(box.x, box.y, trans.crop_width, trans.crop_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius)
55
+ cc.clip
56
+ cc.translate(-trans.crop_x, -trans.crop_y)
57
+
58
+
59
+ cc.set_source(png, box.x, box.y)
60
+ cc.operator = paint.blend unless paint.blend == :none
61
+ if paint.mask.empty?
62
+ cc.paint(paint.alpha)
63
+ else
64
+ cc.set_source_squibcolor(paint.mask)
65
+ cc.mask(png, box.x, box.y)
66
+ end
67
+ end
68
+ end
69
+
70
+ # :nodoc:
71
+ # @api private
72
+ def warn_png_scale(file, scale_width, scale_height)
73
+ if @deck.conf.warn_png_scale? && (scale_width > 1.0 || scale_height > 1.0)
74
+ Squib.logger.warn "PNG is being upscaled - antialiasing could result: #{file}"
75
+ end
76
+ end
77
+
78
+ # :nodoc:
79
+ # @api private
80
+ def svg(file, svg_args, box, paint, trans)
81
+ Squib.logger.debug {"Rendering: #{file}, id: #{id} @#{x},#{y} #{width}x#{height}, alpha: #{alpha}, blend: #{blend}, angle: #{angle}, mask: #{mask}"}
82
+ Squib.logger.warn 'Both an SVG file and SVG data were specified' unless file.to_s.empty? || svg_args.data.to_s.empty?
83
+ return if (file.nil? or file.eql? '') and svg_args.data.nil? # nothing specified TODO Move this out to arg validator
84
+ svg_args.data = File.read(file) if svg_args.data.to_s.empty?
85
+ begin
86
+ svg = Rsvg::Handle.new_from_data(svg_args.data)
87
+ rescue Rsvg::Error::Failed
88
+ Squib.logger.error "Invalid SVG data. Is '#{file}' a valid svg file?"
89
+ return
90
+ end
91
+ box.width = svg.width if box.width == :native
92
+ box.height = svg.height if box.height == :native
93
+ box.width = svg.width.to_f * box.height.to_f / svg.height.to_f if box.width == :scale
94
+ box.height = svg.height.to_f * box.width.to_f / svg.width.to_f if box.height == :scale
95
+ scale_width = box.width.to_f / svg.width.to_f
96
+ scale_height = box.height.to_f / svg.height.to_f
97
+ use_cairo do |cc|
98
+ cc.translate(box.x, box.y)
99
+ cc.flip(trans.flip_vertical, trans.flip_horizontal, box.width / 2, box.height / 2)
100
+ cc.rotate(trans.angle)
101
+ cc.scale(scale_width, scale_height)
102
+
103
+ trans.crop_width = box.width if trans.crop_width == :native
104
+ trans.crop_height = box.height if trans.crop_height == :native
105
+ cc.rounded_rectangle(0, 0, trans.crop_width / scale_width, trans.crop_height / scale_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius)
106
+ cc.clip
107
+ cc.translate(-trans.crop_x, -trans.crop_y)
108
+
109
+ cc.operator = paint.blend unless paint.blend == :none
110
+ if paint.mask.to_s.empty?
111
+ cc.render_rsvg_handle(svg, id: svg_args.id)
112
+ else
113
+ tmp = Cairo::ImageSurface.new(box.width / scale_width, box.height / scale_height)
114
+ tmp_cc = Cairo::Context.new(tmp)
115
+ tmp_cc.render_rsvg_handle(svg, id: svg_args.id)
116
+ cc.set_source_squibcolor(paint.mask)
117
+ cc.mask(tmp, 0, 0)
118
+ end
119
+ end
120
+ end
121
+
122
+ end
123
+ end
@@ -1,61 +1,61 @@
1
- module Squib
2
- class Deck
3
-
4
- # :nodoc:
5
- # @api private
6
- def render_sheet(range, batch, sheet)
7
- sheet_width = (sheet.columns * (@width + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
8
- sheet_height = (sheet.rows * (@height + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
9
- cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
10
- num_this_sheet = 0
11
- sheet_num = 0
12
- y = sheet.margin
13
- x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
14
- @progress_bar.start("Saving PNG sheet to #{batch.summary}", @cards.size + 1) do |bar|
15
- range.each do |i|
16
- if num_this_sheet >= (sheet.columns * sheet.rows) # new sheet
17
- filename = batch.full_filename(sheet_num)
18
- cc.target.write_to_png(filename)
19
- new_sheet = false
20
- num_this_sheet = 0
21
- sheet_num += 1
22
- y = sheet.margin
23
- x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
24
- cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
25
- end
26
- surface = trim(@cards[i].cairo_surface, sheet.trim, @width, @height)
27
- cc.set_source(surface, x, y)
28
- cc.paint
29
- num_this_sheet += 1
30
- x += (surface.width + sheet.gap) * (sheet.rtl ? -1 : 1)
31
- if num_this_sheet % sheet.columns == 0 # new row
32
- x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
33
- y += surface.height + sheet.gap
34
- end
35
- bar.increment
36
- end
37
- cc.target.write_to_png(batch.full_filename(sheet_num))
38
- end
39
- end
40
-
41
- # Return a new Cairo::ImageSurface that is trimmed from the original
42
- #
43
- # @param surface The surface to trim
44
- # @param trim The number of pixels around the edge to trim
45
- # @param width The width of the surface prior to the trim
46
- # @param height The height of the surface prior to the trim
47
- # :nodoc:
48
- # @api private
49
- def trim(surface, trim, width, height)
50
- if trim > 0
51
- tmp = Cairo::ImageSurface.new(width - 2 * trim, height - 2 * trim)
52
- cc = Cairo::Context.new(tmp)
53
- cc.set_source(surface, -1 * trim, -1 * trim)
54
- cc.paint
55
- surface = tmp
56
- end
57
- surface
58
- end
59
-
60
- end
61
- end
1
+ module Squib
2
+ class Deck
3
+
4
+ # :nodoc:
5
+ # @api private
6
+ def render_sheet(range, batch, sheet)
7
+ sheet_width = (sheet.columns * (@width + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
8
+ sheet_height = (sheet.rows * (@height + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
9
+ cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
10
+ num_this_sheet = 0
11
+ sheet_num = 0
12
+ y = sheet.margin
13
+ x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
14
+ @progress_bar.start("Saving PNG sheet to #{batch.summary}", @cards.size + 1) do |bar|
15
+ range.each do |i|
16
+ if num_this_sheet >= (sheet.columns * sheet.rows) # new sheet
17
+ filename = batch.full_filename(sheet_num)
18
+ cc.target.write_to_png(filename)
19
+ new_sheet = false
20
+ num_this_sheet = 0
21
+ sheet_num += 1
22
+ y = sheet.margin
23
+ x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
24
+ cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
25
+ end
26
+ surface = trim(@cards[i].cairo_surface, sheet.trim, @width, @height)
27
+ cc.set_source(surface, x, y)
28
+ cc.paint
29
+ num_this_sheet += 1
30
+ x += (surface.width + sheet.gap) * (sheet.rtl ? -1 : 1)
31
+ if num_this_sheet % sheet.columns == 0 # new row
32
+ x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
33
+ y += surface.height + sheet.gap
34
+ end
35
+ bar.increment
36
+ end
37
+ cc.target.write_to_png(batch.full_filename(sheet_num))
38
+ end
39
+ end
40
+
41
+ # Return a new Cairo::ImageSurface that is trimmed from the original
42
+ #
43
+ # @param surface The surface to trim
44
+ # @param trim The number of pixels around the edge to trim
45
+ # @param width The width of the surface prior to the trim
46
+ # @param height The height of the surface prior to the trim
47
+ # :nodoc:
48
+ # @api private
49
+ def trim(surface, trim, width, height)
50
+ if trim > 0
51
+ tmp = Cairo::ImageSurface.new(width - 2 * trim, height - 2 * trim)
52
+ cc = Cairo::Context.new(tmp)
53
+ cc.set_source(surface, -1 * trim, -1 * trim)
54
+ cc.paint
55
+ surface = tmp
56
+ end
57
+ surface
58
+ end
59
+
60
+ end
61
+ end