squib 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitmodules +14 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +201 -175
- data/Gemfile +2 -4
- data/README.md +650 -645
- data/RELEASE TODO.md +18 -18
- data/Rakefile +106 -99
- data/appveyor.yml +29 -0
- data/lib/squib.rb +32 -32
- data/lib/squib/api/background.rb +20 -20
- data/lib/squib/api/data.rb +131 -131
- data/lib/squib/api/image.rb +108 -90
- data/lib/squib/api/save.rb +151 -149
- data/lib/squib/api/settings.rb +35 -35
- data/lib/squib/api/shapes.rb +255 -230
- data/lib/squib/api/text.rb +65 -65
- data/lib/squib/api/text_embed.rb +96 -96
- data/lib/squib/args/arg_loader.rb +138 -138
- data/lib/squib/args/box.rb +54 -54
- data/lib/squib/args/card_range.rb +32 -32
- data/lib/squib/args/color_validator.rb +11 -11
- data/lib/squib/args/coords.rb +32 -32
- data/lib/squib/args/dir_validator.rb +16 -16
- data/lib/squib/args/draw.rb +92 -92
- data/lib/squib/args/embed_adjust.rb +25 -25
- data/lib/squib/args/embed_key.rb +17 -17
- data/lib/squib/args/hand_special.rb +37 -37
- data/lib/squib/args/import.rb +39 -39
- data/lib/squib/args/input_file.rb +37 -37
- data/lib/squib/args/paint.rb +43 -43
- data/lib/squib/args/paragraph.rb +116 -115
- data/lib/squib/args/save_batch.rb +63 -60
- data/lib/squib/args/scale_box.rb +53 -53
- data/lib/squib/args/sheet.rb +72 -72
- data/lib/squib/args/showcase_special.rb +38 -38
- data/lib/squib/args/svg_special.rb +37 -37
- data/lib/squib/args/transform.rb +60 -24
- data/lib/squib/args/typographer.rb +117 -117
- data/lib/squib/card.rb +66 -67
- data/lib/squib/conf.rb +131 -117
- data/lib/squib/constants.rb +12 -178
- data/lib/squib/deck.rb +113 -113
- data/lib/squib/graphics/cairo_context_wrapper.rb +113 -99
- data/lib/squib/graphics/gradient_regex.rb +46 -46
- data/lib/squib/graphics/hand.rb +42 -42
- data/lib/squib/graphics/image.rb +103 -76
- data/lib/squib/graphics/save_doc.rb +103 -103
- data/lib/squib/graphics/save_images.rb +39 -33
- data/lib/squib/graphics/shapes.rb +135 -119
- data/lib/squib/graphics/showcase.rb +85 -85
- data/lib/squib/graphics/text.rb +176 -176
- data/lib/squib/layout_parser.rb +91 -91
- data/lib/squib/layouts/economy.yml +85 -85
- data/lib/squib/layouts/fantasy.yml +101 -101
- data/lib/squib/layouts/hand.yml +62 -62
- data/lib/squib/layouts/playing-card.yml +35 -35
- data/lib/squib/layouts/tuck_box.yml +45 -45
- data/lib/squib/project_template/IDEAS.md +22 -0
- data/lib/squib/project_template/PLAYTESTING.md +26 -0
- data/lib/squib/project_template/RULES.md +21 -0
- data/lib/squib/project_template/config.yml +49 -45
- data/lib/squib/sample_helpers.rb +34 -0
- data/lib/squib/version.rb +10 -10
- data/samples/autoscale_font/_autoscale_font.rb +29 -0
- data/samples/color_shortcuts.rb +6 -6
- data/samples/csv_import.rb +26 -26
- data/samples/custom-config.yml +5 -5
- data/samples/custom_config.rb +18 -18
- data/samples/draw_shapes.rb +48 -45
- data/samples/embed_text.rb +88 -88
- data/samples/excel.rb +55 -55
- data/samples/hand.rb +24 -24
- data/samples/images/_images.rb +104 -0
- data/samples/intro/01_hello.rb +9 -0
- data/samples/intro/02_options.rb +15 -0
- data/samples/intro/03_layout.rb +12 -0
- data/samples/intro/04_arrays.rb +16 -0
- data/samples/intro/05_excel.rb +15 -0
- data/samples/layouts.rb +62 -62
- data/samples/layouts_builtin.rb +51 -51
- data/samples/load_images.rb +99 -78
- data/samples/load_images_config.yml +1 -0
- data/samples/quantity_explosion.csv +2 -2
- data/samples/ranges.rb +64 -64
- data/samples/sample.csv +2 -2
- data/samples/saves.rb +9 -1
- data/samples/sprites.png +0 -0
- data/samples/text/_text.rb +46 -0
- data/samples/text_options.rb +102 -102
- data/spec/api/api_data_spec.rb +117 -117
- data/spec/api/api_settings_spec.rb +37 -37
- data/spec/args/box_spec.rb +127 -127
- data/spec/args/draw_spec.rb +101 -95
- data/spec/args/embed_key_spec.rb +13 -13
- data/spec/args/input_file_spec.rb +21 -21
- data/spec/args/paint_spec.rb +21 -21
- data/spec/args/paragraph_spec.rb +152 -152
- data/spec/args/range_spec.rb +40 -40
- data/spec/args/save_batch_spec.rb +51 -51
- data/spec/args/scale_box_spec.rb +71 -71
- data/spec/args/sheet_spec.rb +58 -58
- data/spec/args/showcase_special_spec.rb +15 -15
- data/spec/args/transform_spec.rb +25 -0
- data/spec/card_spec.rb +11 -0
- data/spec/conf_spec.rb +13 -3
- data/spec/data/conf/unrecognized.yml +4 -0
- data/spec/data/csv/qty.csv +2 -2
- data/spec/data/csv/qty_named.csv +2 -2
- data/spec/data/csv/with_spaces.csv +2 -2
- data/spec/data/samples/autoscale_font.rb.txt +84 -84
- data/spec/data/samples/basic.rb.txt +227 -209
- data/spec/data/samples/config_text_markup.rb.txt +72 -72
- data/spec/data/samples/csv_import.rb.txt +213 -213
- data/spec/data/samples/custom_config.rb.txt +57 -48
- data/spec/data/samples/draw_shapes.rb.txt +555 -3
- data/spec/data/samples/embed_text.rb.txt +283 -283
- data/spec/data/samples/excel.rb.txt +661 -661
- data/spec/data/samples/gradients.rb.txt +77 -79
- data/spec/data/samples/hand.rb.txt +538 -538
- data/spec/data/samples/hello_world.rb.txt +36 -36
- data/spec/data/samples/load_images.rb.txt +170 -0
- data/spec/data/samples/portrait-landscape.rb.txt +51 -49
- data/spec/data/samples/ranges.rb.txt +472 -460
- data/spec/data/samples/saves.rb.txt +810 -801
- data/spec/data/samples/showcase.rb.txt +5926 -5910
- data/spec/data/samples/text_options.rb.txt +1125 -1125
- data/spec/data/samples/tgc_proofs.rb.txt +95 -81
- data/spec/graphics/cairo_context_wrapper_spec.rb +104 -84
- data/spec/graphics/graphics_save_doc_spec.rb +67 -67
- data/spec/samples/diff-with-css.example.html +39 -0
- data/spec/samples/expected/load_images_00.png +0 -0
- data/spec/samples/expected/shape_00.png +0 -0
- data/spec/samples/run_samples_spec.rb +17 -0
- data/spec/samples/samples_regression_spec.rb +72 -82
- data/spec/spec_helper.rb +9 -1
- data/squib.gemspec +49 -48
- data/squib.sublime-project +42 -42
- metadata +94 -48
- data/spec/graphics/graphics_images_spec.rb +0 -94
@@ -1,99 +1,113 @@
|
|
1
|
-
require 'forwardable'
|
2
|
-
require 'squib/graphics/gradient_regex'
|
3
|
-
|
4
|
-
module Squib
|
5
|
-
module Graphics
|
6
|
-
# Wrapper class for the Cairo context. Private.
|
7
|
-
# @api private
|
8
|
-
class CairoContextWrapper
|
9
|
-
extend Forwardable
|
10
|
-
|
11
|
-
# :nodoc:
|
12
|
-
# @api private
|
13
|
-
attr_accessor :cairo_cxt
|
14
|
-
|
15
|
-
# :nodoc:
|
16
|
-
# @api private
|
17
|
-
def initialize(cairo_cxt)
|
18
|
-
@cairo_cxt = cairo_cxt
|
19
|
-
end
|
20
|
-
|
21
|
-
def_delegators :cairo_cxt, :save, :set_source_color, :paint, :restore,
|
22
|
-
:translate, :rotate, :move_to, :update_pango_layout, :width, :height,
|
23
|
-
:show_pango_layout, :rounded_rectangle, :set_line_width, :stroke, :fill,
|
24
|
-
:set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to,
|
25
|
-
:operator=, :show_page, :clip, :transform, :mask, :create_pango_layout,
|
26
|
-
:antialias=, :curve_to, :matrix, :matrix=, :identity_matrix, :pango_layout_path,
|
27
|
-
:stroke_preserve, :target, :new_path, :fill_preserve, :close_path,
|
28
|
-
:set_line_join, :set_line_cap, :set_dash
|
29
|
-
|
30
|
-
# :nodoc:
|
31
|
-
# @api private
|
32
|
-
def set_source_squibcolor(arg)
|
33
|
-
raise 'nil is not a valid color' if arg.nil?
|
34
|
-
if match = arg.match(LINEAR_GRADIENT)
|
35
|
-
x1, y1, x2, y2 = match.captures
|
36
|
-
linear = Cairo::LinearPattern.new(x1.to_f, y1.to_f, x2.to_f, y2.to_f)
|
37
|
-
arg.scan(STOPS).each do |color, offset|
|
38
|
-
linear.add_color_stop(offset.to_f, color)
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
@cairo_cxt.
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
set_source_squibcolor draw.
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
translate(
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
1
|
+
require 'forwardable'
|
2
|
+
require 'squib/graphics/gradient_regex'
|
3
|
+
|
4
|
+
module Squib
|
5
|
+
module Graphics
|
6
|
+
# Wrapper class for the Cairo context. Private.
|
7
|
+
# @api private
|
8
|
+
class CairoContextWrapper
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
# :nodoc:
|
12
|
+
# @api private
|
13
|
+
attr_accessor :cairo_cxt
|
14
|
+
|
15
|
+
# :nodoc:
|
16
|
+
# @api private
|
17
|
+
def initialize(cairo_cxt)
|
18
|
+
@cairo_cxt = cairo_cxt
|
19
|
+
end
|
20
|
+
|
21
|
+
def_delegators :cairo_cxt, :save, :set_source_color, :paint, :restore,
|
22
|
+
:translate, :rotate, :move_to, :update_pango_layout, :width, :height,
|
23
|
+
:show_pango_layout, :rounded_rectangle, :set_line_width, :stroke, :fill,
|
24
|
+
:set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to,
|
25
|
+
:operator=, :show_page, :clip, :transform, :mask, :create_pango_layout,
|
26
|
+
:antialias=, :curve_to, :matrix, :matrix=, :identity_matrix, :pango_layout_path,
|
27
|
+
:stroke_preserve, :target, :new_path, :fill_preserve, :close_path,
|
28
|
+
:set_line_join, :set_line_cap, :set_dash
|
29
|
+
|
30
|
+
# :nodoc:
|
31
|
+
# @api private
|
32
|
+
def set_source_squibcolor(arg)
|
33
|
+
raise 'nil is not a valid color' if arg.nil?
|
34
|
+
if match = arg.match(LINEAR_GRADIENT)
|
35
|
+
x1, y1, x2, y2 = match.captures
|
36
|
+
linear = Cairo::LinearPattern.new(x1.to_f, y1.to_f, x2.to_f, y2.to_f)
|
37
|
+
arg.scan(STOPS).each do |color, offset|
|
38
|
+
linear.add_color_stop(offset.to_f, color)
|
39
|
+
end
|
40
|
+
linear.matrix = matrix # match the coordinate systems - see bug 127
|
41
|
+
@cairo_cxt.set_source(linear)
|
42
|
+
elsif match = arg.match(RADIAL_GRADIENT)
|
43
|
+
x1, y1, r1, x2, y2, r2 = match.captures
|
44
|
+
radial = Cairo::RadialPattern.new(x1.to_f, y1.to_f, r1.to_f,
|
45
|
+
x2.to_f, y2.to_f, r2.to_f)
|
46
|
+
radial.matrix = matrix # match the coordinate systems - see bug 127
|
47
|
+
arg.scan(STOPS).each do |color, offset|
|
48
|
+
radial.add_color_stop(offset.to_f, color)
|
49
|
+
end
|
50
|
+
@cairo_cxt.set_source(radial)
|
51
|
+
else
|
52
|
+
@cairo_cxt.set_source_color(arg)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Convenience method for a common task
|
57
|
+
# @api private
|
58
|
+
def fill_n_stroke(draw)
|
59
|
+
return stroke_n_fill(draw) if draw.stroke_strategy == :stroke_first
|
60
|
+
set_source_squibcolor draw.fill_color
|
61
|
+
fill_preserve
|
62
|
+
set_source_squibcolor draw.stroke_color
|
63
|
+
set_line_width draw.stroke_width
|
64
|
+
set_line_join draw.join
|
65
|
+
set_line_cap draw.cap
|
66
|
+
set_dash draw.dash
|
67
|
+
stroke
|
68
|
+
end
|
69
|
+
|
70
|
+
def stroke_n_fill(draw)
|
71
|
+
return fill_n_stroke(draw) if draw.stroke_strategy == :fill_first
|
72
|
+
set_source_squibcolor draw.stroke_color
|
73
|
+
set_line_width draw.stroke_width
|
74
|
+
set_line_join draw.join
|
75
|
+
set_line_cap draw.cap
|
76
|
+
set_dash draw.dash
|
77
|
+
stroke_preserve
|
78
|
+
set_source_squibcolor draw.fill_color
|
79
|
+
fill
|
80
|
+
end
|
81
|
+
|
82
|
+
# Convenience method for a common task
|
83
|
+
# @api private
|
84
|
+
def fancy_stroke(draw)
|
85
|
+
set_source_squibcolor draw.stroke_color
|
86
|
+
set_line_width draw.stroke_width
|
87
|
+
set_line_join draw.join
|
88
|
+
set_line_cap draw.cap
|
89
|
+
set_dash draw.dash
|
90
|
+
stroke
|
91
|
+
end
|
92
|
+
|
93
|
+
def rotate_about(x, y, angle)
|
94
|
+
translate(x, y)
|
95
|
+
rotate(angle)
|
96
|
+
translate(-x, -y)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Flip either vertical or horizontal depending
|
100
|
+
# From the cairo website: http://cairographics.org/matrix_transform/
|
101
|
+
# cairo.Matrix(fx, 0, 0,
|
102
|
+
# fy, cx*(1-fx), cy*(fy-1))
|
103
|
+
# fx/fy = 1 means 'no flip', fx/fy = -1 are used for horizontal/vertical flip
|
104
|
+
def flip(vertical, horizontal, x, y)
|
105
|
+
v = vertical ? -1.0 : 1.0
|
106
|
+
h = horizontal ? -1.0 : 1.0
|
107
|
+
transform Cairo::Matrix.new(v, 0.0, 0.0,
|
108
|
+
h, x*(1-v), y*(1-h))
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -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
|
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
47
|
end
|
data/lib/squib/graphics/hand.rb
CHANGED
@@ -1,42 +1,42 @@
|
|
1
|
-
require 'squib/graphics/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 'squib/graphics/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
|
data/lib/squib/graphics/image.rb
CHANGED
@@ -1,76 +1,103 @@
|
|
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] || @img_cache[file] = Cairo::ImageSurface.from_png(file)
|
10
|
-
end
|
11
|
-
module_function :cache_load_image
|
12
|
-
|
13
|
-
class Card
|
14
|
-
|
15
|
-
# :nodoc:
|
16
|
-
# @api private
|
17
|
-
def png(file, box, paint, trans)
|
18
|
-
Squib.logger.debug {"RENDERING PNG: \n file: #{file}\n box: #{box}\n paint: #{paint}\n trans: #{trans}"}
|
19
|
-
return if file.nil? or file.eql? ''
|
20
|
-
png = Squib.cache_load_image(file)
|
21
|
-
use_cairo do |cc|
|
22
|
-
cc.translate(box.x, box.y)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
cc.
|
32
|
-
|
33
|
-
cc.
|
34
|
-
cc.
|
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
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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] || @img_cache[file] = Cairo::ImageSurface.from_png(file)
|
10
|
+
end
|
11
|
+
module_function :cache_load_image
|
12
|
+
|
13
|
+
class Card
|
14
|
+
|
15
|
+
# :nodoc:
|
16
|
+
# @api private
|
17
|
+
def png(file, box, paint, trans)
|
18
|
+
Squib.logger.debug {"RENDERING PNG: \n file: #{file}\n box: #{box}\n paint: #{paint}\n trans: #{trans}"}
|
19
|
+
return if file.nil? or file.eql? ''
|
20
|
+
png = Squib.cache_load_image(file)
|
21
|
+
use_cairo do |cc|
|
22
|
+
cc.translate(box.x, box.y)
|
23
|
+
box.width = png.width.to_f if box.width == :native
|
24
|
+
box.height = png.height.to_f if box.height == :native
|
25
|
+
box.width = png.width.to_f * box.height.to_f / png.height.to_f if box.width == :scale
|
26
|
+
box.height = png.height.to_f * box.width.to_f / png.width.to_f if box.height == :scale
|
27
|
+
|
28
|
+
scale_width = box.width.to_f / png.width.to_f
|
29
|
+
scale_height = box.height.to_f / png.height.to_f
|
30
|
+
warn_png_scale(file, scale_width, scale_height)
|
31
|
+
cc.scale(scale_width, scale_height)
|
32
|
+
|
33
|
+
cc.rotate(trans.angle)
|
34
|
+
cc.flip(trans.flip_vertical, trans.flip_horizontal, box.width / 2, box.height / 2)
|
35
|
+
cc.translate(-box.x, -box.y)
|
36
|
+
|
37
|
+
trans.crop_width = png.width.to_f if trans.crop_width == :native
|
38
|
+
trans.crop_height = png.height.to_f if trans.crop_height == :native
|
39
|
+
cc.rounded_rectangle(box.x, box.y, trans.crop_width, trans.crop_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius)
|
40
|
+
cc.clip
|
41
|
+
cc.translate(-trans.crop_x, -trans.crop_y)
|
42
|
+
|
43
|
+
|
44
|
+
cc.set_source(png, box.x, box.y)
|
45
|
+
cc.operator = paint.blend unless paint.blend == :none
|
46
|
+
if paint.mask.empty?
|
47
|
+
cc.paint(paint.alpha)
|
48
|
+
else
|
49
|
+
cc.set_source_squibcolor(paint.mask)
|
50
|
+
cc.mask(png, box.x, box.y)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# :nodoc:
|
56
|
+
# @api private
|
57
|
+
def warn_png_scale(file, scale_width, scale_height)
|
58
|
+
if @deck.conf.warn_png_scale? && (scale_width > 1.0 || scale_height > 1.0)
|
59
|
+
Squib.logger.warn "PNG is being upscaled - antialiasing could result: #{file}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# :nodoc:
|
64
|
+
# @api private
|
65
|
+
def svg(file, svg_args, box, paint, trans)
|
66
|
+
Squib.logger.debug {"Rendering: #{file}, id: #{id} @#{x},#{y} #{width}x#{height}, alpha: #{alpha}, blend: #{blend}, angle: #{angle}, mask: #{mask}"}
|
67
|
+
Squib.logger.warn 'Both an SVG file and SVG data were specified' unless file.to_s.empty? || svg_args.data.to_s.empty?
|
68
|
+
return if (file.nil? or file.eql? '') and svg_args.data.nil? # nothing specified TODO Move this out to arg validator
|
69
|
+
svg_args.data = File.read(file) if svg_args.data.to_s.empty?
|
70
|
+
svg = RSVG::Handle.new_from_data(svg_args.data)
|
71
|
+
box.width = svg.width if box.width == :native
|
72
|
+
box.height = svg.height if box.height == :native
|
73
|
+
box.width = svg.width.to_f * box.height.to_f / svg.height.to_f if box.width == :scale
|
74
|
+
box.height = svg.height.to_f * box.width.to_f / svg.width.to_f if box.height == :scale
|
75
|
+
scale_width = box.width.to_f / svg.width.to_f
|
76
|
+
scale_height = box.height.to_f / svg.height.to_f
|
77
|
+
use_cairo do |cc|
|
78
|
+
cc.translate(box.x, box.y)
|
79
|
+
cc.flip(trans.flip_vertical, trans.flip_horizontal, box.width / 2, box.height / 2)
|
80
|
+
cc.rotate(trans.angle)
|
81
|
+
cc.scale(scale_width, scale_height)
|
82
|
+
|
83
|
+
trans.crop_width = box.width if trans.crop_width == :native
|
84
|
+
trans.crop_height = box.height if trans.crop_height == :native
|
85
|
+
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)
|
86
|
+
cc.clip
|
87
|
+
cc.translate(-trans.crop_x, -trans.crop_y)
|
88
|
+
|
89
|
+
cc.operator = paint.blend unless paint.blend == :none
|
90
|
+
if paint.mask.to_s.empty?
|
91
|
+
cc.render_rsvg_handle(svg, svg_args.id)
|
92
|
+
else
|
93
|
+
tmp = Cairo::ImageSurface.new(box.width / scale_width, box.height / scale_height)
|
94
|
+
tmp_cc = Cairo::Context.new(tmp)
|
95
|
+
tmp_cc.render_rsvg_handle(svg, svg_args.id)
|
96
|
+
cc.set_source_squibcolor(paint.mask)
|
97
|
+
cc.mask(tmp, 0, 0)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|