1 var util = require('util'),
2 Match = require ('../match');
6 * This is a superclass for the individual detectors for
7 * each of the detectable members of the ISO 2022 family
11 function ISO_2022() {}
13 ISO_2022.prototype.match = function(det) {
16 * Matching function shared among the 2022 detectors JP, CN and KR
17 * Counts up the number of legal an unrecognized escape sequences in
18 * the sample of text, and computes a score based on the total number &
19 * the proportion that fit the encoding.
22 * @param text the byte buffer containing text to analyse
23 * @param textLen the size of the text in the byte.
24 * @param escapeSequences the byte escape sequences to test for.
25 * @return match quality, in the range of 0-100.
36 var text = det.fInputBytes;
37 var textLen = det.fInputLen;
40 for (i = 0; i < textLen; i++) {
41 if (text[i] == 0x1b) {
43 for (escN = 0; escN < this.escapeSequences.length; escN++) {
44 var seq = this.escapeSequences[escN];
46 if ((textLen - i) < seq.length)
47 continue checkEscapes;
49 for (j = 1; j < seq.length; j++)
50 if (seq[j] != text[i + j])
51 continue checkEscapes;
63 if (text[i] == 0x0e || text[i] == 0x0f)
72 // Initial quality is based on relative proportion of recongized vs.
73 // unrecognized escape sequences.
74 // All good: quality = 100;
75 // half or less good: quality = 0;
77 quality = (100 * hits - 100 * misses) / (hits + misses);
79 // Back off quality if there were too few escape sequences seen.
80 // Include shifts in this computation, so that KR does not get penalized
81 // for having only a single Escape sequence, but many shifts.
82 if (hits + shifts < 5)
83 quality -= (5 - (hits + shifts)) * 10;
85 return quality <= 0 ? null : new Match(det, this, quality);
88 module.exports.ISO_2022_JP = function() {
89 this.name = function() {
92 this.escapeSequences = [
93 [ 0x1b, 0x24, 0x28, 0x43 ], // KS X 1001:1992
94 [ 0x1b, 0x24, 0x28, 0x44 ], // JIS X 212-1990
95 [ 0x1b, 0x24, 0x40 ], // JIS C 6226-1978
96 [ 0x1b, 0x24, 0x41 ], // GB 2312-80
97 [ 0x1b, 0x24, 0x42 ], // JIS X 208-1983
98 [ 0x1b, 0x26, 0x40 ], // JIS X 208 1990, 1997
99 [ 0x1b, 0x28, 0x42 ], // ASCII
100 [ 0x1b, 0x28, 0x48 ], // JIS-Roman
101 [ 0x1b, 0x28, 0x49 ], // Half-width katakana
102 [ 0x1b, 0x28, 0x4a ], // JIS-Roman
103 [ 0x1b, 0x2e, 0x41 ], // ISO 8859-1
104 [ 0x1b, 0x2e, 0x46 ] // ISO 8859-7
107 util.inherits(module.exports.ISO_2022_JP, ISO_2022);
111 module.exports.ISO_2022_KR = function() {
112 this.name = function() {
113 return 'ISO-2022-KR';
115 this.escapeSequences = [
116 [ 0x1b, 0x24, 0x29, 0x43 ]
119 util.inherits(module.exports.ISO_2022_KR, ISO_2022);
123 module.exports.ISO_2022_CN = function() {
124 this.name = function() {
125 return 'ISO-2022-CN';
127 this.escapeSequences = [
128 [ 0x1b, 0x24, 0x29, 0x41 ], // GB 2312-80
129 [ 0x1b, 0x24, 0x29, 0x47 ], // CNS 11643-1992 Plane 1
130 [ 0x1b, 0x24, 0x2A, 0x48 ], // CNS 11643-1992 Plane 2
131 [ 0x1b, 0x24, 0x29, 0x45 ], // ISO-IR-165
132 [ 0x1b, 0x24, 0x2B, 0x49 ], // CNS 11643-1992 Plane 3
133 [ 0x1b, 0x24, 0x2B, 0x4A ], // CNS 11643-1992 Plane 4
134 [ 0x1b, 0x24, 0x2B, 0x4B ], // CNS 11643-1992 Plane 5
135 [ 0x1b, 0x24, 0x2B, 0x4C ], // CNS 11643-1992 Plane 6
136 [ 0x1b, 0x24, 0x2B, 0x4D ], // CNS 11643-1992 Plane 7
137 [ 0x1b, 0x4e ], // SS2
138 [ 0x1b, 0x4f ] // SS3
141 util.inherits(module.exports.ISO_2022_CN, ISO_2022);