class PDFBeads::PDFBuilder::PDFLabels

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:

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”+

Public Class Methods

new( arg ) click to toggle source
# 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

Public Instance Methods

getPageLabel( rng_id,page_id ) click to toggle source

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

# 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

Private Instance Methods

int2ralph( num ) click to toggle source
# File lib/pdfbeads/pdflabels.rb, line 120
def int2ralph( num )
  quot, mod = num.divmod(26)
  return (mod + 96).chr * (quot + 1)
end
int2roman( num ) click to toggle source
# File lib/pdfbeads/pdflabels.rb, line 102
def int2roman( num )
  numerals = Hash[  
    1   => "I",  4  => "IV",   5 => "V", 9   => "IX",
    10  => "X", 40  => "XL",  50 => "L", 90  => "XC",
    100 => "C", 400 => "CD", 500 => "D", 900 => "CM", 1000 => "M"
  ]
  res = ''

  numerals.keys.sort{ |a,b| b <=> a }.each do |val|
    while num >= val
      res << numerals[val]
      num -= val
    end
  end

  return res
end
pnum2string( pnum,style ) click to toggle source
# File lib/pdfbeads/pdflabels.rb, line 125
def pnum2string( pnum,style )
  case style
  when 'R'
    return int2roman(pnum)
  when 'r'
    return int2roman(pnum).downcase
  when 'A'
    return int2ralph(pnum)
  when 'a'
    return int2ralph(pnum).downcase
  else
    return pnum.to_s
  end
end