Skip to content

Commit

Permalink
add dpi option
Browse files Browse the repository at this point in the history
  • Loading branch information
Phantom1003 committed Apr 30, 2023
1 parent 249c1a1 commit 2ea2ed9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
46 changes: 34 additions & 12 deletions src/pdfblock/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface PDFBlockParameters {
page: Array<number>;
link: boolean;
scale: number;
dpi: number;
rotat: number;
rect: Array<number>;
annot: string;
Expand Down Expand Up @@ -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<string> = [];
const note: Array<string> = [];
Expand All @@ -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"),
Expand All @@ -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("-")) {
Expand All @@ -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(",");
Expand Down
2 changes: 1 addition & 1 deletion src/pdfblock/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
32 changes: 20 additions & 12 deletions src/pdfview/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()){
Expand All @@ -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], {
Expand All @@ -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({
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}));

}
Expand Down
15 changes: 14 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.")
Expand All @@ -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();
}));

}
}

0 comments on commit 2ea2ed9

Please sign in to comment.