squib 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +162 -133
  3. data/Gemfile +4 -4
  4. data/README.md +630 -550
  5. data/RELEASE TODO.md +18 -18
  6. data/Rakefile +99 -99
  7. data/lib/squib.rb +32 -32
  8. data/lib/squib/api/background.rb +20 -19
  9. data/lib/squib/api/data.rb +100 -99
  10. data/lib/squib/api/image.rb +90 -76
  11. data/lib/squib/api/save.rb +149 -103
  12. data/lib/squib/api/settings.rb +35 -37
  13. data/lib/squib/api/shapes.rb +230 -228
  14. data/lib/squib/api/text.rb +65 -66
  15. data/lib/squib/api/text_embed.rb +96 -66
  16. data/lib/squib/args/arg_loader.rb +138 -0
  17. data/lib/squib/args/box.rb +55 -0
  18. data/lib/squib/args/card_range.rb +32 -0
  19. data/lib/squib/args/color_validator.rb +12 -0
  20. data/lib/squib/args/coords.rb +33 -0
  21. data/lib/squib/args/dir_validator.rb +16 -0
  22. data/lib/squib/args/draw.rb +92 -0
  23. data/lib/squib/args/embed_adjust.rb +25 -0
  24. data/lib/squib/args/embed_key.rb +17 -0
  25. data/lib/squib/args/hand_special.rb +37 -0
  26. data/lib/squib/args/input_file.rb +37 -0
  27. data/lib/squib/args/paint.rb +44 -0
  28. data/lib/squib/args/paragraph.rb +115 -0
  29. data/lib/squib/args/save_batch.rb +60 -0
  30. data/lib/squib/args/scale_box.rb +53 -0
  31. data/lib/squib/args/sheet.rb +72 -0
  32. data/lib/squib/args/showcase_special.rb +38 -0
  33. data/lib/squib/args/svg_special.rb +37 -0
  34. data/lib/squib/args/transform.rb +25 -0
  35. data/lib/squib/args/typographer.rb +117 -117
  36. data/lib/squib/card.rb +67 -67
  37. data/lib/squib/conf.rb +117 -111
  38. data/lib/squib/constants.rb +178 -178
  39. data/lib/squib/deck.rb +113 -111
  40. data/lib/squib/graphics/cairo_context_wrapper.rb +99 -53
  41. data/lib/squib/graphics/gradient_regex.rb +46 -46
  42. data/lib/squib/graphics/hand.rb +42 -43
  43. data/lib/squib/graphics/image.rb +76 -73
  44. data/lib/squib/graphics/save_doc.rb +103 -137
  45. data/lib/squib/graphics/save_images.rb +33 -33
  46. data/lib/squib/graphics/shapes.rb +119 -152
  47. data/lib/squib/graphics/showcase.rb +85 -88
  48. data/lib/squib/graphics/text.rb +176 -216
  49. data/lib/squib/layout_parser.rb +91 -89
  50. data/lib/squib/layouts/economy.yml +85 -0
  51. data/lib/squib/layouts/fantasy.yml +101 -0
  52. data/lib/squib/layouts/hand.yml +62 -46
  53. data/lib/squib/layouts/playing-card.yml +35 -18
  54. data/lib/squib/project_template/config.yml +45 -40
  55. data/lib/squib/version.rb +10 -10
  56. data/samples/color_shortcuts.rb +6 -0
  57. data/samples/csv_import.rb +18 -18
  58. data/samples/custom-config.yml +5 -5
  59. data/samples/custom_config.rb +18 -18
  60. data/samples/draw_shapes.rb +45 -35
  61. data/samples/embed_text.rb +88 -90
  62. data/samples/hand.rb +24 -24
  63. data/samples/layouts.rb +62 -61
  64. data/samples/layouts_builtin.rb +51 -0
  65. data/samples/load_images.rb +78 -64
  66. data/samples/ranges.rb +64 -53
  67. data/samples/sample.csv +2 -2
  68. data/samples/text_options.rb +102 -94
  69. data/spec/api/api_data_spec.rb +57 -50
  70. data/spec/api/api_settings_spec.rb +37 -17
  71. data/spec/args/box_spec.rb +127 -0
  72. data/spec/args/draw_spec.rb +95 -0
  73. data/spec/args/embed_key_spec.rb +13 -0
  74. data/spec/args/input_file_spec.rb +21 -0
  75. data/spec/args/paint_spec.rb +22 -0
  76. data/spec/args/paragraph_spec.rb +153 -0
  77. data/spec/args/range_spec.rb +36 -0
  78. data/spec/args/save_batch_spec.rb +51 -0
  79. data/spec/args/scale_box_spec.rb +71 -0
  80. data/spec/args/sheet_spec.rb +58 -0
  81. data/spec/args/showcase_special_spec.rb +15 -0
  82. data/spec/data/samples/autoscale_font.rb.txt +84 -87
  83. data/spec/data/samples/basic.rb.txt +209 -203
  84. data/spec/data/samples/cairo_access.rb.txt +2 -2
  85. data/spec/data/samples/config_text_markup.rb.txt +72 -75
  86. data/spec/data/samples/csv_import.rb.txt +76 -80
  87. data/spec/data/samples/custom_config.rb.txt +48 -49
  88. data/spec/data/samples/draw_shapes.rb.txt +100 -42
  89. data/spec/data/samples/embed_text.rb.txt +283 -295
  90. data/spec/data/samples/excel.rb.txt +162 -171
  91. data/spec/data/samples/gradients.rb.txt +79 -67
  92. data/spec/data/samples/hand.rb.txt +538 -514
  93. data/spec/data/samples/hello_world.rb.txt +36 -38
  94. data/spec/data/samples/load_images.rb.txt +41 -5
  95. data/spec/data/samples/portrait-landscape.rb.txt +49 -51
  96. data/spec/data/samples/ranges.rb.txt +460 -429
  97. data/spec/data/samples/saves.rb.txt +801 -785
  98. data/spec/data/samples/showcase.rb.txt +5910 -5906
  99. data/spec/data/samples/text_options.rb.txt +1125 -981
  100. data/spec/data/samples/tgc_proofs.rb.txt +81 -79
  101. data/spec/data/samples/units.rb.txt +18 -12
  102. data/spec/data/xlsx/with_macros.xlsm +0 -0
  103. data/spec/graphics/cairo_context_wrapper_spec.rb +84 -75
  104. data/spec/graphics/graphics_images_spec.rb +94 -85
  105. data/spec/graphics/graphics_save_doc_spec.rb +67 -65
  106. data/spec/samples/expected/hand.png +0 -0
  107. data/spec/samples/expected/hand_pretty.png +0 -0
  108. data/spec/samples/expected/layout_00.png +0 -0
  109. data/spec/samples/expected/load_images_00.png +0 -0
  110. data/spec/samples/expected/ranges_00.png +0 -0
  111. data/spec/samples/expected/shape_00.png +0 -0
  112. data/spec/samples/expected/showcase.png +0 -0
  113. data/spec/samples/expected/showcase2.png +0 -0
  114. data/spec/samples/expected/showcase_individual_00.png +0 -0
  115. data/spec/samples/expected/showcase_individual_01.png +0 -0
  116. data/spec/samples/expected/showcase_individual_02.png +0 -0
  117. data/spec/samples/expected/showcase_individual_03.png +0 -0
  118. data/spec/samples/expected/text_00.png +0 -0
  119. data/spec/samples/expected/text_01.png +0 -0
  120. data/spec/samples/expected/text_02.png +0 -0
  121. data/spec/samples/samples_regression_spec.rb +82 -82
  122. data/spec/spec_helper.rb +3 -2
  123. data/squib.gemspec +48 -48
  124. data/squib.sublime-project +42 -36
  125. metadata +61 -33
  126. data/lib/squib/input_helpers.rb +0 -238
  127. data/spec/api/api_image_spec.rb +0 -38
  128. data/spec/api/api_text_spec.rb +0 -37
  129. data/spec/graphics/graphics_shapes_spec.rb +0 -85
  130. data/spec/graphics/graphics_text_spec.rb +0 -164
  131. data/spec/input_helpers_spec.rb +0 -238
  132. data/spec/samples/expected/embed_multi_00.png +0 -0
  133. data/spec/samples/expected/embed_multi_01.png +0 -0
  134. data/spec/samples/expected/embed_multi_02.png +0 -0
  135. data/spec/samples/expected/ranges_01.png +0 -0
  136. data/spec/samples/expected/ranges_02.png +0 -0
