tesseract 4.1.1
Loading...
Searching...
No Matches
ocrpara.cpp
Go to the documentation of this file.
1
2// File: ocrpara.cpp
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 <cstdio>
21
22#include "ocrpara.h"
23#include "host.h" // For NearlyEqual()
24
26
31
32static STRING ParagraphJustificationToString(
34 switch (justification) {
36 return "LEFT";
38 return "RIGHT";
40 return "CENTER";
41 default:
42 return "UNKNOWN";
43 }
44}
45
46bool ParagraphModel::ValidFirstLine(int lmargin, int lindent,
47 int rindent, int rmargin) const {
48 switch (justification_) {
50 return NearlyEqual(lmargin + lindent, margin_ + first_indent_,
51 tolerance_);
53 return NearlyEqual(rmargin + rindent, margin_ + first_indent_,
54 tolerance_);
56 return NearlyEqual(lindent, rindent, tolerance_ * 2);
57 default:
58 // shouldn't happen
59 return false;
60 }
61}
62
63bool ParagraphModel::ValidBodyLine(int lmargin, int lindent,
64 int rindent, int rmargin) const {
65 switch (justification_) {
67 return NearlyEqual(lmargin + lindent, margin_ + body_indent_,
68 tolerance_);
70 return NearlyEqual(rmargin + rindent, margin_ + body_indent_,
71 tolerance_);
73 return NearlyEqual(lindent, rindent, tolerance_ * 2);
74 default:
75 // shouldn't happen
76 return false;
77 }
78}
79
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
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}
@ JUSTIFICATION_LEFT
Definition: capi.h:154
@ JUSTIFICATION_RIGHT
Definition: capi.h:156
@ JUSTIFICATION_CENTER
Definition: capi.h:155
@ JUSTIFICATION_UNKNOWN
Definition: capi.h:153
#define ELISTIZE(CLASSNAME)
Definition: elst.h:931
bool NearlyEqual(T x, T y, T tolerance)
Definition: host.h:37
ParagraphJustification
Definition: publictypes.h:251
@ JUSTIFICATION_LEFT
Definition: publictypes.h:253
@ JUSTIFICATION_UNKNOWN
Definition: publictypes.h:252
@ JUSTIFICATION_RIGHT
Definition: publictypes.h:255
@ JUSTIFICATION_CENTER
Definition: publictypes.h:254
Definition: ocrpara.h:29
bool Comparable(const ParagraphModel &other) const
Definition: ocrpara.cpp:80
bool ValidBodyLine(int lmargin, int lindent, int rindent, int rmargin) const
Definition: ocrpara.cpp:63
int tolerance() const
Definition: ocrpara.h:170
STRING ToString() const
Definition: ocrpara.cpp:93
bool ValidFirstLine(int lmargin, int lindent, int rindent, int rmargin) const
Definition: ocrpara.cpp:46
Definition: strngs.h:45
const char * string() const
Definition: strngs.cpp:194