Tesseract  3.02
tesseract-ocr/ccstruct/ocrpara.cpp
Go to the documentation of this file.
00001 
00002 // File:        ocrpara.h
00003 // Description: OCR Paragraph Output Type
00004 // Author:      David Eger
00005 // Created:     2010-11-15
00006 //
00007 // (C) Copyright 2010, Google Inc.
00008 // Licensed under the Apache License, Version 2.0 (the "License");
00009 // you may not use this file except in compliance with the License.
00010 // You may obtain a copy of the License at
00011 // http://www.apache.org/licenses/LICENSE-2.0
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 //
00019 
00020 #include <stdio.h>
00021 
00022 #include "ocrpara.h"
00023 #include "host.h"  // For NearlyEqual()
00024 
00025 ELISTIZE(PARA)
00026 
00027 using tesseract::JUSTIFICATION_LEFT;
00028 using tesseract::JUSTIFICATION_RIGHT;
00029 using tesseract::JUSTIFICATION_CENTER;
00030 using tesseract::JUSTIFICATION_UNKNOWN;
00031 
00032 static STRING ParagraphJustificationToString(
00033     tesseract::ParagraphJustification justification) {
00034   switch (justification) {
00035     case JUSTIFICATION_LEFT:
00036       return "LEFT";
00037     case JUSTIFICATION_RIGHT:
00038       return "RIGHT";
00039     case JUSTIFICATION_CENTER:
00040       return "CENTER";
00041     default:
00042       return "UNKNOWN";
00043   }
00044 }
00045 
00046 bool ParagraphModel::ValidFirstLine(int lmargin, int lindent,
00047                                     int rindent, int rmargin) const {
00048   switch (justification_) {
00049     case JUSTIFICATION_LEFT:
00050       return NearlyEqual(lmargin + lindent, margin_ + first_indent_,
00051                          tolerance_);
00052     case JUSTIFICATION_RIGHT:
00053       return NearlyEqual(rmargin + rindent, margin_ + first_indent_,
00054                          tolerance_);
00055     case JUSTIFICATION_CENTER:
00056       return NearlyEqual(lindent, rindent, tolerance_ * 2);
00057     default:
00058       // shouldn't happen
00059       return false;
00060   }
00061 }
00062 
00063 bool ParagraphModel::ValidBodyLine(int lmargin, int lindent,
00064                                    int rindent, int rmargin) const {
00065   switch (justification_) {
00066     case JUSTIFICATION_LEFT:
00067       return NearlyEqual(lmargin + lindent, margin_ + body_indent_,
00068                          tolerance_);
00069     case JUSTIFICATION_RIGHT:
00070       return NearlyEqual(rmargin + rindent, margin_ + body_indent_,
00071                          tolerance_);
00072     case JUSTIFICATION_CENTER:
00073       return NearlyEqual(lindent, rindent, tolerance_ * 2);
00074     default:
00075       // shouldn't happen
00076       return false;
00077   }
00078 }
00079 
00080 bool ParagraphModel::Comparable(const ParagraphModel &other) const {
00081   if (justification_ != other.justification_)
00082     return false;
00083   if (justification_ == JUSTIFICATION_CENTER ||
00084       justification_ == JUSTIFICATION_UNKNOWN)
00085     return true;
00086   int tolerance = (tolerance_ + other.tolerance_) / 4;
00087   return NearlyEqual(margin_ + first_indent_,
00088                      other.margin_ + other.first_indent_, tolerance) &&
00089          NearlyEqual(margin_ + body_indent_,
00090                      other.margin_ + other.body_indent_, tolerance);
00091 }
00092 
00093 STRING ParagraphModel::ToString() const {
00094   char buffer[200];
00095   const STRING &alignment = ParagraphJustificationToString(justification_);
00096   snprintf(buffer, sizeof(buffer),
00097            "margin: %d, first_indent: %d, body_indent: %d, alignment: %s",
00098            margin_, first_indent_, body_indent_, alignment.string());
00099   return STRING(buffer);
00100 }