33 static const char* kMultiBlobLabelCode =
"WordStr";
38 FILE* box_file =
nullptr;
39 if (!(box_file = fopen(filename.
string(),
"rb"))) {
63 return ReadMemBoxes(target_page, skip_blanks, &box_data[0],
64 true, boxes, texts, box_texts,
69 bool ReadMemBoxes(
int target_page,
bool skip_blanks,
const char* box_data,
70 bool continue_on_failure,
77 box_str.
split(
'\n', &lines);
78 if (lines.
empty())
return false;
80 for (
int i = 0; i < lines.
size(); ++i) {
85 if (continue_on_failure)
90 if (skip_blanks && (utf8_str ==
" " || utf8_str ==
"\t"))
continue;
91 if (target_page >= 0 && page != target_page)
continue;
92 if (boxes !=
nullptr) boxes->
push_back(box);
93 if (texts !=
nullptr) texts->
push_back(utf8_str);
94 if (box_texts !=
nullptr) {
99 if (pages !=
nullptr) pages->
push_back(page);
102 return num_boxes > 0;
107 STRING box_filename = image_filename;
108 const char *lastdot = strrchr(box_filename.
string(),
'.');
109 if (lastdot !=
nullptr)
112 box_filename +=
".box";
129 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
135 bool ReadNextBox(
int target_page,
int *line_number, FILE* box_file,
139 char *buffptr = buff;
141 while (fgets(buff,
sizeof(buff) - 1, box_file)) {
145 const auto *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
146 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
149 if (*buffptr ==
'\n' || *buffptr ==
'\0')
continue;
151 if (*buffptr ==
' ' || *buffptr ==
'\t')
continue;
152 if (*buffptr !=
'\0') {
154 tprintf(
"Box file format error on line %i; ignored\n", *line_number);
157 if (target_page >= 0 && target_page != page)
176 *bounding_box =
TBOX();
179 const char *buffptr = boxfile_str;
186 const auto *ubuf =
reinterpret_cast<const unsigned char*
>(buffptr);
187 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
191 if (*buffptr ==
'\0')
return false;
193 uch[uch_len++] = *buffptr++;
194 }
while (*buffptr !=
'\0' && *buffptr !=
' ' && *buffptr !=
'\t' &&
197 if (*buffptr !=
'\0') ++buffptr;
203 std::stringstream stream(buffptr);
204 stream.imbue(std::locale::classic());
209 stream >> *page_number;
210 if (x_max < x_min || y_max < y_min) {
211 tprintf(
"Bad box coordinates in boxfile string! %s\n", ubuf);
215 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
216 (buffptr = strchr(buffptr,
'#')) !=
nullptr) {
220 uch_len = strlen(uch);
224 while (used < uch_len) {
228 tprintf(
"Bad UTF-8 str %s starts with 0x%02x at col %d\n",
229 uch + used, uch[used], used + 1);
235 if (x_min > x_max)
Swap(&x_min, &x_max);
236 if (y_min > y_max)
Swap(&y_min, &y_max);
244 *box_str = unichar_str;