Skip to content

Commit

Permalink
🎉 (fonts) use Playfair & Lato in exported PNGs
Browse files Browse the repository at this point in the history
  • Loading branch information
samizdatco committed Oct 19, 2023
1 parent d73814e commit 77ed4a3
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions packages/@ourworldindata/grapher/src/modal/DownloadModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class DownloadModal extends React.Component<DownloadModalProps> {
return this.props.manager
}

@observable private svgContent?: string
@observable private svgBlob?: Blob
@observable private svgPreviewUrl?: string

Expand All @@ -94,17 +95,38 @@ export class DownloadModal extends React.Component<DownloadModalProps> {

@action.bound private export(): void {
this.createSvg()

const reader = new FileReader()
reader.onload = (ev: any): void => {
this.svgPreviewUrl = ev.target.result as string
this.tryCreatePng(this.svgPreviewUrl)
}
reader.readAsDataURL(this.svgBlob as Blob)

// merge the embedded font data into the svg before rendering to png
fetch("/fonts/embedded.css")
.then((data) => data.text())
.then((css) => {
const svgWithFonts = this.svgContent?.replace(
/(<svg[^>]*?>)/,
`$1<defs><style>${css}</style></defs>`
)
if (svgWithFonts)
reader.readAsDataURL(
new Blob([svgWithFonts], {
type: "image/svg+xml;charset=utf-8",
})
)
else throw new Error()
})
.catch(() => {
// fall back to the font-free version if something goes wrong
reader.readAsDataURL(this.svgBlob as Blob)
})
}

@action.bound private createSvg(): void {
const staticSVG = this.manager.staticSVG
this.svgBlob = new Blob([staticSVG], {
this.svgContent = this.manager.staticSVG
this.svgBlob = new Blob([this.svgContent], {
type: "image/svg+xml;charset=utf-8",
})
}
Expand Down

0 comments on commit 77ed4a3

Please sign in to comment.