squib 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +162 -133
- data/Gemfile +4 -4
- data/README.md +630 -550
- data/RELEASE TODO.md +18 -18
- data/Rakefile +99 -99
- data/lib/squib.rb +32 -32
- data/lib/squib/api/background.rb +20 -19
- data/lib/squib/api/data.rb +100 -99
- data/lib/squib/api/image.rb +90 -76
- data/lib/squib/api/save.rb +149 -103
- data/lib/squib/api/settings.rb +35 -37
- data/lib/squib/api/shapes.rb +230 -228
- data/lib/squib/api/text.rb +65 -66
- data/lib/squib/api/text_embed.rb +96 -66
- data/lib/squib/args/arg_loader.rb +138 -0
- data/lib/squib/args/box.rb +55 -0
- data/lib/squib/args/card_range.rb +32 -0
- data/lib/squib/args/color_validator.rb +12 -0
- data/lib/squib/args/coords.rb +33 -0
- data/lib/squib/args/dir_validator.rb +16 -0
- data/lib/squib/args/draw.rb +92 -0
- data/lib/squib/args/embed_adjust.rb +25 -0
- data/lib/squib/args/embed_key.rb +17 -0
- data/lib/squib/args/hand_special.rb +37 -0
- data/lib/squib/args/input_file.rb +37 -0
- data/lib/squib/args/paint.rb +44 -0
- data/lib/squib/args/paragraph.rb +115 -0
- data/lib/squib/args/save_batch.rb +60 -0
- data/lib/squib/args/scale_box.rb +53 -0
- data/lib/squib/args/sheet.rb +72 -0
- data/lib/squib/args/showcase_special.rb +38 -0
- data/lib/squib/args/svg_special.rb +37 -0
- data/lib/squib/args/transform.rb +25 -0
- data/lib/squib/args/typographer.rb +117 -117
- data/lib/squib/card.rb +67 -67
- data/lib/squib/conf.rb +117 -111
- data/lib/squib/constants.rb +178 -178
- data/lib/squib/deck.rb +113 -111
- data/lib/squib/graphics/cairo_context_wrapper.rb +99 -53
- data/lib/squib/graphics/gradient_regex.rb +46 -46
- data/lib/squib/graphics/hand.rb +42 -43
- data/lib/squib/graphics/image.rb +76 -73
- data/lib/squib/graphics/save_doc.rb +103 -137
- data/lib/squib/graphics/save_images.rb +33 -33
- data/lib/squib/graphics/shapes.rb +119 -152
- data/lib/squib/graphics/showcase.rb +85 -88
- data/lib/squib/graphics/text.rb +176 -216
- data/lib/squib/layout_parser.rb +91 -89
- data/lib/squib/layouts/economy.yml +85 -0
- data/lib/squib/layouts/fantasy.yml +101 -0
- data/lib/squib/layouts/hand.yml +62 -46
- data/lib/squib/layouts/playing-card.yml +35 -18
- data/lib/squib/project_template/config.yml +45 -40
- data/lib/squib/version.rb +10 -10
- data/samples/color_shortcuts.rb +6 -0
- data/samples/csv_import.rb +18 -18
- data/samples/custom-config.yml +5 -5
- data/samples/custom_config.rb +18 -18
- data/samples/draw_shapes.rb +45 -35
- data/samples/embed_text.rb +88 -90
- data/samples/hand.rb +24 -24
- data/samples/layouts.rb +62 -61
- data/samples/layouts_builtin.rb +51 -0
- data/samples/load_images.rb +78 -64
- data/samples/ranges.rb +64 -53
- data/samples/sample.csv +2 -2
- data/samples/text_options.rb +102 -94
- data/spec/api/api_data_spec.rb +57 -50
- data/spec/api/api_settings_spec.rb +37 -17
- data/spec/args/box_spec.rb +127 -0
- data/spec/args/draw_spec.rb +95 -0
- data/spec/args/embed_key_spec.rb +13 -0
- data/spec/args/input_file_spec.rb +21 -0
- data/spec/args/paint_spec.rb +22 -0
- data/spec/args/paragraph_spec.rb +153 -0
- data/spec/args/range_spec.rb +36 -0
- data/spec/args/save_batch_spec.rb +51 -0
- data/spec/args/scale_box_spec.rb +71 -0
- data/spec/args/sheet_spec.rb +58 -0
- data/spec/args/showcase_special_spec.rb +15 -0
- data/spec/data/samples/autoscale_font.rb.txt +84 -87
- data/spec/data/samples/basic.rb.txt +209 -203
- data/spec/data/samples/cairo_access.rb.txt +2 -2
- data/spec/data/samples/config_text_markup.rb.txt +72 -75
- data/spec/data/samples/csv_import.rb.txt +76 -80
- data/spec/data/samples/custom_config.rb.txt +48 -49
- data/spec/data/samples/draw_shapes.rb.txt +100 -42
- data/spec/data/samples/embed_text.rb.txt +283 -295
- data/spec/data/samples/excel.rb.txt +162 -171
- data/spec/data/samples/gradients.rb.txt +79 -67
- data/spec/data/samples/hand.rb.txt +538 -514
- data/spec/data/samples/hello_world.rb.txt +36 -38
- data/spec/data/samples/load_images.rb.txt +41 -5
- data/spec/data/samples/portrait-landscape.rb.txt +49 -51
- data/spec/data/samples/ranges.rb.txt +460 -429
- data/spec/data/samples/saves.rb.txt +801 -785
- data/spec/data/samples/showcase.rb.txt +5910 -5906
- data/spec/data/samples/text_options.rb.txt +1125 -981
- data/spec/data/samples/tgc_proofs.rb.txt +81 -79
- data/spec/data/samples/units.rb.txt +18 -12
- data/spec/data/xlsx/with_macros.xlsm +0 -0
- data/spec/graphics/cairo_context_wrapper_spec.rb +84 -75
- data/spec/graphics/graphics_images_spec.rb +94 -85
- data/spec/graphics/graphics_save_doc_spec.rb +67 -65
- data/spec/samples/expected/hand.png +0 -0
- data/spec/samples/expected/hand_pretty.png +0 -0
- data/spec/samples/expected/layout_00.png +0 -0
- data/spec/samples/expected/load_images_00.png +0 -0
- data/spec/samples/expected/ranges_00.png +0 -0
- data/spec/samples/expected/shape_00.png +0 -0
- data/spec/samples/expected/showcase.png +0 -0
- data/spec/samples/expected/showcase2.png +0 -0
- data/spec/samples/expected/showcase_individual_00.png +0 -0
- data/spec/samples/expected/showcase_individual_01.png +0 -0
- data/spec/samples/expected/showcase_individual_02.png +0 -0
- data/spec/samples/expected/showcase_individual_03.png +0 -0
- data/spec/samples/expected/text_00.png +0 -0
- data/spec/samples/expected/text_01.png +0 -0
- data/spec/samples/expected/text_02.png +0 -0
- data/spec/samples/samples_regression_spec.rb +82 -82
- data/spec/spec_helper.rb +3 -2
- data/squib.gemspec +48 -48
- data/squib.sublime-project +42 -36
- metadata +61 -33
- data/lib/squib/input_helpers.rb +0 -238
- data/spec/api/api_image_spec.rb +0 -38
- data/spec/api/api_text_spec.rb +0 -37
- data/spec/graphics/graphics_shapes_spec.rb +0 -85
- data/spec/graphics/graphics_text_spec.rb +0 -164
- data/spec/input_helpers_spec.rb +0 -238
- data/spec/samples/expected/embed_multi_00.png +0 -0
- data/spec/samples/expected/embed_multi_01.png +0 -0
- data/spec/samples/expected/embed_multi_02.png +0 -0
- data/spec/samples/expected/ranges_01.png +0 -0
- data/spec/samples/expected/ranges_02.png +0 -0
data/lib/squib/api/text.rb
CHANGED
@@ -1,66 +1,65 @@
|
|
1
|
-
require 'squib/api/text_embed'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# @option opts
|
29
|
-
# @option opts
|
30
|
-
# @option opts
|
31
|
-
# @option opts
|
32
|
-
# @option opts
|
33
|
-
#
|
34
|
-
# @option opts
|
35
|
-
# @option opts
|
36
|
-
# @option opts
|
37
|
-
#
|
38
|
-
# @option opts
|
39
|
-
# @option opts
|
40
|
-
# @option opts
|
41
|
-
# @option opts
|
42
|
-
# @option opts
|
43
|
-
# @
|
44
|
-
# @
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
opts
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
|
data/lib/squib/api/text_embed.rb
CHANGED
@@ -1,66 +1,96 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#
|
16
|
-
# @
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
#
|
27
|
-
#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
#
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|