21 #pragma warning(disable:4244) // Conversion warnings
25 #include "config_auto.h"
58 "max fraction of mean blob width allowed for vertical gaps in vertical text");
61 "Fraction of box matches required to declare a line vertical");
68 TabConstraint_LIST* constraints =
new TabConstraint_LIST;
69 TabConstraint_IT it(constraints);
70 it.add_to_end(constraint);
72 vector->set_top_constraints(constraints);
74 vector->set_bottom_constraints(constraints);
79 TabConstraint_LIST* list2) {
85 tprintf(
"Testing constraint compatibility\n");
86 GetConstraints(list1, &y_min, &y_max);
87 GetConstraints(list2, &y_min, &y_max);
89 tprintf(
"Resulting range = [%d,%d]\n", y_min, y_max);
90 return y_max >= y_min;
96 TabConstraint_LIST* list2) {
99 TabConstraint_IT it(list2);
101 tprintf(
"Merging constraints\n");
103 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
106 constraint->vector_->
Print(
"Merge");
107 if (constraint->is_top_)
113 it.add_list_before(list2);
122 GetConstraints(constraints, &y_min, &y_max);
123 int y = (y_min + y_max) / 2;
124 TabConstraint_IT it(constraints);
125 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
128 if (constraint->is_top_) {
140 : vector_(vector), is_top_(is_top) {
142 y_min_ = vector->
endpt().
y();
151 void TabConstraint::GetConstraints(TabConstraint_LIST* constraints,
152 int* y_min,
int* y_max) {
153 TabConstraint_IT it(constraints);
154 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
157 tprintf(
"Constraint is [%d,%d]", constraint->y_min_, constraint->y_max_);
158 constraint->vector_->Print(
" for");
160 *y_min =
MAX(*y_min, constraint->y_min_);
161 *y_max =
MIN(*y_max, constraint->y_max_);
183 int extended_start_y,
int extended_end_y,
184 BLOBNBOX_CLIST* good_points,
185 int* vertical_x,
int* vertical_y) {
187 alignment, good_points);
188 if (!vector->
Fit(vertical,
false)) {
193 vertical = vector->endpt_ - vector->startpt_;
195 *vertical_x += vertical.
x() * weight;
196 *vertical_y += vertical.
y() * weight;
206 : extended_ymin_(src.extended_ymin_), extended_ymax_(src.extended_ymax_),
207 sort_key_(0), percent_score_(0), mean_width_(0),
208 needs_refit_(true), needs_evaluation_(true), intersects_other_lines_(false),
209 alignment_(alignment),
210 top_constraints_(
NULL), bottom_constraints_(
NULL) {
211 BLOBNBOX_C_IT it(&boxes_);
221 sort_key_ =
SortKey(vertical_skew,
222 (startpt_.
x() + endpt_.
x()) / 2,
223 (startpt_.
y() + endpt_.
y()) / 2);
225 Print(
"Constructed a new tab vector:");
235 copy->startpt_ = startpt_;
236 copy->endpt_ = endpt_;
237 copy->alignment_ = alignment_;
238 copy->extended_ymax_ = extended_ymax_;
239 copy->extended_ymin_ = extended_ymin_;
240 copy->intersects_other_lines_ = intersects_other_lines_;
248 BLOBNBOX_C_IT it(&boxes_);
252 while (!it.at_last() && box.
top() <= new_box.
top()) {
253 if (blob == new_blob)
259 if (box.
top() >= new_box.
top()) {
260 it.add_before_stay_put(new_blob);
266 it.add_after_stay_put(new_blob);
272 startpt_.
set_y(start_y);
282 startpt_.
rotate(rotation);
284 int dx = endpt_.
x() - startpt_.
x();
285 int dy = endpt_.
y() - startpt_.
y();
286 if ((dy < 0 && abs(dy) > abs(dx)) || (dx < 0 && abs(dx) > abs(dy))) {
306 TabVector_C_IT it(&partners_);
308 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
310 if (partner->top_constraints_ ==
NULL ||
311 partner->bottom_constraints_ ==
NULL) {
312 partner->
Print(
"Impossible: has no constraints");
313 Print(
"This vector has it as a partner");
316 if (prev_partner ==
NULL) {
319 partner->bottom_constraints_))
321 partner->bottom_constraints_);
325 partner->bottom_constraints_))
327 partner->bottom_constraints_);
329 prev_partner = partner;
333 partner->top_constraints_))
335 partner->top_constraints_);
343 partner->bottom_constraints_))
345 partner->bottom_constraints_);
347 partner->top_constraints_))
349 partner->top_constraints_);
354 if (top_constraints_ !=
NULL)
356 if (bottom_constraints_ !=
NULL)
362 TabVector_LIST* vectors,
364 TabVector_IT it1(vectors);
365 for (it1.mark_cycle_pt(); !it1.cycled_list(); it1.forward()) {
367 TabVector_IT it2(it1);
368 for (it2.forward(); !it2.at_first(); it2.forward()) {
370 if (v2->
SimilarTo(vertical, *v1, grid)) {
374 v2->
Print(
"Merging");
375 v1->
Print(
"by deleting");
379 v2->
Print(
"Producing");
382 merged_vector -= v2->
startpt();
384 v2->
Print(
"Garbage result of merge?");
402 int v_scale = abs(vertical.
y());
406 if (sort_key_ + kSimilarVectorDist * v_scale >= other.sort_key_ &&
407 sort_key_ - kSimilarVectorDist * v_scale <= other.sort_key_)
411 sort_key_ + kSimilarRaggedDist * v_scale < other.sort_key_ ||
412 sort_key_ - kSimilarRaggedDist * v_scale > other.sort_key_)
422 sort_key_ < other.sort_key_) ?
this : &other;
423 int top_y = mover->endpt_.
y();
424 int bottom_y = mover->startpt_.
y();
425 int left =
MIN(mover->
XAtY(top_y), mover->
XAtY(bottom_y));
426 int right =
MAX(mover->
XAtY(top_y), mover->
XAtY(bottom_y));
427 int shift = abs(sort_key_ - other.sort_key_) / v_scale;
439 if (box.
top() > bottom_y)
444 int right_at_box = left_at_box;
446 right_at_box += shift;
448 left_at_box -= shift;
459 extended_ymin_ =
MIN(extended_ymin_, other->extended_ymin_);
460 extended_ymax_ =
MAX(extended_ymax_, other->extended_ymax_);
462 alignment_ = other->alignment_;
465 BLOBNBOX_C_IT it1(&boxes_);
466 BLOBNBOX_C_IT it2(&other->boxes_);
467 while (!it2.empty()) {
473 while (box1.
bottom() < box2.
bottom() && !it1.at_last()) {
479 it1.add_to_end(bbox2);
480 }
else if (bbox1 != bbox2) {
481 it1.add_before_stay_put(bbox2);
495 TabVector_C_IT it(&partners_);
498 if (it.data() == partner)
501 it.add_after_then_move(partner);
506 TabVector_C_IT it(&partners_);
507 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
508 if (it.data() == other)
527 tprintf(
"%s <null>\n", prefix);
529 tprintf(
"%s %s (%d,%d)->(%d,%d) w=%d s=%d, sort key=%d, boxes=%d,"
531 prefix, kAlignmentNames[alignment_],
532 startpt_.
x(), startpt_.
y(), endpt_.
x(), endpt_.
y(),
533 mean_width_, percent_score_, sort_key_,
534 boxes_.length(), partners_.length());
541 BLOBNBOX_C_IT it(&boxes_);
542 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
545 tprintf(
"Box at (%d,%d)->(%d,%d)\n",
552 #ifndef GRAPHICS_DISABLED
565 tab_win->
Line(startpt_.
x(), startpt_.
y(), endpt_.
x(), endpt_.
y());
567 tab_win->
Line(startpt_.
x(), startpt_.
y(), startpt_.
x(), extended_ymin_);
568 tab_win->
Line(endpt_.
x(), extended_ymax_, endpt_.
x(), endpt_.
y());
570 snprintf(score_buf,
sizeof(score_buf),
"%d", percent_score_);
572 tab_win->
Text(startpt_.
x(), startpt_.
y(), score_buf);
581 if (needs_evaluation_)
593 needs_evaluation_ =
false;
594 int length = endpt_.
y() - startpt_.
y();
595 if (length == 0 || boxes_.empty()) {
597 Print(
"Zero length in evaluate");
601 BLOBNBOX_C_IT it(&boxes_);
603 int height_count = 0;
604 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
607 int height = box.
height();
608 mean_height += height;
611 if (height_count > 0) mean_height /= height_count;
612 int max_gutter = kGutterMultiple * mean_height;
616 max_gutter = kGutterToNeighbourRatio * mean_height;
619 STATS gutters(0, max_gutter + 1);
623 int num_deleted_boxes = 0;
624 bool text_on_image =
false;
627 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
630 int mid_y = (box.
top() + box.
bottom()) / 2;
633 tprintf(
"After already deleting %d boxes, ", num_deleted_boxes);
634 Print(
"Starting evaluation");
642 int tab_x =
XAtY(mid_y);
646 bbox, &gutter_width, &neighbour_gap);
648 tprintf(
"Box (%d,%d)->(%d,%d) has gutter %d, ndist %d\n",
650 gutter_width, neighbour_gap);
653 if (neighbour_gap * kGutterToNeighbourRatio <= gutter_width) {
656 gutters.
add(gutter_width, 1);
660 if (prev_good_box !=
NULL) {
661 int vertical_gap = box.
bottom() - prev_good_box->
top();
662 double size1 = sqrt(static_cast<double>(prev_good_box->
area()));
663 double size2 = sqrt(static_cast<double>(box.
area()));
664 if (vertical_gap < kMaxFillinMultiple *
MIN(size1, size2))
665 good_length += vertical_gap;
667 tprintf(
"Box and prev good, gap=%d, target %g, goodlength=%d\n",
668 vertical_gap, kMaxFillinMultiple *
MIN(size1, size2),
675 prev_good_box = &box;
677 text_on_image =
true;
681 tprintf(
"Bad Box (%d,%d)->(%d,%d) with gutter %d, ndist %d\n",
683 gutter_width, neighbour_gap);
690 Print(
"Evaluating:");
695 int search_top = endpt_.
y();
696 int search_bottom = startpt_.
y();
699 prev_good_box =
NULL;
700 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
703 int mid_y = (box.
top() + box.
bottom()) / 2;
707 int tab_x =
XAtY(mid_y);
708 int max_gutter = kGutterMultiple * mean_height;
712 max_gutter = kGutterToNeighbourRatio * mean_height;
717 bbox, &gutter_width, &neighbour_gap);
719 if (gutter_width >= median_gutter * kMinGutterFraction) {
720 if (prev_good_box ==
NULL) {
723 search_bottom = box.
top();
725 prev_good_box = &box;
726 search_top = box.
bottom();
730 tprintf(
"Bad Box (%d,%d)->(%d,%d) with gutter %d, mean gutter %d\n",
732 gutter_width, median_gutter);
740 if (prev_good_box !=
NULL) {
743 int length = endpt_.
y() - startpt_.
y();
744 percent_score_ = 100 * good_length / length;
745 if (num_deleted_boxes > 0) {
753 if (search_bottom > search_top) {
754 search_bottom = startpt_.
y();
755 search_top = endpt_.
y();
757 double min_gutter_width = kLineCountReciprocal / boxes_.length();
759 min_gutter_width *= mean_height;
761 if (median_gutter > max_gutter_width)
762 max_gutter_width = median_gutter;
763 int gutter_width = finder->
GutterWidth(search_bottom, search_top, *
this,
764 text_on_image, max_gutter_width,
766 if (gutter_width < min_gutter_width) {
768 tprintf(
"Rejecting bad tab Vector with %d gutter vs %g min\n",
769 gutter_width, min_gutter_width);
771 boxes_.shallow_clear();
774 tprintf(
"Final gutter %d, vs limit of %g, required shift = %d\n",
775 gutter_width, min_gutter_width, required_shift);
783 Print(
"Evaluation complete:");
793 needs_refit_ =
false;
794 if (boxes_.empty()) {
803 sort_key_ =
SortKey(vertical, midpt.
x(), midpt.
y());
804 return startpt_.
y() != endpt_.
y();
806 if (!force_parallel && !
IsRagged()) {
809 BLOBNBOX_C_IT it(&boxes_);
811 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
816 linepoints.
Add(boxpt);
819 linepoints.
Add(top_pt);
822 linepoints.
Fit(&startpt_, &endpt_);
823 if (startpt_.
y() != endpt_.
y()) {
825 vertical -= startpt_;
828 int start_y = startpt_.
y();
829 int end_y = endpt_.
y();
831 BLOBNBOX_C_IT it(&boxes_);
836 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
839 mean_width_ += box.
width();
844 int bottom_y = box.
bottom();
845 int top_y = box.
top();
846 int key =
SortKey(vertical, x1, bottom_y);
849 startpt_ =
ICOORD(x1, bottom_y);
851 key =
SortKey(vertical, x1, top_y);
854 startpt_ =
ICOORD(x1, top_y);
861 if (width_count > 0) {
862 mean_width_ = (mean_width_ + width_count - 1) / width_count;
864 endpt_ = startpt_ + vertical;
865 needs_evaluation_ =
true;
866 if (start_y != end_y) {
868 startpt_.
set_x(
XAtY(vertical, sort_key_, start_y));
869 startpt_.
set_y(start_y);
870 endpt_.
set_x(
XAtY(vertical, sort_key_, end_y));
879 if (!partners_.singleton())
881 TabVector_C_IT partner_it(&partners_);
889 if (!partners_.singleton())
891 TabVector_C_IT partner_it(&partners_);
893 BLOBNBOX_C_IT box_it1(&boxes_);
894 BLOBNBOX_C_IT box_it2(&partner->boxes_);
898 Print(
"Testing for vertical text");
899 partner->
Print(
" partner");
902 int num_unmatched = 0;
903 int total_widths = 0;
907 STATS gaps(0, width * 2);
909 box_it2.mark_cycle_pt();
910 for (box_it1.mark_cycle_pt(); !box_it1.cycled_list(); box_it1.forward()) {
913 if (prev_bbox !=
NULL) {
916 while (!box_it2.cycled_list() && box_it2.data() != bbox &&
920 if (!box_it2.cycled_list() && box_it2.data() == bbox &&
926 total_widths += box.
width();
929 if (num_unmatched + num_matched == 0)
return NULL;
930 double avg_width = total_widths * 1.0 / (num_unmatched + num_matched);
932 int min_box_match =
static_cast<int>((num_matched + num_unmatched) *
934 bool is_vertical = (gaps.
get_total() > 0 &&
935 num_matched >= min_box_match &&
936 gaps.
median() <= max_gap);
938 tprintf(
"gaps=%d, matched=%d, unmatched=%d, min_match=%d "
939 "median gap=%.2f, width=%.2f max_gap=%.2f Vertical=%s\n",
940 gaps.
get_total(), num_matched, num_unmatched, min_box_match,
941 gaps.
median(), avg_width, max_gap, is_vertical?
"Yes":
"No");
943 return (is_vertical) ? partner :
NULL;
949 : extended_ymin_(extended_ymin), extended_ymax_(extended_ymax),
950 sort_key_(0), percent_score_(0), mean_width_(0),
951 needs_refit_(true), needs_evaluation_(true), alignment_(alignment),
952 top_constraints_(
NULL), bottom_constraints_(
NULL) {
953 BLOBNBOX_C_IT it(&boxes_);
954 it.add_list_after(boxes);
960 void TabVector::Delete(TabVector* replacement) {
961 TabVector_C_IT it(&partners_);
962 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
964 TabVector_C_IT p_it(&partner->partners_);
967 TabVector* partner_replacement = replacement;
968 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward()) {
970 if (p_partner == partner_replacement) {
971 partner_replacement =
NULL;
976 for (p_it.mark_cycle_pt(); !p_it.cycled_list(); p_it.forward()) {
978 if (p_partner ==
this) {
980 if (partner_replacement !=
NULL)
981 p_it.add_before_stay_put(partner_replacement);
984 if (partner_replacement !=
NULL) {
985 partner_replacement->AddPartner(partner);
void set_x(inT16 xin)
rewrite function
void ExtendToBox(BLOBNBOX *blob)
int extended_ymin() const
const int kSimilarRaggedDist
static bool WithinTestRegion(int detail_level, int x, int y)
const int kGutterToNeighbourRatio
bool Fit(ICOORD vertical, bool force_parallel)
const ICOORD & botleft() const
const ICOORD & endpt() const
const int kSimilarVectorDist
bool IsAPartner(const TabVector *other)
#define double_VAR(name, val, comment)
BBC * NextVerticalSearch(bool top_to_bottom)
void AddPartner(TabVector *partner)
void SetYStart(int start_y)
void Text(int x, int y, const char *mystring)
TabVector * VerticalTextlinePartner()
#define ELIST2IZE(CLASSNAME)
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
void add(inT32 value, inT32 count)
void Rotate(const FCOORD &rotation)
void Evaluate(const ICOORD &vertical, TabFind *finder)
bool SimilarTo(const ICOORD &vertical, const TabVector &other, BlobGrid *grid) const
TabVector * ShallowCopy() const
const double kLineCountReciprocal
static bool CompatibleConstraints(TabConstraint_LIST *list1, TabConstraint_LIST *list2)
TabVector * GetSinglePartner()
void rotate(const FCOORD &vec)
static void MergeSimilarTabVectors(const ICOORD &vertical, TabVector_LIST *vectors, BlobGrid *grid)
int textord_debug_tabfind
int ExtendedOverlap(int top_y, int bottom_y) const
int extended_ymax() const
static int SortKey(const ICOORD &vertical, int x, int y)
void Print(const char *prefix)
#define CLISTIZE(CLASSNAME)
inT16 y() const
access_function
void GutterWidthAndNeighbourGap(int tab_x, int mean_height, int max_gutter, bool left, BLOBNBOX *bbox, int *gutter_width, int *neighbour_gap)
bool textord_debug_printable
BlobRegionType region_type() const
const char * kAlignmentNames[]
void SetupPartnerConstraints()
const int kGutterMultiple
void set_bottom_constraints(TabConstraint_LIST *constraints)
const ICOORD & startpt() const
const double kMinGutterFraction
static void MergeConstraints(TabConstraint_LIST *list1, TabConstraint_LIST *list2)
const int kMaxFillinMultiple
const double kMinAlignedGutter
void Add(const ICOORD &pt)
static void CreateConstraint(TabVector *vector, bool is_top)
void set_y(inT16 yin)
rewrite function
int IntCastRounded(double x)
inT16 x() const
access function
double Fit(ICOORD *pt1, ICOORD *pt2)
double textord_tabvector_vertical_gap_fraction
const double kMinRaggedGutter
void Debug(const char *prefix)
double textord_tabvector_vertical_box_ratio
const ICOORD & topright() const
const TBOX & bounding_box() const
void set_top_constraints(TabConstraint_LIST *constraints)
static TabVector * FitVector(TabAlignment alignment, ICOORD vertical, int extended_start_y, int extended_end_y, BLOBNBOX_CLIST *good_points, int *vertical_x, int *vertical_y)
BlobTextFlowType flow() const
void Line(int x1, int y1, int x2, int y2)
void Display(ScrollView *tab_win)
void MergeWith(const ICOORD &vertical, TabVector *other)
void FitAndEvaluateIfNeeded(const ICOORD &vertical, TabFind *finder)
static void ApplyConstraints(TabConstraint_LIST *constraints)
int GutterWidth(int bottom_y, int top_y, const TabVector &v, bool ignore_unmergeables, int max_gutter_width, int *required_shift)
void StartVerticalSearch(int xmin, int xmax, int y)