Class: Uniword::Wordprocessingml::PageSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/wordprocessingml/page_setup.rb

Overview

Applies uniform page setup across every section of a document — Word's Layout dialog as an API.

Paper size (named presets), orientation (with dimension swap, like Word), and margins (twips, or strings with in/cm/mm suffix). Header/footer/gutter margins are left untouched.

Examples:

A4 landscape with 2 cm margins

setup = PageSetup.new(size: "a4", orientation: "landscape",
                      margins: "2cm")
setup.apply(document)
setup.sections_updated # => 1

Constant Summary collapse

PAPER_SIZES =

Named paper sizes in twips [width, height] (portrait).

{
  "letter" => [12_240, 15_840],
  "legal" => [12_240, 20_160],
  "a4" => [11_906, 16_838],
  "a5" => [8_391, 11_906],
  "executive" => [10_440, 15_120],
}.freeze
TWIPS_PER_INCH =
1440
TWIPS_PER_CM =
567
TWIPS_PER_MM =
57
ORIENTATIONS =
%w[portrait landscape].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: nil, orientation: nil, margins: nil, **side_margins) ⇒ PageSetup

Returns a new instance of PageSetup.

Parameters:

  • size (String, nil) (defaults to: nil)

    Paper size name (see PAPER_SIZES)

  • orientation (String, nil) (defaults to: nil)

    "portrait" or "landscape"

  • margins (Integer, String, nil) (defaults to: nil)

    Uniform margin for all four sides (twips, or "1in" / "2.5cm" / "25mm")

  • side_margins (Hash)

    Per-side overrides (:top, :right, :bottom, :left)

Raises:

  • (ArgumentError)

    on unknown size, orientation, or margin



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/uniword/wordprocessingml/page_setup.rb', line 43

def initialize(size: nil, orientation: nil, margins: nil,
               **side_margins)
  @size = normalize_size(size)
  @orientation = normalize_orientation(orientation)
  @margins = side_margins.to_h do |side, value|
    [side, parse_margin(value)]
  end
  uniform = parse_margin(margins)
  %i[top right bottom left].each do |side|
    @margins[side] = uniform if @margins[side].nil?
  end
  @sections_updated = 0
end

Instance Attribute Details

#sections_updatedInteger (readonly)

Returns number of sections updated.

Returns:

  • (Integer)

    number of sections updated



34
35
36
# File 'lib/uniword/wordprocessingml/page_setup.rb', line 34

def sections_updated
  @sections_updated
end

Instance Method Details

#apply(document) ⇒ Integer

Apply the setup to every section in the document.

Parameters:

Returns:

  • (Integer)

    number of sections updated



61
62
63
64
65
66
67
68
# File 'lib/uniword/wordprocessingml/page_setup.rb', line 61

def apply(document)
  each_section_properties(document) do |sect_pr|
    apply_page_size(sect_pr) if @size || @orientation
    apply_margins(sect_pr)
    @sections_updated += 1
  end
  sections_updated
end