From 949ab15857c14bfb34debd3ea84c9432ab73d945 Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Wed, 18 May 2022 09:44:46 +0200 Subject: [PATCH 01/24] + Added text printing option to HTML interface --- www/index.html | 1 + www/lang/de-DE.json | 1 + www/lang/en-US.json | 1 + www/main.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/www/index.html b/www/index.html index a587d0d..ce6d81c 100644 --- a/www/index.html +++ b/www/index.html @@ -120,6 +120,7 @@
+

Or drag file to below

diff --git a/www/lang/de-DE.json b/www/lang/de-DE.json index a0dba02..b7f14eb 100644 --- a/www/lang/de-DE.json +++ b/www/lang/de-DE.json @@ -9,6 +9,7 @@ "canvas": "Leinwand", "document": "Dokument", "insert-picture": "Bild einfügen", + "insert-text": "Text einfügen", "help": "Hilfe", "javascript-license-information": "Informationen zur JavaScript-Lizenz", "settings": "Einstellungen", diff --git a/www/lang/en-US.json b/www/lang/en-US.json index 65bb520..88cac2a 100644 --- a/www/lang/en-US.json +++ b/www/lang/en-US.json @@ -8,6 +8,7 @@ "canvas": "Canvas", "document": "Document", "insert-picture": "Insert Picture", + "insert-text": "Insert Text", "help": "Help", "javascript-license-information": "JavaScript License Information", "settings": "Settings", diff --git a/www/main.js b/www/main.js index 06a6707..bdff35b 100644 --- a/www/main.js +++ b/www/main.js @@ -317,6 +317,7 @@ class CanvasController { putEvent('input[name="algo"]', 'change', (event) => this.useAlgorithm(event.currentTarget.value), this); putEvent('#insert-picture' , 'click', () => this.insertPicture(), this); + putEvent('#insert-text' , 'click', () => this.insertText(), this); putEvent('#button-preview' , 'click', this.activatePreview , this); putEvent('#button-reset' , 'click', this.reset , this); putEvent('#canvas-expand' , 'click', this.expand , this); @@ -437,6 +438,48 @@ class CanvasController { input.click(); } } + insertText() { + let text = prompt("Enter text"); + let lines = []; + + // Use Word Wrap to split the text over multiple lines + const wrapText = (text, maxLength) => { + let splitPos = maxLength - 1; + while (text[splitPos] != " " && splitPos > 0) { + splitPos--; + } + if (splitPos == 0) { splitPos = maxLength; } + return [text.slice(0, splitPos), text.slice(splitPos, text.length)]; + } + + const maxLength = 33; + while (text.length > maxLength) { + let line; + [line, text] = wrapText(text, maxLength); + lines.push(line); + } + lines.push(text) + + let ctx = this.canvas.getContext("2d"); + ctx.font = "20px Sans-Serif"; + + const yStep = 20; + let yPos = yStep; + for (let line of lines) { + ctx.fillText(line, 0, yPos); + yPos += yStep; + } + this.crop(); + + this.imageUrl = this.canvas.toDataURL(); + this.activatePreview(); + + this.controls.classList.add('hidden'); + this.preview.hidden = true; + + hint('#button-print'); + return true; + } reset() { let canvas = this.canvas; canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height); From 8f402eb80f5929e32cb654c466ae789cdbe06096 Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Wed, 18 May 2022 12:02:30 +0200 Subject: [PATCH 02/24] + Added font and text size to settings panel --- www/index.html | 13 +++++++++++++ www/main.js | 33 ++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/www/index.html b/www/index.html index ce6d81c..ad0fb2e 100644 --- a/www/index.html +++ b/www/index.html @@ -91,6 +91,19 @@ Dump Traffic --> + + +
+ @@ -91,19 +108,6 @@ Dump Traffic --> - - -
- @@ -194,6 +176,34 @@ +
+

Insert Text

+ +
+ +
+ +
+ + +
+ +
+ + Wrap text + +
+
+
diff --git a/www/lang/de-DE.json b/www/lang/de-DE.json index adbebc6..1ade635 100644 --- a/www/lang/de-DE.json +++ b/www/lang/de-DE.json @@ -107,5 +107,9 @@ "text-font": "Schriftart", "text-size": "Textgröße", "enter-text": "Text eingeben", - "wrap-by-space": "Autom. Zeilenumbruch" + "wrap-by-space": "Autom. Zeilenumbruch", + "text-align": "Textausrichtung", + "align-left": "Linksbündig", + "align-mid": "Zentriert", + "align-right": "Rechtsbündig" } \ No newline at end of file diff --git a/www/lang/en-US.json b/www/lang/en-US.json index 8c39e0f..80ba3ce 100644 --- a/www/lang/en-US.json +++ b/www/lang/en-US.json @@ -132,5 +132,9 @@ "text-font": "Font", "text-size": "Size", "enter-text": "Enter text", - "wrap-by-space": "Wrap words by spaces" + "wrap-by-space": "Wrap words by spaces", + "text-align": "Align text", + "align-left": "Left", + "align-mid": "Center", + "align-right": "Right" } \ No newline at end of file diff --git a/www/main.css b/www/main.css index e7b8d80..100c7f7 100644 --- a/www/main.css +++ b/www/main.css @@ -660,4 +660,11 @@ body.high-contrast #control-overlay { background-color: var(--shade); } font-family: 'Unifont'; src: local('Unifont') url('unifont.ttf') url('unifont.otf'); } -#insert-text-area { white-space: pre; height: 50vh; width: var(--paper-width); overflow: hidden auto; white-space: pre-line;} +#insert-text-area { + white-space: pre; + height: 50vh; + width: var(--paper-width); + overflow: hidden auto; + white-space: pre-line; + resize:none; +} diff --git a/www/main.js b/www/main.js index afa6bbd..d44c459 100644 --- a/www/main.js +++ b/www/main.js @@ -307,7 +307,7 @@ class CanvasController { this._thresholdRange = document.querySelector('[name="threshold"]'); this._energyRange = document.querySelector('[name="energy"]'); this.imageUrl = null; - this.textAlign = 0; // 0: Left, 1: Center, 2: Right + this.textAlign = document.getElementById("text-align"); const prevent_default = (event) => { event.preventDefault(); @@ -330,6 +330,7 @@ class CanvasController { putEvent('#insert-text' , 'click', () => Dialog.alert("#text-input", () => this.insertText(this.textArea.value))); putEvent('#text-size' , 'change', () => this.textArea.style["font-size"] = this.textSize.value + "px"); putEvent('#text-font' , 'change', () => this.textArea.style["font-family"] = this.textFont.value); + putEvent('#text-align' , 'change', () => this.textArea.style["text-align"] = this.textAlign.value); putEvent('input[name="wrap-by-space"]' , 'change', () => this.textArea.style["word-break"] = this.wrapBySpace.checked ? "break-word" : "break-all"); putEvent('#button-preview' , 'click', this.activatePreview , this); putEvent('#button-reset' , 'click', this.reset , this); @@ -513,9 +514,10 @@ class CanvasController { let y_pos = y_step; for (let line of lines) { let x_pos = 0; - if (this.textAlign == 2) { + // Text-alignment + if (this.textAlign.value == "right") { x_pos = Math.max(max_width - ctx.measureText(line).width, 0) - } else if (this.textAlign == 1) { + } else if (this.textAlign.value == "center") { x_pos = Math.max(max_width - ctx.measureText(line).width, 0) / 2; } From 0685603832767a567407c55138bab0a426c243d8 Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Wed, 25 May 2022 11:32:19 +0200 Subject: [PATCH 16/24] + Added drag & drop for text files * Fixed spaces not being allowed in text area --- www/main.css | 2 +- www/main.js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/www/main.css b/www/main.css index 100c7f7..7116736 100644 --- a/www/main.css +++ b/www/main.css @@ -665,6 +665,6 @@ body.high-contrast #control-overlay { background-color: var(--shade); } height: 50vh; width: var(--paper-width); overflow: hidden auto; - white-space: pre-line; + white-space: break-spaces; resize:none; } diff --git a/www/main.js b/www/main.js index d44c459..42f8fbb 100644 --- a/www/main.js +++ b/www/main.js @@ -317,7 +317,16 @@ class CanvasController { this.canvas.addEventListener('dragover', prevent_default); this.canvas.addEventListener('dragenter', prevent_default); this.canvas.addEventListener('drop', (event) => { - this.insertPicture(event.dataTransfer.files); + if (event.dataTransfer?.files[0]?.type.split("/")[0] == "text") { + let file_reader = new FileReader(); + file_reader.onload = () => { + this.textArea.value = file_reader.result; + Dialog.alert("#text-input", () => this.insertText(this.textArea.value)); + }; + file_reader.readAsText(event.dataTransfer.files[0]); + } else { + this.insertPicture(event.dataTransfer.files); + } return prevent_default(event); }); @@ -480,7 +489,7 @@ class CanvasController { let split_pos = max_length; let newline_index = text.indexOf("\n"); if (newline_index > 0 && newline_index < max_length) { - return [text.slice(0, newline_index).trim(), text.slice(newline_index, text.length).trim()]; + return [text.slice(0, newline_index), text.slice(newline_index, text.length)]; } if (this.wrapBySpace.checked) { @@ -488,7 +497,7 @@ class CanvasController { if (split_pos <= 0) { split_pos = max_length; } } - return [text.slice(0, split_pos).trim(), text.slice(split_pos, text.length).trim()]; + return [text.slice(0, split_pos), text.slice(split_pos, text.length)]; } ctx.font = canvas_font; From 9a4a81c78bd595ac1eb87b2200a899fe18f67efe Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Wed, 25 May 2022 11:56:37 +0200 Subject: [PATCH 17/24] + Added websafe fonts to font selection --- www/index.html | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/www/index.html b/www/index.html index c43e312..1bf9d0d 100644 --- a/www/index.html +++ b/www/index.html @@ -183,14 +183,21 @@
+
- -
- + + + + + + + + + + + + +
diff --git a/www/main.css b/www/main.css index 7116736..d14b498 100644 --- a/www/main.css +++ b/www/main.css @@ -668,3 +668,35 @@ body.high-contrast #control-overlay { background-color: var(--shade); } white-space: break-spaces; resize:none; } + +.text-align-container span { + padding: 0px; + margin: 0; + display: inline-block; +} +.text-align-container input { + position: absolute; + opacity: 0; + cursor: pointer; + height: 25px; + width: 25px; +} + +.text-align-checkmark { + /* position: absolute; */ + background-repeat: no-repeat; + background-image: url("icons/text-left.svg)"); + background-position: center; + background-size: 90%; + width: 25px; + height: 25px; + min-width: 25px; + border: var(--border) solid var(--fore-color); + padding: var(--span-half) var(--span); +} +.text-align-container input:checked ~ .text-align-checkmark { + background-color: whitesmoke; + } +.text-align-left { background-image: url("icons/text-left.svg")} +.text-align-center { background-image: url("icons/text-center.svg")} +.text-align-right { background-image: url("icons/text-right.svg")} \ No newline at end of file diff --git a/www/main.js b/www/main.js index bfcdb99..fb6bb8c 100644 --- a/www/main.js +++ b/www/main.js @@ -307,7 +307,11 @@ class CanvasController { this._thresholdRange = document.querySelector('[name="threshold"]'); this._energyRange = document.querySelector('[name="energy"]'); this.imageUrl = null; - this.textAlign = document.getElementById("text-align"); + this.textAlign = "left"; + + for (let elem of document.querySelectorAll("input[name=text-align]")){ + if (elem.checked) { this.textAlign = elem.value; } + } const prevent_default = (event) => { event.preventDefault(); @@ -339,7 +343,10 @@ class CanvasController { putEvent('#insert-text' , 'click', () => Dialog.alert("#text-input", () => this.insertText(this.textArea.value))); putEvent('#text-size' , 'change', () => this.textArea.style["font-size"] = this.textSize.value + "px"); putEvent('#text-font' , 'change', () => this.textArea.style["font-family"] = this.textFont.value); - putEvent('#text-align' , 'change', () => this.textArea.style["text-align"] = this.textAlign.value); + putEvent('input[name="text-align"]', 'change', (event) => { + this.textAlign = event.currentTarget.value + this.textArea.style["text-align"] = this.textAlign; + }, this); putEvent('input[name="wrap-by-space"]' , 'change', () => this.textArea.style["word-break"] = this.wrapBySpace.checked ? "break-word" : "break-all"); putEvent('#button-preview' , 'click', this.activatePreview , this); putEvent('#button-reset' , 'click', this.reset , this); From 44df87011c80f5c7718ea3900bba0156b8e689f8 Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Sat, 28 May 2022 13:22:57 +0200 Subject: [PATCH 20/24] * Improved element alignment in "Insert Text" dialog --- www/index.html | 60 ++++++++++++++++++++++++--------------------- www/lang/de-DE.json | 6 +---- www/lang/en-US.json | 6 +---- www/main.css | 17 ++++++++++++- 4 files changed, 50 insertions(+), 39 deletions(-) diff --git a/www/index.html b/www/index.html index 6f2b266..ad23430 100644 --- a/www/index.html +++ b/www/index.html @@ -179,36 +179,40 @@

