Module: RRTF::PageFormatting

Included in:
DocumentProperties, SectionStyle
Defined in:
lib/rrtf/formatting.rb

Overview

Page formatting attributes and methods.

Author:

  • Wesley Hileman

Since:

  • 1.0.0

Constant Summary

PAGE_ATTRIBUTES =

Formatting attributes that can be applied to an RTF document or a section in a document.

Returns:

  • (Hash<String, Hash>)

    a hash mapping each attribute to a hash that describes (1) the attribute's default value, (2) how to parse the attribute from the user, and (3) how to convert the attribute to an RTF sequence.

Since:

  • 1.0.0

{
  "orientation" => {
    "default" => :portrait,
    "dictionary" => {
      "PORTRAIT" => :portrait,
      "LANDSCAPE" => :landscape
    },
    "to_rtf" => lambda do |value, targ|
      case targ
      when :document
        "\\landscape" if value == :landscape
      when :section
        "\\lndscpsxn" if value == :landscape
      end # case
    end
  },
  "size" => {
    "default" => RRTF::Page::Size.new,
    "from_user" => lambda{ |value| RRTF::Page::Size.new(value) },
    "to_rtf" => lambda do |value, targ|
      case targ
      when :document
        "\\paperw#{value.width}\\paperh#{value.height}"
      when :section
        "\\pgwsxn#{value.width}\\pghsxn#{value.height}"
      end # case
    end
  },
  "margin" => {
    "default" => RRTF::Page::Margin.new,
    "from_user" => lambda{ |value| RRTF::Page::Margin.new(value) },
    "to_rtf" => lambda do |value, targ|
      case targ
      when :document
        "\\margl#{value.left}\\margr#{value.right}\\margt#{value.top}\\margb#{value.bottom}"
      when :section
        "\\marglsxn#{value.left}\\margrsnx#{value.right}\\margtsxn#{value.top}\\margbsnx#{value.bottom}"
      end # case
    end
  },
  "gutter" => {
    "default" => nil,
    "from_user" => lambda{ |value| RRTF::Utilities.value2twips(value) },
    "to_rtf" => lambda do |value, targ|
      case targ
      when :document
        "\\gutter#{value}" unless value.nil?
      when :section
        "\\guttersxn#{value}" unless value.nil?
      end # case
    end
  }
}.freeze
PAGE_FORMATTING_TARGET_DICTIONARY =

Since:

  • 1.0.0

{
  "DOCUMENT"      => :document,
  "SECTION"       => :section
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#targetObject

Since:

  • 1.0.0



1055
1056
1057
# File 'lib/rrtf/formatting.rb', line 1055

def target
  @target
end

Class Method Details

.included(base) ⇒ Object

Generates attribute accessors for all page attributes when the module is included in another module or class.

Since:

  • 1.0.0



1059
1060
1061
1062
1063
1064
1065
1066
# File 'lib/rrtf/formatting.rb', line 1059

def self.included(base)
  # define accessors in base for document attributes
  base.class_eval do
    PAGE_ATTRIBUTES.each do |key, options|
      attr_accessor :#{key}"
    end # each
  end # class_eval
end

Instance Method Details

#initialize_page_formatting(options = {}, target = "DOCUMENT") ⇒ Object

Note:

The behavior of the “gutter” option changes with the document “facing_pages” setting.

Initializes page formatting attributes.

Parameters:

  • options (Hash) (defaults to: {})

    the document formatting options.

Options Hash (options):

  • "orientation" (String) — default: "PORTRAIT"

    the orientation of the paper (“PORTRAIT” or “LANDSCAPE”).

  • "size" (String, Page::Size) — default: Page::Size.new

    the size of the paper (object or string; see RRTF::Page::Size#initialize).

  • "margin" (String, Page::Margin) — default: Page::Margin.new

    the paper margin (object or string; see RRTF::Page::Margin#initialize).

  • "gutter" (String) — default: nil

    the page gutter width (specify a string, see Utilities.value2twips).

Since:

  • 1.0.0



1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
# File 'lib/rrtf/formatting.rb', line 1077

def initialize_page_formatting(options = {}, target = "DOCUMENT")
  @target = PAGE_FORMATTING_TARGET_DICTIONARY[target]

  # load default attribute values
  PAGE_ATTRIBUTES.each do |key, options|
    send("#{key}=", options["default"])
  end # each
  # overwrite default attribute values with given values
  set_page_formatting_from_hashmap(options)
end

#page_formatting_to_rtfString

Generates an RTF string representing all applied page formatting.

Returns:

  • (String)

    RTF string.

Since:

  • 1.0.0



1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'lib/rrtf/formatting.rb', line 1108

def page_formatting_to_rtf
  text = StringIO.new

  # accumulate RTF representations of page attributes
  PAGE_ATTRIBUTES.each do |key, options|
    if options.has_key?("to_rtf")
      rtf = options["to_rtf"].call(send(key), @target)
      text << rtf unless rtf.nil?
    end # if
  end # each

  text.string
end

#set_page_formatting_from_hashmap(hash) ⇒ Object

Sets document formatting attributes according to the supplied hashmap.

See Also:

Since:

  • 1.0.0



1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/rrtf/formatting.rb', line 1090

def set_page_formatting_from_hashmap(hash)
  hash.each do |attribute, value|
    # skip unreconized attributes
    next unless(PAGE_ATTRIBUTES.keys.include?(attribute))
    # preprocess value if nessesary
    if PAGE_ATTRIBUTES[attribute].has_key?("from_user")
      value = PAGE_ATTRIBUTES[attribute]["from_user"].call(value)
    elsif PAGE_ATTRIBUTES[attribute].has_key?("dictionary") && value.is_a?(String)
      value = PAGE_ATTRIBUTES[attribute]["dictionary"][value]
    end # if
    # set attribute value
    send("#{attribute}=", value)
  end # each
end