From 2ea2ed9b0ea7ccffcdbfcc2b64c2e514b28e766b Mon Sep 17 00:00:00 2001 From: phantom1003 Date: Sun, 30 Apr 2023 19:31:54 +0800 Subject: [PATCH] add dpi option --- src/pdfblock/processor.ts | 46 +++++++++++++++++++++++++++++---------- src/pdfblock/renderer.ts | 2 +- src/pdfview/canvas.ts | 32 +++++++++++++++++---------- src/settings.ts | 15 ++++++++++++- 4 files changed, 69 insertions(+), 26 deletions(-) diff --git a/src/pdfblock/processor.ts b/src/pdfblock/processor.ts index d385d37..f207cca 100644 --- a/src/pdfblock/processor.ts +++ b/src/pdfblock/processor.ts @@ -9,6 +9,7 @@ export interface PDFBlockParameters { page: Array; link: boolean; scale: number; + dpi: number; rotat: number; rect: Array; annot: string; @@ -40,7 +41,7 @@ export class PDFBlockProcessor { async parseParameters(src: string, frontmatter: FrontMatterCache) { const lines = src.split("\n"); - const keywords = ["file", "page", "link", "scale", "rotat", "rect"]; + const keywords = ["file", "page", "link", "scale", "rotat", "rect", "dpi"]; const paramsRaw: { [k: string]: string } = {}; const annot: Array = []; const note: Array = []; @@ -63,7 +64,8 @@ export class PDFBlockProcessor { file: "", page: [], link: this.plugin.settings.default_link, - scale: 0, + scale: 1, + dpi: this.plugin.settings.default_dpi, rotat: 0, rect: [-1, -1, -1, -1], annot: annot.join("\n"), @@ -84,7 +86,7 @@ export class PDFBlockProcessor { // handle pages if (paramsRaw["page"] == undefined) - paramsRaw["page"] = frontmatter && "default_page" in frontmatter ? frontmatter["default_page"] : "0"; + paramsRaw["page"] = frontmatter && "default_page" in frontmatter ? frontmatter["default_page"] : ["0"]; const pages = paramsRaw["page"].split(","); for (let i = 0; i < pages.length; i++) { if (pages[i].contains("-")) { @@ -99,22 +101,42 @@ export class PDFBlockProcessor { } // handle link - if (paramsRaw["link"] == undefined) - params.link = frontmatter && "default_link" in frontmatter ? frontmatter["default_link"] : this.plugin.settings.default_link; - else + if (paramsRaw["link"] == undefined) { + if (frontmatter && "default_link" in frontmatter) + params.link = frontmatter["default_link"]; + } + else { params.link = paramsRaw["link"].toLowerCase() === 'true'; + } // handle scale - if (paramsRaw["scale"] == undefined) - params.scale = frontmatter && "default_scale" in frontmatter ? frontmatter["default_scale"] : 1; - else + if (paramsRaw["scale"] == undefined) { + if (frontmatter && "default_scale" in frontmatter) + params.scale = frontmatter["default_scale"]; + } + else { params.scale = parseFloat(paramsRaw["scale"]); + } + + // handle dpi + if (paramsRaw["dpi"] == undefined) { + if (frontmatter && "default_dpi" in frontmatter) + params.dpi = frontmatter["default_dpi"]; + } + else { + params.dpi = parseInt(paramsRaw["dpi"]); + } // handle rotation - if (paramsRaw["rotat"] == undefined) - params.rotat = frontmatter && "default_scale" in frontmatter ? frontmatter["default_scale"] : 0; - else + if (paramsRaw["rotat"] == undefined) { + if (frontmatter && "default_scale" in frontmatter) { + params.rotat = frontmatter["default_scale"]; + } + } + else { params.rotat = parseInt(paramsRaw["rotat"]); + } + if (paramsRaw["rect"] != undefined) { const rect = paramsRaw["rect"].split(","); diff --git a/src/pdfblock/renderer.ts b/src/pdfblock/renderer.ts index 210e538..2ff9d69 100644 --- a/src/pdfblock/renderer.ts +++ b/src/pdfblock/renderer.ts @@ -111,7 +111,7 @@ export class PDFBlockRenderer extends MarkdownRenderChild { return; const context = canvas.getContext("2d"); - const zoom = 2 + const zoom = this.params.dpi; const offsetX = this.params.rect[0] == -1 ? 0 : - this.params.rect[0] * page.view[2] * zoom; const offsetY = this.params.rect[1] == -1 ? 0 : - this.params.rect[1] * page.view[3] * zoom; const pageview = page.getViewport({ diff --git a/src/pdfview/canvas.ts b/src/pdfview/canvas.ts index f44f6e5..c1592ca 100644 --- a/src/pdfview/canvas.ts +++ b/src/pdfview/canvas.ts @@ -33,14 +33,19 @@ export class PDFCanvasView extends ItemView { image.alt = "Click slide to start ..."; image.style.position = "absolute"; image.style.width = "95%"; + + function resize2Image() { + drawboard.setHeight(image.innerHeight); + drawboard.setWidth(image.innerWidth); + drawboard.renderAll(); + } + const canvas = preview.createEl("canvas") canvas.style.position = "absolute"; const drawboard = new fabric.Canvas(canvas, { isDrawingMode: false, }); - drawboard.setHeight(image.innerHeight); - drawboard.setWidth(image.innerWidth); - drawboard.renderAll(); + resize2Image(); drawboard.freeDrawingBrush.color = "rgba(250,230,50,0.5)"; drawboard.freeDrawingBrush.width = 10; @@ -70,9 +75,11 @@ export class PDFCanvasView extends ItemView { option.createEl("h4").setText("Options:") option.createEl("button", {text: "Select", attr: {style: "margin-right: 4px;"}}).addEventListener("click", () => { drawboard.isDrawingMode = false; + resize2Image(); }); option.createEl("button", {text: "Pen", attr: {style: "margin-right: 4px;"}}).addEventListener("click", () => { drawboard.isDrawingMode = true; + resize2Image(); }); option.createEl("button", {text: "Delete", attr: {style: "margin-right: 4px;"}}).addEventListener("click", () => { if(drawboard.getActiveObject()){ @@ -92,6 +99,7 @@ export class PDFCanvasView extends ItemView { }); drawboard.add(textbox); drawboard.setActiveObject(textbox); + resize2Image(); }); option.createEl("button", {text: "Line", attr: {style: "margin-right: 4px;"}}).addEventListener("click", () => { const line = new fabric.Line([50, 50, 150, 50], { @@ -107,6 +115,7 @@ export class PDFCanvasView extends ItemView { }); drawboard.add(line); drawboard.setActiveObject(line); + resize2Image(); }); option.createEl("button", {text: "Rect", attr: {style: "margin-right: 4px;"}}).addEventListener("click", () => { const rectangle = new fabric.Rect({ @@ -120,6 +129,7 @@ export class PDFCanvasView extends ItemView { rectangle.setControlsVisibility({ mtr: false }); drawboard.add(rectangle); drawboard.setActiveObject(rectangle); + resize2Image(); }); option.createEl("button", {text: "Save", attr: {style: "margin-right: 4px;"}}).addEventListener("click", () => { const fractionDigit = 3; @@ -168,11 +178,13 @@ export class PDFCanvasView extends ItemView { buffer.push(`ctx.strokeStyle = "${element.stroke}";`); buffer.push(`ctx.lineWidth = W(${strokeWidth});`); buffer.push("ctx.beginPath();"); + const path: string[] = []; for (const point of element.path) { const x = (point[1]/canvasWidth).toFixed(fractionDigit); const y = (point[2]/canvasHeight).toFixed(fractionDigit); - buffer.push(`ctx.lineTo(W(${x}), H(${y}));`); + path.push(`ctx.lineTo(W(${x}), H(${y}));`); } + buffer.push(path.join(" ")); buffer.push("ctx.stroke();"); break; } @@ -181,21 +193,17 @@ export class PDFCanvasView extends ItemView { } } output.setText(buffer.map((s) => ("@ " + s)).join("\n")); - output.style.height = output.scrollHeight.toString() + output.style.height = output.scrollHeight.toString() + "px"; }); this.registerEvent(app.workspace.on("slidenote:newcanvas", (src) => { - image.src = src - drawboard.setHeight(image.innerHeight); - drawboard.setWidth(image.innerWidth); - drawboard.renderAll(); + image.src = src; + resize2Image(); })); this.registerEvent(app.workspace.on("resize", (event) => { - drawboard.setHeight(image.innerHeight); - drawboard.setWidth(image.innerWidth); - drawboard.renderAll(); + resize2Image(); })); } diff --git a/src/settings.ts b/src/settings.ts index a7f9562..9ad5cc1 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,6 +4,7 @@ import SlideNotePlugin from './main'; export class SlideNoteSettings { allow_annotations: boolean = false; default_link: boolean = false; + default_dpi: number = 1; } export class SlideNoteSettingsTab extends PluginSettingTab { @@ -19,6 +20,8 @@ export class SlideNoteSettingsTab extends PluginSettingTab { containerEl.empty(); + containerEl.createEl('h1', { text: 'Slide Note Settings' }); + new Setting(containerEl) .setName("Link pages by default") .setDesc("When turned on, pages will be linked to their document by default. Can be overridden using the 'link' parameter and 'default_link' frontmatter.") @@ -36,5 +39,15 @@ export class SlideNoteSettingsTab extends PluginSettingTab { this.plugin.settings.allow_annotations = value; this.plugin.saveSettings(); })); - } + + new Setting(containerEl) + .setName("Default DPI level") + .setDesc("Increase the value to improve the resolution of the slide.") + .addText(text => text.setValue(this.plugin.settings.default_dpi.toString()) + .onChange((value) => { + this.plugin.settings.default_dpi = parseInt(value); + this.plugin.saveSettings(); + })); + + } }