|
| 1 | +/** |
| 2 | + * Calculates the optimal font size and position for text display |
| 3 | + * @param {string} testo - The text to display |
| 4 | + * @param {number} dimensione - Scaling factor for the font size |
| 5 | + * @param {number} interlinea - Line spacing factor |
| 6 | + * @param {object} font - The p5.js font object |
| 7 | + * @param {string} allineamento - Text alignment: "centro", "sinistra", or "destra" |
| 8 | + * @param {number} width - Canvas width |
| 9 | + * @param {number} height - Canvas height |
| 10 | + */ |
| 11 | +export function calculateTextProperties( |
| 12 | + testo, |
| 13 | + dimensione, |
| 14 | + interlinea, |
| 15 | + font, |
| 16 | + allineamento, |
| 17 | + width, |
| 18 | + height |
| 19 | +) { |
| 20 | + // Calculate the optimal font size |
| 21 | + const fontSize = calculateFontSize( |
| 22 | + testo, |
| 23 | + dimensione, |
| 24 | + interlinea, |
| 25 | + font, |
| 26 | + width, |
| 27 | + height |
| 28 | + ); |
| 29 | + |
| 30 | + // Calculate the text bounds at the calculated font size |
| 31 | + const bounds = getTextBounds( |
| 32 | + testo, |
| 33 | + fontSize, |
| 34 | + interlinea, |
| 35 | + font, |
| 36 | + getTextAlignment(allineamento) |
| 37 | + ); |
| 38 | + |
| 39 | + // Calculate position based on alignment |
| 40 | + const position = calculatePosition(bounds, allineamento, width, height); |
| 41 | + |
| 42 | + return { |
| 43 | + fontSize, |
| 44 | + position, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Calculates the optimal font size to fit the text within the canvas |
| 50 | + * @param {string} testo - The text to display |
| 51 | + * @param {number} dimensione - Scaling factor for the font size |
| 52 | + * @param {number} interlinea - Line spacing factor |
| 53 | + * @param {object} font - The p5.js font object |
| 54 | + * @param {number} width - Canvas width |
| 55 | + * @param {number} height - Canvas height |
| 56 | + * @returns {number} The calculated optimal font size |
| 57 | + */ |
| 58 | +function calculateFontSize(testo, dimensione, interlinea, font, width, height) { |
| 59 | + const testFontSize = 10; |
| 60 | + |
| 61 | + // Calculate scaling with test font size |
| 62 | + push(); |
| 63 | + textSize(testFontSize); |
| 64 | + textLeading(testFontSize * interlinea); |
| 65 | + textAlign(getTextAlignment("centro")); |
| 66 | + const bounds = font.textBounds(testo, 0, 0); |
| 67 | + pop(); |
| 68 | + |
| 69 | + // Calculate the ratio needed to fit the width and height within canvas |
| 70 | + const widthRatio = width / bounds.w; |
| 71 | + const heightRatio = height / bounds.h; |
| 72 | + |
| 73 | + // Use the smaller ratio to ensure text fits in both dimensions |
| 74 | + // Apply some margin by multiplying by 0.9 (90% of available space) |
| 75 | + const ratio = Math.min(widthRatio, heightRatio) * 0.9; |
| 76 | + |
| 77 | + // Calculate the base font size (without user scaling) |
| 78 | + const baseFontSize = testFontSize * ratio; |
| 79 | + |
| 80 | + // Apply user's dimensione scaling factor to get the actual font size |
| 81 | + return baseFontSize * dimensione; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Gets the text bounds at a specific font size |
| 86 | + */ |
| 87 | +function getTextBounds(testo, fontSize, interlinea, font, alignment) { |
| 88 | + push(); |
| 89 | + textSize(fontSize); |
| 90 | + textLeading(fontSize * interlinea); |
| 91 | + textAlign(alignment); |
| 92 | + const bounds = font.textBounds(testo, 0, 0); |
| 93 | + pop(); |
| 94 | + |
| 95 | + return bounds; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Calculates the position for drawing text based on alignment |
| 100 | + * @param {object} bounds - The text bounds object containing x, y, w, h properties |
| 101 | + * @param {string} allineamento - The text alignment ('centro', 'sinistra', or 'destra') |
| 102 | + * @param {number} width - The canvas width |
| 103 | + * @param {number} height - The canvas height |
| 104 | + * @returns {object} An object containing x and y coordinates for text positioning |
| 105 | + */ |
| 106 | +function calculatePosition(bounds, allineamento, width, height) { |
| 107 | + let xPos, yPos; |
| 108 | + |
| 109 | + if (allineamento === "centro") { |
| 110 | + xPos = width / 2; |
| 111 | + // Adjust y position to account for the actual center of the text bounds |
| 112 | + yPos = height / 2 - bounds.y - bounds.h / 2; |
| 113 | + } else if (allineamento === "sinistra") { |
| 114 | + xPos = width * 0.1; // 10% margin from left |
| 115 | + yPos = height / 2 - bounds.y - bounds.h / 2; |
| 116 | + } else if (allineamento === "destra") { |
| 117 | + xPos = width * 0.9; // 10% margin from right |
| 118 | + yPos = height / 2 - bounds.y - bounds.h / 2; |
| 119 | + } |
| 120 | + |
| 121 | + return { x: xPos, y: yPos }; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Gets p5.js text alignment constant from string |
| 126 | + * @param {string} allineamento - The text alignment string ('centro', 'sinistra', or 'destra') |
| 127 | + */ |
| 128 | +function getTextAlignment(allineamento) { |
| 129 | + switch (allineamento) { |
| 130 | + case "centro": |
| 131 | + return CENTER; |
| 132 | + case "sinistra": |
| 133 | + return LEFT; |
| 134 | + case "destra": |
| 135 | + return RIGHT; |
| 136 | + default: |
| 137 | + return CENTER; |
| 138 | + } |
| 139 | +} |
0 commit comments