Class: Eleanor::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/eleanor.rb,
lib/eleanor/hpdfpaper.rb

Overview

Encapsulates paragraphs.

Note that Eleanor.load_config dynamically adds both class and instance methods to Page and its subclasses depending on the options present in the user’s configuration file. See “Configuration” in the README.

Direct Known Subclasses

TitlePage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(screenplay, page_no) ⇒ Page

Pages must be attached to a screenplay. page_no is the page number.



336
337
338
339
340
# File 'lib/eleanor.rb', line 336

def initialize screenplay, page_no
  @screenplay= screenplay
  @page_no= page_no
  @paras= []
end

Instance Attribute Details

#page_noObject (readonly)

See initialize



320
321
322
# File 'lib/eleanor.rb', line 320

def page_no
  @page_no
end

#parasObject (readonly)

Array of Paragraphs



323
324
325
# File 'lib/eleanor.rb', line 323

def paras
  @paras
end

#screenplayObject (readonly)

See initialize



320
321
322
# File 'lib/eleanor.rb', line 320

def screenplay
  @screenplay
end

Instance Method Details

#body_heightObject

The page’s body is all the paragraphs (and the margins between them) between the page’s top and bottom margins. Returns a Length.



327
328
329
330
331
332
333
# File 'lib/eleanor.rb', line 327

def body_height
  prev_para= nil
  @paras.inject(0.points) do |total, para|
    prev, prev_para= [prev_para, para]
    total + para.height + (prev.nil?? 0.points : prev.margin_between(para))
  end
end

#margin_top_actualObject

The page’s top margin plus the top margin of the first paragraph on the page. A Length.



344
345
346
# File 'lib/eleanor.rb', line 344

def margin_top_actual
  self.margin_top + (@paras.empty?? 0.points : @paras.first.margin_top)
end

#max_body_heightObject

The page’s maximum body height (see body_height) given its top and bottom margins.



350
351
352
# File 'lib/eleanor.rb', line 350

def max_body_height
  self.height - self.margin_top_actual - self.margin_bottom
end

#push_para(para, force = false) ⇒ Object

Attempts to add para to the end of the page. If para is split by the page break as a result or cannot be added at all, returns a new Paragraph which should be added to the top of a new page. Otherwise, if para fits on the page, nil is returned. If force is true para is added to the page regardless of constraints.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/eleanor.rb', line 359

def push_para para, force=false
  prev_para= @paras.last
  if prev_para.nil? && (para.is_a?(Dialog) || para.is_a?(Parenthetical))
    prev_para= CharacterCue.new(para.screenplay,
                                para.last_character_cue.input_line,
                                nil,
                                :widow)
    self.push_para(prev_para)
  end
  margin_between= (prev_para.nil?? 0.points : prev_para.margin_between(para))
  height_before_para= self.body_height + margin_between
  # para overruns the current page.  need to start a new page.
  curr_page_para, new_page_para=
    if (height_before_para + para.height > self.max_body_height) && !force
      if height_before_para >= self.max_body_height
        [nil, para]
      else
        orphan, widow= para.split(self.max_body_height - height_before_para)
        if orphan.nil?
          [nil, para]
        else
          [orphan, widow]
        end
      end
    else
      [para, nil]
    end
  unless curr_page_para.nil?
    curr_page_para.is_first_on_page= true if @paras.empty?
    @paras << curr_page_para
  end
  new_page_para
end

#to_sObject



393
394
395
396
# File 'lib/eleanor.rb', line 393

def to_s
  str= '- ' + @page_no.to_s + ' ' + ('-' * 72) + "\n"
  @paras.inject(str) { |s, para| "#{s}#{para}\n"}
end

#write_to_paper(pdf_page, line_height_pts) ⇒ Object

An implementation detail in the backend. See lib/eleanor/hpdfpaper.rb.



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
# File 'lib/eleanor/hpdfpaper.rb', line 167

def write_to_paper pdf_page, line_height_pts
  pdf_page.begin_text
  # header
  if self.header
    pdf_page.move_to_top
    pdf_page.move_down(self.header_margin_top)
    pdf_page.line_center(self.header, line_height_pts)
  end
  # page number
  if self.page_number_display
    pdf_page.move_to_top
    pdf_page.move_down(self.page_number_margin_top)
    pdf_page.line_flush_right(self.page_number_display,
                              line_height_pts,
                              self.page_number_margin_right)
  end
  # finally, paragraphs
  pdf_page.move_to_top
  pdf_page.move_down(self.margin_top_actual)
  prev_para= nil
  @paras.each do |para|
    pdf_page.move_down(prev_para.margin_between(para)) if prev_para
    para.write_to_paper(pdf_page, line_height_pts)
    prev_para= para
  end
  pdf_page.end_text
end