Class: WizRtf::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/wiz_rtf/text.rb

Overview

Represents Rtf text.

Constant Summary collapse

TEXT_ALIGN_MAP =
{left:'ql',center:'qc',right:'qr'}

Instance Method Summary collapse

Constructor Details

#initialize(str = '', styles = {}) ⇒ Text

creates a text of str to the document.

Styles:

  • text-align - sets the horizontal alignment of the text. optional values: :left, :center, :right

  • font-family - set the font family of the text. optional values:

  • font-size - set font size of the text.

  • font-bold - setting the value true for bold of the text.

  • font-italic - setting the value true for italic of the text.

  • font-underline - setting the value true for underline of the text.

Example:

WizRtf::Text.new(“A Example of Rtf Document”, ‘text-align’ => :center, ‘font-family’ => ‘Microsoft YaHei’, ‘font-size’ => 48, ‘font-bold’ => true, ‘font-italic’ => true, ‘font-underline’ => true)



25
26
27
28
# File 'lib/wiz_rtf/text.rb', line 25

def initialize(str = '', styles = {})
  @str = str
  @styles = {'text-align' => :left, 'font-family' => 0, 'font-size' => 24, 'font-bold' => false, 'font-italic' => false, 'font-underline' => false, 'foreground-color' => 0, 'background-color' => 0 }.merge(styles)
end

Instance Method Details

#render(io) ⇒ Object

Outputs the Partial Rtf Document to a Generic Stream as a Rich Text Format (RTF).

  • io - The Generic IO to Output the RTF Document.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wiz_rtf/text.rb', line 32

def render(io)
  io.group do
    io.cmd :pard
    io.cmd TEXT_ALIGN_MAP[@styles['text-align']]
    io.cmd :f, @styles['font-family']
    io.cmd :fs, @styles['font-size']
    io.cmd @styles['font-bold'] ? 'b' : 'b0'
    io.cmd @styles['font-italic'] ? 'i' : 'i0'
    io.cmd @styles['font-underline'] ? 'ul' : 'ulnone'
    io.cmd :cf, @styles['foreground-color']
    io.cmd :cb, @styles['background-color']
    io.cmd :chcfpat, @styles['foreground-color']
    io.cmd :chcbpat, @styles['background-color']
    io.txt @str
    io.cmd :par
  end
end