squib 0.0.1 → 0.0.2
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/.gitignore +29 -26
- data/.travis.yml +6 -4
- data/.yardopts +6 -0
- data/API.md +59 -0
- data/Gemfile +2 -2
- data/LICENSE.txt +22 -22
- data/README.md +92 -55
- data/Rakefile +11 -6
- data/bin/squib +19 -2
- data/lib/squib/api/background.rb +16 -11
- data/lib/squib/api/data.rb +52 -29
- data/lib/squib/api/image.rb +48 -10
- data/lib/squib/api/save.rb +36 -15
- data/lib/squib/api/settings.rb +35 -0
- data/lib/squib/api/shapes.rb +106 -12
- data/lib/squib/api/text.rb +45 -25
- data/lib/squib/api/units.rb +17 -0
- data/lib/squib/card.rb +36 -27
- data/lib/squib/commands/new.rb +40 -0
- data/lib/squib/constants.rb +40 -0
- data/lib/squib/deck.rb +113 -48
- data/lib/squib/graphics/background.rb +12 -10
- data/lib/squib/graphics/image.rb +28 -12
- data/lib/squib/graphics/save_doc.rb +50 -21
- data/lib/squib/graphics/save_images.rb +10 -8
- data/lib/squib/graphics/shapes.rb +55 -11
- data/lib/squib/graphics/text.rb +107 -27
- data/lib/squib/input_helpers.rb +140 -0
- data/lib/squib/project_template/.gitignore +3 -0
- data/lib/squib/project_template/ABOUT.md +19 -0
- data/lib/squib/project_template/Gemfile +3 -0
- data/lib/squib/project_template/PNP NOTES.md +4 -0
- data/lib/squib/project_template/_output/gitkeep.txt +1 -0
- data/lib/squib/project_template/config.yml +2 -0
- data/lib/squib/project_template/deck.rb +6 -0
- data/lib/squib/project_template/layout.yml +0 -0
- data/lib/squib/version.rb +6 -3
- data/lib/squib.rb +19 -2
- data/samples/_output/gitkeep.txt +0 -0
- data/samples/basic.rb +20 -22
- data/samples/colors.rb +17 -0
- data/samples/custom-config.yml +1 -0
- data/samples/custom-layout.yml +34 -0
- data/samples/custom_config.rb +6 -0
- data/samples/draw_shapes.rb +19 -0
- data/samples/excel.rb +19 -16
- data/samples/hello_world.rb +7 -0
- data/samples/load_images.rb +19 -0
- data/samples/pokercard.png +0 -0
- data/samples/ranges.rb +47 -0
- data/samples/sample.xlsx +0 -0
- data/samples/save_pdf.rb +14 -0
- data/samples/spanner.svg +91 -0
- data/samples/text_options.rb +58 -0
- data/samples/tgc_proofs.rb +20 -0
- data/samples/units.rb +12 -0
- data/samples/use_layout.rb +29 -0
- data/spec/api/api_text_spec.rb +37 -0
- data/spec/commands/new_spec.rb +48 -0
- data/spec/deck_spec.rb +49 -48
- data/spec/samples_run_spec.rb +15 -0
- data/spec/spec_helper.rb +9 -0
- data/squib.gemspec +42 -27
- metadata +109 -11
- data/samples/_output/watch.html +0 -25
@@ -0,0 +1,35 @@
|
|
1
|
+
module Squib
|
2
|
+
class Deck
|
3
|
+
|
4
|
+
# Toggle hints globally.
|
5
|
+
#
|
6
|
+
# Text hints are rectangles around where the text will be laid out. They are intended to be temporary.
|
7
|
+
# Setting a hint to nil or to :off will disable hints. @see samples/text.rb
|
8
|
+
# @example
|
9
|
+
# hint text:cyan
|
10
|
+
#
|
11
|
+
# @param [String] text the color of the text hint. To turn off use nil or :off. @see API.md
|
12
|
+
# @return [nil] Returns nothing
|
13
|
+
# @api public
|
14
|
+
def hint(text: nil)
|
15
|
+
text = nil if text == :off
|
16
|
+
@text_hint = text
|
17
|
+
end
|
18
|
+
|
19
|
+
# Sets various defaults for this deck. Defaults can be overriden by the commands themselves
|
20
|
+
# @example
|
21
|
+
# set font: 'Arial 26'
|
22
|
+
# text 'blah' # in Arial 26
|
23
|
+
# text 'blah24', font: 'Arial 24' # in Arial 24
|
24
|
+
# set font: :default # Back to Squib-wide default
|
25
|
+
#
|
26
|
+
# @option opts font: the font string to set as default. Can also be set to `:default` to use the Squib-wide default.
|
27
|
+
# @return [nil] Returns nothing
|
28
|
+
# @api public
|
29
|
+
def set(opts = {})
|
30
|
+
opts = needs(opts, [:font])
|
31
|
+
@font = opts[:font]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
data/lib/squib/api/shapes.rb
CHANGED
@@ -1,13 +1,107 @@
|
|
1
|
-
module Squib
|
2
|
-
class Deck
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module Squib
|
2
|
+
class Deck
|
3
|
+
|
4
|
+
# Draw a rounded rectangle
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# rect x: 0, y: 0, width: 825, height: 1125, radius: 25
|
8
|
+
#
|
9
|
+
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
|
10
|
+
# @option opts x [Integer] (0) the x-coordinate to place
|
11
|
+
# @option opts y [Integer] (0) the y-coordinate to place
|
12
|
+
# @option opts width [Integer] the width of the rectangle.
|
13
|
+
# @option opts height [Integer] the height of the rectangle.
|
14
|
+
# @option opts x_radius [Integer] (0) the radius of the rounded corner horiztonally. Zero is a non-rounded corner.
|
15
|
+
# @option opts y_radius [Integer] (0) the radius of the rounded corner vertically. Zero is a non-rounded corner.
|
16
|
+
# @option opts radius [Integer] (nil) when set, overrides both x_radius and y_radius
|
17
|
+
# @option opts fill_color [String] ('#0000') the color with which to fill the rectangle
|
18
|
+
# @option opts stroke_color [String] (:black) the color with which to stroke the outside of the rectangle
|
19
|
+
# @option opts stroke_width [Decimal] (2.0) the width of the outside stroke
|
20
|
+
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:API.md#label-Custom+Layouts Custom Layouts}
|
21
|
+
# @return [nil] intended to be void
|
22
|
+
# @api public
|
23
|
+
def rect(opts = {})
|
24
|
+
opts = needs(opts, [:range, :x, :y, :width, :height, :radius,
|
25
|
+
:fill_color, :stroke_color, :stroke_width, :layout])
|
26
|
+
opts[:range].each do |i|
|
27
|
+
@cards[i].rect(opts[:x], opts[:y], opts[:width], opts[:height],
|
28
|
+
opts[:x_radius], opts[:y_radius],
|
29
|
+
opts[:fill_color], opts[:stroke_color], opts[:stroke_width])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Draw a circle centered at the given coordinates
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# circle x: 0, y: 0, radius: 100
|
37
|
+
#
|
38
|
+
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
|
39
|
+
# @option opts x [Integer] (0) the x-coordinate to place
|
40
|
+
# @option opts y [Integer] (0) the y-coordinate to place
|
41
|
+
# @option opts radius [Integer] (100) radius of the circle
|
42
|
+
# @option opts fill_color [String] ('#0000') the color with which to fill the rectangle
|
43
|
+
# @option opts stroke_color [String] (:black) the color with which to stroke the outside of the rectangle
|
44
|
+
# @option opts stroke_width [Decimal] (2.0) the width of the outside stroke
|
45
|
+
# @return [nil] intended to be void
|
46
|
+
# @api public
|
47
|
+
def circle(opts = {})
|
48
|
+
opts = {radius: 100}.merge(opts)
|
49
|
+
opts = needs(opts, [:range, :x, :y, :circle_radius, :layout,
|
50
|
+
:fill_color, :stroke_color, :stroke_width])
|
51
|
+
opts[:range].each do |i|
|
52
|
+
@cards[i].circle(opts[:x], opts[:y], opts[:radius],
|
53
|
+
opts[:fill_color], opts[:stroke_color], opts[:stroke_width])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Draw a triangle using the given coordinates
|
58
|
+
#
|
59
|
+
# @example
|
60
|
+
# triangle :x1 => 0, :y1 => 0, :x2 => 50, :y2 => 50, :x3 => 0, :y3 => 50
|
61
|
+
#
|
62
|
+
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
|
63
|
+
# @option opts x1 [Integer] (0) the x-coordinate to place
|
64
|
+
# @option opts y1 [Integer] (0) the y-coordinate to place
|
65
|
+
# @option opts x2 [Integer] (50) the x-coordinate to place
|
66
|
+
# @option opts y2 [Integer] (50) the y-coordinate to place
|
67
|
+
# @option opts x3 [Integer] (0) the x-coordinate to place
|
68
|
+
# @option opts y3 [Integer] (50) the y-coordinate to place
|
69
|
+
# @option opts fill_color [String] ('#0000') the color with which to fill the triangle
|
70
|
+
# @option opts stroke_color [String] (:black) the color with which to stroke the outside of the triangle
|
71
|
+
# @option opts stroke_width [Decimal] (2.0) the width of the outside stroke
|
72
|
+
# @return [nil] intended to be void
|
73
|
+
# @api public
|
74
|
+
def triangle(opts = {})
|
75
|
+
opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :x3, :y3, :layout,
|
76
|
+
:fill_color, :stroke_color, :stroke_width])
|
77
|
+
opts[:range].each do |i|
|
78
|
+
@cards[i].triangle(opts[:x1], opts[:y1], opts[:x2], opts[:y2],opts[:x3], opts[:y3],
|
79
|
+
opts[:fill_color], opts[:stroke_color], opts[:stroke_width])
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Draw a line using the given coordinates
|
84
|
+
#
|
85
|
+
# @example
|
86
|
+
# triangle :x1 => 0, :y1 => 0, :x2 => 50, :y2 => 50
|
87
|
+
#
|
88
|
+
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
|
89
|
+
# @option opts x1 [Integer] (0) the x-coordinate to place
|
90
|
+
# @option opts y1 [Integer] (0) the y-coordinate to place
|
91
|
+
# @option opts x2 [Integer] (50) the x-coordinate to place
|
92
|
+
# @option opts y2 [Integer] (50) the y-coordinate to place
|
93
|
+
# @option opts stroke_color [String] (:black) the color with which to stroke the line
|
94
|
+
# @option opts stroke_width [Decimal] (2.0) the width of the outside stroke
|
95
|
+
# @return [nil] intended to be void
|
96
|
+
# @api public
|
97
|
+
def line(opts = {})
|
98
|
+
opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :layout,
|
99
|
+
:stroke_color, :stroke_width])
|
100
|
+
opts[:range].each do |i|
|
101
|
+
@cards[i].line(opts[:x1], opts[:y1], opts[:x2], opts[:y2],
|
102
|
+
opts[:stroke_color], opts[:stroke_width])
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
13
107
|
end
|
data/lib/squib/api/text.rb
CHANGED
@@ -1,26 +1,46 @@
|
|
1
|
-
module Squib
|
2
|
-
class Deck
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
1
|
+
module Squib
|
2
|
+
class Deck
|
3
|
+
|
4
|
+
# Renders a string at a given location, width, alignment, font, etc.
|
5
|
+
#
|
6
|
+
# Unix-like newlines are interpreted even on Windows.
|
7
|
+
# See the {file:samples/text-options.rb samples/text.rb} for a lengthy example.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# text str: 'hello'
|
11
|
+
# text str: 'hello', x: 50, y:50, align: center
|
12
|
+
#
|
13
|
+
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
|
14
|
+
# @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.
|
15
|
+
# @option opts font [String] (Arial 36 or whatever was set with `set`) the Font description string, including family, styles, and size.
|
16
|
+
# (e.g. `'Arial bold italic 12'`)
|
17
|
+
# For the official documentation, see the [Pango docs](http://ruby-gnome2.sourceforge.jp/hiki.cgi?Pango%3A%3AFontDescription#style).
|
18
|
+
# This [description](http://www.pygtk.org/pygtk2reference/class-pangofontdescription.html) is also quite good.
|
19
|
+
# See the {file:samples/text-options.rb samples/text.rb} as well.
|
20
|
+
# @option opts x [Integer] (0) the x-coordinate to place
|
21
|
+
# @option opts y [Integer] (0) the y-coordinate to place
|
22
|
+
# @option opts color [String] (:black) the color the font will render to. See {file:API.md#label-Specifying+Colors Specifying Colors}
|
23
|
+
# @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).
|
24
|
+
# @option opts width [Integer, :native] (:native) the width of the box the string will be placed in. Stretches to the content by default.
|
25
|
+
# @option opts height [Integer, :native] the height of the box the string will be placed in. Stretches to the content by default.
|
26
|
+
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:API.md#label-Custom+Layouts Custom Layouts}
|
27
|
+
# @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. #
|
28
|
+
# Options are `:none, :word, :char, :word_char`. Also: `true` is the same as `:word_char`, `false` is the same as `:none`. Default `:word_char`
|
29
|
+
# @option opts align [:left, right, :center] (:left) The alignment of the text
|
30
|
+
# @option opts justify [Boolean] (false) toggles whether or not the text is justified or not.
|
31
|
+
# @option opts valign [:top, :middle, :bottom] (:top) When width and height are set, align text vertically according to the logical extents of the text.
|
32
|
+
# @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`
|
33
|
+
# @option opts hint [String] (:nil) draw a rectangle around the text with the given color. Overrides global hints (see {Deck#hint}).
|
34
|
+
# @return [nil] Returns nothing
|
35
|
+
# @api public
|
36
|
+
def text(opts = {})
|
37
|
+
opts = needs(opts, [:range, :str, :font, :x, :y, :width, :height, :text_color, :wrap,
|
38
|
+
:align, :justify, :valign, :ellipsize, :hint, :layout])
|
39
|
+
opts[:str] = [opts[:str]] * @cards.size unless opts[:str].respond_to? :each
|
40
|
+
opts[:range].each do |i|
|
41
|
+
@cards[i].text(opts[:str][i], opts[:font], opts[:x], opts[:y], opts[:color], opts)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
26
46
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Squib
|
2
|
+
class Deck
|
3
|
+
|
4
|
+
# Given inches, returns the number of pixels according to the deck's DPI.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# inches(2.5) # 750 (for default Deck::dpi of 300)
|
8
|
+
#
|
9
|
+
# @param [Decimal] n, the number of inches
|
10
|
+
# @return [Decimal] the number of pixels, according to the deck's DPI
|
11
|
+
# @api public
|
12
|
+
def inches(n)
|
13
|
+
@dpi * n
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
data/lib/squib/card.rb
CHANGED
@@ -1,27 +1,36 @@
|
|
1
|
-
require 'cairo'
|
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
|
-
|
1
|
+
require 'cairo'
|
2
|
+
require 'squib/input_helpers'
|
3
|
+
|
4
|
+
module Squib
|
5
|
+
# Back end graphics. Private.
|
6
|
+
class Card
|
7
|
+
include Squib::InputHelpers
|
8
|
+
|
9
|
+
# :nodoc:
|
10
|
+
# @api private
|
11
|
+
attr_reader :width, :height
|
12
|
+
|
13
|
+
# :nodoc:
|
14
|
+
# @api private
|
15
|
+
attr_accessor :cairo_surface, :cairo_context
|
16
|
+
|
17
|
+
# :nodoc:
|
18
|
+
# @api private
|
19
|
+
def initialize(deck, width, height)
|
20
|
+
@deck=deck; @width=width; @height=height
|
21
|
+
@cairo_surface = Cairo::ImageSurface.new(width,height)
|
22
|
+
@cairo_context = Cairo::Context.new(@cairo_surface)
|
23
|
+
end
|
24
|
+
|
25
|
+
########################
|
26
|
+
### BACKEND GRAPHICS ###
|
27
|
+
########################
|
28
|
+
require 'squib/graphics/background'
|
29
|
+
require 'squib/graphics/image'
|
30
|
+
require 'squib/graphics/save_doc'
|
31
|
+
require 'squib/graphics/save_images'
|
32
|
+
require 'squib/graphics/shapes'
|
33
|
+
require 'squib/graphics/text'
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Squib
|
2
|
+
# Squib's command-line options
|
3
|
+
module Commands
|
4
|
+
|
5
|
+
# Generate a new Squib project into a fresh directory.
|
6
|
+
#
|
7
|
+
# Provides conventions for using Git (you are using version control, right??).
|
8
|
+
# Also provides some basic layout and config files to start from, along with templates for instructions and other notes you don't want to forget.
|
9
|
+
#
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# squib new foo-blasters
|
13
|
+
# cd foo-blasters
|
14
|
+
# ruby deck.rb
|
15
|
+
# git commit -am "Starting my cool new game using Squib!"
|
16
|
+
#
|
17
|
+
# @api public
|
18
|
+
class New
|
19
|
+
|
20
|
+
# :nodoc:
|
21
|
+
# @api private
|
22
|
+
def process(args)
|
23
|
+
raise ArgumentError.new('Please specify a path.') if args.empty?
|
24
|
+
|
25
|
+
new_project_path = File.expand_path(args.join(" "), Dir.pwd)
|
26
|
+
template_path = File.expand_path('../project_template', File.dirname(__FILE__))
|
27
|
+
|
28
|
+
FileUtils.mkdir_p new_project_path
|
29
|
+
if !Dir["#{new_project_path}/**/*"].empty?
|
30
|
+
$stderr.puts "#{new_project_path} exists and is not empty. Doing nothing and quitting."
|
31
|
+
else
|
32
|
+
Dir.chdir(new_project_path) do
|
33
|
+
FileUtils.cp_r template_path + '/.', new_project_path
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Squib
|
2
|
+
# Squib's defaults for when arguments are not specified in the command nor layouts.
|
3
|
+
#
|
4
|
+
# @api public
|
5
|
+
SYSTEM_DEFAULTS = {
|
6
|
+
:range => :all,
|
7
|
+
:color => :black,
|
8
|
+
:str => '',
|
9
|
+
:fill_color => '#0000',
|
10
|
+
:stroke_color => :black,
|
11
|
+
:stroke_width => 2.0,
|
12
|
+
:font => :use_set,
|
13
|
+
:default_font => 'Arial 36',
|
14
|
+
:sheet => 0,
|
15
|
+
:x => 0,
|
16
|
+
:y => 0,
|
17
|
+
:x1 => 100,
|
18
|
+
:y1 => 100,
|
19
|
+
:x2 => 150,
|
20
|
+
:y2 => 150,
|
21
|
+
:x3 => 100,
|
22
|
+
:y3 => 150,
|
23
|
+
:x_radius => 0,
|
24
|
+
:y_radius => 0,
|
25
|
+
:align => :left,
|
26
|
+
:valign => :top,
|
27
|
+
:justify => false,
|
28
|
+
:ellipsize => :end,
|
29
|
+
:width => :native,
|
30
|
+
:height => :native,
|
31
|
+
:alpha => 1.0,
|
32
|
+
:format => :png,
|
33
|
+
:dir => "_output",
|
34
|
+
:prefix => "card_",
|
35
|
+
:margin => 75,
|
36
|
+
:gap => 0,
|
37
|
+
:trim => 0
|
38
|
+
}
|
39
|
+
|
40
|
+
end
|
data/lib/squib/deck.rb
CHANGED
@@ -1,49 +1,114 @@
|
|
1
|
-
require '
|
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
|
-
|
1
|
+
require 'yaml'
|
2
|
+
require 'pp'
|
3
|
+
require 'squib/card'
|
4
|
+
require 'squib/input_helpers'
|
5
|
+
require 'squib/constants'
|
6
|
+
|
7
|
+
# The project module
|
8
|
+
#
|
9
|
+
# @api public
|
10
|
+
module Squib
|
11
|
+
|
12
|
+
# The main interface to Squib. Provides a front-end porcelain whereas the Card class interacts with the graphics plumbing.
|
13
|
+
#
|
14
|
+
# @api public
|
15
|
+
class Deck
|
16
|
+
include Enumerable
|
17
|
+
include Squib::InputHelpers
|
18
|
+
|
19
|
+
# :nodoc:
|
20
|
+
# @api private
|
21
|
+
attr_reader :width, :height
|
22
|
+
|
23
|
+
# :nodoc:
|
24
|
+
# @api private
|
25
|
+
attr_reader :cards
|
26
|
+
|
27
|
+
# :nodoc:
|
28
|
+
# @api private
|
29
|
+
attr_reader :text_hint
|
30
|
+
|
31
|
+
# Squib's constructor that sets the immutable properties.
|
32
|
+
#
|
33
|
+
# This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
|
34
|
+
# The documented methods in Deck are the ones intended for use by most users.
|
35
|
+
# If your game requires multiple different sizes or orientations, I recommend using multiple `Squib::Deck`s in your `deck.rb`. You can modify the internals of `Squib::Deck` (e.g. `@cards`), but that's not recommended.
|
36
|
+
# @example
|
37
|
+
# require 'squib'
|
38
|
+
# Squib::Deck.new do
|
39
|
+
# text str: 'Hello, World!'
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# @param width: [Integer] the width of each card in pixels
|
43
|
+
# @param height: [Integer] the height of each card in pixels
|
44
|
+
# @param cards: [Integer] the number of cards in the deck
|
45
|
+
# @param dpi: [Integer] the pixels per inch when rendering out to PDF or calculating using inches.
|
46
|
+
# @param config: [String] the file used for global settings of this deck
|
47
|
+
# @param block [Block] the main body of the script.
|
48
|
+
# @api public
|
49
|
+
def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
|
50
|
+
@width=width; @height=height
|
51
|
+
@dpi = dpi
|
52
|
+
@font = Squib::SYSTEM_DEFAULTS[:default_font]
|
53
|
+
@cards = []
|
54
|
+
cards.times{ @cards << Squib::Card.new(self, width, height) }
|
55
|
+
load_config(config)
|
56
|
+
load_layout(layout)
|
57
|
+
if block_given?
|
58
|
+
instance_eval(&block)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Directly accesses the array of cards in the deck
|
63
|
+
#
|
64
|
+
# @api private
|
65
|
+
def [](key)
|
66
|
+
@cards[key]
|
67
|
+
end
|
68
|
+
|
69
|
+
# Iterates over each card in the deck
|
70
|
+
#
|
71
|
+
# @api private
|
72
|
+
def each(&block)
|
73
|
+
@cards.each { |card| block.call(card) }
|
74
|
+
end
|
75
|
+
|
76
|
+
# Load the configuration file, if exists, overriding hardcoded defaults
|
77
|
+
# @api private
|
78
|
+
def load_config(file)
|
79
|
+
if File.exists? file
|
80
|
+
if config = YAML.load_file(file)
|
81
|
+
@dpi = config['dpi'].to_i
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Load the layout configuration file, if exists
|
87
|
+
# @api private
|
88
|
+
def load_layout(file)
|
89
|
+
return if file.nil?
|
90
|
+
prelayout = YAML.load_file(file)
|
91
|
+
@layout = {}
|
92
|
+
prelayout.each do |key, value|
|
93
|
+
if value.key? "extends"
|
94
|
+
@layout[key] = prelayout[value["extends"]].merge prelayout[key]
|
95
|
+
else
|
96
|
+
@layout[key] = value
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
##################
|
102
|
+
### PUBLIC API ###
|
103
|
+
##################
|
104
|
+
require 'squib/api/background'
|
105
|
+
require 'squib/api/data'
|
106
|
+
require 'squib/api/image'
|
107
|
+
require 'squib/api/save'
|
108
|
+
require 'squib/api/settings'
|
109
|
+
require 'squib/api/shapes'
|
110
|
+
require 'squib/api/text'
|
111
|
+
require 'squib/api/units'
|
112
|
+
|
113
|
+
end
|
49
114
|
end
|
@@ -1,11 +1,13 @@
|
|
1
|
-
module Squib
|
2
|
-
class Card
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
cc
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
module Squib
|
2
|
+
class Card
|
3
|
+
|
4
|
+
# :nodoc:
|
5
|
+
# @api private
|
6
|
+
def background(color)
|
7
|
+
cc = cairo_context
|
8
|
+
cc.set_source_color(color)
|
9
|
+
cc.paint
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
11
13
|
end
|
data/lib/squib/graphics/image.rb
CHANGED
@@ -1,12 +1,28 @@
|
|
1
|
-
module Squib
|
2
|
-
class Card
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
cc
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
module Squib
|
2
|
+
class Card
|
3
|
+
|
4
|
+
# :nodoc:
|
5
|
+
# @api private
|
6
|
+
def png(file, x, y, alpha)
|
7
|
+
cc = cairo_context
|
8
|
+
png = Cairo::ImageSurface.from_png(file)
|
9
|
+
cc.set_source(png, x, y)
|
10
|
+
cc.paint(alpha)
|
11
|
+
end
|
12
|
+
|
13
|
+
# :nodoc:
|
14
|
+
# @api private
|
15
|
+
def svg(file, id, x, y, width, height)
|
16
|
+
svg = RSVG::Handle.new_from_file(file)
|
17
|
+
width = svg.width if width == :native
|
18
|
+
height = svg.height if height == :native
|
19
|
+
tmp = Cairo::ImageSurface.new(width, height)
|
20
|
+
tmp_cc = Cairo::Context.new(tmp)
|
21
|
+
tmp_cc.scale(width.to_f / svg.width.to_f, height.to_f / svg.height.to_f)
|
22
|
+
tmp_cc.render_rsvg_handle(svg, id)
|
23
|
+
cairo_context.set_source(tmp, x, y)
|
24
|
+
cairo_context.paint
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|