summaryrefslogtreecommitdiffstats
path: root/web/api/ilove/measure-text.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-10-17 09:30:23 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-10-17 09:30:23 +0000
commit517a443636daa1e8085cb4e5325524a54e8a8fd7 (patch)
tree5352109cc7cd5122274ab0cfc1f887b685f04edf /web/api/ilove/measure-text.js
parentReleasing debian version 1.42.4-1. (diff)
downloadnetdata-517a443636daa1e8085cb4e5325524a54e8a8fd7.tar.xz
netdata-517a443636daa1e8085cb4e5325524a54e8a8fd7.zip
Merging upstream version 1.43.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'web/api/ilove/measure-text.js')
-rw-r--r--web/api/ilove/measure-text.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/web/api/ilove/measure-text.js b/web/api/ilove/measure-text.js
new file mode 100644
index 00000000..e2a2a6e9
--- /dev/null
+++ b/web/api/ilove/measure-text.js
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+'use strict';
+
+var path = require('path');
+var fs = require('fs');
+var PDFDocument = require('pdfkit');
+var doc = new PDFDocument({size:'A4', layout:'landscape'});
+
+function loadFont(fontPaths, callback) {
+ for (let fontPath of fontPaths) {
+ try {
+ doc = doc.font(fontPath);
+ if (callback) { callback(null); }
+ return; // Exit once a font is loaded successfully
+ } catch(err) {
+ // Log error but continue to next font path
+ console.error(`Failed to load font from path: ${fontPath}. Error: ${err.message}`);
+ }
+ }
+
+ // If we reached here, none of the fonts were loaded successfully.
+ console.error('All font paths failed. Stopping execution.');
+ process.exit(1); // Exit with an error code
+}
+
+loadFont(['IBMPlexSans-Bold.ttf'], function(err) {
+ if (err) {
+ console.error('Could not load any of the specified fonts.');
+ }
+});
+
+doc = doc.fontSize(250);
+
+function measureCombination(charA, charB) {
+ return doc.widthOfString(charA + charB);
+}
+
+function getCharRepresentation(charCode) {
+ return (charCode >= 32 && charCode <= 126) ? String.fromCharCode(charCode) : '';
+}
+
+function generateCombinationArray() {
+ let output = "static const unsigned short int ibm_plex_sans_bold_250[128][128] = {\n";
+
+ for (let i = 0; i <= 126; i++) {
+ output += " {"; // Start of inner array
+ for (let j = 0; j <= 126; j++) {
+ let charA = getCharRepresentation(i);
+ let charB = getCharRepresentation(j);
+ let width = measureCombination(charA, charB) - doc.widthOfString(charB);
+ let encodedWidth = Math.round(width * 100); // Multiply by 100 and round
+
+ if(charA === '*' && charB == '/')
+ charB = '\\/';
+
+ if(charA === '/' && charB == '*')
+ charB = '\\*';
+
+ output += `${encodedWidth} /* ${charA}${charB} */`;
+ if (j < 126) {
+ output += ", ";
+ }
+ }
+ output += "},\n"; // End of inner array
+ }
+ output += "};\n"; // End of 2D array
+
+ return output;
+}
+
+console.log(generateCombinationArray());
+console.log('static const unsigned short int ibm_plex_sans_bold_250_em_size = ' + Math.round(doc.widthOfString('M') * 100) + ';');