Add rotate option for save_sheet (#301) (#345)

* Initial work on rotate for save_sheet

* Feature done! Maybe?
This commit is contained in:
Andy Meneely
2021-10-22 01:26:03 -04:00
committed by GitHub
parent 7b64c1aa49
commit 969f16633e
18 changed files with 724 additions and 67 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ module Squib
range sprue
columns rows rtl
dir prefix count_format suffix
margin gap trim trim_radius
margin gap trim trim_radius rotate
)
end
+32 -16
View File
@@ -4,13 +4,12 @@ module Squib
# :nodoc:
# @api private
def render_sheet(range, batch, sheet)
sheet_width = (sheet.columns * (@width + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
sheet_height = (sheet.rows * (@height + 2 * sheet.gap - 2 * sheet.trim)) + (2 * sheet.margin)
w,h,sheet_width,sheet_height = compute_dimensions(sheet, batch)
cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
num_this_sheet = 0
sheet_num = 0
y = sheet.margin
x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
x = sheet.rtl ? rtl_start_x(sheet_width, sheet, w) : sheet.margin
@progress_bar.start("Saving PNG sheet to #{batch.summary}", @cards.size + 1) do |bar|
range.each do |i|
if num_this_sheet >= (sheet.columns * sheet.rows) # new sheet
@@ -20,36 +19,53 @@ module Squib
num_this_sheet = 0
sheet_num += 1
y = sheet.margin
x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
x = sheet.rtl ? rtl_start_x(sheet_width, sheet, w) : sheet.margin
cc = Cairo::Context.new(Cairo::ImageSurface.new(sheet_width, sheet_height))
end
surface = trim(@cards[i].cairo_surface, sheet.trim, @width, @height)
surface = preprocess(@cards[i].cairo_surface,
sheet.trim, w, h,
batch.rotate[i], batch.angle[i])
cc.set_source(surface, x, y)
cc.paint
num_this_sheet += 1
x += (surface.width + sheet.gap) * (sheet.rtl ? -1 : 1)
x += (w + sheet.gap - 2 * sheet.trim) * (sheet.rtl ? -1 : 1)
if num_this_sheet % sheet.columns == 0 # new row
x = sheet.rtl ? (sheet_width - sheet.margin - sheet.gap - @width) : sheet.margin
x = sheet.rtl ? rtl_start_x(sheet_width, sheet, w) : sheet.margin
y += surface.height + sheet.gap
end
bar.increment
end
cc.target.write_to_png(batch.full_filename(sheet_num))
end
end
# Return a new Cairo::ImageSurface that is trimmed from the original
#
# @param surface The surface to trim
# @param trim The number of pixels around the edge to trim
# @param width The width of the surface prior to the trim
# @param height The height of the surface prior to the trim
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)
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
end
# Return a new Cairo::ImageSurface that is trimmed and rotated
# from the original
# :nodoc:
# @api private
def trim(surface, trim, width, height)
if trim > 0
tmp = Cairo::ImageSurface.new(width - 2 * trim, height - 2 * trim)
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)
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.set_source(surface, -1 * trim, -1 * trim)
cc.paint
surface = tmp