Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add withVariationSelectors option to registerEmojiSource #2467

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/pretty-flies-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@react-pdf/layout': patch
'@react-pdf/types': patch
'@react-pdf/font': patch
---

feat: add withVariationSelectors option to registerEmojiSource [#2466](https://github.com/diegomura/react-pdf/issues/2466)
9 changes: 7 additions & 2 deletions packages/font/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ function FontStore() {
}
};

this.registerEmojiSource = ({ url, format = 'png', builder }) => {
emojiSource = { url, format, builder };
this.registerEmojiSource = ({
url,
format = 'png',
builder,
withVariationSelectors = false,
}) => {
emojiSource = { url, format, builder, withVariationSelectors };
};

this.registerHyphenationCallback = callback => {
Expand Down
14 changes: 7 additions & 7 deletions packages/layout/src/text/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const reflect = promise => (...args) =>
const makeFetchEmojiImage = () => reflect(resolveImage);

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

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

const buildEmojiUrl = (emoji, source) => {
const { url, format, builder } = source;
const { url, format, builder, withVariationSelectors } = source;
if (typeof builder === 'function') {
return builder(getCodePoints(emoji));
return builder(getCodePoints(emoji, withVariationSelectors));
}

return `${url}${getCodePoints(emoji)}.${format}`;
return `${url}${getCodePoints(emoji, withVariationSelectors)}.${format}`;
};

export const fetchEmojis = (string, source) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/types/font.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ interface RegisteredFont {
interface EmojiSourceUrl {
url: string;
format?: string;
withVariationSelectors?: boolean;
}

interface EmojiSourceBuilder {
builder: (code: string) => string;
withVariationSelectors?: boolean;
}

type EmojiSource = EmojiSourceUrl | EmojiSourceBuilder;
Expand Down