tesseract  4.00.00dev
ocrpara.cpp
Go to the documentation of this file.
1 // 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 
26 
31 
32 static STRING ParagraphJustificationToString(
33  tesseract::ParagraphJustification justification) {
34  switch (justification) {
35  case JUSTIFICATION_LEFT:
36  return "LEFT";
38  return "RIGHT";
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_);
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 
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_);
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 
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 
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 }
int tolerance() const
Definition: ocrpara.h:170
bool Comparable(const ParagraphModel &other) const
Definition: ocrpara.cpp:80
const char * string() const
Definition: strngs.cpp:198
bool NearlyEqual(T x, T y, T tolerance)
Definition: host.h:87
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
Definition: ocrpara.h:29
#define ELISTIZE(CLASSNAME)
Definition: elst.h:961
ParagraphJustification
Definition: publictypes.h:239
bool ValidBodyLine(int lmargin, int lindent, int rindent, int rmargin) const
Definition: ocrpara.cpp:63