diff --git a/plugins/plugin-cutlist/src/index.mjs b/plugins/plugin-cutlist/src/index.mjs index ea146f52f97..5f71939eac6 100644 --- a/plugins/plugin-cutlist/src/index.mjs +++ b/plugins/plugin-cutlist/src/index.mjs @@ -81,6 +81,7 @@ function setCutOnFold(store, p1, p2) { return store } +/** Get a list of fabrics used by the pattern for the given settings */ function getCutFabrics(store, settings) { const cutlist = store.get('cutlist') const list = settings.only ? [].concat(settings.only) : Object.keys(cutlist) diff --git a/sites/shared/components/workbench/exporting/export-handler.mjs b/sites/shared/components/workbench/exporting/export-handler.mjs index 3abc04af4e7..91c661065c6 100644 --- a/sites/shared/components/workbench/exporting/export-handler.mjs +++ b/sites/shared/components/workbench/exporting/export-handler.mjs @@ -24,6 +24,15 @@ export const defaultPdfSettings = { cutlist: true, } +/** + * Instantiate a pattern that uses plugins theme, i18n, and cutlist + * @param {Design} design the design to construct the pattern from + * @param {Object} gist the gist + * @param {Object} overwrite settings to overwrite gist settings with + * @param {string} format the export format this pattern will be prepared for + * @param {function} t the i18n function + * @return {Pattern} a pattern + */ const themedPattern = (design, gist, overwrite, format, t) => { const pattern = new design({ ...gist, ...overwrite }) @@ -34,7 +43,18 @@ const themedPattern = (design, gist, overwrite, format, t) => { return pattern } + +/** + * Generate svgs of all cutting layouts for the pattern + * @param {Pattern} pattern the pattern to generate cutting layouts for + * @param {Design} design the design constructor for the pattern + * @param {Object} gist the gist + * @param {string} format the export format this pattern will be prepared for + * @param {function} t the i18n function + * @return {Object} a dictionary of svgs and related translation strings, keyed by fabric + */ const generateCutLayouts = (pattern, design, gist, format, t) => { + // get the fabrics from the already drafted base pattern const fabrics = pattern.setStores[pattern.activeSet].cutlist.getCutFabrics( pattern.settings[0] ) || ['fabric'] @@ -42,15 +62,22 @@ const generateCutLayouts = (pattern, design, gist, format, t) => { const isImperial = gist.units === 'imperial' const cutLayouts = {} + // each fabric fabrics.forEach((f) => { + // get the settings and layout for that fabric const fabricSettings = fabricSettingsOrDefault(gist, f) const fabricLayout = get(gist, ['layouts', 'cuttingLayout', f], true) + + // make a new pattern const fabricPattern = themedPattern(design, gist, { layout: fabricLayout }, format, t) + // add cut layout plugin and fabric plugin .use(cutLayoutPlugin(f, fabricSettings.grainDirection)) .use(fabricPlugin({ ...fabricSettings, printStyle: true, setPatternSize: 'width' })) + // draft and render fabricPattern.draft() const svg = fabricPattern.render() + // include translations cutLayouts[f] = { svg, title: t('plugin:' + f), diff --git a/sites/shared/components/workbench/exporting/pdf-maker.mjs b/sites/shared/components/workbench/exporting/pdf-maker.mjs index 17c26866dc7..d7e63fca39d 100644 --- a/sites/shared/components/workbench/exporting/pdf-maker.mjs +++ b/sites/shared/components/workbench/exporting/pdf-maker.mjs @@ -29,6 +29,8 @@ export class PdfMaker { buffers /** translated strings to add to the cover page */ strings + /** cutting layout svgs and strings */ + cutLayouts /** the usable width (excluding margin) of the pdf page, in points */ pageWidth @@ -124,6 +126,7 @@ export class PdfMaker { await this.generateSvgPage(this.svg) } + /** generate a page that has an svg centered in it below any text */ async generateSvgPage(svg) { //abitrary margin for visual space let coverMargin = 85 @@ -138,8 +141,12 @@ export class PdfMaker { // use aspect ratio to center it preserveAspectRatio: 'xMidYMid meet', }) + + // increment page count this.pageCount++ } + + /** generate the title for the cover page */ async generateCoverPageTitle() { this.addText('FreeSewing', 28) .addText(this.strings.tagline, 12, 20) @@ -162,6 +169,7 @@ export class PdfMaker { this.addText(this.strings.url, 10) } + /** generate the title for a cutting layout page */ async generateCutLayoutTitle(fabricTitle, fabricDimensions) { this.addText(this.strings.cuttingLayout, 12, 2).addText(fabricTitle, 28) @@ -175,6 +183,7 @@ export class PdfMaker { this.addText(fabricDimensions, 16) } + /** generate all cutting layout pages */ async generateCutLayoutPages() { if (!this.settings.cutlist || !this.cutLayouts) return @@ -216,15 +225,24 @@ export class PdfMaker { } } + /** Reset to a clean page */ nextPage() { - // if no pages have been made, we can use the current + // set the line level back to the top this.lineLevel = lineStart + + // if no pages have been made, we can use the current if (this.pageCount === 0) return // otherwise make a new page this.pdf.addPage() } + /** + * Add Text to the page at the current line level + * @param {String} text the text to add + * @param {Number} fontSize the size for the text + * @param {Number} marginBottom additional margin to add below the text + */ addText(text, fontSize, marginBottom = 0) { this.pdf.fontSize(fontSize) this.pdf.text(text, 50, this.lineLevel)