1 /**********************************************************************
2 * File: boxread.cpp
3 * Description: Read data from a box file.
4 * Author: Ray Smith
5 * Created: Fri Aug 24 17:47:23 PDT 2007
6 *
7 * (C) Copyright 2007, Google Inc.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 *
18 **********************************************************************/
19
20 #include "mfcpch.h"
21 #include <string.h>
22 #include "boxread.h"
23 #include "unichar.h"
24 #include "tprintf.h"
25
26 // Box files are used ONLY DURING TRAINING, but by both processes of
27 // creating tr files with tesseract, and unicharset_extractor.
28 // read_next_box factors out the code to interpret a line of a box
29 // file so that applybox and unicharset_extractor interpret the same way.
30 // This function returns the next valid box file utf8 string and coords
31 // and returns true, or false on eof (and closes the file).
32 // It ignores the uft8 file signature, checks for valid utf-8 and allows
33 // space or tab between fields.
34 // utf8_str must be at least kBoxReadBufSize in length.
35 // If there are page numbers in the file, it reads them all.
read_next_box(FILE * box_file,char * utf8_str,int * x_min,int * y_min,int * x_max,int * y_max)36 bool read_next_box(FILE* box_file, char* utf8_str,
37 int* x_min, int* y_min, int* x_max, int* y_max) {
38 return read_next_box(-1, box_file, utf8_str,
39 x_min, y_min, x_max, y_max);
40 }
41
42 // As read_next_box above, but get a specific page number. (0-based)
43 // Use -1 to read any page number. Files without page number all
44 // read as if they are page 0.
read_next_box(int target_page,FILE * box_file,char * utf8_str,int * x_min,int * y_min,int * x_max,int * y_max)45 bool read_next_box(int target_page, FILE* box_file, char* utf8_str,
46 int* x_min, int* y_min, int* x_max, int* y_max) {
47 static int line = 0;
48 int count = 0;
49 int page = 0;
50 char buff[kBoxReadBufSize]; //boxfile read buffer
51 char uch[kBoxReadBufSize];
52 char *buffptr = buff;
53
54 while (fgets(buff, sizeof(buff) - 1, box_file)) {
55 line++;
56
57 buffptr = buff;
58 const unsigned char *ubuf = reinterpret_cast<const unsigned char*>(buffptr);
59 if (ubuf[0] == 0xef && ubuf[1] == 0xbb && ubuf[2] == 0xbf)
60 buffptr += 3; // Skip unicode file designation.
61 /* Check for blank lines in box file */
62 while (*buffptr == ' ' || *buffptr == '\t')
63 buffptr++;
64 if (*buffptr != '\0') {
65 count = sscanf(buffptr, "%s %d %d %d %d %d",
66 uch, x_min, y_min, x_max, y_max, &page);
67 if (count != 6) {
68 page = 0;
69 count = sscanf(buffptr, "%s %d %d %d %d",
70 uch, x_min, y_min, x_max, y_max);
71 }
72 if (target_page >= 0 && target_page != page)
73 continue; // Not on the appropriate page.
74 if (count == 5) {
75 // Validate UTF8 by making unichars with it.
76 int used = 0;
77 int uch_len = strlen(uch);
78 while (used < uch_len) {
79 UNICHAR ch(uch + used, uch_len - used);
80 int new_used = ch.utf8_len();
81 if (new_used == 0) {
82 tprintf("Bad utf-8 char starting with 0x%x at line %d, col %d, \n",
83 uch[used], used + 1, line);
84 count = 0;
85 break;
86 }
87 used += new_used;
88 }
89 if (uch_len > UNICHAR_LEN) {
90 tprintf("utf-8 string too long at line %d\n", line);
91 count = 0;
92 }
93 }
94 if (count < 5) {
95 tprintf("Box file format error on line %i ignored\n", line);
96 } else {
97 strcpy(utf8_str, uch);
98 return true; //read a box ok
99 }
100 }
101 }
102 fclose(box_file);
103 line = 0;
104 return false; //EOF
105 }
106