Class: RRTF::CharacterStyle

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

Overview

This class represents a character style for an RTF document.

Constant Summary

Constants included from CharacterFormatting

RRTF::CharacterFormatting::CHARACTER_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 inherited from Style

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

Constructor Details

#initialize(options = {}) ⇒ CharacterStyle

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 RRTF::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 RRTF::Colour.from_string for string format.

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

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

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

    colour to apply to the underline; see RRTF::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.



13
14
15
16
# File 'lib/rrtf/style/character_style.rb', line 13

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

Instance Method Details

#is_character_style?Boolean

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/rrtf/style/character_style.rb', line 20

def is_character_style?
   true
end

#prefix(document) ⇒ Object

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



56
57
58
59
60
61
62
63
# File 'lib/rrtf/style/character_style.rb', line 56

def prefix(document)
  text = StringIO.new

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

  text.string
end

#rtf_formatting(document) ⇒ Object



65
66
67
# File 'lib/rrtf/style/character_style.rb', line 65

def rtf_formatting(document)
  character_formatting_to_rtf(document)
end

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

Converts the stylesheet character style into its RTF representation (for stylesheet)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rrtf/style/character_style.rb', line 26

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 << "{\\*\\cs#{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