Skip to content

Commit

Permalink
Apply font-only styles for calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghv authored and simonbengtsson committed Apr 9, 2020
1 parent 0cf831b commit 0abbc07
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import state from './state'
import { assign } from './polyfills'

export function getStringWidth(text, styles) {
applyStyles(styles)
applyStyles(styles, true)
const textArr: any = Array.isArray(text) ? text : [text]

const widestLineWidth = textArr
Expand Down Expand Up @@ -75,20 +75,24 @@ export function applyUserStyles() {
applyStyles(state().table.userStyles)
}

export function applyStyles(styles) {
let doc = state().doc
let styleModifiers = {
export function applyStyles(styles, fontOnly = false) {
const doc = state().doc
const nonFontModifiers = {
fillColor: doc.setFillColor,
textColor: doc.setTextColor,
fontStyle: doc.setFontStyle,
lineColor: doc.setDrawColor,
lineWidth: doc.setLineWidth,
}
const styleModifiers = {
font: doc.setFont,
fontSize: doc.setFontSize,
fontStyle: doc.setFontStyle,
...(fontOnly ? {} : nonFontModifiers),
}

Object.keys(styleModifiers).forEach(function (name) {
let style = styles[name]
let modifier = styleModifiers[name]
const style = styles[name]
const modifier = styleModifiers[name]
if (typeof style !== 'undefined') {
if (Array.isArray(style)) {
modifier.apply(this, style)
Expand Down
2 changes: 1 addition & 1 deletion src/widthCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function fitContent(table) {
let cell: Cell = row.cells[column.index]
if (!cell) continue

applyStyles(cell.styles)
applyStyles(cell.styles, true)
let textSpace = cell.width - cell.padding('horizontal')
if (cell.styles.overflow === 'linebreak') {
// Add one pt to textSpace to fix rounding error
Expand Down

0 comments on commit 0abbc07

Please sign in to comment.