Compare commits
1 Commits
master
...
rework-pdf
Author | SHA1 | Date | |
---|---|---|---|
|
a1bf6936fd |
14
README.md
14
README.md
@ -68,14 +68,6 @@ 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:
|
||||||
|
|
||||||
@ -83,8 +75,6 @@ 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'] << '!!'
|
||||||
# }
|
# }
|
||||||
|
|
||||||
@ -94,10 +84,6 @@ 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
|
||||||
```
|
```
|
||||||
|
46
bin/svggvs
46
bin/svggvs
@ -74,24 +74,10 @@ 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(session.png_files_path % index)
|
target = Pathname(context.session.png_files_path % index)
|
||||||
target.parent.mkpath
|
target.parent.mkpath
|
||||||
|
|
||||||
command = %{inkscape --export-area-page --export-png "#{target.expand_path}" --export-background="#ffffffff" }
|
system %{inkscape --export-area-page --export-png "#{target.expand_path}" --export-width #{context.session.png_export_width} --export-background="#ffffffff" "#{svg_file.expand_path}"}
|
||||||
|
|
||||||
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
|
||||||
@ -101,12 +87,12 @@ MSG
|
|||||||
def pdf
|
def pdf
|
||||||
pngs
|
pngs
|
||||||
|
|
||||||
pdf_obj = session.pdf_class.new(card_size: session.pdf_card_size)
|
pdf_obj = context.session.pdf_class.new(card_size: context.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 #{session.pdf_card_size}+0+0 +repage #{tmp_target}}
|
system %{convert #{png} -gravity Center -crop #{context.session.pdf_card_size}+0+0 +repage #{tmp_target}}
|
||||||
|
|
||||||
tmp_target
|
tmp_target
|
||||||
end
|
end
|
||||||
@ -116,37 +102,37 @@ MSG
|
|||||||
page_count = trimmed_pngs.length / pdf_obj.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 #{session.pdf_card_size} xc:white #{placeholder}}
|
system %{convert -size #{context.session.pdf_card_size} xc:white #{placeholder}}
|
||||||
|
|
||||||
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_target = tmp_path.join("page%05d.pdf" % page_index)
|
tmp_pdf_target = tmp_path.join("page%05d.pdf" % page_index)
|
||||||
|
|
||||||
files += Array.new(pdf_obj.cards_per_page - files.length, placeholder)
|
files += Array.new(pdf_obj.cards_per_page - files.length, placeholder)
|
||||||
|
|
||||||
system %{montage -density #{session.pdf_dpi} -tile #{pdf_obj.montage_tiling} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_target}}
|
system %{montage -density #{context.session.pdf_dpi} -tile #{pdf_obj.montage_tiling} -geometry +0+0 #{files.join(' ')} #{tmp_pdf_target}}
|
||||||
|
|
||||||
tmp_pdf_target
|
tmp_pdf_target
|
||||||
end
|
end
|
||||||
|
|
||||||
if session.card_back
|
if context.session.card_back
|
||||||
tmp_target = tmp_target_for(session.card_back)
|
tmp_target = tmp_target_for(context.session.card_back)
|
||||||
tmp_pdf_target = tmp_path.join("backs.pdf")
|
tmp_pdf_target = tmp_path.join("backs.pdf")
|
||||||
|
|
||||||
system %{convert #{session.card_back} -gravity Center -crop #{session.pdf_card_size}+0+0 +repage #{tmp_target}}
|
system %{convert #{context.session.card_back} -gravity Center -crop #{context.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}}
|
system %{montage -density #{context.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(session.pdf_target).parent.mkpath
|
Pathname(context.session.pdf_target).parent.mkpath
|
||||||
|
|
||||||
if session.prepend_pdf
|
if context.session.prepend_pdf
|
||||||
pages.unshift session.prepend_pdf
|
pages.unshift context.session.prepend_pdf
|
||||||
end
|
end
|
||||||
|
|
||||||
system "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=#{session.pdf_target} -dBATCH #{pages.join(" ")}"
|
system "gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=#{context.session.pdf_target} -dBATCH #{pages.join(" ")}"
|
||||||
end
|
end
|
||||||
|
|
||||||
no_tasks do
|
no_tasks do
|
||||||
@ -170,10 +156,6 @@ 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
|
||||||
|
@ -13,7 +13,7 @@ module SVGGVS
|
|||||||
def settings
|
def settings
|
||||||
settings = {}
|
settings = {}
|
||||||
|
|
||||||
sheets.each do |name, sheet|
|
doc.each_with_pagename 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,20 +24,8 @@ module SVGGVS
|
|||||||
settings
|
settings
|
||||||
end
|
end
|
||||||
|
|
||||||
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)
|
def each_card(card_sheet_identifier)
|
||||||
sheets.each do |name, sheet|
|
doc.each_with_pagename do |name, sheet|
|
||||||
if name[card_sheet_identifier]
|
if name[card_sheet_identifier]
|
||||||
headers = sheet.row(1)
|
headers = sheet.row(1)
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
require 'nokogiri'
|
require 'nokogiri'
|
||||||
require 'delegate'
|
|
||||||
|
|
||||||
module SVGGVS
|
module SVGGVS
|
||||||
class File
|
class File
|
||||||
@ -59,42 +58,12 @@ 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, cache: svg_cache)
|
target_obj = Target.new(new_target)
|
||||||
|
|
||||||
reset_defs!
|
reset_defs!
|
||||||
|
|
||||||
|
@ -3,12 +3,11 @@ 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, :orientation
|
attr_accessor :card_sheet_identifier, :prepend_pdf
|
||||||
|
|
||||||
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
|
||||||
@ -55,27 +54,6 @@ 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)
|
||||||
|
|
||||||
@ -89,7 +67,7 @@ module SVGGVS
|
|||||||
|
|
||||||
with_new_target do |target|
|
with_new_target do |target|
|
||||||
target.inject!
|
target.inject!
|
||||||
target.active_layers = ActiveLayerMatcher.new(card[:active_layers])
|
target.active_layers = card[:active_layers]
|
||||||
target.replacements = card[:replacements]
|
target.replacements = card[:replacements]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -115,7 +93,7 @@ module SVGGVS
|
|||||||
:png_export_width => 600
|
:png_export_width => 600
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
:square_shard => {
|
:small_shard => {
|
||||||
:the_game_crafter => {
|
:the_game_crafter => {
|
||||||
:pdf_card_size => '300x300',
|
:pdf_card_size => '300x300',
|
||||||
:pdf_dpi => 300,
|
:pdf_dpi => 300,
|
||||||
|
@ -4,8 +4,8 @@ module SVGGVS
|
|||||||
class Target < SimpleDelegator
|
class Target < SimpleDelegator
|
||||||
attr_reader :target
|
attr_reader :target
|
||||||
|
|
||||||
def initialize(target, options)
|
def initialize(target)
|
||||||
@target, @options = target, options
|
@target = target
|
||||||
end
|
end
|
||||||
|
|
||||||
def __getobj__
|
def __getobj__
|
||||||
@ -20,16 +20,8 @@ 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!
|
||||||
file_layers.each do |layer|
|
css("g[inkscape|groupmode='layer']").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))
|
||||||
@ -47,36 +39,30 @@ module SVGGVS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def active_layers=(layers)
|
def active_layers=(layers)
|
||||||
file_layers.each do |layer|
|
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
|
||||||
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
|
||||||
child_visible_layers.each do |layer|
|
css("g[inkscape|groupmode='layer']").each do |layer|
|
||||||
if layer['style'] != '' && layer.parent['style'] == ''
|
if layer['inkscape:label'].include?('(child visible)') && layer['style'] != '' && layer.parent['style'] == ''
|
||||||
layer['style'] = ''
|
layer['style'] = ''
|
||||||
|
|
||||||
any_changed = true
|
any_changed = true
|
||||||
@ -120,15 +106,11 @@ module SVGGVS
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cache
|
|
||||||
@options[:cache]
|
|
||||||
end
|
|
||||||
|
|
||||||
# only uncloning text
|
# only uncloning text
|
||||||
def unclone
|
def unclone
|
||||||
file_layers.find_all { |layer| layer['style'] == '' }.each do |layer|
|
css('svg|use').each do |clone|
|
||||||
layer.css('svg|use').each do |clone|
|
if source = css(clone['xlink:href']).first
|
||||||
if source = cache.is_clone_dup_type(clone['xlink:href'])
|
if source.name == 'flowRoot' || source.name == 'text'
|
||||||
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|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
module SVGGVS
|
module SVGGVS
|
||||||
VERSION = "0.0.13"
|
VERSION = "0.0.10.1"
|
||||||
end
|
end
|
||||||
|
@ -10,10 +10,6 @@
|
|||||||
# 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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user