1/* -*- C++ -*-
2 SPDX-FileCopyrightText: 1998 Netscape Communications Corporation <developer@mozilla.org>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#ifndef nsHebrewProber_h__
8#define nsHebrewProber_h__
9
10#include "nsCharSetProber.h"
11
12#include <memory>
13
14namespace kencodingprober
15{
16// This prober doesn't actually recognize a language or a charset.
17// It is a helper prober for the use of the Hebrew model probers
18class KCODECS_NO_EXPORT nsHebrewProber : public nsCharSetProber
19{
20public:
21 nsHebrewProber();
22
23 ~nsHebrewProber() override = default;
24
25 nsProbingState HandleData(const char *aBuf, unsigned int aLen) override;
26 const char *GetCharSetName() override;
27 void Reset(void) override;
28
29 nsProbingState GetState(void) override;
30
31 float GetConfidence(void) override;
32
33#ifdef DEBUG_PROBE
34 void DumpStatus() override;
35#endif
36
37protected:
38 int mFinalCharLogicalScore = 0;
39 int mFinalCharVisualScore = 0;
40
41 // The two last characters seen in the previous buffer.
42 char mPrev = ' ';
43 char mBeforePrev = ' ';
44
45 std::unique_ptr<nsCharSetProber> mLogicalProb;
46 std::unique_ptr<nsCharSetProber> mVisualProb;
47};
48}
49
50/**
51 * ** General ideas of the Hebrew charset recognition **
52 *
53 * Four main charsets exist in Hebrew:
54 * "ISO-8859-8" - Visual Hebrew
55 * "windows-1255" - Logical Hebrew
56 * "ISO-8859-8-I" - Logical Hebrew
57 * "x-mac-hebrew" - ?? Logical Hebrew ??
58 *
59 * Both "ISO" charsets use a completely identical set of code points, whereas
60 * "windows-1255" and "x-mac-hebrew" are two different proper supersets of
61 * these code points. windows-1255 defines additional characters in the range
62 * 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific
63 * diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6.
64 * x-mac-hebrew defines similar additional code points but with a different
65 * mapping.
66 *
67 * As far as an average Hebrew text with no diacritics is concerned, all four
68 * charsets are identical with respect to code points. Meaning that for the
69 * main Hebrew alphabet, all four map the same values to all 27 Hebrew letters
70 * (including final letters).
71 *
72 * The dominant difference between these charsets is their directionality.
73 * "Visual" directionality means that the text is ordered as if the renderer is
74 * not aware of a BIDI rendering algorithm. The renderer sees the text and
75 * draws it from left to right. The text itself when ordered naturally is read
76 * backwards. A buffer of Visual Hebrew generally looks like so:
77 * "[last word of first line spelled backwards] [whole line ordered backwards
78 * and spelled backwards] [first word of first line spelled backwards]
79 * [end of line] [last word of second line] ... etc' "
80 * adding punctuation marks, numbers and English text to visual text is
81 * naturally also "visual" and from left to right.
82 *
83 * "Logical" directionality means the text is ordered "naturally" according to
84 * the order it is read. It is the responsibility of the renderer to display
85 * the text from right to left. A BIDI algorithm is used to place general
86 * punctuation marks, numbers and English text in the text.
87 *
88 * Texts in x-mac-hebrew are almost impossible to find on the Internet. From
89 * what little evidence I could find, it seems that its general directionality
90 * is Logical.
91 *
92 * To sum up all of the above, the Hebrew probing mechanism knows about two
93 * charsets:
94 * Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are
95 * backwards while line order is natural. For charset recognition purposes
96 * the line order is unimportant (In fact, for this implementation, even
97 * word order is unimportant).
98 * Logical Hebrew - "windows-1255" - normal, naturally ordered text.
99 *
100 * "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be
101 * specifically identified.
102 * "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew
103 * that contain special punctuation marks or diacritics is displayed with
104 * some unconverted characters showing as question marks. This problem might
105 * be corrected using another model prober for x-mac-hebrew. Due to the fact
106 * that x-mac-hebrew texts are so rare, writing another model prober isn't
107 * worth the effort and performance hit.
108 *
109 * *** The Prober ***
110 *
111 * The prober is divided between two nsSBCharSetProbers and an nsHebrewProber,
112 * all of which are managed, created, fed data, inquired and deleted by the
113 * nsSBCSGroupProber. The two nsSBCharSetProbers identify that the text is in
114 * fact some kind of Hebrew, Logical or Visual. The final decision about which
115 * one is it is made by the nsHebrewProber by combining final-letter scores
116 * with the scores of the two nsSBCharSetProbers to produce a final answer.
117 *
118 * The nsSBCSGroupProber is responsible for stripping the original text of HTML
119 * tags, English characters, numbers, low-ASCII punctuation characters, spaces
120 * and new lines. It reduces any sequence of such characters to a single space.
121 * The buffer fed to each prober in the SBCS group prober is pure text in
122 * high-ASCII.
123 * The two nsSBCharSetProbers (model probers) share the same language model:
124 * Win1255Model.
125 * The first nsSBCharSetProber uses the model normally as any other
126 * nsSBCharSetProber does, to recognize windows-1255, upon which this model was
127 * built. The second nsSBCharSetProber is told to make the pair-of-letter
128 * lookup in the language model backwards. This in practice exactly simulates
129 * a visual Hebrew model using the windows-1255 logical Hebrew model.
130 *
131 * The nsHebrewProber is not using any language model. All it does is look for
132 * final-letter evidence suggesting the text is either logical Hebrew or visual
133 * Hebrew. Disjointed from the model probers, the results of the nsHebrewProber
134 * alone are meaningless. nsHebrewProber always returns 0.00 as confidence
135 * since it never identifies a charset by itself. Instead, the pointer to the
136 * nsHebrewProber is passed to the model probers as a helper "Name Prober".
137 * When the Group prober receives a positive identification from any prober,
138 * it asks for the name of the charset identified. If the prober queried is a
139 * Hebrew model prober, the model prober forwards the call to the
140 * nsHebrewProber to make the final decision. In the nsHebrewProber, the
141 * decision is made according to the final-letters scores maintained and Both
142 * model probers scores. The answer is returned in the form of the name of the
143 * charset identified, either "windows-1255" or "ISO-8859-8".
144 *
145 */
146#endif /* nsHebrewProber_h__ */
147

source code of kcodecs/src/probers/nsHebrewProber.h