{"library":"server-text-width","title":"Server-side Text Width Calculation","description":"server-text-width is a utility library designed for server-side environments, specifically Node.js, to accurately calculate the pixel width of text. Unlike browser-based solutions that leverage Canvas or DOM elements, this package operates without a browser environment by relying on pre-generated lookup tables. This capability is crucial for server-side rendering (SSR) scenarios where text needs to be trimmed, aligned, or used in layout calculations without client-side dependencies. The current stable version is 1.0.2, indicating a nascent but stable 1.x release series. Its key differentiators include zero runtime dependencies, full Unicode support, compatibility with custom fonts, and a highly compact base64 encoding for character width data. Developers must use an included `mappingTool.html` to generate these character width lookup tables for their specific fonts, sizes, and weights, which are then passed to the library's `init` function. This approach ensures consistent text measurement results across environments and allows for precise control over font metrics.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install server-text-width"],"cli":null},"imports":["import { init } from 'server-text-width';","const { getTextWidth } = init(lookupTable);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const TEXT_WIDTH_LOOKUP_TABLE = {\n    // This is a highly truncated example. In a real application, this table\n    // would be much larger and generated using the 'mappingTool.html' provided\n    // with the package, covering all necessary fonts, sizes, weights, and Unicode ranges.\n    \"Arial|16px|normal|0\": \"aAaAaAaAaAaAaAaAaAIAIAIAIAIAaAaAaAaAaAaAaAaAaAaAaAaAaAaAIAIAIAIAIAKqRxQAQAgAaqI5KqKqQASOIAKqIAI5QAQAQAQAQAQAQAQAQAQAKqKqSOSOSOQAdxXGVVXGXGVVTiY5Y5MdQAY5VVeMXGY5TiY5XGRzVVXGXGgAXGXGVVKqI5KqSmQAKqQARzOMRzOMKqQARzI5KqRzI5aqRzQARzRzOMMdKqRzQAXGQAQAOMMmHCMmQoaA\",\n    \"Times|32px|bold|0\": \"aAaAaAaAaAaAaAaAaAIAIAIAIAIAaAaAaAaAaAaAaAaAaAaAaAaAaAaAIAIAIAIAIAKqRxQAQAgAaqI5KqKqQASOIAKqIAI5QAQAQAQAQAQAQAQAQAQAKqKqSOSOSOQAdxXGVVXGXGVVTiY5Y5MdQAY5VVeMXGY5TiY5XGRzVVXGXGgAXGXGVVKqI5KqSmQAKqQARzOMRzOMKqQARzI5KqRzI5aqRzQARzRzOMMdKqRzQAXGQAQAOMMmHCMmQoaA\"\n};\n\nimport { init } from 'server-text-width';\n\n// Initialize the library with your pre-generated lookup table.\n// The table keys must match the font strings used when calling getTextWidth.\nconst { getTextWidth } = init(TEXT_WIDTH_LOOKUP_TABLE);\n\n// Measure the width of a string using a font configuration present in the lookup table.\nconst textToMeasure = 'Hello, server-side world! Привет, мир!';\nconst width = getTextWidth(textToMeasure, 'Arial|16px|normal');\n\nconsole.log(`The width of \"${textToMeasure}\" (Arial 16px normal) is ${width} pixels.`);\n\n// You can also measure text with different font styles if your lookup table includes them.\nconst anotherText = 'Bold text example';\nconst boldWidth = getTextWidth(anotherText, 'Times|32px|bold');\n\nconsole.log(`The width of \"${anotherText}\" (Times 32px bold) is ${boldWidth} pixels.`);","lang":"typescript","description":"This quickstart demonstrates how to initialize the `server-text-width` library with a pre-generated lookup table and then use the `getTextWidth` function to calculate the pixel width of various strings with specified font styles. It highlights the manual setup required for the lookup table.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}