Compare commits

...

9 Commits

Author SHA1 Message Date
John Bintz
e7241ce174 Bump version. 2014-09-30 08:59:12 -04:00
John Bintz
1f293bcaa4 Add ability to use RegExp as an active layer name match. 2014-09-30 08:58:46 -04:00
John Bintz
9d78279d5e Cache more SVG lookup data. Much faster! 2014-09-30 08:56:59 -04:00
John Bintz
ff3d4ee51c Cache some sheet info for a small speedup while searching. 2014-09-30 08:55:21 -04:00
John Bintz
632be845cb Bump version 2014-06-23 21:57:37 -04:00
John Bintz
324503dc96 In this code we follow the Laws of Demeter!
context.session.? -> sesson
2014-06-23 21:54:10 -04:00
John Bintz
901b3b2d60 Add landscape card support.
Setting c.orientation to :landscape will rotate your exported Inkscape
output images 270 degrees, so that you can upload them to The Game
Crafter and create PnP PDFs and they're oriented correctly.
2014-06-23 21:45:57 -04:00
John Bintz
4a9879074a Bump version.
* Rework PDF generation internals
* Remove useless crop marks
* New part types
2014-06-23 20:18:22 -04:00
John Bintz
8f68703e5b Start reworking internals to support other Game Crafter part types. 2014-06-23 20:14:48 -04:00
15 changed files with 261 additions and 144 deletions

View File

@ -68,6 +68,14 @@ The following Card Size and Target settings set these to the following:
* PNG Export Width: 825 * PNG Export Width: 825
* PDF Card Size: 750x1050 * PDF Card Size: 750x1050
* PDF DPI: 300 * PDF DPI: 300
* Small Square Tile
* PNG Export Width: 600
* PDF Card Size: 675x675
* PDF DPI: 300
* Square Shard
* PNG Export Width: 225
* PDF Card Size: 300x300
* PDF DPI: 300
Create a `Cardfile` in your working directory. It should look something like this: Create a `Cardfile` in your working directory. It should look something like this:
@ -75,6 +83,8 @@ Create a `Cardfile` in your working directory. It should look something like thi
@session.configure do |c| @session.configure do |c|
# manipulate the data after reading from the spreadsheet # manipulate the data after reading from the spreadsheet
# c.post_read_data = proc { |data| # c.post_read_data = proc { |data|
# data[:active_layers] << "My Cool Layer"
# data[:active_layers] << /a regular expression/i
# data[:replacements]['Superpower Text'] << '!!' # data[:replacements]['Superpower Text'] << '!!'
# } # }
@ -84,6 +94,10 @@ Create a `Cardfile` in your working directory. It should look something like thi
# prepend this PDF to the outputted PDF (useful for game rules) # prepend this PDF to the outputted PDF (useful for game rules)
# c.prepend_pdf = "rules.pdf" # c.prepend_pdf = "rules.pdf"
# the cards are landscape, so rotate them counterclockwise
# after rendering in Inkscape
# c.orientation = :landscape
c.data_source = "data.ods" c.data_source = "data.ods"
end end
``` ```

View File

