Class: RRTF::ParagraphStyle

Inherits:
Style
  • Object
show all
Includes:
CharacterFormatting, ParagraphFormatting
Defined in:
lib/rrtf/style/paragraph_style.rb

Overview

Note:

paragraphs can be styled with character commands in addition to paragraph commands, thus this class includes both paragraph & character formatting modules.

This class represents a styling for a paragraph within an RTF document.

Constant Summary

Constants included from CharacterFormatting

CharacterFormatting::CHARACTER_ATTRIBUTES

Constants included from ParagraphFormatting

RRTF::ParagraphFormatting::PARAGRAPH_ATTRIBUTES

Instance Attribute Summary

Attributes inherited from Style

#additive, #auto_update, #based_on_style_handle, #handle, #hidden, #name, #next_style_handle, #primary, #priority

Instance Method Summary collapse

Methods included from CharacterFormatting

#character_formatting_to_rtf, included, #initialize_character_formatting, #push_colours, #push_fonts, #set_character_formatting_from_hashmap

Methods included from ParagraphFormatting

included, #initialize_paragraph_formatting, #paragraph_formatting_to_rtf, #set_paragraph_formatting_from_hashmap

Methods inherited from Style

#is_character_style?, #is_document_style?, #is_table_style?, #styledef, #stylename, #suffix

Constructor Details

#initialize(options = {}) ⇒ ParagraphStyle

This is the constructor for the CharacterStyle class.

Parameters:

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

    the character style options.

Options Hash (options):

  • "name" (String) — default: nil

    human-readable name for the style.

  • "handle" (Integer) — default: nil

    16-bit integer that identifies the style in a document.

  • "next_style_handle" (Integer) — default: nil

    16-bit integer that identifies the next style for this style.

  • "based_on_style_handle" (Integer) — default: nil

    16-bit integer that identifies the base style for this style.

  • "priority" (Integer) — default: nil

    16-bit integer that indicates the ordering of the style among other styles in a document.

  • "primary" (Boolean) — default: false

    whether or not this style is a primary or “quick” style.

  • "additive" (Boolean) — default: false

    whether or not this character style is additive to the current paragraph style.

  • "auto_update" (Boolean) — default: false

    whether or not this style should be updated when any node to which the style is applied is updated.

  • "hidden" (Boolean) — default: false

    whether or not the style should be hidden.

  • "bold" (Boolean) — default: nil

    enable or disable bold (nil to remain same).

  • "italic" (Boolean) — default: nil

    enable or disable italic (nil to remain same).

  • "underline" (Boolean, String) — default: nil

    enable or disable underline (nil to remain same); can also be a string (see CharacterFormatting::CHARACTER_ATTRIBUTES).

  • "uppercase" (Boolean) — default: nil

    enable or disable all caps (nil to remain same).

  • "superscript" (Boolean) — default: nil

    enable or disable superscript (nil to remain same).

  • "subscript" (Boolean) — default: nil

    enable or disable subscript (nil to remain same).

  • "strike" (Boolean) — default: nil

    enable or disable single line-through (nil to remain same).

  • "emboss" (Boolean) — default: nil

    enable or disable emboss (nil to remain same).

  • "imprint" (Boolean) — default: nil

    enable or disable imprint (nil to remain same).

  • "outline" (Boolean) — default: nil

    enable or disable outline (nil to remain same).

  • "text_hidden" (Boolean) — default: nil

    enable or disable hidden (nil to remain same).

  • "kerning" (Boolean, Integer) — default: nil

    enable or disable kerning (nil to remain same); to enable specify the font size in half-points above which kerining will be applied.

  • "character_spacing_offset" (Integer) — default: nil

    quarter points by which to expand or compress character spacing (negative for compress).

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

    colour to apply to the foreground (text); see Colour.from_string for string format.

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

    colour to apply to the background (highlight); see Colour.from_string for string format.

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

    colour to apply to the underline; see Colour.from_string for string format.

  • "font" (String, Font) — default: nil

    font to apply to text; see Font.from_string for string format.

  • "font_size" (Integer) — default: nil

    font size in half-points.

  • "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').



18
19
20
21
22
# File 'lib/rrtf/style/paragraph_style.rb', line 18

def initialize(options = {})
   super(options)
   initialize_paragraph_formatting(options)
   initialize_character_formatting(options)
end

Instance Method Details

#is_paragraph_style?Boolean

This method overrides the is_paragraph_style? method inherited from the Style class to always return true.

Returns:

  • (Boolean)


26
27
28
# File 'lib/rrtf/style/paragraph_style.rb', line 26

def is_paragraph_style?
   true
end

#prefix(document) ⇒ Object

This method generates a string containing the prefix associated with a style object.

Parameters

fonts

A reference to a FontTable containing any fonts used by the style (may be nil if no fonts used).

colours

A reference to a ColourTable containing any colours used by the style (may be nil if no colours used).



73
74
75
76
77
78
79
80
# File 'lib/rrtf/style/paragraph_style.rb', line 73

def prefix(document)
  text = StringIO.new

  text << "\\s#{handle} " unless handle.nil?
  text << rtf_formatting(document)

  text.string
end

#rtf_formatting(document) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rrtf/style/paragraph_style.rb', line 82

def rtf_formatting(document)
  rtf = StringIO.new

  pf = paragraph_formatting_to_rtf(document)
  cf = character_formatting_to_rtf(document)

  rtf << pf unless pf.nil?
  rtf << cf unless cf.nil?

  rtf.string
end

#to_rtf(document, options = {}) ⇒ Object

Converts the stylesheet paragraph style into its RTF representation

Parameters

fonts

A reference to a FontTable containing any fonts used by the style (may be nil if no fonts used).

colours

A reference to a ColourTable containing any colours used by the style (may be nil if no colours used).



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rrtf/style/paragraph_style.rb', line 37

def to_rtf(document, options = {})
  # load default options
  options = {
    "uglify" => false,
    "base_indent" => 0
  }.merge(options)
  # build formatting helpers
  base_prefix = options["uglify"] ? '' : ' '*options["base_indent"]
  name_prefix = options["uglify"] ? ' ' : ''
  suffix = options["uglify"] ? '' : ' '

  rtf = StringIO.new

  rtf << base_prefix
  rtf << "{\\s#{handle}#{suffix}"
  rtf << "#{rtf_formatting(document)}#{suffix}"
  rtf << "\\additive#{suffix}" if @additive
  rtf << "\\sbasedon#{@based_on_style_handle}#{suffix}" unless @based_on_style_handle.nil?
  rtf << "\\sautoupd#{suffix}" if @auto_update
  rtf << "\\snext#{@next_style_handle}#{suffix}" unless @next_style_handle.nil?
  rtf << "\\sqformat#{suffix}" if @primary
  rtf << "\\spriority#{@priority}#{suffix}" unless @priority.nil?
  rtf << "\\shidden#{suffix}" if @hidden
  rtf << "#{name_prefix}#{name};}"

  rtf.string
end