squib 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +14 -0
  3. data/.travis.yml +4 -0
  4. data/CHANGELOG.md +201 -175
  5. data/Gemfile +2 -4
  6. data/README.md +650 -645
  7. data/RELEASE TODO.md +18 -18
  8. data/Rakefile +106 -99
  9. data/appveyor.yml +29 -0
  10. data/lib/squib.rb +32 -32
  11. data/lib/squib/api/background.rb +20 -20
  12. data/lib/squib/api/data.rb +131 -131
  13. data/lib/squib/api/image.rb +108 -90
  14. data/lib/squib/api/save.rb +151 -149
  15. data/lib/squib/api/settings.rb +35 -35
  16. data/lib/squib/api/shapes.rb +255 -230
  17. data/lib/squib/api/text.rb +65 -65
  18. data/lib/squib/api/text_embed.rb +96 -96
  19. data/lib/squib/args/arg_loader.rb +138 -138
  20. data/lib/squib/args/box.rb +54 -54
  21. data/lib/squib/args/card_range.rb +32 -32
  22. data/lib/squib/args/color_validator.rb +11 -11
  23. data/lib/squib/args/coords.rb +32 -32
  24. data/lib/squib/args/dir_validator.rb +16 -16
  25. data/lib/squib/args/draw.rb +92 -92
  26. data/lib/squib/args/embed_adjust.rb +25 -25
  27. data/lib/squib/args/embed_key.rb +17 -17
  28. data/lib/squib/args/hand_special.rb +37 -37
  29. data/lib/squib/args/import.rb +39 -39
  30. data/lib/squib/args/input_file.rb +37 -37
  31. data/lib/squib/args/paint.rb +43 -43
  32. data/lib/squib/args/paragraph.rb +116 -115
  33. data/lib/squib/args/save_batch.rb +63 -60
  34. data/lib/squib/args/scale_box.rb +53 -53
  35. data/lib/squib/args/sheet.rb +72 -72
  36. data/lib/squib/args/showcase_special.rb +38 -38
  37. data/lib/squib/args/svg_special.rb +37 -37
  38. data/lib/squib/args/transform.rb +60 -24
  39. data/lib/squib/args/typographer.rb +117 -117
  40. data/lib/squib/card.rb +66 -67
  41. data/lib/squib/conf.rb +131 -117
  42. data/lib/squib/constants.rb +12 -178
  43. data/lib/squib/deck.rb +113 -113
  44. data/lib/squib/graphics/cairo_context_wrapper.rb +113 -99
  45. data/lib/squib/graphics/gradient_regex.rb +46 -46
  46. data/lib/squib/graphics/hand.rb +42 -42
  47. data/lib/squib/graphics/image.rb +103 -76
  48. data/lib/squib/graphics/save_doc.rb +103 -103
  49. data/lib/squib/graphics/save_images.rb +39 -33
  50. data/lib/squib/graphics/shapes.rb +135 -119
  51. data/lib/squib/graphics/showcase.rb +85 -85
  52. data/lib/squib/graphics/text.rb +176 -176
  53. data/lib/squib/layout_parser.rb +91 -91
  54. data/lib/squib/layouts/economy.yml +85 -85
  55. data/lib/squib/layouts/fantasy.yml +101 -101
  56. data/lib/squib/layouts/hand.yml +62 -62
  57. data/lib/squib/layouts/playing-card.yml +35 -35
  58. data/lib/squib/layouts/tuck_box.yml +45 -45
  59. data/lib/squib/project_template/IDEAS.md +22 -0
  60. data/lib/squib/project_template/PLAYTESTING.md +26 -0
  61. data/lib/squib/project_template/RULES.md +21 -0
  62. data/lib/squib/project_template/config.yml +49 -45
  63. data/lib/squib/sample_helpers.rb +34 -0
  64. data/lib/squib/version.rb +10 -10
  65. data/samples/autoscale_font/_autoscale_font.rb +29 -0
  66. data/samples/color_shortcuts.rb +6 -6
  67. data/samples/csv_import.rb +26 -26
  68. data/samples/custom-config.yml +5 -5
  69. data/samples/custom_config.rb +18 -18
  70. data/samples/draw_shapes.rb +48 -45
  71. data/samples/embed_text.rb +88 -88
  72. data/samples/excel.rb +55 -55
  73. data/samples/hand.rb +24 -24
  74. data/samples/images/_images.rb +104 -0
  75. data/samples/intro/01_hello.rb +9 -0
  76. data/samples/intro/02_options.rb +15 -0
  77. data/samples/intro/03_layout.rb +12 -0
  78. data/samples/intro/04_arrays.rb +16 -0
  79. data/samples/intro/05_excel.rb +15 -0
  80. data/samples/layouts.rb +62 -62
  81. data/samples/layouts_builtin.rb +51 -51
  82. data/samples/load_images.rb +99 -78
  83. data/samples/load_images_config.yml +1 -0
  84. data/samples/quantity_explosion.csv +2 -2
  85. data/samples/ranges.rb +64 -64
  86. data/samples/sample.csv +2 -2
  87. data/samples/saves.rb +9 -1
  88. data/samples/sprites.png +0 -0
  89. data/samples/text/_text.rb +46 -0
  90. data/samples/text_options.rb +102 -102
  91. data/spec/api/api_data_spec.rb +117 -117
  92. data/spec/api/api_settings_spec.rb +37 -37
  93. data/spec/args/box_spec.rb +127 -127
  94. data/spec/args/draw_spec.rb +101 -95
  95. data/spec/args/embed_key_spec.rb +13 -13
  96. data/spec/args/input_file_spec.rb +21 -21
  97. data/spec/args/paint_spec.rb +21 -21
  98. data/spec/args/paragraph_spec.rb +152 -152
  99. data/spec/args/range_spec.rb +40 -40
  100. data/spec/args/save_batch_spec.rb +51 -51
  101. data/spec/args/scale_box_spec.rb +71 -71
  102. data/spec/args/sheet_spec.rb +58 -58
  103. data/spec/args/showcase_special_spec.rb +15 -15
  104. data/spec/args/transform_spec.rb +25 -0
  105. data/spec/card_spec.rb +11 -0
  106. data/spec/conf_spec.rb +13 -3
  107. data/spec/data/conf/unrecognized.yml +4 -0
  108. data/spec/data/csv/qty.csv +2 -2
  109. data/spec/data/csv/qty_named.csv +2 -2
  110. data/spec/data/csv/with_spaces.csv +2 -2
  111. data/spec/data/samples/autoscale_font.rb.txt +84 -84
  112. data/spec/data/samples/basic.rb.txt +227 -209
  113. data/spec/data/samples/config_text_markup.rb.txt +72 -72
  114. data/spec/data/samples/csv_import.rb.txt +213 -213
  115. data/spec/data/samples/custom_config.rb.txt +57 -48
  116. data/spec/data/samples/draw_shapes.rb.txt +555 -3
  117. data/spec/data/samples/embed_text.rb.txt +283 -283
  118. data/spec/data/samples/excel.rb.txt +661 -661
  119. data/spec/data/samples/gradients.rb.txt +77 -79
  120. data/spec/data/samples/hand.rb.txt +538 -538
  121. data/spec/data/samples/hello_world.rb.txt +36 -36
  122. data/spec/data/samples/load_images.rb.txt +170 -0
  123. data/spec/data/samples/portrait-landscape.rb.txt +51 -49
  124. data/spec/data/samples/ranges.rb.txt +472 -460
  125. data/spec/data/samples/saves.rb.txt +810 -801
  126. data/spec/data/samples/showcase.rb.txt +5926 -5910
  127. data/spec/data/samples/text_options.rb.txt +1125 -1125
  128. data/spec/data/samples/tgc_proofs.rb.txt +95 -81
  129. data/spec/graphics/cairo_context_wrapper_spec.rb +104 -84
  130. data/spec/graphics/graphics_save_doc_spec.rb +67 -67
  131. data/spec/samples/diff-with-css.example.html +39 -0
  132. data/spec/samples/expected/load_images_00.png +0 -0
  133. data/spec/samples/expected/shape_00.png +0 -0
  134. data/spec/samples/run_samples_spec.rb +17 -0
  135. data/spec/samples/samples_regression_spec.rb +72 -82
  136. data/spec/spec_helper.rb +9 -1
  137. data/squib.gemspec +49 -48
  138. data/squib.sublime-project +42 -42
  139. metadata +94 -48
  140. data/spec/graphics/graphics_images_spec.rb +0 -94
