wizport 0.0.5 → 0.0.6

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.
@@ -3,51 +3,40 @@
3
3
  # Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
4
4
  # Copyright 2012 by sgzhe@163.com
5
5
 
6
-
7
-
6
+ require "stringio"
8
7
  module Wizport
9
8
  module Rtf
10
- class Document < Group
9
+ class Document < Element
11
10
  def initialize(file = nil, &block)
12
- super()
13
- elements << Command.new(:rtf, 1)
14
- elements << Command.new(:ansi)
15
- elements << Command.new(:ansicpg, 2052)
16
- block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
11
+ @rtf = StringIO.new
12
+ group do
13
+ cmd :rtf, 1
14
+ cmd :ansi
15
+ cmd :ansicpg, 2052
16
+ block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
17
+ end
17
18
  save file if file
18
19
  end
19
20
 
20
- #styles = {align,font,font_size,color }
21
- def text(txt, styles = {:align => :left})
22
- elements << Text.new(txt,styles)
23
- end
24
-
25
- def table(rows = [],&block)
26
- elements << Table.new(rows, &block)
21
+ def write(txt)
22
+ @rtf << txt
27
23
  end
28
24
 
29
- def colors(colours = ['ffffff'])
30
-
31
-
25
+ def text(str, styles = {:align => :left})
26
+ Wizport::Rtf::Text.new(self, str, styles)
32
27
  end
33
28
 
34
- def style(txt, styles)
35
-
29
+ def table(rows = [], &block)
30
+ Wizport::Rtf::Table.new(self, rows, &block)
36
31
  end
37
32
 
38
33
 
39
- def method_missing(id, *args, &block)
40
- cmd id, *args, &block
41
- end
42
-
43
34
  def to_s
44
35
 
45
36
  end
46
37
 
47
38
  def to_rtf
48
- builder = RtfBuilder.new
49
- accept(builder)
50
- builder.to_rtf
39
+ @rtf.string
51
40
  end
52
41
 
53
42
  def save(file)
@@ -59,18 +48,10 @@ module Wizport
59
48
  end
60
49
 
61
50
  if __FILE__ == $0
62
-
63
- d = Wizport::Rtf::Document.new('c:/file.rtf') do
64
- text "我们", :align => :center, :size => 48
65
- text "春风不度玉门关", :align => :left, :font => 24
66
- table [[{text:'eee',row_span:2},'s',{text:'ww',col_span:2},'d'],[nil,'s','d','d']] do
67
- row ['f','s','d','d']
68
- end
69
-
70
- end
71
-
72
- #p d.to_rtf
73
-
74
-
51
+ #rtf = Wizport::Rtf::Document.new do
52
+ # text "学生综合素质评价"
53
+ # table [[1, {content :'1', colspan: 2}], [3, {content :'4', rowspan: 2, colspan: 2}], [1]]
54
+ #end
55
+ #rtf.save('c:/r.rtf')
75
56
  end
76
57
 
@@ -6,7 +6,29 @@
6
6
  module Wizport
7
7
  module Rtf
8
8
  class Element
9
- include Wizport::Visitable
9
+ def initialize(rtf)
10
+ @rtf = rtf
11
+ end
12
+
13
+ def cmd(name, value = nil)
14
+ @rtf.write '\\'
15
+ @rtf.write name
16
+ @rtf.write value if value
17
+ end
18
+
19
+ def txt(str)
20
+ str = str.to_s
21
+ str = str.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\")
22
+ str = str.encode("UTF-16LE", :undef=>:replace).each_codepoint.map {|n| n < 128 ? n.chr : "\\u#{n}\\'3f"}.join('')
23
+ @rtf.write ' '
24
+ @rtf.write str
25
+ end
26
+
27
+ def group
28
+ @rtf.write '{'
29
+ yield if block_given?
30
+ @rtf.write '}'
31
+ end
10
32
  end
11
33
  end
12
34
  end
@@ -5,33 +5,71 @@
5
5
 
6
6
  module Wizport
7
7
  module Rtf
