Module: RRTF::ParagraphFormatting

Included in:
ParagraphStyle
Defined in:
lib/rrtf/style/formatting.rb

Overview

Encapsulates all paragraph formatting methods shared between style types.

Author:

  • Wesley Hileman

Constant Summary

PARAGRAPH_ATTRIBUTES =
{
  "justification" => {
    "default" => "l",
    "dictionary" => {
      "LEFT" => "l",
      "RIGHT" => "r",
      "CENTER" => "c",
      "CENTRE" => "c",
      "FULL" => "j"
    },
    "to_rtf" => lambda{ |value, document| "\\q#{value}" }
  },
  "left_indent" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\li#{value}" unless value.nil? }
  },
  "right_indent" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\ri#{value}" unless value.nil? }
  },
  "first_line_indent" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\fi#{value}" unless value.nil? }
  },
  "space_before" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\sb#{value}" unless value.nil? }
  },
  "space_after" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\sa#{value}" unless value.nil? }
  },
  "line_spacing" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\sl#{value}" unless value.nil? }
  },
  "widow_orphan_ctl" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? "\\widctlpar" : "\\nowidctlpar") unless value.nil? }
  },
  "no_break" => {
    "default" => false,
    "to_rtf" => lambda{ |value, document| "\\keep" if value }
  },
  "no_break_with_next" => {
    "default" => false,
    "to_rtf" => lambda{ |value, document| "\\keepn" if value }
  },
  "hyphenate" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? "\\hyphpar" : "\\hyphpar0") unless value.nil? }
  },
  "paragraph_flow" => {
    "default" => 'ltr',
    "dictionary" => {
      "LEFT_TO_RIGHT" => 'ltr',
      "RIGHT_TO_LEFT" => 'rtl'
    },
    "to_rtf" => lambda{ |value, document| "\\#{value}par" unless value.nil? }
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



259
260
261
262
263
264
265
266
# File 'lib/rrtf/style/formatting.rb', line 259

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

Instance Method Details

#initialize_paragraph_formatting(options = {}) ⇒ Object

Initializes paragraph formatting attributes.

Parameters:

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

    the paragraph formatting options.

Options Hash (options):

  • "justification" (String) — default: 'LEFT'

    the paragraph justification ('LEFT', 'CENTER'/'CENTRE', 'RIGHT', or 'FULL').

  • "left_indent" (Integer) — default: nil

    the left indent of the paragraph (twentieth points).

  • "right_indent" (Integer) — default: nil

    the right indent of the paragraph (twentieth points).

  • "first_line_indent" (Integer) — default: nil

    the first line indent of the paragraph (twentieth points).

  • "space_before" (Integer) — default: nil

    the space before the paragraph (twentieth points).

  • "space_after" (Integer) — default: nil

    the space after the paragraph (twentieth points).

  • "line_spacing" (Integer) — default: nil

    the line spacing in the paragraph (twentieth points).

  • "widow_orphan_ctl" (Boolean) — default: nil

    enable or disable widow-and-orphan control.

  • "no_break" (Boolean) — default: nil

    when true, tries to keep the paragraph on the same page (i.e. without breaking).

  • "no_break_with_next" (Boolean) — default: nil

    when true, tries to keep the paragraph with the next paragraph on the same page (i.e. without breaking).

  • "hyphenate" (Boolean) — default: nil

    enable or disable hyphenation for the paragraph.

  • "paragraph_flow" (String) — default: 'LEFT_TO_RIGHT'

    the text flow direction in the paragraph ('LEFT_TO_RIGHT' or 'RIGHT_TO_LEFT').



283
284
285
286
287
288
289
290
# File 'lib/rrtf/style/formatting.rb', line 283

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

#paragraph_formatting_to_rtf(document) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/rrtf/style/formatting.rb', line 307

def paragraph_formatting_to_rtf(document)
  text = StringIO.new

  # accumulate RTF representations of paragraph attributes
  PARAGRAPH_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_paragraph_formatting_from_hashmap(hash) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rrtf/style/formatting.rb', line 292

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