tesseract  4.1.3
altorenderer.cpp
Go to the documentation of this file.
1 // File: altorenderer.cpp
2 // Description: ALTO rendering interface
3 // Author: Jake Sebright
4 
5 // (C) Copyright 2018
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <memory>
17 #include <sstream> // for std::stringstream
18 #include "baseapi.h"
19 #ifdef _WIN32
20 # include "host.h" // windows.h for MultiByteToWideChar, ...
21 #endif
22 #include "renderer.h"
23 #include "strngs.h" // for STRING
24 
25 namespace tesseract {
26 
30 static void AddBoxToAlto(const ResultIterator* it, PageIteratorLevel level,
31  std::stringstream& alto_str) {
32  int left, top, right, bottom;
33  it->BoundingBox(level, &left, &top, &right, &bottom);
34 
35  int hpos = left;
36  int vpos = top;
37  int height = bottom - top;
38  int width = right - left;
39 
40  alto_str << " HPOS=\"" << hpos << "\"";
41  alto_str << " VPOS=\"" << vpos << "\"";
42  alto_str << " WIDTH=\"" << width << "\"";
43  alto_str << " HEIGHT=\"" << height << "\"";
44 
45  if (level == RIL_WORD) {
46  int wc = it->Confidence(RIL_WORD);
47  alto_str << " WC=\"0." << wc << "\"";
48  } else {
49  alto_str << ">";
50  }
51 }
52 
58  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
59  "<alto xmlns=\"http://www.loc.gov/standards/alto/ns-v3#\" "
60  "xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
61  "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
62  "xsi:schemaLocation=\"http://www.loc.gov/standards/alto/ns-v3# "
63  "http://www.loc.gov/alto/v3/alto-3-0.xsd\">\n"
64  "\t<Description>\n"
65  "\t\t<MeasurementUnit>pixel</MeasurementUnit>\n");
66 
67  auto name = title();
68  if (name[0] != '\0') {
70  "\t\t<sourceImageInformation>\n"
71  "\t\t\t<fileName>");
72  AppendString(name);
74  "</fileName>\n"
75  "\t\t</sourceImageInformation>\n");
76  }
77 
79  "\t\t<OCRProcessing ID=\"OCR_0\">\n"
80  "\t\t\t<ocrProcessingStep>\n"
81  "\t\t\t\t<processingSoftware>\n"
82  "\t\t\t\t\t<softwareName>tesseract ");
85  "</softwareName>\n"
86  "\t\t\t\t</processingSoftware>\n"
87  "\t\t\t</ocrProcessingStep>\n"
88  "\t\t</OCRProcessing>\n"
89  "\t</Description>\n"
90  "\t<Layout>\n");
91 
92  return true;
93 }
94 
99  const std::unique_ptr<const char[]> text(api->GetAltoText(imagenum()));
100  if (text == nullptr) return false;
101 
102  AppendString(text.get());
103 
104  return true;
105 }
106 
111  AppendString("\t</Layout>\n</alto>\n");
112 
113  return true;
114 }
115 
116 TessAltoRenderer::TessAltoRenderer(const char* outputbase)
117  : TessResultRenderer(outputbase, "xml") {}
118 
123 char* TessBaseAPI::GetAltoText(int page_number) {
124  return GetAltoText(nullptr, page_number);
125 }
126 
131 char* TessBaseAPI::GetAltoText(ETEXT_DESC* monitor, int page_number) {
132  if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(monitor) < 0))
133  return nullptr;
134 
135  int lcnt = 0, tcnt = 0, bcnt = 0, wcnt = 0;
136 
137  if (input_file_ == nullptr) SetInputName(nullptr);
138 
139 #ifdef _WIN32
140  // convert input name from ANSI encoding to utf-8
141  int str16_len =
142  MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1, nullptr, 0);
143  wchar_t* uni16_str = new WCHAR[str16_len];
144  str16_len = MultiByteToWideChar(CP_ACP, 0, input_file_->string(), -1,
145  uni16_str, str16_len);
146  int utf8_len = WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, nullptr,
147  0, nullptr, nullptr);
148  char* utf8_str = new char[utf8_len];
149  WideCharToMultiByte(CP_UTF8, 0, uni16_str, str16_len, utf8_str, utf8_len,
150  nullptr, nullptr);
151  *input_file_ = utf8_str;
152  delete[] uni16_str;
153  delete[] utf8_str;
154 #endif
155 
156  std::stringstream alto_str;
157  // Use "C" locale (needed for int values larger than 999).
158  alto_str.imbue(std::locale::classic());
159  alto_str
160  << "\t\t<Page WIDTH=\"" << rect_width_ << "\" HEIGHT=\""
161  << rect_height_
162  << "\" PHYSICAL_IMG_NR=\"" << page_number << "\""
163  << " ID=\"page_" << page_number << "\">\n"
164  << "\t\t\t<PrintSpace HPOS=\"0\" VPOS=\"0\""
165  << " WIDTH=\"" << rect_width_ << "\""
166  << " HEIGHT=\"" << rect_height_ << "\">\n";
167 
168  ResultIterator* res_it = GetIterator();
169  while (!res_it->Empty(RIL_BLOCK)) {
170  if (res_it->Empty(RIL_WORD)) {
171  res_it->Next(RIL_WORD);
172  continue;
173  }
174 
175  if (res_it->IsAtBeginningOf(RIL_BLOCK)) {
176  alto_str << "\t\t\t\t<ComposedBlock ID=\"cblock_" << bcnt << "\"";
177  AddBoxToAlto(res_it, RIL_BLOCK, alto_str);
178  alto_str << "\n";
179  }
180 
181  if (res_it->IsAtBeginningOf(RIL_PARA)) {
182  alto_str << "\t\t\t\t\t<TextBlock ID=\"block_" << tcnt << "\"";
183  AddBoxToAlto(res_it, RIL_PARA, alto_str);
184  alto_str << "\n";
185  }
186 
187  if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) {
188  alto_str << "\t\t\t\t\t\t<TextLine ID=\"line_" << lcnt << "\"";
189  AddBoxToAlto(res_it, RIL_TEXTLINE, alto_str);
190  alto_str << "\n";
191  }
192 
193  alto_str << "\t\t\t\t\t\t\t<String ID=\"string_" << wcnt << "\"";
194  AddBoxToAlto(res_it, RIL_WORD, alto_str);
195  alto_str << " CONTENT=\"";
196 
197  bool last_word_in_line = res_it->IsAtFinalElement(RIL_TEXTLINE, RIL_WORD);
198  bool last_word_in_tblock = res_it->IsAtFinalElement(RIL_PARA, RIL_WORD);
199  bool last_word_in_cblock = res_it->IsAtFinalElement(RIL_BLOCK, RIL_WORD);
200 
201 
202  int left, top, right, bottom;
203  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
204 
205  do {
206  const std::unique_ptr<const char[]> grapheme(
207  res_it->GetUTF8Text(RIL_SYMBOL));
208  if (grapheme && grapheme[0] != 0) {
209  alto_str << HOcrEscape(grapheme.get()).c_str();
210  }
211  res_it->Next(RIL_SYMBOL);
212  } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD));
213 
214  alto_str << "\"/>";
215 
216  wcnt++;
217 
218  if (last_word_in_line) {
219  alto_str << "\n\t\t\t\t\t\t</TextLine>\n";
220  lcnt++;
221  } else {
222  int hpos = right;
223  int vpos = top;
224  res_it->BoundingBox(RIL_WORD, &left, &top, &right, &bottom);
225  int width = left - hpos;
226  alto_str << "<SP WIDTH=\"" << width << "\" VPOS=\"" << vpos
227  << "\" HPOS=\"" << hpos << "\"/>\n";
228  }
229 
230  if (last_word_in_tblock) {
231  alto_str << "\t\t\t\t\t</TextBlock>\n";
232  tcnt++;
233  }
234 
235  if (last_word_in_cblock) {
236  alto_str << "\t\t\t\t</ComposedBlock>\n";
237  bcnt++;
238  }
239  }
240 
241  alto_str << "\t\t\t</PrintSpace>\n"
242  << "\t\t</Page>\n";
243  const std::string& text = alto_str.str();
244 
245  char* result = new char[text.length() + 1];
246  strcpy(result, text.c_str());
247  delete res_it;
248  return result;
249 }
250 
251 } // namespace tesseract
bool EndDocumentHandler() override
bool Empty(PageIteratorLevel level) const
const char * title() const
Definition: renderer.h:88
bool Next(PageIteratorLevel level) override
void AppendString(const char *s)
Definition: renderer.cpp:102
static const char * Version()
Definition: baseapi.cpp:233
bool BoundingBox(PageIteratorLevel level, int *left, int *top, int *right, int *bottom) const
Tesseract * tesseract_
The underlying data object.
Definition: baseapi.h:888
const char * string() const
Definition: strngs.cpp:194
char * GetAltoText(ETEXT_DESC *monitor, int page_number)
ResultIterator * GetIterator()
Definition: baseapi.cpp:1323
float Confidence(PageIteratorLevel level) const
virtual char * GetUTF8Text(PageIteratorLevel level) const
STRING * input_file_
Name used by training code.
Definition: baseapi.h:896
bool AddImageHandler(TessBaseAPI *api) override
bool IsAtBeginningOf(PageIteratorLevel level) const override
PAGE_RES * page_res_
The page-level data.
Definition: baseapi.h:895
bool BeginDocumentHandler() override
int Recognize(ETEXT_DESC *monitor)
Definition: baseapi.cpp:830
TessAltoRenderer(const char *outputbase)
void SetInputName(const char *name)
Definition: baseapi.cpp:271
bool IsAtFinalElement(PageIteratorLevel level, PageIteratorLevel element) const override
STRING HOcrEscape(const char *text)
Definition: baseapi.cpp:2309