20 #include "config_auto.h" 55 "max fraction of mean blob width allowed for vertical gaps in vertical text");
58 "Fraction of box matches required to declare a line vertical");
65 auto* constraints =
new TabConstraint_LIST;
66 TabConstraint_IT it(constraints);
67 it.add_to_end(constraint);
69 vector->set_top_constraints(constraints);
71 vector->set_bottom_constraints(constraints);
76 TabConstraint_LIST* list2) {
79 int y_min = -INT32_MAX;
80 int y_max = INT32_MAX;
82 tprintf(
"Testing constraint compatibility\n");
83 GetConstraints(list1, &y_min, &y_max);
84 GetConstraints(list2, &y_min, &y_max);
86 tprintf(
"Resulting range = [%d,%d]\n", y_min, y_max);
87 return y_max >= y_min;
93 TabConstraint_LIST* list2) {
96 TabConstraint_IT it(list2);
98 tprintf(
"Merging constraints\n");
100 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
103 constraint->vector_->
Print(
"Merge");
104 if (constraint->is_top_)
110 it.add_list_before(list2);
117 int y_min = -INT32_MAX;
118 int y_max = INT32_MAX;
119 GetConstraints(constraints, &y_min, &y_max);
120 int y = (y_min + y_max) / 2;
121 TabConstraint_IT it(constraints);
122 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
125 if (constraint->is_top_) {
137 : vector_(vector), is_top_(is_top) {
139 y_min_ = vector->
endpt().
y();
148 void TabConstraint::GetConstraints(TabConstraint_LIST* constraints,
149 int* y_min,
int* y_max) {
150 TabConstraint_IT it(constraints);
151 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
154 tprintf(
"Constraint is [%d,%d]", constraint->y_min_, constraint->y_max_);
155 constraint->vector_->
Print(
" for");
157 *y_min = std::max(*y_min, constraint->y_min_);
158 *y_max = std::min(*y_max, constraint->y_max_);
177 int extended_start_y,
int extended_end_y,
178 BLOBNBOX_CLIST* good_points,
179 int* vertical_x,
int* vertical_y) {
180 auto* vector =
new TabVector(extended_start_y, extended_end_y,
181 alignment, good_points);
182 if (!vector->
Fit(vertical,
false)) {
187 vertical = vector->endpt_ - vector->startpt_;
189 *vertical_x += vertical.x() * weight;
190 *vertical_y += vertical.y() * weight;
200 : extended_ymin_(src.extended_ymin_), extended_ymax_(src.extended_ymax_),
201 sort_key_(0), percent_score_(0), mean_width_(0),
202 needs_refit_(true), needs_evaluation_(true), intersects_other_lines_(false),
203 alignment_(alignment),
204 top_constraints_(nullptr), bottom_constraints_(nullptr) {
205 BLOBNBOX_C_IT it(&boxes_);
215 sort_key_ =
SortKey(vertical_skew,
216 (startpt_.
x() + endpt_.
x()) / 2,
217 (startpt_.
y() + endpt_.
y()) / 2);
219 Print(
"Constructed a new tab vector:");
229 copy->startpt_ = startpt_;
230 copy->endpt_ = endpt_;
231 copy->alignment_ = alignment_;
232 copy->extended_ymax_ = extended_ymax_;
233 copy->extended_ymin_ = extended_ymin_;
234 copy->intersects_other_lines_ = intersects_other_lines_;
242 BLOBNBOX_C_IT it(&boxes_);
246 while (!it.at_last() && box.
top() <= new_box.
top()) {
247 if (blob == new_blob)
253 if (box.
top() >= new_box.
top()) {
254 it.add_before_stay_put(new_blob);
260 it.add_after_stay_put(new_blob);
266 startpt_.
set_y(start_y);
276 startpt_.
rotate(rotation);
278 int dx = endpt_.
x() - startpt_.
x();
279 int dy = endpt_.
y() - startpt_.
y();
280 if ((dy < 0 && abs(dy) > abs(dx)) || (dx < 0 && abs(dx) > abs(dy))) {
300 TabVector_C_IT it(&partners_);
302 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
304 if (partner->top_constraints_ ==
nullptr ||
305 partner->bottom_constraints_ ==
nullptr) {
306 partner->
Print(
"Impossible: has no constraints");
307 Print(
"This vector has it as a partner");
310 if (prev_partner ==
nullptr) {
313 partner->bottom_constraints_))
315 partner->bottom_constraints_);
319 partner->bottom_constraints_))
321 partner->bottom_constraints_);
323 prev_partner = partner;
327 partner->top_constraints_))
329 partner->top_constraints_);
337 partner->bottom_constraints_))
339 partner->bottom_constraints_);
341 partner->top_constraints_))
343 partner->top_constraints_);
348 if (top_constraints_ !=
nullptr)
350 if (bottom_constraints_ !=
nullptr)
356 TabVector_LIST* vectors,
358 TabVector_IT it1(vectors);
359 for (it1.mark_cycle_pt(); !it1.cycled_list(); it1.forward()) {
361 TabVector_IT it2(it1);
362 for (it2.forward(); !it2.at_first(); it2.forward()) {
364 if (v2->
SimilarTo(vertical, *v1, grid)) {
368 v2->
Print(
"Merging");
369 v1->
Print(
"by deleting");
373 v2->
Print(
"Producing");
376 merged_vector -= v2->
startpt();
378 v2->
Print(
"Garbage result of merge?");
396 int v_scale = abs(vertical.
y());
408 if (grid ==
nullptr) {
416 sort_key_ < other.sort_key_) ?
this : &other;
417 int top_y = mover->endpt_.
y();
418 int bottom_y = mover->startpt_.
y();
419 int left = std::min(mover->
XAtY(top_y), mover->
XAtY(bottom_y));
420 int right = std::max(mover->
XAtY(top_y), mover->
XAtY(bottom_y));
421 int shift = abs(sort_key_ - other.sort_key_) / v_scale;
433 if (box.
top() > bottom_y)
438 int right_at_box = left_at_box;
440 right_at_box += shift;
442 left_at_box -= shift;
443 if (std::min(right_at_box, static_cast<int>(box.
right())) > std::max(left_at_box, static_cast<int>(box.
left())))
453 extended_ymin_ = std::min(extended_ymin_, other->extended_ymin_);
454 extended_ymax_ = std::max(extended_ymax_, other->extended_ymax_);
456 alignment_ = other->alignment_;
459 BLOBNBOX_C_IT it1(&boxes_);
460 BLOBNBOX_C_IT it2(&other->boxes_);
461 while (!it2.empty()) {
467 while (box1.
bottom() < box2.
bottom() && !it1.at_last()) {
473 it1.add_to_end(bbox2);
474 }
else if (bbox1 != bbox2) {
475 it1.add_before_stay_put(bbox2);
489 TabVector_C_IT it(&partners_);
492 if (it.data() == partner)
495 it.add_after_then_move(partner);
500 TabVector_C_IT it(&partners_);
501 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
502 if (it.data() == other)
509 static const char*
const kAlignmentNames[] = {
521 "%s %s (%d,%d)->(%d,%d) w=%d s=%d, sort key=%d, boxes=%d," 523 prefix, kAlignmentNames[alignment_], startpt_.
x(), startpt_.
y(),
524 endpt_.
x(), endpt_.
y(), mean_width_, percent_score_, sort_key_,
525 boxes_.length(), partners_.length());
531 BLOBNBOX_C_IT it(&boxes_);
532 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
535 tprintf(
"Box at (%d,%d)->(%d,%d)\n",
542 #ifndef GRAPHICS_DISABLED 555 tab_win->
Line(startpt_.
x(), startpt_.
y(), endpt_.
x(), endpt_.
y());
557 tab_win->
Line(startpt_.
x(), startpt_.
y(), startpt_.
x(), extended_ymin_);
558 tab_win->
Line(endpt_.
x(), extended_ymax_, endpt_.
x(), endpt_.
y());
560 snprintf(score_buf,
sizeof(score_buf),
"%d", percent_score_);
562 tab_win->
Text(startpt_.
x(), startpt_.
y(), score_buf);
571 if (needs_evaluation_)
583 needs_evaluation_ =
false;
584 int length = endpt_.
y() - startpt_.
y();
585 if (length == 0 || boxes_.empty()) {
587 Print(
"Zero length in evaluate");
591 BLOBNBOX_C_IT it(&boxes_);
593 int height_count = 0;
594 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
597 int height = box.
height();
598 mean_height += height;
601 if (height_count > 0) mean_height /= height_count;
609 STATS gutters(0, max_gutter + 1);
613 int num_deleted_boxes = 0;
614 bool text_on_image =
false;
616 const TBOX* prev_good_box =
nullptr;
617 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
620 int mid_y = (box.
top() + box.
bottom()) / 2;
623 tprintf(
"After already deleting %d boxes, ", num_deleted_boxes);
624 Print(
"Starting evaluation");
632 int tab_x =
XAtY(mid_y);
636 bbox, &gutter_width, &neighbour_gap);
638 tprintf(
"Box (%d,%d)->(%d,%d) has gutter %d, ndist %d\n",
640 gutter_width, neighbour_gap);
646 gutters.
add(gutter_width, 1);
650 if (prev_good_box !=
nullptr) {
651 int vertical_gap = box.
bottom() - prev_good_box->
top();
652 double size1 = sqrt(static_cast<double>(prev_good_box->
area()));
653 double size2 = sqrt(static_cast<double>(box.
area()));
655 good_length += vertical_gap;
657 tprintf(
"Box and prev good, gap=%d, target %g, goodlength=%d\n",
665 prev_good_box = &box;
667 text_on_image =
true;
671 tprintf(
"Bad Box (%d,%d)->(%d,%d) with gutter %d, ndist %d\n",
673 gutter_width, neighbour_gap);
680 Print(
"Evaluating:");
685 int search_top = endpt_.
y();
686 int search_bottom = startpt_.
y();
689 prev_good_box =
nullptr;
690 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
693 int mid_y = (box.
top() + box.
bottom()) / 2;
697 int tab_x =
XAtY(mid_y);
707 bbox, &gutter_width, &neighbour_gap);
710 if (prev_good_box ==
nullptr) {
713 search_bottom = box.
top();
715 prev_good_box = &box;
716 search_top = box.
bottom();
720 tprintf(
"Bad Box (%d,%d)->(%d,%d) with gutter %d, mean gutter %d\n",
722 gutter_width, median_gutter);
730 if (prev_good_box !=
nullptr) {
733 int length = endpt_.
y() - startpt_.
y();
734 percent_score_ = 100 * good_length / length;
735 if (num_deleted_boxes > 0) {
743 if (search_bottom > search_top) {
744 search_bottom = startpt_.
y();
745 search_top = endpt_.
y();
749 min_gutter_width *= mean_height;
751 if (median_gutter > max_gutter_width)
752 max_gutter_width = median_gutter;
753 int gutter_width = finder->
GutterWidth(search_bottom, search_top, *
this,
754 text_on_image, max_gutter_width,
756 if (gutter_width < min_gutter_width) {
758 tprintf(
"Rejecting bad tab Vector with %d gutter vs %g min\n",
759 gutter_width, min_gutter_width);
761 boxes_.shallow_clear();
764 tprintf(
"Final gutter %d, vs limit of %g, required shift = %d\n",
765 gutter_width, min_gutter_width, required_shift);
773 Print(
"Evaluation complete:");
783 needs_refit_ =
false;
784 if (boxes_.empty()) {
793 sort_key_ =
SortKey(vertical, midpt.
x(), midpt.
y());
794 return startpt_.
y() != endpt_.
y();
796 if (!force_parallel && !
IsRagged()) {
799 BLOBNBOX_C_IT it(&boxes_);
801 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
806 linepoints.
Add(boxpt);
809 linepoints.
Add(top_pt);
812 linepoints.
Fit(&startpt_, &endpt_);
813 if (startpt_.
y() != endpt_.
y()) {
815 vertical -= startpt_;
818 int start_y = startpt_.
y();
819 int end_y = endpt_.
y();
820 sort_key_ =
IsLeftTab() ? INT32_MAX : -INT32_MAX;
821 BLOBNBOX_C_IT it(&boxes_);
826 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
829 mean_width_ += box.
width();
834 int bottom_y = box.
bottom();
835 int top_y = box.
top();
836 int key =
SortKey(vertical, x1, bottom_y);
839 startpt_ =
ICOORD(x1, bottom_y);
841 key =
SortKey(vertical, x1, top_y);
844 startpt_ =
ICOORD(x1, top_y);
851 if (width_count > 0) {
852 mean_width_ = (mean_width_ + width_count - 1) / width_count;
854 endpt_ = startpt_ + vertical;
855 needs_evaluation_ =
true;
856 if (start_y != end_y) {
858 startpt_.
set_x(
XAtY(vertical, sort_key_, start_y));
859 startpt_.
set_y(start_y);
860 endpt_.
set_x(
XAtY(vertical, sort_key_, end_y));
869 if (!partners_.singleton())
871 TabVector_C_IT partner_it(&partners_);
879 if (!partners_.singleton())
881 TabVector_C_IT partner_it(&partners_);
883 BLOBNBOX_C_IT box_it1(&boxes_);
884 BLOBNBOX_C_IT box_it2(&partner->boxes_);
888 Print(
"Testing for vertical text");
889 partner->
Print(
" partner");
892 int num_unmatched = 0;
893 int total_widths = 0;
897 STATS gaps(0, width * 2);
899 box_it2.mark_cycle_pt();
900 for (box_it1.mark_cycle_pt(); !box_it1.cycled_list(); box_it1.forward()) {
903 if (prev_bbox !=
nullptr) {
906 while (!box_it2.cycled_list() && box_it2.data() != bbox &&
910 if (!box_it2.cycled_list() && box_it2.data() == bbox &&
916 total_widths += box.
width();
919 if (num_unmatched + num_matched == 0)
return nullptr;
920 double avg_width = total_widths * 1.0 / (num_unmatched + num_matched);
922 int min_box_match =
static_cast<int>((num_matched + num_unmatched) *
924 bool is_vertical = (gaps.
get_total() > 0 &&
925 num_matched >= min_box_match &&
926 gaps.
median() <= max_gap);
928 tprintf(
"gaps=%d, matched=%d, unmatched=%d, min_match=%d " 929 "median gap=%.2f, width=%.2f max_gap=%.2f Vertical=%s\n",
930 gaps.
get_total(), num_matched, num_unmatched, min_box_match,
931 gaps.
median(), avg_width, max_gap, is_vertical?
"Yes":
"No");
933 return (is_vertical) ? partner :
nullptr;
940 sort_key_(0), percent_score_(0), mean_width_(0),
941 needs_refit_(
true), needs_evaluation_(
true), alignment_(alignment),
942 top_constraints_(
nullptr), bottom_constraints_(
nullptr) {
943 BLOBNBOX_C_IT it(&boxes_);
944 it.add_list_after(boxes);
950 void TabVector::Delete(
TabVector* replacement) {
951 TabVector_C_IT it(&partners_);
952 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
954 TabVector_C_IT p_it(&partner->partners_);
957 TabVector* partner_replacement = replacement;
958 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward()) {
960 if (p_partner == partner_replacement) {
961 partner_replacement =
nullptr;
966 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward()) {
968 if (p_partner ==
this) {
970 if (partner_replacement !=
nullptr)
971 p_it.add_before_stay_put(partner_replacement);
974 if (partner_replacement !=
nullptr) {
int IntCastRounded(double x)
static void MergeConstraints(TabConstraint_LIST *list1, TabConstraint_LIST *list2)
void Text(int x, int y, const char *mystring)
void rotate(const FCOORD &vec)
void FitAndEvaluateIfNeeded(const ICOORD &vertical, TabFind *finder)
void Display(ScrollView *tab_win)
int GutterWidth(int bottom_y, int top_y, const TabVector &v, bool ignore_unmergeables, int max_gutter_width, int *required_shift)
void set_x(int16_t xin)
rewrite function
void Evaluate(const ICOORD &vertical, TabFind *finder)
const int kSimilarVectorDist
#define double_VAR(name, val, comment)
double Fit(ICOORD *pt1, ICOORD *pt2)
static void ApplyConstraints(TabConstraint_LIST *constraints)
bool SimilarTo(const ICOORD &vertical, const TabVector &other, BlobGrid *grid) const
const double kMinGutterFraction
void Print(const char *prefix)
const TBOX & bounding_box() const
const int kSimilarRaggedDist
int16_t y() const
access_function
void set_y(int16_t yin)
rewrite function
bool IsAPartner(const TabVector *other)
int extended_ymax() const
void set_top_constraints(TabConstraint_LIST *constraints)
BlobTextFlowType flow() const
void add(int32_t value, int32_t count)
void set_bottom_constraints(TabConstraint_LIST *constraints)
double textord_tabvector_vertical_box_ratio
int extended_ymin() const
const int kGutterMultiple
const ICOORD & botleft() const
const double kMinRaggedGutter
static int SortKey(const ICOORD &vertical, int x, int y)
void Add(const ICOORD &pt)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
const int kMaxFillinMultiple
static void MergeSimilarTabVectors(const ICOORD &vertical, TabVector_LIST *vectors, BlobGrid *grid)
const double kLineCountReciprocal
#define ELISTIZE(CLASSNAME)
bool textord_debug_printable
TabVector * GetSinglePartner()
int ExtendedOverlap(int top_y, int bottom_y) const
const ICOORD & topright() const
BBC * NextVerticalSearch(bool top_to_bottom)
double textord_tabvector_vertical_gap_fraction
const ICOORD & startpt() const
void Debug(const char *prefix)
void MergeWith(const ICOORD &vertical, TabVector *other)
int16_t x() const
access function
void SetYStart(int start_y)
void ExtendToBox(BLOBNBOX *blob)
DLLSYM void tprintf(const char *format,...)
static bool CompatibleConstraints(TabConstraint_LIST *list1, TabConstraint_LIST *list2)
static void CreateConstraint(TabVector *vector, bool is_top)
int textord_debug_tabfind
void Rotate(const FCOORD &rotation)
CLISTIZE(BLOCK_RES) ELISTIZE(ROW_RES) ELISTIZE(WERD_RES) static const double kStopperAmbiguityThresholdGain
const ICOORD & endpt() const
void AddPartner(TabVector *partner)
BlobRegionType region_type() const
void StartVerticalSearch(int xmin, int xmax, int y)
TabVector * ShallowCopy() const
int32_t get_total() const
void GutterWidthAndNeighbourGap(int tab_x, int mean_height, int max_gutter, bool left, BLOBNBOX *bbox, int *gutter_width, int *neighbour_gap)
void Line(int x1, int y1, int x2, int y2)
void SetupPartnerConstraints()
const double kMinAlignedGutter
const int kGutterToNeighbourRatio
#define ELIST2IZE(CLASSNAME)
static bool WithinTestRegion(int detail_level, int x, int y)
bool Fit(ICOORD vertical, bool force_parallel)
TabVector * VerticalTextlinePartner()