+2
-2
@@ -42,7 +42,7 @@ router.get('/badge', async (req, res) => {
|
|||||||
const iconSize = size;
|
const iconSize = size;
|
||||||
const fontSize = calcFontSize(iconSize);
|
const fontSize = calcFontSize(iconSize);
|
||||||
const textPadding = Math.round(iconSize * 0.4);
|
const textPadding = Math.round(iconSize * 0.4);
|
||||||
const textHpad = Math.round(fontSize * 0.6); // horizontal padding inside text rect
|
const textHpad = 4; // horizontal padding inside text rect — enough to prevent clipping
|
||||||
const radius = style === 'flat' ? 3 : 0;
|
const radius = style === 'flat' ? 3 : 0;
|
||||||
const textWidth = estimateTextWidth(effectiveLabel, fontSize) + textHpad * 2;
|
const textWidth = estimateTextWidth(effectiveLabel, fontSize) + textHpad * 2;
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ router.get('/badge', async (req, res) => {
|
|||||||
if (heightMatch) iconViewBoxHeight = parseFloat(heightMatch[1]);
|
if (heightMatch) iconViewBoxHeight = parseFloat(heightMatch[1]);
|
||||||
|
|
||||||
// Ikon doboz mérete
|
// Ikon doboz mérete
|
||||||
const iconPad = 5;
|
const iconPad = 2;
|
||||||
const iconBoxWidth = iconViewBoxWidth + iconPad;
|
const iconBoxWidth = iconViewBoxWidth + iconPad;
|
||||||
const iconBoxHeight = iconViewBoxHeight + iconPad;
|
const iconBoxHeight = iconViewBoxHeight + iconPad;
|
||||||
|
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ const calcFontSize = (size) => Math.round(size * 0.6);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Text-width estimate based on font-size and character count.
|
* Text-width estimate based on font-size and character count.
|
||||||
* Uses a 0.65 coefficient with Verdana which has wider letterforms.
|
* Uses a 0.7 coefficient — Verdana has wide letterforms (M, W, D are ~70% of fontSize).
|
||||||
* Adds extra padding so the text never touches the rect edges.
|
* Returns only the raw text bounding box — padding is added by callers.
|
||||||
*/
|
*/
|
||||||
const estimateTextWidth = (text, fontSize) =>
|
const estimateTextWidth = (text, fontSize) =>
|
||||||
text.length * fontSize * 0.65 + fontSize * 0.8;
|
text.length * fontSize * 0.7;
|
||||||
|
|
||||||
module.exports = { FONT_FAMILY, calcFontSize, estimateTextWidth };
|
module.exports = { FONT_FAMILY, calcFontSize, estimateTextWidth };
|
||||||
|
|||||||
@@ -29,21 +29,19 @@ describe('svg-utils', () => {
|
|||||||
describe('estimateTextWidth', () => {
|
describe('estimateTextWidth', () => {
|
||||||
it('should estimate width based on text length and font size', () => {
|
it('should estimate width based on text length and font size', () => {
|
||||||
const width = estimateTextWidth('Hello', 14);
|
const width = estimateTextWidth('Hello', 14);
|
||||||
// formula: text.length * fontSize * 0.65 + fontSize * 0.8
|
// formula: text.length * fontSize * 0.7
|
||||||
expect(width).toBe(5 * 14 * 0.65 + 14 * 0.8); // 56.7
|
expect(width).toBe(5 * 14 * 0.7); // 49
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should include padding for empty string', () => {
|
it('should return 0 for empty string', () => {
|
||||||
// Even empty strings get the base padding
|
expect(estimateTextWidth('', 14)).toBe(0);
|
||||||
expect(estimateTextWidth('', 14)).toBe(14 * 0.8); // 11.2
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should scale with font size', () => {
|
it('should scale with font size', () => {
|
||||||
const w1 = estimateTextWidth('test', 10);
|
const w1 = estimateTextWidth('test', 10);
|
||||||
const w2 = estimateTextWidth('test', 20);
|
const w2 = estimateTextWidth('test', 20);
|
||||||
// w1 = 4*10*0.65 + 10*0.8 = 34
|
expect(w1).toBe(4 * 10 * 0.7); // 28
|
||||||
// w2 = 4*20*0.65 + 20*0.8 = 68
|
expect(w2).toBe(4 * 20 * 0.7); // 56
|
||||||
expect(w2).toBe(68);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user