Skip to content

Commit

Permalink
rename content to text
Browse files Browse the repository at this point in the history
  • Loading branch information
hollandjake committed Jan 27, 2025
1 parent a15bd2d commit c089eb6
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 135 deletions.
42 changes: 21 additions & 21 deletions docs/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ Example:
["width=100", "star-sized", "width=200", "star-sized"],
[
"fixed-width cells have exactly the specified width",
{ content: "nothing interesting here", textColor: "grey" },
{ content: "nothing interesting here", textColor: "grey" },
{ content: "nothing interesting here", textColor: "grey" }
{ text: "nothing interesting here", textColor: "grey" },
{ text: "nothing interesting here", textColor: "grey" },
{ text: "nothing interesting here", textColor: "grey" }
],
],
});
Expand Down Expand Up @@ -101,13 +101,13 @@ Each cell can set a rowSpan or colSpan
doc.table({
columnStyles: [200, "*", "*"],
data: [
[{ colSpan: 2, content: "Header with Colspan = 2" }, "Header 3"],
[{ colSpan: 2, text: "Header with Colspan = 2" }, "Header 3"],
["Header 1", "Header 2", "Header 3"],
["Sample value 1", "Sample value 2", "Sample value 3"],
[
{
rowSpan: 3,
content: "rowspan set to 3\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor",
text: "rowspan set to 3\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor",
},
"Sample value 2",
"Sample value 3",
Expand All @@ -119,7 +119,7 @@ Each cell can set a rowSpan or colSpan
{
colSpan: 2,
rowSpan: 2,
content: "Both:\nrowspan and colspan\ncan be defined at the same time",
text: "Both:\nrowspan and colspan\ncan be defined at the same time",
},
],
["Sample value 1"],
Expand Down Expand Up @@ -241,21 +241,21 @@ Zebra style
doc.table({
data: [
[
{ border: [true, false, false, false], backgroundColor: "#eee", content: "border:\n[true, false, false, false]" },
{ border: false, backgroundColor: "#ddd", content: "border:\nfalse" },
{ border: true, backgroundColor: "#eee", content: "border:\ntrue" },
{ border: [true, false, false, false], backgroundColor: "#eee", text: "border:\n[true, false, false, false]" },
{ border: false, backgroundColor: "#ddd", text: "border:\nfalse" },
{ border: true, backgroundColor: "#eee", text: "border:\ntrue" },
],
[
{ rowSpan: 3, border: true, backgroundColor: "#eef", content: "rowSpan: 3\n\nborder:\ntrue" },
{ border: undefined, backgroundColor: "#eee", content: "border:\nundefined (default)" },
{ border: [false, false, false, true], backgroundColor: "#ddd", content: "border:\n[false, false, false, true]" },
{ rowSpan: 3, border: true, backgroundColor: "#eef", text: "rowSpan: 3\n\nborder:\ntrue" },
{ border: undefined, backgroundColor: "#eee", text: "border:\nundefined (default)" },
{ border: [false, false, false, true], backgroundColor: "#ddd", text: "border:\n[false, false, false, true]" },
],
[
{ colSpan: 2, border: true, backgroundColor: "#efe", content: "colSpan: 2\n\nborder:\ntrue" },
{ colSpan: 2, border: true, backgroundColor: "#efe", text: "colSpan: 2\n\nborder:\ntrue" },
],
[
{ border: 0, backgroundColor: "#eee", content: "border:\n0 (same as false)" },
{ border: [false, true, true, false], backgroundColor: "#ddd", content: "border:\n[false, true, true, false]" },
{ border: 0, backgroundColor: "#eee", text: "border:\n0 (same as false)" },
{ border: [false, true, true, false], backgroundColor: "#ddd", text: "border:\n[false, true, true, false]" },
],
],
})
Expand All @@ -275,8 +275,7 @@ Zebra style
colSpan: 3,
border: true,
backgroundColor: "#ccc",
content:
"rowSpan: 3\ncolSpan: 3\n\nborder:\n[true, true, true, true]",
text: "rowSpan: 3\ncolSpan: 3\n\nborder:\n[true, true, true, true]",
},
],
["row 2"],
Expand Down Expand Up @@ -345,19 +344,20 @@ is added to the document. This way, any calls to `text` or `table` will be place

## Cell options

- `content` - The value, will be cast to a string (boolean is converted to `Y/N`, and `null` and `undefined` are not rendered but the cell is still outlined)
- `text` - The value, will be cast to a string (`null` and `undefined` are not rendered but the cell is still outlined)
- `rowSpan` - How many rows this cell covers, follows the same logic as HTML `rowspan`
- `colSpan` - How many columns this cell covers, follows the same logic as HTML `colspan`
- `padding` - The padding for the cell (default `0.25em`)
- `border` - The border for the cell (default `1pt`)
- `borderColor` - The border colors for the cell (default `black`)
- `font` - Font options for the cell
- `backgroundColor` - Set the background color of the cell
- `align` - The alignment of the cell content (default `{x: 'left', y: 'top'}`)
- `align` - The alignment of the cell text (default `{x: 'left', y: 'top'}`)
- `textStroke` - The text stroke (default `0`)
- `textStrokeColor` - Sets the text stroke color of the cells content (default `black`)
- `textColor` - Sets the text color of the cells content (default `black`)
- `textStrokeColor` - Sets the text stroke color of the cells text (default `black`)
- `textColor` - Sets the text color of the cells text (default `black`)
- `type` - Sets the cell type (for accessibility) (default `TD`)
- `textOptions` - Sets any text options you wish to provide (such as rotation)
- `debug` - Whether to show the debug lines for the cell (default `false`)

## Column options
Expand Down
21 changes: 8 additions & 13 deletions lib/table/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,18 @@ export function normalizeTable() {
}

/**
* Convert content into a string
* - booleans get converted into 'Y'/'N'
* Convert text into a string
* - null and undefined are preserved (as they will be ignored)
* - everything else is run through `String()`
*
* @param {*} content
* @param {*} text
* @returns {string}
* @private
*/
export function normalizeContent(content) {
// Parse out content
if (typeof content === 'boolean') {
content = content ? 'Y' : 'N';
} else if (content !== null && content !== undefined) {
content = String(content);
}
return content;
export function normalizeText(text) {
// Parse out text
if (text !== null && text !== undefined) text = String(text);
return text;
}

/**
Expand Down Expand Up @@ -119,7 +114,7 @@ export function normalizeCell(cell, rowIndex, colIndex) {
});

// Normalize config
config.content = normalizeContent(config.content);
config.text = normalizeText(config.text);
config.rowSpan = config.rowSpan ?? 1;
config.colSpan = config.colSpan ?? 1;
config.padding = normalizeSides(config.padding, '0.25em', (x) =>
Expand Down Expand Up @@ -169,7 +164,7 @@ export function normalizeRow(row, rowIndex) {
return row.map((cell) => {
// Ensure TableCell
if (cell === null || cell === undefined || typeof cell !== 'object') {
cell = { content: cell };
cell = { text: cell };
}
cell = definedProps(cell);

Expand Down
32 changes: 16 additions & 16 deletions lib/table/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function renderCell(cell, rowStruct) {
this.document.restore();
}

// Render content
if (cell.content) renderCellContent.call(this, cell);
// Render text
if (cell.text) renderCellText.call(this, cell);
});
}

Expand All @@ -91,22 +91,22 @@ function renderCell(cell, rowStruct) {
* @memberOf PDFTable
* @param cell
*/
function renderCellContent(cell) {
function renderCellText(cell) {
// Configure fonts
const rollbackFont = this.document._fontSource;
const rollbackFontSize = this.document._fontSize;
const rollbackFontFamily = this.document._fontFamily;
if (cell.font?.src) this.document.font(cell.font.src, cell.font?.family);
if (cell.font?.size) this.document.fontSize(cell.font.size);

const x = cell.contentX;
const y = cell.contentY;
const Ah = cell.contentAllocatedHeight;
const Aw = cell.contentAllocatedWidth;
const Cw = cell.contentBounds.width;
const Ch = cell.contentBounds.height;
const Ox = -cell.contentBounds.x;
const Oy = -cell.contentBounds.y;
const x = cell.textX;
const y = cell.textY;
const Ah = cell.textAllocatedHeight;
const Aw = cell.textAllocatedWidth;
const Cw = cell.textBounds.width;
const Ch = cell.textBounds.height;
const Ox = -cell.textBounds.x;
const Oy = -cell.textBounds.y;

const PxScale =
cell.align.x === 'right' ? 1 : cell.align.x === 'center' ? 0.5 : 0;
Expand All @@ -122,8 +122,8 @@ function renderCellContent(cell) {
this.document.save();
this.document.dash(1, { space: 1 }).lineWidth(1).strokeOpacity(0.3);

// Debug actual contents bounds
if (cell.content) {
// Debug actual text bounds
if (cell.text) {
this.document
.moveTo(x + Px, y)
.lineTo(x + Px, y + Ah)
Expand All @@ -136,21 +136,21 @@ function renderCellContent(cell) {
.lineTo(x + Aw, y + Py + Ch)
.stroke('green');
}
// Debug allocated content bounds
// Debug allocated text bounds
this.document.rect(x, y, Aw, Ah).stroke('orange');

this.document.restore();
}

// Create content mask to cut off any overflowing text
// Create text mask to cut off any overflowing text
// Mask cuts off at the padding not the actual cell, this is intentional!
this.document.save().rect(x, y, Aw, Ah).clip();

this.document.fillColor(cell.textColor).strokeColor(cell.textStrokeColor);
if (cell.textStroke > 0) this.document.lineWidth(cell.textStroke);

// Render the text
this.document.text(cell.content, x + dx, y + dy, cell.textOptions);
this.document.text(cell.text, x + dx, y + dy, cell.textOptions);

// Cleanup
this.document.restore();
Expand Down
Loading

0 comments on commit c089eb6

Please sign in to comment.