class CooCoo::DataSources::Xournal::Loader

Loads a {Document}.

Public Class Methods

from_file(path) click to toggle source

Loads a Xournal document from the file at path. @return [Document]

# File lib/coo-coo/data_sources/xournal/loader.rb, line 26
def self.from_file(path)
  doc = nil
  
  Zlib::GzipReader.open(path) do |io|
    doc = from_xml(io)
  end

  doc
rescue Zlib::GzipFile::Error
  from_regular_file(path)
end
from_xml(data) click to toggle source

Loads a {Document} from XML in a String.

# File lib/coo-coo/data_sources/xournal/loader.rb, line 39
def self.from_xml(data)
  xml = Nokogiri::XML(data)
  root = xml.xpath('//xournal')[0]
  raise ParseError.new("XML root is not 'xournal'") unless root
  title_el = root.xpath("title")
  title = title_el[0].text if title_el.size > 0
  
  self.
    new(Document.new(title, root['version'])).
    from_xml(xml)
end
new(doc) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 20
def initialize(doc)
  @doc = doc || Document.new
end

Protected Class Methods

from_regular_file(path) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 61
def self.from_regular_file(path)
  File.open(path, 'rb') do |f|
    from_xml(f)
  end
end

Public Instance Methods

from_xml(xml) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 51
def from_xml(xml)
  xml.xpath("//page").each do |page|
    @doc.add_page(load_page(page))
  end

  @doc
end

Protected Instance Methods

load_background(xml) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 81
def load_background(xml)
  case xml['type']
  when 'pixmap' then PixmapBackground.new(xml['filename'], xml['domain'])
  when 'pdf' then PDFBackground.new(xml['filename'], xml['pageno'], xml['domain'])
  when 'solid' then Background.new(xml['color'], xml['style'])
  else raise ParseError.new("Unknown background type #{xml['type']}: #{xml}")
  end
end
load_image(xml) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 105
def load_image(xml)
  Image.new(xml['left'],
            xml['top'],
            xml['right'],
            xml['bottom'],
            xml.text)
end
load_layer(xml) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 90
def load_layer(xml)
  layer = Layer.new

  xml.children.select(&:element?).each do |elem|
    case elem.name
    when 'stroke' then layer.add_stroke(load_stroke(elem))
    when 'text' then layer.add_text(load_text(elem))
    when 'image' then layer.add_image(load_image(elem))
    else raise ParseError.new("Unknown element: #{elem}")
    end
  end
  
  layer
end
load_page(xml) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 67
def load_page(xml)
  w = xml['width'].to_f
  h = xml['height'].to_f
  bg_xml = xml.xpath('background')
  bg = load_background(bg_xml[0]) if bg_xml[0]
  page = Page.new(w, h, bg)
  
  xml.xpath('layer').each do |layer|
    page.add_layer(load_layer(layer))
  end

  page
end
load_stroke(xml) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 122
def load_stroke(xml)
  tool = xml['tool']
  tool = Stroke::DefaultTool if tool == nil || tool.empty?
  color = xml['color']
  stroke = Stroke.new(tool, color)
  widths = xml['width'].split.collect(&:to_f)

  width = nil
  xml.children.to_s.
    split.
    collect(&:to_f).
    each_slice(2).
    zip(widths) do |(x, y), w|
    width ||= w if w
    stroke.add_sample(x, y, width)
  end

  stroke
end
load_text(xml) click to toggle source
# File lib/coo-coo/data_sources/xournal/loader.rb, line 113
def load_text(xml)
  Text.new(xml.text,
           xml['x'].to_f,
           xml['y'].to_f,
           xml['size'].to_f,
           xml['color'],
           xml['font'])
end