-
-
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.
Fixes #44
- Loading branch information
Showing
18 changed files
with
243 additions
and
47 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 not shown.
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
Binary file not shown.
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,67 @@ | ||
import React from 'react'; | ||
import { Document, Page, Text, Font } from '../src'; | ||
import MockDate from 'mockdate'; | ||
import render from './testRenderer'; | ||
|
||
const oswaldUrl = | ||
'https://fonts.gstatic.com/s/oswald/v13/Y_TKV6o8WovbUd3m_X9aAA.ttf'; | ||
|
||
const lorem = | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. \ | ||
Proin bibendum, diam non dictum rutrum, ligula velit molestie leo, sit \ | ||
amet suscipit purus ipsum et ligula. Cras placerat, tellus fringilla viverra \ | ||
maximus, ex metus vulputate ante, finibus dapibus eros dolor fermentum massa.'; | ||
|
||
describe('Font', () => { | ||
beforeEach(() => { | ||
MockDate.set(1434319925275); | ||
|
||
// pdfkit generates a random tag for internal font name, | ||
// so we mock Math.random to *not* be random | ||
const mockMath = Object.create(global.Math); | ||
mockMath.random = () => 0.5; | ||
global.Math = mockMath; | ||
}); | ||
|
||
afterEach(() => { | ||
Font.clear(); | ||
}); | ||
|
||
const matchSnapshot = (doc, done) => | ||
render(<Document>{doc}</Document>).then(result => { | ||
expect(result).toMatchSnapshot(); | ||
done(); | ||
}); | ||
|
||
test('should be able to register font families', () => { | ||
Font.register('src', { family: 'MyFont' }); | ||
Font.register('src', { family: 'MyOtherFont' }); | ||
|
||
expect(Font.getRegisteredFonts()).toEqual(['MyFont', 'MyOtherFont']); | ||
}); | ||
|
||
test('should be able to clear registered fonts', () => { | ||
Font.register('src', { family: 'MyFont' }); | ||
|
||
expect(Font.getRegisteredFonts()).toEqual(['MyFont']); | ||
|
||
Font.clear(); | ||
|
||
expect(Font.getRegisteredFonts()).toEqual([]); | ||
}); | ||
|
||
test('should be able to load font from url', done => { | ||
Font.register(oswaldUrl, { family: 'Oswald' }); | ||
|
||
matchSnapshot( | ||
<Page> | ||
<Text style={{ fontFamily: 'Oswald' }}> | ||
{lorem} | ||
</Text> | ||
</Page>, | ||
done, | ||
); | ||
}); | ||
|
||
test('should be able to load a font from file'); | ||
}); |
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
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,44 @@ | ||
import isUrl from 'is-url'; | ||
import standardFonts from './standard'; | ||
import { fetchFont } from '../utils/font'; | ||
|
||
let fonts = {}; | ||
|
||
const register = (src, { family, ...otherOptions }) => { | ||
fonts[family] = { | ||
src, | ||
loaded: false, | ||
...otherOptions, | ||
}; | ||
}; | ||
|
||
const getRegisteredFonts = () => Object.keys(fonts); | ||
|
||
const load = async (fontFamily, doc) => { | ||
const font = fonts[fontFamily]; | ||
|
||
if (font && !font.loaded) { | ||
font.loaded = true; | ||
|
||
const src = isUrl(font.src) ? await fetchFont(font.src) : font.src; | ||
|
||
doc.registerFont(fontFamily, src); | ||
} | ||
|
||
if (!font && !standardFonts.includes(fontFamily)) { | ||
throw new Error( | ||
`Font familiy not registered: ${fontFamily}. Please register it calling Font.register() method.`, | ||
); | ||
} | ||
}; | ||
|
||
const clear = () => { | ||
fonts = {}; | ||
}; | ||
|
||
export default { | ||
register, | ||
getRegisteredFonts, | ||
load, | ||
clear, | ||
}; |
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,16 @@ | ||
export default [ | ||
'Courier', | ||
'Courier-Bold', | ||
'Courier-Oblique', | ||
'Courier-BoldOblique', | ||
'Helvetica', | ||
'Helvetica-Bold', | ||
'Helvetica-Oblique', | ||
'Helvetica-BoldOblique', | ||
'Times-Roman', | ||
'Times-Bold', | ||
'Times-Italic', | ||
'Times-BoldItalic', | ||
'Symbol', | ||
'ZapfDingbats', | ||
]; |
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,19 @@ | ||
const request = require('request'); | ||
|
||
export const fetchFont = src => | ||
new Promise((resolve, reject) => { | ||
request( | ||
{ | ||
url: src, | ||
method: 'GET', | ||
encoding: null, | ||
}, | ||
(error, response, body) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
|
||
return resolve(body); | ||
}, | ||
); | ||
}); |
Oops, something went wrong.