From 2de7de3e8f452f82c9ee6c151731dcdfe99c49d6 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 14 Sep 2021 16:13:34 +0300 Subject: [PATCH] Add support for numerical font weights (#181) Co-authored-by: Lukas Hollaender --- package.json | 2 +- src/applyparseattributes.ts | 21 ++++----- src/utils/fonts.ts | 43 ++++++++++++++----- .../complete-organization-chart-new/spec.svg | 4 +- yarn.lock | 16 +++++-- 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 26c3696..502d3d6 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "cssesc": "^3.0.0", "exorcist": "^1.0.1", "font-family-papandreou": "^0.2.0-patch1", - "jspdf": "^2.2.0", + "jspdf": "^2.4.0", "karma": "^6.3.2", "karma-chai": "^0.1.0", "karma-chrome-launcher": "^3.1.0", diff --git a/src/applyparseattributes.ts b/src/applyparseattributes.ts index e85a8a0..dc45467 100644 --- a/src/applyparseattributes.ts +++ b/src/applyparseattributes.ts @@ -4,7 +4,11 @@ import { toPixels } from './utils/misc' import { parseColor, parseFloats } from './utils/parsing' import FontFamily from 'font-family-papandreou' import { SvgNode } from './nodes/svgnode' -import { findFirstAvailableFontFamily, fontAliases } from './utils/fonts' +import { + combineFontStyleAndFontWeight, + findFirstAvailableFontFamily, + fontAliases +} from './utils/fonts' import { parseFill } from './fill/parseFill' import { ColorFill } from './fill/ColorFill' import { GState } from 'jspdf' @@ -277,17 +281,10 @@ export function applyAttributes( childContext.attributeState.fontWeight !== parentContext.attributeState.fontWeight || childContext.attributeState.fontStyle !== parentContext.attributeState.fontStyle ) { - fontStyle = '' - if (childContext.attributeState.fontWeight === 'bold') { - fontStyle = 'bold' - } - if (childContext.attributeState.fontStyle === 'italic') { - fontStyle += 'italic' - } - - if (fontStyle === '') { - fontStyle = 'normal' - } + fontStyle = combineFontStyleAndFontWeight( + childContext.attributeState.fontStyle, + childContext.attributeState.fontWeight + ) } if (font !== undefined || fontStyle !== undefined) { diff --git a/src/utils/fonts.ts b/src/utils/fonts.ts index 09a7ddf..777f1cc 100644 --- a/src/utils/fonts.ts +++ b/src/utils/fonts.ts @@ -5,6 +5,7 @@ */ import { AttributeState } from '../context/attributestate' import { Context } from '../context/context' +import jsPDF from 'jspdf' export type FontFamily = string @@ -27,16 +28,10 @@ export function findFirstAvailableFontFamily( fontFamilies: FontFamily[], context: Context ): FontFamily { - let fontType = '' - if (attributeState.fontWeight === 'bold') { - fontType = 'bold' - } - if (attributeState.fontStyle === 'italic') { - fontType += 'italic' - } - if (fontType === '') { - fontType = 'normal' - } + const fontType = combineFontStyleAndFontWeight( + attributeState.fontStyle, + attributeState.fontWeight + ) const availableFonts = context.pdf.getFontList() let firstAvailable = '' @@ -62,3 +57,31 @@ export function findFirstAvailableFontFamily( return firstAvailable } + +const isJsPDF23: boolean = (() => { + const parts = jsPDF.version.split('.') + return parseFloat(parts[0]) === 2 && parseFloat(parts[1]) === 3 +})() + +export function combineFontStyleAndFontWeight( + fontStyle: string, + fontWeight: number | string +): string { + if (isJsPDF23) { + return fontWeight == 400 + ? fontStyle == 'italic' + ? 'italic' + : 'normal' + : fontWeight == 700 && fontStyle !== 'italic' + ? 'bold' + : fontStyle + '' + fontWeight + } else { + return fontWeight == 400 || fontWeight === 'normal' + ? fontStyle === 'italic' + ? 'italic' + : 'normal' + : (fontWeight == 700 || fontWeight === 'bold') && fontStyle === 'normal' + ? 'bold' + : (fontWeight == 700 ? 'bold' : fontWeight) + '' + fontStyle + } +} diff --git a/test/specs/complete-organization-chart-new/spec.svg b/test/specs/complete-organization-chart-new/spec.svg index 2988e6b..b258930 100644 --- a/test/specs/complete-organization-chart-new/spec.svg +++ b/test/specs/complete-organization-chart-new/spec.svg @@ -150,7 +150,7 @@ - + Eric Joplin Chief Executive @@ -162,4 +162,4 @@ - \ No newline at end of file + diff --git a/yarn.lock b/yarn.lock index 501d82f..c0cca9e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -23,6 +23,13 @@ chalk "^2.0.0" js-tokens "^4.0.0" +"@babel/runtime@^7.14.0": + version "7.15.4" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.15.4.tgz#fd17d16bfdf878e6dd02d19753a39fa8a8d9c84a" + integrity sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw== + dependencies: + regenerator-runtime "^0.13.4" + "@babel/runtime@^7.6.3": version "7.11.2" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736" @@ -2495,11 +2502,12 @@ jsonfile@^4.0.0: optionalDependencies: graceful-fs "^4.1.6" -jspdf@^2.2.0: - version "2.3.1" - resolved "https://registry.yarnpkg.com/jspdf/-/jspdf-2.3.1.tgz#313d117234b546469694a1fd81a1e02411647576" - integrity sha512-1vp0USP1mQi1h7NKpwxjFgQkJ5ncZvtH858aLpycUc/M+r/RpWJT8PixAU7Cw/3fPd4fpC8eB/Bj42LnsR21YQ== +jspdf@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/jspdf/-/jspdf-2.4.0.tgz#53d6d2acc63203b0b3688949597dd11a633b1db5" + integrity sha512-nsZ92YfbNG/EimR1yqmOkxf2D4iJRypBsw7pvP1aPiIEnoGITaLl6XDR/GYA36/R29vMZSBedpEhBCzutSGytA== dependencies: + "@babel/runtime" "^7.14.0" atob "^2.1.2" btoa "^1.2.1" fflate "^0.4.8"