-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathPreparedPagePostprocessor.cs
307 lines (274 loc) · 9.63 KB
/
PreparedPagePostprocessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using FastReport.Utils;
using System;
using System.Collections.Generic;
namespace FastReport.Preview
{
internal class PreparedPagePostprocessor
{
private Dictionary<string, List<TextObjectBase>> duplicates;
private Dictionary<string, List<TextObject>> mergedTextObjects;
private Dictionary<int, Base> bands;
int iBand;
private void ProcessDuplicates(TextObjectBase obj)
{
if (duplicates.ContainsKey(obj.Name))
{
List<TextObjectBase> list = duplicates[obj.Name];
TextObjectBase lastObj = list[list.Count - 1];
bool isDuplicate = true;
// compare Text
if (obj.Text != lastObj.Text)
isDuplicate = false;
else
{
float lastObjBottom = (lastObj.Parent as ReportComponentBase).Bottom;
float objTop = (obj.Parent as ReportComponentBase).Top;
if (Math.Abs(objTop - lastObjBottom) > 0.5f)
isDuplicate = false;
}
if (isDuplicate)
{
list.Add(obj);
}
else
{
// close duplicates
CloseDuplicates(list);
// add new obj
list.Clear();
list.Add(obj);
}
}
else
{
List<TextObjectBase> list = new List<TextObjectBase>();
list.Add(obj);
duplicates.Add(obj.Name, list);
}
}
private void CloseDuplicates()
{
foreach (List<TextObjectBase> list in duplicates.Values)
{
CloseDuplicates(list);
}
}
private void CloseDuplicates(List<TextObjectBase> list)
{
if (list.Count == 0)
return;
Duplicates duplicates = list[0].Duplicates;
switch (duplicates)
{
case Duplicates.Clear:
CloseDuplicatesClear(list);
break;
case Duplicates.Hide:
CloseDuplicatesHide(list);
break;
case Duplicates.Merge:
CloseDuplicatesMerge(list);
break;
}
}
private void CloseDuplicatesClear(List<TextObjectBase> list)
{
for (int i = 0; i < list.Count; i++)
{
if (i > 0)
list[i].Text = "";
}
}
private void CloseDuplicatesHide(List<TextObjectBase> list)
{
for (int i = 0; i < list.Count; i++)
{
if (i > 0)
list[i].Dispose();
}
}
private void CloseDuplicatesMerge(List<TextObjectBase> list)
{
float top = list[0].AbsTop;
// dispose all objects except the last one
for (int i = 0; i < list.Count - 1; i++)
{
list[i].Dispose();
}
// stretch the last object
TextObjectBase lastObj = list[list.Count - 1];
float delta = lastObj.AbsTop - top;
lastObj.Top -= delta;
lastObj.Height += delta;
}
private void CollectMergedTextObjects(TextObject obj)
{
if (mergedTextObjects.ContainsKey(obj.Band.Name))
{
List<TextObject> list = mergedTextObjects[obj.Band.Name];
list.Add(obj);
}
else
{
List<TextObject> list = new List<TextObject>() { obj };
mergedTextObjects.Add(obj.Band.Name, list);
}
}
private void MergeTextObjects
()
{
foreach (var band in mergedTextObjects)
{
band.Value.Sort(delegate (TextObject txt, TextObject txt2)
{
if (txt.AbsLeft.CompareTo(txt2.AbsLeft) == 0)
return txt.AbsTop.CompareTo(txt2.AbsTop);
return txt.AbsLeft.CompareTo(txt2.AbsLeft);
});
//Vertical merge
MergeTextObjectsInBand(band.Value);
//May be horizontal merge
MergeTextObjectsInBand(band.Value);
}
}
private void MergeTextObjectsInBand(List<TextObject> band)
{
for (int i = 0; i < band.Count; i++)
{
for (int j = i + 1; j < band.Count; j++)
{
if (Merge(band[j], band[i]))
{
TextObject removeObj = band[j];
band.Remove(removeObj);
removeObj.Dispose();
if (j > 0)
j--;
}
}
}
}
private bool Merge(TextObject obj, TextObject obj2)
{
if (obj2.Text != obj.Text)
return false;
var bounds = obj.AbsBounds;
if (bounds.Width < 0 || bounds.Height < 0)
Validator.NormalizeBounds(ref bounds);
var bounds2 = obj2.AbsBounds;
if (bounds2.Width < 0 || bounds2.Height < 0)
Validator.NormalizeBounds(ref bounds2);
if (obj.MergeMode.HasFlag(MergeMode.Vertical) && obj2.MergeMode.HasFlag(MergeMode.Vertical)
&& IsEqualWithInaccuracy(bounds2.Width, bounds.Width) && IsEqualWithInaccuracy(bounds2.Left, bounds.Left))
{
if (IsEqualWithInaccuracy(bounds2.Bottom, bounds.Top))
{
obj2.Height += bounds.Height;
return true;
}
else if (IsEqualWithInaccuracy(bounds2.Top, bounds.Bottom))
{
obj2.Height += bounds.Height;
obj2.Top -= bounds.Height;
return true;
}
}
else if (obj.MergeMode.HasFlag(MergeMode.Horizontal) && obj2.MergeMode.HasFlag(MergeMode.Horizontal)
&& IsEqualWithInaccuracy(bounds2.Height, bounds.Height) && IsEqualWithInaccuracy(bounds2.Top, bounds.Top))
{
if (IsEqualWithInaccuracy(bounds2.Right, bounds.Left))
{
obj2.Width += bounds.Width;
return true;
}
else if (IsEqualWithInaccuracy(bounds2.Left, bounds.Right))
{
obj2.Width += bounds.Width;
obj2.Left -= bounds.Width;
return true;
}
}
return false;
}
private bool IsEqualWithInaccuracy(float value1, float value2)
{
return Math.Abs(value1 - value2) < 0.01;
}
public void Postprocess(ReportPage page)
{
page.ExtractMacros();
ObjectCollection allObjects = page.AllObjects;
for (int i = 0; i < allObjects.Count; i++)
{
Base c = allObjects[i];
if (c.Report == null)
c.SetReport(page.Report);
c.ExtractMacros();
if (c is BandBase band)
band.UpdateWidth();
if (c is TextObjectBase txt && txt.Duplicates != Duplicates.Show)
ProcessDuplicates(txt);
if (c is TextObject text && text.MergeMode != MergeMode.None)
CollectMergedTextObjects(text);
}
MergeTextObjects();
CloseDuplicates();
}
public PreparedPagePostprocessor()
{
duplicates = new Dictionary<string, List<TextObjectBase>>();
mergedTextObjects = new Dictionary<string, List<TextObject>>();
bands = new Dictionary<int, Base>();
iBand = 0;
}
public void PostprocessUnlimited(PreparedPage preparedPage, ReportPage page)
{
bool flag = false;
int i = 0;
foreach (Base b in preparedPage.GetPageItems(page, true))
{
foreach (Base c in b.AllObjects)
{
if (c is TextObjectBase txt && txt.Duplicates != Duplicates.Show)
{
ProcessDuplicates(txt);
flag = true; //flag for keep in dictionary
}
if (c is TextObject text && text.MergeMode != MergeMode.None)
{
CollectMergedTextObjects(text);
flag = true;
}
}
i++;
if (flag)
{
b.ExtractMacros();
bands[i - 1] = b;
}
else
{
b.Dispose();
}
}
MergeTextObjects();
CloseDuplicates();
}
public Base PostProcessBandUnlimitedPage(Base band)
{
if (bands.ContainsKey(iBand))
{
Base replaceBand = bands[iBand];
Base parent = band.Parent;
band.Parent = null;
replaceBand.Parent = parent;
band.Dispose();
iBand++;
return replaceBand;
}
band.ExtractMacros();
iBand++;
return band;
}
}
}