clbustos-rtf 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +3 -0
  2. data/LICENSE +20 -0
  3. data/README +187 -0
  4. data/Rakefile +47 -0
  5. data/VERSION.yml +4 -0
  6. data/examples/example01.rb +51 -0
  7. data/examples/example02.rb +45 -0
  8. data/examples/example03.rb +66 -0
  9. data/examples/example03.rtf +164 -0
  10. data/examples/example04.rb +50 -0
  11. data/examples/rubyrtf.bmp +0 -0
  12. data/examples/rubyrtf.jpg +0 -0
  13. data/examples/rubyrtf.png +0 -0
  14. data/lib/rtf.rb +34 -0
  15. data/lib/rtf/colour.rb +173 -0
  16. data/lib/rtf/font.rb +173 -0
  17. data/lib/rtf/information.rb +112 -0
  18. data/lib/rtf/node.rb +1661 -0
  19. data/lib/rtf/paper.rb +55 -0
  20. data/lib/rtf/style.rb +305 -0
  21. data/ruby-rtf.gemspec +29 -0
  22. data/test/character_style_test.rb +136 -0
  23. data/test/colour_table_test.rb +93 -0
  24. data/test/colour_test.rb +116 -0
  25. data/test/command_node_test.rb +214 -0
  26. data/test/container_node_test.rb +64 -0
  27. data/test/document_style_test.rb +79 -0
  28. data/test/document_test.rb +106 -0
  29. data/test/fixtures/bitmap1.bmp +0 -0
  30. data/test/fixtures/bitmap2.bmp +0 -0
  31. data/test/fixtures/jpeg1.jpg +0 -0
  32. data/test/fixtures/jpeg2.jpg +0 -0
  33. data/test/fixtures/png1.png +0 -0
  34. data/test/fixtures/png2.png +0 -0
  35. data/test/font_table_test.rb +91 -0
  36. data/test/font_test.rb +48 -0
  37. data/test/footer_node_test.rb +30 -0
  38. data/test/header_node_test.rb +30 -0
  39. data/test/image_node_test.rb +125 -0
  40. data/test/information_test.rb +127 -0
  41. data/test/node_test.rb +25 -0
  42. data/test/paragraph_style_test.rb +81 -0
  43. data/test/style_test.rb +16 -0
  44. data/test/table_cell_node_test.rb +89 -0
  45. data/test/table_node_test.rb +83 -0
  46. data/test/table_row_node_test.rb +59 -0
  47. data/test/test_helper.rb +13 -0
  48. data/test/text_node_test.rb +50 -0
  49. metadata +135 -0
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+
3
+ # ColourTable class unit test class.
4
+ class ColourTableTest < Test::Unit::TestCase
5
+ def setup
6
+ @colours = [Colour.new(0, 0, 0),
7
+ Colour.new(100, 100, 100),
8
+ Colour.new(150, 150, 150),
9
+ Colour.new(200, 200, 200),
10
+ Colour.new(250, 250, 250)]
11
+ end
12
+
13
+ def test_01
14
+ tables = []
15
+ tables.push(ColourTable.new)
16
+ tables.push(ColourTable.new(@colours[0], @colours[2]))
17
+ tables.push(ColourTable.new(*@colours))
18
+ tables.push(ColourTable.new(@colours[0], @colours[1], @colours[0]))
19
+
20
+ assert(tables[0].size == 0)
21
+ assert(tables[1].size == 2)
22
+ assert(tables[2].size == 5)
23
+ assert(tables[3].size == 2)
24
+
25
+ assert(tables[0][0] == nil)
26
+ assert(tables[1][1] == @colours[2])
27
+ assert(tables[2][3] == @colours[3])
28
+ assert(tables[3][5] == nil)
29
+
30
+ assert(tables[0].index(@colours[1]) == nil)
31
+ assert(tables[1].index(@colours[2]) == 2)
32
+ assert(tables[2].index(@colours[3]) == 4)
33
+ assert(tables[3].index(@colours[4]) == nil)
34
+
35
+ tables[0].add(@colours[0])
36
+ assert(tables[0].size == 1)
37
+ assert(tables[0].index(@colours[0]) == 1)
38
+
39
+ tables[0] << @colours[1]
40
+ assert(tables[0].size == 2)
41
+ assert(tables[0].index(@colours[1]) == 2)
42
+
43
+ tables[0].add(@colours[1])
44
+ assert(tables[0].size == 2)
45
+ assert([tables[0][0], tables[0][1]] == [@colours[0], @colours[1]])
46
+
47
+ tables[0] << @colours[0]
48
+ assert(tables[0].size == 2)
49
+ assert([tables[0][0], tables[0][1]] == [@colours[0], @colours[1]])
50
+
51
+ flags = [false, false, false, false, false]
52
+ tables[2].each do |colour|
53
+ flags[@colours.index(colour)] = true if @colours.index(colour) != nil
54
+ end
55
+ assert(flags.index(false) == nil)
56
+
57
+ assert(tables[0].to_s(2) == " Colour Table (2 colours)\n"\
58
+ " Colour (0/0/0)\n"\
59
+ " Colour (100/100/100)")
60
+ assert(tables[1].to_s(4) == " Colour Table (2 colours)\n"\
61
+ " Colour (0/0/0)\n"\
62
+ " Colour (150/150/150)")
63
+ assert(tables[2].to_s(-6) == "Colour Table (5 colours)\n"\
64
+ " Colour (0/0/0)\n"\
65
+ " Colour (100/100/100)\n"\
66
+ " Colour (150/150/150)\n"\
67
+ " Colour (200/200/200)\n"\
68
+ " Colour (250/250/250)")
69
+ assert(tables[3].to_s == "Colour Table (2 colours)\n"\
70
+ " Colour (0/0/0)\n"\
71
+ " Colour (100/100/100)")
72
+
73
+ assert(tables[0].to_rtf(3) == " {\\colortbl\n ;\n"\
74
+ " \\red0\\green0\\blue0;\n"\
75
+ " \\red100\\green100\\blue100;\n"\
76
+ " }")
77
+ assert(tables[1].to_rtf(6) == " {\\colortbl\n ;\n"\
78
+ " \\red0\\green0\\blue0;\n"\
79
+ " \\red150\\green150\\blue150;\n"\
80
+ " }")
81
+ assert(tables[2].to_rtf(-9) == "{\\colortbl\n;\n"\
82
+ "\\red0\\green0\\blue0;\n"\
83
+ "\\red100\\green100\\blue100;\n"\
84
+ "\\red150\\green150\\blue150;\n"\
85
+ "\\red200\\green200\\blue200;\n"\
86
+ "\\red250\\green250\\blue250;\n"\
87
+ "}")
88
+ assert(tables[3].to_rtf == "{\\colortbl\n;\n"\
89
+ "\\red0\\green0\\blue0;\n"\
90
+ "\\red100\\green100\\blue100;\n"\
91
+ "}")
92
+ end
93
+ end
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+
3
+ # Colour class unit test class.
4
+ class ColourTest < Test::Unit::TestCase
5
+ def test_01
6
+ colours = []
7
+ colours.push(Colour.new(255, 255, 255))
8
+ colours.push(Colour.new(200, 0, 0))
9
+ colours.push(Colour.new(0, 150, 0))
10
+ colours.push(Colour.new(0, 0, 100))
11
+ colours.push(Colour.new(0, 0, 0))
12
+
13
+ assert(colours[0] == colours[0])
14
+ assert(!(colours[1] == colours[2]))
15
+ assert(colours[3] == Colour.new(0, 0, 100))
16
+ assert(!(colours[4] == 12345))
17
+
18
+ assert(colours[0].red == 255)
19
+ assert(colours[0].green == 255)
20
+ assert(colours[0].blue == 255)
21
+
22
+ assert(colours[1].red == 200)
23
+ assert(colours[1].green == 0)
24
+ assert(colours[1].blue == 0)
25
+
26
+ assert(colours[2].red == 0)
27
+ assert(colours[2].green == 150)
28
+ assert(colours[2].blue == 0)
29
+
30
+ assert(colours[3].red == 0)
31
+ assert(colours[3].green == 0)
32
+ assert(colours[3].blue == 100)
33
+
34
+ assert(colours[4].red == 0)
35
+ assert(colours[4].green == 0)
36
+ assert(colours[4].blue == 0)
37
+
38
+ assert(colours[0].to_s(3) == ' Colour (255/255/255)')
39
+ assert(colours[1].to_s(6) == ' Colour (200/0/0)')
40
+ assert(colours[2].to_s(-20) == 'Colour (0/150/0)')
41
+ assert(colours[3].to_s == 'Colour (0/0/100)')
42
+ assert(colours[4].to_s == 'Colour (0/0/0)')
43
+
44
+ assert(colours[0].to_rtf(2) == ' \red255\green255\blue255;')
45
+ assert(colours[1].to_rtf(4) == ' \red200\green0\blue0;')
46
+ assert(colours[2].to_rtf(-6) == '\red0\green150\blue0;')
47
+ assert(colours[3].to_rtf == '\red0\green0\blue100;')
48
+ assert(colours[4].to_rtf == '\red0\green0\blue0;')
49
+
50
+ begin
51
+ Colour.new(256, 0, 0)
52
+ flunk("Successfully created a colour with an invalid red setting.")
53
+ rescue RTFError
54
+ rescue Test::Unit::AssertionFailedError => error
55
+ raise error
56
+ rescue => error
57
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
58
+ "Message: #{error.message}")
59
+ end
60
+
61
+ begin
62
+ Colour.new(0, 1000, 0)
63
+ flunk("Successfully created a colour with an invalid green setting.")
64
+ rescue RTFError
65
+ rescue Test::Unit::AssertionFailedError => error
66
+ raise error
67
+ rescue => error
68
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
69
+ "Message: #{error.message}")
70
+ end
71
+
72
+ begin
73
+ Colour.new(0, 0, -300)
74
+ flunk("Successfully created a colour with an invalid blue setting.")
75
+ rescue RTFError
76
+ rescue Test::Unit::AssertionFailedError => error
77
+ raise error
78
+ rescue => error
79
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
80
+ "Message: #{error.message}")
81
+ end
82
+
83
+ begin
84
+ Colour.new('La la la', 0, 0)
85
+ flunk("Successfully created a colour with an invalid red type.")
86
+ rescue RTFError
87
+ rescue Test::Unit::AssertionFailedError => error
88
+ raise error
89
+ rescue => error
90
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
91
+ "Message: #{error.message}")
92
+ end
93
+
94
+ begin
95
+ Colour.new(0, {}, 0)
96
+ flunk("Successfully created a colour with an invalid green type.")
97
+ rescue RTFError
98
+ rescue Test::Unit::AssertionFailedError => error
99
+ raise error
100
+ rescue => error
101
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
102
+ "Message: #{error.message}")
103
+ end
104
+
105
+ begin
106
+ Colour.new(0, 0, [])
107
+ flunk("Successfully created a colour with an invalid blue type.")
108
+ rescue RTFError
109
+ rescue Test::Unit::AssertionFailedError => error
110
+ raise error
111
+ rescue => error
112
+ flunk("Unexpected exception caught. Type: #{error.class.name}\n"\
113
+ "Message: #{error.message}")
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,214 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class CommandNodeTest < Test::Unit::TestCase
5
+ def test_basics
6
+ nodes = []
7
+ nodes.push(CommandNode.new(nil, 'prefix'))
8
+ nodes.push(CommandNode.new(nil, '', 'lalala'))
9
+ nodes.push(CommandNode.new(nodes[0], '', nil, false))
10
+
11
+ assert(nodes[0].prefix == 'prefix')
12
+ assert(nodes[1].prefix == '')
13
+ assert(nodes[2].prefix == '')
14
+
15
+ assert(nodes[0].suffix == nil)
16
+ assert(nodes[1].suffix == 'lalala')
17
+ assert(nodes[2].suffix == nil)
18
+
19
+ assert(nodes[0].split == true)
20
+ assert(nodes[1].split == true)
21
+ assert(nodes[2].split == false)
22
+ end
23
+
24
+ # Test line breaks.
25
+ def test_line_break
26
+ root = CommandNode.new(nil, nil)
27
+
28
+ assert(root.line_break == nil)
29
+ assert(root.size == 1)
30
+ assert(root[0].class == CommandNode)
31
+ assert(root[0].prefix == '\line')
32
+ assert(root[0].suffix == nil)
33
+ assert(root[0].split == false)
34
+ end
35
+
36
+ # Test paragraphs.
37
+ def test_paragraph
38
+ root = CommandNode.new(nil, nil)
39
+ style = ParagraphStyle.new
40
+
41
+ assert(root.paragraph(style) != nil)
42
+ assert(root.size == 1)
43
+ assert(root[0].class == CommandNode)
44
+ assert(root[0].prefix == '\pard\ql')
45
+ assert(root[0].suffix == '\par')
46
+ assert(root.split == true)
47
+
48
+ style.justification = ParagraphStyle::RIGHT_JUSTIFY
49
+ assert(root.paragraph(style).prefix == '\pard\qr')
50
+
51
+ style.justification = ParagraphStyle::CENTRE_JUSTIFY
52
+ assert(root.paragraph(style).prefix == '\pard\qc')
53
+
54
+ style.justification = ParagraphStyle::FULL_JUSTIFY
55
+ assert(root.paragraph(style).prefix == '\pard\qj')
56
+
57
+ style.justification = ParagraphStyle::LEFT_JUSTIFY
58
+ style.space_before = 100
59
+ root.paragraph(style)
60
+ assert(root[-1].prefix == '\pard\ql\sb100')
61
+
62
+ style.space_before = nil
63
+ style.space_after = 1234
64
+ root.paragraph(style)
65
+ assert(root[-1].prefix == '\pard\ql\sa1234')
66
+
67
+ style.space_before = nil
68
+ style.space_after = nil
69
+ style.left_indent = 10
70
+ root.paragraph(style)
71
+ assert(root[-1].prefix == '\pard\ql\li10')
72
+
73
+ style.space_before = nil
74
+ style.space_after = nil
75
+ style.left_indent = nil
76
+ style.right_indent = 234
77
+ root.paragraph(style)
78
+ assert(root[-1].prefix == '\pard\ql\ri234')
79
+
80
+ style.space_before = nil
81
+ style.space_after = nil
82
+ style.left_indent = nil
83
+ style.right_indent = nil
84
+ style.first_line_indent = 765
85
+ root.paragraph(style)
86
+ assert(root[-1].prefix == '\pard\ql\fi765')
87
+
88
+ style.space_before = 12
89
+ style.space_after = 23
90
+ style.left_indent = 34
91
+ style.right_indent = 45
92
+ style.first_line_indent = 56
93
+ root.paragraph(style)
94
+ assert(root[-1].prefix == '\pard\ql\li34\ri45\fi56\sb12\sa23')
95
+
96
+ node = nil
97
+ root.paragraph(style) {|n| node = n}
98
+ assert(node == root[-1])
99
+ end
100
+
101
+ # Test applications of styles.
102
+ def test_style
103
+ root = Document.new(Font.new(Font::ROMAN, 'Arial'))
104
+ style = CharacterStyle.new
105
+ style.bold = true
106
+
107
+ assert(root.apply(style) != nil)
108
+ assert(root.size == 1)
109
+ assert(root[0].class == CommandNode)
110
+ assert(root[0].prefix == '\b')
111
+ assert(root[0].suffix == nil)
112
+ assert(root[0].split == true)
113
+
114
+ style.underline = true
115
+ assert(root.apply(style).prefix == '\b\ul')
116
+
117
+ style.bold = false
118
+ style.superscript = true
119
+ assert(root.apply(style).prefix == '\ul\super')
120
+
121
+ style.underline = false
122
+ style.italic = true
123
+ assert(root.apply(style).prefix == '\i\super')
124
+
125
+ style.italic = false
126
+ style.bold = true
127
+ assert(root.apply(style).prefix == '\b\super')
128
+
129
+ style.bold = false
130
+ style.superscript = false
131
+ style.italic = true
132
+ style.font_size = 20
133
+ assert(root.apply(style).prefix == '\i\fs20')
134
+
135
+ node = nil
136
+ root.apply(style) {|n| node = n}
137
+ assert(node == root[-1])
138
+
139
+ # Test style short cuts.
140
+ node = root.bold
141
+ assert(node.prefix == '\b')
142
+ assert(node.suffix == nil)
143
+ assert(node == root[-1])
144
+
145
+ node = root.italic
146
+ assert(node.prefix == '\i')
147
+ assert(node.suffix == nil)
148
+ assert(node == root[-1])
149
+
150
+ node = root.underline
151
+ assert(node.prefix == '\ul')
152
+ assert(node.suffix == nil)
153
+ assert(node == root[-1])
154
+
155
+ node = root.superscript
156
+ assert(node.prefix == '\super')
157
+ assert(node.suffix == nil)
158
+ assert(node == root[-1])
159
+ end
160
+
161
+ # Test text node addition.
162
+ def test_text
163
+ root = CommandNode.new(nil, nil)
164
+
165
+ root << 'A block of text.'
166
+ assert(root.size == 1)
167
+ assert(root[0].class == TextNode)
168
+ assert(root[0].text == 'A block of text.')
169
+
170
+ root << " More text."
171
+ assert(root.size == 1)
172
+ assert(root[0].class == TextNode)
173
+ assert(root[0].text == 'A block of text. More text.')
174
+
175
+ root.paragraph
176
+ root << "A new node."
177
+ assert(root.size == 3)
178
+ assert(root[0].class == TextNode)
179
+ assert(root[-1].class == TextNode)
180
+ assert(root[0].text == 'A block of text. More text.')
181
+ assert(root[-1].text == 'A new node.')
182
+ end
183
+
184
+ # Test table addition.
185
+ def test_table
186
+ root = CommandNode.new(nil, nil)
187
+
188
+ table = root.table(3, 3, 100, 150, 200)
189
+ assert(root.size == 1)
190
+ assert(root[0].class == TableNode)
191
+ assert(root[0] == table)
192
+ assert(table.rows == 3)
193
+ assert(table.columns == 3)
194
+
195
+ assert(table[0][0].width == 100)
196
+ assert(table[0][1].width == 150)
197
+ assert(table[0][2].width == 200)
198
+ end
199
+
200
+ # This test checks the previous_node and next_node methods that could not be
201
+ # fully and properly checked in the NodeTest.rb file.
202
+ def test_peers
203
+ root = Document.new(Font.new(Font::ROMAN, 'Arial'))
204
+ nodes = []
205
+ nodes.push(root.paragraph)
206
+ nodes.push(root.bold)
207
+ nodes.push(root.underline)
208
+
209
+ assert(root.previous_node == nil)
210
+ assert(root.next_node == nil)
211
+ assert(nodes[1].previous_node == nodes[0])
212
+ assert(nodes[1].next_node == nodes[2])
213
+ end
214
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class ContainerNodeTest < Test::Unit::TestCase
5
+ def test01
6
+ nodes = []
7
+ nodes.push(ContainerNode.new(nil))
8
+ nodes.push(ContainerNode.new(nodes[0]))
9
+
10
+ assert(nodes[0].size == 0)
11
+ assert(nodes[1].size == 0)
12
+
13
+ assert(nodes[0].first == nil)
14
+ assert(nodes[0].last == nil)
15
+ assert(nodes[1].first == nil)
16
+ assert(nodes[1].last == nil)
17
+
18
+ assert(nodes[0][0] == nil)
19
+ assert(nodes[1][-1] == nil)
20
+
21
+ count = 0
22
+ nodes[0].each {|entry| count += 1}
23
+ assert(count == 0)
24
+ nodes[1].each {|entry| count += 1}
25
+ assert(count == 0)
26
+ end
27
+
28
+ def test02
29
+ node = ContainerNode.new(nil)
30
+ child1 = ContainerNode.new(nil)
31
+ child2 = ContainerNode.new(nil)
32
+
33
+ node.store(child1)
34
+ assert(node.size == 1)
35
+ assert(node[0] == child1)
36
+ assert(node[-1] == child1)
37
+ assert(node[0].parent == node)
38
+ assert(node.first == child1)
39
+ assert(node.last == child1)
40
+
41
+ count = 0
42
+ node.each {|entry| count += 1}
43
+ assert(count == 1)
44
+
45
+ node.store(child2)
46
+ assert(node.size == 2)
47
+ assert(node[0] == child1)
48
+ assert(node[1] == child2)
49
+ assert(node[-1] == child2)
50
+ assert(node[-2] == child1)
51
+ assert(node[0].parent == node)
52
+ assert(node[1].parent == node)
53
+ assert(node.first == child1)
54
+ assert(node.last == child2)
55
+ end
56
+
57
+ def test03
58
+ begin
59
+ ContainerNode.new(nil).to_rtf
60
+ flunk("Successfully called ContainerNode#to_rtf().")
61
+ rescue => error
62
+ end
63
+ end
64
+ end