forked from diegomura/react-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for verticalAlign "super" and "sub" (diegomura#2214)
* implement support for verticalAlign: super/sub * add tests for verticalAlignment * add verticalAlign to types * removed font scaling * add snapshot test * changeset --------- Co-authored-by: Dmitry Ivakhnenko <[email protected]>
- Loading branch information
1 parent
b194b61
commit 2db67a3
Showing
10 changed files
with
112 additions
and
0 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,7 @@ | ||
--- | ||
'@react-pdf/textkit': minor | ||
'@react-pdf/layout': minor | ||
'@react-pdf/types': minor | ||
--- | ||
|
||
added base support for verticalAlign "super" and "sub" |
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
Binary file added
BIN
+1.82 KB
...pshots/text-test-js-text-should-support-vertical-align-super-and-sub-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,24 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
|
||
/** | ||
* Apply scaling and yOffset for verticalAlign 'sub' and 'super'. | ||
* | ||
* @param {Object} layout options | ||
* @param {Object} attributed string | ||
* @return {Object} attributed string | ||
*/ | ||
const verticalAlignment = () => attributedString => { | ||
attributedString.runs.forEach(run => { | ||
const { attributes } = run; | ||
const { verticalAlign } = attributes; | ||
|
||
if (verticalAlign === 'sub') { | ||
attributes.yOffset = -0.2; | ||
} else if (verticalAlign === 'super') { | ||
attributes.yOffset = 0.4; | ||
} | ||
}); | ||
return attributedString; | ||
}; | ||
|
||
export default verticalAlignment; |
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,56 @@ | ||
import verticalAlignment from '../../src/layout/verticalAlign'; | ||
|
||
describe('verticalAlign', () => { | ||
test('should apply vertical alignment "super" to string', () => { | ||
const instance = verticalAlignment(); | ||
const text = { | ||
string: 'Lorem', | ||
runs: [ | ||
{ | ||
start: 0, | ||
end: 5, | ||
attributes: { fontSize: 12, color: 'red', verticalAlign: 'super' }, | ||
}, | ||
], | ||
}; | ||
|
||
const result = instance(text); | ||
// expect yOffset to be 0.4 | ||
expect(result.runs[0].attributes).toHaveProperty('yOffset', 0.4); | ||
}); | ||
test('should apply vertical alignment "sub" to string', () => { | ||
const instance = verticalAlignment(); | ||
const text = { | ||
string: 'Lorem', | ||
runs: [ | ||
{ | ||
start: 0, | ||
end: 5, | ||
attributes: { fontSize: 12, color: 'red', verticalAlign: 'sub' }, | ||
}, | ||
], | ||
}; | ||
|
||
const result = instance(text); | ||
// expect yOffset to be 0.4 | ||
expect(result.runs[0].attributes).toHaveProperty('yOffset', -0.2); | ||
}); | ||
|
||
test('should ignore vertical alignment value if it is not "sub" or "super"', () => { | ||
const instance = verticalAlignment(); | ||
const text = { | ||
string: 'Lorem', | ||
runs: [ | ||
{ | ||
start: 0, | ||
end: 5, | ||
attributes: { fontSize: 12, color: 'red', verticalAlign: 'center' }, | ||
}, | ||
], | ||
}; | ||
|
||
const result = instance(text); | ||
// expect yOffset property to be undefined | ||
expect(result.runs[0].attributes).not.toHaveProperty('yOffset'); | ||
}); | ||
}); |
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