Coverage Report

Created: 2024-02-28 06:46

/src/tesseract/src/ccstruct/ocrpara.cpp
Line
Count
Source (jump to first uncovered line)
1
/////////////////////////////////////////////////////////////////////
2
// File:        ocrpara.cpp
3
// Description: OCR Paragraph Output Type
4
// Author:      David Eger
5
//
6
// (C) Copyright 2010, 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 "ocrpara.h"
20
21
#include "host.h" // For NearlyEqual()
22
23
#include <cstdio>
24
25
namespace tesseract {
26
27
using tesseract::JUSTIFICATION_CENTER;
28
using tesseract::JUSTIFICATION_LEFT;
29
using tesseract::JUSTIFICATION_RIGHT;
30
using tesseract::JUSTIFICATION_UNKNOWN;
31
32
0
static const char *ParagraphJustificationToString(tesseract::ParagraphJustification justification) {
33
0
  switch (justification) {
34
0
    case JUSTIFICATION_LEFT:
35
0
      return "LEFT";
36
0
    case JUSTIFICATION_RIGHT:
37
0
      return "RIGHT";
38
0
    case JUSTIFICATION_CENTER:
39
0
      return "CENTER";
40
0
    default:
41
0
      return "UNKNOWN";
42
0
  }
43
0
}
44
45
109k
bool ParagraphModel::ValidFirstLine(int lmargin, int lindent, int rindent, int rmargin) const {
46
109k
  switch (justification_) {
47
89.1k
    case JUSTIFICATION_LEFT:
48
89.1k
      return NearlyEqual(lmargin + lindent, margin_ + first_indent_, tolerance_);
49
15.4k
    case JUSTIFICATION_RIGHT:
50
15.4k
      return NearlyEqual(rmargin + rindent, margin_ + first_indent_, tolerance_);
51
4.45k
    case JUSTIFICATION_CENTER:
52
4.45k
      return NearlyEqual(lindent, rindent, tolerance_ * 2);
53
0
    default:
54
      // shouldn't happen
55
0
      return false;
56
109k
  }
57
109k
}
58
59
69.0k
bool ParagraphModel::ValidBodyLine(int lmargin, int lindent, int rindent, int rmargin) const {
60
69.0k
  switch (justification_) {
61
58.4k
    case JUSTIFICATION_LEFT:
62
58.4k
      return NearlyEqual(lmargin + lindent, margin_ + body_indent_, tolerance_);
63
9.23k
    case JUSTIFICATION_RIGHT:
64
9.23k
      return NearlyEqual(rmargin + rindent, margin_ + body_indent_, tolerance_);
65
1.35k
    case JUSTIFICATION_CENTER:
66
1.35k
      return NearlyEqual(lindent, rindent, tolerance_ * 2);
67
0
    default:
68
      // shouldn't happen
69
0
      return false;
70
69.0k
  }
71
69.0k
}
72
73
563
bool ParagraphModel::Comparable(const ParagraphModel &other) const {
74
563
  if (justification_ != other.justification_) {
75
141
    return false;
76
141
  }
77
422
  if (justification_ == JUSTIFICATION_CENTER || justification_ == JUSTIFICATION_UNKNOWN) {
78
3
    return true;
79
3
  }
80
419
  int tolerance = (tolerance_ + other.tolerance_) / 4;
81
419
  return NearlyEqual(margin_ + first_indent_, other.margin_ + other.first_indent_, tolerance) &&
82
419
         NearlyEqual(margin_ + body_indent_, other.margin_ + other.body_indent_, tolerance);
83
422
}
84
85
0
std::string ParagraphModel::ToString() const {
86
0
  char buffer[200];
87
0
  const char *alignment = ParagraphJustificationToString(justification_);
88
0
  snprintf(buffer, sizeof(buffer), "margin: %d, first_indent: %d, body_indent: %d, alignment: %s",
89
0
           margin_, first_indent_, body_indent_, alignment);
90
0
  return std::string(buffer);
91
0
}
92
93
} // namespace tesseract