prawn-fillform 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  module Prawn
2
2
  module Fillform
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
6
6
 
@@ -3,115 +3,166 @@ require 'prawn-fillform/version'
3
3
  module Prawn
4
4
  module Fillform
5
5
 
6
- def fill_form_with(data={})
6
+ class Field
7
+ include Prawn::Document::Internals
7
8
 
8
- 1.upto(acroform_number_of_pages).each do |page|
9
+ def initialize(dictionary)
10
+ @dictionary = dictionary
11
+ end
9
12
 
13
+ def description
14
+ deref(@dictionary[:TU])
15
+ end
10
16
 
11
- page_number = "page_#{page}".to_sym
12
- fields = acroform_fields_for(page_number)
13
- fields.each do |field|
14
- x = field[:x]
15
- y = field[:y]
16
- value = data[page_number][field.fetch(:name)] rescue nil
17
- go_to_page(field[:page_number])
18
-
19
- if field[:type] == :image
20
- image value, :at => [x, y], :position => :center, :vposition => :center, :fit => [field[:width], field[:height]] if value
21
- else
22
- text_box value, :at => [x, y], :align => field[:align],
23
- :width => field[:width], :height => field[:height],
24
- :valign => :center, :align => :left, :single_line => !field[:multi_line], :size => field[:size] if value
25
- end
26
- end
17
+ def rect
18
+ deref(@dictionary[:Rect])
27
19
  end
28
20
 
29
- # delete acroform references
30
- acroform_specifications.last.each do |reference|
31
- reference[:acroform_fields].delete(reference[:field])
21
+ def name
22
+ deref(@dictionary[:T]).to_sym
23
+ end
24
+
25
+ def x
26
+ rect[0]
32
27
  end
33
- end
34
28
 
29
+ def y
30
+ rect[3]
31
+ end
35
32
 
36
- private
37
- def acroform_fields_for(page)
38
- acroform_specifications.first.fetch(page).fetch(:fields)
33
+ def width
34
+ rect[2] - rect[0]
39
35
  end
40
36
 
41
- def acroform_number_of_pages
42
- acroform_specifications.first.keys.length
37
+ def height
38
+ rect[3] - rect[1]
43
39
  end
44
40
 
45
- def acroform_field_info(form_fields, field_ref, page)
41
+ def value
42
+ deref(@dictionary[:V])
43
+ end
46
44
 
47
- field_dict = deref(field_ref)
45
+ def default_value
46
+ deref(@dictionary[:DV])
47
+ end
48
48
 
49
- field = {}
49
+ def flags
50
+ deref(@dictionary[:Ff])
51
+ end
50
52
 
51
- field_type = deref(field_dict[:FT])
53
+ end
52
54
 
53
- field[:name] = deref(field_dict[:T]).to_sym
54
- field[:type] = field_type == :Tx ? :text : :image
55
- field[:rect] = deref(field_dict[:Rect])
56
- field[:x] = field[:rect][0]
57
- field[:y] = field[:rect][3]
58
- field[:width] = field[:rect][2] - field[:rect][0]
59
- field[:height] = field[:rect][3] - field[:rect][1]
60
- field[:page_number] = page
55
+ class Text < Field
61
56
 
62
- if field[:type] == :text
63
- field[:description] = deref(field_dict[:TU])
64
- field[:default_value] = deref(field_dict[:DV])
65
- field[:value] = deref(field_dict[:V])
66
- field[:size] = deref(field_dict[:DA]).split(" ")[1].to_i
67
- field[:style] = deref(field_dict[:DA].split(" ")[0])
68
- field[:align] = case deref(field_dict[:Q])
69
- when 0 then :left
70
- when 2 then :center
71
- when 4 then :right
72
- end
73
- field[:max_length] = deref(field_dict[:MaxLen])
74
- field[:multi_line] = deref(field_dict[:Ff]).to_i > 0 ? :true : :false
75
- field[:border_width] = deref(field_dict[:BS]).fetch(:W, nil) if deref(field_dict[:BS])
76
- field[:border_style] = deref(field_dict[:BS]).fetch(:S, nil) if deref(field_dict[:BS])
77
- field[:border_color] = deref(field_dict[:MK]).fetch(:BC, nil) if deref(field_dict[:MK])
78
- field[:background_color] = deref(field_dict[:MK]).fetch(:BG, nil) if deref(field_dict[:MK])
57
+ def align
58
+ case deref(@dictionary[:Q])
59
+ when 0
60
+ :left
61
+ when 1
62
+ :center
63
+ when 2
64
+ :right
79
65
  end
