Class: PDFBeads::PDFBuilder::PDFLabels

Inherits:
Array
  • Object
show all
Defined in:
lib/pdfbeads/pdflabels.rb

Overview

Parse a specification string passed to pdfbeads via its -L (–labels) option and convert it into a sequence of ranges which can be used for building a PageLabels dictionary embeddable into the PDF file. The specification format is based on the PDF format description, section 12.4.2. and looks as follows:

  • ranges are separated with a semicolon;

  • each range consists from a physical number of the first page, folowed by a colon and a number format description;

  • the number format consists from an optional prefix, followed by a percent sign, an optional number indicating the value of the numeric portion for the first page label in the range, and a single Latin letter indicating the desired numbering style;

  • the following numbering styles are supported:

    D

    – Decimal arabic numerals;

    R

    – Uppercase roman numerals;

    r

    – Lowercase roman numerals;

    A

    – Uppercase Romam letters (A to Z for the first 26 pages,

    AA to ZZ for the next 26, and so on);
    
    a

    – Lowercase letters (a to z for the first 26 pages,

    aa to zz for the next 26, and so on).
    

For example if a book starts from two unnumbered title pages, followed by 16 pages numbered with Roman digits, and then goes the Arabic numeration, which however starts from 17, then the following label specification string would be appropriate: “0:Title %D;2:%R;18:%16D”

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ PDFLabels

Returns a new instance of PDFLabels.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pdfbeads/pdflabels.rb', line 65

def initialize( arg )
  descrs = arg.split(/;/)
  descrs.each do |descr|
    rng = Hash.new()
    fields = descr.split(/:/, 2)
    if /\d+/.match( fields[0] )
      rng[:first] = fields[0].to_i
      if fields.length > 1 and /([^%]*)%?(\d*)([DRrAa]?)/.match(fields[1])
        rng[:prefix]  = $1 unless $1 == ''
        rng[:start ]  = $2.to_i unless $2 == ''
        rng[:style ]  = $3 unless $3 == ''
      end
      push(rng)
    end
  end
end

Instance Method Details

#getPageLabel(rng_id, page_id) ⇒ Object

Convert a physical page number into the label we would like to be displayed for this page in the PDF viewer.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pdfbeads/pdflabels.rb', line 84

def getPageLabel( rng_id,page_id )
  rng = self[rng_id]
  prefix = ''
  start_num = 1

  start_num = rng[:start] if rng.has_key? :start
  pnum = page_id - rng[:first] + start_num

  prefix = rng[:prefix] if rng.has_key? :prefix

  snum = ''
  snum = pnum2string( pnum,rng[:style] ) if rng.has_key? :style

  return "#{prefix}#{snum}"
end