Skip to content

Commit 2de7de3

Browse files
Add support for numerical font weights (#181)
Co-authored-by: Lukas Hollaender <[email protected]>
1 parent d1f7f81 commit 2de7de3

File tree

5 files changed

+57
-29
lines changed

5 files changed

+57
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"cssesc": "^3.0.0",
7070
"exorcist": "^1.0.1",
7171
"font-family-papandreou": "^0.2.0-patch1",
72-
"jspdf": "^2.2.0",
72+
"jspdf": "^2.4.0",
7373
"karma": "^6.3.2",
7474
"karma-chai": "^0.1.0",
7575
"karma-chrome-launcher": "^3.1.0",

src/applyparseattributes.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { toPixels } from './utils/misc'
44
import { parseColor, parseFloats } from './utils/parsing'
55
import FontFamily from 'font-family-papandreou'
66
import { SvgNode } from './nodes/svgnode'
7-
import { findFirstAvailableFontFamily, fontAliases } from './utils/fonts'
7+
import {
8+
combineFontStyleAndFontWeight,
9+
findFirstAvailableFontFamily,
10+
fontAliases
11+
} from './utils/fonts'
812
import { parseFill } from './fill/parseFill'
913
import { ColorFill } from './fill/ColorFill'
1014
import { GState } from 'jspdf'
@@ -277,17 +281,10 @@ export function applyAttributes(
277281
childContext.attributeState.fontWeight !== parentContext.attributeState.fontWeight ||
278282
childContext.attributeState.fontStyle !== parentContext.attributeState.fontStyle
279283
) {
280-
fontStyle = ''
281-
if (childContext.attributeState.fontWeight === 'bold') {
282-
fontStyle = 'bold'
283-
}
284-
if (childContext.attributeState.fontStyle === 'italic') {
285-
fontStyle += 'italic'
286-
}
287-
288-
if (fontStyle === '') {
289-
fontStyle = 'normal'
290-
}
284+
fontStyle = combineFontStyleAndFontWeight(
285+
childContext.attributeState.fontStyle,
286+
childContext.attributeState.fontWeight
287+
)
291288
}
292289

293290
if (font !== undefined || fontStyle !== undefined) {

src/utils/fonts.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import { AttributeState } from '../context/attributestate'
77
import { Context } from '../context/context'
8+
import jsPDF from 'jspdf'
89

910
export type FontFamily = string
1011

@@ -27,16 +28,10 @@ export function findFirstAvailableFontFamily(
2728
fontFamilies: FontFamily[],
2829
context: Context
2930
): FontFamily {
30-
let fontType = ''
31-
if (attributeState.fontWeight === 'bold') {
32-
fontType = 'bold'
33-
}
34-
if (attributeState.fontStyle === 'italic') {
35-
fontType += 'italic'
36-
}
37-
if (fontType === '') {
38-
fontType = 'normal'
39-
}
31+
const fontType = combineFontStyleAndFontWeight(
32+
attributeState.fontStyle,
33+
attributeState.fontWeight
34+
)
4035

4136
const availableFonts = context.pdf.getFontList()
4237
let firstAvailable = ''
@@ -62,3 +57,31 @@ export function findFirstAvailableFontFamily(
6257

6358
return firstAvailable
6459
}
60+
61+
const isJsPDF23: boolean = (() => {
62+
const parts = jsPDF.version.split('.')
63+
return parseFloat(parts[0]) === 2 && parseFloat(parts[1]) === 3
64+
})()
65+
66+
export function combineFontStyleAndFontWeight(
67+
fontStyle: string,
68+
fontWeight: number | string
69+
): string {
70+
if (isJsPDF23) {
71+
return fontWeight == 400
72+
? fontStyle == 'italic'
73+
? 'italic'
74+
: 'normal'
75+
: fontWeight == 700 && fontStyle !== 'italic'
76+
? 'bold'
77+
: fontStyle + '' + fontWeight
78+
} else {
79+
return fontWeight == 400 || fontWeight === 'normal'
80+
? fontStyle === 'italic'
81+
? 'italic'
82+
: 'normal'
83+
: (fontWeight == 700 || fontWeight === 'bold') && fontStyle === 'normal'
84+
? 'bold'
85+
: (fontWeight == 700 ? 'bold' : fontWeight) + '' + fontStyle
86+
}
87+
}

test/specs/complete-organization-chart-new/spec.svg

Lines changed: 2 additions & 2 deletions
Loading

yarn.lock

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
chalk "^2.0.0"
2424
js-tokens "^4.0.0"
2525

26+
"@babel/runtime@^7.14.0":
27+
version "7.15.4"
28+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.15.4.tgz#fd17d16bfdf878e6dd02d19753a39fa8a8d9c84a"
29+
integrity sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==
30+
dependencies:
31+
regenerator-runtime "^0.13.4"
32+
2633
"@babel/runtime@^7.6.3":
2734
version "7.11.2"
2835
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
@@ -2495,11 +2502,12 @@ jsonfile@^4.0.0:
24952502
optionalDependencies:
24962503
graceful-fs "^4.1.6"
24972504

2498-
jspdf@^2.2.0:
2499-
version "2.3.1"
2500-
resolved "https://registry.yarnpkg.com/jspdf/-/jspdf-2.3.1.tgz#313d117234b546469694a1fd81a1e02411647576"
2501-
integrity sha512-1vp0USP1mQi1h7NKpwxjFgQkJ5ncZvtH858aLpycUc/M+r/RpWJT8PixAU7Cw/3fPd4fpC8eB/Bj42LnsR21YQ==
2505+
jspdf@^2.4.0:
2506+
version "2.4.0"
2507+
resolved "https://registry.yarnpkg.com/jspdf/-/jspdf-2.4.0.tgz#53d6d2acc63203b0b3688949597dd11a633b1db5"
2508+
integrity sha512-nsZ92YfbNG/EimR1yqmOkxf2D4iJRypBsw7pvP1aPiIEnoGITaLl6XDR/GYA36/R29vMZSBedpEhBCzutSGytA==
25022509
dependencies:
2510+
"@babel/runtime" "^7.14.0"
25032511
atob "^2.1.2"
25042512
btoa "^1.2.1"
25052513
fflate "^0.4.8"

0 commit comments

Comments
 (0)