Spaces:
Sleeping
Sleeping
File size: 15,639 Bytes
391a73c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | import 'is-url';
import * as fontkit from 'fontkit';
import PDFDocument from '@react-pdf/pdfkit';
// @ts-expect-error ts being silly
const STANDARD_FONTS = [
'Courier',
'Courier-Bold',
'Courier-Oblique',
'Courier-BoldOblique',
'Helvetica',
'Helvetica-Bold',
'Helvetica-Oblique',
'Helvetica-BoldOblique',
'Times-Roman',
'Times-Bold',
'Times-Italic',
'Times-BoldItalic',
];
// Create a shared lightweight document for accessing standard font instances.
// Standard fonts are created once and cached, so this is negligible overhead.
let _sharedDoc = null;
const openStandardFont = (src) => {
if (!_sharedDoc) {
_sharedDoc = new PDFDocument({ autoFirstPage: false });
}
_sharedDoc.font(src);
return _sharedDoc._font;
};
class StandardFont {
name;
src;
fullName;
familyName;
subfamilyName;
postscriptName;
copyright;
version;
underlinePosition;
underlineThickness;
italicAngle;
bbox;
'OS/2';
hhea;
numGlyphs;
characterSet;
availableFeatures;
type;
constructor(src) {
this.name = src;
this.fullName = src;
this.familyName = src;
this.subfamilyName = src;
this.type = 'STANDARD';
this.postscriptName = src;
this.availableFeatures = [];
this.copyright = '';
this.version = 1;
this.underlinePosition = -100;
this.underlineThickness = 50;
this.italicAngle = 0;
this.bbox = {};
this['OS/2'] = {};
this.hhea = {};
this.numGlyphs = 0;
this.characterSet = [];
this.src = openStandardFont(src);
}
encode(str) {
const [encoded, positions] = this.src.encode(str);
// Soft hyphens (U+00AD) should have zero width for line breaking purposes.
// Upstream pdfkit maps them to 'hyphen' in AFM data, so we override here.
for (let i = 0; i < str.length; i++) {
if (str.charCodeAt(i) === 0x00ad) {
positions[i].advanceWidth = 0;
}
}
return [encoded, positions];
}
layout(str) {
const [encoded, positions] = this.encode(str);
const glyphs = encoded.map((g, i) => {
const glyph = this.getGlyph(parseInt(g, 16));
glyph.advanceWidth = positions[i].advanceWidth;
return glyph;
});
const advanceWidth = positions.reduce((acc, p) => acc + p.advanceWidth, 0);
return {
positions,
glyphs,
script: 'latin',
language: 'dflt',
direction: 'ltr',
features: {},
advanceWidth,
advanceHeight: 0,
bbox: undefined,
};
}
glyphForCodePoint(codePoint) {
const glyph = this.getGlyph(codePoint);
glyph.advanceWidth = 400;
return glyph;
}
getGlyph(id) {
return {
id,
codePoints: [id],
isLigature: false,
name: this.src.font.characterToGlyph(id),
_font: this.src,
// @ts-expect-error assign proper value
advanceWidth: undefined,
};
}
hasGlyphForCodePoint(codePoint) {
return this.src.font.characterToGlyph(codePoint) !== '.notdef';
}
// Based on empirical observation
get ascent() {
return 900;
}
// Based on empirical observation
get capHeight() {
switch (this.name) {
case 'Times-Roman':
case 'Times-Bold':
case 'Times-Italic':
case 'Times-BoldItalic':
return 650;
case 'Courier':
case 'Courier-Bold':
case 'Courier-Oblique':
case 'Courier-BoldOblique':
return 550;
default:
return 690;
}
}
// Based on empirical observation
get xHeight() {
switch (this.name) {
case 'Times-Roman':
case 'Times-Bold':
case 'Times-Italic':
case 'Times-BoldItalic':
return 440;
case 'Courier':
case 'Courier-Bold':
case 'Courier-Oblique':
case 'Courier-BoldOblique':
return 390;
default:
return 490;
}
}
// Based on empirical observation
get descent() {
switch (this.name) {
case 'Times-Roman':
case 'Times-Bold':
case 'Times-Italic':
case 'Times-BoldItalic':
return -220;
case 'Courier':
case 'Courier-Bold':
case 'Courier-Oblique':
case 'Courier-BoldOblique':
return -230;
default:
return -200;
}
}
get lineGap() {
return 0;
}
get unitsPerEm() {
return 1000;
}
stringsForGlyph() {
throw new Error('Method not implemented.');
}
glyphsForString() {
throw new Error('Method not implemented.');
}
widthOfGlyph() {
throw new Error('Method not implemented.');
}
getAvailableFeatures() {
throw new Error('Method not implemented.');
}
createSubset() {
throw new Error('Method not implemented.');
}
getVariation() {
throw new Error('Method not implemented.');
}
getFont() {
throw new Error('Method not implemented.');
}
getName() {
throw new Error('Method not implemented.');
}
setDefaultLanguage() {
throw new Error('Method not implemented.');
}
}
const fetchFont = async (src, options) => {
const response = await fetch(src, options);
if (!response.ok) {
throw new Error(`Failed to fetch font from ${src}: ${response.status} ${response.statusText}`);
}
const data = await response.arrayBuffer();
return new Uint8Array(data);
};
const isDataUrl = (dataUrl) => {
const commaIndex = dataUrl.indexOf(',');
if (commaIndex === -1)
return false;
const header = dataUrl.substring(0, commaIndex);
const hasDataPrefix = header.startsWith('data:');
const hasBase64Prefix = header.includes(';base64');
return hasDataPrefix && hasBase64Prefix;
};
class FontSource {
src;
fontFamily;
fontStyle;
fontWeight;
data;
options;
loadResultPromise;
constructor(src, fontFamily, fontStyle, fontWeight, options) {
this.src = src;
this.fontFamily = fontFamily;
this.fontStyle = fontStyle || 'normal';
this.fontWeight = fontWeight || 400;
this.data = null;
this.options = options || {};
this.loadResultPromise = null;
}
async _load() {
const { postscriptName } = this.options;
let data = null;
if (STANDARD_FONTS.includes(this.src)) {
data = new StandardFont(this.src);
}
else if (isDataUrl(this.src)) {
const raw = this.src.split(',')[1];
const uint8Array = new Uint8Array(atob(raw)
.split('')
.map((c) => c.charCodeAt(0)));
data = fontkit.create(uint8Array, postscriptName);
}
else {
const { headers, body, method = 'GET' } = this.options;
const buffer = await fetchFont(this.src, { method, body, headers });
data = fontkit.create(buffer, postscriptName);
}
if (data && 'fonts' in data) {
throw new Error('Font collection is not supported');
}
this.data = data;
}
async load() {
if (this.loadResultPromise === null) {
this.loadResultPromise = this._load();
}
return this.loadResultPromise;
}
}
const FONT_WEIGHTS = {
thin: 100,
hairline: 100,
ultralight: 200,
extralight: 200,
light: 300,
normal: 400,
medium: 500,
semibold: 600,
demibold: 600,
bold: 700,
ultrabold: 800,
extrabold: 800,
heavy: 900,
black: 900,
};
const resolveFontWeight = (value) => {
return typeof value === 'string' ? FONT_WEIGHTS[value] : value;
};
const sortByFontWeight = (a, b) => a.fontWeight - b.fontWeight;
class FontFamily {
family;
sources;
static create(family) {
return new FontFamily(family);
}
constructor(family) {
this.family = family;
this.sources = [];
}
register({ src, fontWeight, fontStyle, ...options }) {
const numericFontWeight = fontWeight
? resolveFontWeight(fontWeight)
: undefined;
this.sources.push(new FontSource(src, this.family, fontStyle, numericFontWeight, options));
}
resolve(descriptor) {
const { fontWeight = 400, fontStyle = 'normal' } = descriptor;
const styleSources = this.sources.filter((s) => s.fontStyle === fontStyle);
const exactFit = styleSources.find((s) => s.fontWeight === fontWeight);
if (exactFit)
return exactFit;
// Weight resolution. https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights
let font = null;
const numericFontWeight = resolveFontWeight(fontWeight);
if (numericFontWeight >= 400 && numericFontWeight <= 500) {
const leftOffset = styleSources.filter((s) => s.fontWeight <= numericFontWeight);
const rightOffset = styleSources.filter((s) => s.fontWeight > 500);
const fit = styleSources.filter((s) => s.fontWeight >= numericFontWeight && s.fontWeight <= 500);
font = fit[0] || leftOffset[leftOffset.length - 1] || rightOffset[0];
}
const lt = styleSources
.filter((s) => s.fontWeight < numericFontWeight)
.sort(sortByFontWeight);
const gt = styleSources
.filter((s) => s.fontWeight > numericFontWeight)
.sort(sortByFontWeight);
if (numericFontWeight < 400) {
font = lt[lt.length - 1] || gt[0];
}
if (numericFontWeight > 500) {
font = gt[0] || lt[lt.length - 1];
}
if (!font) {
throw new Error(`Could not resolve font for ${this.family}, fontWeight ${fontWeight}, fontStyle ${fontStyle}`);
}
return font;
}
}
class FontStore {
fontFamilies = {};
emojiSource = null;
constructor() {
this.register({
family: 'Helvetica',
fonts: [
{ src: 'Helvetica', fontStyle: 'normal', fontWeight: 400 },
{ src: 'Helvetica-Bold', fontStyle: 'normal', fontWeight: 700 },
{ src: 'Helvetica-Oblique', fontStyle: 'italic', fontWeight: 400 },
{ src: 'Helvetica-BoldOblique', fontStyle: 'italic', fontWeight: 700 },
],
});
this.register({
family: 'Courier',
fonts: [
{ src: 'Courier', fontStyle: 'normal', fontWeight: 400 },
{ src: 'Courier-Bold', fontStyle: 'normal', fontWeight: 700 },
{ src: 'Courier-Oblique', fontStyle: 'italic', fontWeight: 400 },
{ src: 'Courier-BoldOblique', fontStyle: 'italic', fontWeight: 700 },
],
});
this.register({
family: 'Times-Roman',
fonts: [
{ src: 'Times-Roman', fontStyle: 'normal', fontWeight: 400 },
{ src: 'Times-Bold', fontStyle: 'normal', fontWeight: 700 },
{ src: 'Times-Italic', fontStyle: 'italic', fontWeight: 400 },
{ src: 'Times-BoldItalic', fontStyle: 'italic', fontWeight: 700 },
],
});
// For backwards compatibility
this.register({
family: 'Helvetica-Bold',
src: 'Helvetica-Bold',
});
this.register({
family: 'Helvetica-Oblique',
src: 'Helvetica-Oblique',
});
this.register({
family: 'Helvetica-BoldOblique',
src: 'Helvetica-BoldOblique',
});
this.register({
family: 'Courier-Bold',
src: 'Courier-Bold',
});
this.register({
family: 'Courier-Oblique',
src: 'Courier-Oblique',
});
this.register({
family: 'Courier-BoldOblique',
src: 'Courier-BoldOblique',
});
this.register({
family: 'Times-Bold',
src: 'Times-Bold',
});
this.register({
family: 'Times-Italic',
src: 'Times-Italic',
});
this.register({
family: 'Times-BoldItalic',
src: 'Times-BoldItalic',
});
// Load default fonts
this.load({
fontFamily: 'Helvetica',
fontStyle: 'normal',
fontWeight: 400,
});
this.load({
fontFamily: 'Helvetica',
fontStyle: 'normal',
fontWeight: 700,
});
this.load({
fontFamily: 'Helvetica',
fontStyle: 'italic',
fontWeight: 400,
});
this.load({
fontFamily: 'Helvetica',
fontStyle: 'italic',
fontWeight: 700,
});
}
hyphenationCallback = null;
register = (data) => {
const { family } = data;
if (!this.fontFamilies[family]) {
this.fontFamilies[family] = FontFamily.create(family);
}
// Bulk loading
if ('fonts' in data) {
for (let i = 0; i < data.fonts.length; i += 1) {
const { src, fontStyle, fontWeight, ...options } = data.fonts[i];
this.fontFamilies[family].register({
src,
fontStyle,
fontWeight,
...options,
});
}
}
else {
const { src, fontStyle, fontWeight, ...options } = data;
this.fontFamilies[family].register({
src,
fontStyle,
fontWeight,
...options,
});
}
};
registerEmojiSource = (emojiSource) => {
this.emojiSource = emojiSource;
};
registerHyphenationCallback = (callback) => {
this.hyphenationCallback = callback;
};
getFont = (descriptor) => {
const { fontFamily } = descriptor;
if (!this.fontFamilies[fontFamily]) {
throw new Error(`Font family not registered: ${fontFamily}. Please register it calling Font.register() method.`);
}
return this.fontFamilies[fontFamily].resolve(descriptor);
};
load = async (descriptor) => {
const font = this.getFont(descriptor);
if (font)
await font.load();
};
reset = () => {
const keys = Object.keys(this.fontFamilies);
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i];
for (let j = 0; j < this.fontFamilies[key].sources.length; j++) {
const fontSource = this.fontFamilies[key].sources[j];
fontSource.data = null;
}
}
};
clear = () => {
this.fontFamilies = {};
this.emojiSource = null;
this.hyphenationCallback = null;
};
getRegisteredFonts = () => this.fontFamilies;
getEmojiSource = () => this.emojiSource;
getHyphenationCallback = () => this.hyphenationCallback;
getRegisteredFontFamilies = () => Object.keys(this.fontFamilies);
}
export { FontStore as default };
|