tesseract  4.00.00dev
tesseract::BoxChar Class Reference

#include <boxchar.h>

Public Member Functions

 BoxChar (const char *utf8_str, int len)
 
 ~BoxChar ()
 
const string & ch () const
 
const Box * box () const
 
const intpage () const
 
void AddBox (int x, int y, int width, int height)
 
void set_page (int page)
 
string * mutable_ch ()
 
Box * mutable_box ()
 
bool operator< (const BoxChar &other) const
 

Static Public Member Functions

static void TranslateBoxes (int xshift, int yshift, std::vector< BoxChar *> *boxes)
 
static void PrepareToWrite (std::vector< BoxChar *> *boxes)
 
static void InsertNewlines (bool rtl_rules, bool vertical_rules, std::vector< BoxChar *> *boxes)
 
static void InsertSpaces (bool rtl_rules, bool vertical_rules, std::vector< BoxChar *> *boxes)
 
static void ReorderRTLText (std::vector< BoxChar *> *boxes)
 
static bool ContainsMostlyRTL (const std::vector< BoxChar *> &boxes)
 
static bool MostlyVertical (const std::vector< BoxChar *> &boxes)
 
static int TotalByteLength (const std::vector< BoxChar *> &boxes)
 
static void RotateBoxes (float rotation, int xcenter, int ycenter, int start_box, int end_box, std::vector< BoxChar *> *boxes)
 
static void WriteTesseractBoxFile (const string &name, int height, const std::vector< BoxChar *> &boxes)
 
static string GetTesseractBoxStr (int height, const std::vector< BoxChar *> &boxes)
 

Detailed Description

Definition at line 40 of file boxchar.h.

Constructor & Destructor Documentation

◆ BoxChar()

tesseract::BoxChar::BoxChar ( const char *  utf8_str,
int  len 
)

Definition at line 41 of file boxchar.cpp.

41  : ch_(utf8_str, len) {
42  box_ = nullptr;
43 }

◆ ~BoxChar()

tesseract::BoxChar::~BoxChar ( )

Definition at line 45 of file boxchar.cpp.

45 { boxDestroy(&box_); }

Member Function Documentation

◆ AddBox()

void tesseract::BoxChar::AddBox ( int  x,
int  y,
int  width,
int  height 
)

Definition at line 47 of file boxchar.cpp.

47  {
48  box_ = boxCreate(x, y, width, height);
49 }

◆ box()

const Box* tesseract::BoxChar::box ( ) const
inline

Definition at line 48 of file boxchar.h.

48 { return box_; }

◆ ch()

const string& tesseract::BoxChar::ch ( ) const
inline

Definition at line 47 of file boxchar.h.

47 { return ch_; }

◆ ContainsMostlyRTL()

bool tesseract::BoxChar::ContainsMostlyRTL ( const std::vector< BoxChar *> &  boxes)
static

Definition at line 216 of file boxchar.cpp.

216  {
217  int num_rtl = 0, num_ltr = 0;
218  for (unsigned int i = 0; i < boxes.size(); ++i) {
219  // Convert the unichar to UTF32 representation
220  GenericVector<char32> uni_vector;
221  if (!UNICHAR::UTF8ToUnicode(boxes[i]->ch_.c_str(), &uni_vector)) {
222  tprintf("Illegal utf8 in boxchar %u string:%s = ", i,
223  boxes[i]->ch_.c_str());
224  for (size_t c = 0; c < boxes[i]->ch_.size(); ++c) {
225  tprintf(" 0x%x", boxes[i]->ch_[c]);
226  }
227  tprintf("\n");
228  continue;
229  }
230  for (int j = 0; j < uni_vector.size(); ++j) {
231  UCharDirection dir = u_charDirection(uni_vector[j]);
232  if (dir == U_RIGHT_TO_LEFT || dir == U_RIGHT_TO_LEFT_ARABIC ||
233  dir == U_ARABIC_NUMBER) {
234  ++num_rtl;
235  } else {
236  ++num_ltr;
237  }
238  }
239  }
240  return num_rtl > num_ltr;
241 }
#define tprintf(...)
Definition: tprintf.h:31
int size() const
Definition: genericvector.h:72
static bool UTF8ToUnicode(const char *utf8_str, GenericVector< int > *unicodes)
Definition: unichar.cpp:211

◆ GetTesseractBoxStr()

