panmind-rtf 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/CHANGES +8 -0
  2. data/LICENSE +20 -0
  3. data/README +186 -0
  4. data/Rakefile +48 -0
  5. data/VERSION.yml +5 -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 +111 -0
  18. data/lib/rtf/node.rb +1680 -0
  19. data/lib/rtf/paper.rb +55 -0
  20. data/lib/rtf/style.rb +305 -0
  21. data/test/character_style_test.rb +136 -0
  22. data/test/colour_table_test.rb +93 -0
  23. data/test/colour_test.rb +116 -0
  24. data/test/command_node_test.rb +219 -0
  25. data/test/container_node_test.rb +64 -0
  26. data/test/document_style_test.rb +79 -0
  27. data/test/document_test.rb +106 -0
  28. data/test/fixtures/bitmap1.bmp +0 -0
  29. data/test/fixtures/bitmap2.bmp +0 -0
  30. data/test/fixtures/jpeg1.jpg +0 -0
  31. data/test/fixtures/jpeg2.jpg +0 -0
  32. data/test/fixtures/png1.png +0 -0
  33. data/test/fixtures/png2.png +0 -0
  34. data/test/font_table_test.rb +91 -0
  35. data/test/font_test.rb +48 -0
  36. data/test/footer_node_test.rb +30 -0
  37. data/test/header_node_test.rb +30 -0
  38. data/test/image_node_test.rb +125 -0
  39. data/test/information_test.rb +127 -0
  40. data/test/node_test.rb +25 -0
  41. data/test/paragraph_style_test.rb +81 -0
  42. data/test/style_test.rb +16 -0
  43. data/test/table_cell_node_test.rb +89 -0
  44. data/test/table_node_test.rb +83 -0
  45. data/test/table_row_node_test.rb +59 -0
  46. data/test/test_helper.rb +13 -0
  47. data/test/text_node_test.rb +50 -0
  48. metadata +133 -0
