Module: RRTF::CharacterFormatting

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

Overview

Encapsulates all character formatting methods shared between style types.

Author:

  • Wesley Hileman

Constant Summary collapse

CHARACTER_ATTRIBUTES =
{
  # toggable attributes
  "bold" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\b' : '\b0') unless value.nil? }
  },
  "italic" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\i' : '\i0') unless value.nil? }
  },
  "underline" => {
    "default" => nil,
    "dictionary" => {
      "SINGLE" => "",
      "DOUBLE" => "db",
      "THICK" => "th",
      "DASH" => "dash",
      "LONG_DASH" => "ldash",
      "DOT" => "d",
      "DASH_DOT" => "dashd",
      "DASH_DOT_DOT" => "dashdd",
      "WAVE" => 'wave',
      "THICK_DASH" => "thdash",
      "THICK_LONG_DASH" => "thldash",
      "THICK_DOT" => "thd",
      "THICK_DASH_DOT" => "thdashd",
      "THICK_DASH_DOT_DOT" => "thdashdd",
      "THICK_WAVE" => 'hwave',
      "DOUBLE_WAVE" => 'uldbwave'
    },
    "to_rtf" => lambda do |value, document|
      return if value.nil?
      case value
      when TrueClass
        '\ul'
      when FalseClass
        '\ulnone'
      when String
        "\\ul#{value}"
      end # case
    end
  },
  "uppercase" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\caps' : '\caps0') unless value.nil? }
  },
  "superscript" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\super' : '\super0') unless value.nil? }
  },
  "subscript" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\sub' : '\sub0') unless value.nil? }
  },
  "strike" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\strike' : '\strike0') unless value.nil? }
  },
  "emboss" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\embo' : '\embo0') unless value.nil? }
  },
  "imprint" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\impr' : '\impr0') unless value.nil? }
  },
  "outline" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\outl' : '\outl0') unless value.nil? }
  },
  "text_hidden" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value ? '\v' : '\v0') unless value.nil? }
  },
  "kerning" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| (value.is_a?(Integer) ? "\\kerning#{value}" : '\kerning0') unless value.nil? }
  },
  # non-toggable attributes
  "character_spacing_offset" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\expnd#{value}" unless value.nil? }
  },
  "foreground_color" => {
    "default" => nil,
    "from_user" => lambda{ |value| value.is_a?(RRTF::Colour) ? value : RRTF::Colour.from_string(value) },
    "to_rtf" => lambda{ |value, document| "\\cf#{document.colours.index(value)}" unless value.nil? }
  },
  "background_color" => {
    "default" => nil,
    "from_user" => lambda{ |value| value.is_a?(RRTF::Colour) ? value : RRTF::Colour.from_string(value) },
    "to_rtf" => lambda{ |value, document| "\\cb#{document.colours.index(value)}" unless value.nil? }
  },
  "underline_color" => {
    "default" => nil,
    "from_user" => lambda{ |value| value.is_a?(RRTF::Colour) ? value : RRTF::Colour.from_string(value) },
    "to_rtf" => lambda{ |value, document| "\\ulc#{document.colours.index(value)}" unless value.nil? }
  },
  "font" => {
    "default" => nil,
    "from_user" => lambda{ |value| value.is_a?(RRTF::Font) ? value : RRTF::Font.from_string(value) },
    "to_rtf" => lambda{ |value, document| "\\f#{document.fonts.index(value)}" unless value.nil? }
  },
  "font_size" => {
    "default" => nil,
    "to_rtf" => lambda{ |value, document| "\\fs#{value}" unless value.nil? }
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/rrtf/style/formatting.rb', line 115

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

Instance Method Details

#character_formatting_to_rtf(document) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rrtf/style/formatting.rb', line 179

def character_formatting_to_rtf(document)
   text = StringIO.new

   # accumulate RTF representations of attributes
   CHARACTER_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_character_formatting(options = {}) ⇒ Object

Initializes character formatting attributes.

Parameters:

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

    the character formatting options.

Options Hash (options):

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



145
146
147
148
149
150
151
152
# File 'lib/rrtf/style/formatting.rb', line 145

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

#push_colours(colours) ⇒ Object



169
170
171
172
173
# File 'lib/rrtf/style/formatting.rb', line 169

def push_colours(colours)
  colours << foreground_color unless foreground_color.nil?
  colours << background_color unless background_color.nil?
  colours << underline_color unless underline_color.nil?
end

#push_fonts(fonts) ⇒ Object



175
176
177
# File 'lib/rrtf/style/formatting.rb', line 175

def push_fonts(fonts)
  fonts << font unless font.nil?
end

#set_character_formatting_from_hashmap(hash) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rrtf/style/formatting.rb', line 154

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