Skip to content

Commit

Permalink
Improve font handling in tables
Browse files Browse the repository at this point in the history
  • Loading branch information
hollandjake committed Feb 14, 2025
1 parent 7c0a94e commit 0debcba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
17 changes: 10 additions & 7 deletions lib/table/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,20 @@ export function normalizeCell(cell, rowIndex, colIndex) {
let rowStyle = this._rowStyle(rowIndex);

const font = deepMerge({}, colStyle.font, rowStyle.font, cell.font);

const customFont = Object.values(font).filter((v) => v != null).length > 0;
const doc = this.document;

// Initialize cell context
const rollbackFont = doc._fontSource;
const rollbackFontSize = doc._fontSize;
const rollbackFontFamily = doc._fontFamily;
if (font.src) doc.font(font.src, font.family);
if (font.size) doc.fontSize(font.size);
if (customFont) {
if (font.src) doc.font(font.src, font.family);
if (font.size) doc.fontSize(font.size);

// Refetch rowStyle to reflect font changes
rowStyle = this._rowStyle(rowIndex);
// Refetch rowStyle to reflect font changes
rowStyle = this._rowStyle(rowIndex);
}

cell.padding = normalizeSides(cell.padding);
cell.border = normalizeSides(cell.border);
Expand All @@ -115,7 +117,8 @@ export function normalizeCell(cell, rowIndex, colIndex) {
const config = deepMerge(this._defaultStyle, colStyle, rowStyle, cell);
config.rowIndex = rowIndex;
config.colIndex = colIndex;
config.font = font;
config.font = font ?? {};
config.customFont = customFont;

// Normalize config
config.text = normalizeText(config.text);
Expand Down Expand Up @@ -153,7 +156,7 @@ export function normalizeCell(cell, rowIndex, colIndex) {
if (typeof this.opts.debug === 'boolean') config.debug = this.opts.debug;

// Rollback font
doc.font(rollbackFont, rollbackFontFamily, rollbackFontSize);
if (customFont) doc.font(rollbackFont, rollbackFontFamily, rollbackFontSize);

return config;
}
Expand Down
8 changes: 5 additions & 3 deletions lib/table/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ function renderCellText(cell) {
const rollbackFont = doc._fontSource;
const rollbackFontSize = doc._fontSize;
const rollbackFontFamily = doc._fontFamily;
if (cell.font?.src) doc.font(cell.font.src, cell.font?.family);
if (cell.font?.size) doc.fontSize(cell.font.size);
if (cell.customFont) {
if (cell.font.src) doc.font(cell.font.src, cell.font.family);
if (cell.font.size) doc.fontSize(cell.font.size);
}

const x = cell.textX;
const y = cell.textY;
Expand Down Expand Up @@ -141,7 +143,7 @@ function renderCellText(cell) {

// Cleanup
doc.restore();
doc.font(rollbackFont, rollbackFontFamily, rollbackFontSize);
if (cell.font) doc.font(rollbackFont, rollbackFontFamily, rollbackFontSize);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/table/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
*
* @property {string} text
* @property {Font} font
* @property {boolean} customFont
* @property {ExpandedSideDefinition<number>} padding
* @property {ExpandedSideDefinition<number>} border
* @property {ExpandedSideDefinition<PDFColor>} borderColor
Expand Down

0 comments on commit 0debcba

Please sign in to comment.