8
- class Table < Group
9
- attr_accessor :rows, :cells
10
-
11
- #options = {header:[],widths:[]}
12
- def initialize(rows = [],options = {}, &block)
13
- super()
14
- @rows = []
15
- #@cells = {}
16
- rows.each do |cells|
17
- row cells
8
+ class Table < Element
9
+
10
+ def initialize(rtf, rows = [], options = {}, &block)
11
+ super(rtf)
12
+ @row_spans = {}
13
+ rows.each_index do |index|
14
+ add_row rows[index]
18
15
  end
19
16
  block.arity<1 ? self.instance_eval(&block) : block.call(self) if block_given?
20
17
  end
21
18
 
22
- def row(cells = [], &block)
23
- r = Row.new(cells,self, &block)
24
- @rows << r
19
+ def add_row(cells = [])
20
+ cmd :trowd
21
+ cmd :trautofit1
22
+ cmd :intbl
23
+ @col_offset = 1
24
+ cells.each do |cell|
25
+ if row_spanned? @col_offset
26
+ add_cell ""
27
+ add_cell cell
28
+ elsif row_spanned? @col_offset + 1
29
+ add_cell cell
30
+ add_cell ""
31
+ else
32
+ add_cell cell
33
+ end
34
+ end
35
+ cmd :row
36
+ end
37
+
38
+ def add_cell(cell)
39
+ if cell.is_a?(Hash)
40
+ rowspan = cell[:rowspan]
41
+ colspan = cell[:colspan]
42
+ content = cell[:content]
43
+ @row_spans[@col_offset] = cell if cell[:rowspan]
44
+ end
45
+ colspan = colspan || 1
46
+ rowspan = rowspan || 1
47
+ content = content || cell
48
+
49
+ cmd :celld
50
+ if rowspan > 1
51
+ cmd :clvmgf
52
+ @col_offset += colspan
53
+ elsif row_spanned? @col_offset
54
+ cmd :clvmrg
55
+ @row_spans[@col_offset][:rowspan] -= 1
56
+ @col_offset += row_spans_with_colspan
57
+ else
58
+ @col_offset += colspan
59
+ end
60
+ cmd :cellx, (@col_offset - 1)* 2000
61
+ txt content
62
+ cmd :cell
63
+ end
64
+
65
+ def row_spanned?(offset)
66
+ @row_spans[offset] && @row_spans[offset][:rowspan].to_i > 1
67
+ end
25
68
 
69
+ def row_spans_with_colspan
70
+ return @row_spans[@col_offset][:colspan] || 1
26
71
  end
27
72
 
28
- #def [](r,c)
29
- # @cells["#{r},#{c}"]
30
- #end
31
- #
32
- #def [](r,c,v)
33
- # @cells["#{r},#{c}"] = v
34
- #end
35
73
  end
36
74
  end
37
75
  end
@@ -5,35 +5,20 @@
5
5
 
6
6
  module Wizport
7
7
  module Rtf
8
- class Text < Group
8
+ class Text < Element
9
9
  ALIGN_MAP = {left:'ql',center:'qc'}
10
10
 
11
- def initialize(txt = '', styles = {})
12
- super()
11
+ def initialize(rtf, str = '', styles = {})
12
+ super(rtf)
13
13
  styles = {:align => :left,:size => 24}.merge(styles)
14
- elements << Command.new(:pard)
15
- elements << @align = Command.new(ALIGN_MAP[styles[:align]])
16
- elements << @size = Command.new(:fs,styles[:size])
17
- elements << @txt = Plaintext.new(txt)
18
- elements << Command.new(:par)
14
+ group do
15
+ cmd :part
16
+ cmd ALIGN_MAP[styles[:align]]
17
+ cmd :fs,styles[:size]
18
+ txt str
19
+ cmd :par
20
+ end
19
21
  end
20
-
21
- def size=(value)
22
- @size.value = value
23
- end
24
-
25
- def size
26
- @size.value
27
- end
28
-
29
- def align=(value)
30
- @align.name = ALIGN_MAP[styles[value]]
31
- end
32
-
33
- def align
34
- ALIGN_MAP.invert[@align.name]
35
- end
36
-
37
22
  end
38
23
  end
39
24
  end
@@ -1,3 +1,3 @@
1
1
  module Wizport
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -5,12 +5,14 @@ require "../../../../spec/spec_helper"
5
5
  describe Wizport::Rtf::Document do
6
6
 
7
7
  it "a simple example of Rtf" do