@@ -0,0 +1,127 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class InformationTest < Test::Unit::TestCase
5
+ def test_01
6
+ date = Time.local(1985, 6, 22, 14, 33, 22)
7
+ info = []
8
+ info.push(Information.new)
9
+ info.push(Information.new('Title 1'))
10
+ info.push(Information.new('Title 2', 'Peter Wood'))
11
+ info.push(Information.new('La la la', '', 'Nowhere Ltd.'))
12
+ info.push(Information.new('', 'N. O. Body', 'Oobly', 'Joobly'))
13
+ info.push(Information.new('Title 5', 'J. Bloggs', '', '', date))
14
+ info.push(Information.new('Title 6', 'J. Bloggs', '', '', '1985-06-22 14:33:22 GMT'))
15
+
16
+ assert(info[0].title == nil)
17
+ assert(info[0].author == nil)
18
+ assert(info[0].company == nil)
19
+ assert(info[0].comments == nil)
20
+
21
+ assert(info[1].title == 'Title 1')
22
+ assert(info[1].author == nil)
23
+ assert(info[1].company == nil)
24
+ assert(info[1].comments == nil)
25
+
26
+ assert(info[2].title == 'Title 2')
27
+ assert(info[2].author == 'Peter Wood')
28
+ assert(info[2].company == nil)
29
+ assert(info[2].comments == nil)
30
+
31
+ assert(info[3].title == 'La la la')
32
+ assert(info[3].author == '')
33
+ assert(info[3].company == 'Nowhere Ltd.')
34
+ assert(info[3].comments == nil)
35
+
36
+ assert(info[4].title == '')
37
+ assert(info[4].author == 'N. O. Body')
38
+ assert(info[4].company == 'Oobly')
39
+ assert(info[4].comments == 'Joobly')
40
+
41
+ assert(info[5].title == 'Title 5')
42
+ assert(info[5].author == 'J. Bloggs')
43
+ assert(info[5].company == '')
44
+ assert(info[5].comments == '')
45
+ assert(info[5].created == date)
46
+
47
+ assert(info[6].title == 'Title 6')
48
+ assert(info[6].author == 'J. Bloggs')
49
+ assert(info[6].company == '')
50
+ assert(info[6].comments == '')
51
+ assert(info[6].created == date)
52
+
53
+ info[6].title = 'Alternative Title'
54
+ assert(info[6].title == 'Alternative Title')
55
+
56
+ info[6].author = 'A. Person'
57
+ assert(info[6].author == 'A. Person')
58
+
59
+ info[6].company = nil
60
+ assert(info[6].company == nil)
61
+
62
+ info[6].comments = 'New information comment text.'
63
+ assert(info[6].comments == 'New information comment text.')
64
+
65
+ date = Time.new
66
+ info[6].created = date
67
+ assert(info[6].created == date)
68
+
69
+ date = Time.local(1985, 6, 22, 14, 33, 22)
70
+ info[6].created = '1985-06-22 14:33:22 GMT'
71
+ assert(info[6].created == date)
72
+
73
+ assert(info[0].to_s(2) == " Information\n Created: "\
74
+ "#{info[0].created}")
75
+ assert(info[1].to_s(4) == " Information\n Title: Title 1\n"\
76
+ " Created: #{info[1].created}")
77
+ assert(info[2].to_s(-10) == "Information\n Title: Title 2\n "\
78
+ "Author: Peter Wood\n Created: "\
79
+ "#{info[2].created}")
80
+ assert(info[3].to_s == "Information\n Title: La la la\n "\
81
+ "Author: \n Company: Nowhere Ltd.\n "\
82
+ "Created: #{info[3].created}")
83
+ assert(info[4].to_s == "Information\n Title: \n Author: "\
84
+ "N. O. Body\n Company: Oobly\n Comments: "\
85
+ "Joobly\n Created: #{info[4].created}")
86
+ assert(info[5].to_s == "Information\n Title: Title 5\n Author: "\
87
+ "J. Bloggs\n Company: \n Comments: \n "\
88
+ "Created: #{date}")
89
+ assert(info[6].to_s == "Information\n Title: Alternative Title"\
90
+ "\n Author: A. Person\n Comments: New "\
91
+ "information comment text.\n Created: #{date}")
92
+
93
+ assert(info[0].to_rtf(3) == " {\\info\n #{to_rtf(info[0].created)}"\
94
+ "\n }")
95
+ assert(info[1].to_rtf(6) == " {\\info\n {\\title Title 1}\n"\
96
+ " #{to_rtf(info[1].created)}\n }")
97
+ assert(info[2].to_rtf(-5) == "{\\info\n{\\title Title 2}\n"\
98
+ "{\\author Peter Wood}\n"\
99
+ "#{to_rtf(info[2].created)}\n}")
100
+ assert(info[3].to_rtf == "{\\info\n{\\title La la la}\n"\
101
+ "{\\author }\n"\
102
+ "{\\company Nowhere Ltd.}\n"\
103
+ "#{to_rtf(info[3].created)}\n}")
104
+ assert(info[4].to_rtf == "{\\info\n{\\title }\n"\
105
+ "{\\author N. O. Body}\n"\
106
+ "{\\company Oobly}\n"\
107
+ "{\\doccomm Joobly}\n"\
108
+ "#{to_rtf(info[4].created)}\n}")
109
+ assert(info[5].to_rtf(3) == " {\\info\n {\\title Title 5}\n"\
110
+ " {\\author J. Bloggs}\n"\
111
+ " {\\company }\n"\
112
+ " {\\doccomm }\n"\
113
+ " #{to_rtf(date)}\n }")
114
+ assert(info[6].to_rtf == "{\\info\n{\\title Alternative Title}\n"\
115
+ "{\\author A. Person}\n"\
116
+ "{\\doccomm New information comment text.}\n"\
117
+ "#{to_rtf(date)}\n}")
118
+ end
119
+
120
+ def to_rtf(time)
121
+ text = StringIO.new
122
+ text << "{\\createim\\yr#{time.year}"
123
+ text << "\\mo#{time.month}\\dy#{time.day}"
124
+ text << "\\hr#{time.hour}\\min#{time.min}}"
125
+ text.string
126
+ end
127
+ end
data/test/node_test.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class NodeTest < Test::Unit::TestCase
5
+ def test01
6
+ nodes = []
7
+ nodes.push(Node.new(nil))
8
+ nodes.push(Node.new(nodes[0]))
9
+
10
+ assert(nodes[0].parent == nil)
11
+ assert(nodes[1].parent != nil)
12
+ assert(nodes[1].parent == nodes[0])
13
+
14
+ assert(nodes[0].is_root?)
15
+ assert(nodes[1].is_root? == false)
16
+
17
+ assert(nodes[0].root == nodes[0])
18
+ assert(nodes[1].root == nodes[0])
19
+
20
+ assert(nodes[0].previous_node == nil)
21
+ assert(nodes[0].next_node == nil)
22
+ assert(nodes[1].previous_node == nil)
23
+ assert(nodes[1].next_node == nil)
24
+ end
25
+ end
@@ -0,0 +1,81 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class ParagraphStyleTest < Test::Unit::TestCase
5
+ def test_basics
6
+ style = ParagraphStyle.new
7
+
8
+ assert(style.is_character_style? == false)
9
+ assert(style.is_document_style? == false)
10
+ assert(style.is_paragraph_style? == true)
11
+ assert(style.is_table_style? == false)
12
+
13
+ assert(style.prefix(nil, nil) == '\ql')
14
+ assert(style.suffix(nil, nil) == nil)
15
+
16
+ assert(style.first_line_indent == nil)
17
+ assert(style.flow == ParagraphStyle::LEFT_TO_RIGHT)
18
+ assert(style.justification == ParagraphStyle::LEFT_JUSTIFY)
19
+ assert(style.left_indent == nil)
20
+ assert(style.right_indent == nil)
21
+ assert(style.line_spacing == nil)
22
+ assert(style.space_after == nil)
23
+ assert(style.space_before == nil)
24
+ end
25
+
26
+ def test_mutators
27
+ style = ParagraphStyle.new
28
+
29
+ style.first_line_indent = 100
30
+ assert(style.first_line_indent == 100)
31
+
32
+ style.flow = ParagraphStyle::RIGHT_TO_LEFT
33
+ assert(style.flow == ParagraphStyle::RIGHT_TO_LEFT)
34
+
35
+ style.justification = ParagraphStyle::RIGHT_JUSTIFY
36
+ assert(style.justification == ParagraphStyle::RIGHT_JUSTIFY)
37
+
38
+ style.left_indent = 234
39
+ assert(style.left_indent == 234)
40
+
41
+ style.right_indent = 1020
42
+ assert(style.right_indent == 1020)
43
+
44
+ style.line_spacing = 645
45
+ assert(style.line_spacing == 645)
46
+
47
+ style.space_after = 25
48
+ assert(style.space_after == 25)
49
+
50
+ style.space_before = 918
51
+ assert(style.space_before == 918)
52
+ end
53
+
54
+ def test_prefix
55
+ style = ParagraphStyle.new
56
+
57
+ style.first_line_indent = 100
58
+ assert(style.prefix(nil, nil) == '\ql\fi100')
59
+
60
+ style.flow = ParagraphStyle::RIGHT_TO_LEFT
61
+ assert(style.prefix(nil, nil) == '\ql\fi100\rtlpar')
62
+
63
+ style.justification = ParagraphStyle::RIGHT_JUSTIFY
64
+ assert(style.prefix(nil, nil) == '\qr\fi100\rtlpar')
65
+
66
+ style.left_indent = 234
67
+ assert(style.prefix(nil, nil) == '\qr\li234\fi100\rtlpar')
68
+
69
+ style.right_indent = 1020
70
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\rtlpar')
71
+
72
+ style.line_spacing = 645
73
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\sl645\rtlpar')
74
+
75
+ style.space_after = 25
76
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\sa25\sl645\rtlpar')
77
+
78
+ style.space_before = 918
79
+ assert(style.prefix(nil, nil) == '\qr\li234\ri1020\fi100\sb918\sa25\sl645\rtlpar')
80
+ end
81
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # Information class unit test class.
4
+ class StyleTest < Test::Unit::TestCase
5
+ def test_basics
6
+ style = Style.new
7
+
8
+ assert(style.is_character_style? == false)
9
+ assert(style.is_document_style? == false)
10
+ assert(style.is_paragraph_style? == false)
11
+ assert(style.is_table_style? == false)
12
+
13
+ assert(style.prefix(nil, nil) == nil)
14
+ assert(style.suffix(nil, nil) == nil)
15
+ end
16
+ end
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class TableCellNodeTest < Test::Unit::TestCase
5
+ def setup
6
+ @table = TableNode.new(nil, 3, 3, 100, 100, 100)
7
+ @row = TableRowNode.new(@table, 3, 100)
8
+ end
9
+
10
+ def test_basics
11
+ cells = []
12
+ cells.push(TableCellNode.new(@row))
13
+ cells.push(TableCellNode.new(@row, 1000))
14
+ cells.push(TableCellNode.new(@row, 250, nil, 5, 10, 15, 20))
15
+
16
+ assert(cells[0].parent == @row)
17
+ assert(cells[0].width == TableCellNode::DEFAULT_WIDTH)
18
+ assert(cells[0].top_border_width == 0)
19
+ assert(cells[0].right_border_width == 0)
20
+ assert(cells[0].bottom_border_width == 0)
21
+ assert(cells[0].left_border_width == 0)
22
+
23
+ assert(cells[1].parent == @row)
24
+ assert(cells[1].width == 1000)
25
+ assert(cells[1].top_border_width == 0)
26
+ assert(cells[1].right_border_width == 0)
27
+ assert(cells[1].bottom_border_width == 0)
28
+ assert(cells[1].left_border_width == 0)
29
+
30
+ assert(cells[2].parent == @row)
31
+ assert(cells[2].width == 250)
32
+ assert(cells[2].top_border_width == 5)
33
+ assert(cells[2].right_border_width == 10)
34
+ assert(cells[2].bottom_border_width == 15)
35
+ assert(cells[2].left_border_width == 20)
36
+
37
+ cells[0].top_border_width = 25
38
+ cells[0].bottom_border_width = 1
39
+ cells[0].left_border_width = 89
40
+ cells[0].right_border_width = 57
41
+
42
+ assert(cells[0].top_border_width == 25)
43
+ assert(cells[0].right_border_width == 57)
44
+ assert(cells[0].bottom_border_width == 1)
45
+ assert(cells[0].left_border_width == 89)
46
+
47
+ cells[0].top_border_width = 0
48
+ cells[0].bottom_border_width = nil
49
+ cells[0].left_border_width = -5
50
+ cells[0].right_border_width = -1000
51
+
52
+ assert(cells[0].top_border_width == 0)
53
+ assert(cells[0].right_border_width == 0)
54
+ assert(cells[0].bottom_border_width == 0)
55
+ assert(cells[0].left_border_width == 0)
56
+
57
+ assert(cells[2].border_widths == [5, 10, 15, 20])
58
+ end
59
+
60
+ def test_exceptions
61
+ begin
62
+ @row[0].paragraph
63
+ flunk("Successfully called the TableCellNode#paragraph method.")
64
+ rescue
65
+ end
66
+
67
+ begin
68
+ @row[0].parent = nil
69
+ flunk("Successfully called the TableCellNode#parent= method.")
70
+ rescue
71
+ end
72
+
73
+ begin
74
+ @row[0].table
75
+ flunk("Successfully called the TableCellNode#table method.")
76
+ rescue
77
+ end
78
+ end
79
+
80
+ def test_rtf_generation
81
+ cells = []
82
+ cells.push(TableCellNode.new(@row))
83
+ cells.push(TableCellNode.new(@row))
84
+ cells[1] << "Added text."
85
+
86
+ assert(cells[0].to_rtf == "\\pard\\intbl\n\n\\cell")
87
+ assert(cells[1].to_rtf == "\\pard\\intbl\nAdded text.\n\\cell")
88
+ end
89
+ end
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class TableNodeTest < Test::Unit::TestCase
5
+ def setup
6
+ @document = Document.new(Font.new(Font::ROMAN, 'Times New Roman'))
7
+ @colours = []
8
+
9
+ @colours << Colour.new(200, 200, 200)
10
+ @colours << Colour.new(200, 0, 0)
11
+ @colours << Colour.new(0, 200, 0)
12
+ @colours << Colour.new(0, 0, 200)
13
+ end
14
+
15
+ def test_basics
16
+ table = TableNode.new(@document, 3, 5, 10, 20, 30, 40, 50)
17
+
18
+ assert(table.rows == 3)
19
+ assert(table.columns == 5)
20
+ assert(table.size == 3)
21
+ assert(table.cell_margin == 100)
22
+ end
23
+
24
+ def test_mutators
25
+ table = TableNode.new(@document, 3, 3)
26
+
27
+ table.cell_margin = 250
28
+ assert(table.cell_margin == 250)
29
+ end
30
+
31
+ def test_colouring
32
+ table = TableNode.new(@document, 3, 3)
33
+
34
+ table.row_shading_colour(1, @colours[0])
35
+ assert(table[0][0].shading_colour == nil)
36
+ assert(table[0][1].shading_colour == nil)
37
+ assert(table[0][2].shading_colour == nil)
38
+ assert(table[1][0].shading_colour == @colours[0])
39
+ assert(table[1][1].shading_colour == @colours[0])
40
+ assert(table[1][2].shading_colour == @colours[0])
41
+ assert(table[2][0].shading_colour == nil)
42
+ assert(table[2][1].shading_colour == nil)
43
+ assert(table[2][2].shading_colour == nil)
44
+
45
+ table.column_shading_colour(2, @colours[1])
46
+ assert(table[0][0].shading_colour == nil)
47
+ assert(table[0][1].shading_colour == nil)
48
+ assert(table[0][2].shading_colour == @colours[1])
49
+ assert(table[1][0].shading_colour == @colours[0])
50
+ assert(table[1][1].shading_colour == @colours[0])
51
+ assert(table[1][2].shading_colour == @colours[1])
52
+ assert(table[2][0].shading_colour == nil)
53
+ assert(table[2][1].shading_colour == nil)
54
+ assert(table[2][2].shading_colour == @colours[1])
55
+
56
+ table.shading_colour(@colours[2]) {|cell, x, y| x == y}
57
+ assert(table[0][0].shading_colour == @colours[2])
58
+ assert(table[0][1].shading_colour == nil)
59
+ assert(table[0][2].shading_colour == @colours[1])
60
+ assert(table[1][0].shading_colour == @colours[0])
61
+ assert(table[1][1].shading_colour == @colours[2])
62
+ assert(table[1][2].shading_colour == @colours[1])
63
+ assert(table[2][0].shading_colour == nil)
64
+ assert(table[2][1].shading_colour == nil)
65
+ assert(table[2][2].shading_colour == @colours[2])
66
+ end
67
+
68
+ def test_border_width
69
+ table = TableNode.new(@document, 2, 2)
70
+
71
+ table.border_width = 5
72
+ assert(table[0][0].border_widths == [5, 5, 5, 5])
73
+ assert(table[0][1].border_widths == [5, 5, 5, 5])
74
+ assert(table[1][0].border_widths == [5, 5, 5, 5])
75
+ assert(table[1][1].border_widths == [5, 5, 5, 5])
76
+
77
+ table.border_width = 0
78
+ assert(table[0][0].border_widths == [0, 0, 0, 0])
79
+ assert(table[0][1].border_widths == [0, 0, 0, 0])
80
+ assert(table[1][0].border_widths == [0, 0, 0, 0])
81
+ assert(table[1][1].border_widths == [0, 0, 0, 0])
82
+ end
83
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ # Information class unit test class.
4
+ class TableRowNodeTest < Test::Unit::TestCase
5
+ def setup
6
+ @table = TableNode.new(nil, 3, 3, 100, 100, 100)
7
+ end
8
+
9
+ def test_basics
10
+ rows = []
11
+ rows.push(TableRowNode.new(@table, 10))
12
+ rows.push(TableRowNode.new(@table, 3, 100, 200, 300))
13
+
14
+ assert(rows[0].size == 10)
15
+ assert(rows[1].size == 3)
16
+
17
+ assert(rows[0][0].width == TableCellNode::DEFAULT_WIDTH)
18
+ assert(rows[0][1].width == TableCellNode::DEFAULT_WIDTH)
19
+ assert(rows[0][2].width == TableCellNode::DEFAULT_WIDTH)
20
+ assert(rows[0][3].width == TableCellNode::DEFAULT_WIDTH)
21
+ assert(rows[0][4].width == TableCellNode::DEFAULT_WIDTH)
22
+ assert(rows[0][5].width == TableCellNode::DEFAULT_WIDTH)
23
+ assert(rows[0][6].width == TableCellNode::DEFAULT_WIDTH)
24
+ assert(rows[0][7].width == TableCellNode::DEFAULT_WIDTH)
25
+ assert(rows[0][8].width == TableCellNode::DEFAULT_WIDTH)
26
+ assert(rows[0][9].width == TableCellNode::DEFAULT_WIDTH)
27
+ assert(rows[1][0].width == 100)
28
+ assert(rows[1][1].width == 200)
29
+ assert(rows[1][2].width == 300)
30
+
31
+ assert(rows[0][1].border_widths == [0, 0, 0, 0])
32
+ rows[0].border_width = 10
33
+ assert(rows[0][1].border_widths == [10, 10, 10, 10])
34
+ end
35
+
36
+ def test_exceptions
37
+ row = TableRowNode.new(@table, 1)
38
+ begin
39
+ row.parent = nil
40
+ flunk("Successfully called the TableRowNode#parent=() method.")
41
+ rescue
42
+ end
43
+ end
44
+
45
+ def test_rtf_generation
46
+ rows = []
47
+ rows.push(TableRowNode.new(@table, 3, 50, 50, 50))
48
+ rows.push(TableRowNode.new(@table, 1, 134))
49
+ rows[1].border_width = 5
50
+ assert(rows[0].to_rtf == "\\trowd\\tgraph100\n\\cellx50\n\\cellx100\n"\
51
+ "\\cellx150\n\\pard\\intbl\n\n\\cell\n"\
52
+ "\\pard\\intbl\n\n\\cell\n"\
53
+ "\\pard\\intbl\n\n\\cell\n\\row")
54
+ assert(rows[1].to_rtf == "\\trowd\\tgraph100\n"\
55
+ "\\clbrdrt\\brdrw5\\brdrs\\clbrdrl\\brdrw5\\brdrs"\
56
+ "\\clbrdrb\\brdrw5\\brdrs\\clbrdrr\\brdrw5\\brdrs"\
57
+ "\\cellx134\n\\pard\\intbl\n\n\\cell\n\\row")
58
+ end
59
+ end