Insert Text

-
+
- - -
- - - - - - - - - - - - - +
+ + + +
+
+ + + + + + + + + + + + + +
diff --git a/www/lang/de-DE.json b/www/lang/de-DE.json index 1ade635..adbebc6 100644 --- a/www/lang/de-DE.json +++ b/www/lang/de-DE.json @@ -107,9 +107,5 @@ "text-font": "Schriftart", "text-size": "Textgröße", "enter-text": "Text eingeben", - "wrap-by-space": "Autom. Zeilenumbruch", - "text-align": "Textausrichtung", - "align-left": "Linksbündig", - "align-mid": "Zentriert", - "align-right": "Rechtsbündig" + "wrap-by-space": "Autom. Zeilenumbruch" } \ No newline at end of file diff --git a/www/lang/en-US.json b/www/lang/en-US.json index 80ba3ce..8c39e0f 100644 --- a/www/lang/en-US.json +++ b/www/lang/en-US.json @@ -132,9 +132,5 @@ "text-font": "Font", "text-size": "Size", "enter-text": "Enter text", - "wrap-by-space": "Wrap words by spaces", - "text-align": "Align text", - "align-left": "Left", - "align-mid": "Center", - "align-right": "Right" + "wrap-by-space": "Wrap words by spaces" } \ No newline at end of file diff --git a/www/main.css b/www/main.css index d14b498..1c3843d 100644 --- a/www/main.css +++ b/www/main.css @@ -699,4 +699,19 @@ body.high-contrast #control-overlay { background-color: var(--shade); } } .text-align-left { background-image: url("icons/text-left.svg")} .text-align-center { background-image: url("icons/text-center.svg")} -.text-align-right { background-image: url("icons/text-right.svg")} \ No newline at end of file +.text-align-right { background-image: url("icons/text-right.svg")} + +input[name="wrap-by-space"] { margin-right: 5px; } +#text-settings { + display: flex; + flex-direction: column; +} +.text-settings-group { + margin-top: 1rem; +} +.text-settings-group[name="wrap-and-align"] { + display: flex; + justify-content: center; +} +label[name="wrap-by-space-label"] { padding-right: 2%; } +#text-font { height: 100%; } \ No newline at end of file From c72658e241853aa1e352cf6694836f87e04fca9c Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Sat, 28 May 2022 13:35:19 +0200 Subject: [PATCH 21/24] * Fixed text clipping out of the text box when increasing the font size * Centered "text-font" and "text-size" inputs * Fixed missing "checked" attribute for the default text alignment option --- www/index.html | 4 ++-- www/main.css | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/www/index.html b/www/index.html index ad23430..e9dec27 100644 --- a/www/index.html +++ b/www/index.html @@ -184,7 +184,7 @@
- @@ -201,7 +201,7 @@
- + diff --git a/www/main.css b/www/main.css index 1c3843d..2614b8c 100644 --- a/www/main.css +++ b/www/main.css @@ -667,6 +667,7 @@ body.high-contrast #control-overlay { background-color: var(--shade); } overflow: hidden auto; white-space: break-spaces; resize:none; + padding-top: .65ex; } .text-align-container span { @@ -714,4 +715,4 @@ input[name="wrap-by-space"] { margin-right: 5px; } justify-content: center; } label[name="wrap-by-space-label"] { padding-right: 2%; } -#text-font { height: 100%; } \ No newline at end of file +#text-font { height: 100%; margin: 0px; } \ No newline at end of file From 4cf1376858876b83214bbba906f0391401544dc8 Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Wed, 1 Jun 2022 11:37:17 +0200 Subject: [PATCH 22/24] * Fixed incorrect icon for aligning text to the right --- www/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/index.html b/www/index.html index e9dec27..684a30d 100644 --- a/www/index.html +++ b/www/index.html @@ -210,7 +210,7 @@ - +
From 5f883908535115008cdc4ac516faa4f263439a38 Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Wed, 1 Jun 2022 12:34:15 +0200 Subject: [PATCH 23/24] * Adjusted line-height of textarea to better reflect the look of the end result --- www/main.css | 1 + 1 file changed, 1 insertion(+) diff --git a/www/main.css b/www/main.css index 2614b8c..2cc5583 100644 --- a/www/main.css +++ b/www/main.css @@ -668,6 +668,7 @@ body.high-contrast #control-overlay { background-color: var(--shade); } white-space: break-spaces; resize:none; padding-top: .65ex; + line-height: 1.25; } .text-align-container span { From 47ace549c6dfbab980a873a342d0f99bffec7695 Mon Sep 17 00:00:00 2001 From: Sync1211 Date: Wed, 1 Jun 2022 12:38:35 +0200 Subject: [PATCH 24/24] * Do not insert text if textarea is empty --- www/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/main.js b/www/main.js index fb6bb8c..9e87b1e 100644 --- a/www/main.js +++ b/www/main.js @@ -469,7 +469,7 @@ class CanvasController { } } insertText(text) { - if (text == null) { return; } + if (text == null || text.trim() == "") { return; } const text_size = parseInt(this.textSize.value); const text_font = this.textFont.value;