8
- rtf = Wizport::Rtf::Document.new
9
- rtf.text "学生综合素质评价"
10
- rtf.table [[{content:'s',rowspan:3},{content:'s',colspan:2},{content:'s',colspan:2},{content:'s',rowspan:2},"ss"],
11
- [1,2,3,4,11,6],
12
- [1,2,3,4,11,6],
13
- [0,1,2,3,4,11,6]]
8
+ rtf = Wizport::Rtf::Document.new do
9
+ text "学生综合素质评价"
10
+ table [[1,{content:'1',colspan:2}],[3,{content:'4',rowspan:2,colspan:2}],[11]] do
11
+ add_row [1,1,1]
12
+ end
13
+ end
14
+
14
15
  rtf.save('c:/r.rtf')
16
+
15
17
  end
16
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wizport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-07 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -56,14 +56,8 @@ files:
56
56
  - lib/wizport.rb
57
57
  - lib/wizport/builder/pdf.rb
58
58
  - lib/wizport/builder/rtf.rb
59
- - lib/wizport/document/Rtf/cell.rb
60
- - lib/wizport/document/Rtf/command.rb
61
59
  - lib/wizport/document/Rtf/document.rb
62
60
  - lib/wizport/document/Rtf/element.rb
63
- - lib/wizport/document/Rtf/group.rb
64
- - lib/wizport/document/Rtf/plaintext.rb
65
- - lib/wizport/document/Rtf/row.rb
66
- - lib/wizport/document/Rtf/rtf_builder.rb
67
61
  - lib/wizport/document/Rtf/spec.rb
68
62
  - lib/wizport/document/Rtf/table.rb
69
63
  - lib/wizport/document/Rtf/text.rb
