@@ -84,8 +84,10 @@ void BoxChar::TranslateBoxes(int xshift, int yshift, std::vector<BoxChar *> *box
84
84
for (auto &boxe : *boxes) {
85
85
Box *box = boxe->box_ ;
86
86
if (box != nullptr ) {
87
- box->x += xshift;
88
- box->y += yshift;
87
+ int32_t box_x;
88
+ int32_t box_y;
89
+ boxGetGeometry (box, &box_x, &box_y, nullptr , nullptr );
90
+ boxSetGeometry (box, box_x + xshift, box_y + yshift, -1 , -1 );
89
91
}
90
92
}
91
93
}
@@ -129,10 +131,18 @@ void BoxChar::InsertNewlines(bool rtl_rules, bool vertical_rules, std::vector<Bo
129
131
continue ;
130
132
}
131
133
if (prev_i != SIZE_MAX) {
134
+ int32_t box_x;
135
+ int32_t box_y;
136
+ boxGetGeometry (box, &box_x, &box_y, nullptr , nullptr );
132
137
Box *prev_box = (*boxes)[prev_i]->box_ ;
133
- int shift = box->x - prev_box->x ;
138
+ int32_t prev_box_x;
139
+ int32_t prev_box_y;
140
+ int32_t prev_box_w;
141
+ int32_t prev_box_h;
142
+ boxGetGeometry (prev_box, &prev_box_x, &prev_box_y, &prev_box_w, &prev_box_h);
143
+ int shift = box_x - prev_box_x;
134
144
if (vertical_rules) {
135
- shift = box-> y - prev_box-> y ;
145
+ shift = box_y - prev_box_y ;
136
146
} else if (rtl_rules) {
137
147
shift = -shift;
138
148
}
@@ -142,15 +152,15 @@ void BoxChar::InsertNewlines(bool rtl_rules, bool vertical_rules, std::vector<Bo
142
152
// a box outside the image by making the width and height 1.
143
153
int width = 1 ;
144
154
int height = 1 ;
145
- int x = prev_box-> x + prev_box-> w ;
146
- int y = prev_box-> y ;
155
+ int x = prev_box_x + prev_box_w ;
156
+ int y = prev_box_y ;
147
157
if (vertical_rules) {
148
- x = prev_box-> x ;
149
- y = prev_box-> y + prev_box-> h ;
158
+ x = prev_box_x ;
159
+ y = prev_box_y + prev_box_h ;
150
160
} else if (rtl_rules) {
151
- x = prev_box-> x - width;
161
+ x = prev_box_x - width;
152
162
if (x < 0 ) {
153
- tprintf (" prev x = %d, width=%d\n " , prev_box-> x , width);
163
+ tprintf (" prev x = %d, width=%d\n " , prev_box_x , width);
154
164
x = 0 ;
155
165
}
156
166
}
@@ -184,36 +194,48 @@ void BoxChar::InsertSpaces(bool rtl_rules, bool vertical_rules, std::vector<BoxC
184
194
if (box == nullptr ) {
185
195
Box *prev = (*boxes)[i - 1 ]->box_ ;
186
196
Box *next = (*boxes)[i + 1 ]->box_ ;
197
+ int32_t prev_x;
198
+ int32_t prev_y;
199
+ int32_t prev_w;
200
+ int32_t prev_h;
201
+ int32_t next_x;
202
+ int32_t next_y;
203
+ int32_t next_w;
204
+ int32_t next_h;
187
205
ASSERT_HOST (prev != nullptr && next != nullptr );
188
- int top = std::min (prev->y , next->y );
189
- int bottom = std::max (prev->y + prev->h , next->y + next->h );
190
- int left = prev->x + prev->w ;
191
- int right = next->x ;
206
+ boxGetGeometry (prev, &prev_x, &prev_y, &prev_w, &prev_h);
207
+ boxGetGeometry (next, &next_x, &next_y, &next_w, &next_h);
208
+ int top = std::min (prev_y, next_y);
209
+ int bottom = std::max (prev_y + prev_h, next_y + next_h);
210
+ int left = prev_x + prev_w;
211
+ int right = next_x;
192
212
if (vertical_rules) {
193
- top = prev-> y + prev-> h ;
194
- bottom = next-> y ;
195
- left = std::min (prev-> x , next-> x );
196
- right = std::max (prev-> x + prev-> w , next-> x + next-> w );
213
+ top = prev_y + prev_h ;
214
+ bottom = next_y ;
215
+ left = std::min (prev_x, next_x );
216
+ right = std::max (prev_x + prev_w, next_x + next_w );
197
217
} else if (rtl_rules) {
198
218
// With RTL we have to account for BiDi.
199
219
// Right becomes the min left of all prior boxes back to the first
200
220
// space or newline.
201
- right = prev-> x ;
202
- left = next-> x + next-> w ;
221
+ right = prev_x ;
222
+ left = next_x + next_w ;
203
223
for (int j = i - 2 ; j >= 0 && (*boxes)[j]->ch_ != " " && (*boxes)[j]->ch_ != " \t " ; --j) {
204
224
prev = (*boxes)[j]->box_ ;
205
225
ASSERT_HOST (prev != nullptr );
206
- if (prev->x < right) {
207
- right = prev->x ;
226
+ boxGetGeometry (prev, &prev_x, nullptr , nullptr , nullptr );
227
+ if (prev_x < right) {
228
+ right = prev_x;
208
229
}
209
230
}
210
231
// Left becomes the max right of all next boxes forward to the first
211
232
// space or newline.
212
233
for (size_t j = i + 2 ;
213
234
j < boxes->size () && (*boxes)[j]->box_ != nullptr && (*boxes)[j]->ch_ != " \t " ; ++j) {
214
235
next = (*boxes)[j]->box_ ;
215
- if (next->x + next->w > left) {
216
- left = next->x + next->w ;
236
+ boxGetGeometry (next, &next_x, nullptr , &next_w, nullptr );
237
+ if (next_x + next_w > left) {
238
+ left = next_x + next_w;
217
239
}
218
240
}
219
241
}
@@ -275,8 +297,14 @@ bool BoxChar::MostlyVertical(const std::vector<BoxChar *> &boxes) {
275
297
for (size_t i = 1 ; i < boxes.size (); ++i) {
276
298
if (boxes[i - 1 ]->box_ != nullptr && boxes[i]->box_ != nullptr &&
277
299
boxes[i - 1 ]->page_ == boxes[i]->page_ ) {
278
- int dx = boxes[i]->box_ ->x - boxes[i - 1 ]->box_ ->x ;
279
- int dy = boxes[i]->box_ ->y - boxes[i - 1 ]->box_ ->y ;
300
+ int32_t x0;
301
+ int32_t y0 ;
302
+ boxGetGeometry (boxes[i]->box_ , &x0, &y0 , nullptr , nullptr );
303
+ int32_t x1;
304
+ int32_t y1 ;
305
+ boxGetGeometry (boxes[i - 1 ]->box_ , &x1, &y1 , nullptr , nullptr );
306
+ int dx = x0 - x1;
307
+ int dy = y0 - y1 ;
280
308
if (abs (dx) > abs (dy) * kMinNewlineRatio || abs (dy) > abs (dx) * kMinNewlineRatio ) {
281
309
total_dx += dx * dx;
282
310
total_dy += dy * dy;
@@ -337,8 +365,14 @@ std::string BoxChar::GetTesseractBoxStr(int height, const std::vector<BoxChar *>
337
365
tprintf (" Error: Call PrepareToWrite before WriteTesseractBoxFile!!\n " );
338
366
return " " ;
339
367
}
340
- int nbytes = snprintf (buffer, kMaxLineLength , " %s %d %d %d %d %d\n " , boxe->ch_ .c_str (), box->x ,
341
- height - box->y - box->h , box->x + box->w , height - box->y , boxe->page_ );
368
+ int32_t box_x;
369
+ int32_t box_y;
370
+ int32_t box_w;
371
+ int32_t box_h;
372
+ boxGetGeometry (const_cast <Box *>(box), &box_x, &box_y, &box_w, &box_h);
373
+ int nbytes = snprintf (buffer, kMaxLineLength , " %s %d %d %d %d %d\n " ,
374
+ boxe->ch_ .c_str (), box_x, height - box_y - box_h,
375
+ box_x + box_w, height - box_y, boxe->page_ );
342
376
output.append (buffer, nbytes);
343
377
}
344
378
return output;
0 commit comments