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