wiz_rtf 0.6.0 → 0.6.7
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.
- checksums.yaml +4 -4
- data/lib/wiz_rtf/cell.rb +10 -0
- data/lib/wiz_rtf/color.rb +0 -1
- data/lib/wiz_rtf/document.rb +83 -30
- data/lib/wiz_rtf/image.rb +17 -1
- data/lib/wiz_rtf/row.rb +11 -0
- data/lib/wiz_rtf/table.rb +22 -0
- data/lib/wiz_rtf/text.rb +16 -0
- data/lib/wiz_rtf/version.rb +1 -1
- data/lib/wiz_rtf/wiz_rtf_error.rb +13 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 665ba51cf6271ff694ce0efb2f216fc003b4b4e0
|
4
|
+
data.tar.gz: 9d7ccf728f5a4a48b407ae140753361fef56bacd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdc6e1fa527e59e1587cb55de522b1fe0d78d8ebe14280c1af463c7280480c63213464863101f7ac1cb18da1f126c2330991169d8cb980cbe7646af576434971
|
7
|
+
data.tar.gz: 8256f04da52d810bd1a2b0094679227e7f5e8acce6b5c11431b65236d838df03b8fd4897bc80cd12780c915242f8137d0d15ab13dd95e05af72187b1d15f91b3
|
data/lib/wiz_rtf/cell.rb
CHANGED
@@ -5,9 +5,17 @@
|
|
5
5
|
# Copyright (C) 2015 by sgzhe@163.com
|
6
6
|
|
7
7
|
module WizRtf
|
8
|
+
|
9
|
+
# = Represents a table cell.
|
8
10
|
class Cell
|
9
11
|
attr_accessor :colspan, :rowspan, :content, :v_merge, :right_width
|
10
12
|
|
13
|
+
# This is the constructor for the Cell class.
|
14
|
+
# * +cell+ - optional values:: number, string, symbol, hash.
|
15
|
+
# == Example:
|
16
|
+
#
|
17
|
+
# WizRtf::Cell.new({content:'4', rowspan:3, colspan:2})
|
18
|
+
#
|
11
19
|
def initialize(cell)
|
12
20
|
if cell.is_a?(Hash)
|
13
21
|
@colspan = cell[:colspan] || 1
|
@@ -20,6 +28,8 @@ module WizRtf
|
|
20
28
|
end
|
21
29
|
end
|
22
30
|
|
31
|
+
# Outputs the Partial Rtf Document to a Generic Stream as a Rich Text Format (RTF).
|
32
|
+
# * +io+ - The Generic IO to Output the RTF Document.
|
23
33
|
def render(io)
|
24
34
|
io.cmd :celld
|
25
35
|
io.cmd :clbrdrt
|
data/lib/wiz_rtf/color.rb
CHANGED
data/lib/wiz_rtf/document.rb
CHANGED
@@ -5,8 +5,25 @@
|
|
5
5
|
# Copyright (C) 2015 by sgzhe@163.com
|
6
6
|
|
7
7
|
module WizRtf
|
8
|
+
# = Rtf Document
|
9
|
+
#
|
10
|
+
# Creates a new Rtf document specifing the format of the pages.
|
11
|
+
# == Example:
|
12
|
+
#
|
13
|
+
# doc = WizRtf::Document.new do
|
14
|
+
# text "A Example of Rtf Document", 'text-align' => :center, 'font-family' => 'Microsoft YaHei', 'font-size' => 48, 'font-bold' => true, 'font-italic' => true, 'font-underline' => true
|
15
|
+
# image('h:\eahey.png')
|
16
|
+
# page_break
|
17
|
+
# text "A Table Demo", 'foreground-color' => WizRtf::Color::RED, 'background-color' => '#0f00ff'
|
18
|
+
# table [[{content: WizRtf::Image.new('h:\eahey.png'),rowspan:4},{content:'4',rowspan:4},1,{content:'1',colspan:2}],
|
19
|
+
# [{content:'4',rowspan:3,colspan:2},8],[11]], column_widths:{1=>100,2 => 100,3 => 50,4 => 50,5 => 50} do
|
20
|
+
# add_row [1]
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
# doc.save('c:\text.rtf')
|
24
|
+
#
|
8
25
|
class Document
|
9
|
-
def initialize(&block)
|
26
|
+
def initialize(options = {}, &block)
|
10
27
|
@fonts = []
|
11
28
|
@colors = []
|
12
29
|
@parts = []
|
@@ -14,10 +31,44 @@ module WizRtf
|
|
14
31
|
block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
|
15
32
|
end
|
16
33
|
|
34
|
+
# Outputs the Complete Rtf Document to a Generic Stream as a Rich Text Format (RTF)
|
35
|
+
# * +io+ - The Generic IO to Output the RTF Document
|
36
|
+
def render(io)
|
37
|
+
io.group do
|
38
|
+
io.cmd :rtf, 1
|
39
|
+
io.cmd :ansi
|
40
|
+
io.cmd :ansicpg, 2052
|
41
|
+
io.cmd :deff, 0
|
42
|
+
io.group do
|
43
|
+
io.cmd :fonttbl
|
44
|
+
@fonts.each do |font|
|
45
|
+
font.render(io)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
io.group do
|
49
|
+
io.cmd :colortbl
|
50
|
+
io.delimit
|
51
|
+
@colors.each do |color|
|
52
|
+
color.render(io)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
@parts.each do |part|
|
56
|
+
part.render(io)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Outputs the complete Rtf Document to a file as a Rich Text Format (RTF)
|
62
|
+
# * +file+ - file path and filename.
|
63
|
+
def save(file)
|
64
|
+
File.open(file, 'w') { |file| render(WizRtf::RtfIO.new(file)) }
|
65
|
+
end
|
66
|
+
|
17
67
|
def head(&block)
|
18
68
|
block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
|
19
69
|
end
|
20
70
|
|
71
|
+
# Sets the Font for the text.
|
21
72
|
def font(name, family = nil, character_set = 0, prq = 2)
|
22
73
|
unless index = @fonts.index {|f| f.name == name}
|
23
74
|
index = @fonts.size
|
@@ -27,6 +78,7 @@ module WizRtf
|
|
27
78
|
index
|
28
79
|
end
|
29
80
|
|
81
|
+
# Sets the color for the text.
|
30
82
|
def color(*rgb)
|
31
83
|
color = WizRtf::Color.new(*rgb)
|
32
84
|
if index = @colors.index {|c| c.to_rgb_hex == color.to_rgb_hex}
|
@@ -38,6 +90,19 @@ module WizRtf
|
|
38
90
|
index
|
39
91
|
end
|
40
92
|
|
93
|
+
# This will add a string of +str+ to the document, starting at the
|
94
|
+
# current drawing position.
|
95
|
+
# == Styles:
|
96
|
+
# * +text-align+ - sets the horizontal alignment of the text. optional values: +:left+, +:center+, +:right+
|
97
|
+
# * +font-family+ - set the font family of the text. optional values:
|
98
|
+
# * +font-size+ - set font size of the text.
|
99
|
+
# * +font-bold+ - setting the value true for bold of the text.
|
100
|
+
# * +font-italic+ - setting the value true for italic of the text.
|
101
|
+
# * +font-underline+ - setting the value true for underline of the text.
|
102
|
+
# == Example:
|
103
|
+
#
|
104
|
+
# text "A Example of Rtf Document", 'text-align' => :center, 'font-family' => 'Microsoft YaHei', 'font-size' => 48, 'font-bold' => true, 'font-italic' => true, 'font-underline' => true
|
105
|
+
#
|
41
106
|
def text(str, styles = {})
|
42
107
|
styles['foreground-color'] = color(styles['foreground-color']) if styles['foreground-color']
|
43
108
|
styles['background-color'] = color(styles['background-color']) if styles['background-color']
|
@@ -45,50 +110,38 @@ module WizRtf
|
|
45
110
|
@parts << WizRtf::Text.new(str, styles)
|
46
111
|
end
|
47
112
|
|
113
|
+
# Puts a image into the current position within the document.
|
114
|
+
# * +file+ - image file path and filename.
|
48
115
|
def image(file)
|
49
116
|
@parts << WizRtf::Image.new(file)
|
50
117
|
end
|
51
118
|
|
119
|
+
# Creates a new Table
|
120
|
+
# * +rows+ - a table can be thought of as consisting of rows and columns.
|
121
|
+
# == Options:
|
122
|
+
# * +column_widths+ - sets the widths of the Columns.
|
123
|
+
# == Example:
|
124
|
+
#
|
125
|
+
# table [
|
126
|
+
# [{content: WizRtf::Image.new('h:\eahey.png'),rowspan:4},{content:'4',rowspan:4},1,{content:'1',colspan:2}],
|
127
|
+
# [{content:'4',rowspan:3,colspan:2},8],[11]
|
128
|
+
# ], column_widths:{1=>100,2 => 100,3 => 50,4 => 50,5 => 50} do
|
129
|
+
# add_row [1]
|
130
|
+
# end
|
131
|
+
#
|
52
132
|
def table(rows = [],options = {}, &block)
|
53
133
|
@parts << WizRtf::Table.new(rows, options, &block)
|
54
134
|
end
|
55
135
|
|
136
|
+
# Writes a new line.
|
56
137
|
def line_break
|
57
138
|
@parts << WizRtf::Cmd.new(:par)
|
58
139
|
end
|
59
140
|
|
141
|
+
# Writes a page interruption (new page)
|
60
142
|
def page_break
|
61
143
|
@parts << WizRtf::Cmd.new(:page)
|
62
144
|
end
|
63
145
|
|
64
|
-
def render(io)
|
65
|
-
io.group do
|
66
|
-
io.cmd :rtf, 1
|
67
|
-
io.cmd :ansi
|
68
|
-
io.cmd :ansicpg, 2052
|
69
|
-
io.cmd :deff, 0
|
70
|
-
io.group do
|
71
|
-
io.cmd :fonttbl
|
72
|
-
@fonts.each do |font|
|
73
|
-
font.render(io)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
io.group do
|
77
|
-
io.cmd :colortbl
|
78
|
-
io.delimit
|
79
|
-
@colors.each do |color|
|
80
|
-
color.render(io)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
@parts.each do |part|
|
84
|
-
part.render(io)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def save(file)
|
90
|
-
File.open(file, 'w') { |file| render(WizRtf::RtfIO.new(file)) }
|
91
|
-
end
|
92
|
-
|
93
146
|
end
|
94
147
|
end
|
data/lib/wiz_rtf/image.rb
CHANGED
@@ -5,10 +5,20 @@
|
|
5
5
|
# Copyright (C) 2015 by sgzhe@163.com
|
6
6
|
|
7
7
|
module WizRtf
|
8
|
+
|
9
|
+
# = Represents an image
|
10
|
+
# This class represents an image within a RTF document. Currently only the
|
11
|
+
# PNG, JPEG, GIF and Windows Bitmap formats are supported.
|
8
12
|
class Image
|
9
13
|
JPEG_SOF_BLOCKS = [0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF]
|
10
14
|
PIC_TYPE = {png: :pngblip, jpg: :jpegblip, bmp: :pngblip, gif: :pngblip}
|
11
15
|
|
16
|
+
# This is the constructor for the Image class.
|
17
|
+
# * +file+ - image file path and filename.
|
18
|
+
# == Example:
|
19
|
+
#
|
20
|
+
# WizRtf::Image.new('h:\eahey.png')
|
21
|
+
#
|
12
22
|
def initialize(file)
|
13
23
|
begin
|
14
24
|
@img = IO.binread(file)
|
@@ -18,6 +28,8 @@ module WizRtf
|
|
18
28
|
end
|
19
29
|
end
|
20
30
|
|
31
|
+
# Returns an symbol indicating the image type fetched from a image file.
|
32
|
+
# It will return nil if the image could not be fetched, or if the image type was not recognised.
|
21
33
|
def type
|
22
34
|
png = Regexp.new("\x89PNG".force_encoding("binary"))
|
23
35
|
jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
|
@@ -35,10 +47,12 @@ module WizRtf
|
|
35
47
|
when /^BM/
|
36
48
|
:bmp
|
37
49
|
else
|
38
|
-
|
50
|
+
nil
|
39
51
|
end
|
40
52
|
end
|
41
53
|
|
54
|
+
# Returns an array containing the width and height of the image.
|
55
|
+
# It will return nil if the image could not be fetched, or if the image type was not recognised.
|
42
56
|
def size
|
43
57
|
case self.type
|
44
58
|
when :gif
|
@@ -64,6 +78,8 @@ module WizRtf
|
|
64
78
|
end
|
65
79
|
end
|
66
80
|
|
81
|
+
# Outputs the Partial Rtf Document to a Generic Stream as a Rich Text Format (RTF).
|
82
|
+
# * +io+ - The Generic IO to Output the RTF Document.
|
67
83
|
def render(io)
|
68
84
|
io.group do
|
69
85
|
io.cmd '*'
|
data/lib/wiz_rtf/row.rb
CHANGED
@@ -5,7 +5,13 @@
|
|
5
5
|
# Copyright (C) 2015 by sgzhe@163.com
|
6
6
|
|
7
7
|
module WizRtf
|
8
|
+
|
9
|
+
# = Represents a table row.
|
8
10
|
class Row
|
11
|
+
|
12
|
+
# This is the constructor for the Row class.
|
13
|
+
# * +table+ - A reference to table that owns the row.
|
14
|
+
# * +cells+ - The number of cells that the row will contain.
|
9
15
|
def initialize(table, cells = [])
|
10
16
|
@table = table
|
11
17
|
@cells = []
|
@@ -16,6 +22,9 @@ module WizRtf
|
|
16
22
|
end
|
17
23
|
end
|
18
24
|
|
25
|
+
# add a Cell object to the Cells array.
|
26
|
+
# * +cell+ - a Cell object.
|
27
|
+
# * +merge+ - is merges the specified table cells.
|
19
28
|
def add_cell(cell, merge = false)
|
20
29
|
add_cell('', true) if !merge && row_spanned?(@col_offset)
|
21
30
|
|
@@ -49,6 +58,8 @@ module WizRtf
|
|
49
58
|
@table.column_widths * 20
|
50
59
|
end
|
51
60
|
|
61
|
+
# Outputs the Partial Rtf Document to a Generic Stream as a Rich Text Format (RTF).
|
62
|
+
# * +io+ - The Generic IO to Output the RTF Document.
|
52
63
|
def render(io)
|
53
64
|
io.cmd :trowd
|
54
65
|
io.cmd :trbrdrt
|
data/lib/wiz_rtf/table.rb
CHANGED
@@ -5,10 +5,24 @@
|
|
5
5
|
# Copyright (C) 2015 by sgzhe@163.com
|
6
6
|
|
7
7
|
module WizRtf
|
8
|
+
# = the Rtf Document Table.
|
8
9
|
class Table
|
9
10
|
DEFAULT_COLUMN_WIDTH = 40
|
10
11
|
attr_accessor :row_spans, :column_widths
|
11
12
|
|
13
|
+
# Creates a new Table
|
14
|
+
# * +rows+ - a table can be thought of as consisting of rows and columns.
|
15
|
+
# == Options:
|
16
|
+
# * +column_widths+ - sets the widths of the Columns.
|
17
|
+
# == Example:
|
18
|
+
#
|
19
|
+
# WizRtf::Table.new([
|
20
|
+
# [{content: WizRtf::Image.new('h:\eahey.png'),rowspan:4},{content:'4',rowspan:4},1,{content:'1',colspan:2}],
|
21
|
+
# [{content:'4',rowspan:3,colspan:2},8],[11]
|
22
|
+
# ], column_widths:{1=>100,2 => 100,3 => 50,4 => 50,5 => 50}) do
|
23
|
+
# add_row [1]
|
24
|
+
# end
|
25
|
+
#
|
12
26
|
def initialize(rows = [], options = {}, &block)
|
13
27
|
@rows = []
|
14
28
|
@row_spans = {}
|
@@ -19,10 +33,18 @@ module WizRtf
|
|
19
33
|
block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
|
20
34
|
end
|
21
35
|
|
36
|
+
# Add The Cells Array of the Row.
|
37
|
+
# * +cells+ - the cells array.
|
38
|
+
# == Example:
|
39
|
+
#
|
40
|
+
# add_row [{content:'4',rowspan:3,colspan:2},8]
|
41
|
+
#
|
22
42
|
def add_row(cells = [])
|
23
43
|
@rows << WizRtf::Row.new(self, cells)
|
24
44
|
end
|
25
45
|
|
46
|
+
# Outputs the Partial Rtf Document to a Generic Stream as a Rich Text Format (RTF).
|
47
|
+
# * +io+ - The Generic IO to Output the RTF Document.
|
26
48
|
def render(io)
|
27
49
|
@rows.each do |row|
|
28
50
|
row.render(io)
|
data/lib/wiz_rtf/text.rb
CHANGED
@@ -5,14 +5,30 @@
|
|
5
5
|
# Copyright (C) 2015 by sgzhe@163.com
|
6
6
|
|
7
7
|
module WizRtf
|
8
|
+
|
9
|
+
# = Represents Rtf text.
|
8
10
|
class Text
|
9
11
|
TEXT_ALIGN_MAP = {left:'ql',center:'qc',right:'qr'}
|
10
12
|
|
13
|
+
# creates a text of +str+ to the document.
|
14
|
+
# == Styles:
|
15
|
+
# * +text-align+ - sets the horizontal alignment of the text. optional values: +:left+, +:center+, +:right+
|
16
|
+
# * +font-family+ - set the font family of the text. optional values:
|
17
|
+
# * +font-size+ - set font size of the text.
|
18
|
+
# * +font-bold+ - setting the value true for bold of the text.
|
19
|
+
# * +font-italic+ - setting the value true for italic of the text.
|
20
|
+
# * +font-underline+ - setting the value true for underline of the text.
|
21
|
+
# == Example:
|
22
|
+
#
|
23
|
+
# 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)
|
24
|
+
#
|
11
25
|
def initialize(str = '', styles = {})
|
12
26
|
@str = str
|
13
27
|
@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)
|
14
28
|
end
|
15
29
|
|
30
|
+
# Outputs the Partial Rtf Document to a Generic Stream as a Rich Text Format (RTF).
|
31
|
+
# * +io+ - The Generic IO to Output the RTF Document.
|
16
32
|
def render(io)
|
17
33
|
io.group do
|
18
34
|
io.cmd :pard
|
data/lib/wiz_rtf/version.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# WizRft: A gem for exporting Word Documents in ruby
|
4
|
+
# using the Microsoft Rich Text Format (RTF) Specification
|
5
|
+
# Copyright (C) 2015 by sgzhe@163.com
|
6
|
+
|
7
|
+
module WizRtf
|
8
|
+
# = Wiz Rtf Errors
|
9
|
+
#
|
10
|
+
# Generic Wiz Rtf exception class.
|
11
|
+
class WizRtfError < StandardError
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wiz_rtf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- songgz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/wiz_rtf/table.rb
|
68
68
|
- lib/wiz_rtf/text.rb
|
69
69
|
- lib/wiz_rtf/version.rb
|
70
|
+
- lib/wiz_rtf/wiz_rtf_error.rb
|
70
71
|
- wiz_rtf.gemspec
|
71
72
|
homepage: https://github.com/songgz/wiz_rtf
|
72
73
|
licenses:
|