21 #include "config_auto.h" 56 "max fraction of mean blob width allowed for vertical gaps in vertical text");
59 "Fraction of box matches required to declare a line vertical");
66 TabConstraint_LIST* constraints =
new TabConstraint_LIST;
67 TabConstraint_IT it(constraints);
68 it.add_to_end(constraint);
70 vector->set_top_constraints(constraints);
72 vector->set_bottom_constraints(constraints);
77 TabConstraint_LIST* list2) {
80 int y_min = -INT32_MAX;
81 int y_max = INT32_MAX;
83 tprintf(
"Testing constraint compatibility\n");
84 GetConstraints(list1, &y_min, &y_max);
85 GetConstraints(list2, &y_min, &y_max);
87 tprintf(
"Resulting range = [%d,%d]\n", y_min, y_max);
88 return y_max >= y_min;
94 TabConstraint_LIST* list2) {
97 TabConstraint_IT it(list2);
99 tprintf(
"Merging constraints\n");
101 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
104 constraint->vector_->
Print(
"Merge");
105 if (constraint->is_top_)
111 it.add_list_before(list2);
118 int y_min = -INT32_MAX;
119 int y_max = INT32_MAX;
120 GetConstraints(constraints, &y_min, &y_max);
121 int y = (y_min + y_max) / 2;
122 TabConstraint_IT it(constraints);
123 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
126 if (constraint->is_top_) {
138 : vector_(vector), is_top_(is_top) {
140 y_min_ = vector->
endpt().
y();
149 void TabConstraint::GetConstraints(TabConstraint_LIST* constraints,
150 int* y_min,
int* y_max) {
151 TabConstraint_IT it(constraints);
152 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
155 tprintf(
"Constraint is [%d,%d]", constraint->y_min_, constraint->y_max_);
156 constraint->vector_->Print(
" for");
158 *y_min = std::max(*y_min, constraint->y_min_);
159 *y_max = std::min(*y_max, constraint->y_max_);
178 int extended_start_y,
int extended_end_y,
179 BLOBNBOX_CLIST* good_points,
180 int* vertical_x,
int* vertical_y) {
182 alignment, good_points);
183 if (!vector->
Fit(vertical,
false)) {
188 vertical = vector->endpt_ - vector->startpt_;
190 *vertical_x += vertical.x() * weight;
191 *vertical_y += vertical.y() * weight;
201 : extended_ymin_(src.extended_ymin_), extended_ymax_(src.extended_ymax_),
202 sort_key_(0), percent_score_(0), mean_width_(0),
203 needs_refit_(true), needs_evaluation_(true), intersects_other_lines_(false),
204 alignment_(alignment),
205 top_constraints_(nullptr), bottom_constraints_(nullptr) {
206 BLOBNBOX_C_IT it(&boxes_);
216 sort_key_ =
SortKey(vertical_skew,
217 (startpt_.
x() + endpt_.
x()) / 2,
218 (startpt_.
y() + endpt_.
y()) / 2);
220 Print(
"Constructed a new tab vector:");
230 copy->startpt_ = startpt_;
231 copy->endpt_ = endpt_;
232 copy->alignment_ = alignment_;
233 copy->extended_ymax_ = extended_ymax_;
234 copy->extended_ymin_ = extended_ymin_;
235 copy->intersects_other_lines_ = intersects_other_lines_;
243 BLOBNBOX_C_IT it(&boxes_);
247 while (!it.at_last() && box.
top() <= new_box.
top()) {
248 if (blob == new_blob)
254 if (box.
top() >= new_box.
top()) {
255 it.add_before_stay_put(new_blob);
261 it.add_after_stay_put(new_blob);
267 startpt_.
set_y(start_y);
277 startpt_.
rotate(rotation);
279 int dx = endpt_.
x() - startpt_.
x();
280 int dy = endpt_.
y() - startpt_.
y();
281 if ((dy < 0 && abs(dy) > abs(dx)) || (dx < 0 && abs(dx) > abs(dy))) {
301 TabVector_C_IT it(&partners_);
303 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
305 if (partner->top_constraints_ ==
nullptr ||
306 partner->bottom_constraints_ ==
nullptr) {
307 partner->
Print(
"Impossible: has no constraints");
308 Print(
"This vector has it as a partner");
311 if (prev_partner ==
nullptr) {
314 partner->bottom_constraints_))
316 partner->bottom_constraints_);
320 partner->bottom_constraints_))
322 partner->bottom_constraints_);
324 prev_partner = partner;
328 partner->top_constraints_))
330 partner->top_constraints_);
338 partner->bottom_constraints_))
340 partner->bottom_constraints_);
342 partner->top_constraints_))
344 partner->top_constraints_);
349 if (top_constraints_ !=
nullptr)
351 if (bottom_constraints_ !=
nullptr)
357 TabVector_LIST* vectors,
359 TabVector_IT it1(vectors);
360 for (it1.mark_cycle_pt(); !it1.cycled_list(); it1.forward()) {
362 TabVector_IT it2(it1);
363 for (it2.forward(); !it2.at_first(); it2.forward()) {
365 if (v2->
SimilarTo(vertical, *v1, grid)) {
369 v2->
Print(
"Merging");
370 v1->
Print(
"by deleting");
374 v2->
Print(
"Producing");
377 merged_vector -= v2->
startpt();
379 v2->
Print(
"Garbage result of merge?");
397 int v_scale = abs(vertical.
y());
409 if (grid ==
nullptr) {
417 sort_key_ < other.sort_key_) ?
this : &other;
418 int top_y = mover->endpt_.
y();
419 int bottom_y = mover->startpt_.
y();
420 int left = std::min(mover->
XAtY(top_y), mover->
XAtY(bottom_y));
421 int right = std::max(mover->
XAtY(top_y), mover->
XAtY(bottom_y));
422 int shift = abs(sort_key_ - other.sort_key_) / v_scale;
434 if (box.
top() > bottom_y)
439 int right_at_box = left_at_box;
441 right_at_box += shift;
443 left_at_box -= shift;
444 if (std::min(right_at_box, static_cast<int>(box.
right())) > std::max(left_at_box, static_cast<int>(box.
left())))
454 extended_ymin_ = std::min(extended_ymin_, other->extended_ymin_);
455 extended_ymax_ = std::max(extended_ymax_, other->extended_ymax_);
457 alignment_ = other->alignment_;
460 BLOBNBOX_C_IT it1(&boxes_);
461 BLOBNBOX_C_IT it2(&other->boxes_);
462 while (!it2.empty()) {
468 while (box1.
bottom() < box2.
bottom() && !it1.at_last()) {
474 it1.add_to_end(bbox2);
475 }
else if (bbox1 != bbox2) {
476 it1.add_before_stay_put(bbox2);
490 TabVector_C_IT it(&partners_);
493 if (it.data() == partner)
496 it.add_after_then_move(partner);
501 TabVector_C_IT it(&partners_);
502 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
503 if (it.data() == other)
522 "%s %s (%d,%d)->(%d,%d) w=%d s=%d, sort key=%d, boxes=%d," 525 endpt_.
x(), endpt_.
y(), mean_width_, percent_score_, sort_key_,
526 boxes_.length(), partners_.length());
532 BLOBNBOX_C_IT it(&boxes_);
533 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
536 tprintf(
"Box at (%d,%d)->(%d,%d)\n",
543 #ifndef GRAPHICS_DISABLED 556 tab_win->
Line(startpt_.
x(), startpt_.
y(), endpt_.
x(), endpt_.
y());
558 tab_win->
Line(startpt_.
x(), startpt_.
y(), startpt_.
x(), extended_ymin_);
559 tab_win->
Line(endpt_.
x(), extended_ymax_, endpt_.
x(), endpt_.
y());
561 snprintf(score_buf,
sizeof(score_buf),
"%d", percent_score_);
563 tab_win->
Text(startpt_.
x(), startpt_.
y(), score_buf);
572 if (needs_evaluation_)
584 needs_evaluation_ =
false;
585 int length = endpt_.
y() - startpt_.
y();
586 if (length == 0 || boxes_.empty()) {
588 Print(
"Zero length in evaluate");
592 BLOBNBOX_C_IT it(&boxes_);
594 int height_count = 0;
595 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
598 int height = box.
height();
599 mean_height += height;
602 if (height_count > 0) mean_height /= height_count;
610 STATS gutters(0, max_gutter + 1);
614 int num_deleted_boxes = 0;
615 bool text_on_image =
false;
617 const TBOX* prev_good_box =
nullptr;
618 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
621 int mid_y = (box.
top() + box.
bottom()) / 2;
624 tprintf(
"After already deleting %d boxes, ", num_deleted_boxes);
625 Print(
"Starting evaluation");
633 int tab_x =
XAtY(mid_y);
637 bbox, &gutter_width, &neighbour_gap);
639 tprintf(
"Box (%d,%d)->(%d,%d) has gutter %d, ndist %d\n",
641 gutter_width, neighbour_gap);
647 gutters.
add(gutter_width, 1);
651 if (prev_good_box !=
nullptr) {
652 int vertical_gap = box.
bottom() - prev_good_box->
top();
653 double size1 = sqrt(static_cast<double>(prev_good_box->
area()));
654 double size2 = sqrt(static_cast<double>(box.
area()));
656 good_length += vertical_gap;
658 tprintf(
"Box and prev good, gap=%d, target %g, goodlength=%d\n",
666 prev_good_box = &box;
668 text_on_image =
true;
672 tprintf(
"Bad Box (%d,%d)->(%d,%d) with gutter %d, ndist %d\n",
674 gutter_width, neighbour_gap);
681 Print(
"Evaluating:");
686 int search_top = endpt_.
y();
687 int search_bottom = startpt_.
y();
690 prev_good_box =
nullptr;
691 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
694 int mid_y = (box.
top() + box.
bottom()) / 2;
698 int tab_x =
XAtY(mid_y);
708 bbox, &gutter_width, &neighbour_gap);
711 if (prev_good_box ==
nullptr) {
714 search_bottom = box.
top();
716 prev_good_box = &box;
717 search_top = box.
bottom();
721 tprintf(
"Bad Box (%d,%d)->(%d,%d) with gutter %d, mean gutter %d\n",
723 gutter_width, median_gutter);
731 if (prev_good_box !=
nullptr) {
734 int length = endpt_.
y() - startpt_.
y();
735 percent_score_ = 100 * good_length / length;
736 if (num_deleted_boxes > 0) {
744 if (search_bottom > search_top) {
745 search_bottom = startpt_.
y();
746 search_top = endpt_.
y();
750 min_gutter_width *= mean_height;
752 if (median_gutter > max_gutter_width)
753 max_gutter_width = median_gutter;
754 int gutter_width = finder->
GutterWidth(search_bottom, search_top, *
this,
755 text_on_image, max_gutter_width,
757 if (gutter_width < min_gutter_width) {
759 tprintf(
"Rejecting bad tab Vector with %d gutter vs %g min\n",
760 gutter_width, min_gutter_width);
762 boxes_.shallow_clear();
765 tprintf(
"Final gutter %d, vs limit of %g, required shift = %d\n",
766 gutter_width, min_gutter_width, required_shift);
774 Print(
"Evaluation complete:");
784 needs_refit_ =
false;
785 if (boxes_.empty()) {
794 sort_key_ =
SortKey(vertical, midpt.
x(), midpt.
y());
795 return startpt_.
y() != endpt_.
y();
797 if (!force_parallel && !
IsRagged()) {
800 BLOBNBOX_C_IT it(&boxes_);
802 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
807 linepoints.
Add(boxpt);
810 linepoints.
Add(top_pt);
813 linepoints.
Fit(&startpt_, &endpt_);
814 if (startpt_.
y() != endpt_.
y()) {
816 vertical -= startpt_;
819 int start_y = startpt_.
y();
820 int end_y = endpt_.
y();
821 sort_key_ =
IsLeftTab() ? INT32_MAX : -INT32_MAX;
822 BLOBNBOX_C_IT it(&boxes_);
827 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
830 mean_width_ += box.
width();
835 int bottom_y = box.
bottom();
836 int top_y = box.
top();
837 int key =
SortKey(vertical, x1, bottom_y);
840 startpt_ =
ICOORD(x1, bottom_y);
842 key =
SortKey(vertical, x1, top_y);
845 startpt_ =
ICOORD(x1, top_y);
852 if (width_count > 0) {
853 mean_width_ = (mean_width_ + width_count - 1) / width_count;
855 endpt_ = startpt_ + vertical;
856 needs_evaluation_ =
true;
857 if (start_y != end_y) {
859 startpt_.
set_x(
XAtY(vertical, sort_key_, start_y));
860 startpt_.
set_y(start_y);
861 endpt_.
set_x(
XAtY(vertical, sort_key_, end_y));
870 if (!partners_.singleton())
872 TabVector_C_IT partner_it(&partners_);
880 if (!partners_.singleton())
882 TabVector_C_IT partner_it(&partners_);
884 BLOBNBOX_C_IT box_it1(&boxes_);
885 BLOBNBOX_C_IT box_it2(&partner->boxes_);
889 Print(
"Testing for vertical text");
890 partner->
Print(
" partner");
893 int num_unmatched = 0;
894 int total_widths = 0;
898 STATS gaps(0, width * 2);
900 box_it2.mark_cycle_pt();
901 for (box_it1.mark_cycle_pt(); !box_it1.cycled_list(); box_it1.forward()) {
904 if (prev_bbox !=
nullptr) {
907 while (!box_it2.cycled_list() && box_it2.data() != bbox &&
911 if (!box_it2.cycled_list() && box_it2.data() == bbox &&
917 total_widths += box.
width();
920 if (num_unmatched + num_matched == 0)
return nullptr;
921 double avg_width = total_widths * 1.0 / (num_unmatched + num_matched);
923 int min_box_match =
static_cast<int>((num_matched + num_unmatched) *
925 bool is_vertical = (gaps.
get_total() > 0 &&
926 num_matched >= min_box_match &&
927 gaps.
median() <= max_gap);
929 tprintf(
"gaps=%d, matched=%d, unmatched=%d, min_match=%d " 930 "median gap=%.2f, width=%.2f max_gap=%.2f Vertical=%s\n",
931 gaps.
get_total(), num_matched, num_unmatched, min_box_match,
932 gaps.
median(), avg_width, max_gap, is_vertical?
"Yes":
"No");
934 return (is_vertical) ? partner :
nullptr;
940 : extended_ymin_(extended_ymin), extended_ymax_(extended_ymax),
941 sort_key_(0), percent_score_(0), mean_width_(0),
942 needs_refit_(true), needs_evaluation_(true), alignment_(alignment),
943 top_constraints_(nullptr), bottom_constraints_(nullptr) {
944 BLOBNBOX_C_IT it(&boxes_);
945 it.add_list_after(boxes);
951 void TabVector::Delete(TabVector* replacement) {
952 TabVector_C_IT it(&partners_);
953 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
955 TabVector_C_IT p_it(&partner->partners_);
958 TabVector* partner_replacement = replacement;
959 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward()) {
961 if (p_partner == partner_replacement) {
962 partner_replacement =
nullptr;
967 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward()) {
969 if (p_partner ==
this) {
971 if (partner_replacement !=
nullptr)
972 p_it.add_before_stay_put(partner_replacement);
975 if (partner_replacement !=
nullptr) {
976 partner_replacement->AddPartner(partner);
const ICOORD & topright() const
void set_bottom_constraints(TabConstraint_LIST *constraints)
void GutterWidthAndNeighbourGap(int tab_x, int mean_height, int max_gutter, bool left, BLOBNBOX *bbox, int *gutter_width, int *neighbour_gap)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
bool Fit(ICOORD vertical, bool force_parallel)
const ICOORD & startpt() const
const char * kAlignmentNames[]
static void CreateConstraint(TabVector *vector, bool is_top)
double textord_tabvector_vertical_box_ratio
TabVector * GetSinglePartner()
void set_x(int16_t xin)
rewrite function
#define double_VAR(name, val, comment)
int16_t y() const
access_function
const double kMinGutterFraction
static void ApplyConstraints(TabConstraint_LIST *constraints)
void Evaluate(const ICOORD &vertical, TabFind *finder)
BlobTextFlowType flow() const
static bool WithinTestRegion(int detail_level, int x, int y)
void Debug(const char *prefix)
int extended_ymin() const
void set_top_constraints(TabConstraint_LIST *constraints)
bool IsAPartner(const TabVector *other)
static bool CompatibleConstraints(TabConstraint_LIST *list1, TabConstraint_LIST *list2)
const int kGutterMultiple
void Rotate(const FCOORD &rotation)
void AddPartner(TabVector *partner)
#define ELIST2IZE(CLASSNAME)
const double kMinAlignedGutter
bool textord_debug_printable
double textord_tabvector_vertical_gap_fraction
int GutterWidth(int bottom_y, int top_y, const TabVector &v, bool ignore_unmergeables, int max_gutter_width, int *required_shift)
void Add(const ICOORD &pt)
int16_t x() const
access function
void Text(int x, int y, const char *mystring)
BlobRegionType region_type() const
int textord_debug_tabfind
int ExtendedOverlap(int top_y, int bottom_y) const
int IntCastRounded(double x)
static void MergeConstraints(TabConstraint_LIST *list1, TabConstraint_LIST *list2)
void SetupPartnerConstraints()
void ExtendToBox(BLOBNBOX *blob)
#define ELISTIZE(CLASSNAME)
void MergeWith(const ICOORD &vertical, TabVector *other)
const double kMinRaggedGutter
double Fit(ICOORD *pt1, ICOORD *pt2)
void StartVerticalSearch(int xmin, int xmax, int y)
DLLSYM void tprintf(const char *format,...)
void rotate(const FCOORD &vec)
int extended_ymax() const
void add(int32_t value, int32_t count)
const ICOORD & botleft() const
const double kLineCountReciprocal
static int SortKey(const ICOORD &vertical, int x, int y)
const int kGutterToNeighbourRatio
static void MergeSimilarTabVectors(const ICOORD &vertical, TabVector_LIST *vectors, BlobGrid *grid)
const TBOX & bounding_box() const
const int kSimilarRaggedDist
TabVector * VerticalTextlinePartner()
const int kMaxFillinMultiple
void FitAndEvaluateIfNeeded(const ICOORD &vertical, TabFind *finder)
void SetYStart(int start_y)
BBC * NextVerticalSearch(bool top_to_bottom)
CLISTIZE(BLOCK_RES) ELISTIZE(ROW_RES) ELISTIZE(WERD_RES) static const double kStopperAmbiguityThresholdGain
TabVector * ShallowCopy() const
bool SimilarTo(const ICOORD &vertical, const TabVector &other, BlobGrid *grid) const
const int kSimilarVectorDist
void set_y(int16_t yin)
rewrite function
void Display(ScrollView *tab_win)
void Print(const char *prefix)
int32_t get_total() const
const ICOORD & endpt() const
void Line(int x1, int y1, int x2, int y2)