@ -74,69 +74,79 @@ MSG
ensure_tmp ensure_tmp
@exported_pngs = Parallel.map(context.individual_files.each_with_index) do |svg_file, index| @exported_pngs = Parallel.map(context.individual_files.each_with_index) do |svg_file, index|
target = Pathname(context.session.png_files_path % index) target = Pathname(session.png_files_path % index)
target.parent.mkpath target.parent.mkpath
system %{inkscape --export-area-page --export-png "#{target.expand_path}" --export-width #{context.session.png_export_width} --export-background="#ffffffff" "#{svg_file.expand_path}"} command = %{inkscape --export-area-page --export-png "#{target.expand_path}" --export-background="#ffffffff" }
case session.orientation
when :portrait
command += %{--export-width #{session.png_export_width}}
when :landscape
command += %{--export-height #{session.png_export_width}}
end
command += %{ "#{svg_file.expand_path}"}
system command
if session.orientation == :landscape
system %{convert -verbose "#{target.expand_path}" -rotate 270 "#{target.expand_path}"}
end
target target
end end
end end
CARDS_PER_PAGE = 9
desc "pdf", "Create PDF of card images" desc "pdf", "Create PDF of card images"
def pdf def pdf
pngs pngs
pdf_obj = session.pdf_class.new(card_size: session.pdf_card_size)
trimmed_pngs = Parallel.map(@exported_pngs) do |png| trimmed_pngs = Parallel.map(@exported_pngs) do |png|
tmp_target = tmp_target_for(png) tmp_target = tmp_target_for(png)
system %{convert #{png} -gravity Center -crop #{context.session.pdf_card_size}+0+0 +repage #{tmp_target}} system %{convert #{png} -gravity Center -crop #{session.pdf_card_size}+0+0 +repage #{tmp_target}}
tmp_target tmp_target
end end
png_slices = trimmed_pngs.each_slice(CARDS_PER_PAGE) png_slices = trimmed_pngs.each_slice(pdf_obj.cards_per_page)
page_count = trimmed_pngs.length / CARDS_PER_PAGE page_count = trimmed_pngs.length / pdf_obj.cards_per_page
placeholder = tmp_target_for("placeholder.png") placeholder = tmp_target_for("placeholder.png")
system %{convert -size #{context.session.pdf_card_size} xc:white #{placeholder}} system %{convert -size #{session.pdf_card_size} xc:white #{placeholder}}
pdf_obj = SVGGVS::PDF.new(card_size: context.session.pdf_card_size)
pages = Parallel.map(png_slices.each_with_index) do |files, page_index| pages = Parallel.map(png_slices.each_with_index) do |files, page_index|
tmp_pdf_png_target = tmp_path.join("page%05d.pdf" % page_index)
tmp_pdf_target = tmp_path.join("page%05d.pdf" % page_index) tmp_pdf_target = tmp_path.join("page%05d.pdf" % page_index)
files += Array.new(9 - files.length, placeholder) files += Array.new(pdf_obj.cards_per_page - files.length, placeholder)
system %{montage -density #{context.session.pdf_dpi} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_png_target}} system %{montage -density #{session.pdf_dpi} -tile #{pdf_obj.montage_tiling} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_target}}
system %{convert -density #{context.session.pdf_dpi} #{tmp_pdf_png_target} -bordercolor white -border #{SVGGVS::PDF.border_size} #{pdf_obj.generate_crop_mark_draws.join(' ')} #{tmp_pdf_target}}.tap { |o| p o }
tmp_pdf_target tmp_pdf_target
end end
if context.session.card_back if session.card_back
tmp_target = tmp_target_for(context.session.card_back) tmp_target = tmp_target_for(session.card_back)
tmp_pdf_target = tmp_path.join("backs.pdf") tmp_pdf_target = tmp_path.join("backs.pdf")
system %{convert #{context.session.card_back} -gravity Center -crop #{context.session.pdf_card_size}+0+0 +repage #{tmp_target}} system %{convert #{session.card_back} -gravity Center -crop #{session.pdf_card_size}+0+0 +repage #{tmp_target}}
system %{montage -density #{context.session.pdf_dpi} -geometry +0+0 #{Array.new(9, tmp_target).join(' ')} #{tmp_pdf_target}} system %{montage -density #{session.pdf_dpi} -geometry +0+0 #{Array.new(pdf_obj.cards_per_page, tmp_target).join(' ')} #{tmp_pdf_target}}
pages.length.times do |page| pages.length.times do |page|
pages << tmp_pdf_target pages << tmp_pdf_target
end end
end end
Pathname(context.session.pdf_target).parent.mkpath Pathname(session.pdf_target).parent.mkpath
if context.session.prepend_pdf if session.prepend_pdf
pages.unshift context.session.prepend_pdf pages.unshift session.prepend_pdf
end end
system "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=#{context.session.pdf_target} -dBATCH #{pages.join(" ")}" system "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=#{session.pdf_target} -dBATCH #{pages.join(" ")}"
end end
no_tasks do no_tasks do
@ -160,6 +170,10 @@ MSG
def write_svgs def write_svgs
context.write_individual_files context.write_individual_files
end end
def session
context.session
end
end end
end end
end end

View File

@ -3,7 +3,7 @@ require_relative './svggvs/target'
require_relative './svggvs/context' require_relative './svggvs/context'
require_relative './svggvs/session' require_relative './svggvs/session'
require_relative './svggvs/data_source' require_relative './svggvs/data_source'
require_relative './svggvs/pdf' require_relative './svggvs/page/base'
module SVGGVS module SVGGVS
end end

View File

@ -13,7 +13,7 @@ module SVGGVS
def settings def settings
settings = {} settings = {}
doc.each_with_pagename do |name, sheet| sheets.each do |name, sheet|
if name['SVGGVS Settings'] if name['SVGGVS Settings']
sheet.each do |setting, value| sheet.each do |setting, value|
settings[setting.spunderscore.to_sym] = value settings[setting.spunderscore.to_sym] = value
@ -24,8 +24,20 @@ module SVGGVS
settings settings
end end
def each_card(card_sheet_identifier) def sheets
return @sheets if @sheets
@sheets = []
doc.each_with_pagename do |name, sheet| doc.each_with_pagename do |name, sheet|
@sheets << [ name, sheet.dup ]
end
@sheets
end
def each_card(card_sheet_identifier)
sheets.each do |name, sheet|
if name[card_sheet_identifier] if name[card_sheet_identifier]
headers = sheet.row(1) headers = sheet.row(1)

View File

@ -1,4 +1,5 @@
require 'nokogiri' require 'nokogiri'
require 'delegate'
module SVGGVS module SVGGVS
class File class File
@ -58,12 +59,42 @@ module SVGGVS
target.children.each(&:remove) target.children.each(&:remove)
end end
class SVGCache < SimpleDelegator
def initialize(doc)
@doc = doc
end
def __getobj__
@doc
end
def is_clone_dup_type(href)
@is_clone_dup_type ||= {}
return @is_clone_dup_type[href] if @is_clone_dup_type[href] != nil
if source = css(href).first
if source.name == 'flowRoot' || source.name == 'text'
@is_clone_dup_type[href] = source
end
end
@is_clone_dup_type[href] ||= false
@is_clone_dup_type[href]
end
end
def svg_cache
@svg_cache ||= SVGCache.new(source)
end
def with_new_target def with_new_target
new_target = source.dup new_target = source.dup
new_target[:id] = new_target[:id] + "_#{@instance}" new_target[:id] = new_target[:id] + "_#{@instance}"
new_target['inkscape:label'] = new_target['inkscape:label'] + "_#{@instance}" new_target['inkscape:label'] = new_target['inkscape:label'] + "_#{@instance}"
target_obj = Target.new(new_target) target_obj = Target.new(new_target, cache: svg_cache)
reset_defs! reset_defs!

43
lib/svggvs/page/base.rb Normal file
View File

@ -0,0 +1,43 @@
module SVGGVS
module Page
class Base
def initialize(options)
@options = options
end
def cards_per_page
self.class::CARDS_X * self.class::CARDS_Y
end
def montage_tiling
[ self.class::CARDS_X, self.class::CARDS_Y ].join('x')
end
private
def card_width
card_size.first
end
def card_height
card_size.last
end
def page_height
card_height * cards_per_width
end
def page_width
card_width * cards_per_height
end
def card_size
@card_size ||= @options[:card_size].split('x').collect(&:to_i)
end
end
end
end
require_relative './letter/poker'
require_relative './letter/small_shard'
require_relative './letter/small_square_tile'

View File

@ -0,0 +1,13 @@
require 'svggvs/page/base'
module SVGGVS
module Page
module Letter
class Poker < SVGGVS::Page::Base
CARDS_X = 3
CARDS_Y = 3
end
end
end
end

View File

@ -0,0 +1,13 @@
require 'svggvs/page/base'
module SVGGVS
module Page
module Letter
class SmallShard < SVGGVS::Page::Base
CARDS_X = 11
CARDS_Y = 14
end
end
end
end

View File

@ -0,0 +1,13 @@
require 'svggvs/page/base'
module SVGGVS
module Page
module Letter
class SmallSquareTile < SVGGVS::Page::Base
CARDS_X = 4
CARDS_Y = 5
end
end
end
end

View File

@ -1,57 +1,5 @@
module SVGGVS module SVGGVS
CROP_MARK_SIZE = 20.freeze
class PDF class PDF
def initialize(options)
@options = options
end
def self.border_size
([ CROP_MARK_SIZE ] * 2).join('x')
end
def page_size_with_crop_marks
[ card_width * 3, card_height * 3 ].collect { |size| size + CROP_MARK_SIZE * 2 }.join('x')
end
def generate_crop_mark_directives
(0..3).collect { |index|
pos_x = CROP_MARK_SIZE + index * card_width
pos_y = CROP_MARK_SIZE + index * card_height
[ [ 0 ], [ CROP_MARK_SIZE + page_height ] ].collect { |size|
[ pos_x ] + size + [ pos_x, size.first + CROP_MARK_SIZE ]
} +
[ [ 0 ], [ CROP_MARK_SIZE + page_width ] ].collect { |size|
size + [ pos_y ] + [ size.first + CROP_MARK_SIZE, pos_y ]
}
}.flatten(1).collect { |sx, sy, ex, ey| "#{sx},#{sy} #{ex},#{ey}" }
end
def generate_crop_mark_draws
generate_crop_mark_directives.collect { |coords| %{-stroke black -strokewidth 3 -draw "line #{coords}"} }
end
private
def card_width
card_size.first
end
def card_height
card_size.last
end
def page_height
card_height * 3
end
def page_width
card_width * 3
end
def card_size
@card_size ||= @options[:card_size].split('x').collect(&:to_i)
end
end end
end end

View File

@ -3,11 +3,12 @@ module SVGGVS
attr_accessor :svg_source, :svg_merged_target, :individual_files_path, :on_card_finished attr_accessor :svg_source, :svg_merged_target, :individual_files_path, :on_card_finished
attr_accessor :png_files_path, :png_export_width, :pdf_card_size, :pdf_dpi attr_accessor :png_files_path, :png_export_width, :pdf_card_size, :pdf_dpi
attr_accessor :pdf_target, :card_back, :card_size, :target, :post_read_data attr_accessor :pdf_target, :card_back, :card_size, :target, :post_read_data
attr_accessor :card_sheet_identifier, :prepend_pdf attr_accessor :card_sheet_identifier, :prepend_pdf, :orientation
def initialize def initialize
@index = 0 @index = 0
@card_sheet_identifier = "Card Data" @card_sheet_identifier = "Card Data"
@orientation = :portrait
end end
def configure def configure
@ -54,6 +55,27 @@ module SVGGVS
card_finished! card_finished!
end end
class ActiveLayerMatcher < SimpleDelegator
def initialize(layers)
@layers = layers
end
def __getobj__
@layers
end
def include?(name)
@layers.any? { |layer|
case layer
when Regexp
layer =~ name
else
layer == name
end
}
end
end
def data_source=(source) def data_source=(source)
data_source = DataSource.new(source) data_source = DataSource.new(source)
@ -67,13 +89,17 @@ module SVGGVS
with_new_target do |target| with_new_target do |target|
target.inject! target.inject!
target.active_layers = card[:active_layers] target.active_layers = ActiveLayerMatcher.new(card[:active_layers])
target.replacements = card[:replacements] target.replacements = card[:replacements]
end end
end end
end end
end end
def pdf_class
@pdf_class ||= ("SVGGVS::Page::Letter::" + @card_size.spunderscore.camelize).constantize
end
EXPORT_DEFAULTS = { EXPORT_DEFAULTS = {
:poker => { :poker => {
:the_game_crafter => { :the_game_crafter => {
@ -81,7 +107,21 @@ module SVGGVS
:pdf_dpi => 300, :pdf_dpi => 300,
:png_export_width => 825 :png_export_width => 825
} }
},
:small_square_tile => {
:the_game_crafter => {
:pdf_card_size => '675x675',
:pdf_dpi => 300,
:png_export_width => 600
} }
},
:square_shard => {
:the_game_crafter => {
:pdf_card_size => '300x300',
:pdf_dpi => 300,
:png_export_width => 225
}
},
}.freeze }.freeze
end end
end end

View File

@ -4,8 +4,8 @@ module SVGGVS
class Target < SimpleDelegator class Target < SimpleDelegator
attr_reader :target attr_reader :target
def initialize(target) def initialize(target, options)
@target = target @target, @options = target, options
end end
def __getobj__ def __getobj__
@ -20,8 +20,16 @@ module SVGGVS
@injected_defs ||= {} @injected_defs ||= {}
end end
def file_layers
@file_layers ||= css("g[inkscape|groupmode='layer']")
end
def child_visible_layers
@child_visible_layers ||= file_layers.find_all { |layer| layer['inkscape:label'].include?('(child visible)') }
end
def inject! def inject!
css("g[inkscape|groupmode='layer']").each do |layer| file_layers.each do |layer|
if filename = layer['inkscape:label'][/inject (.*\.svg)/, 1] if filename = layer['inkscape:label'][/inject (.*\.svg)/, 1]
injected_sources[filename] ||= begin injected_sources[filename] ||= begin
data = Nokogiri::XML(::File.read(filename)) data = Nokogiri::XML(::File.read(filename))
@ -39,30 +47,36 @@ module SVGGVS
end end
def active_layers=(layers) def active_layers=(layers)
css("g[inkscape|groupmode='layer']").each do |layer| file_layers.each do |layer|
if layers.include?(layer['inkscape:label'])
layer['style'] = ''
current_parent = layer.parent
while current_parent && current_parent.name == "g"
current_parent['style'] = ''
current_parent = current_parent.parent
end
else
layer['style'] = if layer['inkscape:label'].include?('(visible)') layer['style'] = if layer['inkscape:label'].include?('(visible)')
'' ''
else else
'display:none' 'display:none'
end end
end end
file_layers.each do |layer|
if layers.include?(layer['inkscape:label'])
layer['style'] = ''
current_parent = layer.parent
while current_parent && current_parent.name == "g" && current_parent['style'] != ''
current_parent['style'] = ''
current_parent = current_parent.parent
end
layers.delete(layer)
end
break if layers.empty?
end end
loop do loop do
any_changed = false any_changed = false
css("g[inkscape|groupmode='layer']").each do |layer| child_visible_layers.each do |layer|
if layer['inkscape:label'].include?('(child visible)') && layer['style'] != '' && layer.parent['style'] == '' if layer['style'] != '' && layer.parent['style'] == ''
layer['style'] = '' layer['style'] = ''
any_changed = true any_changed = true
@ -106,11 +120,15 @@ module SVGGVS
end end
end end
def cache
@options[:cache]
end
# only uncloning text # only uncloning text
def unclone def unclone
css('svg|use').each do |clone| file_layers.find_all { |layer| layer['style'] == '' }.each do |layer|
if source = css(clone['xlink:href']).first layer.css('svg|use').each do |clone|
if source.name == 'flowRoot' || source.name == 'text' if source = cache.is_clone_dup_type(clone['xlink:href'])
new_group = clone.add_next_sibling("<g />").first new_group = clone.add_next_sibling("<g />").first
clone.attributes.each do |key, attribute| clone.attributes.each do |key, attribute|

View File

@ -1,3 +1,3 @@
module SVGGVS module SVGGVS
VERSION = "0.0.10.1" VERSION = "0.0.13"
end end

View File

@ -10,6 +10,10 @@
# prepend this PDF to the outputted PDF (useful for game rules) # prepend this PDF to the outputted PDF (useful for game rules)
# c.prepend_pdf = "rules.pdf" # c.prepend_pdf = "rules.pdf"
# the cards are landscape, so rotate them counterclockwise
# after rendering in Inkscape
# c.orientation = :landscape
c.data_source = "data.ods" c.data_source = "data.ods"
end end

View File

@ -1,46 +0,0 @@
require_relative '../spec_helper'
require 'svggvs/pdf'
require 'digest/md5'
describe SVGGVS::PDF do
subject { SVGGVS::PDF.new(options) }
let(:options) {
{ card_size: '100x100' }
}
describe '#page_size_with_crop_marks' do
it "should have the right size" do
subject.page_size_with_crop_marks.should be == "340x340"
end
end
describe '#generate_crop_mark_directives' do
let(:result) {
[
"20,0 20,20",
"20,320 20,340",
"120,0 120,20",
"120,320 120,340",
"220,0 220,20",
"220,320 220,340",
"320,0 320,20",
"320,20 340,20",
"320,120 340,120",
"320,220 340,220",
"320,320 320,340",
"320,320 340,320",
"0,20 20,20",
"0,120 20,120",
"0,220 20,220",
"0,320 20,320",
]
}
it 'should create correct definitions' do
subject.generate_crop_mark_directives.each { |coords|
result.should include(coords)
}
end
end
end