Module: RRTF::BorderFormatting

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

Overview

Paragraph, table, and image border formatting attributes and methods.

Author:

  • Wesley Hileman

Since:

  • 1.0.0

Constant Summary

BORDER_ATTRIBUTES =
Note:

The “sides” attribute must appear at the top of this hash so that #border_formatting_to_rtf generates correct RTF (borders are initiated by the “sides” attribute in the RTF spec).

Note:

The “line_type” attribute must appear second in this hash so that #border_formatting_to_rtf generates correct RTF.

Formatting attributes that can be applied to borders of paragraphs, tables, & images.

Since:

  • 1.0.0

{
  "sides" => {
    "default" => "box",
    "dictionary" => {
      "ALL" => "box",
      "LEFT" => "brdrl",
      "RIGHT" => "brdrr",
      "TOP" => "brdrt",
      "BOTTOM" => "brdrb"
    },
    "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? }
  },
  "line_type" => {
    "default" => "brdrs",
    "dictionary" => {
      "SINGLE" => "brdrs",
      "THICK" => "brdrth",
      "DOUBLE" => "brdrdb",
      "DOT" => "brdrdot",
      "DASH" => "brdrdash",
      "HAIRLINE" => "brdrhair"
    },
    "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? }
  },
  "width" => {
    "default" => nil,
    "from_user" => lambda{ |value| RRTF::Utilities.value2twips(value) },
    "to_rtf" => lambda{ |value, document| "\\brdrw#{value}" unless value.nil? }
  },
  "spacing" => {
    "default" => nil,
    "from_user" => lambda{ |value| RRTF::Utilities.value2twips(value) },
    "to_rtf" => lambda{ |value, document| "\\brsp#{value}" unless value.nil? }
  },
  "color" => {
    "default" => nil,
    "from_user" => lambda{ |value| value.is_a?(RRTF::Colour) ? value : RRTF::Colour.from_string(value) },
    "to_rtf" => lambda{ |value, document| "\\brdrcf#{document.colours.index(value)}" unless value.nil? }
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

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

Since:

  • 1.0.0



465
466
467
468
469
470
471
472
# File 'lib/rrtf/formatting.rb', line 465

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

Instance Method Details

#border_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 border formatting.

Parameters:

  • document (Document)

    the document for which the RTF is to be generated.

Since:

  • 1.0.0



514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/rrtf/formatting.rb', line 514

def border_formatting_to_rtf(document)
  text = StringIO.new

  # accumulate RTF representations of paragraph attributes
  BORDER_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

#initialize_border_formatting(options = {}) ⇒ Object

Initializes border 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”).

  • "line_type" (String) — default: 'SINGLE'

    the border line type (“SINGLE”, “THICK”, “DOUBLE”, “DOT”, “DASH”, or “HAIRLINE”).

  • "width" (String) — default: nil

    the width of the the border line in twips (can be a string, see Utilities.value2twips).

  • "spacing" (String) — default: nil

    the spacing between the border and paragraph content in twips (can be a string, see Utilities.value2twips).

  • "color" (String, Colour) — default: nil

    the color of the the border line (can be a string, see Colour.from_string).

Since:

  • 1.0.0



482
483
484
485
486
487
488
489
# File 'lib/rrtf/formatting.rb', line 482

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

#set_border_formatting_from_hashmap(hash) ⇒ Object

Sets paragraph formatting attributes according to the supplied hashmap.

See Also:

Since:

  • 1.0.0



493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/rrtf/formatting.rb', line 493

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