Module: RRTF::CharacterFormatting
- Included in:
- CharacterStyle, ParagraphStyle
- Defined in:
- lib/rrtf/style/formatting.rb
Overview
Encapsulates all character formatting methods shared between style types.
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
- #character_formatting_to_rtf(document) ⇒ Object
-
#initialize_character_formatting(options = {}) ⇒ Object
Initializes character formatting attributes.
- #push_colours(colours) ⇒ Object
- #push_fonts(fonts) ⇒ Object
- #set_character_formatting_from_hashmap(hash) ⇒ Object
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, | 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, | if .has_key?("to_rtf") rtf = ["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.
145 146 147 148 149 150 151 152 |
# File 'lib/rrtf/style/formatting.rb', line 145 def initialize_character_formatting( = {}) # load default attribute values CHARACTER_ATTRIBUTES.each do |key, | send("#{key}=", ["default"]) end # each # overwrite default attribute values with given values set_character_formatting_from_hashmap() 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 |