34 static const char* kMultiBlobLabelCode =
"WordStr";
37 static std::string BoxFileName(
const char* image_filename) {
39 size_t length = box_filename.length();
41 if (
last ==
".bin.png" ||
last ==
".nrm.png") {
42 box_filename.resize(length - 8);
44 size_t lastdot = box_filename.find_last_of(
'.');
45 if (lastdot < length) {
46 box_filename.resize(lastdot);
49 box_filename +=
".box";
56 FILE* box_file =
nullptr;
57 if (!(box_file = fopen(filename.c_str(),
"rb"))) {
81 return ReadMemBoxes(target_page, skip_blanks, &box_data[0],
82 true, boxes, texts, box_texts,
87 bool ReadMemBoxes(
int target_page,
bool skip_blanks,
const char* box_data,
88 bool continue_on_failure,
95 box_str.
split(
'\n', &lines);
96 if (lines.
empty())
return false;
98 for (
int i = 0; i < lines.
size(); ++i) {
103 if (continue_on_failure)
108 if (skip_blanks && (utf8_str ==
" " || utf8_str ==
"\t"))
continue;
109 if (target_page >= 0 && page != target_page)
continue;
110 if (boxes !=
nullptr) boxes->
push_back(box);
111 if (texts !=
nullptr) texts->
push_back(utf8_str);
112 if (box_texts !=
nullptr) {
117 if (pages !=
nullptr) pages->
push_back(page);
120 return num_boxes > 0;
136 return ReadNextBox(-1, line_number, box_file, utf8_str, bounding_box);
142 bool ReadNextBox(
int target_page,
int *line_number, FILE* box_file,
146 char *buffptr = buff;
148 while (fgets(buff,
sizeof(buff) - 1, box_file)) {
152 const auto *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
153 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
156 if (*buffptr ==
'\n' || *buffptr ==
'\0')
continue;
158 if (*buffptr ==
' ' || *buffptr ==
'\t')
continue;
159 if (*buffptr !=
'\0') {
161 tprintf(
"Box file format error on line %i; ignored\n", *line_number);
164 if (target_page >= 0 && target_page != page)
183 *bounding_box =
TBOX();
186 const char *buffptr = boxfile_str;
193 const auto *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
194 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
198 if (*buffptr ==
'\0')
return false;
200 uch[uch_len++] = *buffptr++;
201 }
while (*buffptr !=
'\0' && *buffptr !=
' ' && *buffptr !=
'\t' &&
204 if (*buffptr !=
'\0') ++buffptr;
210 std::stringstream stream(buffptr);
211 stream.imbue(std::locale::classic());
216 stream >> *page_number;
217 if (x_max < x_min || y_max < y_min) {
218 tprintf(
"Bad box coordinates in boxfile string! %s\n", ubuf);
222 if (strcmp(uch, kMultiBlobLabelCode) == 0 &&
223 (buffptr = strchr(buffptr,
'#')) !=
nullptr) {
227 uch_len = strlen(uch);
231 while (used < uch_len) {
235 tprintf(
"Bad UTF-8 str %s starts with 0x%02x at col %d\n",
236 uch + used, uch[used], used + 1);
242 if (x_min > x_max)
Swap(&x_min, &x_max);
243 if (y_min > y_max)
Swap(&y_min, &y_max);
251 *box_str = unichar_str;