Class: RRTF::Page::Margin

Inherits:
Object
  • Object
show all
Defined in:
lib/rrtf/page/margin.rb

Overview

Represents the left, right, top, and bottom margin in a document page.

Author:

  • Wesley Hileman

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Margin

Note:

Margins are stored internally in twentieth points (twips).

Builds a new margin object from a string or hash.

Parameters:

  • value (String, Hash) (defaults to: nil)

    the value from which to parse the margin.

Options Hash (value):

  • "left" (Integer)

    the left margin in twips.

  • "right" (Integer)

    the right margin in twips.

  • "top" (Integer)

    the top margin in twips.

  • "bottom" (Integer)

    the bottom margin in twips.

See Also:

Since:

  • 1.0.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rrtf/page/margin.rb', line 62

def initialize(value = nil)
  options = {
    # default 1 inch margins
    "left" => 1440,
    "right" => 1440,
    "top" => 1440,
    "bottom" => 1440
  }

  case value
  when String
    options = options.merge(self.class.parse_string(value))
  when Hash
    options = options.merge(value)
  when nil
  else
    RRTF::RTFError.fire("Cannot create margin from '#{value}'.")
  end # case

  @left = options.delete("left")
  @right = options.delete("right")
  @top = options.delete("top")
  @bottom = options.delete("bottom")
end

Instance Attribute Details

#bottomObject

Since:

  • 1.0.0



6
7
8
# File 'lib/rrtf/page/margin.rb', line 6

def bottom
  @bottom
end

#leftObject

Since:

  • 1.0.0



6
7
8
# File 'lib/rrtf/page/margin.rb', line 6

def left
  @left
end

#rightObject

Since:

  • 1.0.0



6
7
8
# File 'lib/rrtf/page/margin.rb', line 6

def right
  @right
end

#topObject

Since:

  • 1.0.0



6
7
8
# File 'lib/rrtf/page/margin.rb', line 6

def top
  @top
end

Class Method Details

.from_string(string) ⇒ Margin

Extracts a margin object from a string.

Examples:

Parse margin from a String

Margin.from_string("12.1pt, 2.2in, 5cm, 4in")
# => #<RRTF::Margin:0x007fa1b3094500 @left=242, @right=3168, @top=2834, @bottom=5760>

Returns:

  • (Margin)

    the margin object created from the string.

See Also:

Since:

  • 1.0.0



16
17
18
# File 'lib/rrtf/page/margin.rb', line 16

def self.from_string(string)
  self.new(parse_string(string))
end

.parse_string(string) ⇒ Hash<String, Integer>

Extracts a margin hash from a string.

Examples:

Parse margin from a String

Margin.parse_string("12.1pt, 2.2in, 5cm, 4in")
# => {"top"=>2834, "bottom"=>5760, "left"=>242, "right"=>3168} (twips)

Parameters:

  • string (String)

    the string from which to parse the margin, taking one of the following formats: “<marg:all>”, “<marg:lr>,<marg:tb>”, or “<marg:l>,<marg:r>,<marg:t>,<marg:b>” where each number may be suffixed by an optional unit (see Utilities.value2twips).

Returns:

  • (Hash<String, Integer>)

    the margin hash created from the string.

Raises:

  • (RTFError)

    if the string cannot be converted into a margin hash.

Since:

  • 1.0.0



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rrtf/page/margin.rb', line 30

def self.parse_string(string)
  values = string.split(',').map(&:strip).collect{ |str| RRTF::Utilities.value2twips(str) }
  case values.length
  when 1
    tblr = values.first
    {"top" => tblr, "bottom" => tblr, "left" => tblr, "right" => tblr}
  when 2
    tb = values.last
    lr = values.first
    {"top" => tb, "bottom" => tb, "left" => lr, "right" => lr}
  when 4
    l = values[0]
    r = values[1]
    t = values[2]
    b = values[3]
    {"top" => t, "bottom" => b, "left" => l, "right" => r}
  else
    RRTF::RTFError.fire("Invalid margin '#{string}'.")
  end # case
end

Instance Method Details

#==(obj) ⇒ Object Also known as: eql?

initialize

Since:

  • 1.0.0



87
88
89
# File 'lib/rrtf/page/margin.rb', line 87

def ==(obj)
  obj.class == self.class && obj.state == state
end