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