string tesseract::BoxChar::GetTesseractBoxStr ( int  height,
const std::vector< BoxChar *> &  boxes 
)
static

Definition at line 301 of file boxchar.cpp.

302  {
303  string output;
304  char buffer[kMaxLineLength];
305  for (size_t i = 0; i < boxes.size(); ++i) {
306  const Box* box = boxes[i]->box_;
307  if (box == nullptr) {
308  tprintf("Error: Call PrepareToWrite before WriteTesseractBoxFile!!\n");
309  return "";
310  }
311  int nbytes =
312  snprintf(buffer, kMaxLineLength, "%s %d %d %d %d %d\n",
313  boxes[i]->ch_.c_str(), box->x, height - box->y - box->h,
314  box->x + box->w, height - box->y, boxes[i]->page_);
315  output.append(buffer, nbytes);
316  }
317  return output;
318 }
#define tprintf(...)
Definition: tprintf.h:31
const Box * box() const
Definition: boxchar.h:48
const int kMaxLineLength
Definition: boxchar.cpp:292

◆ InsertNewlines()

void tesseract::BoxChar::InsertNewlines ( bool  rtl_rules,
bool  vertical_rules,
std::vector< BoxChar *> *  boxes 
)
static

Definition at line 81 of file boxchar.cpp.

82  {
83  int prev_i = -1;
84  int max_shift = 0;
85  for (int i = 0; static_cast<unsigned int>(i) < boxes->size(); ++i) {
86  Box* box = (*boxes)[i]->box_;
87  if (box == nullptr) {
88  if (prev_i < 0 || prev_i < i - 1 || static_cast<unsigned int>(i) + 1 == boxes->size()) {
89  // Erase null boxes at the start of a line and after another null box.
90  do {
91  delete (*boxes)[i];
92  boxes->erase(boxes->begin() + i);
93  --i;
94  } while (i >= 0 && static_cast<unsigned int>(i) + 1 == boxes->size() &&
95  (*boxes)[i]->box_ == nullptr);
96  }
97  continue;
98  }
99  if (prev_i >= 0) {
100  Box* prev_box = (*boxes)[prev_i]->box_;
101  int shift = box->x - prev_box->x;
102  if (vertical_rules) {
103  shift = box->y - prev_box->y;
104  } else if (rtl_rules) {
105  shift = -shift;
106  }
107  if (-shift > max_shift) {
108  // This is a newline.
109  int width = prev_box->w;
110  int height = prev_box->h;
111  int x = prev_box->x + width;
112  int y = prev_box->y;
113  if (vertical_rules) {
114  x = prev_box->x;
115  y = prev_box->y + height;
116  } else if (rtl_rules) {
117  x = prev_box->x - width;
118  if (x < 0) {
119  tprintf("prev x = %d, width=%d\n", prev_box->x, width);
120  x = 0;
121  }
122  }
123  if (prev_i == i - 1) {
124  // New character needed.
125  BoxChar* new_box = new BoxChar("\t", 1);
126  new_box->AddBox(x, y, width, height);
127  new_box->page_ = (*boxes)[i]->page_;
128  boxes->insert(boxes->begin() + i, new_box);
129  ++i;
130  } else {
131  (*boxes)[i - 1]->AddBox(x, y, width, height);
132  (*boxes)[i - 1]->ch_ = "\t";
133  }
134  max_shift = 0;
135  } else if (shift > max_shift) {
136  max_shift = shift;
137  }
138  }
139  prev_i = i;
140  }
141 }
#define tprintf(...)
Definition: tprintf.h:31
const Box * box() const
Definition: boxchar.h:48
BoxChar(const char *utf8_str, int len)
Definition: boxchar.cpp:41

◆ InsertSpaces()

void tesseract::BoxChar::InsertSpaces ( bool  rtl_rules,
bool  vertical_rules,
std::vector< BoxChar *> *  boxes 
)
static

Definition at line 145 of file boxchar.cpp.

