-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: font selection regression (#2747)
- Loading branch information
1 parent
f64f3bd
commit 5af35ec
Showing
4 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@react-pdf/examples": patch | ||
"@react-pdf/layout": patch | ||
--- | ||
|
||
fix: font selection regression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import fontSubstitution from '../../src/text/fontSubstitution'; | ||
|
||
const instance = fontSubstitution(); | ||
|
||
describe('FontSubstitution', () => { | ||
test('should return empty array if no runs passed', () => { | ||
const string = instance({ string: '', runs: [] }); | ||
|
||
expect(string).toHaveProperty('runs', []); | ||
expect(string).toHaveProperty('string', ''); | ||
}); | ||
|
||
test('should merge consecutive runs with same font', () => { | ||
const run1 = { start: 0, end: 3, attributes: { font: ['Helvetica'] } }; | ||
const run2 = { start: 3, end: 5, attributes: { font: ['Helvetica'] } }; | ||
const string = instance({ string: 'Lorem', runs: [run1, run2] }); | ||
|
||
expect(string).toHaveProperty('string', 'Lorem'); | ||
expect(string.runs).toHaveLength(1); | ||
expect(string.runs[0]).toHaveProperty('start', 0); | ||
expect(string.runs[0]).toHaveProperty('end', 5); | ||
expect(string.runs[0].attributes.font.name).toBe('Helvetica'); | ||
}); | ||
|
||
test('should substitute many runs', () => { | ||
const run1 = { start: 0, end: 3, attributes: { font: ['Courier'] } }; | ||
const run2 = { start: 3, end: 5, attributes: { font: ['Helvetica'] } }; | ||
const string = instance({ string: 'Lorem', runs: [run1, run2] }); | ||
|
||
expect(string).toHaveProperty('string', 'Lorem'); | ||
expect(string.runs).toHaveLength(2); | ||
expect(string.runs[0]).toHaveProperty('start', 0); | ||
expect(string.runs[0]).toHaveProperty('end', 3); | ||
expect(string.runs[0].attributes.font.name).toBe('Courier'); | ||
expect(string.runs[1]).toHaveProperty('start', 3); | ||
expect(string.runs[1]).toHaveProperty('end', 5); | ||
expect(string.runs[1].attributes.font.name).toBe('Helvetica'); | ||
}); | ||
|
||
describe('Fallback Font', () => { | ||
const SimplifiedChineseFont = { | ||
name: 'SimplifiedChineseFont', | ||
hasGlyphForCodePoint: (codePoint) => codePoint === 20320, | ||
}; | ||
|
||
test('should utilize a fallback font that supports the provided glyph', () => { | ||
const run = { | ||
start: 0, | ||
end: 1, | ||
attributes: { | ||
font: ['Courier', SimplifiedChineseFont], | ||
}, | ||
}; | ||
|
||
const string = instance({ string: '你', runs: [run] }); | ||
|
||
expect(string).toHaveProperty('string', '你'); | ||
expect(string.runs).toHaveLength(1); | ||
expect(string.runs[0]).toHaveProperty('start', 0); | ||
expect(string.runs[0]).toHaveProperty('end', 1); | ||
expect(string.runs[0].attributes.font.name).toBe( | ||
SimplifiedChineseFont.name, | ||
); | ||
}); | ||
|
||
test('should split a run when fallback font is used on a portion of the run', () => { | ||
const run = { | ||
start: 0, | ||
end: 2, | ||
attributes: { | ||
font: ['Courier', SimplifiedChineseFont], | ||
}, | ||
}; | ||
|
||
const string = instance({ string: 'A你', runs: [run] }); | ||
|
||
expect(string).toHaveProperty('string', 'A你'); | ||
expect(string.runs).toHaveLength(2); | ||
expect(string.runs[0]).toHaveProperty('start', 0); | ||
expect(string.runs[0]).toHaveProperty('end', 1); | ||
expect(string.runs[0].attributes.font.name).toBe('Courier'); | ||
expect(string.runs[1]).toHaveProperty('start', 1); | ||
expect(string.runs[1]).toHaveProperty('end', 2); | ||
expect(string.runs[1].attributes.font.name).toBe( | ||
SimplifiedChineseFont.name, | ||
); | ||
}); | ||
}); | ||
}); |