@@ -1,66 +1,65 @@
1
- require 'squib/api/text_embed'
2
-
3
- module Squib
4
- class Deck
5
-
6
- # Renders a string at a given location, width, alignment, font, etc.
7
- #
8
- # Unix-like newlines are interpreted even on Windows.
9
- # See the {file:samples/text-options.rb samples/text.rb} for a lengthy example.
10
- #
11
- # @example
12
- # text str: 'hello'
13
- # text str: 'hello', x: 50, y:50, align: center
14
- #
15
- # Options support Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
16
- #
17
- # @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges}
18
- # @option opts str [String, Array] ('') the string to be rendered. Must support `#to_s`. If the card responds to `#each`, it's mapped out one at a time across the cards.
19
- # @option opts font [String] (Arial 36 or whatever was set with `set`) the Font description string, including family, styles, and size.
20
- # (e.g. `'Arial bold italic 12'`)
21
- # For the official documentation, see the [Pango docs](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Pango%3A%3AFontDescription#style).
22
- # This [description](http://www.pygtk.org/pygtk2reference/class-pangofontdescription.html) is also quite good.
23
- # See the {file:samples/text-options.rb samples/text.rb} as well.
24
- # @option opts font_size [Integer] (nil) an override of font string description, for scaling the font according to the size of the string
25
- # @option opts x [Integer] (0) the x-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
26
- # @option opts y [Integer] (0) the y-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
27
- # @option opts color [String] (:black) the color the font will render to. Gradients supported. See {file:README.md#Specifying_Colors___Gradients Specifying Colors}
28
- # @option opts markup: [Boolean] (false) Enable markup parsing of `str` using the HTML-like Pango Markup syntax, defined [here](http://ruby-gnome2.sourceforge.jp/hiki.cgi?pango-markup) and [here](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html). Also does other replacements, such as smart quotes, curly apostraphes, en- and em-dashes, and explict ellipses (not to be confused with ellipsize option). See README for full explanation.
29
- # @option opts width [Integer, :native] (:native) the width of the box the string will be placed in. Stretches to the content by default.. Supports Unit Conversion, see {file:README.md#Units Units}.
30
- # @option opts height [Integer, :native] the height of the box the string will be placed in. Stretches to the content by default. Supports Unit Conversion, see {file:README.md#Units Units}.
31
- # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}
32
- # @option opts wrap [:none, :word, :char, :word_char, true, false] (:word_char) When height is set, determines the behavior of how the string wraps. The `:word_char` option will break at words, but then fall back to characters when the word cannot fit. #
33
- # Options are `:none, :word, :char, :word_char`. Also: `true` is the same as `:word_char`, `false` is the same as `:none`. Default `:word_char`
34
- # @option opts spacing [Integer] (0) Adjust the spacing when the text is multiple lines. No effect when the text does not wrap.
35
- # @option opts align [:left, right, :center] (:left) The alignment of the text
36
- # @option opts justify [Boolean] (false) toggles whether or not the text is justified or not.
37
- # @option opts valign [:top, :middle, :bottom] (:top) When width and height are set, align text vertically according to the ink extents of the text.
38
- # @option opts ellipsize [:none, :start, :middle, :end, true, false] (:end) When width and height are set, determines the behavior of overflowing text. Also: `true` maps to `:end` and `false` maps to `:none`. Default `:end`
39
- # @option opts angle [FixNum] (0) Rotation of the text in radians. Note that this rotates around the upper-left corner of the text box, making the placement of x-y coordinates slightly tricky.
40
- # @option opts stroke_color [String] (:black) the color with which to stroke the outside of the rectangle. {file:README.md#Specifying_Colors___Gradients Specifying Colors & Gradients}
41
- # @option opts stroke_width [Decimal] (2.0) the width of the outside stroke. Supports Unit Conversion, see {file:README.md#Units Units}.
42
- # @option opts hint [String] (:nil) draw a rectangle around the text with the given color. Overrides global hints (see {Deck#hint}).
43
- # @return [Array] Returns an Array of hashes keyed by :width and :height that mark the ink extents of the text rendered.
44
- # @api public
45
- def text(opts = {})
46
- opts = { stroke_width: 0 }.merge(opts)
47
- opts = needs(opts, [:range, :str, :font, :font_size, :x, :y, :width, :height, :color, :wrap,
48
- :align, :justify, :spacing, :valign, :markup, :ellipsize, :hint, :layout,
49
- :angle, :quotes, :stroke_color, :stroke_width])
50
- embed = TextEmbed.new
51
- yield(embed) if block_given? #store the opts for later use
52
- extents = Array.new(@cards.size)
53
- opts[:range].each do |i|
54
- extents[i] = @cards[i].text(embed, opts[:str][i],
55
- opts[:font][i], opts[:font_size][i], opts[:color][i],
56
- opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i],
57
- opts[:markup][i], opts[:justify][i], opts[:wrap][i],
58
- opts[:ellipsize][i], opts[:spacing][i], opts[:align][i],
59
- opts[:valign][i], opts[:hint][i], opts[:angle][i],
60
- opts[:stroke_color][i], opts[:stroke_width][i] )
61
- end
62
- return extents
63
- end
64
-
65
- end
66
- end
1
+ require 'squib/api/text_embed'
2
+ require 'squib/args/box'
3
+ require 'squib/args/card_range'
4
+ require 'squib/args/draw'
5
+ require 'squib/args/paragraph'
6
+
7
+ module Squib
8
+ class Deck
9
+
10
+ # Renders a string at a given location, width, alignment, font, etc.
11
+ #
12
+ # Unix-like newlines are interpreted even on Windows.
13
+ # See the {file:samples/text-options.rb samples/text.rb} for a lengthy example.
14
+ #
15
+ # @example
16
+ # text str: 'hello'
17
+ # text str: 'hello', x: 50, y:50, align: center
18
+ #
19
+ # Options support Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
20
+ #
21
+ # @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges}
22
+ # @option opts str [String, Array] ('') the string to be rendered. Must support `#to_s`. If the card responds to `#each`, it's mapped out one at a time across the cards.
23
+ # @option opts font [String] (Arial 36 or whatever was set with `set`) the Font description string, including family, styles, and size.
24
+ # (e.g. `'Arial bold italic 12'`)
25
+ # For the official documentation, see the [Pango docs](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Pango%3A%3AFontDescription#style).
26
+ # This [description](http://www.pygtk.org/pygtk2reference/class-pangofontdescription.html) is also quite good.
27
+ # See the {file:samples/text-options.rb samples/text.rb} as well.
28
+ # @option opts font_size [Integer] (nil) an override of font string description, for scaling the font according to the size of the string
29
+ # @option opts x [Integer] (0) the x-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
30
+ # @option opts y [Integer] (0) the y-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
31
+ # @option opts color [String] (:black) the color the font will render to. Gradients supported. See {file:README.md#Specifying_Colors___Gradients Specifying Colors}
32
+ # @option opts markup: [Boolean] (false) Enable markup parsing of `str` using the HTML-like Pango Markup syntax, defined [here](http://ruby-gnome2.sourceforge.jp/hiki.cgi?pango-markup) and [here](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html). Also does other replacements, such as smart quotes, curly apostraphes, en- and em-dashes, and explict ellipses (not to be confused with ellipsize option). See README for full explanation.
33
+ # @option opts width [Integer, :auto] (:auto) the width of the box the string will be placed in. Stretches to the content by default.. Supports Unit Conversion, see {file:README.md#Units Units}.
34
+ # @option opts height [Integer, :auto] the height of the box the string will be placed in. Stretches to the content by default. Supports Unit Conversion, see {file:README.md#Units Units}.
35
+ # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}
36
+ # @option opts wrap [:none, :word, :char, :word_char, true, false] (:word_char) When height is set, determines the behavior of how the string wraps. The `:word_char` option will break at words, but then fall back to characters when the word cannot fit. #
37
+ # Options are `:none, :word, :char, :word_char`. Also: `true` is the same as `:word_char`, `false` is the same as `:none`. Default `:word_char`
38
+ # @option opts spacing [Integer] (0) Adjust the spacing when the text is multiple lines. No effect when the text does not wrap.
39
+ # @option opts align [:left, right, :center] (:left) The alignment of the text
40
+ # @option opts justify [Boolean] (false) toggles whether or not the text is justified or not.
41
+ # @option opts valign [:top, :middle, :bottom] (:top) When width and height are set, align text vertically according to the ink extents of the text.
42
+ # @option opts ellipsize [:none, :start, :middle, :end, true, false] (:end) When width and height are set, determines the behavior of overflowing text. Also: `true` maps to `:end` and `false` maps to `:none`. Default `:end`
43
+ # @option opts angle [FixNum] (0) Rotation of the text in radians. Note that this rotates around the upper-left corner of the text box, making the placement of x-y coordinates slightly tricky.
44
+ # @option opts stroke_width [Decimal] (0.0) the width of the outside stroke. Supports Unit Conversion, see {file:README.md#Units Units}.
45
+ # @option opts stroke_color [String] (:black) the color with which to stroke the outside of the rectangle. {file:README.md#Specifying_Colors___Gradients Specifying Colors & Gradients}
46
+ # @option opts stroke_strategy [:fill_first, :stroke_first] (:fill_first) specify whether the stroke is done before (thinner) or after (thicker) filling the shape.
47
+ # @option opts dash [String] ('') define a dash pattern for the stroke. Provide a string with space-separated numbers that define the pattern of on-and-off alternating strokes, measured in pixels by defautl. Supports Unit Conversion, see {file:README.md#Units Units} (e.g. `'0.02in 0.02in'`).
48
+ # @option opts hint [String] (:nil) draw a rectangle around the text with the given color. Overrides global hints (see {Deck#hint}).
49
+ # @return [Array] Returns an Array of hashes keyed by :width and :height that mark the ink extents of the text rendered.
50
+ # @api public
51
+ def text(opts = {})
52
+ range = Args::CardRange.new(opts[:range], deck_size: size)
53
+ para = Args::Paragraph.new(font).load!(opts, expand_by: size, layout: layout)
54
+ box = Args::Box.new(self, {width: :auto, height: :auto}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
55
+ trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
56
+ draw = Args::Draw.new(custom_colors, {stroke_width: 0.0}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
57
+ embed = TextEmbed.new(size, custom_colors, layout, dpi, img_dir)
58
+ yield(embed) if block_given? #store the opts for later use
59
+ extents = Array.new(@cards.size)
60
+ range.each { |i| extents[i] = @cards[i].text(embed, para[i], box[i], trans[i], draw[i]) }
61
+ return extents
62
+ end
63
+
64
+ end
65
+ end
@@ -1,66 +1,96 @@
1
- module Squib
2
- class TextEmbed
3
- # :nodoc:
4
- # @api private
5
- attr_reader :rules
6
-
7
- # :nodoc:
8
- # @api private
9
- def initialize
10
- @rules = {} # store an array of options for later usage
11
- end
12
-
13
- # Context object for embedding an svg icon within text
14
- #
15
- # @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:'
16
- # @option opts file [String] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
17
- # @option opts id [String] (nil) if set, then only render the SVG element with the given id. Prefix '#' is optional. Note: the x-y coordinates are still relative to the SVG document's page. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
18
- # @option opts force_id [Boolean] (false) if set, then this svg will not be rendered at all if the id is empty or nil. If not set, the entire SVG is rendered. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
19
- # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
20
- # @option opts width [Integer] (32) the width of the image rendered.
21
- # @option opts height [Integer] (32) the height the height of the image rendered.
22
- # @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
23
- # @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
24
- # @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
25
- # @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
26
- # @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
27
- # @api public
28
- def svg(opts)
29
- opts = { width: 32, height: 32 }.merge(opts)
30
- opts = Squib::SYSTEM_DEFAULTS.merge(opts)
31
- # TODO: add input validation here. We need the key for example.
32
- rule = {type: :svg}.merge(opts)
33
- rule[:draw] = Proc.new do |card, x,y|
34
- card.svg(rule[:file], rule[:data], rule[:id], x, y, rule[:width], rule[:height],
35
- rule[:alpha], rule[:blend], rule[:angle], rule[:mask])
36
- end
37
- @rules[opts[:key]] = rule
38
- end
39
-
40
- # Context object for embedding a png within text
41
- #
42
- # @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:'
43
- # @option opts file [String] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
44
- # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
45
- # @option opts width [Fixnum] (32) the width of the image rendered
46
- # @option opts height [Fixnum] (32) the height of the image rendered
47
- # @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
48
- # @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
49
- # @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
50
- # @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
51
- # @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
52
- # @api public
53
- def png(opts)
54
- opts = { width: 32, height: 32 }.merge(opts)
55
- opts = Squib::SYSTEM_DEFAULTS.merge(opts)
56
- # TODO: add input validation here. We need the key for example.
57
- rule = {type: :png}.merge(opts)
58
- rule[:draw] = Proc.new do |card, x,y|
59
- card.png(rule[:file], x, y, rule[:width], rule[:height],
60
- rule[:alpha], rule[:blend], rule[:angle], rule[:mask])
61
- end
62
- @rules[opts[:key]] = rule
63
- end
64
-
65
- end
66
- end
1
+ require 'squib/args/box'
2
+ require 'squib/args/card_range'
3
+ require 'squib/args/embed_adjust'
4
+ require 'squib/args/embed_key'
5
+ require 'squib/args/input_file'
6
+ require 'squib/args/paint'
7
+ require 'squib/args/transform'
8
+
9
+ module Squib
10
+ class TextEmbed
11
+ # :nodoc:
12
+ # @api private
13
+ attr_reader :rules
14
+
15
+ # :nodoc:
16
+ # @api private
17
+ def initialize(deck_size, custom_colors, layout, dpi, img_dir)
18
+ @deck_size = deck_size
19
+ @custom_colors = custom_colors
20
+ @layout = layout
21
+ @dpi = dpi
22
+ @img_dir = img_dir
23
+ @rules = {} # store an array of options for later usage
24
+ end
25
+
26
+ # Context object for embedding an svg icon within text
27
+ #
28
+ # @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:'
29
+ # @option opts file [String] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
30
+ # @option opts id [String] (nil) if set, then only render the SVG element with the given id. Prefix '#' is optional. Note: the x-y coordinates are still relative to the SVG document's page. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
31
+ # @option opts force_id [Boolean] (false) if set, then this svg will not be rendered at all if the id is empty or nil. If not set, the entire SVG is rendered. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
32
+ # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
33
+ # @option opts width [Integer] (:native) the width of the image rendered.
34
+ # @option opts height [Integer] (:native) the height the height of the image rendered.
35
+ # @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
36
+ # @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
37
+ # @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
38
+ # @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
39
+ # @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
40
+ # @api public
41
+ def svg(opts = {})
42
+ key = Args::EmbedKey.new.validate_key(opts[:key])
43
+ range = Args::CardRange.new(opts[:range], deck_size: @deck_size)
44
+ paint = Args::Paint.new(@custom_colors).load!(opts, expand_by: @deck_size, layout: @layout)
45
+ box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
46
+ adjust= Args::EmbedAdjust.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
47
+ trans = Args::Transform.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
48
+ ifile = Args::InputFile.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
49
+ svg_args = Args::SvgSpecial.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
50
+ rule = { type: :png, file: ifile, box: box, paint: paint, trans: trans, adjust: adjust }
51
+ rule[:draw] = Proc.new do |card, x, y|
52
+ i = card.index
53
+ b = box[i]
54
+ b.x, b.y = x, y
55
+ Dir.chdir(@img_dir) do
56
+ card.svg(ifile[i].file, svg_args[i], b, paint[i], trans[i])
57
+ end
58
+ end
59
+ @rules[key] = rule
60
+ end
61
+
62
+ # Context object for embedding a png within text
63
+ #
64
+ # @option opts key [String] ('*') the string to replace with the graphic. Can be multiple letters, e.g. ':tool:'
65
+ # @option opts file [String] ('') file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
66
+ # @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
67
+ # @option opts width [Fixnum] (:native) the width of the image rendered
68
+ # @option opts height [Fixnum] (:native) the height of the image rendered
69
+ # @option opts dx [Integer] (0) "delta x", or adjust the icon horizontally by x pixels
70
+ # @option opts dy [Integer] (0) "delta y", or adjust the icon vertically by y pixels
71
+ # @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
72
+ # @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
73
+ # @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
74
+ # @api public
75
+ def png(opts = {})
76
+ key = Args::EmbedKey.new.validate_key(opts[:key])
77
+ range = Args::CardRange.new(opts[:range], deck_size: @deck_size)
78
+ paint = Args::Paint.new(@custom_colors).load!(opts, expand_by: @deck_size, layout: @layout)
79
+ box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
80
+ adjust= Args::EmbedAdjust.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
81
+ trans = Args::Transform.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
82
+ ifile = Args::InputFile.new.load!(opts, expand_by: @deck_size, layout: @layout, dpi: @dpi)
83
+ rule = { type: :png, file: ifile, box: box, paint: paint, trans: trans, adjust: adjust }
84
+ rule[:draw] = Proc.new do |card, x, y|
85
+ i = card.index
86
+ b = box[i]
87
+ b.x, b.y = x, y
88
+ Dir.chdir(@img_dir) do
89
+ card.png(ifile[i].file, b, paint[i], trans[i])
90
+ end
91
+ end
92
+ @rules[key] = rule
93
+ end
94
+
95
+ end
96
+ end
@@ -0,0 +1,138 @@
1
+ require 'squib/constants'
2
+ require 'squib/conf'
3
+ require 'ostruct'
4
+
5
+ module Squib
6
+ # @api private
7
+ module Args
8
+
9
+ # Intended to be used a a mix-in,
10
+ # For example use see Box as an example
11
+ module ArgLoader
12
+
13
+ # Main class invoked by the client (i.e. api/ methods)
14
+ def load!(args, expand_by: 1, layout: {}, dpi: 300)
15
+ Squib.logger.debug { "ARG LOADER: load! for #{self.class}, args: #{args}" }
16
+ @dpi = dpi
17
+ args[:layout] = prep_layout_args(args[:layout], expand_by: expand_by)
18
+ expand_and_set_and_defaultify(args: args, by: expand_by, layout: layout)
19
+ validate
20
+ convert_units
21
+ self
22
+ end
23
+
24
+ def expand_and_set_and_defaultify(args: {}, by: 1, layout: {})
25
+ attributes = self.class.parameters.keys
26
+ attributes.each do |p|
27
+ args[p] = defaultify(p, args, layout)
28
+ val = if expandable_singleton?(p, args[p])
29
+ [args[p]] * by
30
+ else
31
+ args[p] # not an expanding parameter
32
+ end
33
+ instance_variable_set "@#{p}", val
34
+ end
35
+ self.class.class_eval { attr_reader *(attributes) }
36
+ end
37
+
38
+ # Must be:
39
+ # (a) an expanding parameter, and
40
+ # (b) a singleton already (i.e. doesn't respond to :each)
41
+ def expandable_singleton?(p, arg)
42
+ self.class.expanding_parameters.include?(p) && !arg.respond_to?(:each)
43
+ end
44
+
45
+ # Incorporate defaults and layouts
46
+ # (1) Use whatever is specified if it is
47
+ # (2) Go over all layout specifications (if any) and look them up
48
+ # - Use layout when it's specified for that card
49
+ # - Use "default" if no layout was specified, or the layout itself did not specify
50
+ # Defaut can be overriden for a given dsl method (@dsl_method_defaults)
51
+ # (e.g stroke width is 0.0 for text, non-zero everywhere else)
52
+ #
53
+ def defaultify(p, args, layout)
54
+ return args[p] if args.key? p # arg was specified, no defaults used
55
+ defaults = self.class.parameters.merge(@dsl_method_defaults || {})
56
+ args[:layout].map do |layout_arg|
57
+ return defaults[p] if layout_arg.nil? # no layout specified, use default
58
+ unless layout.key? layout_arg.to_s # specified a layout, but it doesn't exist in layout. Oops!
59
+ Squib.logger.warn("Layout \"#{layout_arg.to_s}\" does not exist in layout file - using default instead")
60
+ return defaults[p]
61
+ end
62
+ if layout[layout_arg.to_s].key?(p.to_s)
63
+ layout[layout_arg.to_s][p.to_s] # param specified in layout
64
+ else
65
+ defaults[p] # layout specified, but not this param
66
+ end
67
+ end
68
+ end
69
+
70
+ # Do singleton expansion on the layout argument as well
71
+ # Treated differently since layout is not always specified
72
+ def prep_layout_args(layout_args, expand_by: 1)
73
+ unless layout_args.respond_to?(:each)
74
+ layout_args = [layout_args] * expand_by
75
+ end
76
+ layout_args || []
77
+ end
78
+
79
+ # For each parameter/attribute foo we try to invoke a validate_foo
80
+ def validate
81
+ self.class.parameters.each do |param, default|
82
+ method = "validate_#{param}"
83
+ if self.respond_to? method
84
+ attribute = "@#{param}"
85
+ val = instance_variable_get(attribute)
86
+ if val.respond_to? :each
87
+ new_val = val.map.with_index{ |v, i| send(method, v, i) }
88
+ instance_variable_set(attribute, new_val)
89
+ else
90
+ instance_variable_set(attribute,send(method, val))
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ # Access an individual arg for a given card
97
+ # @return an OpenStruct that looks just like the mixed-in class
98
+ # @api private
99
+ def [](i)
100
+ card_arg = OpenStruct.new
101
+ self.class.expanding_parameters.each do |p|
102
+ p_val = instance_variable_get("@#{p}")
103
+ card_arg[p] = p_val[i]
104
+ end
105
+ card_arg
106
+ end
107
+
108
+ # Convert units
109
+ def convert_units(dpi: 300)
110
+ self.class.params_with_units.each do |p|
111
+ p_str = "@#{p}"
112
+ p_val = instance_variable_get(p_str)
113
+ if p_val.respond_to? :each
114
+ arr = p_val.map { |x| convert_unit(x, dpi) }
115
+ instance_variable_set p_str, arr
116
+ else
117
+ instance_variable_set p_str, convert_unit(p_val, dpi)
118
+ end
119
+ end
120
+ self
121
+ end
122
+
123
+ def convert_unit(arg, dpi)
124
+ case arg.to_s.rstrip
125
+ when /in$/ #ends with "in"
126
+ arg.rstrip[0..-2].to_f * dpi
127
+ when /cm$/ #ends with "cm"
128
+ arg.rstrip[0..-2].to_f * dpi * INCHES_IN_CM
129
+ else
130
+ arg
131
+ end
132
+ end
133
+ module_function :convert_unit
134
+
135
+ end
136
+
137
+ end
138
+ end
@@ -0,0 +1,55 @@
1
+ require 'squib/args/arg_loader'
2
+
3
+ module Squib
4
+ # @api private
5
+ module Args
6
+
7
+ class Box
8
+ include ArgLoader
9
+
10
+ def initialize(deck = nil, dsl_method_defaults = {})
11
+ @deck = deck
12
+ @dsl_method_defaults = dsl_method_defaults
13
+ end
14
+
15
+ def self.parameters
16
+ { x: 0, y: 0,
17
+ width: :deck, height: :deck,
18
+ radius: nil, x_radius: 0, y_radius: 0
19
+ }
20
+ end
21
+
22
+ def self.expanding_parameters
23
+ parameters.keys # all of them
24
+ end
25
+
26
+ def self.params_with_units
27
+ parameters.keys # all of them
28
+ end
29
+
30
+ def validate_width(arg, _i)
31
+ return arg if @deck.nil?
32
+ return @deck.width if arg == :deck
33
+ arg
34
+ end
35
+
36
+ def validate_height(arg, _i)
37
+ return arg if @deck.nil?
38
+ return @deck.height if arg == :deck
39
+ arg
40
+ end
41
+
42
+ def validate_x_radius(arg, i)
43
+ return radius[i] unless radius[i].nil?
44
+ arg
45
+ end
46
+
47
+ def validate_y_radius(arg, i)
48
+ return radius[i] unless radius[i].nil?
49
+ arg
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end