+ Added comments to getMaxCharsPerLine and wrapText

+ Added German and English strings for text-size and text-font
This commit is contained in:
Sync1211 2022-05-18 12:12:11 +02:00
parent 8f402eb80f
commit 7afe68c151
No known key found for this signature in database
GPG Key ID: B8878699435E69EC
3 changed files with 10 additions and 3 deletions

View File

@ -43,6 +43,8 @@
"coming-soon": "Demnächst verfügbar…",
"dry-run": "Testlauf",
"dry-run-test-print-process-only": "Testlauf: nur Probedruckvorgang",
"text-font": "Schriftart",
"text-size": "Textgröße",
"you-can-close-this-page-manually": "Sie können diese Seite manuell schließen",
"please-enable-bluetooth": "Bitte aktivieren Sie Bluetooth",
"error-happened-please-check-error-message": "Fehler aufgetreten, bitte Fehlermeldung einsehen",

View File

@ -42,6 +42,8 @@
"coming-soon": "Coming Soon…",
"dry-run": "Dry Run",
"dry-run-test-print-process-only": "Dry Run: test print process only",
"text-font": "Font",
"text-size": "Text size",
"you-can-close-this-page-manually": "You can close this page manually",
"please-enable-bluetooth": "Please enable Bluetooth",
"error-happened-please-check-error-message": "Error happened, please check error message",

View File

@ -455,13 +455,17 @@ class CanvasController {
// Use Word Wrap to split the text over multiple lines
let lines = [];
const getMaxCharsPerLine = (text, ctx) => {
// Calculate the aproximate maximum length of a string
// taking font and text size in account
const getMaxCharsPerLine = (text, ctx) => {
let textWidth = ctx.measureText(text).width;
let textIndex = maxWidth / textWidth;
if (textIndex > 1) { return text.length}
return Math.floor(textIndex * text.length);
}
// Wrap the text if it does not fit on a single line
const wrapText = (text, maxLength) => {
let splitPos = maxLength - 1;
while (text[splitPos] != " " && splitPos > 0) {
@ -470,7 +474,7 @@ class CanvasController {
if (splitPos == 0) { splitPos = maxLength; }
return [text.slice(0, splitPos).trim(), text.slice(splitPos, text.length).trim()];
}
while (ctx.measureText(text).width > maxWidth) {
let line;
[line, text] = wrapText(text, getMaxCharsPerLine(text, ctx));
@ -478,7 +482,6 @@ class CanvasController {
}
lines.push(text)
let yPos = yStep;
for (let line of lines) {
ctx.fillText(line, 0, yPos);