Class: WizRtf::Document

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Document

Returns a new instance of Document.



9
10
11
12
13
14
15
# File 'lib/wiz_rtf/document.rb', line 9

def initialize(&block)
  @fonts = []
  @colors = []
  @parts = []
  font 'Courier New'
  block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
end

Instance Method Details

#color(*rgb) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/wiz_rtf/document.rb', line 30

def color(*rgb)
  color = WizRtf::Color.new(*rgb)
  if index = @colors.index {|c| c.to_rgb_hex == color.to_rgb_hex}
    index += 1
  else
    @colors << color
    index = @colors.size
  end
  index
end

#font(name, family = nil, character_set = 0, prq = 2) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/wiz_rtf/document.rb', line 21

def font(name, family = nil,  character_set = 0, prq = 2)
  unless index = @fonts.index {|f| f.name == name}
    index = @fonts.size
    opts = WizRtf::Font::FONTS.detect {|f| f[:name] == name}
    @fonts << WizRtf::Font.new(index, opts[:name], opts[:family], opts[:character], opts[:prq])
  end
  index
end

#head(&block) ⇒ Object



17
18
19
# File 'lib/wiz_rtf/document.rb', line 17

def head(&block)
  block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
end

#image(file) ⇒ Object



48
49
50
# File 'lib/wiz_rtf/document.rb', line 48

def image(file)
  @parts << WizRtf::Image.new(file)
end

#line_breakObject



56
57
58
# File 'lib/wiz_rtf/document.rb', line 56

def line_break
  @parts << WizRtf::Cmd.new(:par)
end

#page_breakObject



60
61
62
# File 'lib/wiz_rtf/document.rb', line 60

def page_break
  @parts << WizRtf::Cmd.new(:page)
end

#render(io) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/wiz_rtf/document.rb', line 64

def render(io)
  io.group do
    io.cmd :rtf, 1
    io.cmd :ansi
    io.cmd :ansicpg, 2052
    io.cmd :deff, 0
    io.group do
      io.cmd :fonttbl
      @fonts.each do |font|
        font.render(io)
      end
    end
    io.group do
      io.cmd :colortbl
      io.delimit
      @colors.each do |color|
        color.render(io)
      end
    end
    @parts.each do |part|
      part.render(io)
    end
  end
end

#save(file) ⇒ Object



89
90
91
# File 'lib/wiz_rtf/document.rb', line 89

def save(file)
  File.open(file, 'w') { |file| render(WizRtf::RtfIO.new(file)) }
end

#table(rows = [], options = {}, &block) ⇒ Object



52
53
54
# File 'lib/wiz_rtf/document.rb', line 52

def table(rows = [],options = {}, &block)
  @parts << WizRtf::Table.new(rows, options, &block)
end

#text(str, styles = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/wiz_rtf/document.rb', line 41

def text(str, styles = {})
  styles['foreground-color'] = color(styles['foreground-color']) if styles['foreground-color']
  styles['background-color'] = color(styles['background-color']) if styles['background-color']
  styles['font-family'] = font(styles['font-family']) if styles['font-family']
  @parts << WizRtf::Text.new(str, styles)
end