Class: HexaPDF::Type::PageLabel

Inherits:
Dictionary show all
Defined in:
lib/hexapdf/type/page_label.rb

Overview

Represents a page label dictionary.

A page label dictionary contains information about the numbering style, the label prefix and the start number to construct page labels like ‘A-1’ or ‘iii’. What is not stored is the page to which it is applied since that is stored in a number tree referenced through the /PageLabels entry in the document catalog.

See HexaPDF::Document::Pages for details on how to create and manage page labels.

Examples:

  • numbering style :decimal, prefix none, start number default value

    1, 2, 3, 4, …

  • numbering style :lowercase_letters, prefix ‘Appendix ’, start number 5

    Appendix e, Appendix f, Appendix g, …

  • numbering style :uppercase_roman, prefix none, start number 10

    X, XI, XII, XIII, …

  • numbering style :none, prefix ‘Page’, start number default value

    Page, Page, Page, Page, …

  • numbering style :none, prefix none, start number default value

    “”, “”, “”, … (i.e. always the empty string)

See: PDF2.0 s12.4.2, HexaPDF::Document::Pages, HexaPDF::Type::Catalog

Constant Summary collapse

NUMBERING_STYLE_MAPPING =

:nodoc:

{ # :nodoc:
  decimal: :D, D: :D,
  uppercase_roman: :R, R: :R,
  lowercase_roman: :r, r: :r,
  uppercase_letters: :A, A: :A,
  lowercase_letters: :a, a: :a,
  none: nil
}
REVERSE_NUMBERING_STYLE_MAPPING =

:nodoc:

Constants included from DictionaryFields

DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Dictionary

#[], #[]=, define_field, define_type, #delete, #each, each_field, #empty?, field, #key?, #to_hash, type, #type

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, field, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, make_direct, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#construct_label(index) ⇒ Object

Constructs the page label for the given index which needs to be relative to the page index of the first page in the associated labelling range.

This method is usually not called directly but through HexaPDF::Document::Pages#page_label.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hexapdf/type/page_label.rb', line 87

def construct_label(index)
  label = (prefix || '').dup
  number = start_number + index
  case numbering_style
  when :decimal
    label + number.to_s
  when :uppercase_roman
    label + number_to_roman_numeral(number)
  when :lowercase_roman
    label + number_to_roman_numeral(number, lowercase: true)
  when :uppercase_letters
    label + number_to_letters(number)
  when :lowercase_letters
    label + number_to_letters(number, lowercase: true)
  when :none
    label
  end
end

#numbering_style(value = nil) ⇒ Object

:call-seq:

page_label.numbering_style             -> numbering_style
page_label.numbering_style(value)      -> numbering_style

Returns the numbering style if no argument is given. Otherwise sets the numbering style to the given value.

The following numbering styles are available:

:none

No numbering is done; the label only consists of the prefix.

:decimal

Decimal arabic numerals (1, 2, 3, 4, …).

:uppercase_roman

Uppercase roman numerals (I, II, III, IV, …)

:lowercase_roman

Lowercase roman numerals (i, ii, iii, iv, …)

:uppercase_letters

Uppercase letters (A, B, C, D, …)

:lowercase_letters

Lowercase letters (a, b, c, d, …)



132
133
134
135
136
137
138
139
140
# File 'lib/hexapdf/type/page_label.rb', line 132

def numbering_style(value = nil)
  if value
    self[:S] = NUMBERING_STYLE_MAPPING.fetch(value) do
      raise ArgumentError, "Invalid numbering style specified: #{value}"
    end
  else
    REVERSE_NUMBERING_STYLE_MAPPING.fetch(self[:S], :none)
  end
end

#prefix(value = nil) ⇒ Object

:call-seq:

page_label.prefix             -> prefix
page_label.prefix(value)      -> prefix

Returns the label prefix if no argument is given. Otherwise sets the label prefix to the given string value.



148
149
150
151
152
153
154
# File 'lib/hexapdf/type/page_label.rb', line 148

def prefix(value = nil)
  if value
    self[:P] = value
  else
    self[:P]
  end
end

#start_number(value = nil) ⇒ Object

:call-seq:

page_label.start_number             -> start_number
page_label.start_number(value)      -> start_number

Returns the start number if no argument is given. Otherwise sets the start number to the given integer value.



162
163
164
165
166
167
168
169
170
171
# File 'lib/hexapdf/type/page_label.rb', line 162

def start_number(value = nil)
  if value
    if !value.kind_of?(Integer) || value < 1
      raise ArgumentError, "Start number must be an integer greater than or equal to 1"
    end
    self[:St] = value
  else
    self[:St]
  end
end