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
8 changed files with 156 additions and 37 deletions

View File

@ -68,6 +68,14 @@ The following Card Size and Target settings set these to the following:
* PNG Export Width: 825
* PDF Card Size: 750x1050
* 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:
@ -75,6 +83,8 @@ Create a `Cardfile` in your working directory. It should look something like thi
@session.configure do |c|
# manipulate the data after reading from the spreadsheet
# c.post_read_data = proc { |data|
# data[:active_layers] << "My Cool Layer"
# data[:active_layers] << /a regular expression/i
# 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)
# 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"
end
```

View File

@ -74,10 +74,24 @@ MSG
ensure_tmp
@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
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
end
@ -87,12 +101,12 @@ MSG
def pdf
pngs
pdf_obj = context.session.pdf_class.new(card_size: context.session.pdf_card_size)
pdf_obj = session.pdf_class.new(card_size: session.pdf_card_size)
trimmed_pngs = Parallel.map(@exported_pngs) do |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
end
@ -102,37 +116,37 @@ MSG
page_count = trimmed_pngs.length / pdf_obj.cards_per_page
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}}
pages = Parallel.map(png_slices.each_with_index) do |files, page_index|
tmp_pdf_target = tmp_path.join("page%05d.pdf" % page_index)
files += Array.new(pdf_obj.cards_per_page - files.length, placeholder)
system %{montage -density #{context.session.pdf_dpi} -tile #{pdf_obj.montage_tiling} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_target}}
system %{montage -density #{session.pdf_dpi} -tile #{pdf_obj.montage_tiling} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_target}}
tmp_pdf_target
end
if context.session.card_back
tmp_target = tmp_target_for(context.session.card_back)
if session.card_back
tmp_target = tmp_target_for(session.card_back)
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 %{montage -density #{context.session.pdf_dpi} -geometry +0+0 #{Array.new(pdf_obj.cards_per_page, tmp_target).join(' ')} #{tmp_pdf_target}}
system %{convert #{session.card_back} -gravity Center -crop #{session.pdf_card_size}+0+0 +repage #{tmp_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 << tmp_pdf_target
end
end
Pathname(context.session.pdf_target).parent.mkpath
Pathname(session.pdf_target).parent.mkpath
if context.session.prepend_pdf
pages.unshift context.session.prepend_pdf
if session.prepend_pdf
pages.unshift session.prepend_pdf
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
no_tasks do
@ -156,6 +170,10 @@ MSG
def write_svgs
context.write_individual_files
end
def session
context.session
end
end
end
end

View File

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

View File

@ -1,4 +1,5 @@
require 'nokogiri'
require 'delegate'
module SVGGVS
class File
@ -58,12 +59,42 @@ module SVGGVS
target.children.each(&:remove)
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
new_target = source.dup
new_target[:id] = new_target[:id] + "_#{@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!

View File

@ -3,11 +3,12 @@ module SVGGVS
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 :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
@index = 0
@card_sheet_identifier = "Card Data"
@orientation = :portrait
end
def configure
@ -54,6 +55,27 @@ module SVGGVS
card_finished!
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)
data_source = DataSource.new(source)
@ -67,7 +89,7 @@ module SVGGVS
with_new_target do |target|
target.inject!
target.active_layers = card[:active_layers]
target.active_layers = ActiveLayerMatcher.new(card[:active_layers])
target.replacements = card[:replacements]
end
end
@ -93,7 +115,7 @@ module SVGGVS
:png_export_width => 600
}
},
:small_shard => {
:square_shard => {
:the_game_crafter => {
:pdf_card_size => '300x300',
:pdf_dpi => 300,

View File

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

View File

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

View File

@ -10,6 +10,10 @@
# prepend this PDF to the outputted PDF (useful for game rules)
# 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"
end