diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..86acf76 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "restructuredtext.confPath": "${workspaceFolder}\\docs", + "restructuredtext.linter.disabled": true +} \ No newline at end of file diff --git a/docs/dsl/save_sheet.rst b/docs/dsl/save_sheet.rst index c515956..aeb382e 100644 --- a/docs/dsl/save_sheet.rst +++ b/docs/dsl/save_sheet.rst @@ -44,7 +44,9 @@ suffix rotate default: ``false`` - If ``true``, the saved cards will be rotated 90 degrees clockwise. Or, rotate by the number of radians. Intended to rendering landscape instead of portrait. Possible values: ``true``, ``false``, ``:clockwise``, ``:counterclockwise`` + if ``true``, all saved cards will be rotated 90 degrees clockwise. Possible values: ``true``, ``false``, ``:clockwise``, ``:counterclockwise`` + + Supports arrays so you can rotate individual cards different ways if that's how you want to roll, e.g. ``rotate: [:clockwise, :counterclockwise]`` dir default: ``'_output'`` @@ -78,3 +80,7 @@ rtl Examples -------- + +.. literalinclude:: ../../samples/saves/_saves.rb + :language: ruby + :linenos: diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb index 4c6c2fa..982893e 100644 --- a/lib/squib/graphics/save_doc.rb +++ b/lib/squib/graphics/save_doc.rb @@ -4,7 +4,8 @@ module Squib # :nodoc: # @api private def render_sheet(range, batch, sheet) - w,h,sheet_width,sheet_height = compute_dimensions(sheet, batch) + rotate = batch.rotate.any? true # either rotate all or none + w,h,sheet_width,sheet_height = compute_dimensions(sheet, rotate) cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height)) num_this_sheet = 0 sheet_num = 0 @@ -24,31 +25,32 @@ module Squib end surface = preprocess(@cards[i].cairo_surface, sheet.trim, w, h, - batch.rotate[i], batch.angle[i]) + rotate, batch.angle[i]) cc.set_source(surface, x, y) cc.paint num_this_sheet += 1 - x += (w + sheet.gap - 2 * sheet.trim) * (sheet.rtl ? -1 : 1) + x += (w + sheet.gap) * (sheet.rtl ? -1 : 1) if num_this_sheet % sheet.columns == 0 # new row x = sheet.rtl ? rtl_start_x(sheet_width, sheet, w) : sheet.margin - y += surface.height + sheet.gap + y += h + sheet.gap end - bar.increment end cc.target.write_to_png(batch.full_filename(sheet_num)) end end - def compute_dimensions(sheet, batch) - w,h = batch.rotate ? [@height,@width] : [@width,@height] - sheet_width = (sheet.columns * (w + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin) - sheet_height = (sheet.rows * (h + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin) + def compute_dimensions(sheet, rotate) + w,h = rotate ? [@height,@width] : [@width,@height] + w -= 2 * sheet.trim + h -= 2 * sheet.trim + sheet_width = (sheet.columns * (w + 2 * sheet.gap)) + (2 * sheet.margin) + sheet_height = (sheet.rows * (h + 2 * sheet.gap)) + (2 * sheet.margin) return [w, h, sheet_width, sheet_height] end def rtl_start_x(sheet_width, sheet, w) - return sheet_width - sheet.margin - sheet.gap - w + 2 * sheet.trim + return sheet_width - sheet.margin - sheet.gap - w end # Return a new Cairo::ImageSurface that is trimmed and rotated @@ -56,16 +58,14 @@ module Squib # :nodoc: # @api private def preprocess(surface, trim, w, h, rotate, angle) - trimmed_w = w - 2 * trim - trimmed_h = h - 2 * trim if trim > 0 || rotate - tmp = Cairo::ImageSurface.new(trimmed_w, trimmed_h) + tmp = Cairo::ImageSurface.new(w, h) cc = Cairo::Context.new(tmp) if rotate - cc.translate trimmed_w * 0.5, trimmed_h * 0.5 - cc.rotate angle - cc.translate trimmed_h * -0.5, trimmed_w * -0.5 - end + cc.translate w * 0.5, h * 0.5 + cc.rotate angle + cc.translate h * -0.5, w * -0.5 + end cc.set_source(surface, -1 * trim, -1 * trim) cc.paint surface = tmp diff --git a/samples/saves/_saves.rb b/samples/saves/_saves.rb index 6e4a2e3..82b3d8e 100644 --- a/samples/saves/_saves.rb +++ b/samples/saves/_saves.rb @@ -2,7 +2,6 @@ require_relative '../../lib/squib' # This sample demonstrates how to use the various save methods - Squib::Deck.new(width: 825, height: 1125, cards: 16) do background color: :gray rect x: 38, y: 38, width: 750, height: 1050, @@ -61,14 +60,3 @@ Squib::Deck.new(width: 100, height: 100, cards: 3) do # Buggy behavior was to revert to 1 row and not respect the rows arg. save_sheet prefix: 'save_sheet_bug332_', rows: 2, columns: 4 end - -# Allow rotating -Squib::Deck.new(width: 100, height: 50, cards: 8) do - background color: :white - rect x: 10, y: 10, width: 80, height: 30 - rect x: 5, y: 5, width: 90, height: 40, stroke_width: 5, stroke_color: :blue - text y: 2, str: 0..7, font: 'Open Sans Bold 8', align: :center, width: 100 - save_sheet prefix: 'save_sheet_rotated_', rows: 2, columns: 3, rotate: true - save_sheet prefix: 'save_sheet_rotated_trimmed_', rows: 2, columns: 3, rotate: true, trim: 5 - save_sheet prefix: 'save_sheet_rotated_trimmed_rtl_', rows: 2, columns: 3, rotate: true, trim: 5, rtl: true -end diff --git a/samples/saves/save_sheet_custom_rotate_00_expected.png b/samples/saves/save_sheet_custom_rotate_00_expected.png new file mode 100644 index 0000000..2291d02 Binary files /dev/null and b/samples/saves/save_sheet_custom_rotate_00_expected.png differ diff --git a/samples/saves/save_sheet_custom_rotate_01_expected.png b/samples/saves/save_sheet_custom_rotate_01_expected.png new file mode 100644 index 0000000..7a2ee31 Binary files /dev/null and b/samples/saves/save_sheet_custom_rotate_01_expected.png differ diff --git a/samples/saves/save_sheet_unrotated_00_expected.png b/samples/saves/save_sheet_unrotated_00_expected.png new file mode 100644 index 0000000..eba7a4b Binary files /dev/null and b/samples/saves/save_sheet_unrotated_00_expected.png differ diff --git a/samples/saves/save_sheet_unrotated_01_expected.png b/samples/saves/save_sheet_unrotated_01_expected.png new file mode 100644 index 0000000..d3c931b Binary files /dev/null and b/samples/saves/save_sheet_unrotated_01_expected.png differ