@@ -1,117 +1,131 @@
1
- require 'forwardable'
2
- require 'squib'
3
- require 'squib/args/typographer'
4
- require 'yaml'
5
-
6
- module Squib
7
- # @api private
8
- class Conf
9
-
10
- DEFAULTS = {
11
- 'antialias' => 'best',
12
- 'backend' => 'memory',
13
- 'count_format' => SYSTEM_DEFAULTS[:count_format],
14
- 'custom_colors' => {},
15
- 'dir' => SYSTEM_DEFAULTS[:dir],
16
- 'hint' => :none,
17
- 'img_dir' => '.',
18
- 'progress_bars' => false,
19
- 'ldquote' => "\u201C", # UTF8 chars
20
- 'rdquote' => "\u201D",
21
- 'lsquote' => "\u2018",
22
- 'rsquote' => "\u2019",
23
- 'em_dash' => "\u2014",
24
- 'en_dash' => "\u2013",
25
- 'ellipsis' => "\u2026",
26
- 'smart_quotes' => true,
27
- 'text_hint' => 'off',
28
- 'warn_ellipsize'=> true,
29
- }
30
-
31
- #Translate the hints to the methods.
32
- ANTIALIAS_OPTS = {
33
- nil => 'subpixel',
34
- 'best' => 'subpixel',
35
- 'good' => 'gray',
36
- 'fast' => 'gray',
37
- 'gray' => 'gray',
38
- 'subpixel' => 'subpixel'
39
- }
40
-
41
- def initialize(config_hash = DEFAULTS)
42
- @config_hash = config_hash
43
- @typographer = Args::Typographer.new(config_hash)
44
- normalize_antialias
45
- end
46
-
47
- # Load the configuration file, if exists, overriding hardcoded defaults
48
- # @api private
49
- def self.load(file)
50
- yaml = {}
51
- if File.exists? file
52
- Squib::logger.info { " using config: #{file}" }
53
- yaml = YAML.load_file(file) || {}
54
- end
55
- Conf.new(DEFAULTS.merge(yaml))
56
- end
57
-
58
- def to_s
59
- "Conf: #{@config_hash.to_s}"
60
- end
61
-
62
- def img_dir
63
- @config_hash['img_dir']
64
- end
65
-
66
- def text_hint
67
- @config_hash['text_hint']
68
- end
69
-
70
- def text_hint=(hint)
71
- @config_hash['text_hint'] = hint
72
- end
73
-
74
- def progress_bars
75
- @config_hash['progress_bars']
76
- end
77
-
78
- def typographer
79
- @typographer
80
- end
81
-
82
- def dir
83
- @config_hash['dir']
84
- end
85
-
86
- def prefix
87
- @config_hash['prefix']
88
- end
89
-
90
- def count_format
91
- @config_hash['count_format']
92
- end
93
-
94
- def antialias
95
- @config_hash['antialias']
96
- end
97
-
98
- def backend
99
- @config_hash['backend']
100
- end
101
-
102
- def custom_colors
103
- @config_hash['custom_colors']
104
- end
105
-
106
- def warn_ellipsize?
107
- @config_hash['warn_ellipsize']
108
- end
109
-
110
- private
111
-
112
- def normalize_antialias
113
- @config_hash['antialias'] = ANTIALIAS_OPTS[@config_hash['antialias'].downcase.strip]
114
- end
115
-
116
- end
117
- end
1
+ require 'forwardable'
2
+ require 'squib'
3
+ require 'squib/args/typographer'
4
+ require 'yaml'
5
+
6
+ module Squib
7
+ # @api private
8
+ class Conf
9
+
10
+ DEFAULTS = {
11
+ 'antialias' => 'best',
12
+ 'backend' => 'memory',
13
+ 'count_format' => '%02d',
14
+ 'custom_colors' => {},
15
+ 'dir' => '_output',
16
+ 'hint' => :none,
17
+ 'img_dir' => '.',
18
+ 'progress_bars' => false,
19
+ 'ldquote' => "\u201C", # UTF8 chars
20
+ 'rdquote' => "\u201D",
21
+ 'lsquote' => "\u2018",
22
+ 'rsquote' => "\u2019",
23
+ 'em_dash' => "\u2014",
24
+ 'en_dash' => "\u2013",
25
+ 'ellipsis' => "\u2026",
26
+ 'smart_quotes' => true,
27
+ 'text_hint' => 'off',
28
+ 'warn_ellipsize'=> true,
29
+ 'warn_png_scale'=> true,
30
+ }
31
+
32
+ #Translate the hints to the methods.
33
+ ANTIALIAS_OPTS = {
34
+ nil => 'subpixel',
35
+ 'best' => 'subpixel',
36
+ 'good' => 'gray',
37
+ 'fast' => 'gray',
38
+ 'gray' => 'gray',
39
+ 'subpixel' => 'subpixel'
40
+ }
41
+
42
+ def initialize(config_hash = DEFAULTS)
43
+ @config_hash = config_hash
44
+ @typographer = Args::Typographer.new(config_hash)
45
+ normalize_antialias
46
+ end
47
+
48
+ # Load the configuration file, if exists, overriding hardcoded defaults
49
+ # @api private
50
+ def self.load(file)
51
+ yaml = {}
52
+ if File.exists? file
53
+ Squib::logger.info { " using config: #{file}" }
54
+ yaml = YAML.load_file(file) || {}
55
+ end
56
+ warn_unrecognized(yaml)
57
+ Conf.new(DEFAULTS.merge(yaml))
58
+ end
59
+
60
+ def to_s
61
+ "Conf: #{@config_hash.to_s}"
62
+ end
63
+
64
+ def img_dir
65
+ @config_hash['img_dir']
66
+ end
67
+
68
+ def text_hint
69
+ @config_hash['text_hint']
70
+ end
71
+
72
+ def text_hint=(hint)
73
+ @config_hash['text_hint'] = hint
74
+ end
75
+
76
+ def progress_bars
77
+ @config_hash['progress_bars']
78
+ end
79
+
80
+ def typographer
81
+ @typographer
82
+ end
83
+
84
+ def dir
85
+ @config_hash['dir']
86
+ end
87
+
88
+ def prefix
89
+ @config_hash['prefix']
90
+ end
91
+
92
+ def count_format
93
+ @config_hash['count_format']
94
+ end
95
+
96
+ def antialias
97
+ @config_hash['antialias']
98
+ end
99
+
100
+ def backend
101
+ @config_hash['backend']
102
+ end
103
+
104
+ def custom_colors
105
+ @config_hash['custom_colors']
106
+ end
107
+
108
+ def warn_ellipsize?
109
+ @config_hash['warn_ellipsize']
110
+ end
111
+
112
+ def warn_png_scale?
113
+ @config_hash['warn_png_scale']
114
+ end
115
+
116
+ private
117
+
118
+ def normalize_antialias
119
+ @config_hash['antialias'] = ANTIALIAS_OPTS[@config_hash['antialias'].downcase.strip]
120
+ end
121
+
122
+ # Were there any unrecognized options in the config file?
123
+ def self.warn_unrecognized(yaml)
124
+ unrec = yaml.keys - DEFAULTS.keys
125
+ if unrec.any?
126
+ Squib::logger.warn "Unrecognized configuration option(s): #{unrec.join(',')}"
127
+ end
128
+ end
129
+
130
+ end
131
+ end
@@ -1,178 +1,12 @@
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
- :align => :left,
7
- :alpha => 1.0,
8
- :angle => 0,
9
- :angle_range => (Math::PI / -4.0)..(Math::PI / 4),
10
- :blend => :none,
11
- :color => :black,
12
- :columns => 5,
13
- :count_format => '%02d',
14
- :cx1 => 0,
15
- :cx2 => 0,
16
- :cy1 => 0,
17
- :cy2 => 0,
18
- :data => nil,
19
- :default_font => 'Arial 36',
20
- :dir => '_output',
21
- :dx => 0, # delta
22
- :dy => 0, # delta
23
- :ellipsize => :end,
24
- :face => :left,
25
- :fill_color => '#0000',
26
- :force_id => false,
27
- :font => :use_set,
28
- :font_size => nil,
29
- :format => :png,
30
- :gap => 0,
31
- :height => :native,
32
- :hint => :off,
33
- :inner_radius => 50,
34
- :img_dir => '.',
35
- :justify => false,
36
- :key => '*',
37
- :margin => 75,
38
- :markup => false,
39
- :mask => nil,
40
- :n => 5,
41
- :offset => 1.1,
42
- :outer_radius => 100,
43
- :prefix => 'card_',
44
- :progress_bar => false,
45
- :quotes => :dumb,
46
- :reflect_offset => 15,
47
- :reflect_percent => 0.25,
48
- :reflect_strength => 0.2,
49
- :range => :all,
50
- :rotate => false,
51
- :rows => :infinite,
52
- :scale => 0.85,
53
- :sheet => 0,
54
- :spacing => 0,
55
- :str => '',
56
- :stroke_color => :black,
57
- :stroke_width => 2.0,
58
- :trim => 0,
59
- :trim_radius => 38,
60
- :valign => :top,
61
- :width => :native,
62
- :wrap => true,
63
- :x => 0,
64
- :x1 => 100,
65
- :x2 => 150,
66
- :x3 => 100,
67
- :x_radius => 0,
68
- :y => 0,
69
- :y1 => 100,
70
- :y2 => 150,
71
- :y3 => 150,
72
- :y_radius => 0,
73
- }
74
-
75
- # These are parameters that are intended to be "expanded" across
76
- # range if they are singletons.
77
- #
78
- # For example: using a different font for each card, using one `text` command
79
- #
80
- # key: the internal name of the param (e.g. :files)
81
- # value: the user-facing API key (e.g. file: 'abc.png')
82
- #
83
- # @api private
84
- EXPANDING_PARAMS = {
85
- :align => :align,
86
- :alpha => :alpha,
87
- :angle => :angle,
88
- :blend => :blend,
89
- :circle_radius => :radius,
90
- :color => :color,
91
- :cx1 => :cx1,
92
- :cx2 => :cx2,
93
- :cy1 => :cy1,
94
- :cy2 => :cy2,
95
- :dx => :dx,
96
- :dy => :dy,
97
- :ellipsize => :ellipsize,
98
- :files => :file,
99
- :fill_color => :fill_color,
100
- :force_svgid => :force_id,
101
- :font => :font,
102
- :font_size => :font_size,
103
- :height => :height,
104
- :hint => :hint,
105
- :inner_radius => :inner_radius,
106
- :justify => :justify,
107
- :layout => :layout,
108
- :markup => :markup,
109
- :mask => :mask,
110
- :n => :n,
111
- :outer_radius => :outer_radius,
112
- :quotes => :quotes,
113
- :rect_radius => :radius,
114
- :spacing => :spacing,
115
- :str => :str,
116
- :stroke_color => :stroke_color,
117
- :stroke_width => :stroke_width,
118
- :svgdata => :data,
119
- :svgid => :id,
120
- :valign => :valign,
121
- :width => :width,
122
- :wrap => :wrap,
123
- :x => :x,
124
- :x1 => :x1,
125
- :x2 => :x2,
126
- :x3 => :x3,
127
- :x_radius => :x_radius,
128
- :y => :y,
129
- :y1 => :y1,
130
- :y2 => :y2,
131
- :y3 => :y3,
132
- :y_radius => :y_radius,
133
- }
134
-
135
- # These parameters are considered for unit conversion
136
- #
137
- # For example
138
- # text str: 'Hello, World', x: '1in'
139
- #
140
- # key: the internal name of the param (e.g. :circle_radius)
141
- # value: the user-facing API key (e.g. radius: '1in')
142
- UNIT_CONVERSION_PARAMS = {
143
- :circle_radius => :radius,
144
- :cx1 => :cx1,
145
- :cx2 => :cx2,
146
- :cy1 => :cy1,
147
- :cy2 => :cy2,
148
- :dx => :dx, # delta
149
- :dy => :dx, # delta
150
- :gap => :gap,
151
- :height => :height,
152
- :inner_radius => :inner_radius,
153
- :margin => :margin,
154
- :outer_radius => :outer_radius,
155
- :paper_width => :width,
156
- :paper_height => :height,
157
- :rect_radius => :radius,
158
- :spacing => :spacing,
159
- :stroke_width => :stroke_width,
160
- :trim => :trim,
161
- :width => :width,
162
- :x => :x,
163
- :x1 => :x1,
164
- :x2 => :x2,
165
- :x3 => :x3,
166
- :x_radius => :x_radius,
167
- :y => :y,
168
- :y1 => :y1,
169
- :y2 => :y2,
170
- :y3 => :y3,
171
- :y_radius => :y_radius,
172
- }
173
-
174
- # Used for inch-cm conversion
175
- # :nodoc:
176
- # @api private
177
- INCHES_IN_CM = 0.393700787
178
- end
1
+ module Squib
2
+
3
+ # System-wide default font
4
+ # :nodoc:
5
+ # @api public
6
+ DEFAULT_FONT = 'Arial 36'
7
+
8
+ # Used for inch-cm conversion
9
+ # :nodoc:
10
+ # @api private
11
+ INCHES_IN_CM = 0.393700787
12
+ end
@@ -1,113 +1,113 @@
1
- require 'forwardable'
2
- require 'pp'
3
- require 'squib'
4
- require 'squib/args/unit_conversion'
5
- require 'squib/card'
6
- require 'squib/conf'
7
- require 'squib/constants'
8
- require 'squib/graphics/hand'
9
- require 'squib/graphics/showcase'
10
- require 'squib/layout_parser'
11
- require 'squib/progress'
12
-
13
-
14
- # The project module
15
- #
16
- # @api public
17
- module Squib
18
-
19
- # The main interface to Squib. Provides a front-end porcelain whereas the Card class interacts with the graphics plumbing.
20
- #
21
- # @api public
22
- class Deck
23
- include Enumerable
24
- extend Forwardable
25
-
26
- # Attributes for the width, height (in pixels) and number of cards
27
- # These are expected to be immuatble for the life of Deck
28
- # @api private
29
- attr_reader :width, :height, :cards
30
-
31
- # Delegate these configuration options to the Squib::Conf object
32
- def_delegators :conf, :antialias, :backend, :count_format, :custom_colors, :dir,
33
- :img_dir, :prefix, :text_hint, :typographer
34
- # :nodoc:
35
- # @api private
36
- attr_reader :layout, :conf, :dpi, :font
37
-
38
- #
39
- # deck.size is really just @cards.size
40
- def_delegators :cards, :size
41
-
42
- # Squib's constructor that sets the immutable properties.
43
- #
44
- # This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
45
- # The documented methods in Deck are the ones intended for use by most users.
46
- # 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.
47
- # @example
48
- # require 'squib'
49
- # Squib::Deck.new do
50
- # text str: 'Hello, World!"
51
- # end
52
- #
53
- # @param width [Integer] the width of each card in pixels. Supports unit conversion (e.g. '2.5in').
54
- # @param height [Integer] the height of each card in pixels. Supports unit conversion (e.g. '3.5in').
55
- # @param cards [Integer] the number of cards in the deck
56
- # @param dpi [Integer] the pixels per inch when rendering out to PDF or calculating using inches.
57
- # @param config [String] the file used for global settings of this deck
58
- # @param layout [String, Array] load a YML file of custom layouts. Multiple files are merged sequentially, redefining collisons. See README and sample for details.
59
- # @param block [Block] the main body of the script.
60
- # @api public
61
- def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
62
- @dpi = dpi
63
- @font = SYSTEM_DEFAULTS[:default_font]
64
- @cards = []
65
- @conf = Conf.load(config)
66
- @progress_bar = Progress.new(@conf.progress_bars) # FIXME this is evil. Using something different with @ and non-@
67
- show_info(config, layout)
68
- @width = Args::UnitConversion.parse width, dpi
69
- @height = Args::UnitConversion.parse height, dpi
70
- cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) }
71
- @layout = LayoutParser.load_layout(layout)
72
- if block_given?
73
- instance_eval(&block) # here we go. wheeeee!
74
- end
75
- end
76
-
77
- # Directly accesses the array of cards in the deck
78
- #
79
- # @api private
80
- def [](key)
81
- @cards[key]
82
- end
83
-
84
- # Iterates over each card in the deck
85
- #
86
- # @api private
87
- def each(&block)
88
- @cards.each { |card| block.call(card) }
89
- end
90
-
91
- # Use Logger to show more detail on the run
92
- # :nodoc:
93
- # @api private
94
- def show_info(config, layout)
95
- Squib::logger.info "Squib v#{Squib::VERSION}"
96
- Squib::logger.info " building #{@cards.size} #{@width}x#{@height} cards"
97
- Squib::logger.info " using #{@backend}"
98
- end
99
-
100
- ##################
101
- ### PUBLIC API ###
102
- ##################
103
- require 'squib/api/background'
104
- require 'squib/api/data'
105
- require 'squib/api/image'
106
- require 'squib/api/save'
107
- require 'squib/api/settings'
108
- require 'squib/api/shapes'
109
- require 'squib/api/text'
110
- require 'squib/api/units'
111
-
112
- end
113
- end
1
+ require 'forwardable'
2
+ require 'pp'
3
+ require 'squib'
4
+ require 'squib/args/unit_conversion'
5
+ require 'squib/card'
6
+ require 'squib/conf'
7
+ require 'squib/constants'
8
+ require 'squib/graphics/hand'
9
+ require 'squib/graphics/showcase'
10
+ require 'squib/layout_parser'
11
+ require 'squib/progress'
12
+
13
+
14
+ # The project module
15
+ #
16
+ # @api public
17
+ module Squib
18
+
19
+ # The main interface to Squib. Provides a front-end porcelain whereas the Card class interacts with the graphics plumbing.
20
+ #
21
+ # @api public
22
+ class Deck
23
+ include Enumerable
24
+ extend Forwardable
25
+
26
+ # Attributes for the width, height (in pixels) and number of cards
27
+ # These are expected to be immuatble for the life of Deck
28
+ # @api private
29
+ attr_reader :width, :height, :cards
30
+
31
+ # Delegate these configuration options to the Squib::Conf object
32
+ def_delegators :conf, :antialias, :backend, :count_format, :custom_colors, :dir,
33
+ :img_dir, :prefix, :text_hint, :typographer
34
+ # :nodoc:
35
+ # @api private
36
+ attr_reader :layout, :conf, :dpi, :font
37
+
38
+ #
39
+ # deck.size is really just @cards.size
40
+ def_delegators :cards, :size
41
+
42
+ # Squib's constructor that sets the immutable properties.
43
+ #
44
+ # This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
45
+ # The documented methods in Deck are the ones intended for use by most users.
46
+ # 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.
47
+ # @example
48
+ # require 'squib'
49
+ # Squib::Deck.new do
50
+ # text str: 'Hello, World!"
51
+ # end
52
+ #
53
+ # @param width [Integer] the width of each card in pixels. Supports unit conversion (e.g. '2.5in').
54
+ # @param height [Integer] the height of each card in pixels. Supports unit conversion (e.g. '3.5in').
55
+ # @param cards [Integer] the number of cards in the deck
56
+ # @param dpi [Integer] the pixels per inch when rendering out to PDF or calculating using inches.
57
+ # @param config [String] the file used for global settings of this deck
58
+ # @param layout [String, Array] load a YML file of custom layouts. Multiple files are merged sequentially, redefining collisons. See README and sample for details.
59
+ # @param block [Block] the main body of the script.
60
+ # @api public
61
+ def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
62
+ @dpi = dpi
63
+ @font = DEFAULT_FONT
64
+ @cards = []
65
+ @conf = Conf.load(config)
66
+ @progress_bar = Progress.new(@conf.progress_bars) # FIXME this is evil. Using something different with @ and non-@
67
+ show_info(config, layout)
68
+ @width = Args::UnitConversion.parse width, dpi
69
+ @height = Args::UnitConversion.parse height, dpi
70
+ cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) }
71
+ @layout = LayoutParser.load_layout(layout)
72
+ if block_given?
73
+ instance_eval(&block) # here we go. wheeeee!
74
+ end
75
+ end
76
+
77
+ # Directly accesses the array of cards in the deck
78
+ #
79
+ # @api private
80
+ def [](key)
81
+ @cards[key]
82
+ end
83
+
84
+ # Iterates over each card in the deck
85
+ #
86
+ # @api private
87
+ def each(&block)
88
+ @cards.each { |card| block.call(card) }
89
+ end
90
+
91
+ # Use Logger to show more detail on the run
92
+ # :nodoc:
93
+ # @api private
94
+ def show_info(config, layout)
95
+ Squib::logger.info "Squib v#{Squib::VERSION}"
96
+ Squib::logger.info " building #{@cards.size} #{@width}x#{@height} cards"
97
+ Squib::logger.info " using #{@backend}"
98
+ end
99
+
100
+ ##################
101
+ ### PUBLIC API ###
102
+ ##################
103
+ require 'squib/api/background'
104
+ require 'squib/api/data'
105
+ require 'squib/api/image'
106
+ require 'squib/api/save'
107
+ require 'squib/api/settings'
108
+ require 'squib/api/shapes'
109
+ require 'squib/api/text'
110
+ require 'squib/api/units'
111
+
112
+ end
113
+ end