{"name":"measury","version":"0.1.1","keywords":["text","width","height","measure","font","typography","text-metrics"],"author":"","license":"MIT","_id":"measury@0.1.1","maintainers":[{"name":"iaaron","email":"943720372@qq.com"}],"homepage":"https://github.com/Aarebecca/measury#readme","bugs":{"url":"https://github.com/Aarebecca/measury/issues"},"dist":{"shasum":"f6331ca27c64d8867f22b4fcb14a2d0f9431c898","tarball":"http://registry-npm.ywwl.com/measury/download/measury-0.1.1.tgz","fileCount":24,"integrity":"sha512-yjRcNrcoYTIiqU3jsp2C6pd90LZQsI+7YJ7YNKhYwSERVQOuRpjOJZTWMjdV/QELx7clFhvwRbJz1SNeyBlR0Q==","signatures":[{"sig":"MEYCIQCjp4hY0La9q2e7Tc5ANf/sP9otXFIHFMLhz+rekam5/gIhAIb9PCM9GUAuU0iXHjoioLQOmIGk3sP/BA9iKnsfpGN1","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":257969,"size":53297,"noattachment":false,"key":"/measury/-/measury-0.1.1.tgz"},"main":"lib/index.js","types":"lib/index.d.ts","exports":{".":{"types":"./lib/index.d.ts","default":"./lib/index.js"},"./*":{"types":"./lib/fonts/*.d.ts","default":"./lib/fonts/*.js"}},"gitHead":"51ca920083867ff4fcec2953fc6e71d529d53bfd","scripts":{"ci":"run-p lint test build","dev":"npx serve test-browser -p 8090","lint":"eslint src/**/*.ts","test":"vitest run","build":"tsc","extract":"tsx scripts/extract-font.ts","prebuild":"rm -rf lib","test:watch":"vitest","test:coverage":"vitest run --coverage","prepublishOnly":"npm run build"},"_npmUser":{"name":"iaaron","email":"943720372@qq.com"},"repository":{"url":"git+https://github.com/Aarebecca/measury.git","type":"git"},"_npmVersion":"10.8.2","description":"A lightweight library for estimating text width and height in non-browser environments","directories":{},"sideEffects":false,"_nodeVersion":"20.19.5","_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.21.0","eslint":"^9.39.1","vitest":"^4.0.15","globals":"^16.5.0","minimist":"^1.2.8","prettier":"^3.7.4","typescript":"^5.9.3","@types/node":"^24.10.2","npm-run-all":"^4.1.5","opentype.js":"^1.3.4","@types/minimist":"^1.2.5","typescript-eslint":"^8.49.0","@types/opentype.js":"^1.3.8","@typescript-eslint/eslint-plugin":"^8.49.0"},"_npmOperationalInternal":{"tmp":"tmp/measury_0.1.1_1765422973953_0.4114661485857858","host":"s3://npm-registry-packages-npm-production"},"readme":"# Measury\n\n一个轻量级的 JavaScript 库，用于在非浏览器环境中估算文本的宽度和高度。\n\n与 `node-canvas` 或 `opentype` 等重量级方案不同，本库提供了一个基于字体度量配置的轻量级替代方案，适用于近似文本测量。\n\n## ✨ 特性\n\n- 📏 精确的文本宽度和高度测量\n- 🎨 支持多种文本样式（字体、大小、粗细、间距等）\n- 🔤 文本转换支持（大写、小写、首字母大写）\n- 📦 **轻量级** - 按需加载，核心库仅 3-5KB\n- 🌲 **Tree-shaking 友好** - 只打包使用的字体\n- 🚀 快速 - 基于预计算的字体度量\n- 🛠️ 完整的 TypeScript 类型定义\n- 🔧 内置字体提取脚本\n\n## 📦 安装\n\n```bash\nnpm install measury\n```\n\n## 🚀 快速开始\n\n### 方式 1：使用系统字体（最小体积）\n\n```typescript\nimport { measureText } from 'measury';\n\n// 测量系统字体，无需额外配置\nconst metrics = measureText('Hello World', {\n  fontFamily: 'Arial',\n  fontSize: 16,\n});\n\nconsole.log(metrics.width);    // 文本宽度（像素）\nconsole.log(metrics.height);   // 行高（像素）\nconsole.log(metrics.baseline); // 基线位置（像素）\n```\n\n### 方式 2：按需加载字体数据（精确测量）\n\n```typescript\nimport { measureText, registerFont } from 'measury';\nimport AlibabaPuHuiTiRegular from 'measury/fonts/AlibabaPuHuiTi-Regular';\n\n// 注册字体\nregisterFont(AlibabaPuHuiTiRegular);\n\n// 精确测量\nconst metrics = measureText('你好世界', {\n  fontFamily: 'Alibaba PuHuiTi',\n  fontSize: 16,\n  fontStyle: 'normal', // 支持 'normal' | 'italic' | 'oblique'\n});\n```\n\n**优势**：\n- ✅ 核心库体积小（3-5KB），只在需要时加载字体数据\n- ✅ Tree-shaking 友好，打包工具自动去除未使用的字体\n- ✅ 灵活控制打包体积\n- ✅ 支持多种字体样式（normal, italic, oblique）\n\n**字体导入方式**：\n\n```typescript\nimport AlibabaPuHuiTiRegular from 'measury/fonts/AlibabaPuHuiTi-Regular';\n```\n\n## 📖 API\n\n### `measureText(text, style?)`\n\n测量单行文本的尺寸。\n\n**参数：**\n- `text` - 要测量的文本\n- `style` - 可选的文本样式配置\n\n**返回：** `TextMetrics` 对象\n- `width` - 文本宽度（像素）\n- `height` - 行高（像素）\n- `baseline` - 基线位置（像素）\n\n**样式选项：**\n\n```typescript\ninterface TextStyle {\n  fontSize?: number;              // 默认: 16\n  fontFamily?: string;            // 默认: 'sans-serif'\n  fontWeight?: 'normal' | 'bold' | number;\n  fontStyle?: 'normal' | 'italic' | 'oblique';\n  textTransform?: 'none' | 'capitalize' | 'uppercase' | 'lowercase';\n  letterSpacing?: number;         // 字符间距（像素）\n  wordSpacing?: number;           // 单词间距（像素）\n  lineHeight?: number | { type: 'pixel', value: number };\n}\n```\n\n### `registerFont(fontData)`\n\n注册字体供文本测量使用。\n\n```typescript\nimport { registerFont } from 'measury';\nimport MyFont from 'measury/myfont';\n\nregisterFont(MyFont);\n```\n\n支持在字体数据中声明别名：\n\n```typescript\nregisterFont({\n  ...MyFont,\n  aliases: ['AlibabaPuHuiTi', 'AlibabaPuHuiTi-Regular'],\n});\n```\n\n### `setDefaultFontFamily(fontFamily)`\n\n设置默认字体族。\n\n```typescript\nimport { setDefaultFontFamily } from 'measury';\n\nsetDefaultFontFamily('Alibaba PuHuiTi');\n```\n\n## 🔧 从 TTF 文件提取字体数据\n\n本库内置了从 TTF 文件提取字体度量数据的脚本。\n\n### 基本用法\n\n```bash\nnpm run extract <字体文件路径.ttf>\n```\n\n这将：\n1. 从 TTF/OFT 文件提取字体数据\n2. 在 `src/fonts/` 生成 TypeScript 文件\n3. 自动添加到 `src/fonts/index.ts`\n\n### 示例\n\n**提取字体：**\n```bash\nnpm run extract fonts/Roboto-Regular.ttf\n# 生成: src/fonts/roboto-regular.ts\n```\n\n**指定字重：**\n```bash\nnpm run extract fonts/Roboto-Bold.ttf --weight 700\n# 或简写\nnpm run extract fonts/Roboto-Bold.ttf -w 700\n```\n\n**指定字体样式：**\n```bash\nnpm run extract fonts/Roboto-Italic.ttf --style italic\n# 或简写\nnpm run extract fonts/Roboto-Italic.ttf -s italic\n```\n\n**自定义字体名称：**\n```bash\nnpm run extract fonts/CustomFont.ttf --family \"自定义字体\"\n```\n\n**只提取特定字符（减小文件体积）：**\n```bash\n# 只提取数字\nnpm run extract fonts/Arial.ttf --charset \"0123456789\"\n\n# 只提取英文字母\nnpm run extract fonts/Font.ttf -c \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\"\n```\n\n**跳过字偶距数据：**\n```bash\nnpm run extract fonts/Font.ttf --no-kerning\n```\n\n### 选项说明\n\n| 选项 | 简写 | 说明 | 示例 |\n|------|------|------|------|\n| `--weight <weight>` | `-w` | 设置字重 | `--weight 700` |\n| `--style <style>` | `-s` | 设置字体样式 (normal/italic/oblique) | `--style italic` |\n| `--family <name>` | `-f` | 自定义字体名称 | `--family \"我的字体\"` |\n| `--charset <chars>` | `-c` | 只提取指定字符 | `--charset \"ABC123\"` |\n| `--output <file>` | `-o` | 输出文件路径 | `-o src/fonts/my-font.ts` |\n| `--no-kerning` | - | 跳过字偶距数据 | `--no-kerning` |\n\n### 提取后使用\n\n```typescript\nimport { measureText } from 'measury';\n\nconst metrics = measureText('Hello', {\n  fontFamily: 'Roboto',\n  fontSize: 16,\n});\n```\n\n### 优化建议\n\n- **减小文件体积**：\n  - 使用 `--charset` 只提取需要的字符\n  - 使用 `--no-kerning` 跳过字偶距数据\n  - 脚本自动优化：将最常见的字形宽度设为默认值\n  \n- **字重**：分别提取每个字重（Regular、Bold 等）\n\n- **提取速度**：\n  - 小字体（拉丁字符，~100-500 字形）：< 1 秒\n  - 大字体（CJK，10,000+ 字形）：1-10 秒\n\n## 🧪 测试\n\n运行测试：\n\n```bash\nnpm test\n```\n\n监听模式：\n\n```bash\nnpm run test:watch\n```\n\n生成覆盖率报告：\n\n```bash\nnpm run test:coverage\n```\n\n### 浏览器验证\n\n验证测量准确性：\n\n```bash\nnpm run dev\n# 访问 http://localhost:8090\n```\n\n功能：\n- ✅ 包含字体文件，开箱即用\n- ✅ 对比真实浏览器渲染结果\n- ✅ 生成测试代码\n- ✅ 验证测量精度\n\n## ⚙️ 工作原理\n\n1. 从 TTF 文件预提取字体度量数据（字形宽度、垂直度量、字偶距）\n2. 通过累加每个字符的推进宽度来测量文本\n3. 应用字符对之间的字偶距调整\n4. 添加指定的字符间距和单词间距\n5. 根据字体度量或样式设置计算行高\n\n这种方法提供快速的近似测量，无需完整字体渲染的开销。\n\n## ⚠️ 限制\n\n- 仅支持单行文本测量（不支持自动换行）\n- 需要预注册字体数据\n- 测量结果是基于字体度量的近似值，非像素完美\n- 不考虑字体微调、亚像素渲染或平台特定的文本渲染差异\n\n## 📄 许可证\n\nMIT\n","publish_time":1765422974103,"_cnpm_publish_time":1765422974103,"dist-tags":{"latest":"0.1.5"}}