146  {
147  // After InsertNewlines, any remaining null boxes are not newlines, and are
148  // singletons, so add a box to each remaining null box.
149  for (int i = 1; static_cast<unsigned int>(i) + 1 < boxes->size(); ++i) {
150  Box* box = (*boxes)[i]->box_;
151  if (box == nullptr) {
152  Box* prev = (*boxes)[i - 1]->box_;
153  Box* next = (*boxes)[i + 1]->box_;
154  ASSERT_HOST(prev != nullptr && next != nullptr);
155  int top = MIN(prev->y, next->y);
156  int bottom = MAX(prev->y + prev->h, next->y + next->h);
157  int left = prev->x + prev->w;
158  int right = next->x;
159  if (vertical_rules) {
160  top = prev->y + prev->h;
161  bottom = next->y;
162  left = MIN(prev->x, next->x);
163  right = MAX(prev->x + prev->w, next->x + next->w);
164  } else if (rtl_rules) {
165  // With RTL we have to account for BiDi.
166  // Right becomes the min left of all prior boxes back to the first
167  // space or newline.
168  right = prev->x;
169  left = next->x + next->w;
170  for (int j = i - 2;
171  j >= 0 && (*boxes)[j]->ch_ != " " && (*boxes)[j]->ch_ != "\t";
172  --j) {
173  prev = (*boxes)[j]->box_;
174  ASSERT_HOST(prev != nullptr);
175  if (prev->x < right) {
176  right = prev->x;
177  }
178  }
179  // Left becomes the max right of all next boxes forward to the first
180  // space or newline.
181  for (size_t j = i + 2; j < boxes->size() && (*boxes)[j]->box_ != nullptr &&
182  (*boxes)[j]->ch_ != "\t";
183  ++j) {
184  next = (*boxes)[j]->box_;
185  if (next->x + next->w > left) {
186  left = next->x + next->w;
187  }
188  }
189  }
190  // Italic and stylized characters can produce negative spaces, which
191  // Leptonica doesn't like, so clip to a positive size.
192  if (right <= left) right = left + 1;
193  if (bottom <= top) bottom = top + 1;
194  (*boxes)[i]->AddBox(left, top, right - left, bottom - top);
195  (*boxes)[i]->ch_ = " ";
196  }
197  }
198 }
#define ASSERT_HOST(x)
Definition: errcode.h:84
const Box * box() const
Definition: boxchar.h:48
#define MAX(x, y)
Definition: ndminx.h:24
#define MIN(x, y)
Definition: ndminx.h:28

◆ MostlyVertical()

bool tesseract::BoxChar::MostlyVertical ( const std::vector< BoxChar *> &  boxes)
static

Definition at line 245 of file boxchar.cpp.

245  {
246  inT64 total_dx = 0, total_dy = 0;
247  for (size_t i = 1; i < boxes.size(); ++i) {
248  if (boxes[i - 1]->box_ != nullptr && boxes[i]->box_ != nullptr &&
249  boxes[i - 1]->page_ == boxes[i]->page_) {
250  int dx = boxes[i]->box_->x - boxes[i - 1]->box_->x;
251  int dy = boxes[i]->box_->y - boxes[i - 1]->box_->y;
252  if (abs(dx) > abs(dy) * kMinNewlineRatio ||
253  abs(dy) > abs(dx) * kMinNewlineRatio) {
254  total_dx += dx * dx;
255  total_dy += dy * dy;
256  }
257  }
258  }
259  return total_dy > total_dx;
260 }
int64_t inT64
Definition: host.h:40
const int kMinNewlineRatio
Definition: boxchar.cpp:37

◆ mutable_box()

Box* tesseract::BoxChar::mutable_box ( )
inline

Definition at line 58 of file boxchar.h.

58 { return box_; }

◆ mutable_ch()

string* tesseract::BoxChar::mutable_ch ( )
inline

Definition at line 57 of file boxchar.h.

57 { return &ch_; }

◆ operator<()

bool tesseract::BoxChar::operator< ( const BoxChar other) const
inline

Definition at line 62 of file boxchar.h.

62  {
63  if (box_ == nullptr) return true;
64  if (other.box_ == nullptr) return false;
65  return box_->x < other.box_->x;
66  }

◆ page()

const int& tesseract::BoxChar::page ( ) const
inline

Definition at line 49 of file boxchar.h.

49 { return page_; }

◆ PrepareToWrite()

void tesseract::BoxChar::PrepareToWrite ( std::vector< BoxChar *> *  boxes)
static

Definition at line 66 of file boxchar.cpp.

