Skip to content

Commit

Permalink
feat: function as emoji source (#2252)
Browse files Browse the repository at this point in the history
* implement function as emoji source to build more complex urls

* change set
  • Loading branch information
jeetiss authored Mar 16, 2023
1 parent 6eadee7 commit 47e91cb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/moody-suns-smash.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
---

implement function as emoji source to build more complex urls
4 changes: 2 additions & 2 deletions packages/font/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function FontStore() {
}
};

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

this.registerHyphenationCallback = callback => {
Expand Down
8 changes: 6 additions & 2 deletions packages/layout/src/text/emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ const getCodePoints = string =>
.join('-');

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

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

export const fetchEmojis = (string, source) => {
if (!source || !source.url) return [];
if (!source || (!source.url && !source.builder)) return [];

const promises = [];

Expand Down
21 changes: 21 additions & 0 deletions packages/renderer/tests/emoji.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import renderToImage from './renderComponent';
import { Document, Page, Text, Font } from '..';

Font.registerEmojiSource({
builder: code =>
`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_3d.png`,
});

describe('emoji', () => {
test('should support builder function', async () => {
const image = await renderToImage(
<Document>
<Page size={[100, 100]}>
<Text style={{ fontSize: 80 }}>💩</Text>
</Page>
</Document>,
);

expect(image).toMatchImageSnapshot();
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions packages/types/font.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ interface RegisteredFont {
[key: string]: any;
}

interface EmojiSource {
interface EmojiSourceUrl {
url: string;
format: string;
format?: string;
}

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

type EmojiSource = EmojiSourceUrl | EmojiSourceBuilder;

interface SingleLoad {
family: string;
src: string;
Expand Down

0 comments on commit 47e91cb

Please sign in to comment.