|
| 1 | +/********************************************************************** |
| 2 | + * File: wordstrboxrenderer.cpp |
| 3 | + * Description: Renderer for creating box file with WordStr strings. |
| 4 | + * based on the tsv renderer. |
| 5 | + * |
| 6 | + * (C) Copyright 2006, Google Inc. |
| 7 | + ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + ** you may not use this file except in compliance with the License. |
| 9 | + ** You may obtain a copy of the License at |
| 10 | + ** http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + ** Unless required by applicable law or agreed to in writing, software |
| 12 | + ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + ** See the License for the specific language governing permissions and |
| 15 | + ** limitations under the License. |
| 16 | + * |
| 17 | + **********************************************************************/ |
| 18 | + |
| 19 | +#include "baseapi.h" // for TessBaseAPI |
| 20 | +#include "renderer.h" |
| 21 | +#include "tesseractclass.h" // for Tesseract |
| 22 | + |
| 23 | +namespace tesseract { |
| 24 | + |
| 25 | +/** |
| 26 | + * Create a UTF8 box file with WordStr strings from the internal data structures. |
| 27 | + * page_number is a 0-base page index that will appear in the box file. |
| 28 | + * Returned string must be freed with the delete [] operator. |
| 29 | + */ |
| 30 | + |
| 31 | +char* TessBaseAPI::GetWordStrBoxText(int page_number) { |
| 32 | + if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0)) |
| 33 | + return nullptr; |
| 34 | + |
| 35 | + STRING wordstr_box_str(""); |
| 36 | + int left, top, right, bottom; |
| 37 | + int page_num = page_number; |
| 38 | + bool first_line = true; |
| 39 | + |
| 40 | + LTRResultIterator* res_it = GetLTRIterator(); |
| 41 | + while (!res_it->Empty(RIL_BLOCK)) { |
| 42 | + if (res_it->Empty(RIL_WORD)) { |
| 43 | + res_it->Next(RIL_WORD); |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + if (res_it->IsAtBeginningOf(RIL_TEXTLINE)) { |
| 48 | + if (!first_line) { |
| 49 | + wordstr_box_str.add_str_int("\n\t ", right + 1); |
| 50 | + wordstr_box_str.add_str_int(" ", image_height_ - bottom); |
| 51 | + wordstr_box_str.add_str_int(" ", right + 5); |
| 52 | + wordstr_box_str.add_str_int(" ", image_height_ - top); |
| 53 | + wordstr_box_str.add_str_int(" ", page_num); // row for tab for EOL |
| 54 | + wordstr_box_str += "\n"; |
| 55 | + } else { |
| 56 | + first_line = false; |
| 57 | + } |
| 58 | + // Use bounding box for whole line for WordStr |
| 59 | + res_it->BoundingBox(RIL_TEXTLINE, &left, &top, &right, &bottom); |
| 60 | + wordstr_box_str.add_str_int("WordStr ", left); |
| 61 | + wordstr_box_str.add_str_int(" ", image_height_ - bottom); |
| 62 | + wordstr_box_str.add_str_int(" ", right); |
| 63 | + wordstr_box_str.add_str_int(" ", image_height_ - top); |
| 64 | + wordstr_box_str.add_str_int(" ", page_num); // word |
| 65 | + wordstr_box_str += " #"; |
| 66 | + } |
| 67 | + do { wordstr_box_str += |
| 68 | + std::unique_ptr<const char[]>(res_it->GetUTF8Text(RIL_WORD)).get(); |
| 69 | + wordstr_box_str += " "; |
| 70 | + res_it->Next(RIL_WORD); |
| 71 | + } while (!res_it->Empty(RIL_BLOCK) && !res_it->IsAtBeginningOf(RIL_WORD)); |
| 72 | + } |
| 73 | + wordstr_box_str.add_str_int("\n\t ", right + 1); |
| 74 | + wordstr_box_str.add_str_int(" ", image_height_ - bottom); |
| 75 | + wordstr_box_str.add_str_int(" ", right + 5); |
| 76 | + wordstr_box_str.add_str_int(" ", image_height_ - top); |
| 77 | + wordstr_box_str.add_str_int(" ", page_num); // row for tab for EOL |
| 78 | + wordstr_box_str += "\n"; |
| 79 | + char* ret = new char[wordstr_box_str.length() + 1]; |
| 80 | + strcpy(ret, wordstr_box_str.string()); |
| 81 | + delete res_it; |
| 82 | + return ret; |
| 83 | +} |
| 84 | + |
| 85 | +/********************************************************************** |
| 86 | + * WordStrBox Renderer interface implementation |
| 87 | + **********************************************************************/ |
| 88 | + TessWordStrBoxRenderer::TessWordStrBoxRenderer(const char *outputbase) |
| 89 | + : TessResultRenderer(outputbase, "box") { |
| 90 | +} |
| 91 | + |
| 92 | +bool TessWordStrBoxRenderer::AddImageHandler(TessBaseAPI* api) { |
| 93 | + const std::unique_ptr<const char[]> wordstrbox(api->GetWordStrBoxText(imagenum())); |
| 94 | + if (wordstrbox == nullptr) return false; |
| 95 | + |
| 96 | + AppendString(wordstrbox.get()); |
| 97 | + |
| 98 | + return true; |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace tesseract. |
0 commit comments