-
Notifications
You must be signed in to change notification settings - Fork 3
/
textitem.cpp
215 lines (191 loc) · 7.11 KB
/
textitem.cpp
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
/*
Copyright © 2008-13 Qtrac Ltd. All rights reserved.
This program or module is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 2 of
the License, or (at your option) any later version. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
*/
#include "textitem.hpp"
#include <QDir>
#include <QFile>
#include <QTextStream>
QStringList TextItems::texts() const
{
QStringList list;
foreach (const TextItem &item, items)
list << item.text;
return list;
}
QList<QRectF> TextItems::rects() const
{
QList<QRectF> list;
foreach (const TextItem &item, items)
list << item.rect;
return list;
}
struct Key
{
Key(const int a, const int b, const int c) : a(a), b(b), c(c) {}
bool operator<(const Key &other) const {
if (a != other.a) return a < other.a;
if (b != other.b) return b < other.b;
return c < other.c;
}
int a, b, c;
};
void TextItems::columnZoneYxOrder(const int Width, const int ToleranceR,
const int ToleranceY, const int Columns)
{
// Phase #1: Sort all the texts into (column, y, x) order
columnYxOrder(Width, ToleranceY, Columns);
// Phase #2: Sort all the texts into (column, zone, y, x) order
QList<QPainterPath> zones = generateZones(Width, ToleranceR,
ToleranceY, Columns);
QMap<Key, TextItem> itemForZoneYx;
foreach (const TextItem &item, items) {
const QRectF rect = item.rect.adjusted(-ToleranceR, -ToleranceR,
ToleranceR, ToleranceR);
const int y = normalizedY(static_cast<int>(item.rect.y()),
ToleranceY);
for (int i = 0; i < zones.count(); ++i) {
if (zones.at(i).intersects(rect)) {
itemForZoneYx.insert(Key(i, y, item.rect.x()), item);
break;
}
}
}
items = itemForZoneYx.values();
}
void TextItems::columnYxOrder(const int Width, const int ToleranceY,
const int Columns)
{
// Phase #1: Sort all the texts into (column, y, x) order
const int Span = Width / Columns;
QMap<Key, TextItem> itemForColumnYx;
foreach (const TextItem &item, items) {
const QRect &rect = item.toRect();
const int Column = ((Columns == 1) ? 0
: (rect.width() > Span) ? Columns : rect.right() / Span);
const int y = normalizedY(static_cast<int>(rect.y()), ToleranceY);
itemForColumnYx.insert(Key(Column, y, rect.x()), item);
}
items = itemForColumnYx.values();
}
const QList<QPainterPath> TextItems::generateZones(const int Width,
const int ToleranceR, const int ToleranceY,
const int Columns) const
{ // Assumes that items are already in column, y, x order!
// Phase #1: Generate the zones
QList<QPainterPath> zones;
foreach (const TextItem &item, items) {
if (zones.isEmpty()) { // First word becomes first zone
QPainterPath zone;
zone.addRect(item.rect);
zones << zone;
} else { // Add to an existing zone within tolerance or a new one
const QRectF tolerantRect = item.rect.adjusted(-ToleranceR,
-ToleranceR, ToleranceR, ToleranceR);
bool found = false;
for (int i = 0; i < zones.count(); ++i) {
QPainterPath zone = zones.at(i);
if (zone.intersects(tolerantRect)) {
zone.addRect(item.rect);
zones[i] = zone;
found = true;
break;
}
}
if (!found) {
QPainterPath zone;
zone.addRect(item.rect);
zones << zone;
}
}
}
// Phase #2: Order the zones by (column, y, x)
const int Span = Width / Columns;
QMultiMap<Key, QPainterPath> zonesForColumn;
foreach (const QPainterPath &zone, zones) {
const QRect &rect = zone.boundingRect().toRect();
const int Column = ((Columns == 1) ? 0
: (rect.width() > Span) ? Columns : rect.right() / Span);
const int y = normalizedY(static_cast<int>(rect.y()), ToleranceY);
zonesForColumn.insertMulti(Key(Column, y, rect.x()), zone);
}
return zonesForColumn.values();
}
void TextItems::debug(const int page, const int ToleranceY,
const bool ComparingWords, const bool Yx)
{
QString filename = QDir::tempPath() + QString("/page1-%1.txt")
.arg(page);
int count = 1;
while (QFile::exists(filename)) {
filename = QDir::tempPath() + QString("/page%2-%1.txt").arg(page)
.arg(++count);
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {
qDebug("%s", qPrintable(QString("cannot write debug file: %1")
.arg(file.errorString())));
}
QTextStream out(&file);
out.setCodec("UTF-8");
out << "Page #" << page << ": "
<< (ComparingWords ? "Words" : "Characters") << " mode\n";
for (int i = 0; i < items.count(); ++i) {
const TextItem &item = items.at(i);
const QRect rect = item.toRect();
out << item.text;
if (!ComparingWords)
out << QString(" %1").arg(item.text.at(0).unicode(), 4, 16,
QChar('0'));
if (Yx) {
const int y = normalizedY(static_cast<int>(item.rect.y()),
ToleranceY);
out << QString(" (%1, %2)").arg(y).arg(rect.x());
}
out << "\n";
}
qDebug("%s", qPrintable(QString("wrote '%1'").arg(filename)));
}
inline int normalizedY(const int y, const int ToleranceY)
{
if (ToleranceY == 0)
return y;
const int remainder = y % ToleranceY;
return (remainder == 0) ? y : y + ToleranceY - remainder;
}
const TextItems getWords(const TextBoxList &list)
{
TextItems items;
foreach (const PdfTextBox &box, list) {
QString word = box->text().trimmed();
for (int i = 0; i < word.length(); ++i)
word[i] = canonicalizedCharacter(word[i]);
// DON'T DO: if (!word.isEmpty()) words << word;
// since it can mess up highlighting.
items.append(TextItem(word, box->boundingBox()));
}
return items;
}
const TextItems getCharacters(const TextBoxList &list)
{
TextItems items;
foreach (const PdfTextBox &box, list) {
const QString word = box->text();
int limit = word.count() - 1;
for (int i = limit; i >= 0; --i)
if (!word[i].isSpace())
break;
for (int i = 0; i <= limit; ++i) {
items.append(TextItem(QString(canonicalizedCharacter(word[i])),
box->charBoundingBox(i)));
}
}
return items;
}