14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/analysis/zones.rb', line 14
def self.include_in pdf
deps = [:top_margins, :left_margins, :right_margins, :bottom_margins, :regions]
pdf.spatials :zones, :paged => true, :depends_on => deps do |parser|
y_mask = MultiRange.new
t_margin = nil
b_margin = nil
left_margin_x = 0
right_margin_x = 0
parser.before do
y_mask = MultiRange.new
end
parser.objects :left_margins do |lm|
left_margin_x = lm[:x] + lm[:width]
end
parser.objects :right_margins do |rm|
right_margin_x = rm[:x]
end
parser.objects :top_margins do |m|
t_margin = m
end
parser.objects :bottom_margins do |m|
b_margin = m
end
parser.objects :regions do |r|
y_mask.append r[:y]..(r[:y] + r[:height])
end
parser.after do
marginless_height = t_margin[:y] - (b_margin[:y] + b_margin[:height])
a = (marginless_height - (marginless_height * pdf.settings[:body_ratio])) / 2
y_mask.append((b_margin[:y] + b_margin[:height] + a)..(t_margin[:y] - a))
objs = []
if y_mask.count < 2
objs << {
:group => :bodies,
:x => left_margin_x,
:y => b_margin[:y] + b_margin[:height],
:width => right_margin_x - left_margin_x,
:height => t_margin[:y] - (b_margin[:y] + b_margin[:height])
}
elsif y_mask.count < 3
top = {
:x => left_margin_x,
:y => y_mask.max_excluded,
:width => right_margin_x - left_margin_x,
:height => t_margin[:y] - y_mask.max_excluded
}
bottom = {
:x => left_margin_x,
:y => b_margin[:y] + b_margin[:height],
:width => right_margin_x - left_margin_x,
:height => top[:y] - (b_margin[:y] + b_margin[:height])
}
if top[:height] > bottom[:height]
top[:group] = :bodies
bottom[:group] = :footers
else
top[:group] = :headers
bottom[:group] = :bodies
end
objs += [top, bottom]
else
= {
:group => :headers,
:x => left_margin_x,
:y => y_mask.max_excluded,
:width => right_margin_x - left_margin_x,
:height => t_margin[:y] - y_mask.max_excluded
}
= {
:group => :footers,
:x => left_margin_x,
:y => b_margin[:y] + b_margin[:height],
:width => right_margin_x - left_margin_x,
:height => y_mask.min_excluded - (b_margin[:y] + b_margin[:height])
}
body = {
:group => :bodies,
:x => left_margin_x,
:y => [:y] + [:height],
:width => right_margin_x - left_margin_x,
:height => [:y] - ([:y] + [:height])
}
objs += [, body, ]
end
page_base = {
:page => t_margin[:page],
:page_width => t_margin[:page_width],
:page_height => t_margin[:page_height]
}
objs.map { |o| page_base.merge o }
end
end
pdf.spatials :headers, :depends_on => [:zones]
pdf.spatials :footers, :depends_on => [:zones]
pdf.spatials :bodies, :depends_on => [:zones]
end
|