|
| 1 | +/* eslint-disable camelcase */ |
| 2 | +import {describe, test, expect} from '../../util/vitest'; |
| 3 | +import {needsRotationInVerticalMode} from '../../../src/util/script_detection'; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// needsRotationInVerticalMode — tested against UAX #50 Vertical Text Layout |
| 7 | +// https://www.unicode.org/reports/tr50/ |
| 8 | +// Data: https://www.unicode.org/Public/UCD/latest/ucd/VerticalOrientation.txt |
| 9 | +// |
| 10 | +// UAX #50 assigns every Unicode codepoint one of four Vertical_Orientation (vo) |
| 11 | +// values: |
| 12 | +// U — Upright: always drawn upright |
| 13 | +// R — Rotated: always drawn sideways (90° CW) |
| 14 | +// Tu — Transformed, fallback Upright |
| 15 | +// Tr — Transformed, fallback Rotated |
| 16 | +// |
| 17 | +// In allowVerticalPlacement=true mode (text-writing-mode includes "vertical"), |
| 18 | +// needsRotationInVerticalMode is the SOLE mechanism for rotating a character. |
| 19 | +// Characters absent from this function appear upright regardless of their vo. |
| 20 | +// |
| 21 | +// Scope |
| 22 | +// ───── |
| 23 | +// This function only handles characters from the Shift JIS 2-byte standard |
| 24 | +// that appear in Japanese map labels. It does NOT attempt full UAX #50 |
| 25 | +// coverage (e.g. Latin letters have vo=R but are not in scope here). |
| 26 | +// |
| 27 | +// Deviations from UAX #50 |
| 28 | +// ──────────────────────── |
| 29 | +// Where we deliberately diverge from the standard, the reason is documented |
| 30 | +// in SHIFT_JIS_OVERRIDES below. All other characters should align exactly. |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | + |
| 33 | +// Characters where UAX #50 assigns vo=Tr. |
| 34 | +// "Tr" means the character ideally needs a special vertical-form glyph; |
| 35 | +// absent that, it should fall back to rotation. Since we don't load font |
| 36 | +// vertical alternates (vert/vrt2 OpenType features), we always rotate. |
| 37 | +const UAX50_Tr = [ |
| 38 | + 0x3018, // 〘 LEFT WHITE TORTOISE SHELL BRACKET (CJK Symbols) |
| 39 | + 0x3019, // 〙 RIGHT WHITE TORTOISE SHELL BRACKET (CJK Symbols) |
| 40 | + 0x301C, // 〜 WAVE DASH (CJK Symbols) |
| 41 | + 0x30A0, // ゠ KATAKANA-HIRAGANA DOUBLE HYPHEN (Katakana) |
| 42 | + 0x30FC, // ー KATAKANA-HIRAGANA PROLONGED SOUND MARK (Katakana) |
| 43 | + 0xFF5E, // ~ FULLWIDTH TILDE (HF Forms) |
| 44 | + 0xFFE3, //  ̄ FULLWIDTH MACRON (HF Forms) |
| 45 | +]; |
| 46 | + |
| 47 | +// Characters where UAX #50 assigns vo=R, within the Shift JIS 2-byte scope. |
| 48 | +// Ranges are verified by checking start, end, and immediate out-of-range |
| 49 | +// neighbours as separate boundary tests below. |
| 50 | +const UAX50_R = [ |
| 51 | + 0x2010, // ‐ HYPHEN (General Punctuation, 2010..2015 ; R) |
| 52 | + 0x2015, // ― HORIZONTAL BAR (General Punctuation, 2010..2015 ; R) |
| 53 | + 0x2025, // ‥ TWO DOT LEADER (General Punctuation, 2022..2027 ; R) |
| 54 | + 0x2225, // ∥ PARALLEL TO (Mathematical Operators, 221F..2233 ; R) |
| 55 | + 0x2190, // ← LEFTWARDS ARROW (Arrows, 2190..2194 ; R — we cover 2190..2193) |
| 56 | + 0x2191, // ↑ UPWARDS ARROW |
| 57 | + 0x2192, // → RIGHTWARDS ARROW |
| 58 | + 0x2193, // ↓ DOWNWARDS ARROW |
| 59 | + 0xFF1D, // = FULLWIDTH EQUALS SIGN (HF Forms, FF1C..FF1E ; R) |
| 60 | + // Box Drawing block — UAX #50 assigns 2500..257F ; R in its entirety. |
| 61 | + // We support 2500..254B (the characters listed in the Shift JIS 2-byte standard). |
| 62 | + // The full range is exercised in the dedicated loop test below. |
| 63 | +]; |
| 64 | + |
| 65 | +// Characters where we intentionally deviate from UAX #50. |
| 66 | +// These are Shift JIS 2-byte convention overrides: Japanese typography rotates |
| 67 | +// them in vertical text even though UAX #50 says upright. |
| 68 | +const SHIFT_JIS_OVERRIDES = [ |
| 69 | + {char: 0x3013, uaxVo: 'U', name: '〓 GETA MARK'}, |
| 70 | + {char: 0xFF0E, uaxVo: 'Tu', name: '. FULLWIDTH FULL STOP'}, |
| 71 | +]; |
| 72 | + |
| 73 | +// Characters that must NOT return true from needsRotationInVerticalMode. |
| 74 | +// Covers three categories: |
| 75 | +// a) UAX #50 vo=U (always upright) — CJK ideographs, kana, etc. |
| 76 | +// b) UAX #50 vo=Tr, but handled by verticalizedCharacterMap substitution |
| 77 | +// instead (glyph swapped before shaping; needsRotation is never consulted) |
| 78 | +// c) UAX #50 vo=R, but outside the Shift JIS 2-byte scope we support |
| 79 | +const MUST_NOT_ROTATE = [ |
| 80 | + // (a) UAX #50 vo=U — upright CJK and kana |
| 81 | + {char: 0x30A2, uaxVo: 'U', name: 'ア KATAKANA LETTER A'}, |
| 82 | + {char: 0x3042, uaxVo: 'U', name: 'あ HIRAGANA LETTER A'}, |
| 83 | + {char: 0x4E2D, uaxVo: 'U', name: '中 CJK UNIFIED IDEOGRAPH'}, |
| 84 | + {char: 0x3012, uaxVo: 'U', name: '〒 POSTAL MARK'}, |
| 85 | + |
| 86 | + // (b) UAX #50 vo=Tr — handled by glyph substitution in verticalize_punctuation.ts |
| 87 | + {char: 0x3014, uaxVo: 'Tr', name: '〔 LEFT TORTOISE SHELL BRACKET (substituted to ︹)'}, |
| 88 | + {char: 0x3015, uaxVo: 'Tr', name: '〕 RIGHT TORTOISE SHELL BRACKET (substituted to ︺)'}, |
| 89 | + {char: 0xFF08, uaxVo: 'Tr', name: '( FULLWIDTH LEFT PARENTHESIS (substituted to ︵)'}, |
| 90 | + |
| 91 | + // (c) UAX #50 vo=R — outside Shift JIS 2-byte scope |
| 92 | + {char: 0x0041, uaxVo: 'R', name: 'A LATIN CAPITAL LETTER A'}, |
| 93 | + {char: 0x0061, uaxVo: 'R', name: 'a LATIN SMALL LETTER A'}, |
| 94 | + {char: 0x2013, uaxVo: 'R', name: '– EN DASH (handled by verticalizedCharacterMap)'}, |
| 95 | + {char: 0x2014, uaxVo: 'R', name: '— EM DASH (handled by verticalizedCharacterMap)'}, |
| 96 | + {char: 0x2194, uaxVo: 'R', name: '↔ LEFT RIGHT ARROW (just outside 2190–2193 range)'}, |
| 97 | + {char: 0x254C, uaxVo: 'R', name: '╌ LIGHT DOUBLE DASH HORIZONTAL (just outside 2500–254B range)'}, |
| 98 | + {char: 0x2580, uaxVo: 'R', name: '▀ UPPER HALF BLOCK (Block Elements, not Box Drawing)'}, |
| 99 | +]; |
| 100 | + |
| 101 | +// --------------------------------------------------------------------------- |
| 102 | + |
| 103 | +describe('needsRotationInVerticalMode', () => { |
| 104 | + |
| 105 | + describe('UAX #50 vo=Tr characters — rotate as fallback (no vert glyph support)', () => { |
| 106 | + for (const char of UAX50_Tr) { |
| 107 | + test(`U+${char.toString(16).toUpperCase().padStart(4, '0')}`, () => { |
| 108 | + expect(needsRotationInVerticalMode(char)).toBe(true); |
| 109 | + }); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + describe('UAX #50 vo=R characters — Shift JIS 2-byte scope', () => { |
| 114 | + for (const char of UAX50_R) { |
| 115 | + test(`U+${char.toString(16).toUpperCase().padStart(4, '0')}`, () => { |
| 116 | + expect(needsRotationInVerticalMode(char)).toBe(true); |
| 117 | + }); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + describe('UAX #50 vo=R — Box Drawing block U+2500–U+254B (Shift JIS 2-byte subset)', () => { |
| 122 | + test('all 76 codepoints in range return true', () => { |
| 123 | + for (let char = 0x2500; char <= 0x254B; char++) { |
| 124 | + expect(needsRotationInVerticalMode(char)).toBe(true); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + test('U+24FF immediately before range — must not rotate', () => { |
| 129 | + expect(needsRotationInVerticalMode(0x24FF)).toBe(false); |
| 130 | + }); |
| 131 | + |
| 132 | + test('U+254C immediately after range — must not rotate', () => { |
| 133 | + expect(needsRotationInVerticalMode(0x254C)).toBe(false); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('UAX #50 vo=R — Directional Arrows boundary check', () => { |
| 138 | + test('U+218F immediately before U+2190–U+2193 range — must not rotate', () => { |
| 139 | + expect(needsRotationInVerticalMode(0x218F)).toBe(false); |
| 140 | + }); |
| 141 | + |
| 142 | + test('U+2194 immediately after range — must not rotate', () => { |
| 143 | + expect(needsRotationInVerticalMode(0x2194)).toBe(false); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('Shift JIS overrides — rotate despite UAX #50 saying upright', () => { |
| 148 | + for (const {char, uaxVo, name} of SHIFT_JIS_OVERRIDES) { |
| 149 | + test(`U+${char.toString(16).toUpperCase().padStart(4, '0')} ${name} (UAX #50 vo=${uaxVo}, Shift JIS convention overrides)`, () => { |
| 150 | + expect(needsRotationInVerticalMode(char)).toBe(true); |
| 151 | + }); |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + describe('characters that must NOT rotate', () => { |
| 156 | + for (const {char, uaxVo, name} of MUST_NOT_ROTATE) { |
| 157 | + test(`U+${char.toString(16).toUpperCase().padStart(4, '0')} ${name} (UAX #50 vo=${uaxVo})`, () => { |
| 158 | + expect(needsRotationInVerticalMode(char)).toBe(false); |
| 159 | + }); |
| 160 | + } |
| 161 | + }); |
| 162 | +}); |
0 commit comments