Module: RRTF::PositionFormatting

Included in:
PositionStyle
Defined in:
lib/rrtf/formatting.rb

Overview

Paragraph absolute positioning formatting attributes and methods.

Author:

  • Wesley Hileman

Since:

  • 1.0.0

Constant Summary

POSITION_ATTRIBUTES =

Formatting attributes that can be applied to position paragraphs as frames.

Since:

  • 1.0.0

{
  "size" => {
    "default" => nil,
    "from_user" => lambda{ |value| RRTF::Page::Size.new(value) },
    "to_rtf" => lambda{ |value, document| "\\absw#{value.width}\\absh#{value.height}" unless value.nil? }
  },
  "horizontal_reference" => {
    "default" => nil,
    "dictionary" => {
      "PAGE" => "phpg",
      "MARGIN" => "phmrg",
      "COLUMN" => "phcol"
    },
    "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? }
  },
  "vertical_reference" => {
    "default" => nil,
    "dictionary" => {
      "PAGE" => "pvpg",
      "MARGIN" => "pvmrg",
      "PARAGRAPH" => "pvpara"
    },
    "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? }
  },
  "horizontal_position" => {
    "default" => nil,
    "dictionary" => {
      "CENTER" => "posxc",
      "LEFT" => "posxl",
      "RIGHT" => "posxr"
    },
    "from_user" => lambda do |value|
      return nil if value.nil?
      if value.is_a?(String) && value =~ /^([A-Z]+)$/
        POSITION_ATTRIBUTES["horizontal_position"]["dictionary"][value]
      else
        "posx#{RRTF::Utilities.value2twips(value)}"
      end
    end,
    "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? }
  },
  "vertical_position" => {
    "default" => nil,
    "dictionary" => {
      "CENTER" => "posyc",
      "TOP" => "posyt",
      "BOTTOM" => "posyb"
    },
    "from_user" => lambda do |value|
      return nil if value.nil?
      if value.is_a?(String) && value =~ /^([A-Z]+)$/
        POSITION_ATTRIBUTES["vertical_position"]["dictionary"][value]
      else
        "posy#{RRTF::Utilities.value2twips(value)}"
      end
    end,
    "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? }
  },
  "text_wrap" => {
    "default" => nil,
    "dictionary" => {
      "NONE" => "nowrap",
      "DEFAULT" => "wrapdefault",
      "AROUND" => "wraparound",
      "TIGHT" => "wraptight",
      "THROUGH" => "wrapthrough"
    },
    "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? }
  },
  "drop_cap_lines" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\dropcapli#{value}" unless value.nil? }
  },
  "drop_cap_type" => {
    "default" => nil,
    "dictionary" => {
      "IN_TEXT" => 1,
      "IN_MARGIN" => 2
    },
    "to_rtf" => lambda{ |value, document| "\\dropcapt#{value}" unless value.nil? }
  },
  "lock_anchor" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? "\\abslock0" : "\\abslock1") unless value.nil? }
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

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

Since:

  • 1.0.0



650
651
652
653
654
655
656
657
# File 'lib/rrtf/formatting.rb', line 650

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

Instance Method Details

#initialize_position_formatting(options = {}) ⇒ Object

Initializes position formatting attributes.

Parameters:

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

    the border formatting options.

Options Hash (options):

  • "sides" (String) — default: 'ALL'

    the sides to which the border applied (“ALL”, “LEFT”, “RIGHT”, “TOP”, or “BOTTOM”).

Since:

  • 1.0.0



663
664
665
666
667
668
669
670
# File 'lib/rrtf/formatting.rb', line 663

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

#position_formatting_to_rtf(document) ⇒ Object

Note:

To generate correct RTF control words for colours and fonts, a document object must be provided to this method so that colour and font indicies may be found in the document's colour and font tables, respectively.

Generates an RTF string representing all applied position formatting.

Parameters:

  • document (Document)

    the document for which the RTF is to be generated.

Since:

  • 1.0.0



695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/rrtf/formatting.rb', line 695

def position_formatting_to_rtf(document)
  text = StringIO.new

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

  text.string
end

#set_position_formatting_from_hashmap(hash) ⇒ Object

Sets formatting attributes according to the supplied hashmap.

See Also:

Since:

  • 1.0.0



674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/rrtf/formatting.rb', line 674

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