Stabilizing the code after the options switch. Getting there...

This commit is contained in:
Andy Meneely
2014-07-28 17:08:57 -04:00
parent e38b5dfdbe
commit f6480445df
11 changed files with 45 additions and 49 deletions
+7 -7
View File
@@ -1,21 +1,21 @@
module Squib
class Deck
def save_pdf(file: 'deck.pdf', dir: '_output', margin: 75, gap: 0, trim: 0)
dir = dirify(dir, allow_create: true)
def save_pdf(opts = {})
p = needs(opts, [:file_to_save, :prefix, :margin, :gap, :trim])
width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too
cc = Cairo::Context.new(Cairo::PDFSurface.new("#{dir}/#{file}", width, height))
x = margin ; y = margin
x = p[:margin] ; y = p[:margin]
@cards.each_with_index do |card, i|
surface = trim(card.cairo_surface, trim, @width, @height)
surface = trim(card.cairo_surface, p[:trim], @width, @height)
cc.set_source(surface, x, y)
cc.paint
x += surface.width + gap
if x > (width - surface.width - margin)
x = margin
y += surface.height + gap
x = p[:margin]
y += surface.height + p[:gap]
if y > (height - surface.height - margin)
x = margin ; y = margin
x = p[:margin] ; y = p[:margin]
cc.show_page #next page
end
end
+4 -13
View File
@@ -11,7 +11,7 @@ module Squib
w = layout.extents[1].width / Pango::SCALE if w < 0
h = layout.height / Pango::SCALE
h = layout.extents[1].height / Pango::SCALE if h < 0
draw_rectangle(x,y,w,h,0,0,'#0000',color, 2.0)
rect(x,y,w,h,0,0,'#0000',color, 2.0)
end
def ellipsize(layout, options)
@@ -65,16 +65,8 @@ module Squib
end
def setwh(layout, options)
layout.width = options[:width] * Pango::SCALE unless options[:width].nil?
layout.height = options[:height] * Pango::SCALE unless options[:height].nil?
layout
end
def fitxy(layout, x,y, options)
w = options[:width] ; h = options[:height]
w ||= (@width - 2*x); h ||= (@height - 2*y) # default centers to x,y
w *= Pango::SCALE ; h *= Pango::SCALE
layout.width=w ; layout.height=h
layout.width = options[:width] * Pango::SCALE unless options[:width].nil? || options[:width] == :native
layout.height = options[:height] * Pango::SCALE unless options[:height].nil? || options[:height] == :native
layout
end
@@ -86,8 +78,7 @@ module Squib
layout.font_description = Pango::FontDescription.new(font)
layout.text = str.to_s
layout.markup = str.to_s if options[:markup]
layout = setwh(layout, options) unless options[:width].nil? && options[:height].nil?
layout = fitxy(layout, x, y , options) if options[:fitxy]
layout = setwh(layout, options)
layout = wrap(layout, options)
layout = ellipsize(layout, options)
layout = align(layout, options)