Skip to content

Commit

Permalink
scale afterwards
Browse files Browse the repository at this point in the history
  • Loading branch information
andymeneely committed May 30, 2019
1 parent 8d476df commit 0c34b90
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
54 changes: 25 additions & 29 deletions lib/squib/graphics/save_images.rb
Expand Up @@ -4,66 +4,62 @@ class Card
# :nodoc:
# @api private
def save_png(batch, scale_box)
surface = if preprocess_save?(batch, scale_box)
w, h = compute_dimensions(scale_box, batch.rotate, batch.trim)
preprocessed_save(w, h, batch, scale_box)
surface = if preprocess_save?(batch)
w, h = compute_dimensions(batch.rotate, batch.trim)
preprocessed_save(w, h, batch)
else
@cairo_surface
end
surface = scaled_surface(surface, scale_box) if scaling?(scale_box)
write_png(surface, index, batch.dir, batch.prefix, batch.count_format)
end

# :nodoc:
# @api private
def preprocess_save?(batch, box)
box.width != @width ||
box.height != @height ||
batch.rotate != false ||
batch.trim > 0
def preprocess_save?(batch)
batch.rotate != false || batch.trim > 0
end

def compute_dimensions(box, rotate, trim)
def scaling?(box)
box.width != @width || box.height != @height
end

def compute_dimensions(rotate, trim)
if rotate
[ @height - 2 * trim, @width - 2 * trim ]
else
[ @width - 2 * trim, @height - 2 * trim ]
end
end

def preprocessed_save(trimmed_w, trimmed_h, batch, scale_box)
scale_w = scale_box.width.to_f / trimmed_w
scale_h = scale_box.height.to_f / trimmed_h
w = if scale_box.width == @width
trimmed_w
else # user specified a width
scale_box.width
end
h = if scale_box.height == @height # specified height
trimmed_h
else # user specified a width
scale_box.height
end
def preprocessed_save(w, h, batch)
new_cc = Cairo::Context.new(Cairo::ImageSurface.new(w, h))
new_cc.scale(scale_w, scale_h) # scale back
trim_radius = batch.trim_radius
if batch.rotate != false
new_cc.translate(w * 0.5, h * 0.5)
new_cc.rotate(batch.angle)
new_cc.translate(h * -0.5, w * -0.5)
new_cc.rounded_rectangle(0, 0,
trimmed_h, trimmed_w,
trim_radius, trim_radius)
new_cc.rounded_rectangle(0, 0, h, w, trim_radius, trim_radius)
else
new_cc.rounded_rectangle(0, 0,
trimmed_w, trimmed_h,
trim_radius, trim_radius)
new_cc.rounded_rectangle(0, 0, w, h, trim_radius, trim_radius)
end
new_cc.clip
new_cc.set_source(@cairo_surface, -batch.trim, -batch.trim)
new_cc.paint
return new_cc.target
end

def scaled_surface(surface, box)
new_cc = Cairo::Context.new(Cairo::ImageSurface.new(box.width, box.height))
new_cc.scale(
box.width.to_f / surface.width,
box.height.to_f / surface.height
)
new_cc.set_source(surface)
new_cc.paint
return new_cc.target
end

def write_png(surface, i, dir, prefix, count_format)
surface.write_to_png("#{dir}/#{prefix}#{count_format % i}.png")
end
Expand Down
6 changes: 4 additions & 2 deletions samples/saves/_save_scaled.rb
Expand Up @@ -3,14 +3,16 @@

Squib::Deck.new(width: 50, height: 20) do
background color: :gray
text str: 'Text!', font: 'Sans 6'
text str: 'Text!', font: 'Sans 5'

save_png prefix: 'save_png_no_scaled_'
save_png prefix: 'save_png_no_scaled_trimmed_', trim: 5, trim_radius: 5
# save_png supports scaling to a different size
save_png width: 100, height: 40, prefix: 'save_png_scaled_'
save_png width: 100, height: 40, trim: 5, trim_radius: 5,
prefix: 'save_png_scaled_trimmed_'
save_png width: 100, height: 40, trim: 5, trim_radius: 5,
save_png width: 40, height: 100, trim: 5, trim_radius: 5,
rotate: :clockwise, prefix: 'save_png_scaled_trimmed_rotated_'
save_png width: 40, height: 100,
rotate: :clockwise, prefix: 'save_png_scaled_rotated_'
end

0 comments on commit 0c34b90

Please sign in to comment.