@evenrealities/pretext

中文文档
0.1.4发布于 2026/04/16MITTypeScriptES Module零依赖近 30 天下载 12.9万

Pixel-accurate font measurement library for Even Realities G2 glasses

原始使用文档
文档目录13 节
  1. Features
  2. Installation
  3. API
  4. getTextWidth(text: string): number
  5. measureTextWrap(text: string, maxWidth: number): MeasureTex…
  6. pxTruncate(text: string, maxPx: number): string
  7. getAdvW(cp: number): number
  8. Display Reference
  9. How It Works
  10. Changelog
  11. 0.1.4
  12. 0.1.3
  13. 0.1.2

@evenrealities/pretext

Pixel-accurate font measurement library for Even Realities G2 smart glasses (EvenHub specifically). Predicts exact text layout dimensions matching the LVGL rendering engine used in the glasses firmware, so you can size UI containers precisely without guesswork. Inspired by pretext.

Note: Measurements have been manually compared and verified against the actual glasses rendering for most cases, but edge cases may exist where predictions differ from the firmware output. In particular, results may be off for characters that are not present in the firmware fonts, since the firmware and this library may handle missing glyphs differently in some cases.

Features

  • Text measurement — multi-line layout prediction with word wrapping
  • Kerning — per-character pair adjustments using the firmware's exact formula
  • Wide character support — Latin, Cyrillic, Greek, CJK (Chinese, Japanese, Korean), Bopomofo

Installation

npm install @evenrealities/pretext

API

getTextWidth(text: string): number

Returns the single-line pixel width of a string, with kerning applied.

import { getTextWidth } from '@evenrealities/pretext';

const width = getTextWidth('Hello, world!');
// => 79 (pixels)

measureTextWrap(text: string, maxWidth: number): MeasureTextResult

Measures multi-line text layout within the given pixel width. Handles word wrapping at spaces and hyphens, CJK boundary breaks, explicit newlines, and hard breaks when no break opportunity exists.

maxWidth should be the inner content width — if your container has padding or borders, subtract them before calling. The returned height is pure text height; add your own padding/border to get the final container size.

import { measureTextWrap } from '@evenrealities/pretext';

const result = measureTextWrap('The quick brown fox jumps over the lazy dog', 200);
// => {
//   lineCount: 2,
//   height: 54,        // lineCount * 27 (line height)
//   lineWidths: [192, 96]
// }

// With padding + border:
const pad = 8, border = 2;
const inner = 300 - 2 * (pad + border);  // 280px available for text
const m = measureTextWrap('Hello world', inner);
const containerHeight = m.height + 2 * (pad + border);

Parameters:

ParameterTypeDefaultDescription
textstring—The string to measure
maxWidthnumber—Available width for text in pixels (subtract padding/border first)

Return type:

FieldTypeDescription
lineCountnumberNumber of lines after wrapping
heightnumberTotal text height in pixels (lineCount * 27)
lineWidthsnumber[]Pixel width of each line

pxTruncate(text: string, maxPx: number): string

Truncates a string to fit within a pixel budget, appending '...' if needed. Returns the original string if it already fits. Uses binary search for efficient fitting and is codepoint-aware (won't break surrogate pairs like emoji).

import { pxTruncate } from '@evenrealities/pretext';

const truncated = pxTruncate('Hello, world!', 50);
// => 'Hell...'

getAdvW(cp: number): number

Returns the raw advance width of a codepoint in 1/16px units (no kerning, no rounding). Follows the font fallback chain: evenroster -> evenroster_crylgrek -> cn. Primarily useful for debugging or building custom measurement logic.

Display Reference

The G2 glasses have a 576 x 288 pixel display rendered by LVGL with a fixed line height of 27px.

How It Works

The library embeds extracted font metrics from the LVGL C font files used in the G2 firmware. It replicates the firmware's text shaping pipeline:

  1. Glyph lookup — resolves each codepoint through a three-font fallback chain (Latin -> Cyrillic/Greek -> CJK)
  2. Kerning — applies pair adjustments using the firmware formula: (kern_value * kern_scale) >> 4
  3. Per-glyph rounding — each advance width is rounded to pixels individually ((adv + kern + 8) >> 4), matching LVGL's behavior exactly
  4. Line breaking — breaks at spaces, hyphens, and CJK boundaries, with hard breaks as fallback

Note: if the firmware font metrics change, this library must be updated to match.

Changelog

0.1.4

  • add Emoji font

0.1.3

  • Breaking: Removed containerPadding parameter from measureTextWrap. The function now measures pure text — callers should subtract padding/border from maxWidth before calling and add it back to the returned height. This avoids the inconsistency where padding was handled internally but border width was not.
  • Breaking: Removed measureList. Its logic was trivial (item count × 40px, plus basic width comparisons) and callers can replicate it inline with getTextWidth.

0.1.2

  • Initial public release with getTextWidth, measureTextWrap, pxTruncate, measureList, and getAdvW.

版本与发布记录

@evenrealities/pretext 共发布 4 个版本,首次发布于 2026/04/09;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.1.4latest2026/04/166 个月前
0.1.32026/04/137 个月前
0.1.22026/04/097 个月前
0.1.12026/04/097 个月前

关于 @evenrealities/pretext 的常见问题

@evenrealities/pretext 是什么?

Pixel-accurate font measurement library for Even Realities G2 glasses

如何安装 @evenrealities/pretext?

在项目目录运行 npm install @evenrealities/pretext,或使用 pnpm add / yarn add / bun add。

@evenrealities/pretext 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@evenrealities/pretext 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@evenrealities/pretext 可以商用吗?

@evenrealities/pretext 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@evenrealities/pretext 的下载量有多少?

根据 npm 官方下载统计,@evenrealities/pretext 在 2026/10/14 – 2026/11/12 这 30 天内被下载 128,566 次,日均约 4,286 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@evenrealities/pretext 还在维护吗?

最新版本 0.1.4 发布于 2026/04/16(6 个月前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/04/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。