66  {
67  bool rtl_rules = ContainsMostlyRTL(*boxes);
68  bool vertical_rules = MostlyVertical(*boxes);
69  InsertNewlines(rtl_rules, vertical_rules, boxes);
70  InsertSpaces(rtl_rules, vertical_rules, boxes);
71  for (unsigned int i = 0; i < boxes->size(); ++i) {
72  if ((*boxes)[i]->box_ == nullptr) tprintf("Null box at index %u\n", i);
73  }
74  if (rtl_rules) {
75  ReorderRTLText(boxes);
76  }
77 }
static void ReorderRTLText(std::vector< BoxChar *> *boxes)
Definition: boxchar.cpp:202
static bool MostlyVertical(const std::vector< BoxChar *> &boxes)
Definition: boxchar.cpp:245
#define tprintf(...)
Definition: tprintf.h:31
static void InsertNewlines(bool rtl_rules, bool vertical_rules, std::vector< BoxChar *> *boxes)
Definition: boxchar.cpp:81
static bool ContainsMostlyRTL(const std::vector< BoxChar *> &boxes)
Definition: boxchar.cpp:216
static void InsertSpaces(bool rtl_rules, bool vertical_rules, std::vector< BoxChar *> *boxes)
Definition: boxchar.cpp:145

◆ ReorderRTLText()

void tesseract::BoxChar::ReorderRTLText ( std::vector< BoxChar *> *  boxes)
static

Definition at line 202 of file boxchar.cpp.

202  {
203  // After adding newlines and spaces, this task is simply a matter of sorting
204  // by left each group of boxes between newlines.
205  BoxCharPtrSort sorter;
206  size_t end = 0;
207  for (size_t start = 0; start < boxes->size(); start = end + 1) {
208  end = start + 1;
209  while (end < boxes->size() && (*boxes)[end]->ch_ != "\t") ++end;
210  std::sort(boxes->begin() + start, boxes->begin() + end, sorter);
211  }
212 }
voidpf void uLong size
Definition: ioapi.h:39

◆ RotateBoxes()

void tesseract::BoxChar::RotateBoxes ( float  rotation,
int  xcenter,
int  ycenter,
int  start_box,
int  end_box,
std::vector< BoxChar *> *  boxes 
)
static

Definition at line 273 of file boxchar.cpp.

275  {
276  Boxa* orig = boxaCreate(0);
277  for (int i = start_box; i < end_box; ++i) {
278  BOX* box = (*boxes)[i]->box_;
279  if (box) boxaAddBox(orig, box, L_CLONE);
280  }
281  Boxa* rotated = boxaRotate(orig, xcenter, ycenter, rotation);
282  boxaDestroy(&orig);
283  for (int i = start_box, box_ind = 0; i < end_box; ++i) {
284  if ((*boxes)[i]->box_) {
285  boxDestroy(&((*boxes)[i]->box_));
286  (*boxes)[i]->box_ = boxaGetBox(rotated, box_ind++, L_CLONE);
287  }
288  }
289  boxaDestroy(&rotated);
290 }
const Box * box() const
Definition: boxchar.h:48

◆ set_page()

void tesseract::BoxChar::set_page ( int  page)
inline

Definition at line 55 of file boxchar.h.

55 { page_ = page; }
const int & page() const
Definition: boxchar.h:49

◆ TotalByteLength()

int tesseract::BoxChar::TotalByteLength ( const std::vector< BoxChar *> &  boxes)
static

Definition at line 264 of file boxchar.cpp.

264  {
265  int total_length = 0;
266  for (size_t i = 0; i < boxes.size(); ++i) total_length += boxes[i]->ch_.size();
267  return total_length;
268 }

◆ TranslateBoxes()

void tesseract::BoxChar::TranslateBoxes ( int  xshift,
int  yshift,
std::vector< BoxChar *> *  boxes 
)
static

Definition at line 52 of file boxchar.cpp.

53  {
54  for (size_t i = 0; i < boxes->size(); ++i) {
55  BOX* box = (*boxes)[i]->box_;
56  if (box != nullptr) {
57  box->x += xshift;
58  box->y += yshift;
59  }
60  }
61 }
const Box * box() const
Definition: boxchar.h:48

◆ WriteTesseractBoxFile()

void tesseract::BoxChar::WriteTesseractBoxFile ( const string &  name,
int  height,
const std::vector< BoxChar *> &  boxes 
)
static

Definition at line 294 of file boxchar.cpp.

295  {
296  string output = GetTesseractBoxStr(height, boxes);
298 }
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:52
static string GetTesseractBoxStr(int height, const std::vector< BoxChar *> &boxes)
Definition: boxchar.cpp:301
const char * filename
Definition: ioapi.h:38

The documentation for this class was generated from the following files: