Skip to content

Commit 237692b

Browse files
committed
docs(guides): add fonts guide
Covers standard 14 fonts, embedding custom fonts, text measurement, alignment, and common patterns like fitting text to width.
1 parent 71701ec commit 237692b

File tree

2 files changed

+223
-1
lines changed

2 files changed

+223
-1
lines changed

content/docs/guides/fonts.mdx

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
---
2+
title: Fonts
3+
description: Work with standard fonts, embed custom fonts, and measure text.
4+
---
5+
6+
# Fonts
7+
8+
This guide covers font embedding, standard PDF fonts, and text measurement.
9+
10+
## Standard 14 Fonts
11+
12+
PDF defines 14 fonts that every PDF reader must support. These fonts don't need embedding — they're built into the reader.
13+
14+
```typescript
15+
import { StandardFonts } from "@libpdf/core";
16+
17+
page.drawText("Hello", {
18+
x: 50,
19+
y: 700,
20+
size: 12,
21+
font: StandardFonts.Helvetica,
22+
});
23+
```
24+
25+
Available fonts:
26+
27+
| Name | Description |
28+
| ------------------------ | ------------------------ |
29+
| `Helvetica` | Sans-serif |
30+
| `HelveticaBold` | Sans-serif bold |
31+
| `HelveticaOblique` | Sans-serif italic |
32+
| `HelveticaBoldOblique` | Sans-serif bold italic |
33+
| `TimesRoman` | Serif |
34+
| `TimesBold` | Serif bold |
35+
| `TimesItalic` | Serif italic |
36+
| `TimesBoldItalic` | Serif bold italic |
37+
| `Courier` | Monospace |
38+
| `CourierBold` | Monospace bold |
39+
| `CourierOblique` | Monospace italic |
40+
| `CourierBoldOblique` | Monospace bold italic |
41+
| `Symbol` | Greek and math symbols |
42+
| `ZapfDingbats` | Decorative symbols |
43+
44+
---
45+
46+
## Embedding Custom Fonts
47+
48+
For fonts not in the Standard 14, embed them from TTF or OTF files:
49+
50+
```typescript
51+
import { readFile } from "fs/promises";
52+
53+
const fontBytes = await readFile("fonts/OpenSans-Regular.ttf");
54+
const font = pdf.embedFont(fontBytes);
55+
56+
page.drawText("Custom font text", {
57+
x: 50,
58+
y: 700,
59+
size: 14,
60+
font,
61+
});
62+
```
63+
64+
### Font Subsetting
65+
66+
By default, fonts are subsetted when saving — only the glyphs used in the document are included. This reduces file size significantly.
67+
68+
```typescript
69+
// Subsetting enabled by default
70+
await pdf.save();
71+
72+
// Disable subsetting (larger file, but font fully preserved)
73+
await pdf.save({ subsetFonts: false });
74+
```
75+
76+
---
77+
78+
## Text Measurement
79+
80+
To calculate text dimensions before drawing, use measurement methods on font objects.
81+
82+
### Measuring with Standard Fonts
83+
84+
Use `Standard14Font.of()` to create a font instance for measurement:
85+
86+
```typescript
87+
import { Standard14Font, StandardFonts } from "@libpdf/core";
88+
89+
const font = Standard14Font.of(StandardFonts.HelveticaBold);
90+
91+
const width = font.widthOfTextAtSize("Hello, World!", 12);
92+
const height = font.heightAtSize(12);
93+
```
94+
95+
Available measurement methods:
96+
97+
| Method | Description |
98+
| ---------------------------- | ---------------------------------------- |
99+
| `widthOfTextAtSize(text, size)` | Width of text in points |
100+
| `heightAtSize(size)` | Full height (ascent to descent) in points |
101+
| `sizeAtWidth(text, width)` | Font size to fit text within a width |
102+
| `sizeAtHeight(height)` | Font size to achieve a height |
103+
104+
### Measuring with Embedded Fonts
105+
106+
`EmbeddedFont` provides the same measurement API:
107+
108+
```typescript
109+
const fontBytes = await readFile("fonts/OpenSans-Regular.ttf");
110+
const font = pdf.embedFont(fontBytes);
111+
112+
const width = font.widthOfTextAtSize("Hello, World!", 12);
113+
const height = font.heightAtSize(12);
114+
```
115+
116+
### Standalone measureText Helper
117+
118+
For quick measurements without creating a font object:
119+
120+
```typescript
121+
import { measureText, StandardFonts } from "@libpdf/core";
122+
123+
// With a standard font name
124+
const width = measureText("Hello", StandardFonts.HelveticaBold, 12);
125+
126+
// With an embedded font
127+
const width = measureText("Hello", embeddedFont, 12);
128+
```
129+
130+
---
131+
132+
## Text Alignment
133+
134+
For multiline text with `maxWidth`, use the `alignment` option:
135+
136+
```typescript
137+
page.drawText("This paragraph will be centered within the max width.", {
138+
x: 50,
139+
y: 700,
140+
size: 12,
141+
maxWidth: 200,
142+
alignment: "center", // "left" | "center" | "right" | "justify"
143+
});
144+
```
145+
146+
For single-line text or custom positioning, calculate the position manually:
147+
148+
```typescript
149+
import { Standard14Font, StandardFonts } from "@libpdf/core";
150+
151+
const text = "Centered Title";
152+
const fontSize = 24;
153+
const font = Standard14Font.of(StandardFonts.HelveticaBold);
154+
155+
const textWidth = font.widthOfTextAtSize(text, fontSize);
156+
const x = (page.width - textWidth) / 2;
157+
158+
page.drawText(text, {
159+
x,
160+
y: 700,
161+
size: fontSize,
162+
font: StandardFonts.HelveticaBold,
163+
});
164+
```
165+
166+
---
167+
168+
## Common Patterns
169+
170+
### Fitting Text to a Width
171+
172+
```typescript
173+
const text = "This text must fit";
174+
const maxWidth = 200;
175+
const font = Standard14Font.of(StandardFonts.Helvetica);
176+
177+
const fontSize = font.sizeAtWidth(text, maxWidth);
178+
179+
page.drawText(text, {
180+
x: 50,
181+
y: 700,
182+
size: fontSize,
183+
});
184+
```
185+
186+
### Text with Background
187+
188+
```typescript
189+
import { Standard14Font, StandardFonts, rgb } from "@libpdf/core";
190+
191+
const text = "Highlighted";
192+
const fontSize = 14;
193+
const padding = 4;
194+
const font = Standard14Font.of(StandardFonts.HelveticaBold);
195+
196+
const textWidth = font.widthOfTextAtSize(text, fontSize);
197+
const textHeight = font.heightAtSize(fontSize);
198+
199+
// Draw background
200+
page.drawRectangle({
201+
x: 50 - padding,
202+
y: 700 - padding,
203+
width: textWidth + padding * 2,
204+
height: textHeight + padding * 2,
205+
color: rgb(1, 1, 0), // Yellow
206+
});
207+
208+
// Draw text on top
209+
page.drawText(text, {
210+
x: 50,
211+
y: 700,
212+
size: fontSize,
213+
font: StandardFonts.HelveticaBold,
214+
});
215+
```
216+
217+
---
218+
219+
## See Also
220+
221+
- [Drawing](/docs/guides/drawing) - Draw text, shapes, and images
222+
- [Forms](/docs/guides/forms) - Set fonts on form fields

content/docs/guides/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"pages": ["pages", "text-extraction", "forms", "drawing", "encryption", "signatures"]
2+
"pages": ["pages", "text-extraction", "forms", "drawing", "fonts", "encryption", "signatures"]
33
}

0 commit comments

Comments
 (0)