20 #include "config_auto.h"
29 static INT_VAR(textord_testregion_left, -1,
"Left edge of debug reporting rectangle");
30 static INT_VAR(textord_testregion_top, -1,
"Top edge of debug reporting rectangle");
31 static INT_VAR(textord_testregion_right, INT32_MAX,
"Right edge of debug rectangle");
32 static INT_VAR(textord_testregion_bottom, INT32_MAX,
"Bottom edge of debug rectangle");
74 int height,
int v_gap_multiple,
116 : gutter_fraction(0.0),
136 if (vertical_y > INT16_MAX)
137 factor = vertical_y / INT16_MAX + 1;
145 :
BlobGrid(gridsize, bleft, tright) {
153 return x >= textord_testregion_left && x <= textord_testregion_right &&
154 y <= textord_testregion_top && y >= textord_testregion_bottom;
160 #ifndef GRAPHICS_DISABLED
161 if (tab_win ==
nullptr)
169 int left_x = box.
left();
170 int right_x = box.
right();
171 int top_y = box.
top();
172 int bottom_y = box.
bottom();
183 tab_win->
Line(left_x, top_y, left_x, bottom_y);
195 tab_win->
Line(right_x, top_y, right_x, bottom_y);
205 static bool AtLeast2LineCrossings(BLOBNBOX_CLIST* blobs) {
206 BLOBNBOX_C_IT it(blobs);
207 int total_crossings = 0;
208 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
209 total_crossings += it.data()->line_crossings();
211 return total_crossings >= 2;
229 int ext_start_y, ext_end_y;
230 BLOBNBOX_CLIST good_points;
234 int pt_count = AlignTabs(align_params,
false, bbox, &good_points, &ext_end_y);
235 pt_count += AlignTabs(align_params,
true, bbox, &good_points, &ext_start_y);
236 BLOBNBOX_C_IT it(&good_points);
238 box = it.data()->bounding_box();
239 int end_y = box.
top();
242 box = it.data()->bounding_box();
244 int start_y = box.
bottom();
250 bool at_least_2_crossings = AtLeast2LineCrossings(&good_points);
255 at_least_2_crossings) {
256 int confirmed_points = 0;
258 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
269 if (!align_params.
ragged ||
270 confirmed_points + confirmed_points < pt_count) {
273 tprintf(
"Confirming tab vector of %d pts starting at %d,%d\n",
277 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
291 ext_start_y, ext_end_y,
293 vertical_x, vertical_y);
297 result->
Print(
"After fitting");
301 tprintf(
"Ragged tab used too many used points: %d out of %d\n",
302 confirmed_points, pt_count);
305 tprintf(
"Tab vector failed basic tests: pt count %d vs min %d, "
306 "length %d vs min %d, min grad %g\n",
307 pt_count, align_params.
min_points, end_y - start_y,
319 BLOBNBOX_CLIST* good_points,
int* end_y) {
321 BLOBNBOX_C_IT it(good_points);
326 tprintf(
"Starting alignment run at blob:");
330 while (bbox !=
nullptr) {
336 (it.empty() || it.data() != bbox)) {
338 it.add_before_then_move(bbox);
340 it.add_after_then_move(bbox);
347 bbox = FindAlignedBlob(params, top_to_bottom, bbox, x_start, end_y);
348 if (bbox !=
nullptr) {
355 tprintf(
"Alignment run ended with %d pts at blob:", ptcount);
368 BLOBNBOX* AlignedBlob::FindAlignedBlob(
const AlignedBlobParams& p,
370 int x_start,
int* end_y) {
373 int left_column_edge = bbox->
left_rule();
378 int start_y = top_to_bottom ? box.
bottom() : box.
top();
380 tprintf(
"Column edges for blob at (%d,%d)->(%d,%d) are [%d, %d]\n",
382 left_column_edge, right_column_edge);
390 int x2 = (p.max_v_gap * p.vertical.x() + p.vertical.y()/2) / p.vertical.y();
393 *end_y = start_y - p.max_v_gap;
396 *end_y = start_y + p.max_v_gap;
399 int xmin = std::min(x_start, x2) - skew_tolerance;
400 int xmax = std::max(x_start, x2) + skew_tolerance;
403 xmax += p.min_gutter;
404 xmin -= p.l_align_tolerance;
406 xmax += p.r_align_tolerance;
407 xmin -= p.min_gutter;
410 GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> vsearch(
this);
412 tprintf(
"Starting %s %s search at %d-%d,%d, search_size=%d, gutter=%d\n",
413 p.ragged ?
"Ragged" :
"Aligned", p.right_tab ?
"Right" :
"Left",
414 xmin, xmax, start_y, p.max_v_gap, p.min_gutter);
415 vsearch.StartVerticalSearch(xmin, xmax, start_y);
423 while ((neighbour = vsearch.NextVerticalSearch(top_to_bottom)) !=
nullptr) {
424 if (neighbour == bbox)
427 int n_y = (nbox.
top() + nbox.
bottom()) / 2;
428 if ((!top_to_bottom && n_y > start_y + p.max_v_gap) ||
429 (top_to_bottom && n_y < start_y - p.max_v_gap)) {
431 tprintf(
"Neighbour too far at (%d,%d)->(%d,%d)\n",
440 if ((n_y < start_y) != top_to_bottom || nbox.
y_overlap(box))
444 if (backup_result !=
nullptr && p.ragged && result ==
nullptr &&
446 return backup_result;
450 int x_at_n_y = x_start + (n_y - start_y) * p.vertical.x() / p.vertical.y();
451 if (x_at_n_y < neighbour->left_crossing_rule() ||
454 int n_left = nbox.
left();
455 int n_right = nbox.
right();
456 int n_x = p.right_tab ? n_right : n_left;
458 tprintf(
"neighbour at (%d,%d)->(%d,%d), n_x=%d, n_y=%d, xatn=%d\n",
462 n_left < x_at_n_y + p.min_gutter &&
463 n_right > x_at_n_y + p.r_align_tolerance &&
464 (p.ragged || n_left < x_at_n_y + p.gutter_fraction * nbox.
height())) {
468 *end_y = top_to_bottom ? nbox.
top() : nbox.
bottom();
474 n_left < x_at_n_y - p.l_align_tolerance &&
475 n_right > x_at_n_y - p.min_gutter &&
476 (p.ragged || n_right > x_at_n_y - p.gutter_fraction * nbox.
height())) {
480 *end_y = top_to_bottom ? nbox.
top() : nbox.
bottom();
488 if (n_x <= x_at_n_y + p.r_align_tolerance &&
489 n_x >= x_at_n_y - p.l_align_tolerance) {
493 tprintf(
"aligned, seeking%d, l=%d, r=%d\n",
499 if (result ==
nullptr) {
505 int x_diff = p.right_tab ? old_box.
right() : old_box.
left();
507 int y_diff = (old_box.
top() + old_box.
bottom()) / 2 - start_y;
508 int old_dist = x_diff * x_diff + y_diff * y_diff;
509 x_diff = n_x - x_at_n_y;
510 y_diff = n_y - start_y;
511 int new_dist = x_diff * x_diff + y_diff * y_diff;
512 if (new_dist < old_dist)
515 }
else if (backup_result ==
nullptr) {
518 backup_result = neighbour;
521 if ((p.right_tab && backup_box.
right() < nbox.
right()) ||
522 (!p.right_tab && backup_box.
left() > nbox.
left())) {
525 backup_result = neighbour;
530 return result !=
nullptr ? result : backup_result;