Skip to content

Commit 9458584

Browse files
committed
feat: add withVariationSelectors option to registerEmojiSource (#2467)
* feat: add withVariationSelectors option to registerEmojiSource fixed: #2466 * feat: update EmojiSource * chore: create changeset
1 parent 56cf948 commit 9458584

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

.changeset/pretty-flies-clean.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@react-pdf/layout': patch
3+
'@react-pdf/types': patch
4+
'@react-pdf/font': patch
5+
---
6+
7+
feat: add withVariationSelectors option to registerEmojiSource [#2466](https://github.com/diegomura/react-pdf/issues/2466)

packages/font/src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ function FontStore() {
2525
}
2626
};
2727

28-
this.registerEmojiSource = ({ url, format = 'png', builder }) => {
29-
emojiSource = { url, format, builder };
28+
this.registerEmojiSource = ({
29+
url,
30+
format = 'png',
31+
builder,
32+
withVariationSelectors = false,
33+
}) => {
34+
emojiSource = { url, format, builder, withVariationSelectors };
3035
};
3136

3237
this.registerHyphenationCallback = callback => {

packages/layout/src/text/emoji.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const reflect = promise => (...args) =>
1616
const makeFetchEmojiImage = () => reflect(resolveImage);
1717

1818
/**
19-
* When an emoji as no color, it might still have 2 parts,
19+
* When an emoji as no variations, it might still have 2 parts,
2020
* the canonical emoji and an empty string.
2121
* ex.
2222
* (no color) Array.from('❤️') => ["❤", "️"]
@@ -25,21 +25,21 @@ const makeFetchEmojiImage = () => reflect(resolveImage);
2525
* The empty string needs to be removed otherwise the generated
2626
* url will be incorect.
2727
*/
28-
const _removeNoColor = x => x !== '️';
28+
const _removeVariationSelectors = x => x !== '️';
2929

30-
const getCodePoints = string =>
30+
const getCodePoints = (string, withVariationSelectors) =>
3131
Array.from(string)
32-
.filter(_removeNoColor)
32+
.filter(withVariationSelectors ? () => true : _removeVariationSelectors)
3333
.map(char => char.codePointAt(0).toString(16))
3434
.join('-');
3535

3636
const buildEmojiUrl = (emoji, source) => {
37-
const { url, format, builder } = source;
37+
const { url, format, builder, withVariationSelectors } = source;
3838
if (typeof builder === 'function') {
39-
return builder(getCodePoints(emoji));
39+
return builder(getCodePoints(emoji, withVariationSelectors));
4040
}
4141

42-
return `${url}${getCodePoints(emoji)}.${format}`;
42+
return `${url}${getCodePoints(emoji, withVariationSelectors)}.${format}`;
4343
};
4444

4545
export const fetchEmojis = (string, source) => {

packages/types/font.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ interface RegisteredFont {
4949
interface EmojiSourceUrl {
5050
url: string;
5151
format?: string;
52+
withVariationSelectors?: boolean;
5253
}
5354

5455
interface EmojiSourceBuilder {
5556
builder: (code: string) => string;
57+
withVariationSelectors?: boolean;
5658
}
5759

5860
type EmojiSource = EmojiSourceUrl | EmojiSourceBuilder;

0 commit comments

Comments
 (0)