Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ocrpara.cpp
Go to the documentation of this file.
1
2 // File: ocrpara.h
3 // Description: OCR Paragraph Output Type
4 // Author: David Eger
5 // Created: 2010-11-15
6 //
7 // (C) Copyright 2010, 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 //
19
20 #include <stdio.h>
21
22 #include "ocrpara.h"
23 #include "host.h" // For NearlyEqual()
24
25 ELISTIZE(PARA)
26
27 using tesseract::JUSTIFICATION_LEFT;
28 using tesseract::JUSTIFICATION_RIGHT;
29 using tesseract::JUSTIFICATION_CENTER;
30 using tesseract::JUSTIFICATION_UNKNOWN;
31
32 static STRING ParagraphJustificationToString(
33 tesseract::ParagraphJustification justification) {
34 switch (justification) {
35 case JUSTIFICATION_LEFT:
36 return "LEFT";
37 case JUSTIFICATION_RIGHT:
38 return "RIGHT";
39 case JUSTIFICATION_CENTER:
40 return "CENTER";
41 default:
42 return "UNKNOWN";
43 }
44 }
45
46 bool ParagraphModel::ValidFirstLine(int lmargin, int lindent,
47 int rindent, int rmargin) const {
48 switch (justification_) {
49 case JUSTIFICATION_LEFT:
50 return NearlyEqual(lmargin + lindent, margin_ + first_indent_,
51 tolerance_);
52 case JUSTIFICATION_RIGHT:
53 return NearlyEqual(rmargin + rindent, margin_ + first_indent_,
54 tolerance_);
55 case JUSTIFICATION_CENTER:
56 return NearlyEqual(lindent, rindent, tolerance_ * 2);
57 default:
58 // shouldn't happen
59 return false;
60 }
61 }
62
63 bool ParagraphModel::ValidBodyLine(int lmargin, int lindent,
64 int rindent, int rmargin) const {
65 switch (justification_) {
66 case JUSTIFICATION_LEFT:
67 return NearlyEqual(lmargin + lindent, margin_ + body_indent_,
68 tolerance_);
69 case JUSTIFICATION_RIGHT:
70 return NearlyEqual(rmargin + rindent, margin_ + body_indent_,
71 tolerance_);
72 case JUSTIFICATION_CENTER:
73 return NearlyEqual(lindent, rindent, tolerance_ * 2);
74 default:
75 // shouldn't happen
76 return false;
77 }
78 }
79
80 bool ParagraphModel::Comparable(const ParagraphModel &other) const {
81 if (justification_ != other.justification_)
82 return false;
83 if (justification_ == JUSTIFICATION_CENTER ||
84 justification_ == JUSTIFICATION_UNKNOWN)
85 return true;
86 int tolerance = (tolerance_ + other.tolerance_) / 4;
87 return NearlyEqual(margin_ + first_indent_,
88 other.margin_ + other.first_indent_, tolerance) &&
89 NearlyEqual(margin_ + body_indent_,
90 other.margin_ + other.body_indent_, tolerance);
91 }
92
93 STRING ParagraphModel::ToString() const {
94 char buffer[200];
95 const STRING &alignment = ParagraphJustificationToString(justification_);
96 snprintf(buffer, sizeof(buffer),
97 "margin: %d, first_indent: %d, body_indent: %d, alignment: %s",
98 margin_, first_indent_, body_indent_, alignment.string());
99 return STRING(buffer);
100 }