30 static const char* kMultiBlobLabelCode =
"WordStr";
35 FILE* box_file = NULL;
36 if (!(box_file = fopen(filename.
string(),
"rb"))) {
60 return ReadMemBoxes(target_page, skip_blanks, &box_data[0],
61 true, boxes, texts, box_texts,
66 bool ReadMemBoxes(
int target_page,
bool skip_blanks,
const char* box_data,
67 bool continue_on_failure,
74 box_str.
split(
'\n', &lines);
75 if (lines.
empty())
return false;
77 for (
int i = 0; i < lines.
size(); ++i) {
82 if (continue_on_failure)
87 if (skip_blanks && (utf8_str ==
" " || utf8_str ==
"\t"))
continue;
88 if (target_page >= 0 && page != target_page)
continue;
90 if (texts != NULL) texts->
push_back(utf8_str);
91 if (box_texts != NULL) {
96 if (pages != NULL) pages->
push_back(page);
104 STRING box_filename = image_filename;
105 const char *lastdot = strrchr(box_filename.
string(),
'.');
109 box_filename +=
".box";
126 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
132 bool ReadNextBox(
int target_page,
int *line_number, FILE* box_file,
136 char *buffptr = buff;
138 while (fgets(buff,
sizeof(buff) - 1, box_file)) {
142 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
143 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
146 if (*buffptr ==
'\n' || *buffptr ==
'\0')
continue;
148 if (*buffptr ==
' ' || *buffptr ==
'\t')
continue;
149 if (*buffptr !=
'\0') {
151 tprintf(
"Box file format error on line %i; ignored\n", *line_number);
154 if (target_page >= 0 && target_page != page)
173 *bounding_box =
TBOX();
176 const char *buffptr = boxfile_str;
183 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
184 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
188 if (*buffptr ==
'\0')
return false;
190 uch[uch_len++] = *buffptr++;
191 }
while (*buffptr !=
'\0' && *buffptr !=
' ' && *buffptr !=
'\t' &&
194 if (*buffptr !=
'\0') ++buffptr;
195 int x_min, y_min, x_max, y_max;
197 int count = sscanf(buffptr,
"%d %d %d %d %d",
198 &x_min, &y_min, &x_max, &y_max, page_number);
199 if (count != 5 && count != 4) {
200 tprintf(
"Bad box coordinates in boxfile string! %s\n", ubuf);
204 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
205 (buffptr = strchr(buffptr,
'#')) != NULL) {
209 uch_len = strlen(uch);
213 while (used < uch_len) {
217 tprintf(
"Bad UTF-8 str %s starts with 0x%02x at col %d\n",
218 uch + used, uch[used], used + 1);
224 if (x_min > x_max)
Swap(&x_min, &x_max);
225 if (y_min > y_max)
Swap(&y_min, &y_max);
233 *box_str = unichar_str;
const ERRCODE CANTOPENFILE
void add_str_int(const char *str, int number)
FILE * OpenBoxFile(const STRING &fname)
void chomp_string(char *str)
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
bool ReadMemBoxes(int target_page, bool skip_blanks, const char *box_data, bool continue_on_failure, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
const char * string() const
const int kBoxReadBufSize
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
STRING BoxFileName(const STRING &image_filename)
void truncate_at(inT32 index)
void error(const char *caller, TessErrorLogCode action, const char *format,...) const
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
void split(const char c, GenericVector< STRING > *splited)