tesseract  4.0.0-1-g2a2b
validate_indic.cpp
Go to the documentation of this file.
1 #include "validate_indic.h"
2 #include "errcode.h"
3 #include "tprintf.h"
4 
5 namespace tesseract {
6 
7 // Returns whether codes matches the pattern for an Indic Grapheme.
8 // The ISCII standard http://varamozhi.sourceforge.net/iscii91.pdf
9 // has a BNF for valid syllables (Graphemes) which is modified slightly
10 // for Unicode. Notably U+200C and U+200D are used before/after the
11 // virama/virama to express explicit or soft viramas.
12 // Also the unicode v.9 Malayalam entry states that CZHC can be used in several
13 // Indic languages to request traditional ligatures, and CzHC is Malayalam-
14 // specific for requesting open conjuncts.
15 //
16 // + vowel Grapheme: V[D](v)*
17 // + consonant Grapheme: (C[N](H|HZ|Hz|ZH)?)*C[N](H|Hz)?[M[P]][D](v)*
19  switch (codes_[codes_used_].first) {
21  return ConsumeConsonantHeadIfValid() && ConsumeConsonantTailIfValid();
22  case CharClass::kVowel:
24  return ConsumeVowelIfValid();
27  // Apart from within an aksara, joiners are silently dropped.
28  if (report_errors_)
29  tprintf("Dropping isolated joiner: 0x%x\n", codes_[codes_used_].second);
30  ++codes_used_;
31  return true;
32  case CharClass::kOther:
33  UseMultiCode(1);
34  return true;
35  default:
36  if (report_errors_) {
37  tprintf("Invalid start of grapheme sequence:%c=0x%x\n",
38  codes_[codes_used_].first, codes_[codes_used_].second);
39  }
40  return false;
41  }
42 }
43 
45  if (IsVedicAccent(ch)) return CharClass::kVedicMark;
48  // Offset from the start of the relevant unicode code block aka code page.
49  int base = static_cast<char32>(script_);
50  int off = ch - base;
51  // Anything in another code block is other.
52  if (off < 0 || off >= kIndicCodePageSize) return CharClass::kOther;
53  // Exception for Tamil. The aytham character is considered a letter.
54  if (script_ == ViramaScript::kTamil && off == 0x03) return CharClass::kVowel;
55  if (off < 0x4) return CharClass::kVowelModifier;
57  // Sinhala is an exception.
58  if (off <= 0x19) return CharClass::kVowel;
59  if (off <= 0x49) return CharClass::kConsonant;
60  if (off == 0x4a) return CharClass::kVirama;
61  if (off <= 0x5f) return CharClass::kMatra;
62  } else {
63  if (off <= 0x14 || off == 0x50) return CharClass::kVowel;
64  if (off <= 0x3b || (0x58 <= off && off <= 0x5f))
65  return CharClass::kConsonant;
66  // Sinhala doesn't have Nukta or Avagraha.
67  if (off == 0x3c) return CharClass::kNukta;
68  if (off == 0x3d) return CharClass::kVowel; // avagraha
69  if (off <= 0x4c || (0x51 <= off && off <= 0x54)) return CharClass::kMatra;
70  if (0x55 <= off && off <= 0x57) return CharClass::kMatraPiece;
71  if (off == 0x4d) return CharClass::kVirama;
72  }
73  if (off == 0x60 || off == 0x61) return CharClass::kVowel;
74  if (off == 0x62 || off == 0x63) return CharClass::kMatra;
75  // Danda and digits up to 6f are OK as other.
76  // 70-7f are script-specific.
77  if (script_ == ViramaScript::kBengali && (off == 0x70 || off == 0x71))
78  return CharClass::kConsonant;
79  if (script_ == ViramaScript::kGurmukhi && (off == 0x72 || off == 0x73))
80  return CharClass::kConsonant;
81  if (script_ == ViramaScript::kSinhala && off == 0x70)
82  return CharClass::kConsonant;
83  if (script_ == ViramaScript::kDevanagari && off == 0x70)
84  return CharClass::kOther;
85  if (0x70 <= off && off <= 0x73) return CharClass::kVowelModifier;
86  // Non Indic, Digits, Measures, danda, etc.
87  return CharClass::kOther;
88 }
89 
90 // Helper consumes/copies a virama and any associated post-virama joiners.
91 // A linking virama (with either type of pre-virama joiner, post-virama ZWJ, or
92 // no joiner at all) must be followed by a consonant.
93 // A non-linking (explicit) virama is indicated by a ZWNJ after it, or a non
94 // consonant, space, or character from a different script. We clean up the
95 // representation to make it consistent by adding a ZWNJ if missing from a
96 // non-linking virama. Returns false with an invalid sequence.
97 bool ValidateIndic::ConsumeViramaIfValid(IndicPair joiner, bool post_matra) {
98  int num_codes = codes_.size();
99  if (joiner.first == CharClass::kOther) {
101  if (codes_used_ < num_codes &&
102  codes_[codes_used_].second == kZeroWidthJoiner) {
103  // Post-matra viramas must be explicit, so no joiners allowed here.
104  if (post_matra) {
105  if (report_errors_) tprintf("ZWJ after a post-matra virama!!\n");
106  return false;
107  }
108  if (codes_used_ + 1 < num_codes &&
109  codes_[codes_used_ - 2].second != kRayana &&
110  (codes_[codes_used_ + 1].second == kZeroWidthNonJoiner ||
111  codes_[codes_used_ + 1].second == kYayana ||
112  codes_[codes_used_ + 1].second == kRayana)) {
113  // This combination will be picked up later.
115  } else {
116  // Half-form with optional Nukta.
117  int len = output_.size() + 1 - output_used_;
118  if (UseMultiCode(len)) return true;
119  }
120  if (codes_used_ < num_codes &&
122  if (output_used_ == output_.size() ||
123  output_[output_used_] != kRayana) {
124  if (report_errors_) {
125  tprintf("Virama ZWJ ZWNJ in non-Sinhala: base=0x%x!\n",
126  static_cast<int>(script_));
127  }
128  return false;
129  }
130  // Special Sinhala case of Stand-alone Repaya. ['RA' H Z z]
131  if (UseMultiCode(4)) return true;
132  }
133  } else if (codes_used_ == num_codes ||
135  post_matra) {
136  if (codes_used_ == num_codes ||
138  // It is valid to have an unterminated virama at the end of a word, but
139  // for consistency, we will always add ZWNJ if not present.
140  output_.push_back(kZeroWidthNonJoiner);
141  } else {
143  }
144  // Explicit virama [H z]
145  MultiCodePart(2);
146  }
147  } else {
148  // Pre-virama joiner [{Z|z} H] requests specific conjunct.
149  if (UseMultiCode(2)) {
150  if (report_errors_)
151  tprintf("Invalid pre-virama joiner with no 2nd consonant!!\n");
152  return false;
153  }
154  if (codes_[codes_used_].second == kZeroWidthJoiner ||
156  if (report_errors_) {
157  tprintf("JHJ!!: 0x%x 0x%x 0x%x\n", joiner.second, output_.back(),
158  codes_[codes_used_].second);
159  }
160  return false;
161  }
162  }
163  // It is good so far as it goes.
164  return true;
165 }
166 
167 // Helper consumes/copies a series of consonants separated by viramas while
168 // valid, but not any vowel or other modifiers.
169 bool ValidateIndic::ConsumeConsonantHeadIfValid() {
170  const int num_codes = codes_.size();
171  // Consonant aksara
172  do {
174  // Special Sinhala case of [H Z Yayana/Rayana].
175  int index = output_.size() - 3;
176  if (output_used_ <= index &&
177  (output_.back() == kYayana || output_.back() == kRayana) &&
178  IsVirama(output_[index]) && output_[index + 1] == kZeroWidthJoiner) {
179  MultiCodePart(3);
180  }
181  bool have_nukta = false;
182  if (codes_used_ < num_codes &&
184  have_nukta = true;
186  }
187  // Test for subscript conjunct.
188  index = output_.size() - 2 - have_nukta;
189  if (output_used_ <= index && IsSubscriptScript() &&
190  IsVirama(output_[index])) {
191  // Output previous virama, consonant + optional nukta.
192  MultiCodePart(2 + have_nukta);
193  }
194  IndicPair joiner(CharClass::kOther, 0);
195  if (codes_used_ < num_codes &&
196  (codes_[codes_used_].second == kZeroWidthJoiner ||
197  (codes_[codes_used_].second == kZeroWidthNonJoiner &&
199  joiner = codes_[codes_used_];
200  if (++codes_used_ == num_codes) {
201  if (report_errors_) {
202  tprintf("Skipping ending joiner: 0x%x 0x%x\n", output_.back(),
203  joiner.second);
204  }
205  return true;
206  }
207  if (codes_[codes_used_].first == CharClass::kVirama) {
208  output_.push_back(joiner.second);
209  } else {
210  if (report_errors_) {
211  tprintf("Skipping unnecessary joiner: 0x%x 0x%x 0x%x\n",
212  output_.back(), joiner.second, codes_[codes_used_].second);
213  }
214  joiner = std::make_pair(CharClass::kOther, 0);
215  }
216  }
217  if (codes_used_ < num_codes &&
219  if (!ConsumeViramaIfValid(joiner, false)) return false;
220  } else {
221  break; // No virama, so the run of consonants is over.
222  }
223  } while (codes_used_ < num_codes &&
225  if (output_used_ < output_.size()) MultiCodePart(1);
226  return true;
227 }
228 
229 // Helper consumes/copies a tail part of a consonant, comprising optional
230 // matra/piece, vowel modifier, vedic mark, terminating virama.
231 bool ValidateIndic::ConsumeConsonantTailIfValid() {
232  if (codes_used_ == codes_.size()) return true;
233  // No virama: Finish the grapheme.
234  // Are multiple matras allowed?
235  if (codes_[codes_used_].first == CharClass::kMatra) {
236  if (UseMultiCode(1)) return true;
237  if (codes_[codes_used_].first == CharClass::kMatraPiece) {
238  if (UseMultiCode(1)) return true;
239  }
240  }
241  while (codes_[codes_used_].first == CharClass::kVowelModifier) {
242  if (UseMultiCode(1)) return true;
243  // Only Malayalam allows only repeated 0xd02.
244  if (script_ != ViramaScript::kMalayalam || output_.back() != 0xd02) break;
245  }
246  while (codes_[codes_used_].first == CharClass::kVedicMark) {
247  if (UseMultiCode(1)) return true;
248  }
249  if (codes_[codes_used_].first == CharClass::kVirama) {
250  if (!ConsumeViramaIfValid(IndicPair(CharClass::kOther, 0), true)) {
251  return false;
252  }
253  }
254  // What we have consumed so far is a valid consonant cluster.
255  if (output_used_ < output_.size()) MultiCodePart(1);
256 
257  return true;
258 }
259 
260 // Helper consumes/copies a vowel and optional modifiers.
261 bool ValidateIndic::ConsumeVowelIfValid() {
262  if (UseMultiCode(1)) return true;
263  while (codes_[codes_used_].first == CharClass::kVowelModifier) {
264  if (UseMultiCode(1)) return true;
265  // Only Malayalam allows repeated modifiers?
266  if (script_ != ViramaScript::kMalayalam) break;
267  }
268  while (codes_[codes_used_].first == CharClass::kVedicMark) {
269  if (UseMultiCode(1)) return true;
270  }
271  // What we have consumed so far is a valid vowel cluster.
272  return true;
273 }
274 
275 } // namespace tesseract
std::vector< IndicPair > codes_
Definition: validator.h:232
static bool IsVedicAccent(char32 unicode)
Definition: validator.cpp:191
static const char32 kZeroWidthNonJoiner
Definition: validator.h:96
bool IsSubscriptScript() const
Definition: validator.cpp:198
signed int char32
Definition: unichar.h:52
void MultiCodePart(int length)
Definition: validator.h:182
static const int kIndicCodePageSize
Definition: validator.h:214
ViramaScript script_
Definition: validator.h:230
bool ConsumeGraphemeIfValid() override
static bool IsVirama(char32 unicode)
Definition: validator.cpp:180
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
bool UseMultiCode(int length)
Definition: validator.h:196
static const char32 kZeroWidthJoiner
Definition: validator.h:97
Validator::CharClass UnicodeToCharClass(char32 ch) const override
std::vector< char32 > output_
Definition: validator.h:236
std::pair< CharClass, char32 > IndicPair
Definition: validator.h:134
#define ASSERT_HOST(x)
Definition: errcode.h:84