Module: RRTF::BorderFormatting
- Included in:
- BorderStyle
- Defined in:
- lib/rrtf/formatting.rb
Overview
Paragraph, table, and image border formatting attributes and methods.
Constant Summary
- BORDER_ATTRIBUTES =
Note:
The “sides” attribute must appear at the top of this hash so that #border_formatting_to_rtf generates correct RTF (borders are initiated by the “sides” attribute in the RTF spec).
Note:The “line_type” attribute must appear second in this hash so that #border_formatting_to_rtf generates correct RTF.
Formatting attributes that can be applied to borders of paragraphs, tables, & images.
{ "sides" => { "default" => "box", "dictionary" => { "ALL" => "box", "LEFT" => "brdrl", "RIGHT" => "brdrr", "TOP" => "brdrt", "BOTTOM" => "brdrb" }, "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? } }, "line_type" => { "default" => "brdrs", "dictionary" => { "SINGLE" => "brdrs", "THICK" => "brdrth", "DOUBLE" => "brdrdb", "DOT" => "brdrdot", "DASH" => "brdrdash", "HAIRLINE" => "brdrhair" }, "to_rtf" => lambda{ |value, document| "\\#{value}" unless value.nil? } }, "width" => { "default" => nil, "from_user" => lambda{ |value| RRTF::Utilities.value2twips(value) }, "to_rtf" => lambda{ |value, document| "\\brdrw#{value}" unless value.nil? } }, "spacing" => { "default" => nil, "from_user" => lambda{ |value| RRTF::Utilities.value2twips(value) }, "to_rtf" => lambda{ |value, document| "\\brsp#{value}" unless value.nil? } }, "color" => { "default" => nil, "from_user" => lambda{ |value| value.is_a?(RRTF::Colour) ? value : RRTF::Colour.from_string(value) }, "to_rtf" => lambda{ |value, document| "\\brdrcf#{document.colours.index(value)}" unless value.nil? } } }.freeze
Class Method Summary collapse
-
.included(base) ⇒ Object
Generates attribute accessors for all paragraph attributes when the module is included in another module or class.
Instance Method Summary collapse
-
#border_formatting_to_rtf(document) ⇒ Object
Generates an RTF string representing all applied border formatting.
-
#initialize_border_formatting(options = {}) ⇒ Object
Initializes border formatting attributes.
-
#set_border_formatting_from_hashmap(hash) ⇒ Object
Sets paragraph formatting attributes according to the supplied hashmap.
Class Method Details
.included(base) ⇒ Object
Generates attribute accessors for all paragraph attributes when the module is included in another module or class.
465 466 467 468 469 470 471 472 |
# File 'lib/rrtf/formatting.rb', line 465 def self.included(base) # define accessors in base for paragraph attributes base.class_eval do BORDER_ATTRIBUTES.each do |key, | attr_accessor :#{key}" end # each end # class_eval end |
Instance Method Details
#border_formatting_to_rtf(document) ⇒ Object
To generate correct RTF control words for colours and fonts, a document object must be provided to this method so that colour and font indicies may be found in the document's colour and font tables, respectively.
Generates an RTF string representing all applied border formatting.
514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/rrtf/formatting.rb', line 514 def border_formatting_to_rtf(document) text = StringIO.new # accumulate RTF representations of paragraph attributes BORDER_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_border_formatting(options = {}) ⇒ Object
Initializes border formatting attributes.
482 483 484 485 486 487 488 489 |
# File 'lib/rrtf/formatting.rb', line 482 def initialize_border_formatting( = {}) # load default attribute values BORDER_ATTRIBUTES.each do |key, | send("#{key}=", ["default"]) end # each # overwrite default attribute values with given values set_border_formatting_from_hashmap() end |
#set_border_formatting_from_hashmap(hash) ⇒ Object
Sets paragraph formatting attributes according to the supplied hashmap.
493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/rrtf/formatting.rb', line 493 def set_border_formatting_from_hashmap(hash) hash.each do |attribute, value| # skip unreconized attributes next unless(BORDER_ATTRIBUTES.keys.include?(attribute)) # preprocess value if nessesary if BORDER_ATTRIBUTES[attribute].has_key?("from_user") value = BORDER_ATTRIBUTES[attribute]["from_user"].call(value) elsif BORDER_ATTRIBUTES[attribute].has_key?("dictionary") && value.is_a?(String) value = BORDER_ATTRIBUTES[attribute]["dictionary"][value] end # if # set attribute value send("#{attribute}=", value) end # each end |