Skip to content

Commit

Permalink
Add support for numerical font weights (#181)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Hollaender <lukas.hollaender@yworks.com>
  • Loading branch information
EugeneBalabai and HackbrettXXX committed Sep 14, 2021
1 parent d1f7f81 commit 2de7de3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 9 additions & 12 deletions src/applyparseattributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
43 changes: 33 additions & 10 deletions src/utils/fonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { AttributeState } from '../context/attributestate'
import { Context } from '../context/context'
import jsPDF from 'jspdf'

export type FontFamily = string

Expand All @@ -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 = ''
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions test/specs/complete-organization-chart-new/spec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 12 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2de7de3

Please sign in to comment.