Skip to content

Commit 694508b

Browse files
committed
set to helvetica for now
1 parent a798279 commit 694508b

File tree

4 files changed

+253
-27
lines changed

4 files changed

+253
-27
lines changed

src/pdf/src/graphics/textObject.ts

Lines changed: 127 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,106 @@ import type {
88
} from "../resources/fonts/fontRegistry.ts";
99
import type { Color } from "./graphics.d.ts";
1010
import { parseColor } from "./utils.ts";
11-
11+
const widths = [
12+
278,
13+
355,
14+
556,
15+
556,
16+
889,
17+
667,
18+
222,
19+
333,
20+
333,
21+
389,
22+
584,
23+
278,
24+
333,
25+
278,
26+
278,
27+
556,
28+
556,
29+
556,
30+
556,
31+
556,
32+
556,
33+
556,
34+
556,
35+
556,
36+
556,
37+
278,
38+
278,
39+
584,
40+
584,
41+
584,
42+
556,
43+
1015,
44+
667,
45+
667,
46+
722,
47+
722,
48+
667,
49+
611,
50+
778,
51+
722,
52+
278,
53+
500,
54+
667,
55+
556,
56+
833,
57+
722,
58+
778,
59+
667,
60+
778,
61+
722,
62+
667,
63+
611,
64+
722,
65+
667,
66+
944,
67+
667,
68+
667,
69+
611,
70+
278,
71+
278,
72+
278,
73+
469,
74+
556,
75+
222,
76+
556,
77+
556,
78+
500,
79+
556,
80+
556,
81+
278,
82+
556,
83+
556,
84+
222,
85+
222,
86+
500,
87+
222,
88+
833,
89+
556,
90+
556,
91+
556,
92+
556,
93+
333,
94+
500,
95+
278,
96+
556,
97+
500,
98+
722,
99+
500,
100+
500,
101+
500,
102+
334,
103+
260,
104+
334,
105+
584,
106+
];
12107
export class TextObject extends ObjectBase {
13108
#x = 0;
14109
#y = 0;
15-
#fontFamily?: string;
110+
#fontFamily: string = "Helvetica";
16111
#fontWeight: FontWeight = "normal";
17112
#alignX: TextAlign = "left";
18113
#alignY: VerticalAlign = "bottom";
@@ -73,22 +168,35 @@ export class TextObject extends ObjectBase {
73168
return this;
74169
}
75170
generate(): string {
76-
const fontName = this.#fontFamily
77-
? this.page.getFontName(this.#fontFamily, {
78-
fontWeight: this.#fontWeight,
79-
italic: this.#italic,
80-
})
81-
: undefined;
82-
if (!fontName) {
83-
throw new Error(
84-
`font ${this.#fontFamily} ${this.#fontWeight} ${this.#italic} not found`,
85-
);
86-
}
87-
const font = this.page.resources.fontRegistry.fonts.get(fontName);
88-
if (!font) {
89-
throw new Error("Font not found in registry");
90-
}
91-
const lineHeight = font.maxHeight / 1000 * this.#fontSize / 2;
171+
// const fontName = this.#fontFamily
172+
// ? this.page.getFontName(this.#fontFamily, {
173+
// fontWeight: this.#fontWeight,
174+
// italic: this.#italic,
175+
// })
176+
// : undefined;
177+
// if (!fontName) {
178+
// throw new Error(
179+
// `font ${this.#fontFamily} ${this.#fontWeight} ${this.#italic} not found`,
180+
// );
181+
// }
182+
// const font = this.page.resources.fontRegistry.fonts.get(fontName);
183+
// if (!font) {
184+
// throw new Error("Font not found in registry");
185+
// }
186+
187+
const fontName = "F1";
188+
// const lineHeight = font.maxHeight / 1000 * this.#fontSize / 2;
189+
const lineHeight = 1097 / 1000 * this.#fontSize / 2;
190+
const getStringWidth = (textLine: string) => {
191+
let width = 0;
192+
if (textLine.length === 0) return 0;
193+
for (const char of textLine) {
194+
const charCode = char.charCodeAt(0);
195+
const charWidth = widths[charCode - 33];
196+
width += charWidth;
197+
}
198+
return width / 1000 * this.#fontSize;
199+
};
92200
const leading = lineHeight * 1.5 - lineHeight;
93201
let text = "";
94202
const textLines = this.#text.split("\n");
@@ -108,7 +216,7 @@ export class TextObject extends ObjectBase {
108216
break;
109217
}
110218
textLines.forEach((textLine, _index) => {
111-
const textWidth = font.getStringWidth(textLine, this.#fontSize);
219+
const textWidth = getStringWidth(textLine);
112220
switch (this.#alignX) {
113221
case "center":
114222
x = x - (textWidth / 2) + lastOffset;

src/pdf/src/resources/fonts/font.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class Font {
9090
descObj.set("FontName", `/${this.fontName}`);
9191
descObj.set("FontFamily", `/${fontDesc.fontFamily}`);
9292
descObj.set("Flags", fontDesc.flags);
93-
descObj.setArray("FontBBox", fontDesc.fontBBox);
93+
// descObj.setArray("FontBBox", fontDesc.fontBBox);
9494
descObj.set("ItalicAngle", fontDesc.italicAngle);
9595
descObj.set("Ascent", fontDesc.ascent);
9696
descObj.set("Descent", fontDesc.descent);
@@ -113,21 +113,20 @@ export class Font {
113113
#setupFileObject() {
114114
const fontData = this.data;
115115
const fontFile = this.fileObject;
116-
fontFile.set("Length1", fontData.length);
117-
/*
116+
fontFile.set("Length1", fontData.length + 1);
118117

119118
const bytesToHex = (bytes: Uint8Array) => {
120119
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join(
121120
"",
122121
);
123122
};
124123
const hexData = bytesToHex(fontData);
125-
const hexBytes = new TextEncoder().encode(hexData + ">");
124+
const hexBytes = new TextEncoder().encode(hexData);
126125
fontFile.setArray("Filter", ["/ASCIIHexDecode"]);
127-
fontFile.set("Length", hexData.length + 1); // +1 for the '>' at the end of ASCIIHexDecode
128-
*/
129-
const hexBytes = fontData;
130-
fontFile.set("Length", hexBytes.length);
126+
fontFile.set("Length", hexData.length); // +1 for the '>' at the end of ASCIIHexDecode
127+
128+
// const hexBytes = fontData;
129+
// fontFile.set("Length", hexBytes.length);
131130
this.fileObject.setCustomByteGenerator(() => {
132131
const encoder = new TextEncoder();
133132
const content = fontFile.generate();

src/pdf/src/resources/fonts/fontLoader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export async function loadFont(filePath: string) {
3737
lastChar: 127,
3838
widths: [] as number[],
3939
encoding: "/WinAnsiEncoding",
40+
// encoding: "/MacRomanEncoding",
4041
};
4142
const fontDesc = {
4243
fontName: fontDict.baseFont,

src/pdf/src/resources/resourcManager.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,126 @@ export class ResourceManager {
1919
"Font",
2020
this.fontRegistry.fontDict,
2121
);
22+
this.#addDefaultFont();
2223
}
24+
#addDefaultFont() {
25+
const helvetica = this.#addObject();
26+
helvetica.set("Type", "/Font");
27+
helvetica.set("Subtype", "/Type1");
28+
helvetica.set("BaseFont", "/Helvetica");
29+
helvetica.set("FirstChar", 33);
30+
helvetica.set("LastChar", 126);
31+
helvetica.setArray("Widths", [
32+
278,
33+
355,
34+
556,
35+
556,
36+
889,
37+
667,
38+
222,
39+
333,
40+
333,
41+
389,
42+
584,
43+
278,
44+
333,
45+
278,
46+
278,
47+
556,
48+
556,
49+
556,
50+
556,
51+
556,
52+
556,
53+
556,
54+
556,
55+
556,
56+
556,
57+
278,
58+
278,
59+
584,
60+
584,
61+
584,
62+
556,
63+
1015,
64+
667,
65+
667,
66+
722,
67+
722,
68+
667,
69+
611,
70+
778,
71+
722,
72+
278,
73+
500,
74+
667,
75+
556,
76+
833,
77+
722,
78+
778,
79+
667,
80+
778,
81+
722,
82+
667,
83+
611,
84+
722,
85+
667,
86+
944,
87+
667,
88+
667,
89+
611,
90+
278,
91+
278,
92+
278,
93+
469,
94+
556,
95+
222,
96+
556,
97+
556,
98+
500,
99+
556,
100+
556,
101+
278,
102+
556,
103+
556,
104+
222,
105+
222,
106+
500,
107+
222,
108+
833,
109+
556,
110+
556,
111+
556,
112+
556,
113+
333,
114+
500,
115+
278,
116+
556,
117+
500,
118+
722,
119+
500,
120+
500,
121+
500,
122+
334,
123+
260,
124+
334,
125+
584,
126+
]);
127+
const desc = this.#addObject();
128+
desc.set("Type", "/FontDescriptor");
129+
desc.set("FontName", "/Helvetica");
130+
desc.set("Flags", 32);
131+
desc.setArray("FontBBox", [-166, -225, 1000, 931]);
132+
desc.set("ItalicAngle", 0);
133+
desc.set("Ascent", 718);
134+
desc.set("Descent", -207);
135+
desc.set("CapHeight", 718);
136+
desc.set("StemV", 88);
137+
desc.set("MissingWidth", 0);
138+
helvetica.addReference("FontDescriptor", desc.objNumber);
23139

140+
this.fontRegistry.fontDict.addReference("F1", helvetica.objNumber);
141+
}
24142
#addObject(objectName?: string) {
25143
return this.#pdf.objects.addObject(objectName);
26144
}

0 commit comments

Comments
 (0)