Skip to content

Commit

Permalink
Add support for lineHeightFactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghv authored and simonbengtsson committed Oct 2, 2023
1 parent 6ab944f commit 4c04f6a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
12 changes: 7 additions & 5 deletions src/autoTableText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export default function (
doc: jsPDFDocument
) {
styles = styles || {}
const FONT_ROW_RATIO = 1.15
const PHYSICAL_LINE_HEIGHT = 1.15

const k = doc.internal.scaleFactor
const fontSize = doc.internal.getFontSize() / k
const lineHeightFactor = doc.getLineHeightFactor ? doc.getLineHeightFactor() : PHYSICAL_LINE_HEIGHT
const lineHeight = fontSize * lineHeightFactor

const splitRegex = /\r\n|\r|\n/g
let splitText: string | string[] = ''
Expand All @@ -31,12 +33,12 @@ export default function (
}

// Align the top
y += fontSize * (2 - FONT_ROW_RATIO)
y += fontSize * (2 - PHYSICAL_LINE_HEIGHT)

if (styles.valign === 'middle')
y -= (lineCount / 2) * fontSize * FONT_ROW_RATIO
y -= (lineCount / 2) * lineHeight
else if (styles.valign === 'bottom')
y -= lineCount * fontSize * FONT_ROW_RATIO
y -= lineCount * lineHeight

if (styles.halign === 'center' || styles.halign === 'right') {
let alignSize = fontSize
Expand All @@ -49,7 +51,7 @@ export default function (
x - doc.getStringUnitWidth(splitText[iLine]) * alignSize,
y
)
y += fontSize * FONT_ROW_RATIO
y += lineHeight
}
return doc
}
Expand Down
5 changes: 0 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { CellHook, PageHook } from './models'

/**
* Ratio between font size and font height. The number comes from jspdf's source code
*/
export const FONT_ROW_RATIO = 1.15

export interface LineWidths {
bottom: number
top: number
Expand Down
9 changes: 9 additions & 0 deletions src/documentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ export class DocHandler {
return this.jsPDFDocument.internal.scaleFactor
}

get lineHeightFactor(): number {
const doc = this.jsPDFDocument
return doc.getLineHeightFactor ? doc.getLineHeightFactor() : 1.15
}

getLineHeight(fontSize: number): number {
return (fontSize / this.scaleFactor()) * this.lineHeightFactor
}

pageNumber(): number {
const pageInfo = this.jsPDFDocument.internal.getCurrentPageInfo()
if (!pageInfo) {
Expand Down
13 changes: 6 additions & 7 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
CellInput,
Color,
ColumnInput,
FONT_ROW_RATIO,
HtmlRowInput,
RowInput,
Styles,
Expand Down Expand Up @@ -216,10 +215,9 @@ export class Row {
return columns.reduce((acc: number, column: Column) => {
const cell = this.cells[column.index]
if (!cell) return 0
const fontHeight =
(cell.styles.fontSize / doc.scaleFactor()) * FONT_ROW_RATIO
const lineHeight = doc.getLineHeight(cell.styles.fontSize)
const vPadding = cell.padding('vertical')
const oneRowHeight = vPadding + fontHeight
const oneRowHeight = vPadding + lineHeight
return oneRowHeight > acc ? oneRowHeight : acc
}, 0)
}
Expand Down Expand Up @@ -292,10 +290,11 @@ export class Cell {
return { x, y }
}

getContentHeight(scaleFactor: number) {
// TODO (v4): replace parameters with only (lineHeight)
getContentHeight(scaleFactor: number, lineHeightFactor: number = 1.15) {
const lineCount = Array.isArray(this.text) ? this.text.length : 1
const fontHeight = (this.styles.fontSize / scaleFactor) * FONT_ROW_RATIO
const height = lineCount * fontHeight + this.padding('vertical')
const lineHeight = (this.styles.fontSize / scaleFactor) * lineHeightFactor
const height = lineCount * lineHeight + this.padding('vertical')
return Math.max(height, this.styles.minCellHeight)
}

Expand Down
11 changes: 6 additions & 5 deletions src/tableDrawer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Color, FONT_ROW_RATIO, LineWidths } from './config'
import { Color, LineWidths } from './config'
import { addTableBorder, getFillStyle } from './common'
import { Cell, Column, Pos, Row, Table } from './models'
import { DocHandler, jsPDFDocument } from './documentHandler'
Expand Down Expand Up @@ -153,10 +153,10 @@ function getRemainingLineCount(
remainingPageSpace: number,
doc: DocHandler
) {
const fontHeight = (cell.styles.fontSize / doc.scaleFactor()) * FONT_ROW_RATIO
const lineHeight = doc.getLineHeight(cell.styles.fontSize)
const vPadding = cell.padding('vertical')
const remainingLines = Math.floor(
(remainingPageSpace - vPadding) / fontHeight
(remainingPageSpace - vPadding) / lineHeight
)
return Math.max(0, remainingLines)
}
Expand Down Expand Up @@ -198,7 +198,8 @@ function modifyRowToFit(
}

const scaleFactor = doc.scaleFactor()
cell.contentHeight = cell.getContentHeight(scaleFactor)
const lineHeightFactor = doc.lineHeightFactor
cell.contentHeight = cell.getContentHeight(scaleFactor, lineHeightFactor)

if (cell.contentHeight >= remainingPageSpace) {
cell.contentHeight = remainingPageSpace
Expand All @@ -208,7 +209,7 @@ function modifyRowToFit(
row.height = cell.contentHeight
}

remainderCell.contentHeight = remainderCell.getContentHeight(scaleFactor)
remainderCell.contentHeight = remainderCell.getContentHeight(scaleFactor, lineHeightFactor)
if (remainderCell.contentHeight > rowHeight) {
rowHeight = remainderCell.contentHeight
}
Expand Down
2 changes: 1 addition & 1 deletion src/widthCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ function fitContent(table: Table, doc: DocHandler) {
}
}

cell.contentHeight = cell.getContentHeight(doc.scaleFactor())
cell.contentHeight = cell.getContentHeight(doc.scaleFactor(), doc.lineHeightFactor)

let realContentHeight = cell.contentHeight / cell.rowSpan
if (
Expand Down

0 comments on commit 4c04f6a

Please sign in to comment.