Skip to content

Commit

Permalink
Streamlined letter spacing check
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Aug 27, 2024
1 parent 31e8536 commit b6bbc6b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/util/script_detection.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {charAllowsIdeographicBreaking, charAllowsLetterSpacing, charHasUprightVerticalOrientation, charInComplexShapingScript, stringContainsRTLText} from './script_detection';
import {allowsLetterSpacing, charAllowsIdeographicBreaking, charHasUprightVerticalOrientation, charInComplexShapingScript, stringContainsRTLText} from './script_detection';

describe('charAllowsIdeographicBreaking', () => {
test('disallows ideographic breaking of Latin text', () => {
Expand Down Expand Up @@ -35,24 +35,24 @@ describe('charAllowsIdeographicBreaking', () => {
});
});

describe('charAllowsLetterSpacing', () => {
describe('allowsLetterSpacing', () => {
test('allows letter spacing of Latin text', () => {
expect(charAllowsLetterSpacing('A'.codePointAt(0))).toBe(true);
expect(allowsLetterSpacing('A')).toBe(true);
});

test('disallows ideographic breaking of Arabic text', () => {
// Arabic
expect(charAllowsLetterSpacing('۳'.codePointAt(0))).toBe(false);
expect(allowsLetterSpacing('۳')).toBe(false);
// Arabic Supplement
expect(charAllowsLetterSpacing('ݣ'.codePointAt(0))).toBe(false);
expect(allowsLetterSpacing('ݣ')).toBe(false);
// Arabic Extended-A
expect(charAllowsLetterSpacing('ࢳ'.codePointAt(0))).toBe(false);
expect(allowsLetterSpacing('ࢳ')).toBe(false);
// Arabic Extended-B
expect(charAllowsLetterSpacing('࢐'.codePointAt(0))).toBe(false);
expect(allowsLetterSpacing('࢐')).toBe(false);
// Arabic Presentation Forms-A
expect(charAllowsLetterSpacing('ﰤ'.codePointAt(0))).toBe(false);
expect(allowsLetterSpacing('ﰤ')).toBe(false);
// Arabic Presentation Forms-B
expect(charAllowsLetterSpacing('ﺽ'.codePointAt(0))).toBe(false);
expect(allowsLetterSpacing('ﺽ')).toBe(false);
});
});

Expand Down
9 changes: 1 addition & 8 deletions src/util/script_detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ export function allowsVerticalWritingMode(chars: string) {
}

export function allowsLetterSpacing(chars: string) {
for (const char of chars) {
if (!charAllowsLetterSpacing(char.codePointAt(0))) return false;
}
return true;
return !cursiveScriptRegExp.test(chars);
}

/**
Expand Down Expand Up @@ -87,10 +84,6 @@ const cursiveScriptCodes = [

const cursiveScriptRegExp = sanitizedRegExpFromScriptCodes(cursiveScriptCodes);

export function charAllowsLetterSpacing(char: number) {
return !cursiveScriptRegExp.test(String.fromCodePoint(char));
}

/**
* ISO 15924 script codes of scripts that allow ideographic line breaking beyond
* the CJKV scripts that are considered ideographic in Unicode 16.0.0.
Expand Down

0 comments on commit b6bbc6b

Please sign in to comment.