66
+ end
80
67
 
81
- field
68
+ def max_length
69
+ deref(@dictionary[:MaxLen])
70
+ end
82
71
 
72
+ def type
73
+ :text
83
74
  end
75
+ end
84
76
 
85
- def acroform_specifications
77
+ class Button < Field
78
+ def type
79
+ :button
80
+ end
81
+ end
86
82
 
87
- specifications = {}
88
- references = []
83
+ class References
84
+ include Prawn::Document::Internals
85
+ def initialize(state)
86
+ @state = state
87
+ @refs = []
88
+ collect!
89
+ end
89
90
 
90
- state.pages.each_with_index do |page, i|
91
- form_fields = deref(page.dictionary.data[:Annots])
91
+ def delete!
92
+ @refs.each do |ref|
93
+ ref[:annots].delete(ref[:ref])
94
+ end
95
+ end
92
96
 
93
- page_number = "page_#{i+1}".to_sym
94
- specifications[page_number] = {}
95
- specifications[page_number][:fields] = []
97
+ protected
98
+ def collect!
99
+ @state.pages.each_with_index do |page, i|
100
+ @annots = deref(page.dictionary.data[:Annots])
101
+ @annots.map do |ref|
102
+ reference = {}
103
+ reference[:ref] = ref
104
+ reference[:annots] = @annots
105
+ @refs << reference
106
+ end
107
+ end
108
+ end
109
+ end
96
110
 
97
- form_fields.map do |field_ref|
98
- field_dict = deref(field_ref)
99
- next unless deref(field_dict[:Type]) == :Annot and deref(field_dict[:Subtype]) == :Widget
100
- next unless (deref(field_dict[:FT]) == :Tx || deref(field_dict[:FT]) == :Btn)
111
+ def acroform_fields
112
+ acroform = {}
113
+ state.pages.each_with_index do |page, i|
114
+ annots = deref(page.dictionary.data[:Annots])
115
+ page_number = "page_#{i+1}".to_sym
116
+ acroform[page_number] = []
117
+ acroform[page_number] = []
118
+ annots.map do |ref|
119
+ dictionary = deref(ref)
120
+
121
+ next unless deref(dictionary[:Type]) == :Annot and deref(dictionary[:Subtype]) == :Widget
122
+ next unless (deref(dictionary[:FT]) == :Tx || deref(dictionary[:FT]) == :Btn)
123
+
124
+ type = deref(dictionary[:FT]).to_sym
125
+ case type
126
+ when :Tx
127
+ acroform[page_number] << Text.new(dictionary)
128
+ when :Btn
129
+ acroform[page_number] << Button.new(dictionary)
130
+ end
131
+ end
132
+ end
133
+ acroform
134
+ end
101
135
 
102
- field = acroform_field_info(form_fields, field_ref, i+1)
103
- specifications[page_number][:fields] << field
136
+ def fill_form_with(data={})
104
137
 
105
- reference = {}
106
- reference[:field] = field_ref
107
- reference[:acroform_fields] = form_fields
108
- references << reference
138
+ acroform_fields.each do |page, fields|
139
+ fields.each do |field|
140
+ number = page.to_s.split("_").last.to_i
141
+ go_to_page(number)
142
+ value = data[page][field.name] rescue nil
143
+
144
+ if value
145
+ if field.instance_of? Prawn::Fillform::Text
146
+ text_box value, :at => [field.x, field.y],
147
+ :align => field.align,
148
+ :width => field.width,
149
+ :height => field.height,
150
+ :valign => :center
151
+ elsif field.instance_of? Prawn::Fillform::Button
152
+ image value, :at => [field.x, field.y],
153
+ :position => :center,
154
+ :vposition => :center,
155
+ :fit => [field.width, field.height]
156
+ end
109
157
  end
110
158
  end
111
- [specifications, references]
112
159
  end
113
160
 
161
+ references = References.new(state)
162
+ references.delete!
163
+
114
164
  end
165
+ end
115
166
  end
116
167
 
117
168
  require 'prawn/document'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-fillform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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: 2011-09-27 00:00:00.000000000Z
12
+ date: 2011-09-28 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: