Skip to content

Commit 25ffaad

Browse files
committed
registerFonts
1 parent 37898ce commit 25ffaad

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

packages/abstract-document/src/abstract-document-exporters/pdf/font.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ import { TextFontWeight } from "../../abstract-document/styles/text-style.js";
44
import { getResources } from "../shared/get_resources.js";
55

66
export function registerFonts(
7-
registerFont: (fontName: string, fontSource: AD.Font.FontSource) => void,
7+
registerFont: (fontName: string, fontSource: AD.Font.FontSource, family: string) => void,
88
document: AD.AbstractDoc.AbstractDoc
99
): void {
1010
const resources = getResources(document);
1111
for (const [fontName, font] of Object.entries(resources.fonts ?? {})) {
1212
// Required
13-
registerFont(fontName, font.normal);
14-
registerFont(fontName + "-Bold", font.bold);
15-
registerFont(fontName + "-Oblique", font.italic);
16-
registerFont(fontName + "-Italic", font.italic);
17-
registerFont(fontName + "-BoldOblique", font.boldItalic);
18-
registerFont(fontName + "-BoldItalic", font.boldItalic);
13+
registerFont(fontName, font.normal, fontName);
14+
registerFont(fontName + "-Bold", font.bold, fontName);
15+
registerFont(fontName + "-Oblique", font.italic, fontName);
16+
registerFont(fontName + "-Italic", font.italic, fontName);
17+
registerFont(fontName + "-BoldOblique", font.boldItalic, fontName);
18+
registerFont(fontName + "-BoldItalic", font.boldItalic, fontName);
1919
// Optional
20-
registerFont(fontName + "-Light", font.light || font.normal);
21-
registerFont(fontName + "-Medium", font.medium || font.normal);
22-
registerFont(fontName + "-ExtraBold", font.extraBold || font.bold);
23-
registerFont(fontName + "-LightOblique", font.lightItalic || font.normal);
24-
registerFont(fontName + "-LightItalic", font.lightItalic || font.normal);
25-
registerFont(fontName + "-MediumOblique", font.mediumItalic || font.italic);
26-
registerFont(fontName + "-MediumItalic", font.mediumItalic || font.italic);
27-
registerFont(fontName + "-ExtraBoldItalic", font.extraBoldItalic || font.boldItalic);
28-
registerFont(fontName + "-ExtraBoldOblique", font.extraBoldItalic || font.boldItalic);
20+
registerFont(fontName + "-Light", font.light || font.normal, fontName);
21+
registerFont(fontName + "-Medium", font.medium || font.normal, fontName);
22+
registerFont(fontName + "-ExtraBold", font.extraBold || font.bold, fontName);
23+
registerFont(fontName + "-LightOblique", font.lightItalic || font.normal, fontName);
24+
registerFont(fontName + "-LightItalic", font.lightItalic || font.normal, fontName);
25+
registerFont(fontName + "-MediumOblique", font.mediumItalic || font.italic, fontName);
26+
registerFont(fontName + "-MediumItalic", font.mediumItalic || font.italic, fontName);
27+
registerFont(fontName + "-ExtraBoldItalic", font.extraBoldItalic || font.boldItalic, fontName);
28+
registerFont(fontName + "-ExtraBoldOblique", font.extraBoldItalic || font.boldItalic, fontName);
2929
}
3030
}
3131

@@ -58,10 +58,10 @@ export function getFontNameStyle(textStyle: AD.TextStyle.TextStyle): string {
5858
export function getFontStyleName(attributes: Record<string, string>): keyof Font {
5959
const fontWeight = getFontWeightFromAttributes(attributes);
6060
const italic = attributes.italic === "true";
61-
if(fontWeight === "normal") {
61+
if (fontWeight === "normal") {
6262
return italic ? "italic" : fontWeight;
6363
}
64-
return `${fontWeight === "mediumBold" ? "medium" : fontWeight}${italic ? "Italic": ""}`;
64+
return `${fontWeight === "mediumBold" ? "medium" : fontWeight}${italic ? "Italic" : ""}`;
6565
}
6666

6767
export function getFontName(

packages/abstract-document/src/abstract-document-exporters/pdf/measure.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const heightOfStringCache = new Map<string, number>();
1010

1111
export function measure(pdfKit: PDFKit.PDFDocument, document: AD.AbstractDoc.AbstractDoc): Map<any, AD.Size.Size> {
1212
let pdf = new pdfKit();
13-
registerFonts((fontName: string, fontSource: AD.Font.FontSource) => pdf.registerFont(fontName, fontSource), document);
13+
registerFonts(pdf.registerFont, document);
1414
const result = mergeMaps(document.children.map((s) => measureSection(pdf, document, s)));
1515
widthOfStringCache.clear();
1616
heightOfStringCache.clear();
@@ -23,7 +23,7 @@ export function measurePages(
2323
pages: ReadonlyArray<Page>
2424
): Map<any, AD.Size.Size> {
2525
const pdf = new pdfKit();
26-
registerFonts((fontName: string, fontSource: AD.Font.FontSource) => pdf.registerFont(fontName, fontSource), document);
26+
registerFonts(pdf.registerFont, document);
2727
return mergeMaps(
2828
pages.flatMap((page) =>
2929
page.columns.map(({ elements }) =>

packages/abstract-document/src/abstract-document-exporters/pdf/paginate.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function paginate(
2626
const resources = getResources(document);
2727
const pdf = new pdfKit({ compress: false, autoFirstPage: false, bufferPages: true });
2828

29-
registerFonts((fontName: string, fontSource: AD.Font.FontSource) => pdf.registerFont(fontName, fontSource), document);
29+
registerFonts(pdf.registerFont, document);
3030
const pages = new Array<Page>();
3131
for (let section of document.children) {
3232
const previousPage = pages.length > 0 ? pages[pages.length - 1] : undefined;
@@ -280,12 +280,16 @@ export function getHeaderAndFooter(
280280
return {
281281
footer: normalFooter ? section.page.footer : section.page.frontFooter,
282282
header: normalHeader ? section.page.header : section.page.frontHeader,
283-
headerMargins: AD.LayoutFoundation.orDefault(normalHeader
284-
? section.page.style.headerMargins
285-
: section.page.style.firstPageHeaderMargins ?? section.page.style.headerMargins),
286-
footerMargins: AD.LayoutFoundation.orDefault(normalFooter
287-
? section.page.style.footerMargins
288-
: section.page.style.firstPageFooterMargins ?? section.page.style.footerMargins),
283+
headerMargins: AD.LayoutFoundation.orDefault(
284+
normalHeader
285+
? section.page.style.headerMargins
286+
: section.page.style.firstPageHeaderMargins ?? section.page.style.headerMargins
287+
),
288+
footerMargins: AD.LayoutFoundation.orDefault(
289+
normalFooter
290+
? section.page.style.footerMargins
291+
: section.page.style.firstPageFooterMargins ?? section.page.style.footerMargins
292+
),
289293
};
290294
}
291295
case pageNo === 0:
@@ -486,7 +490,7 @@ function splitTableAt(
486490

487491
const availableSize = getDesiredSize(table, desiredSizes);
488492
let pdf = new pdfKit();
489-
registerFonts((fontName: string, fontSource: AD.Font.FontSource) => pdf.registerFont(fontName, fontSource), document);
493+
registerFonts(pdf.registerFont, document);
490494
const headSizes = measureTable(pdf, resources, availableSize, tableHead);
491495
const tailSizes = measureTable(pdf, resources, availableSize, tableTail);
492496

packages/abstract-document/src/abstract-document-exporters/pdf/render.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function createDocument(
5050
const pdf = new pdfKit({ ...options, autoFirstPage: false, bufferPages: true });
5151

5252
const document = preProcess(ad);
53-
registerFonts((fontName: string, fontSource: AD.Font.FontSource) => pdf.registerFont(fontName, fontSource), document);
53+
registerFonts(pdf.registerFont, document);
5454
const desiredSizes = measure(pdfKit, document);
5555
const pages = paginate(pdfKit, document, desiredSizes);
5656
const updatedPages = updatePageRefs(pages);
@@ -825,10 +825,8 @@ function renderCell(
825825
.reduce((a, b) => a + b, 0);
826826
const startY = finalRect.y + padding.top;
827827
let y = startY;
828-
if (style.verticalAlignment === "Middle")
829-
y += 0.5 * (availableHeight - contentHeight - padding.top - padding.bottom);
830-
else if (style.verticalAlignment === "Bottom")
831-
y += availableHeight - contentHeight - padding.top - padding.bottom;
828+
if (style.verticalAlignment === "Middle") y += 0.5 * (availableHeight - contentHeight - padding.top - padding.bottom);
829+
else if (style.verticalAlignment === "Bottom") y += availableHeight - contentHeight - padding.top - padding.bottom;
832830

833831
for (const element of cell.children) {
834832
const elementSize = getDesiredSize(element, desiredSizes);

0 commit comments

Comments
 (0)