Fix bugs and cleanup to save_sheet
parent
969f16633e
commit
d385e205a5
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"restructuredtext.confPath": "${workspaceFolder}\\docs",
|
||||
"restructuredtext.linter.disabled": true
|
||||
}
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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,15 +58,13 @@ 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.translate w * 0.5, h * 0.5
|
||||
cc.rotate angle
|
||||
cc.translate trimmed_h * -0.5, trimmed_w * -0.5
|
||||
cc.translate h * -0.5, w * -0.5
|
||||
end
|
||||
cc.set_source(surface, -1 * trim, -1 * trim)
|
||||
cc.paint
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
Loading…
Reference in New Issue