@@ -1,64 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
4
- # Copyright 2012 by sgzhe@163.com
5
-
6
- module Wizport
7
- module Rtf
8
- class Cell
9
- attr_accessor :colspan, :rowspan
10
-
11
- def initialize(data, row = nil)
12
- @row = row
13
- @index = @row.cells.size
14
- @colspan = 1
15
- @rowspan = 1
16
- @content = data
17
- #:content => "2x1", :colspan
18
- if data.is_a?(Hash)
19
- @colspan = data[:colspan] if data[:colspan]
20
- @rowspan = data[:rowspan] if data[:rowspan]
21
- @content = data[:content]
22
- end
23
-
24
- #unless col_spanned?
25
- @row.table.elements << Command.new(:celld)
26
- @row.table.elements << Command.new(:clvmgf) if @rowspan > 1
27
- @row.table.elements << Command.new(:clvmrg) if row_spanned?
28
- @row.table.elements << Command.new(:cellx, (col_spanned)*1000)
29
- @row.table.elements << Plaintext.new(@content.to_s)
30
- @row.table.elements << Command.new(:cell)
31
- #end
32
-
33
- #@row.cellx_command Command.new(:clvmgf) if @row_span > 1
34
- #@row.cellx_command Command.new(:clvmrg) if row_spanned?
35
- #@row.cellx_command Command.new(:cellx, (@index+@col_span)*1000)
36
- #@row.cellt_command Plaintext.new(@text)
37
- #@row.cellt_command Command.new(:cell)
38
- end
39
-
40
- def col_spanned?
41
- col_spanned >= (@index + 1) && colspan == 1
42
- end
43
-
44
- def col_spanned
45
- cols = 0
46
- (@index).times do |c|
47
- cols += @row.cells[c].colspan
48
- end
49
- cols += colspan
50
- end
51
-
52
-
53
- def row_spanned?
54
- @row.row_spanned?(col_spanned - colspan)
55
- #@row.index.times do |r|
56
- # return true if @row.table.rows[r].cells[@index] && @row.table.rows[r].cells[@index].rowspan + r > @row.index
57
- #end
58
- #return false
59
- end
60
-
61
-
62
- end
63
- end
64
- end
@@ -1,22 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
4
- # Copyright 2012 by sgzhe@163.com
5
-
6
- module Wizport
7
- module Rtf
8
- class Command < Element
9
- attr_accessor :name,:value
10
-
11
- def initialize(name,value = nil)
12
- @name = name
13
- @value = value
14
- end
15
-
16
- def ctrl
17
- @name.to_s + @value.to_s
18
- end
19
-
20
- end
21
- end
22
- end
@@ -1,17 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
4
- # Copyright 2012 by sgzhe@163.com
5
-
6
-
7
- module Wizport
8
- module Rtf
9
- class Group < Element
10
- attr_accessor :elements
11
- def initialize()
12
- @elements = []
13
- end
14
-
15
- end
16
- end
17
- end
@@ -1,19 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
4
- # Copyright 2012 by sgzhe@163.com
5
-
6
- module Wizport
7
- module Rtf
8
- class Plaintext < Element
9
- attr_accessor :txt
10
-
11
- def initialize(txt)
12
- txt = txt.gsub("{", "\\{").gsub("}", "\\}").gsub("\\", "\\\\")
13
- txt = txt.encode("UTF-16LE", :undef=>:replace).each_codepoint.map {|n| n < 128 ? n.chr : "\\u#{n}\\'3f"}.join('')
14
- @txt = txt
15
- end
16
-
17
- end
18
- end
19
- end
@@ -1,74 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
4
- # Copyright 2012 by sgzhe@163.com
5
-
6
- module Wizport
7
- module Rtf
8
- class Row
9
- attr_accessor :table, :cells, :index
10
- def initialize(columns,tbl = nil)
11
- @table = tbl
12
- @cells = []
13
- @index = @table.rows.size
14
-
15
- @table.elements << Command.new(:trowd)
16
- @table.elements << Command.new(:trautofit1)
17
- @table.elements << Command.new(:intbl)
18
- columns.each_index do |i|
19
- @cellx_index = nil
20
- @cellt_index = nil
21
- temp = 0
22
- if columns[i].is_a?(Hash)
23
- temp += columns[i][:colspan].to_i
24
- else
25
- temp += 1
26
- end
27
-
28
- if row_spanned?(temp)
29
- @cells << Cell.new("",self)
30
- end
31
- @cells << Cell.new(columns[i],self)
32
- #if columns[i].is_a?(Hash)
33
- # if columns[i][:colspan] && columns[i][:colspan] > 1
34
- # (columns[i][:colspan]-1).times do
35
- # @cells << Cell.new("",self)
36
- # end
37
- # end
38
- #end
39
- end
40
- @table.elements << Command.new(:row)
41
- end
42
-
43
- def row_spanned?(c_i)
44
- rs = 0
45
-
46
- (@index).times do |r|
47
- t = 0
48
- @table.rows[r].cells.each do |c|
49
- t += c.colspan
50
- if t == c_i + 1
51
- rs += c.rowspan
52
- end
53
- end
54
-
55
- #rs += @table.rows[r].cells[c_i].rowspan if @table.rows[r].cells[c_i] && @table.rows[r].cells[c_i].rowspan > 1
56
- end
57
- rs >= (@index + 1)
58
- end
59
-
60
- def cellx_command(cmd)
61
- @cellx_index = @cellx_index || @table.elements.size
62
- @table.elements.insert @cellx_index, cmd
63
- @cellx_index += 1
64
- end
65
-
66
- def cellt_command(cmd)
67
- @cellt_index = @cellt_index || @table.elements.size
68
- @table.elements.insert @cellt_index, cmd
69
- @cellt_index += 1
70
- end
71
-
72
- end
73
- end
74
- end
@@ -1,40 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Wizport: A gem for creating reports by specifying columns, groups, styles, etc.
4
- # Copyright 2012 by sgzhe@163.com
5
-
6
- require "stringio"
7
- module Wizport
8
- module Rtf
9
- class RtfBuilder
10
- include Wizport::Visitor
11
-
12
- def initialize
13
- @rtf = StringIO.new
14
- end
15
-
16
- visit Group do |element|
17
- @rtf << '{'
18
- element.elements.each do |el|
19
- el.accept(self)
20
- end if element.is_a?(Wizport::Rtf::Group)
21
- @rtf << '}'
22
- end
23
-
24
- visit Command do |element|
25
- @rtf << '\\'
26
- @rtf << element.ctrl
27
- end
28
-
29
- visit Plaintext do |element|
30
- @rtf << ' '
31
- @rtf << element.txt
32
- end
33
-
34
- def to_rtf
35
- @rtf.string
36
- end
37
-
38
- end
39
- end
40
- end