Class: CooCoo::DataSources::Xournal::TrainingDocument::DocumentMaker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(training_doc, columns, rows, cells_per_example, page_width, page_height) ⇒ DocumentMaker

Returns a new instance of DocumentMaker.



14
15
16
17
18
19
20
21
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 14

def initialize(training_doc, columns, rows, cells_per_example, page_width, page_height)
  @doc = training_doc
  @columns = columns
  @rows = rows
  @cells_per_example = cells_per_example
  @page_width = page_width
  @page_height = page_height
end

Instance Attribute Details

#cells_per_exampleObject (readonly)

Returns the value of attribute cells_per_example.



10
11
12
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 10

def cells_per_example
  @cells_per_example
end

#columnsObject (readonly)

Returns the value of attribute columns.



11
12
13
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 11

def columns
  @columns
end

#page_heightObject (readonly)

Returns the value of attribute page_height.



9
10
11
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 9

def page_height
  @page_height
end

#page_widthObject (readonly)

Returns the value of attribute page_width.



8
9
10
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 8

def page_width
  @page_width
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

Instance Method Details

#make_cell(layer, label, strokes, x, y, grid_w, grid_h) ⇒ Object



33
34
35
36
37
38
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 33

def make_cell(layer, label, strokes, x, y, grid_w, grid_h)
  layer.add_text(Text.new(label, x + 1, y + 1, 12, 'black', 'Serif'))
  strokes.each do |stroke|
    layer.add_stroke(stroke.scale(grid_w, grid_h, grid_w).translate(x, y))
  end
end

#make_cells(examples, grid_w, grid_h) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 40

def make_cells(examples, grid_w, grid_h)
  layer = Layer.new

  examples = examples.collect { |e|
    e.each_set.collect { |s|
      [ e.label, s ]
    } + Array.new(@cells_per_example - e.size, [ e.label, [] ])
  }.flatten(1)

  examples.each_slice(@columns).with_index do |row, y|
    row.each_with_index do |(label, strokes), x|
      make_cell(layer, label, strokes, x * grid_w, y * grid_h, grid_w, grid_h)
    end
  end

  layer
end

#make_documentObject



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

def make_document
  Document.new do |d|
    @doc.each_example.each_slice(@columns * @rows / @cells_per_example).with_index do |labels, page_num|
      d.add_page(make_page(labels, page_num))
    end

    d.pages.first.layers.last.add_text(Text.new("#{META_LABEL} #{VERSION}: #{@columns} #{@rows} #{@cells_per_example}", 0, @page_height - 14, 12, 'gray'))
  end
end

#make_grid(grid_w, grid_h) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 58

def make_grid(grid_w, grid_h)
  grid_layer = Layer.new

  (1...@rows).each do |y|
    (1...@columns).each do |x|
      grid_layer.add_stroke(Stroke.new('pen', GRID_COLOR).
                            add_sample(x * grid_w, 0).
                            add_sample(x * grid_w, @page_height))
    end

    grid_layer.add_stroke(Stroke.new('pen', GRID_COLOR).
                          add_sample(0, y * grid_h).
                          add_sample(@page_width, y * grid_h))
  end

  grid_layer
end

#make_page(examples, page_num) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/coo-coo/data_sources/xournal/training_document/document_maker.rb', line 76

def make_page(examples, page_num)
  grid_w = @page_width / @columns
  grid_h = @page_height / @rows
  
  Page.new(@page_width, @page_height).
    add_layer(make_grid(grid_w, grid_h)).
    add_layer(make_cells(examples, grid_w, grid_h))
end