Merging in svg back end work. All but save_doc is done.

This commit is contained in:
Andy Meneely
2015-02-20 22:56:07 -05:00
parent d030bdd7f9
commit 8876b27473
26 changed files with 278 additions and 217 deletions
+20 -3
View File
@@ -17,12 +17,29 @@ module Squib
# :nodoc:
# @api private
def initialize(deck, width, height)
@deck=deck; @width=width; @height=height
@cairo_surface = Cairo::ImageSurface.new(width,height)
def initialize(deck, width, height, backend=:memory, index=-1)
@deck = deck
@width = width
@height = height
@svgfile = "#{deck.dir}/#{deck.prefix}#{deck.count_format % index}.svg"
@cairo_surface = make_surface(@svgfile, backend)
@cairo_context = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(@cairo_surface))
end
def make_surface(svgfile, backend)
case backend
when :memory
Cairo::ImageSurface.new(@width, @height)
when :svg
Dir.mkdir @deck.dir unless Dir.exists?(@deck.dir)
Cairo::SVGSurface.new(svgfile, @width, @height)
else
Squib.logger.fatal "Back end not recognized: '#{backend}'"
abort
end
end
# A save/restore wrapper for using Cairo
# :nodoc:
# @api private
+7 -4
View File
@@ -64,10 +64,13 @@ module Squib
# @api public
CONFIG_DEFAULTS = {
'custom_colors' => {},
'dpi' => 300,
'hint' => :none,
'progress_bar' => false,
'img_dir' => '.',
'dpi' => 300,
'hint' => :none,
'progress_bar' => false,
'img_dir' => '.',
'dir' => SYSTEM_DEFAULTS[:dir],
'backend' => 'memory',
'count_format' => SYSTEM_DEFAULTS[:count_format]
}
# These are parameters that are intended to be "expanded" across
+21 -10
View File
@@ -36,6 +36,8 @@ module Squib
# @api private
attr_reader :layout, :config
attr_reader :dir, :prefix, :count_format
# Squib's constructor that sets the immutable properties.
#
# This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
@@ -57,17 +59,21 @@ module Squib
# @api public
def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
@dpi = dpi
@width = Args::UnitConversion.parse width, dpi
@height = Args::UnitConversion.parse height, dpi
@font = Squib::SYSTEM_DEFAULTS[:default_font]
@font = SYSTEM_DEFAULTS[:default_font]
@cards = []
@custom_colors = {}
@img_dir = '.'
@progress_bar = Squib::Progress.new(false)
@progress_bar = Progress.new(false)
@text_hint = :off
cards.times{ @cards << Squib::Card.new(self, @width, @height) }
@backend = :memory
@dir = SYSTEM_DEFAULTS[:dir]
@prefix = SYSTEM_DEFAULTS[:prefix]
@count_format = SYSTEM_DEFAULTS[:count_format]
show_info(config, layout)
load_config(config)
@width = Args::UnitConversion.parse width, dpi
@height = Args::UnitConversion.parse height, dpi
cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, @backend, i) }
@layout = LayoutParser.load_layout(layout)
if block_given?
instance_eval(&block) # here we go. wheeeee!
@@ -93,12 +99,16 @@ module Squib
def load_config(file)
if File.exists?(file) && config = YAML.load_file(file)
Squib::logger.info { " using config: #{file}" }
config = Squib::CONFIG_DEFAULTS.merge(config)
@dpi = config['dpi'].to_i
@text_hint = config['text_hint']
config = CONFIG_DEFAULTS.merge(config)
@dpi = config['dpi'].to_i
@text_hint = config['text_hint']
@progress_bar.enabled = config['progress_bars']
@custom_colors = config['custom_colors']
@img_dir = config['img_dir']
@custom_colors = config['custom_colors']
@img_dir = config['img_dir']
@backend = (config['backend'].to_s.downcase.strip == 'svg') ? :svg : :memory
@dir = config['dir']
@prefix = config['prefix']
@count_format = config['count_format']
end
end
@@ -108,6 +118,7 @@ module Squib
def show_info(config, layout)
Squib::logger.info "Squib v#{Squib::VERSION}"
Squib::logger.info " building #{@cards.size} #{@width}x#{@height} cards"
Squib::logger.info " using #{@backend}"
end
##################
+13 -12
View File
@@ -27,7 +27,7 @@ module Squib
cc.scale(width.to_f / png.width.to_f, height.to_f / png.height.to_f)
end
cc.rotate(angle)
cc.translate(-1 * x, -1 * y)
cc.translate(-x, -y)
cc.set_source(png, x, y)
cc.operator = blend unless blend == :none
if mask.nil?
@@ -44,24 +44,25 @@ module Squib
def svg(file, id, x, y, width, height, alpha, blend, angle, mask)
Squib.logger.debug {"Rendering: #{file}, id: #{id} @#{x},#{y} #{width}x#{height}, alpha: #{alpha}, blend: #{blend}, angle: #{angle}, mask: #{mask}"}
return if file.nil? or file.eql? ''
svg = RSVG::Handle.new_from_file(file)
width = svg.width if width == :native
height = svg.height if height == :native
tmp = Cairo::ImageSurface.new(width, height)
tmp_cc = Cairo::Context.new(tmp)
tmp_cc.scale(width.to_f / svg.width.to_f, height.to_f / svg.height.to_f)
tmp_cc.render_rsvg_handle(svg, id)
svg = RSVG::Handle.new_from_file(file)
width = svg.width if width == :native
height = svg.height if height == :native
scale_width = width.to_f / svg.width.to_f
scale_height = height.to_f / svg.height.to_f
use_cairo do |cc|
cc.translate(x, y)
cc.rotate(angle)
cc.translate(-1 * x, -1 * y)
cc.scale(scale_width, scale_height)
cc.operator = blend unless blend == :none
#FIXME Alpha is no longer used since we are not using cc.paint anymore
if mask.nil?
cc.set_source(tmp, x, y)
cc.paint(alpha)
cc.render_rsvg_handle(svg, id)
else
tmp = Cairo::ImageSurface.new(width / scale_width, height / scale_height)
tmp_cc = Cairo::Context.new(tmp)
tmp_cc.render_rsvg_handle(svg, id)
cc.set_source_squibcolor(mask)
cc.mask(tmp, x, y)
cc.mask(tmp, 0, 0)
end
end
end
+14 -8
View File
@@ -16,22 +16,28 @@ module Squib
# @return [nil]
# @api public
def save_pdf(opts = {})
opts = {width: 3300, height: 2550}.merge(opts)
p = needs(opts, [:range, :paper_width, :paper_height, :file_to_save, :creatable_dir, :margin, :gap, :trim])
cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", p[:width], p[:height]))
x = p[:margin]
y = p[:margin]
@progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar|
opts = {width: 3300, height: 2550}.merge(opts)
p = needs(opts, [:range, :paper_width, :paper_height, :file_to_save,
:creatable_dir, :margin, :gap, :trim])
width = p[:width]
height = p[:height]
file = "#{p[:dir]}/#{p[:file]}"
cc = Cairo::Context.new(Cairo::PDFSurface.new(file, width, height))
x = p[:margin]
y = p[:margin]
@progress_bar.start("Saving PDF to #{file}", p[:range].size) do |bar|
p[:range].each do |i|
surface = trim(@cards[i].cairo_surface, p[:trim], @width, @height)
cc.set_source(surface, x, y)
surface = @cards[i].cairo_surface
cc.set_source(surface, x, y)
cc.paint
bar.increment
x += surface.width + p[:gap]
if x > (p[:width] - surface.width - p[:margin])
if x > (width - surface.width - p[:margin])
x = p[:margin]
y += surface.height + p[:gap]
if y > (p[:height] - surface.height - p[:margin])
if y > (height - surface.height - p[:margin])
x = p[:margin]
y = p[:margin]
cc.show_page #next page
+6 -1
View File
@@ -17,4 +17,9 @@
#For reading image file command (e.g. png and svg), read from this directory instead
#img_dir: img-color
#img_dir: img-bw
#img_dir: img-bw
# Use a SVG cairo back end, instead of an in-memory buffer
# backend: :memory # default
# backend: :svg # slower, but can create scalable pdfs
# tmp_dir: _tmp # for SVG backend, you MUST specify a tmp_dir