From bc58919bb10b367840bd637d58de2ff6b4de0e4e Mon Sep 17 00:00:00 2001 From: Benjamin Fan Date: Tue, 13 Feb 2024 08:09:57 -0800 Subject: [PATCH 1/2] feat(shared): Add pattern information to PDF cover sheet --- .../workbench/exporting/export-handler.mjs | 34 ++++++++++++++ .../workbench/exporting/export-worker.js | 3 ++ .../workbench/exporting/pdf-maker.mjs | 46 ++++++++++++++++--- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/sites/shared/components/workbench/exporting/export-handler.mjs b/sites/shared/components/workbench/exporting/export-handler.mjs index 59c4df5b3c9..1c7b1e683eb 100644 --- a/sites/shared/components/workbench/exporting/export-handler.mjs +++ b/sites/shared/components/workbench/exporting/export-handler.mjs @@ -13,6 +13,8 @@ import { printSettingsPath, } from 'shared/components/workbench/views/print/config.mjs' import get from 'lodash.get' +import mustache from 'mustache' +import he from 'he' export const ns = ['cut', 'plugin', 'common'] export const exportTypes = { @@ -187,6 +189,18 @@ export const handleExport = async ({ pattern.draft() workerArgs.svg = pattern.render() + // Save pattern forName, notes, warnings info for coversheet + const store = pattern.setStores[pattern.activeSet] + workerArgs.strings.forName = store.get('data.for', 'ephemeral') + const notes = store?.plugins?.['plugin-annotations']?.flags?.note + ? store?.plugins?.['plugin-annotations']?.flags?.note + : [] + const warns = store?.plugins?.['plugin-annotations']?.flags?.warn + ? store?.plugins?.['plugin-annotations']?.flags?.warn + : [] + workerArgs.strings.notes = flagsToString(notes, mustache, t) + workerArgs.strings.warns = flagsToString(warns, mustache, t) + if (format === 'pdf') pageSettings.size = [pattern.width, pattern.height] // add the svg and pages data to the worker args @@ -209,3 +223,23 @@ export const handleExport = async ({ // post a message to the worker with all needed data worker.postMessage(workerArgs) } + +/** + * Convert pattern flags to a formatted string for printing + */ +const flagsToString = (flags, mustache, t) => { + let first = true + let string = '' + for (const flag of Object.values(flags)) { + let title = flag.replace ? mustache.render(t(flag.title), flag.replace) : t(flag.title) + title = he.decode(title) + let desc = flag.replace ? mustache.render(t(flag.desc), flag.replace) : t(flag.desc) + desc = desc.replaceAll('\n\n', '\n') + desc = desc.replaceAll('\n', ' ') + desc = he.decode(desc) + if (!first) string += '\n' + first = false + string += '- ' + title + ': ' + desc + } + return string +} diff --git a/sites/shared/components/workbench/exporting/export-worker.js b/sites/shared/components/workbench/exporting/export-worker.js index 47e783f3302..024ae75a107 100644 --- a/sites/shared/components/workbench/exporting/export-worker.js +++ b/sites/shared/components/workbench/exporting/export-worker.js @@ -51,6 +51,9 @@ const exportYaml = (settings) => exportBlob(yaml.dump(settings), 'application/x- const exportSvg = (svg) => exportBlob(svg, 'image/svg+xml') const exportPdf = async (data) => { + // Save yaml for coversheet + data.strings.yaml = yaml.dump(data.settings) + const maker = data.format === 'pdf' ? new SinglePdfMaker(data) : new PdfMaker(data) await maker.makePdf() postSuccess(await maker.toBlob()) diff --git a/sites/shared/components/workbench/exporting/pdf-maker.mjs b/sites/shared/components/workbench/exporting/pdf-maker.mjs index d0e6ae9e8d1..0e5a2351564 100644 --- a/sites/shared/components/workbench/exporting/pdf-maker.mjs +++ b/sites/shared/components/workbench/exporting/pdf-maker.mjs @@ -118,12 +118,44 @@ export class PdfMaker { /** generate the title for the cover page */ async generateCoverPageTitle() { - this.addText('FreeSewing', 28) - .addText(this.strings.tagline, 12, 20) - .addText(this.strings.design, 48, -8) + // FreeSewing tag + this.addText('FreeSewing', 20).addText(this.strings.tagline, 10, 4) - await SVGtoPDF(this.pdf, logoSvg, this.pdf.page.width - lineStart - 100, lineStart, { - width: 100, + // Design name + this.addText(this.strings.design, 32) + + // Measurement Set + this.addText('Measurement Set: ' + this.strings.forName, 10) + + // Date + const currentDateTime = new Date().toLocaleString('en', { + year: 'numeric', + month: 'short', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + timeZoneName: 'short', + }) + this.addText('PDF Generated: ' + currentDateTime, 10) + + // Settings YAML + const savedLineLevel = this.lineLevel + 2 + const savedWidth = this.pdf.widthOfString('Settings: ') + 50 + this.addText('Settings: ', 10) + this.pdf.fontSize(6) + this.pdf.text('(Measurement values are in mm.)', savedWidth, savedLineLevel) + this.addText(this.strings.yaml, 8) + + // Notes and Warnings + if (this.strings.notes) { + this.addText('Notes:', 10).addText(this.strings.notes, 8) + } + if (this.strings.warns) { + this.addText('Warnings:', 10).addText(this.strings.warns, 8) + } + + await SVGtoPDF(this.pdf, logoSvg, this.pdf.page.width - lineStart - 50, lineStart, { + width: 50, height: this.lineLevel - lineStart, preserveAspectRatio: 'xMaxYMin meet', }) @@ -220,7 +252,9 @@ export class PdfMaker { this.pdf.fontSize(fontSize) this.pdf.text(text, 50, this.lineLevel) - this.lineLevel += fontSize + marginBottom + const lines = text.split(/\r\n|\r|\n/).length + this.lineLevel += this.pdf.heightOfString(text) + marginBottom + return this } } From 8353e2e561b426bfc1b6b524ef0b363751955213 Mon Sep 17 00:00:00 2001 From: Benjamin Fan Date: Tue, 13 Feb 2024 08:47:45 -0800 Subject: [PATCH 2/2] fix(shared): Remove debug code accidentally left in --- sites/shared/components/workbench/exporting/pdf-maker.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/sites/shared/components/workbench/exporting/pdf-maker.mjs b/sites/shared/components/workbench/exporting/pdf-maker.mjs index 0e5a2351564..54b4939d214 100644 --- a/sites/shared/components/workbench/exporting/pdf-maker.mjs +++ b/sites/shared/components/workbench/exporting/pdf-maker.mjs @@ -252,7 +252,6 @@ export class PdfMaker { this.pdf.fontSize(fontSize) this.pdf.text(text, 50, this.lineLevel) - const lines = text.split(/\r\n|\r|\n/).length this.lineLevel += this.pdf.heightOfString(text) + marginBottom return this