@react-pdf/font
Advanced tools
Comparing version 2.5.1 to 2.5.2
@@ -1,5 +0,1 @@ | ||
import _regeneratorRuntime from '@babel/runtime/helpers/regeneratorRuntime'; | ||
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator'; | ||
import _extends from '@babel/runtime/helpers/extends'; | ||
import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose'; | ||
import 'is-url'; | ||
@@ -9,4 +5,5 @@ import fetch from 'cross-fetch'; | ||
var _excluded = ["src", "fontWeight", "fontStyle"]; | ||
var FONT_WEIGHTS = { | ||
/* eslint-disable max-classes-per-file */ | ||
const FONT_WEIGHTS = { | ||
thin: 100, | ||
@@ -27,41 +24,19 @@ hairline: 100, | ||
}; | ||
var fetchFont = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(src, options) { | ||
var response, data; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return fetch(src, options); | ||
case 2: | ||
response = _context.sent; | ||
_context.next = 5; | ||
return response.arrayBuffer(); | ||
case 5: | ||
data = _context.sent; | ||
return _context.abrupt("return", new Uint8Array(data)); | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function fetchFont(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var isDataUrl = function isDataUrl(dataUrl) { | ||
var header = dataUrl.split(',')[0]; | ||
var hasDataPrefix = header.substring(0, 5) === 'data:'; | ||
var hasBase64Prefix = header.split(';')[1] === 'base64'; | ||
const fetchFont = async (src, options) => { | ||
const response = await fetch(src, options); | ||
const data = await response.arrayBuffer(); | ||
return new Uint8Array(data); | ||
}; | ||
const isDataUrl = dataUrl => { | ||
const header = dataUrl.split(',')[0]; | ||
const hasDataPrefix = header.substring(0, 5) === 'data:'; | ||
const hasBase64Prefix = header.split(';')[1] === 'base64'; | ||
return hasDataPrefix && hasBase64Prefix; | ||
}; | ||
var resolveFontWeight = function resolveFontWeight(value) { | ||
const resolveFontWeight = value => { | ||
return typeof value === 'string' ? FONT_WEIGHTS[value] : value; | ||
}; | ||
var sortByFontWeight = function sortByFontWeight(a, b) { | ||
return a.fontWeight - b.fontWeight; | ||
}; | ||
var FontSource = /*#__PURE__*/function () { | ||
function FontSource(src, fontFamily, fontStyle, fontWeight, options) { | ||
const sortByFontWeight = (a, b) => a.fontWeight - b.fontWeight; | ||
class FontSource { | ||
constructor(src, fontFamily, fontStyle, fontWeight, options) { | ||
this.src = src; | ||
@@ -75,124 +50,68 @@ this.fontFamily = fontFamily; | ||
} | ||
var _proto = FontSource.prototype; | ||
_proto._load = /*#__PURE__*/function () { | ||
var _load2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { | ||
var postscriptName, raw, uint8Array, _this$options, headers, body, _this$options$method, method, data; | ||
return _regeneratorRuntime().wrap(function _callee2$(_context2) { | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
postscriptName = this.options.postscriptName; | ||
if (!isDataUrl(this.src)) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
raw = this.src.split(',')[1]; | ||
uint8Array = new Uint8Array(atob(raw).split('').map(function (c) { | ||
return c.charCodeAt(0); | ||
})); | ||
this.data = fontkit.create(uint8Array, postscriptName); | ||
_context2.next = 19; | ||
break; | ||
case 7: | ||
_this$options = this.options, headers = _this$options.headers, body = _this$options.body, _this$options$method = _this$options.method, method = _this$options$method === void 0 ? 'GET' : _this$options$method; | ||
_context2.next = 11; | ||
return fetchFont(this.src, { | ||
method: method, | ||
body: body, | ||
headers: headers | ||
}); | ||
case 11: | ||
data = _context2.sent; | ||
this.data = fontkit.create(data, postscriptName); | ||
_context2.next = 19; | ||
break; | ||
case 15: | ||
{ | ||
_context2.next = 19; | ||
break; | ||
} | ||
case 18: | ||
this.data = _context2.sent; | ||
case 19: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}, _callee2, this); | ||
})); | ||
function _load() { | ||
return _load2.apply(this, arguments); | ||
async _load() { | ||
const { | ||
postscriptName | ||
} = this.options; | ||
if (isDataUrl(this.src)) { | ||
const raw = this.src.split(',')[1]; | ||
const uint8Array = new Uint8Array(atob(raw).split('').map(c => c.charCodeAt(0))); | ||
this.data = fontkit.create(uint8Array, postscriptName); | ||
} else { | ||
const { | ||
headers, | ||
body, | ||
method = 'GET' | ||
} = this.options; | ||
const data = await fetchFont(this.src, { | ||
method, | ||
body, | ||
headers | ||
}); | ||
this.data = fontkit.create(data, postscriptName); | ||
} | ||
return _load; | ||
}(); | ||
_proto.load = /*#__PURE__*/function () { | ||
var _load3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { | ||
return _regeneratorRuntime().wrap(function _callee3$(_context3) { | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (this.loadResultPromise === null) { | ||
this.loadResultPromise = this._load(); | ||
} | ||
return _context3.abrupt("return", this.loadResultPromise); | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}, _callee3, this); | ||
})); | ||
function load() { | ||
return _load3.apply(this, arguments); | ||
} | ||
async load() { | ||
if (this.loadResultPromise === null) { | ||
this.loadResultPromise = this._load(); | ||
} | ||
return load; | ||
}(); | ||
return FontSource; | ||
}(); | ||
var Font = /*#__PURE__*/function () { | ||
Font.create = function create(family) { | ||
return this.loadResultPromise; | ||
} | ||
} | ||
class Font { | ||
static create(family) { | ||
return new Font(family); | ||
}; | ||
function Font(family) { | ||
} | ||
constructor(family) { | ||
this.family = family; | ||
this.sources = []; | ||
} | ||
var _proto2 = Font.prototype; | ||
_proto2.register = function register(_ref2) { | ||
var src = _ref2.src, | ||
fontWeight = _ref2.fontWeight, | ||
fontStyle = _ref2.fontStyle, | ||
options = _objectWithoutPropertiesLoose(_ref2, _excluded); | ||
var numericFontWeight = resolveFontWeight(fontWeight); | ||
register(_ref) { | ||
let { | ||
src, | ||
fontWeight, | ||
fontStyle, | ||
...options | ||
} = _ref; | ||
const numericFontWeight = resolveFontWeight(fontWeight); | ||
this.sources.push(new FontSource(src, this.family, fontStyle, numericFontWeight, options)); | ||
}; | ||
_proto2.resolve = function resolve(descriptor) { | ||
var _descriptor$fontWeigh = descriptor.fontWeight, | ||
fontWeight = _descriptor$fontWeigh === void 0 ? 400 : _descriptor$fontWeigh, | ||
_descriptor$fontStyle = descriptor.fontStyle, | ||
fontStyle = _descriptor$fontStyle === void 0 ? 'normal' : _descriptor$fontStyle; | ||
var styleSources = this.sources.filter(function (s) { | ||
return s.fontStyle === fontStyle; | ||
}); | ||
} | ||
resolve(descriptor) { | ||
const { | ||
fontWeight = 400, | ||
fontStyle = 'normal' | ||
} = descriptor; | ||
const styleSources = this.sources.filter(s => s.fontStyle === fontStyle); | ||
// Weight resolution. https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights | ||
var exactFit = styleSources.find(function (s) { | ||
return s.fontWeight === fontWeight; | ||
}); | ||
const exactFit = styleSources.find(s => s.fontWeight === fontWeight); | ||
if (exactFit) return exactFit; | ||
var res; | ||
let res; | ||
if (fontWeight >= 400 && fontWeight <= 500) { | ||
var leftOffset = styleSources.filter(function (s) { | ||
return s.fontWeight <= fontWeight; | ||
}); | ||
var rightOffset = styleSources.filter(function (s) { | ||
return s.fontWeight > 500; | ||
}); | ||
var fit = styleSources.filter(function (s) { | ||
return s.fontWeight >= fontWeight && s.fontWeight < 500; | ||
}); | ||
const leftOffset = styleSources.filter(s => s.fontWeight <= fontWeight); | ||
const rightOffset = styleSources.filter(s => s.fontWeight > 500); | ||
const fit = styleSources.filter(s => s.fontWeight >= fontWeight && s.fontWeight < 500); | ||
res = fit[0] || leftOffset[leftOffset.length - 1] || rightOffset[0]; | ||
} | ||
var lt = styleSources.filter(function (s) { | ||
return s.fontWeight < fontWeight; | ||
}).sort(sortByFontWeight); | ||
var gt = styleSources.filter(function (s) { | ||
return s.fontWeight > fontWeight; | ||
}).sort(sortByFontWeight); | ||
const lt = styleSources.filter(s => s.fontWeight < fontWeight).sort(sortByFontWeight); | ||
const gt = styleSources.filter(s => s.fontWeight > fontWeight).sort(sortByFontWeight); | ||
if (fontWeight < 400) { | ||
@@ -208,5 +127,4 @@ res = lt[lt.length - 1] || gt[0]; | ||
return res; | ||
}; | ||
return Font; | ||
}(); | ||
} | ||
} | ||
@@ -216,8 +134,9 @@ var standard = ['Courier', 'Courier-Bold', 'Courier-Oblique', 'Courier-BoldOblique', 'Helvetica', 'Helvetica-Bold', 'Helvetica-Oblique', 'Helvetica-BoldOblique', 'Times-Roman', 'Times-Bold', 'Times-Italic', 'Times-BoldItalic']; | ||
function FontStore() { | ||
var _this = this; | ||
var fonts = {}; | ||
var emojiSource = null; | ||
var hyphenationCallback = null; | ||
this.register = function (data) { | ||
var family = data.family; | ||
let fonts = {}; | ||
let emojiSource = null; | ||
let hyphenationCallback = null; | ||
this.register = data => { | ||
const { | ||
family | ||
} = data; | ||
if (!fonts[family]) { | ||
@@ -229,6 +148,7 @@ fonts[family] = Font.create(family); | ||
if (data.fonts) { | ||
for (var i = 0; i < data.fonts.length; i += 1) { | ||
fonts[family].register(_extends({ | ||
family: family | ||
}, data.fonts[i])); | ||
for (let i = 0; i < data.fonts.length; i += 1) { | ||
fonts[family].register({ | ||
family, | ||
...data.fonts[i] | ||
}); | ||
} | ||
@@ -239,22 +159,24 @@ } else { | ||
}; | ||
this.registerEmojiSource = function (_ref) { | ||
var url = _ref.url, | ||
_ref$format = _ref.format, | ||
format = _ref$format === void 0 ? 'png' : _ref$format, | ||
builder = _ref.builder, | ||
_ref$withVariationSel = _ref.withVariationSelectors, | ||
withVariationSelectors = _ref$withVariationSel === void 0 ? false : _ref$withVariationSel; | ||
this.registerEmojiSource = _ref => { | ||
let { | ||
url, | ||
format = 'png', | ||
builder, | ||
withVariationSelectors = false | ||
} = _ref; | ||
emojiSource = { | ||
url: url, | ||
format: format, | ||
builder: builder, | ||
withVariationSelectors: withVariationSelectors | ||
url, | ||
format, | ||
builder, | ||
withVariationSelectors | ||
}; | ||
}; | ||
this.registerHyphenationCallback = function (callback) { | ||
this.registerHyphenationCallback = callback => { | ||
hyphenationCallback = callback; | ||
}; | ||
this.getFont = function (descriptor) { | ||
var fontFamily = descriptor.fontFamily; | ||
var isStandard = standard.includes(fontFamily); | ||
this.getFont = descriptor => { | ||
const { | ||
fontFamily | ||
} = descriptor; | ||
const isStandard = standard.includes(fontFamily); | ||
if (isStandard) return null; | ||
@@ -266,70 +188,36 @@ if (!fonts[fontFamily]) { | ||
}; | ||
this.load = /*#__PURE__*/function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(descriptor) { | ||
var fontFamily, fontFamilies, promises, len, i, family, isStandard, f; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
fontFamily = descriptor.fontFamily; | ||
fontFamilies = typeof fontFamily === 'string' ? [fontFamily] : [].concat(fontFamily || []); | ||
promises = []; | ||
len = fontFamilies.length, i = 0; | ||
case 4: | ||
if (!(i < len)) { | ||
_context.next = 14; | ||
break; | ||
} | ||
family = fontFamilies[i]; | ||
isStandard = standard.includes(family); | ||
if (!isStandard) { | ||
_context.next = 9; | ||
break; | ||
} | ||
return _context.abrupt("return"); | ||
case 9: | ||
f = _this.getFont(_extends({}, descriptor, { | ||
fontFamily: family | ||
})); | ||
promises.push(f.load()); | ||
case 11: | ||
i += 1; | ||
_context.next = 4; | ||
break; | ||
case 14: | ||
_context.next = 16; | ||
return Promise.all(promises); | ||
case 16: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function (_x) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
this.reset = function () { | ||
var keys = Object.keys(fonts); | ||
for (var i = 0; i < keys.length; i += 1) { | ||
var key = keys[i]; | ||
this.load = async descriptor => { | ||
const { | ||
fontFamily | ||
} = descriptor; | ||
const fontFamilies = typeof fontFamily === 'string' ? [fontFamily] : [...(fontFamily || [])]; | ||
const promises = []; | ||
for (let len = fontFamilies.length, i = 0; i < len; i += 1) { | ||
const family = fontFamilies[i]; | ||
const isStandard = standard.includes(family); | ||
if (isStandard) return; | ||
const f = this.getFont({ | ||
...descriptor, | ||
fontFamily: family | ||
}); | ||
promises.push(f.load()); | ||
} | ||
await Promise.all(promises); | ||
}; | ||
this.reset = () => { | ||
const keys = Object.keys(fonts); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
fonts[key].data = null; | ||
} | ||
}; | ||
this.clear = function () { | ||
this.clear = () => { | ||
fonts = {}; | ||
}; | ||
this.getRegisteredFonts = function () { | ||
return fonts; | ||
}; | ||
this.getEmojiSource = function () { | ||
return emojiSource; | ||
}; | ||
this.getHyphenationCallback = function () { | ||
return hyphenationCallback; | ||
}; | ||
this.getRegisteredFontFamilies = function () { | ||
return Object.keys(fonts); | ||
}; | ||
this.getRegisteredFonts = () => fonts; | ||
this.getEmojiSource = () => emojiSource; | ||
this.getHyphenationCallback = () => hyphenationCallback; | ||
this.getRegisteredFontFamilies = () => Object.keys(fonts); | ||
} | ||
export { FontStore as default }; |
370
lib/index.js
@@ -1,5 +0,1 @@ | ||
import _regeneratorRuntime from '@babel/runtime/helpers/regeneratorRuntime'; | ||
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator'; | ||
import _extends from '@babel/runtime/helpers/extends'; | ||
import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose'; | ||
import isUrl from 'is-url'; | ||
@@ -9,4 +5,5 @@ import fetch from 'cross-fetch'; | ||
var _excluded = ["src", "fontWeight", "fontStyle"]; | ||
var FONT_WEIGHTS = { | ||
/* eslint-disable max-classes-per-file */ | ||
const FONT_WEIGHTS = { | ||
thin: 100, | ||
@@ -27,41 +24,19 @@ hairline: 100, | ||
}; | ||
var fetchFont = /*#__PURE__*/function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(src, options) { | ||
var response, data; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return fetch(src, options); | ||
case 2: | ||
response = _context.sent; | ||
_context.next = 5; | ||
return response.arrayBuffer(); | ||
case 5: | ||
data = _context.sent; | ||
return _context.abrupt("return", new Uint8Array(data)); | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function fetchFont(_x, _x2) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
var isDataUrl = function isDataUrl(dataUrl) { | ||
var header = dataUrl.split(',')[0]; | ||
var hasDataPrefix = header.substring(0, 5) === 'data:'; | ||
var hasBase64Prefix = header.split(';')[1] === 'base64'; | ||
const fetchFont = async (src, options) => { | ||
const response = await fetch(src, options); | ||
const data = await response.arrayBuffer(); | ||
return new Uint8Array(data); | ||
}; | ||
const isDataUrl = dataUrl => { | ||
const header = dataUrl.split(',')[0]; | ||
const hasDataPrefix = header.substring(0, 5) === 'data:'; | ||
const hasBase64Prefix = header.split(';')[1] === 'base64'; | ||
return hasDataPrefix && hasBase64Prefix; | ||
}; | ||
var resolveFontWeight = function resolveFontWeight(value) { | ||
const resolveFontWeight = value => { | ||
return typeof value === 'string' ? FONT_WEIGHTS[value] : value; | ||
}; | ||
var sortByFontWeight = function sortByFontWeight(a, b) { | ||
return a.fontWeight - b.fontWeight; | ||
}; | ||
var FontSource = /*#__PURE__*/function () { | ||
function FontSource(src, fontFamily, fontStyle, fontWeight, options) { | ||
const sortByFontWeight = (a, b) => a.fontWeight - b.fontWeight; | ||
class FontSource { | ||
constructor(src, fontFamily, fontStyle, fontWeight, options) { | ||
this.src = src; | ||
@@ -75,126 +50,70 @@ this.fontFamily = fontFamily; | ||
} | ||
var _proto = FontSource.prototype; | ||
_proto._load = /*#__PURE__*/function () { | ||
var _load2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { | ||
var postscriptName, raw, uint8Array, _this$options, headers, body, _this$options$method, method, data; | ||
return _regeneratorRuntime().wrap(function _callee2$(_context2) { | ||
while (1) switch (_context2.prev = _context2.next) { | ||
case 0: | ||
postscriptName = this.options.postscriptName; | ||
if (!isDataUrl(this.src)) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
raw = this.src.split(',')[1]; | ||
uint8Array = new Uint8Array(atob(raw).split('').map(function (c) { | ||
return c.charCodeAt(0); | ||
})); | ||
this.data = fontkit.create(uint8Array, postscriptName); | ||
_context2.next = 19; | ||
break; | ||
case 7: | ||
if (!(isUrl(this.src))) { | ||
_context2.next = 15; | ||
break; | ||
} | ||
_this$options = this.options, headers = _this$options.headers, body = _this$options.body, _this$options$method = _this$options.method, method = _this$options$method === void 0 ? 'GET' : _this$options$method; | ||
_context2.next = 11; | ||
return fetchFont(this.src, { | ||
method: method, | ||
body: body, | ||
headers: headers | ||
}); | ||
case 11: | ||
data = _context2.sent; | ||
this.data = fontkit.create(data, postscriptName); | ||
_context2.next = 19; | ||
break; | ||
case 15: | ||
_context2.next = 18; | ||
return fontkit.open(this.src, postscriptName); | ||
case 18: | ||
this.data = _context2.sent; | ||
case 19: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
}, _callee2, this); | ||
})); | ||
function _load() { | ||
return _load2.apply(this, arguments); | ||
async _load() { | ||
const { | ||
postscriptName | ||
} = this.options; | ||
if (isDataUrl(this.src)) { | ||
const raw = this.src.split(',')[1]; | ||
const uint8Array = new Uint8Array(atob(raw).split('').map(c => c.charCodeAt(0))); | ||
this.data = fontkit.create(uint8Array, postscriptName); | ||
} else if (isUrl(this.src)) { | ||
const { | ||
headers, | ||
body, | ||
method = 'GET' | ||
} = this.options; | ||
const data = await fetchFont(this.src, { | ||
method, | ||
body, | ||
headers | ||
}); | ||
this.data = fontkit.create(data, postscriptName); | ||
} else { | ||
this.data = await fontkit.open(this.src, postscriptName); | ||
} | ||
return _load; | ||
}(); | ||
_proto.load = /*#__PURE__*/function () { | ||
var _load3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { | ||
return _regeneratorRuntime().wrap(function _callee3$(_context3) { | ||
while (1) switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (this.loadResultPromise === null) { | ||
this.loadResultPromise = this._load(); | ||
} | ||
return _context3.abrupt("return", this.loadResultPromise); | ||
case 2: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
}, _callee3, this); | ||
})); | ||
function load() { | ||
return _load3.apply(this, arguments); | ||
} | ||
async load() { | ||
if (this.loadResultPromise === null) { | ||
this.loadResultPromise = this._load(); | ||
} | ||
return load; | ||
}(); | ||
return FontSource; | ||
}(); | ||
var Font = /*#__PURE__*/function () { | ||
Font.create = function create(family) { | ||
return this.loadResultPromise; | ||
} | ||
} | ||
class Font { | ||
static create(family) { | ||
return new Font(family); | ||
}; | ||
function Font(family) { | ||
} | ||
constructor(family) { | ||
this.family = family; | ||
this.sources = []; | ||
} | ||
var _proto2 = Font.prototype; | ||
_proto2.register = function register(_ref2) { | ||
var src = _ref2.src, | ||
fontWeight = _ref2.fontWeight, | ||
fontStyle = _ref2.fontStyle, | ||
options = _objectWithoutPropertiesLoose(_ref2, _excluded); | ||
var numericFontWeight = resolveFontWeight(fontWeight); | ||
register(_ref) { | ||
let { | ||
src, | ||
fontWeight, | ||
fontStyle, | ||
...options | ||
} = _ref; | ||
const numericFontWeight = resolveFontWeight(fontWeight); | ||
this.sources.push(new FontSource(src, this.family, fontStyle, numericFontWeight, options)); | ||
}; | ||
_proto2.resolve = function resolve(descriptor) { | ||
var _descriptor$fontWeigh = descriptor.fontWeight, | ||
fontWeight = _descriptor$fontWeigh === void 0 ? 400 : _descriptor$fontWeigh, | ||
_descriptor$fontStyle = descriptor.fontStyle, | ||
fontStyle = _descriptor$fontStyle === void 0 ? 'normal' : _descriptor$fontStyle; | ||
var styleSources = this.sources.filter(function (s) { | ||
return s.fontStyle === fontStyle; | ||
}); | ||
} | ||
resolve(descriptor) { | ||
const { | ||
fontWeight = 400, | ||
fontStyle = 'normal' | ||
} = descriptor; | ||
const styleSources = this.sources.filter(s => s.fontStyle === fontStyle); | ||
// Weight resolution. https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights | ||
var exactFit = styleSources.find(function (s) { | ||
return s.fontWeight === fontWeight; | ||
}); | ||
const exactFit = styleSources.find(s => s.fontWeight === fontWeight); | ||
if (exactFit) return exactFit; | ||
var res; | ||
let res; | ||
if (fontWeight >= 400 && fontWeight <= 500) { | ||
var leftOffset = styleSources.filter(function (s) { | ||
return s.fontWeight <= fontWeight; | ||
}); | ||
var rightOffset = styleSources.filter(function (s) { | ||
return s.fontWeight > 500; | ||
}); | ||
var fit = styleSources.filter(function (s) { | ||
return s.fontWeight >= fontWeight && s.fontWeight < 500; | ||
}); | ||
const leftOffset = styleSources.filter(s => s.fontWeight <= fontWeight); | ||
const rightOffset = styleSources.filter(s => s.fontWeight > 500); | ||
const fit = styleSources.filter(s => s.fontWeight >= fontWeight && s.fontWeight < 500); | ||
res = fit[0] || leftOffset[leftOffset.length - 1] || rightOffset[0]; | ||
} | ||
var lt = styleSources.filter(function (s) { | ||
return s.fontWeight < fontWeight; | ||
}).sort(sortByFontWeight); | ||
var gt = styleSources.filter(function (s) { | ||
return s.fontWeight > fontWeight; | ||
}).sort(sortByFontWeight); | ||
const lt = styleSources.filter(s => s.fontWeight < fontWeight).sort(sortByFontWeight); | ||
const gt = styleSources.filter(s => s.fontWeight > fontWeight).sort(sortByFontWeight); | ||
if (fontWeight < 400) { | ||
@@ -210,5 +129,4 @@ res = lt[lt.length - 1] || gt[0]; | ||
return res; | ||
}; | ||
return Font; | ||
}(); | ||
} | ||
} | ||
@@ -218,8 +136,9 @@ var standard = ['Courier', 'Courier-Bold', 'Courier-Oblique', 'Courier-BoldOblique', 'Helvetica', 'Helvetica-Bold', 'Helvetica-Oblique', 'Helvetica-BoldOblique', 'Times-Roman', 'Times-Bold', 'Times-Italic', 'Times-BoldItalic']; | ||
function FontStore() { | ||
var _this = this; | ||
var fonts = {}; | ||
var emojiSource = null; | ||
var hyphenationCallback = null; | ||
this.register = function (data) { | ||
var family = data.family; | ||
let fonts = {}; | ||
let emojiSource = null; | ||
let hyphenationCallback = null; | ||
this.register = data => { | ||
const { | ||
family | ||
} = data; | ||
if (!fonts[family]) { | ||
@@ -231,6 +150,7 @@ fonts[family] = Font.create(family); | ||
if (data.fonts) { | ||
for (var i = 0; i < data.fonts.length; i += 1) { | ||
fonts[family].register(_extends({ | ||
family: family | ||
}, data.fonts[i])); | ||
for (let i = 0; i < data.fonts.length; i += 1) { | ||
fonts[family].register({ | ||
family, | ||
...data.fonts[i] | ||
}); | ||
} | ||
@@ -241,22 +161,24 @@ } else { | ||
}; | ||
this.registerEmojiSource = function (_ref) { | ||
var url = _ref.url, | ||
_ref$format = _ref.format, | ||
format = _ref$format === void 0 ? 'png' : _ref$format, | ||
builder = _ref.builder, | ||
_ref$withVariationSel = _ref.withVariationSelectors, | ||
withVariationSelectors = _ref$withVariationSel === void 0 ? false : _ref$withVariationSel; | ||
this.registerEmojiSource = _ref => { | ||
let { | ||
url, | ||
format = 'png', | ||
builder, | ||
withVariationSelectors = false | ||
} = _ref; | ||
emojiSource = { | ||
url: url, | ||
format: format, | ||
builder: builder, | ||
withVariationSelectors: withVariationSelectors | ||
url, | ||
format, | ||
builder, | ||
withVariationSelectors | ||
}; | ||
}; | ||
this.registerHyphenationCallback = function (callback) { | ||
this.registerHyphenationCallback = callback => { | ||
hyphenationCallback = callback; | ||
}; | ||
this.getFont = function (descriptor) { | ||
var fontFamily = descriptor.fontFamily; | ||
var isStandard = standard.includes(fontFamily); | ||
this.getFont = descriptor => { | ||
const { | ||
fontFamily | ||
} = descriptor; | ||
const isStandard = standard.includes(fontFamily); | ||
if (isStandard) return null; | ||
@@ -268,70 +190,36 @@ if (!fonts[fontFamily]) { | ||
}; | ||
this.load = /*#__PURE__*/function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(descriptor) { | ||
var fontFamily, fontFamilies, promises, len, i, family, isStandard, f; | ||
return _regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) switch (_context.prev = _context.next) { | ||
case 0: | ||
fontFamily = descriptor.fontFamily; | ||
fontFamilies = typeof fontFamily === 'string' ? [fontFamily] : [].concat(fontFamily || []); | ||
promises = []; | ||
len = fontFamilies.length, i = 0; | ||
case 4: | ||
if (!(i < len)) { | ||
_context.next = 14; | ||
break; | ||
} | ||
family = fontFamilies[i]; | ||
isStandard = standard.includes(family); | ||
if (!isStandard) { | ||
_context.next = 9; | ||
break; | ||
} | ||
return _context.abrupt("return"); | ||
case 9: | ||
f = _this.getFont(_extends({}, descriptor, { | ||
fontFamily: family | ||
})); | ||
promises.push(f.load()); | ||
case 11: | ||
i += 1; | ||
_context.next = 4; | ||
break; | ||
case 14: | ||
_context.next = 16; | ||
return Promise.all(promises); | ||
case 16: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
}, _callee); | ||
})); | ||
return function (_x) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
this.reset = function () { | ||
var keys = Object.keys(fonts); | ||
for (var i = 0; i < keys.length; i += 1) { | ||
var key = keys[i]; | ||
this.load = async descriptor => { | ||
const { | ||
fontFamily | ||
} = descriptor; | ||
const fontFamilies = typeof fontFamily === 'string' ? [fontFamily] : [...(fontFamily || [])]; | ||
const promises = []; | ||
for (let len = fontFamilies.length, i = 0; i < len; i += 1) { | ||
const family = fontFamilies[i]; | ||
const isStandard = standard.includes(family); | ||
if (isStandard) return; | ||
const f = this.getFont({ | ||
...descriptor, | ||
fontFamily: family | ||
}); | ||
promises.push(f.load()); | ||
} | ||
await Promise.all(promises); | ||
}; | ||
this.reset = () => { | ||
const keys = Object.keys(fonts); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
const key = keys[i]; | ||
fonts[key].data = null; | ||
} | ||
}; | ||
this.clear = function () { | ||
this.clear = () => { | ||
fonts = {}; | ||
}; | ||
this.getRegisteredFonts = function () { | ||
return fonts; | ||
}; | ||
this.getEmojiSource = function () { | ||
return emojiSource; | ||
}; | ||
this.getHyphenationCallback = function () { | ||
return hyphenationCallback; | ||
}; | ||
this.getRegisteredFontFamilies = function () { | ||
return Object.keys(fonts); | ||
}; | ||
this.getRegisteredFonts = () => fonts; | ||
this.getEmojiSource = () => emojiSource; | ||
this.getHyphenationCallback = () => hyphenationCallback; | ||
this.getRegisteredFontFamilies = () => Object.keys(fonts); | ||
} | ||
export { FontStore as default }; |
{ | ||
"name": "@react-pdf/font", | ||
"version": "2.5.1", | ||
"version": "2.5.2", | ||
"license": "MIT", | ||
@@ -34,3 +34,3 @@ "description": "Register font and emoji source for react-pdf document", | ||
"@babel/runtime": "^7.20.13", | ||
"@react-pdf/types": "^2.5.0", | ||
"@react-pdf/types": "^2.6.0", | ||
"cross-fetch": "^3.1.5", | ||
@@ -37,0 +37,0 @@ "fontkit": "^2.0.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27783
881
Updated@react-pdf/types@^2.6.0