Module: JasperCommandLine::Prawn::JS

Defined in:
lib/jasper-command-line/prawn-js.rb

Constant Summary collapse

NAME_TREE_CHILDREN_LIMIT =

The maximum number of children to fit into a single node in the JavaScript tree.

20

Instance Method Summary collapse

Instance Method Details

#add_docopen_js(name, script) ⇒ Object

add a Javascript fragment that will execute when the document is opened.

There can only be as many fragments as required. Calling this function multiple times will append the new fragment to the list.



20
21
22
23
# File 'lib/jasper-command-line/prawn-js.rb', line 20

def add_docopen_js(name, script)
  obj = ref!(:S => :JavaScript, :JS => script)
  javascript.data.add(name, obj)
end

#interactive_js(auto) ⇒ Object



44
45
46
# File 'lib/jasper-command-line/prawn-js.rb', line 44

def interactive_js(auto)
  "pp.interactive = pp.constants.interactionLevel.silent;" if auto
end

#javascriptObject

create or access the Javascript Name Tree in the document names dict. See section 3.6.3 and table 3.28 in the PDF spec.



70
71
72
# File 'lib/jasper-command-line/prawn-js.rb', line 70

def javascript
  names.data[:JavaScript] ||= ref!(::Prawn::Core::NameTree::Node.new(self, NAME_TREE_CHILDREN_LIMIT))
end


25
26
27
# File 'lib/jasper-command-line/prawn-js.rb', line 25

def print
  print_with_auto_to_printer false, nil
end


29
30
31
# File 'lib/jasper-command-line/prawn-js.rb', line 29

def print_silent
  print_with_auto_to_printer true, nil
end


33
34
35
36
37
38
39
40
41
42
# File 'lib/jasper-command-line/prawn-js.rb', line 33

def print_with_auto_to_printer(auto, printer)
  js = [
    'var pp = this.getPrintParams();',
    interactive_js(auto),
    select_printer_js(printer),
    'this.print(pp);'
  ]

  add_docopen_js "print", js.join(' ')
end

#select_printer_js(printer) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jasper-command-line/prawn-js.rb', line 48

def select_printer_js(printer)
  if printer
    escaped_printer = printer.gsub('"') { "\\#$0" }

    [
      'var names = app.printerNames;',
      'var regex = new RegExp("#{escaped_printer}", "i");',
      'for (var i = 0; i < names.length; i++) {',
        'if (names[i].match(regex)) {',
          'pp.printerName = names[i];',
          'break;',
        '}',
      '}'
    ].join(' ')
  else
    'pp.printerName = "";'
  end
end