Class: RRTF::Stylesheet

Inherits:
Object
  • Object
show all
Defined in:
lib/rrtf/stylesheet.rb

Overview

Represents a stylesheet in an RTF document.

Author:

  • Wesley Hileman

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ Stylesheet

Builds a Stylesheet object.

Parameters:

  • document (Document)

    the document to which the stylesheet belongs.

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

    the stylesheet options.

Options Hash (options):

  • "styles" (Array<Hash>) — default: []

    a hashmap array specifying the styles to include in the stylesheet.

  • "assign_style_handles" (Array<Boolean>) — default: true

    whether or not to auto-assign handles to included styles.

  • "assign_style_priorities" (Array<Boolean>) — default: true

    whether or not to auto-assign priority to included styles.

  • "base_style_handle" (Array<Integer>) — default: 1

    the handle number at which to start indexing styles.

  • "base_style_handle" (Array<Integer>) — default: 20

    the priority number at which to start indexing styles.

See Also:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rrtf/stylesheet.rb', line 27

def initialize(document, options = {})
  @options = {
    "styles" => [],
    "base_style_handle" => 1,
    "base_style_priority" => 1,
    "assign_style_handles" => true,
    "assign_style_priorities" => true
  }.merge(options)
  @document = document
  @next_styles_hash = {}
  @base_styles_hash = {}
  @styles = {}
  add_styles(@options["styles"])
end

Instance Attribute Details

#documentDocument

The document to which the stylesheet belongs.

Returns:



15
16
17
# File 'lib/rrtf/stylesheet.rb', line 15

def document
  @document
end

#stylesHash<String, Style> (readonly)

Stores the Style objects associated with the stylesheet, each of which is keyed by its assigned ID.

Returns:

  • (Hash<String, Style>)

    the style hash.



11
12
13
# File 'lib/rrtf/stylesheet.rb', line 11

def styles
  @styles
end

Instance Method Details

#add_style(options) ⇒ Object

Adds a single style to the stylesheet.

Parameters:

  • options (Hash)

    the options to use in building the style.

Options Hash (options):

  • "style" (Style)

    the style object to add to the stylesheet (can be specified directly in place of “type”).

  • "type" (String)

    the type of style to build (“character” or “paragraph”).

  • "id" (String)

    the ID for the style (used in generating code only).

  • "default" (Boolean) — default: false

    whether or not this style is the default style for the document.

  • "next_style" (Integer) — default: nil

    the ID of the next style (the style to be used in the paragraph created after paragraphs with this style applied).

  • "base_style" (Integer) — default: nil

    the ID of the base style (the style on which this one is based).

  • "assign_handle" (Boolean)

    whether or not a handle should be auto-assigned to this style.

  • "assign_priority" (Boolean)

    whether or not a priority should be auto-assigned to this style.

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rrtf/stylesheet.rb', line 59

def add_style(options)
  style = options.delete("style")
  type = options.delete("type")
  add_options = extract_add_options(options)

  if !style.nil?
    # style object given; add directly
    if !add_style_object(style, add_options)
      RTFError.fire("#{style.to_s} could not be added to the stylesheet (hint: make sure it's a style object).")
    end # if
  elsif !type.nil?
    # style object not given; create based on type
    case type
    when "paragraph"
      add_style_object(ParagraphStyle.new(options), add_options)
    when "character"
      add_style_object(CharacterStyle.new(options), add_options)
    else
      RTFError.fire("Unreconized style type '#{type.to_s}'.")
    end # case
  else
    RTFError.fire("A style type or style object must be specified for each style in a stylesheet.")
  end # if
end

#add_styles(hash_array) ⇒ Object

Adds the specified styles to the stylesheet.

Parameters:

  • hash_array (Array<Hash>)

    a hashmap array specifying the styles to add to the stylesheet.

See Also:



46
47
48
# File 'lib/rrtf/stylesheet.rb', line 46

def add_styles(hash_array)
  hash_array.each { |hash| add_style(hash) }
end

#to_rtf(options = {}) ⇒ Object

Note:

calling ‘to_rtf` causes all next and base styles to be updated (to_rtf “commits” the stylesheet); errors might be raised if next or base styles are missing (have yet to be added to the stylesheet).

Converts the stylesheet to its RTF representation.

Parameters:

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

Options Hash (options):

  • "uglify" (Boolean) — default: false

    removes most line breaks and spaces from RTF output.

  • "base_indent" (Integer) — default: 0

    the base indent (in spaces) for RTF output (ignored if uglify is true).

  • "child_indent" (Integer) — default: 0

    the amount of spaces by which to indent the component styles (ignored if uglify is true).



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rrtf/stylesheet.rb', line 93

def to_rtf(options = {})
  # load default options
  options = {
    "uglify" => false,
    "base_indent" => 0,
    "child_indent" => 0
  }.merge(options)
  # build line prefixes
  newline_prefix = options["uglify"] ? '' : "\n"
  base_prefix = options["uglify"] ? '' : " "*options["base_indent"]

  # lookup and set next and base style handles on component styles
  substitute_next_style_handles()
  substitute_base_style_handles()

  rtf = StringIO.new

  rtf << "#{base_prefix}{\\stylesheet"
  @styles.values.each do |style|
    rtf << newline_prefix
    rtf << style.to_rtf(
      document,
      "uglify" => options["uglify"],
      "base_indent" => options["base_indent"]+options["child_indent"]
    )
  end
  rtf << "#{newline_prefix}#{base_prefix}}"

  rtf.string
end