Fix bugs and cleanup to save_sheet

This commit is contained in:
Andy Meneely
2021-10-22 23:15:11 -04:00
parent 969f16633e
commit d385e205a5
8 changed files with 28 additions and 30 deletions
+17 -17
View File
@@ -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