| 1 |
-- |
| 2 |
-- polyglossia-cjk-spacing.lua |
| 3 |
-- part of polyglossia v2.10 -- 2026/04/18 |
| 4 |
-- |
| 5 |
|
| 6 |
local glyph_id = node.id"glyph" |
| 7 |
local hbox_id = node.id"hlist" |
| 8 |
local vbox_id = node.id"vlist" |
| 9 |
local glue_id = node.id"glue" |
| 10 |
local penalty_id = node.id"penalty" |
| 11 |
local whatsit_id = node.id"whatsit" |
| 12 |
local math_id = node.id"math" |
| 13 |
local localpar_id = node.id"local_par" |
| 14 |
local ins_id = node.id"ins" |
| 15 |
local vadjust_id = node.id"adjust" |
| 16 |
local mark_id = node.id"mark" |
| 17 |
|
| 18 |
-- |
| 19 |
-- attr_cjk: variant = plain: 0, JP/classic: 1, KR/modern: 2, SC: 3, TC: 4 |
| 20 |
-- |
| 21 |
local attr_cjk = luatexbase.attributes["xpg@attr@cjkspacing"] |
| 22 |
-- |
| 23 |
-- attr_josa: ONLY For Korean. DO NOT declare \newattribute for other langs |
| 24 |
-- |
| 25 |
local attr_josa = luatexbase.attributes["xpg@attr@autojosa"] |
| 26 |
|
| 27 |
-- |
| 28 |
-- characters after which linebreak is not allowed |
| 29 |
-- |
| 30 |
local nobr_after = { |
| 31 |
[0x28] = 1, -- ( LEFT PARENTHESIS |
| 32 |
[0x3C] = 1, -- < LESS-THAN SIGN |
| 33 |
[0x5B] = 1, -- [ LEFT SQUARE BRACKET |
| 34 |
[0x5C] = 1, -- \ REVERSE SOLIDUS |
| 35 |
[0x60] = 1, -- ` GRAVE ACCENT |
| 36 |
[0x7B] = 1, -- { LEFT CURLY BRACKET |
| 37 |
[0xA1] = 1, -- ¡ INVERTED EXCLAMATION MARK |
| 38 |
[0xAB] = 1, -- « LEFT-POINTING DOUBLE ANGLE QUOTATION MARK |
| 39 |
[0xBF] = 1, -- ¿ INVERTED QUESTION MARK |
| 40 |
[0x2018] = 1, -- ‘ LEFT SINGLE QUOTATION MARK |
| 41 |
[0x201C] = 1, -- “ LEFT DOUBLE QUOTATION MARK |
| 42 |
[0x2329] = 1, -- 〈 LEFT-POINTING ANGLE BRACKET |
| 43 |
[0x3008] = 1, -- 〈 LEFT ANGLE BRACKET |
| 44 |
[0x300A] = 1, -- 《 LEFT DOUBLE ANGLE BRACKET |
| 45 |
[0x300C] = 1, -- 「 LEFT CORNER BRACKET |
| 46 |
[0x300E] = 1, -- 『 LEFT WHITE CORNER BRACKET |
| 47 |
[0x3010] = 1, -- 【 LEFT BLACK LENTICULAR BRACKET |
| 48 |
[0x3014] = 1, -- 〔 LEFT TORTOISE SHELL BRACKET |
| 49 |
[0x3016] = 1, -- 〖 LEFT WHITE LENTICULAR BRACKET |
| 50 |
[0x3018] = 1, -- 〘 LEFT WHITE TORTOISE SHELL BRACKET |
| 51 |
[0x301A] = 1, -- 〚 LEFT WHITE SQUARE BRACKET |
| 52 |
[0x301D] = 1, -- 〝 REVERSED DOUBLE PRIME QUOTATION MARK |
| 53 |
[0xFE17] = 1, -- ︗ PRESENTATION FORM FOR VERTICAL LEFT WHITE LENTICULAR BRACKET |
| 54 |
[0xFE35] = 1, -- ︵ PRESENTATION FORM FOR VERTICAL LEFT PARENTHESIS |
| 55 |
[0xFE37] = 1, -- ︷ PRESENTATION FORM FOR VERTICAL LEFT CURLY BRACKET |
| 56 |
[0xFE39] = 1, -- ︹ PRESENTATION FORM FOR VERTICAL LEFT TORTOISE SHELL BRACKET |
| 57 |
[0xFE3B] = 1, -- ︻ PRESENTATION FORM FOR VERTICAL LEFT BLACK LENTICULAR BRACKET |
| 58 |
[0xFE3D] = 1, -- ︽ PRESENTATION FORM FOR VERTICAL LEFT DOUBLE ANGLE BRACKET |
| 59 |
[0xFE3F] = 1, -- ︿ PRESENTATION FORM FOR VERTICAL LEFT ANGLE BRACKET |
| 60 |
[0xFE41] = 1, -- ﹁ PRESENTATION FORM FOR VERTICAL LEFT CORNER BRACKET |
| 61 |
[0xFE43] = 1, -- ﹃ PRESENTATION FORM FOR VERTICAL LEFT WHITE CORNER BRACKET |
| 62 |
[0xFE47] = 1, -- ﹇ PRESENTATION FORM FOR VERTICAL LEFT SQUARE BRACKET |
| 63 |
[0xFE59] = 1, -- ﹙ SMALL LEFT PARENTHESIS |
| 64 |
[0xFE5B] = 1, -- ﹛ SMALL LEFT CURLY BRACKET |
| 65 |
[0xFF03] = 1, -- # FULLWIDTH NUMBER SIGN |
| 66 |
[0xFF04] = 1, -- $ FULLWIDTH DOLLAR SIGN |
| 67 |
[0xFE5D] = 1, -- ﹝ SMALL LEFT TORTOISE SHELL BRACKET |
| 68 |
[0xFF08] = 1, -- ( FULLWIDTH LEFT PARENTHESIS |
| 69 |
[0xFF3B] = 1, -- [ FULLWIDTH LEFT SQUARE BRACKET |
| 70 |
[0xFF40] = 1, -- ` FULLWIDTH GRAVE ACCENT |
| 71 |
[0xFF5B] = 1, -- { FULLWIDTH LEFT CURLY BRACKET |
| 72 |
[0xFF5F] = 1, -- ⦅ FULLWIDTH LEFT WHITE PARENTHESIS |
| 73 |
[0xFF62] = 1, -- 「 HALFWIDTH LEFT CORNER BRACKET |
| 74 |
[0xFFE5] = 1, -- ¥ FULLWIDTH YEN SIGN |
| 75 |
[0xFFE6] = 1, -- ₩ FULLWIDTH WON SIGN |
| 76 |
} |
| 77 |
|
| 78 |
-- |
| 79 |
-- characters before which linebreak is not allowed |
| 80 |
-- 1: normal chars |
| 81 |
-- 2: hangul jamo vowels and trailing consonants plus combinings |
| 82 |
-- 3: kana small letters (not suppressing char orphan) |
| 83 |
-- 4: dashes and iterations (not suppressing char orphan) |
| 84 |
-- |
| 85 |
local nobr_before = setmetatable({ |
| 86 |
[0x21] = 1, -- ! EXCLAMATION MARK |
| 87 |
[0x22] = 1, -- " QUOTATION MARK |
| 88 |
[0x27] = 1, -- ' APOSTROPHE |
| 89 |
[0x29] = 1, -- ) RIGHT PARENTHESIS |
| 90 |
[0x2C] = 1, -- , COMMA |
| 91 |
[0x2D] = 4, -- - HYPHEN-MINUS |
| 92 |
[0x2E] = 1, -- . FULL STOP |
| 93 |
[0x2F] = 1, -- / SOLIDUS |
| 94 |
[0x3A] = 1, -- : COLON |
| 95 |
[0x3B] = 1, -- ; SEMICOLON |
| 96 |
[0x3E] = 1, -- > GREATER-THAN SIGN |
| 97 |
[0x3F] = 1, -- ? QUESTION MARK |
| 98 |
[0x5D] = 1, -- ] RIGHT SQUARE BRACKET |
| 99 |
[0x7D] = 1, -- } RIGHT CURLY BRACKET |
| 100 |
[0x7E] = 1, -- ~ TILDE |
| 101 |
[0xAA] = 1, -- ª FEMININE ORDINAL INDICATOR |
| 102 |
[0xB0] = 1, -- ° DEGREE SIGN |
| 103 |
[0xB2] = 1, -- ² SUPERSCRIPT TWO |
| 104 |
[0xB3] = 1, -- ³ SUPERSCRIPT THREE |
| 105 |
[0xB7] = 1, -- · MIDDLE DOT |
| 106 |
[0xB9] = 1, -- ¹ SUPERSCRIPT ONE |
| 107 |
[0xBA] = 1, -- º MASCULINE ORDINAL INDICATOR |
| 108 |
[0xBB] = 1, -- » RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK |
| 109 |
[0x2013] = 4, -- – EN DASH |
| 110 |
[0x2014] = 4, -- — EM DASH |
| 111 |
[0x2015] = 4, -- ― HORIZONTAL BAR |
| 112 |
[0x2019] = 1, -- ’ RIGHT SINGLE QUOTATION MARK |
| 113 |
[0x201D] = 1, -- ” RIGHT DOUBLE QUOTATION MARK |
| 114 |
[0x2025] = 4, -- ‥ TWO DOT LEADER |
| 115 |
[0x2026] = 4, -- … HORIZONTAL ELLIPSIS |
| 116 |
[0x232A] = 1, -- 〉 RIGHT-POINTING ANGLE BRACKET |
| 117 |
[0x3001] = 1, -- 、 IDEOGRAPHIC COMMA |
| 118 |
[0x3002] = 1, -- 。 IDEOGRAPHIC FULL STOP |
| 119 |
[0x3005] = 4, -- 々 IDEOGRAPHIC ITERATION MARK |
| 120 |
[0x3009] = 1, -- 〉 RIGHT ANGLE BRACKET |
| 121 |
[0x300B] = 1, -- 》 RIGHT DOUBLE ANGLE BRACKET |
| 122 |
[0x300D] = 1, -- 」 RIGHT CORNER BRACKET |
| 123 |
[0x300F] = 1, -- 』 RIGHT WHITE CORNER BRACKET |
| 124 |
[0x3011] = 1, -- 】 RIGHT BLACK LENTICULAR BRACKET |
| 125 |
[0x3015] = 1, -- 〕 RIGHT TORTOISE SHELL BRACKET |
| 126 |
[0x3017] = 1, -- 〗 RIGHT WHITE LENTICULAR BRACKET |
| 127 |
[0x3019] = 1, -- 〙 RIGHT WHITE TORTOISE SHELL BRACKET |
| 128 |
[0x301B] = 1, -- 〛 RIGHT WHITE SQUARE BRACKET |
| 129 |
[0x301C] = 4, -- 〜 WAVE DASH |
| 130 |
[0x301E] = 1, -- 〞 DOUBLE PRIME QUOTATION MARK |
| 131 |
[0x301F] = 1, -- 〟 LOW DOUBLE PRIME QUOTATION MARK |
| 132 |
[0x3030] = 4, -- 〰 WAVY DASH |
| 133 |
[0x3031] = 4, -- 〱 VERTICAL KANA REPEAT MARK |
| 134 |
[0x3032] = 4, -- 〲 VERTICAL KANA REPEAT WITH VOICED SOUND MARK |
| 135 |
[0x3033] = 4, -- 〳 VERTICAL KANA REPEAT MARK UPPER HALF |
| 136 |
[0x3034] = 4, -- 〴 VERTICAL KANA REPEAT WITH VOICED SOUND MARK UPPER HALF |
| 137 |
[0x3035] = 2, -- 〵 VERTICAL KANA REPEAT MARK LOWER HALF |
| 138 |
[0x303B] = 4, -- 〻 VERTICAL IDEOGRAPHIC ITERATION MARK |
| 139 |
[0x3041] = 3, -- ぁ HIRAGANA LETTER SMALL A |
| 140 |
[0x3043] = 3, -- ぃ HIRAGANA LETTER SMALL I |
| 141 |
[0x3045] = 3, -- ぅ HIRAGANA LETTER SMALL U |
| 142 |
[0x3047] = 3, -- ぇ HIRAGANA LETTER SMALL E |
| 143 |
[0x3049] = 3, -- ぉ HIRAGANA LETTER SMALL O |
| 144 |
[0x3063] = 3, -- っ HIRAGANA LETTER SMALL TU |
| 145 |
[0x3083] = 3, -- ゃ HIRAGANA LETTER SMALL YA |
| 146 |
[0x3085] = 3, -- ゅ HIRAGANA LETTER SMALL YU |
| 147 |
[0x3087] = 3, -- ょ HIRAGANA LETTER SMALL YO |
| 148 |
[0x308E] = 3, -- ゎ HIRAGANA LETTER SMALL WA |
| 149 |
[0x3095] = 3, -- ゕ HIRAGANA LETTER SMALL KA |
| 150 |
[0x3096] = 3, -- ゖ HIRAGANA LETTER SMALL KE |
| 151 |
[0x3099] = 2, -- COMBINING KATAKANA-HIRAGANA VOICED SOUND MARK |
| 152 |
[0x309A] = 2, -- COMBINING KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK |
| 153 |
[0x309B] = 4, -- ゛ KATAKANA-HIRAGANA VOICED SOUND MARK |
| 154 |
[0x309C] = 4, -- ゜ KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK |
| 155 |
[0x309D] = 4, -- ゝ HIRAGANA ITERATION MARK |
| 156 |
[0x309E] = 4, -- ゞ HIRAGANA VOICED ITERATION MARK |
| 157 |
[0x30A0] = 4, -- ゠ KATAKANA-HIRAGANA DOUBLE HYPHEN |
| 158 |
[0x30A1] = 3, -- ァ KATAKANA LETTER SMALL A |
| 159 |
[0x30A3] = 3, -- ィ KATAKANA LETTER SMALL I |
| 160 |
[0x30A5] = 3, -- ゥ KATAKANA LETTER SMALL U |
| 161 |
[0x30A7] = 3, -- ェ KATAKANA LETTER SMALL E |
| 162 |
[0x30A9] = 3, -- ォ KATAKANA LETTER SMALL O |
| 163 |
[0x30C3] = 3, -- ッ KATAKANA LETTER SMALL TU |
| 164 |
[0x30E3] = 3, -- ャ KATAKANA LETTER SMALL YA |
| 165 |
[0x30E5] = 3, -- ュ KATAKANA LETTER SMALL YU |
| 166 |
[0x30E7] = 3, -- ョ KATAKANA LETTER SMALL YO |
| 167 |
[0x30EE] = 3, -- ヮ KATAKANA LETTER SMALL WA |
| 168 |
[0x30F5] = 3, -- ヵ KATAKANA LETTER SMALL KA |
| 169 |
[0x30F6] = 3, -- ヶ KATAKANA LETTER SMALL KE |
| 170 |
[0x30FB] = 4, -- ・ KATAKANA MIDDLE DOT |
| 171 |
[0x30FC] = 4, -- ー KATAKANA-HIRAGANA PROLONGED SOUND MARK |
| 172 |
[0x30FD] = 4, -- ヽ KATAKANA ITERATION MARK |
| 173 |
[0x30FE] = 4, -- ヾ KATAKANA VOICED ITERATION MARK |
| 174 |
[0xFE19] = 4, -- ︙ PRESENTATION FORM FOR VERTICAL HORIZONTAL ELLIPSIS |
| 175 |
[0xFE30] = 4, -- ︰ PRESENTATION FORM FOR VERTICAL TWO DOT LEADER |
| 176 |
[0xFE31] = 4, -- ︱ PRESENTATION FORM FOR VERTICAL EM DASH |
| 177 |
[0xFE32] = 4, -- ︲ PRESENTATION FORM FOR VERTICAL EN DASH |
| 178 |
[0xFE36] = 1, -- ︶ PRESENTATION FORM FOR VERTICAL RIGHT PARENTHESIS |
| 179 |
[0xFE38] = 1, -- ︸ PRESENTATION FORM FOR VERTICAL RIGHT CURLY BRACKET |
| 180 |
[0xFE3A] = 1, -- ︺ PRESENTATION FORM FOR VERTICAL RIGHT TORTOISE SHELL BRACKET |
| 181 |
[0xFE3C] = 1, -- ︼ PRESENTATION FORM FOR VERTICAL RIGHT BLACK LENTICULAR BRACKET |
| 182 |
[0xFE3E] = 1, -- ︾ PRESENTATION FORM FOR VERTICAL RIGHT DOUBLE ANGLE BRACKET |
| 183 |
[0xFE40] = 1, -- ﹀ PRESENTATION FORM FOR VERTICAL RIGHT ANGLE BRACKET |
| 184 |
[0xFE42] = 1, -- ﹂ PRESENTATION FORM FOR VERTICAL RIGHT CORNER BRACKET |
| 185 |
[0xFE44] = 1, -- ﹄ PRESENTATION FORM FOR VERTICAL RIGHT WHITE CORNER BRACKET |
| 186 |
[0xFE48] = 1, -- ﹈ PRESENTATION FORM FOR VERTICAL RIGHT SQUARE BRACKET |
| 187 |
[0xFE5A] = 1, -- ﹚ SMALL RIGHT PARENTHESIS |
| 188 |
[0xFE5C] = 1, -- ﹜ SMALL RIGHT CURLY BRACKET |
| 189 |
[0xFE5E] = 1, -- ﹞ SMALL RIGHT TORTOISE SHELL BRACKET |
| 190 |
[0xFF01] = 1, -- ! FULLWIDTH EXCLAMATION MARK |
| 191 |
[0xFF09] = 1, -- ) FULLWIDTH RIGHT PARENTHESIS |
| 192 |
[0xFF0C] = 1, -- , FULLWIDTH COMMA |
| 193 |
[0xFF0E] = 1, -- . FULLWIDTH FULL STOP |
| 194 |
[0xFF1A] = 1, -- : FULLWIDTH COLON |
| 195 |
[0xFF1B] = 1, -- ; FULLWIDTH SEMICOLON |
| 196 |
[0xFF1F] = 1, -- ? FULLWIDTH QUESTION MARK |
| 197 |
[0xFF3D] = 1, -- ] FULLWIDTH RIGHT SQUARE BRACKET |
| 198 |
[0xFF5D] = 1, -- } FULLWIDTH RIGHT CURLY BRACKET |
| 199 |
[0xFF5E] = 4, -- ~ FULLWIDTH TILDE |
| 200 |
[0xFF60] = 1, -- ⦆ FULLWIDTH RIGHT WHITE PARENTHESIS |
| 201 |
[0xFF61] = 1, -- 。 HALFWIDTH IDEOGRAPHIC FULL STOP |
| 202 |
[0xFF63] = 1, -- 」 HALFWIDTH RIGHT CORNER BRACKET |
| 203 |
[0xFF64] = 1, -- 、 HALFWIDTH IDEOGRAPHIC COMMA |
| 204 |
[0xFF65] = 1, -- ・ HALFWIDTH KATAKANA MIDDLE DOT |
| 205 |
[0xFF9E] = 2, -- ゙ HALFWIDTH KATAKANA VOICED SOUND MARK |
| 206 |
[0xFF9F] = 2, -- ゚ HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK |
| 207 |
}, { __index = function(_,c) |
| 208 |
if c >= 0x1160 and c <= 0x11FF then return 2 end |
| 209 |
if c >= 0xD7B0 and c <= 0xD7FF then return 2 end |
| 210 |
if c >= 0x302A and c <= 0x302F then return 2 end -- tone marks |
| 211 |
if c >= 0x31F0 and c <= 0x31FF then return 3 end |
| 212 |
if c >= 0xFF67 and c <= 0xFF70 then return 3 end |
| 213 |
if c >= 0xFE00 and c <= 0xFE0F then return 2 end -- variation selectors |
| 214 |
if c >= 0xFE10 and c <= 0xFE19 and not (c == 0xFE17) then return 1 end |
| 215 |
if c >= 0xFE50 and c <= 0xFE58 then return 1 end |
| 216 |
if c >= 0xE0100 and c <= 0xE01EF then return 2 end -- variation selecters |
| 217 |
end |
| 218 |
}) |
| 219 |
|
| 220 |
-- |
| 221 |
-- characters before/after which zero-width glue will be inserted |
| 222 |
-- 0: dashes (suppress stretching between dashes) |
| 223 |
-- |
| 224 |
local zero_spacing = { |
| 225 |
[0x23] = 1, -- # NUMBER SIGN |
| 226 |
[0x24] = 1, -- $ DOLLAR SIGN |
| 227 |
[0x25] = 1, -- % PERCENT SIGN |
| 228 |
[0x26] = 1, -- & AMPERSAND |
| 229 |
[0x2A] = 1, -- * ASTERISK |
| 230 |
[0x2B] = 1, -- + PLUS SIGN |
| 231 |
[0x2D] = 0, -- - HYPHEN-MINUS |
| 232 |
[0x2F] = 1, -- / SOLIDUS |
| 233 |
[0x3C] = 1, -- < LESS-THAN SIGN |
| 234 |
[0x3D] = 1, -- = EQUALS SIGN |
| 235 |
[0x3E] = 1, -- > GREATER-THAN SIGN |
| 236 |
[0x40] = 1, -- @ COMMERCIAL AT |
| 237 |
[0x5C] = 1, -- \ REVERSE SOLIDUS |
| 238 |
[0x5E] = 1, -- ^ CIRCUMFLEX ACCENT |
| 239 |
[0x5F] = 1, -- _ LOW LINE |
| 240 |
[0x7C] = 1, -- | VERTICAL LINE |
| 241 |
[0x7E] = 1, -- ~ TILDE |
| 242 |
[0xA5] = 1, -- ¥ YEN SIGN |
| 243 |
[0xB1] = 1, -- ± PLUS-MINUS SIGN |
| 244 |
[0x2013] = 0, -- – EN DASH |
| 245 |
[0x2014] = 0, -- — EM DASH |
| 246 |
[0x2015] = 0, -- ― HORIZONTAL BAR |
| 247 |
[0x2025] = 0, -- ‥ TWO DOT LEADER |
| 248 |
[0x2026] = 0, -- … HORIZONTAL ELLIPSIS |
| 249 |
[0x2030] = 1, -- ‰ PER MILLE SIGN |
| 250 |
[0x20A9] = 1, -- ₩ WON SIGN |
| 251 |
[0x2103] = 1, -- ℃ DEGREE CELSIUS |
| 252 |
[0x2109] = 1, -- ℉ DEGREE FAHRENHEIT |
| 253 |
[0x223C] = 1, -- ∼ TILDE OPERATOR |
| 254 |
[0x301C] = 1, -- 〜 WAVE DASH |
| 255 |
[0x3030] = 0, -- 〰 WAVY DASH |
| 256 |
[0xFE19] = 0, -- ︙ PRESENTATION FORM FOR VERTICAL HORIZONTAL ELLIPSIS |
| 257 |
[0xFE30] = 0, -- ︰ PRESENTATION FORM FOR VERTICAL TWO DOT LEADER |
| 258 |
[0xFE31] = 0, -- ︱ PRESENTATION FORM FOR VERTICAL EM DASH |
| 259 |
[0xFE32] = 0, -- ︲ PRESENTATION FORM FOR VERTICAL EN DASH |
| 260 |
[0xFF03] = 1, -- # FULLWIDTH NUMBER SIGN |
| 261 |
[0xFF04] = 1, -- $ FULLWIDTH DOLLAR SIGN |
| 262 |
[0xFF05] = 1, -- % FULLWIDTH PERCENT SIGN |
| 263 |
[0xFF5E] = 1, -- ~ FULLWIDTH TILDE |
| 264 |
[0xFFE5] = 1, -- ¥ FULLWIDTH YEN SIGN |
| 265 |
[0xFFE6] = 1, -- ₩ FULLWIDTH WON SIGN |
| 266 |
} |
| 267 |
|
| 268 |
-- |
| 269 |
-- whether 'c' is a cjk character |
| 270 |
-- |
| 271 |
local function is_cjk (c) |
| 272 |
return c >= 0xAC00 and c <= 0xD7FF |
| 273 |
or c >= 0x1100 and c <= 0x11FF |
| 274 |
or c >= 0xA960 and c <= 0xA97F |
| 275 |
or c >= 0x2E80 and c <= 0x9FFF |
| 276 |
or c >= 0xF900 and c <= 0xFAFF |
| 277 |
or c >= 0xFE10 and c <= 0xFE1F |
| 278 |
or c >= 0xFE30 and c <= 0xFE6F |
| 279 |
or c >= 0xFF00 and c <= 0xFFEF |
| 280 |
or c >= 0x1F100 and c <= 0x1F2FF |
| 281 |
or c >= 0x20000 and c <= 0x3347F |
| 282 |
or c > 0x2014 and nobr_after[c] |
| 283 |
or c > 0x2014 and nobr_before[c] |
| 284 |
end |
| 285 |
|
| 286 |
-- |
| 287 |
-- classify cjk characters |
| 288 |
-- 1: openings |
| 289 |
-- 2: closings |
| 290 |
-- 3: centered chars |
| 291 |
-- 4: full stops |
| 292 |
-- 5: ellipses |
| 293 |
-- 6: exclamation and question marks |
| 294 |
-- 0: all others |
| 295 |
-- |
| 296 |
local charclass = setmetatable({ |
| 297 |
[0x2018] = 1, [0x201C] = 1, [0x2329] = 1, [0x3008] = 1, |
| 298 |
[0x300A] = 1, [0x300C] = 1, [0x300E] = 1, [0x3010] = 1, |
| 299 |
[0x3014] = 1, [0x3016] = 1, [0x3018] = 1, [0x301A] = 1, |
| 300 |
[0x301D] = 1, [0xFE17] = 1, [0xFE35] = 1, [0xFE37] = 1, |
| 301 |
[0xFE39] = 1, [0xFE3B] = 1, [0xFE3D] = 1, [0xFE3F] = 1, |
| 302 |
[0xFE41] = 1, [0xFE43] = 1, [0xFE47] = 1, [0xFF08] = 1, |
| 303 |
[0xFF3B] = 1, [0xFF5B] = 1, [0xFF5F] = 1, |
| 304 |
-- |
| 305 |
[0x2019] = 2, [0x201D] = 2, [0x232A] = 2, [0x3001] = 2, |
| 306 |
[0x3009] = 2, [0x300B] = 2, [0x300D] = 2, [0x300F] = 2, |
| 307 |
[0x3011] = 2, [0x3015] = 2, [0x3017] = 2, [0x3019] = 2, |
| 308 |
[0x301B] = 2, [0x301E] = 2, [0x301F] = 2, [0xFE10] = 2, |
| 309 |
[0xFE11] = 2, [0xFE18] = 2, [0xFE36] = 2, [0xFE38] = 2, |
| 310 |
[0xFE3A] = 2, [0xFE3C] = 2, [0xFE3E] = 2, [0xFE40] = 2, |
| 311 |
[0xFE42] = 2, [0xFE44] = 2, [0xFE48] = 2, [0xFF09] = 2, |
| 312 |
[0xFF0C] = 2, [0xFF3D] = 2, [0xFF5D] = 2, [0xFF60] = 2, |
| 313 |
-- |
| 314 |
[0x00B7] = 3, [0x30FB] = 3, [0xFF1A] = 3, [0xFF1B] = 3, |
| 315 |
-- |
| 316 |
[0x3002] = 4, [0xFE12] = 4, [0xFF0E] = 4, |
| 317 |
-- |
| 318 |
[0x2015] = 5, [0x2025] = 5, [0x2026] = 5, [0xFE19] = 5, |
| 319 |
[0xFE30] = 5, [0xFE31] = 5, |
| 320 |
-- |
| 321 |
[0xFE15] = 6, [0xFE16] = 6, [0xFF01] = 6, [0xFF1F] = 6, |
| 322 |
}, { __index = function() return 0 end }) |
| 323 |
|
| 324 |
-- |
| 325 |
-- get character class |
| 326 |
-- var : variant = plain, JP/classic, KR/modern, SC, TC |
| 327 |
-- c : codepoint |
| 328 |
-- |
| 329 |
local function get_charclass (var, c) |
| 330 |
if var < 3 then |
| 331 |
return charclass[c] |
| 332 |
elseif var == 3 then |
| 333 |
-- SC : these are left aligned |
| 334 |
return (c == 0xFF01 or c == 0xFF1F) and 4 -- FULLWIDTH EXCLAMATION/QUESTION MARK |
| 335 |
or (c == 0xFF1A or c == 0xFF1B) and 2 -- FULLWIDTH COLON/SEMICOLON |
| 336 |
or charclass[c] |
| 337 |
end |
| 338 |
-- TC : these are center aligned |
| 339 |
return (c == 0x3001 or c == 0xFF0C) and 3 -- IDEOGRAPHIC/FULLWIDTH COMMA |
| 340 |
or (c == 0x3002 or c == 0xFF0E) and 3 -- IDEOGRAPHIC/FULLWIDTH FULL STOP -- 5 ? |
| 341 |
or charclass[c] |
| 342 |
end |
| 343 |
|
| 344 |
-- |
| 345 |
-- table for spacing between char classes |
| 346 |
-- 1 stands for 0.5*fontsize when variant = JP/classic or SC or TC |
| 347 |
-- |
| 348 |
local intercharclass = { [0] = |
| 349 |
{ [0] = nil, {1,1}, nil, {.5,.5} }, |
| 350 |
{ [0] = nil, nil, nil, {.5,.5} }, |
| 351 |
{ [0] = {1,1}, {1,1}, nil, {.5,.5}, nil, {1,1}, {1,1} }, |
| 352 |
{ [0] = {.5,.5},{.5,.5},{.5,.5},{1,.5}, {.5,.5},{.5,.5},{.5,.5} }, |
| 353 |
{ [0] = {1,0}, {1,0}, nil, {1.5,.5},nil, {1,0}, {1,0} }, |
| 354 |
{ [0] = nil, {1,1}, nil, {.5,.5} }, |
| 355 |
{ [0] = {1,1}, {1,1}, nil, {.5,.5} }, |
| 356 |
} |
| 357 |
|
| 358 |
-- |
| 359 |
-- get a new penalty node |
| 360 |
-- |
| 361 |
local function get_new_penalty (p) |
| 362 |
local penalty = node.new("penalty") |
| 363 |
penalty.penalty = p |
| 364 |
return penalty |
| 365 |
end |
| 366 |
|
| 367 |
-- |
| 368 |
-- get a new glue node |
| 369 |
-- |
| 370 |
local function get_new_glue (...) |
| 371 |
local glue = node.new("glue") |
| 372 |
node.setglue(glue, ...) |
| 373 |
return glue |
| 374 |
end |
| 375 |
|
| 376 |
-- |
| 377 |
-- return 0.5*fontsize of given fontid |
| 378 |
-- space: true if variant=KR/modern; then 0.5*interword_space |
| 379 |
-- |
| 380 |
local function get_font_size (fid, space) |
| 381 |
local size = font.getparameters(fid) |
| 382 |
if space then |
| 383 |
size = size and size.space or 196608 |
| 384 |
else |
| 385 |
size = size and size.quad or 655360 |
| 386 |
end |
| 387 |
return size/2 |
| 388 |
end |
| 389 |
|
| 390 |
-- |
| 391 |
-- charclass 1 thru 4 will be packed in \hbox to 0.5em{\hss? curr \hss?} |
| 392 |
-- when variant ~= plain |
| 393 |
-- |
| 394 |
local function glyph_to_box (head, curr, class) |
| 395 |
local g, h = curr |
| 396 |
local size = get_font_size(g.font) |
| 397 |
head, curr = node.remove(head, curr) |
| 398 |
g.next, g.prev = nil, nil |
| 399 |
local hss = get_new_glue(0, 65536, 65536, 2, 2) |
| 400 |
if class == 1 then |
| 401 |
h, hss.next, g.prev = hss, g, hss |
| 402 |
elseif class == 2 or class == 4 then |
| 403 |
h, g.next, hss.prev = g, hss, g |
| 404 |
else |
| 405 |
local hss2 = node.copy(hss) |
| 406 |
h, hss.next, g.prev, g.next, hss2.prev = hss, g, hss, hss2, g |
| 407 |
end |
| 408 |
h = nodes.simple_font_handler(h) |
| 409 |
local box = node.hpack(h, size, "exactly") |
| 410 |
if curr then |
| 411 |
head, curr = node.insert_before(head, curr, box) |
| 412 |
else |
| 413 |
head, curr = node.insert_after(head, node.tail(head), box) |
| 414 |
end |
| 415 |
return head, curr |
| 416 |
end |
| 417 |
|
| 418 |
-- |
| 419 |
-- insert spacing defined as charclass[a][b] between a and b |
| 420 |
-- f: fontid |
| 421 |
-- var: variant = plain, JP/classic, KR/modern, SC, TC |
| 422 |
-- cc: charclass of current char |
| 423 |
-- nc: charclass of next char |
| 424 |
-- nobr: linebreak is not allowed |
| 425 |
-- |
| 426 |
local function insert_cjk_penalty_glue (head, curr, f, var, cc, nc, nobr) |
| 427 |
if nobr or cc == 1 or nc > 1 then |
| 428 |
local penalty = get_new_penalty(10000) |
| 429 |
head, curr = node.insert_after(head, curr, penalty) |
| 430 |
end |
| 431 |
local factor = get_font_size(f, var == 2) |
| 432 |
local t = intercharclass[cc][nc] |
| 433 |
local glue = get_new_glue(t[1]*factor, nil, t[2]*factor) |
| 434 |
head, curr = node.insert_after(head, curr, glue) |
| 435 |
return head, curr |
| 436 |
end |
| 437 |
|
| 438 |
-- |
| 439 |
-- insert inter-character spacing in other normal cases |
| 440 |
-- f: fontid |
| 441 |
-- var: variant = plain, JP/classic, KR/modern, SC, TC |
| 442 |
-- nobr: no linebreak |
| 443 |
-- x: true between cjk and non-cjk (a little more spacing) |
| 444 |
-- |
| 445 |
local function insert_penalty_glue (head, curr, f, var, nobr, x, orph) |
| 446 |
if curr.id ~= penalty_id then |
| 447 |
local penalty = nobr and 10000 or orph and 1000 |
| 448 |
or (var == 0 or var == 2) and 50 or not f and 0 |
| 449 |
if penalty then |
| 450 |
head, curr = node.insert_after(head, curr, get_new_penalty(penalty)) |
| 451 |
end |
| 452 |
end |
| 453 |
if not f then return head, curr end |
| 454 |
local size, glue = get_font_size(f, x and var == 2) |
| 455 |
if x then |
| 456 |
glue = get_new_glue(size/2, size/4, size/8) |
| 457 |
elseif var == 0 or var == 2 then |
| 458 |
glue = get_new_glue(0, size/10, size/50) |
| 459 |
else |
| 460 |
glue = get_new_glue(0, size/5, size/50) |
| 461 |
end |
| 462 |
head, curr = node.insert_after(head, curr, glue) |
| 463 |
return head, curr |
| 464 |
end |
| 465 |
|
| 466 |
-- |
| 467 |
-- overwrite \inhibitglue which suppresses inter-character glue |
| 468 |
-- |
| 469 |
local inhibitglue_attr = luatexbase.new_attribute"polyglossia_inhibitglue_attr" |
| 470 |
local inhibitglue_index = luatexbase.new_luafunction"polyglossia_inhibitglue_func" |
| 471 |
lua.get_functions_table()[inhibitglue_index] = function () |
| 472 |
local s = get_font_size(font.current()) |
| 473 |
local n = get_new_glue(0, s/5, s/50) |
| 474 |
node.set_attribute(n, inhibitglue_attr, 1) |
| 475 |
node.write(n) |
| 476 |
end |
| 477 |
token.set_lua("inhibitglue", inhibitglue_index, "global", "protected") |
| 478 |
|
| 479 |
-- |
| 480 |
-- helper functions for cjk_break |
| 481 |
-- |
| 482 |
local is_skippable = { |
| 483 |
[whatsit_id] = true, |
| 484 |
[penalty_id] = true, |
| 485 |
[vadjust_id] = true, |
| 486 |
[ins_id] = true, |
| 487 |
[mark_id] = true, |
| 488 |
} |
| 489 |
local function is_skippable_node(n) |
| 490 |
return is_skippable[n.id] |
| 491 |
or n.kern and n.subtype ~= 1 -- non-user kern |
| 492 |
or n.list and n.next and n.next.id == ins_id -- footnote. TODO: mpfootnote |
| 493 |
end |
| 494 |
local function get_glyph_in_box (n, first) |
| 495 |
local getnext = first and node.getnext or node.getprev |
| 496 |
local curr = first and n.list or node.slide(n.list) |
| 497 |
while curr do |
| 498 |
if curr.char and node.has_attribute(curr, attr_cjk) then |
| 499 |
return curr.char, curr.font |
| 500 |
elseif curr.list then -- intentionally put before is_skippable_node |
| 501 |
return get_glyph_in_box(curr, first) |
| 502 |
elseif is_skippable_node(curr) then -- skip |
| 503 |
else |
| 504 |
return 1 -- 1 indicates non-glyph box |
| 505 |
end |
| 506 |
curr = getnext(curr) |
| 507 |
end |
| 508 |
return 1 |
| 509 |
end |
| 510 |
local function is_glue_to_skip(n) |
| 511 |
return n.id == glue_id and |
| 512 |
(n.subtype >= 13 and n.subtype <= 15 or node.has_attribute(n, inhibitglue_attr)) |
| 513 |
end |
| 514 |
local function is_normal_box(n) |
| 515 |
return (n.id == hbox_id or n.id == vbox_id) and (n.subtype == 0 or n.subtype == 2) |
| 516 |
end |
| 517 |
local function is_math_onoff(n, onoff) |
| 518 |
return n and n.id == math_id and n.subtype == onoff -- TODO: $\vcenter{...}$ |
| 519 |
end |
| 520 |
|
| 521 |
-- |
| 522 |
-- main process for linebreak and inter-character spacing |
| 523 |
-- |
| 524 |
local function cjk_break (head) |
| 525 |
local curr = head |
| 526 |
local para = head.id == localpar_id |
| 527 |
|
| 528 |
while curr do |
| 529 |
local var = node.has_attribute(curr, attr_cjk) |
| 530 |
if var and (curr.id == glyph_id or is_math_onoff(curr, 1) or is_normal_box(curr)) then |
| 531 |
|
| 532 |
local c, f |
| 533 |
if curr.id == hbox_id or curr.id == vbox_id then |
| 534 |
c, f = get_glyph_in_box(curr) |
| 535 |
else |
| 536 |
c, f = curr.char or 0, curr.font |
| 537 |
end |
| 538 |
local cc = get_charclass(var, c) |
| 539 |
|
| 540 |
-- compress cjk punctuations when charclass is 1 thru 4 |
| 541 |
if curr.char and var > 0 and cc > 0 and cc < 5 then |
| 542 |
head, curr = glyph_to_box(head, curr, cc) |
| 543 |
end |
| 544 |
|
| 545 |
local next = node.getnext(curr) |
| 546 |
while next and is_skippable_node(next) do |
| 547 |
-- skip whatsit/penalty/ins/vadjust/mark/normal footnote/non-user kern |
| 548 |
curr, next = next, node.getnext(next) |
| 549 |
end |
| 550 |
if not next then break end |
| 551 |
|
| 552 |
local nvar = node.has_attribute(next, attr_cjk) |
| 553 |
if nvar and (next.id == glyph_id or is_math_onoff(next, 0) or is_normal_box(next)) then |
| 554 |
|
| 555 |
local n, f2 |
| 556 |
if next.id == hbox_id or next.id == vbox_id then |
| 557 |
n, f2 = get_glyph_in_box(next, true) |
| 558 |
else |
| 559 |
n, f2 = next.char or 0, next.font |
| 560 |
end |
| 561 |
if not (f or f2) then goto skip_combining end |
| 562 |
|
| 563 |
local nbrn, zroc, zron = nobr_before[n], zero_spacing[c], zero_spacing[n] |
| 564 |
|
| 565 |
-- skip combining. or dash+dash case to suppress stretching |
| 566 |
if nbrn == 2 or zroc == 0 and zron == 0 then goto skip_combining end |
| 567 |
|
| 568 |
local nc = get_charclass(nvar, n) |
| 569 |
local nobr = nbrn or nobr_after[c] |
| 570 |
|
| 571 |
f = nc > 0 and nvar ~= 2 and f2 or f or f2 -- priority to cjk punctuation font |
| 572 |
if f == f2 then var = nvar end |
| 573 |
|
| 574 |
-- insert spacing as of intercharclass |
| 575 |
if var > 0 and intercharclass[cc][nc] then |
| 576 |
head, curr = insert_cjk_penalty_glue(head, curr, f, var, cc, nc, nobr) |
| 577 |
|
| 578 |
else |
| 579 |
local cjkc, cjkn = is_cjk(c), is_cjk(n) |
| 580 |
|
| 581 |
-- if curr or next is cjk char |
| 582 |
if cjkc or cjkn then |
| 583 |
if c == 1 or n == 1 then -- non-glyph box: penalty only |
| 584 |
head, curr = insert_penalty_glue(head, curr, false, var, nobr) |
| 585 |
|
| 586 |
else |
| 587 |
local orph |
| 588 |
if para and not nobr then |
| 589 |
local nn = node.getnext(next) |
| 590 |
while nn.char do |
| 591 |
local nb = nobr_before[nn.char] |
| 592 |
if not nb or nb >= 3 then break end |
| 593 |
nn = node.getnext(nn) |
| 594 |
end |
| 595 |
if nn.penalty and nn.penalty == 10000 then |
| 596 |
nn = node.getnext(nn) |
| 597 |
if nn.id == glue_id and nn.subtype == 15 then -- parfillskip |
| 598 |
orph = true -- suppress char orphan |
| 599 |
end |
| 600 |
end |
| 601 |
end |
| 602 |
|
| 603 |
-- plain variant / cjk+cjk / nobr cjk+noncjk / around dash : 0pt glue |
| 604 |
local x = not (var == 0 or cjkc and cjkn or nobr or zroc or zron) |
| 605 |
|
| 606 |
head, curr = insert_penalty_glue(head, curr, f, var, nobr, x, orph) |
| 607 |
end |
| 608 |
end |
| 609 |
end |
| 610 |
|
| 611 |
elseif var > 0 and intercharclass[cc][0] then |
| 612 |
if is_glue_to_skip(next) then |
| 613 |
-- skip spaceskip, xspaceskkp, parfillskip, inhibitglue |
| 614 |
else |
| 615 |
-- insert glue between cjk_punct and non-glyph node |
| 616 |
head, curr = insert_cjk_penalty_glue(head, curr, f, var, cc, 0, false) |
| 617 |
end |
| 618 |
end |
| 619 |
elseif curr.id == localpar_id |
| 620 |
or curr.id == hbox_id and curr.subtype == 3 -- indentbox |
| 621 |
or is_glue_to_skip(curr) then |
| 622 |
-- skip |
| 623 |
else |
| 624 |
local n = node.getnext(curr) |
| 625 |
if n and n.id == glyph_id then |
| 626 |
local var = node.has_attribute(n, attr_cjk) |
| 627 |
if var and var > 0 then |
| 628 |
local nc = get_charclass(var, n.char) |
| 629 |
if intercharclass[0][nc] then |
| 630 |
-- insert glue between non-glyph node and cjk_punct |
| 631 |
head, curr = insert_cjk_penalty_glue(head, curr, n.font, var, 0, nc, false) |
| 632 |
end |
| 633 |
end |
| 634 |
end |
| 635 |
end |
| 636 |
::skip_combining:: |
| 637 |
curr = node.getnext(curr) |
| 638 |
if is_math_onoff(curr, 0) then curr = node.end_of_math(curr) end |
| 639 |
end |
| 640 |
return head |
| 641 |
end |
| 642 |
|
| 643 |
-- |
| 644 |
-- automatic josa selection |
| 645 |
-- |
| 646 |
local josa_table = { |
| 647 |
-- consonant ㄹ, vowel, other consonants |
| 648 |
[0xAC00] = {0xC774, 0xAC00, 0xC774}, -- 가 => 이, 가, 이 |
| 649 |
[0xC740] = {0xC740, 0xB294, 0xC740}, -- 은 => 은, 는, 은 |
| 650 |
[0xC744] = {0xC744, 0xB97C, 0xC744}, -- 을 => 을, 를, 을 |
| 651 |
[0xC640] = {0xACFC, 0xC640, 0xACFC}, -- 와 => 과, 와, 과 |
| 652 |
[0xC73C] = {nil, nil, 0xC73C}, -- 으(로) => , , 으 |
| 653 |
[0xC774] = {0xC774, nil, 0xC774}, -- 이(라) => 이, , 이 |
| 654 |
} |
| 655 |
|
| 656 |
-- |
| 657 |
-- helper functions for josa_code |
| 658 |
-- |
| 659 |
local function josa_char_num (t, c, diff) |
| 660 |
local j = t[(c - diff) % 10 + 0x30] or 2 |
| 661 |
t[c] = j; return j |
| 662 |
end |
| 663 |
local function josa_char_alph (t, c, diff) |
| 664 |
local j = t[c - diff + 0x61] or 2 |
| 665 |
t[c] = j; return j |
| 666 |
end |
| 667 |
local function josa_char_jamo (t, c) |
| 668 |
if c >= 0x1160 and c <= 0x11A7 or c >= 0xD7B0 and c <= 0xD7C6 then |
| 669 |
t[c] = 2; return 2 -- HJF (0x1160) is in the main table below |
| 670 |
end |
| 671 |
t[c] = 3; return 3 -- ㄹ (0x1105, 0x11AF) is in the main table below |
| 672 |
end |
| 673 |
|
| 674 |
-- |
| 675 |
-- decide josa selection |
| 676 |
-- |
| 677 |
local josa_code = setmetatable({ |
| 678 |
[0x30] = 3, [0x31] = 1, [0x33] = 3, [0x36] = 3, [0x37] = 1, |
| 679 |
[0x38] = 1, [0x4C] = 1, [0x4D] = 3, [0x4E] = 3, [0x6C] = 1, |
| 680 |
[0x6D] = 3, [0x6E] = 3, [0xFB02] = 1, [0xFB04] = 1, |
| 681 |
[0x1105] = 1, [0x1160] = false, [0x11AF] = 1, [0x3139] = 1, |
| 682 |
[0x3203] = 1, [0x321D] = 3, [0x3263] = 1, |
| 683 |
},{ __index = function(t,c) |
| 684 |
if c >= 0x30 and c <= 0x39 or c >= 0x41 and c <= 0x5A or c >= 0x61 and c <= 0x7A |
| 685 |
or c >= 0xFB00 and c <= 0xFB06 then |
| 686 |
t[c] = 2; return 2 -- 0, 1, 3, 6, 7, 8, M, N, L, m, n, l, fl, ffl in the main table |
| 687 |
elseif c >= 0xAC00 and c <= 0xD7A3 then |
| 688 |
local j = josa_char_jamo(t, (c - 0xAC00) % 28 + 0x11A7) |
| 689 |
t[c] = j; return j |
| 690 |
elseif c >= 0x1100 and c <= 0x11FF |
| 691 |
or c >= 0xA960 and c <= 0xA97C |
| 692 |
or c >= 0xD7B0 and c <= 0xD7FB then |
| 693 |
return josa_char_jamo(t, c) |
| 694 |
elseif c >= 0x3131 and c <= 0x318E then |
| 695 |
if c >= 0x314F and c <= 0x3163 or c >= 0x3187 then t[c] = 2; return 2 end |
| 696 |
t[c] = 3; return 3 -- ㄹ (0x3139) is in the main table |
| 697 |
elseif c >= 0x3200 and c <= 0x321E then |
| 698 |
if c >= 0x320E then t[c] = 2; return 2 end -- ㈝ (0x321D) is in the main table |
| 699 |
t[c] = 3; return 3 -- ㈃ (0x3203) is in the main table |
| 700 |
elseif c >= 0x3260 and c <= 0x327F then |
| 701 |
if c >= 0x326E then t[c] = 2; return 2 end |
| 702 |
t[c] = 3; return 3 -- ㉣ (0x3263) is in the main table |
| 703 |
elseif c >= 0x2160 and c <= 0x217F then |
| 704 |
if c <= 0x216B then return josa_char_num(t, c, 0x215F) |
| 705 |
elseif c <= 0x216F then t[c] = 3; return 3 |
| 706 |
elseif c <= 0x217B then return josa_char_num(t, c, 0x216F) |
| 707 |
else t[c] = 3; return 3 |
| 708 |
end |
| 709 |
elseif c >= 0x2460 and c <= 0x24E9 then |
| 710 |
if c <= 0x2473 then return josa_char_num(t, c, 0x245F) |
| 711 |
elseif c <= 0x2487 then return josa_char_num(t, c, 0x2473) |
| 712 |
elseif c <= 0x249B then return josa_char_num(t, c, 0x2487) |
| 713 |
elseif c <= 0x24B5 then return josa_char_alph(t, c, 0x249C) |
| 714 |
elseif c <= 0x24CF then return josa_char_alph(t, c, 0x24B6) |
| 715 |
else return josa_char_alph(t, c, 0x24D0) |
| 716 |
end |
| 717 |
elseif c >= 0xFF10 and c <= 0xFF19 then return josa_char_num(t, c, 0xFF10) |
| 718 |
elseif c >= 0xFF21 and c <= 0xFF3A then return josa_char_alph(t, c, 0xFF21) |
| 719 |
elseif c >= 0xFF41 and c <= 0xFF5A then return josa_char_alph(t, c, 0xFF41) |
| 720 |
elseif c >= 0x3400 and c <= 0x9FFF |
| 721 |
or c >= 0xF900 and c <= 0xFAFF |
| 722 |
or c >= 0x20000 and c <= 0x3347F then |
| 723 |
t[c] = 2; return 2; -- TODO: need to parse UniHan database |
| 724 |
end |
| 725 |
end |
| 726 |
}) |
| 727 |
|
| 728 |
-- |
| 729 |
-- obtain char that comes just before the josa |
| 730 |
-- |
| 731 |
local function get_prev_josa_type (p) |
| 732 |
while p do |
| 733 |
if p.char then |
| 734 |
local j = josa_code[p.char] |
| 735 |
if j then return j end |
| 736 |
elseif p.list then |
| 737 |
local j = get_prev_josa_type(node.slide(p.list)) |
| 738 |
if j then return j end |
| 739 |
end |
| 740 |
p = node.getprev(p) |
| 741 |
end |
| 742 |
end |
| 743 |
|
| 744 |
-- |
| 745 |
-- main process of josa selection |
| 746 |
-- |
| 747 |
local function auto_josa (head) |
| 748 |
local curr, tofree = head, {} |
| 749 |
while curr do |
| 750 |
if curr.id == glyph_id then |
| 751 |
local var = node.has_attribute(curr, attr_cjk) |
| 752 |
if var and var <= 2 then |
| 753 |
local josa = node.has_attribute(curr, attr_josa) |
| 754 |
if josa then |
| 755 |
if josa == 0 then |
| 756 |
josa = get_prev_josa_type(node.getprev(curr)) or 2 |
| 757 |
end |
| 758 |
local cc = curr.char or 0 |
| 759 |
if cc == 0xC774 then |
| 760 |
local n = node.getnext(curr) |
| 761 |
if n and n.char and n.char >= 0xAC00 and n.char <= 0xD7A3 then |
| 762 |
else |
| 763 |
cc = 0xAC00 |
| 764 |
end |
| 765 |
end |
| 766 |
local new = josa_table[cc] |
| 767 |
if new then |
| 768 |
cc = new[josa] |
| 769 |
if cc then |
| 770 |
curr.char = cc |
| 771 |
else |
| 772 |
head = node.remove(head, curr) |
| 773 |
table.insert(tofree, curr) |
| 774 |
end |
| 775 |
end |
| 776 |
node.unset_attribute(curr, attr_josa) |
| 777 |
end |
| 778 |
end |
| 779 |
end |
| 780 |
curr = node.getnext(curr) |
| 781 |
end |
| 782 |
for _,v in ipairs(tofree) do node.free(v) end |
| 783 |
return head |
| 784 |
end |
| 785 |
|
| 786 |
-- |
| 787 |
-- now register to luatex callbacks |
| 788 |
-- As char value of glyphs can be changed by opentype GSUB process, |
| 789 |
-- we have to occupy the first position among callback functions. |
| 790 |
-- |
| 791 |
luatexbase.add_to_callback( "pre_shaping_filter", |
| 792 |
function(head) |
| 793 |
if attr_josa then head = auto_josa(head) end |
| 794 |
head = cjk_break(head) |
| 795 |
return head |
| 796 |
end, |
| 797 |
"polyglossia.lang_cjk_spacing") |
| 798 |
|
| 799 |
-- vim:ft=lua:tw=0:sw=4:ts=4:expandtab |