Class: CooCoo::DataSources::Xournal::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/coo-coo/data_sources/xournal/renderer.rb

Instance Method Summary collapse

Constructor Details

#initializeRenderer

Returns a new instance of Renderer.



9
10
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 9

def initialize()
end

Instance Method Details

#chunky_color(color) ⇒ Object



44
45
46
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 44

def chunky_color(color)
  color && ChunkyPNG::Color.parse(color)
end

#render(*args) ⇒ Object



12
13
14
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 12

def render(*args)
  render_to_chunky(*args)
end

#render_background(canvas, bg, min_x, min_y, max_x, max_y, zx, zy) ⇒ Object



55
56
57
58
59
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 55

def render_background(canvas, bg, min_x, min_y, max_x, max_y, zx, zy)
  color = chunky_color(bg.color || :white)
  canvas.stroke_color = canvas.fill_color = color
  canvas.rect(0, 0, ((max_x - min_x) * zx).to_i, ((max_y - min_y) * zy).to_i)
end

#render_image(canvas, src, min_x, min_y, zx, zy) ⇒ Object



72
73
74
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 72

def render_image(canvas, src, min_x, min_y, zx, zy)
  canvas.blit(src.raw_data, ((src.left - min_x) * zx), ((src.top - min_y) * zy), src.width * zx, src.height * zy)
end

#render_layer(canvas, layer, min_x, min_y, max_x, max_y, zx, zy) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 61

def render_layer(canvas, layer, min_x, min_y, max_x, max_y, zx, zy)
  layer.each do |child|
    #next unless child.within?(min_x, min_y, max_x, max_y)
    case child
    when Image then render_image(canvas, child, min_x, min_y, zx, zy)
    when Stroke then render_stroke(canvas, child, min_x, min_y, max_x, max_y, zx, zy)
    when Text then render_text(canvas, child, min_x, min_y, zx, zy)
    end
  end
end

#render_page(canvas, page, min_x, min_y, max_x, max_y, zx, zy) ⇒ Object



48
49
50
51
52
53
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 48

def render_page(canvas, page, min_x, min_y, max_x, max_y, zx, zy)
  render_background(canvas, page.background, min_x, min_y, max_x, max_y, zx, zy)
  page.each_layer do |layer|
    render_layer(canvas, layer, min_x, min_y, max_x, max_y, zx, zy)
  end
end

#render_stroke(canvas, stroke, min_x, min_y, max_x, max_y, zx, zy) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 76

def render_stroke(canvas, stroke, min_x, min_y, max_x, max_y, zx, zy)
  points = stroke.each_sample.inject([]) do |acc, sample|
    #next unless sample.within?(min_x, min_y, max_x, max_y)
    acc << [ (sample.x - min_x) * zx,
             (sample.y - min_y) * zy,
             sample.width * zx
           ]
  end

  canvas.stroke_color = chunky_color(stroke.color)
  canvas.fill_color = chunky_color(stroke.color)
  canvas.stroke(points)
end

#render_text(canvas, text, min_x, min_y, zx, zy) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 90

def render_text(canvas, text, min_x, min_y, zx, zy)
  canvas.fill_color = chunky_color(text.color)
  canvas.text(text.text,
              (text.x - min_x) * zx,
              (text.y - min_y) * zy,
              text.font,
              text.size * zy)
end

#render_to_cairo(document, page_num, x = 0, y = 0, w = nil, h = nil, zx = 1.0, zy = 1.0) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 34

def render_to_cairo(document, page_num, x = 0, y = 0, w = nil, h = nil, zx = 1.0, zy = 1.0)
  page = document.pages[page_num]
  w ||= (page.width - x).ceil.to_i
  h ||= (page.height - y).ceil.to_i
  surface = Cairo::ImageSurface.new((w * zx).to_i, (h * zy).to_i)
  canvas = Drawing::CairoCanvas.new(surface)
  render_to_canvas(canvas, document, page_num, x, y, w, h, zx, zy)
  surface
end

#render_to_canvas(canvas, document, page_num, x = 0, y = 0, w = nil, h = nil, zx = 1.0, zy = 1.0) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 16

def render_to_canvas(canvas, document, page_num, x = 0, y = 0, w = nil, h = nil, zx = 1.0, zy = 1.0)
  page = document.pages[page_num]
  w ||= (page.width - x).ceil.to_i
  h ||= (page.height - y).ceil.to_i
  render_page(canvas, page, x, y, x + w, y + h, zx, zy)
  canvas
end

#render_to_chunky(document, page_num, x = 0, y = 0, w = nil, h = nil, zx = 1.0, zy = 1.0) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/coo-coo/data_sources/xournal/renderer.rb', line 24

def render_to_chunky(document, page_num, x = 0, y = 0, w = nil, h = nil, zx = 1.0, zy = 1.0)
  page = document.pages[page_num]
  w ||= (page.width - x).ceil.to_i
  h ||= (page.height - y).ceil.to_i
  img = ChunkyPNG::Image.new((w * zx).to_i, (h * zy).to_i, chunky_color(page.background.color || :white))
  canvas = Drawing::ChunkyCanvas.new(img)
  render_to_canvas(canvas, document, page_num, x, y, w, h, zx, zy)
  img
end