30 static const char* kMultiBlobLabelCode =
"WordStr";
35 FILE* box_file = NULL;
36 if (!(box_file = fopen(
filename.string(),
"rb"))) {
58 return ReadMemBoxes(target_page, skip_blanks, &box_data[0], boxes, texts,
63 bool ReadMemBoxes(
int target_page,
bool skip_blanks,
const char* box_data,
70 box_str.
split(
'\n', &lines);
71 if (lines.
empty())
return false;
73 for (
int i = 0; i < lines.
size(); ++i) {
80 if (skip_blanks && (utf8_str ==
" " || utf8_str ==
"\t"))
continue;
81 if (target_page >= 0 && page != target_page)
continue;
83 if (texts != NULL) texts->
push_back(utf8_str);
84 if (box_texts != NULL) {
89 if (pages != NULL) pages->
push_back(page);
97 STRING box_filename = image_filename;
98 const char *lastdot = strrchr(box_filename.
string(),
'.');
102 box_filename +=
".box";
119 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
125 bool ReadNextBox(
int target_page,
int *line_number, FILE* box_file,
129 char *buffptr = buff;
131 while (fgets(buff,
sizeof(buff) - 1, box_file)) {
135 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
136 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
139 if (*buffptr ==
'\n' || *buffptr ==
'\0')
continue;
141 if (*buffptr ==
' ' || *buffptr ==
'\t')
continue;
142 if (*buffptr !=
'\0') {
144 tprintf(
"Box file format error on line %i; ignored\n", *line_number);
147 if (target_page >= 0 && target_page != page)
166 *bounding_box =
TBOX();
169 const char *buffptr = boxfile_str;
176 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
177 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
181 if (*buffptr ==
'\0')
return false;
183 uch[uch_len++] = *buffptr++;
184 }
while (*buffptr !=
'\0' && *buffptr !=
' ' && *buffptr !=
'\t' &&
187 if (*buffptr !=
'\0') ++buffptr;
188 int x_min, y_min, x_max, y_max;
190 int count = sscanf(buffptr,
"%d %d %d %d %d",
191 &x_min, &y_min, &x_max, &y_max, page_number);
193 tprintf(
"Bad box coordinates in boxfile string! %s\n", ubuf);
197 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
198 (buffptr = strchr(buffptr,
'#')) != NULL) {
202 uch_len = strlen(uch);
206 while (used < uch_len) {
207 UNICHAR ch(uch + used, uch_len - used);
210 tprintf(
"Bad UTF-8 str %s starts with 0x%02x at col %d\n",
211 uch + used, uch[used], used + 1);
217 if (x_min > x_max)
Swap(&x_min, &x_max);
218 if (y_min > y_max)
Swap(&y_min, &y_max);
226 *box_str = unichar_str;
const int kBoxReadBufSize
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
void split(const char c, GenericVector< STRING > *splited)
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
void add_str_int(const char *str, int number)
void truncate_at(inT32 index)
const char * string() const
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
STRING BoxFileName(const STRING &image_filename)
const ERRCODE CANTOPENFILE
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
FILE * OpenBoxFile(const STRING &fname)
void chomp_string(char *str)
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)