Module: OBF::PDF

Defined in:
lib/obf/pdf.rb

Constant Summary collapse

RTL_SCRIPTS =
%w(Arabic Hebrew Nko Kharoshthi Phoenician Syriac Thaana Tifinagh Tamil)

Class Method Summary collapse

Class Method Details

.build_page(pdf, obj, options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/obf/pdf.rb', line 84

def self.build_page(pdf, obj, options)
  OBF::Utils.as_progress_percent(0, 1.0) do
    pdf.font(options['font']) if options['font'] && File.exists?(options['font'])
    doc_width = 11*72 - 72
    doc_height = 8.5*72 - 72
    default_radius = 3
    text_height = 20
    header_height = 0
  
    if options['pages']
      page_num = options['pages'][obj['id']]
      pdf.add_dest("page#{page_num}", pdf.dest_fit)
    end
    # header
    if !options['headerless']
      header_height = 100
      pdf.bounding_box([0, doc_height], :width => doc_width, :height => 100) do
        pdf.line_width = 2
        pdf.font_size 16
      
        pdf.fill_color "eeeeee"
        pdf.stroke_color "888888"
        pdf.fill_and_stroke_rounded_rectangle [0, 100], 100, 100, default_radius
        pdf.fill_color "6D81D1"
        pdf.fill_and_stroke_polygon([5, 50], [35, 85], [35, 70], [95, 70], [95, 30], [35, 30], [35, 15])
        pdf.fill_color "ffffff"
        pdf.formatted_text_box [{:text => "Go Back", :anchor => "page1"}], :at => [10, 90], :width => 80, :height => 80, :align => :center, :valign => :center, :overflow => :shrink_to_fit
        pdf.fill_color "ffffff"
        pdf.fill_and_stroke_rounded_rectangle [110, 100], (doc_width - 200 - 20), 100, default_radius
        pdf.fill_color "DDDB54"
        pdf.fill_and_stroke do
          pdf.move_to 160, 50
          pdf.line_to 190, 70
          pdf.curve_to [190, 30], :bounds => [[100, 130], [100, -30]]
          pdf.line_to 160, 50
        end
        pdf.fill_color "444444"
        pdf.text_box "Say that sentence out loud for me", :at => [210, 90], :width => (doc_width - 200 - 120), :height => 80, :align => :left, :valign => :center, :overflow => :shrink_to_fit
        pdf.fill_color "eeeeee"
        pdf.fill_and_stroke_rounded_rectangle [(doc_width - 100), 100], 100, 100, default_radius
        pdf.fill_color "aaaaaa"
        pdf.fill_and_stroke_polygon([doc_width - 100 + 5, 50], [doc_width - 100 + 35, 85], [doc_width - 100 + 95, 85], [doc_width - 100 + 95, 15], [doc_width - 100 + 35, 15])
        pdf.fill_color "ffffff"
        pdf.text_box "Erase", :at => [(doc_width - 100 + 10), 90], :width => 80, :height => 80, :align => :center, :valign => :center, :overflow => :shrink_to_fit
      end
    end
  
    # board
    pdf.font_size 12
    padding = 10
    grid_height = doc_height - header_height - text_height - (padding * 2)
    grid_width = doc_width
    if obj['grid'] && obj['grid']['rows'] > 0 && obj['grid']['columns'] > 0
      button_height = (grid_height - (padding * (obj['grid']['rows'] - 1))) / obj['grid']['rows'].to_f
      button_width = (grid_width - (padding * (obj['grid']['columns'] - 1))) / obj['grid']['columns'].to_f
      obj['grid']['order'].each_with_index do |buttons, row|
        buttons.each_with_index do |button_id, col|
          button = obj['buttons'].detect{|b| b['id'] == button_id }
          next if !button || button['hidden'] == true
          x = (padding * col) + (col * button_width)
          y = text_height + padding - (padding * row) + grid_height - (row * button_height)
          pdf.bounding_box([x, y], :width => button_width, :height => button_height) do
            fill = "ffffff"
            border = "eeeeee"
            if button['background_color']
              fill = OBF::Utils.fix_color(button['background_color'], 'hex')
            end   
            if button['border_color']
              border = OBF::Utils.fix_color(button['border_color'], 'hex')
            end         
            pdf.fill_color fill
            pdf.stroke_color border
            pdf.fill_and_stroke_rounded_rectangle [0, button_height], button_width, button_height, default_radius
            vertical = options['text_on_top'] ? button_height - text_height : button_height - 5

            text = (button['label'] || button['vocalization']).to_s
            font = options['font']
            if text.match(/\p{Thai}/)
              font = File.expand_path('../../THFahKwangBold.ttf', __FILE__)
            end
            pdf.font(font) if font && File.exists?(font)
            direction = text.match(rtl_regex) ? :rtl : :ltr
            if options['text_case'] == 'upper'
              text = text.upcase
            elsif options['text_case'] == 'lower'
              text = text.downcase
            end
            
            if options['text_only']
              # render text
              pdf.fill_color "000000"
              pdf.text_box text, :at => [0, 0], :width => button_width, :height => button_height, :align => :center, :valign => :center, :overflow => :shrink_to_fit, :direction => direction
            else
              # render image
              pdf.bounding_box([5, vertical], :width => button_width - 10, :height => button_height - text_height - 5) do
                image = (obj['images_hash'] || {})[button['image_id']]
                if image
                  bg = 'white'
                  if options['transparent_background']
                    bg = "\##{fill}"
                  end
                  image_local_path = image && OBF::Utils.save_image(image, options['zipper'], bg)
                  if image_local_path && File.exist?(image_local_path)
                    pdf.image image_local_path, :fit => [button_width - 10, button_height - text_height - 5], :position => :center, :vposition => :center
                    File.unlink image_local_path
                  end
                end
              end
              if options['pages'] && button['load_board']
                page = options['pages'][button['load_board']['id']]
                if page
                  page_vertical = options['text_on_top'] ? -2 + text_height : button_height + 2
                  pdf.fill_color "ffffff"            
                  pdf.stroke_color "eeeeee"            
                  pdf.fill_and_stroke_rounded_rectangle [button_width - 18, page_vertical], 20, text_height, 5
                  pdf.fill_color "000000"
                  pdf.formatted_text_box [{:text => page, :anchor => "page#{page}"}], :at => [button_width - 18, page_vertical], :width => 20, :height => text_height, :align => :center, :valign => :center
                end
              end
            
              # render text
              pdf.fill_color "000000"
              vertical = options['text_on_top'] ? button_height : text_height
              pdf.text_box text, :at => [0, vertical], :width => button_width, :height => text_height, :align => :center, :valign => :center, :overflow => :shrink_to_fit, :direction => direction
            end
            pdf.font(options['font']) if options['font'] && File.exists?(options['font'])
          end
          index = col + (row * obj['grid']['columns'])
          OBF::Utils.update_current_progress(index.to_f / (obj['grid']['rows'] * obj['grid']['columns']).to_f)
        end
      end
    end
  
    # footer
    pdf.fill_color "aaaaaa"
    if OBF::PDF.footer_text
      text = OBF::PDF.footer_text
      pdf.formatted_text_box [{:text => text, :link => OBF::PDF.footer_url}], :at => [doc_width - 300, text_height], :width => 200, :height => text_height, :align => :right, :valign => :center, :overflow => :shrink_to_fit
    end
    pdf.fill_color "000000"
    if options['pages']
      pdf.formatted_text_box [{:text => options['pages'][obj['id']], :anchor => "page1"}], :at => [doc_width - 100, text_height], :width => 100, :height => text_height, :align => :right, :valign => :center, :overflow => :shrink_to_fit
    end
  end
end

.build_pdf(obj, dest_path, zipper, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/obf/pdf.rb', line 34

def self.build_pdf(obj, dest_path, zipper, opts={})
  OBF::Utils.as_progress_percent(0, 1.0) do
    # parse obf, draw as pdf
    pdf = Prawn::Document.new(
      :page_layout => :landscape, 
      :page_size => [8.5*72, 11*72],
      :info => {
        :Title => obj['name']
      }
    )
    font = opts['font'] if opts['font'] && File.exists?(opts['font'])
    font ||= File.expand_path('../../TimesNewRoman.ttf', __FILE__)
    pdf.font(font) if File.exists?(font)
  
  
    if obj['boards']
      obj['boards'].each_with_index do |board, idx|
        pre = idx.to_f / obj['boards'].length.to_f
        post = (idx + 1).to_f / obj['boards'].length.to_f
        OBF::Utils.as_progress_percent(pre, post) do
          pdf.start_new_page unless idx == 0
          build_page(pdf, board, {
            'zipper' => zipper, 
            'pages' => obj['pages'], 
            'headerless' => !!opts['headerless'], 
            'font' => font,
            'text_on_top' => !!opts['text_on_top'], 
            'transparent_background' => !!opts['transparent_background'],
            'text_case' => opts['text_case']
          })
        end
      end
    else
      build_page(pdf, obj, {
        'headerless' => !!opts['headerless'], 
        'font' => font,
        'text_on_top' => !!opts['text_on_top'], 
        'transparent_background' => !!opts['transparent_background'],
        'text_case' => opts['text_case']
      })
    end
  
    pdf.render_file(dest_path)
  end
end


7
8
9
# File 'lib/obf/pdf.rb', line 7

def self.footer_text
  @@footer_text
end


11
12
13
# File 'lib/obf/pdf.rb', line 11

def self.footer_text=(text)
  @@footer_text = text
end


15
16
17
# File 'lib/obf/pdf.rb', line 15

def self.footer_url
  @@footer_url
end


19
20
21
# File 'lib/obf/pdf.rb', line 19

def self.footer_url=(url)
  @@footer_url = url
end

.from_external(content, dest_path) ⇒ Object



277
278
279
280
281
282
283
284
285
286
# File 'lib/obf/pdf.rb', line 277

def self.from_external(content, dest_path)
  tmp_path = OBF::Utils.temp_path("stash")
  if content['boards']
    from_obz(OBF::OBZ.from_external(content, tmp_path), dest_path)
  else
    from_obf(OBF::OBF.from_external(content, tmp_path), dest_path)
  end
  File.unlink(tmp_path) if File.exist?(tmp_path)
  dest_path
end

.from_obf(obf_json_or_path, dest_path, zipper = nil, opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/obf/pdf.rb', line 23

def self.from_obf(obf_json_or_path, dest_path, zipper=nil, opts={})
  obj = obf_json_or_path
  if obj.is_a?(String)
    obj = OBF::Utils.parse_obf(File.read(obf_json_or_path))
  else
    obj = OBF::Utils.parse_obf(obf_json_or_path)
  end
  build_pdf(obj, dest_path, zipper, opts)
  return dest_path
end

.from_obz(obz_path, dest_path, opts = {}) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/obf/pdf.rb', line 230

def self.from_obz(obz_path, dest_path, opts={})
  OBF::Utils.load_zip(obz_path) do |zipper|
    manifest = JSON.parse(zipper.read('manifest.json'))
    root = manifest['root']
    board = OBF::Utils.parse_obf(zipper.read(root))
    board['path'] = root
    unvisited_boards = [board]
    visited_boards = []
    while unvisited_boards.length > 0
      board = unvisited_boards.shift
      visited_boards << board
      children = []
      board['buttons'].each do |button|
        if button['load_board']
          children << button['load_board']
          all_boards = visited_boards + unvisited_boards
          if all_boards.none?{|b| b['id'] == button['load_board']['id'] || b['path'] == button['load_board']['path'] }
            path = button['load_board']['path'] || (manifest['paths'] && manifest['paths']['boards'] && manifest['paths']['boards'][button['load_board']['id']])
            if path
              b = OBF::Utils.parse_obf(zipper.read(path))
              b['path'] = path
              button['load_board']['id'] = b['id']
              unvisited_boards << b
            end
          end
        end
      end
    end
    
    pages = {}
    visited_boards.each_with_index do |board, idx|
      pages[board['id']] = (idx + 1).to_s
    end
    
    build_pdf({
      'name' => 'Communication Board Set',
      'boards' => visited_boards,
      'pages' => pages
    }, dest_path, zipper, opts)
  end
  # parse obz, draw as pdf

  # TODO: helper files included at the end for emergencies (eg. body parts)
  
  return dest_path
end

.rtl_regexObject



80
81
82
# File 'lib/obf/pdf.rb', line 80

def self.rtl_regex
  @res ||= /[#{RTL_SCRIPTS.map{ |script| "\\p{#{script}}" }.join}]/
end

.to_png(pdf, dest_path) ⇒ Object



288
289
290
# File 'lib/obf/pdf.rb', line 288

def self.to_png(pdf, dest_path)
  OBF::PNG.from_pdf(pdf, dest_path)
end