class CooCoo::DataSources::Xournal::Saver
Saves a {Document} @todo Keep linked images with the Xournal
when it is saved.
Public Class Methods
new()
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 18 def initialize end
save(doc, io_or_path)
click to toggle source
Saves doc
to io_or_path
using Xournal's compressed XML format. @param io_or_path [String, IO] File
name or an IO
# File lib/coo-coo/data_sources/xournal/saver.rb, line 9 def self.save(doc, io_or_path) new.save(doc, io_or_path) end
to_xml(doc)
click to toggle source
Saves doc
to an XML string.
# File lib/coo-coo/data_sources/xournal/saver.rb, line 14 def self.to_xml(doc) new.to_xml(doc) end
Public Instance Methods
save(doc, io_or_path)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 21 def save(doc, io_or_path) if io_or_path.respond_to?(:write) save_to_io(doc, io_or_path) elsif io_or_path.kind_of?(String) save_to_file(doc, io_or_path) else raise ArgumentError.new("Only paths as String and IO are supported outputs. Not #{io_or_path.class}") end end
to_xml(doc)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 31 def to_xml(doc) Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| xml.xournal(version: doc.version) do xml.title(doc.title) doc.pages.each do |p| page_to_xml(p, xml) end end end.to_xml end
Protected Instance Methods
background_to_xml(bg, xml)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 63 def background_to_xml(bg, xml) case bg when PixmapBackground then xml.background(type: 'pixmap', domain: bg.domain, filename: bg.filename) when PDFBackground then xml.background(type: 'pdf', domain: bg.domain, filename: bg.filename, pageno: bg.page_number) else xml.background(type: 'solid', color: bg.color, style: bg.style) end end
image_to_xml(img, xml)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 84 def image_to_xml(img, xml) xml.image(img.data_string, left: img.left, top: img.top, right: img.right, bottom: img.bottom) end
layer_to_xml(layer, xml)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 71 def layer_to_xml(layer, xml) xml.layer do layer.each do |child| case child when Image then image_to_xml(child, xml) when Stroke then stroke_to_xml(child, xml) when Text then text_to_xml(child, xml) else raise ParseError.new("Unknown layer child: #{child.class} #{child.inspect}") end end end end
page_to_xml(p, xml)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 54 def page_to_xml(p, xml) xml.page(width: p.width, height: p.height) do background_to_xml(p.background || Background::Default, xml) p.layers.each do |l| layer_to_xml(l, xml) end end end
save_to_file(doc, path)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 44 def save_to_file(doc, path) Zlib::GzipWriter.open(path) do |f| save_to_io(doc, f) end end
save_to_io(doc, io)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 50 def save_to_io(doc, io) io.write(to_xml(doc)) end
stroke_to_xml(stroke, xml)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 88 def stroke_to_xml(stroke, xml) xml.stroke(stroke.samples.collect { |s| [ s.x, s.y ] }.flatten.join(' '), tool: stroke.tool, color: stroke.color, width: stroke.samples.collect(&:width).join(' ')) end
text_to_xml(text, xml)
click to toggle source
# File lib/coo-coo/data_sources/xournal/saver.rb, line 93 def text_to_xml(text, xml) xml.text_(text.text, x: text.x, y: text.y, size: text.size, color: text.color, font: text.font) end