30 static const char* kMultiBlobLabelCode =
"WordStr";
35 FILE* box_file =
NULL;
36 if (!(box_file = fopen(filename.
string(),
"rb"))) {
38 "Cant open box file %s",
59 return ReadMemBoxes(target_page, skip_blanks, &box_data[0], boxes, texts,
64 bool ReadMemBoxes(
int target_page,
bool skip_blanks,
const char* box_data,
71 box_str.
split(
'\n', &lines);
72 if (lines.
empty())
return false;
74 for (
int i = 0; i < lines.
size(); ++i) {
81 if (skip_blanks && (utf8_str ==
" " || utf8_str ==
"\t"))
continue;
82 if (target_page >= 0 && page != target_page)
continue;
85 if (box_texts !=
NULL) {
98 STRING box_filename = image_filename;
99 const char *lastdot = strrchr(box_filename.
string(),
'.');
103 box_filename +=
".box";
120 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
126 bool ReadNextBox(
int target_page,
int *line_number, FILE* box_file,
130 char *buffptr = buff;
132 while (fgets(buff,
sizeof(buff) - 1, box_file)) {
136 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
137 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
140 if (*buffptr ==
'\n' || *buffptr ==
'\0')
continue;
142 if (*buffptr ==
' ' || *buffptr ==
'\t')
continue;
143 if (*buffptr !=
'\0') {
145 tprintf(
"Box file format error on line %i; ignored\n", *line_number);
148 if (target_page >= 0 && target_page != page)
167 *bounding_box =
TBOX();
170 const char *buffptr = boxfile_str;
177 const unsigned char *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
178 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
182 if (*buffptr ==
'\0')
return false;
184 uch[uch_len++] = *buffptr++;
185 }
while (*buffptr !=
'\0' && *buffptr !=
' ' && *buffptr !=
'\t' &&
188 if (*buffptr !=
'\0') ++buffptr;
189 int x_min, y_min, x_max, y_max;
191 int count = sscanf(buffptr,
"%d %d %d %d %d",
192 &x_min, &y_min, &x_max, &y_max, page_number);
193 if (count != 5 && count != 4) {
194 tprintf(
"Bad box coordinates in boxfile string! %s\n", ubuf);
198 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
199 (buffptr = strchr(buffptr,
'#')) !=
NULL) {
203 uch_len = strlen(uch);
207 while (used < uch_len) {
208 UNICHAR ch(uch + used, uch_len - used);
211 tprintf(
"Bad UTF-8 str %s starts with 0x%02x at col %d\n",
212 uch + used, uch[used], used + 1);
218 if (x_min > x_max)
Swap(&x_min, &x_max);
219 if (y_min > y_max)
Swap(&y_min, &y_max);
227 *box_str = unichar_str;
FILE * OpenBoxFile(const STRING &fname)
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)
bool ParseBoxFileStr(const char *boxfile_str, int *page_number, STRING *utf8_str, TBOX *bounding_box)
const ERRCODE CANTOPENFILE
void set_to_given_coords(int x_min, int y_min, int x_max, int y_max)
void chomp_string(char *str)
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)
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
void truncate_at(inT32 index)
STRING BoxFileName(const STRING &image_filename)
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
void add_str_int(const char *str, int number)
const char * string() const
bool ReadAllBoxes(int target_page, bool skip_blanks, const STRING &filename, GenericVector< TBOX > *boxes, GenericVector< STRING > *texts, GenericVector< STRING > *box_texts, GenericVector< int > *pages)