32 static const char* kMultiBlobLabelCode =
"WordStr";
37 FILE* box_file =
nullptr;
38 if (!(box_file = fopen(filename.
string(),
"rb"))) {
62 return ReadMemBoxes(target_page, skip_blanks, &box_data[0],
63 true, boxes, texts, box_texts,
68 bool ReadMemBoxes(
int target_page,
bool skip_blanks,
const char* box_data,
69 bool continue_on_failure,
76 box_str.
split(
'\n', &lines);
77 if (lines.
empty())
return false;
79 for (
int i = 0; i < lines.
size(); ++i) {
84 if (continue_on_failure)
89 if (skip_blanks && (utf8_str ==
" " || utf8_str ==
"\t"))
continue;
90 if (target_page >= 0 && page != target_page)
continue;
91 if (boxes !=
nullptr) boxes->
push_back(box);
92 if (texts !=
nullptr) texts->
push_back(utf8_str);
93 if (box_texts !=
nullptr) {
98 if (pages !=
nullptr) pages->
push_back(page);
101 return num_boxes > 0;
106 STRING box_filename = image_filename;
107 const char *lastdot = strrchr(box_filename.
string(),
'.');
108 if (lastdot !=
nullptr)
111 box_filename +=
".box";
128 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
134 bool ReadNextBox(
int target_page,
int *line_number, FILE* box_file,
138 char *buffptr = buff;
140 while (fgets(buff,
sizeof(buff) - 1, box_file)) {
144 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
145 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
148 if (*buffptr ==
'\n' || *buffptr ==
'\0')
continue;
150 if (*buffptr ==
' ' || *buffptr ==
'\t')
continue;
151 if (*buffptr !=
'\0') {
153 tprintf(
"Box file format error on line %i; ignored\n", *line_number);
156 if (target_page >= 0 && target_page != page)
175 *bounding_box =
TBOX();
178 const char *buffptr = boxfile_str;
185 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
186 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
190 if (*buffptr ==
'\0')
return false;
192 uch[uch_len++] = *buffptr++;
193 }
while (*buffptr !=
'\0' && *buffptr !=
' ' && *buffptr !=
'\t' &&
196 if (*buffptr !=
'\0') ++buffptr;
197 int x_min, y_min, x_max, y_max;
199 int count = sscanf(buffptr,
"%d %d %d %d %d",
200 &x_min, &y_min, &x_max, &y_max, page_number);
202 tprintf(
"Bad box coordinates in boxfile string! %s\n", ubuf);
206 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
207 (buffptr = strchr(buffptr,
'#')) !=
nullptr) {
211 uch_len = strlen(uch);
215 while (used < uch_len) {
219 tprintf(
"Bad UTF-8 str %s starts with 0x%02x at col %d\n",
220 uch + used, uch[used], used + 1);
226 if (x_min > x_max)
Swap(&x_min, &x_max);
227 if (y_min > y_max)
Swap(&y_min, &y_max);
235 *box_str = unichar_str;
const char * string() const
FILE * OpenBoxFile(const STRING &fname)
const ERRCODE CANTOPENFILE
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
void split(const char c, GenericVector< STRING > *splited)
void chomp_string(char *str)
DLLSYM void tprintf(const char *format,...)
void truncate_at(int32_t index)
void add_str_int(const char *str, int number)
STRING BoxFileName(const STRING &image_filename)
bool ReadNextBox(int *line_number, FILE *box_file, STRING *utf8_str, TBOX *bounding_box)
void MakeBoxFileStr(const char *unichar_str, const TBOX &box, int page_num, STRING *box_str)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
const int kBoxReadBufSize
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)
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
void error(const char *caller, TessErrorLogCode action, const char *format,...) const