| 1 | package scsu |
| 2 | |
| 3 | const ( |
| 4 | /** Single Byte mode command values */ |
| 5 | |
| 6 | /** SQ<i>n</i> Quote from Window . <p> |
| 7 | If the following byte is less than 0x80, quote from |
| 8 | static window <i>n</i>, else quote from dynamic window <i>n</i>. |
| 9 | */ |
| 10 | |
| 11 | SQ0 = 0x01 // Quote from window pair 0 |
| 12 | SQ1 = 0x02 // Quote from window pair 1 |
| 13 | SQ2 = 0x03 // Quote from window pair 2 |
| 14 | SQ3 = 0x04 // Quote from window pair 3 |
| 15 | SQ4 = 0x05 // Quote from window pair 4 |
| 16 | SQ5 = 0x06 // Quote from window pair 5 |
| 17 | SQ6 = 0x07 // Quote from window pair 6 |
| 18 | SQ7 = 0x08 // Quote from window pair 7 |
| 19 | |
| 20 | SDX = 0x0B // Define a window as extended |
| 21 | Srs = 0x0C // reserved |
| 22 | |
| 23 | SQU = 0x0E // Quote a single Unicode character |
| 24 | SCU = 0x0F // Change to Unicode mode |
| 25 | |
| 26 | /** SC<i>n</i> Change to Window <i>n</i>. <p> |
| 27 | If the following bytes are less than 0x80, interpret them |
| 28 | as command bytes or pass them through, else add the offset |
| 29 | for dynamic window <i>n</i>. */ |
| 30 | SC0 = 0x10 // Select window 0 |
| 31 | SC1 = 0x11 // Select window 1 |
| 32 | SC2 = 0x12 // Select window 2 |
| 33 | SC3 = 0x13 // Select window 3 |
| 34 | SC4 = 0x14 // Select window 4 |
| 35 | SC5 = 0x15 // Select window 5 |
| 36 | SC6 = 0x16 // Select window 6 |
| 37 | SC7 = 0x17 // Select window 7 |
| 38 | SD0 = 0x18 // Define and select window 0 |
| 39 | SD1 = 0x19 // Define and select window 1 |
| 40 | SD2 = 0x1A // Define and select window 2 |
| 41 | SD3 = 0x1B // Define and select window 3 |
| 42 | SD4 = 0x1C // Define and select window 4 |
| 43 | SD5 = 0x1D // Define and select window 5 |
| 44 | SD6 = 0x1E // Define and select window 6 |
| 45 | SD7 = 0x1F // Define and select window 7 |
| 46 | |
| 47 | UC0 = 0xE0 // Select window 0 |
| 48 | UC1 = 0xE1 // Select window 1 |
| 49 | UC2 = 0xE2 // Select window 2 |
| 50 | UC3 = 0xE3 // Select window 3 |
| 51 | UC4 = 0xE4 // Select window 4 |
| 52 | UC5 = 0xE5 // Select window 5 |
| 53 | UC6 = 0xE6 // Select window 6 |
| 54 | UC7 = 0xE7 // Select window 7 |
| 55 | UD0 = 0xE8 // Define and select window 0 |
| 56 | UD1 = 0xE9 // Define and select window 1 |
| 57 | UD2 = 0xEA // Define and select window 2 |
| 58 | UD3 = 0xEB // Define and select window 3 |
| 59 | UD4 = 0xEC // Define and select window 4 |
| 60 | UD5 = 0xED // Define and select window 5 |
| 61 | UD6 = 0xEE // Define and select window 6 |
| 62 | UD7 = 0xEF // Define and select window 7 |
| 63 | |
| 64 | UQU = 0xF0 // Quote a single Unicode character |
| 65 | UDX = 0xF1 // Define a Window as extended |
| 66 | Urs = 0xF2 // reserved |
| 67 | |
| 68 | ) |
| 69 | |
| 70 | var ( |
| 71 | /** constant offsets for the 8 static windows */ |
| 72 | staticOffset = [...]int32{ |
| 73 | 0x0000, // ASCII for quoted tags |
| 74 | 0x0080, // Latin - 1 Supplement (for access to punctuation) |
| 75 | 0x0100, // Latin Extended-A |
| 76 | 0x0300, // Combining Diacritical Marks |
| 77 | 0x2000, // General Punctuation |
| 78 | 0x2080, // Currency Symbols |
| 79 | 0x2100, // Letterlike Symbols and Number Forms |
| 80 | 0x3000, // CJK Symbols and punctuation |
| 81 | } |
| 82 | |
| 83 | /** initial offsets for the 8 dynamic (sliding) windows */ |
| 84 | initialDynamicOffset = [...]int32{ |
| 85 | 0x0080, // Latin-1 |
| 86 | 0x00C0, // Latin Extended A //@005 fixed from 0x0100 |
| 87 | 0x0400, // Cyrillic |
| 88 | 0x0600, // Arabic |
| 89 | 0x0900, // Devanagari |
| 90 | 0x3040, // Hiragana |
| 91 | 0x30A0, // Katakana |
| 92 | 0xFF00, // Fullwidth ASCII |
| 93 | } |
| 94 | ) |
| 95 | |
| 96 | const ( |
| 97 | /** |
| 98 | These values are used in defineWindow |
| 99 | **/ |
| 100 | |
| 101 | /** |
| 102 | * Unicode code points from 3400 to E000 are not addressable by |
| 103 | * dynamic window, since in these areas no short run alphabets are |
| 104 | * found. Therefore add gapOffset to all values from gapThreshold */ |
| 105 | gapThreshold = 0x68 |
| 106 | gapOffset = 0xAC00 |
| 107 | |
| 108 | /* values between reservedStart and fixedThreshold are reserved */ |
| 109 | reservedStart = 0xA8 |
| 110 | |
| 111 | /* use table of predefined fixed offsets for values from fixedThreshold */ |
| 112 | fixedThreshold = 0xF9 |
| 113 | ) |
| 114 | |
| 115 | var ( |
| 116 | /** Table of fixed predefined Offsets, and byte values that index into **/ |
| 117 | fixedOffset = [...]int32{ |
| 118 | /* 0xF9 */ 0x00C0, // Latin-1 Letters + half of Latin Extended A |
| 119 | /* 0xFA */ 0x0250, // IPA extensions |
| 120 | /* 0xFB */ 0x0370, // Greek |
| 121 | /* 0xFC */ 0x0530, // Armenian |
| 122 | /* 0xFD */ 0x3040, // Hiragana |
| 123 | /* 0xFE */ 0x30A0, // Katakana |
| 124 | /* 0xFF */ 0xFF60, // Halfwidth Katakana |
| 125 | } |
| 126 | ) |
| 127 | |
| 128 | type scsu struct { |
| 129 | window int // current active window |
| 130 | unicodeMode bool |
| 131 | dynamicOffset [8]int32 |
| 132 | } |
| 133 | |
| 134 | /** whether a character is compressible */ |
| 135 | func isCompressible(ch rune) bool { |
| 136 | return ch < 0x3400 || ch >= 0xE000 && ch <= 0x20000 |
| 137 | } |
| 138 | |
| 139 | func (scsu *scsu) init() { |
| 140 | scsu.dynamicOffset = initialDynamicOffset |
| 141 | } |
| 142 | |
| 143 | func (scsu *scsu) reset() { |
| 144 | scsu.window = 0 |
| 145 | scsu.unicodeMode = false |
| 146 | } |