Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fonts with different font-weights #181

Merged
merged 5 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/applyparseattributes.ts
Expand Up @@ -9,6 +9,7 @@ import { parseFill } from './fill/parseFill'
import { ColorFill } from './fill/ColorFill'
import { GState } from 'jspdf'
import { RGBColor } from './utils/rgbcolor'
import { combineFontStyleAndFontWeight } from './utils/combineFontStyleAndFontWeight'

export function parseAttributes(context: Context, svgNode: SvgNode, node?: Element): void {
const domNode = node || svgNode.element
Expand Down Expand Up @@ -277,17 +278,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
31 changes: 31 additions & 0 deletions src/utils/combineFontStyleAndFontWeight.ts
@@ -0,0 +1,31 @@
/**
* @function combineFontStyleAndFontWeight
* @param {string} fontStyle Fontstyle or variant. Example: "italic".
* @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400
* @returns {string}
*/
export function combineFontStyleAndFontWeight(
fontStyle: string,
fontWeight: number | string
): string {
if (
(fontStyle == 'bold' && fontWeight == 'normal') ||
(fontStyle == 'bold' && fontWeight == 400) ||
(fontStyle == 'normal' && fontWeight == 'italic') ||
(fontStyle == 'bold' && fontWeight == 'italic')
) {
throw new Error('Invalid Combination of fontweight and fontstyle')
}
if (fontWeight && fontStyle !== fontWeight) {
//if fontstyle is normal and fontweight is normal too no need to append the font-weight
fontStyle =
fontWeight == 400
? fontStyle == 'italic'
? 'italic'
: 'normal'
: fontWeight == 700 && fontStyle !== 'italic'
? 'bold'
: fontStyle + '' + fontWeight
}
return fontStyle
}
15 changes: 5 additions & 10 deletions src/utils/fonts.ts
Expand Up @@ -5,6 +5,7 @@
*/
import { AttributeState } from '../context/attributestate'
import { Context } from '../context/context'
import { combineFontStyleAndFontWeight } from './combineFontStyleAndFontWeight'

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 Down