-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaaf.html
More file actions
561 lines (501 loc) · 19.6 KB
/
aaf.html
File metadata and controls
561 lines (501 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<html>
<head>
<title>AAF File Format</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
.viewer-panel {
background: #11170f;
border: 1px solid #535b5aba;
margin: 15px 0;
padding: 12px;
}
.viewer-controls {
align-items: end;
display: grid;
gap: 10px;
grid-template-columns: 2fr 1fr 1fr 1fr;
margin-bottom: 10px;
}
.viewer-controls label {
display: block;
}
.viewer-controls input {
background: #222;
border: 1px solid #555;
color: #cdcdcd;
box-sizing: border-box;
margin-top: 4px;
padding: 4px;
width: 100%;
}
.viewer-status {
color: #f49205;
min-height: 18px;
}
.font-summary {
margin: 8px 0;
}
.font-summary td:first-child {
color: #f49205;
width: 180px;
}
.canvas-wrap {
background: #040c00;
border: 1px solid #333;
margin-top: 10px;
max-width: 100%;
overflow: auto;
padding: 8px;
}
.canvas-wrap canvas {
display: block;
image-rendering: pixelated;
}
@media (max-width: 900px) {
.viewer-controls {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div style="width: 1200px; margin: auto;">
<h1>AAF File Format</h1>
<p>The AAF file format stores bitmap fonts used by Fallout and Fallout 2. This page describes the English-version files; localized releases may contain variations that are not covered here.</p>
<p>An AAF file stores one glyph descriptor for each of the 256 byte values. Glyphs are usually proportional: each glyph has its own width and height. The glyph bitmap is stored as one byte per pixel, with values in the range <code>0</code> through <code>9</code>.</p>
<ul>
<li><code>0</code> means the pixel is transparent.</li>
<li><code>1</code> through <code>9</code> represent relative brightness, with <code>9</code> being the brightest. The brightness scale does not appear to be linear.</li>
</ul>
<p>The following bitmap is a glyph from <code>FONT1.AAF</code> representing the character <code>b</code> (ASCII 98):</p>
<pre style="background: #222; margin: 10px; width: 80px; padding: 5px;">
77
77
777763
77 76
77 77
77 76
777763
</pre>
<p>Pixels with value <code>0</code> are omitted because they are transparent. The values <code>7</code> and <code>6</code> mark brighter parts of the glyph, while <code>3</code> is dimmer and appears around the rounded corners.</p>
<p>All integer fields are stored in Motorola / big-endian byte order. The file starts with a 12-byte header followed by 256 glyph descriptors. Each descriptor is 8 bytes: width, height, and an offset relative to the start of the glyph data block. The glyph data block starts at <code>0x000C + 256 * 8 = 0x080C</code>.</p>
<h3>AAF viewer</h3>
<p>The viewer below reads an uploaded <code>.aaf</code> file, validates the header and glyph descriptors, and renders both a text preview and a 256-slot glyph grid. Pixel values are shown as brightness levels of the selected foreground color.</p>
<div class="viewer-panel">
<div class="viewer-controls">
<label>AAF file
<input id="aaf-file" type="file" accept=".aaf" />
</label>
<label>Zoom
<input id="aaf-zoom" type="number" min="1" max="12" value="4" />
</label>
<label>Foreground
<input id="aaf-fg" type="color" value="#cdcdcd" />
</label>
<label>Background
<input id="aaf-bg" type="color" value="#040c00" />
</label>
</div>
<label>Preview text
<input id="aaf-preview-text" type="text" value="Fallout 2 interface text" />
</label>
<div id="aaf-status" class="viewer-status"></div>
<table class="font-summary">
<tbody>
<tr><td>Maximum glyph height</td><td id="aaf-max-height">-</td></tr>
<tr><td>Horizontal gap</td><td id="aaf-horizontal-gap">-</td></tr>
<tr><td>Space width</td><td id="aaf-space-width">-</td></tr>
<tr><td>Vertical gap</td><td id="aaf-vertical-gap">-</td></tr>
<tr><td>Glyph data size</td><td id="aaf-data-size">-</td></tr>
</tbody>
</table>
<h4>Text preview</h4>
<div class="canvas-wrap"><canvas id="aaf-preview" width="1" height="1"></canvas></div>
<h4>Glyph grid</h4>
<div class="canvas-wrap"><canvas id="aaf-grid" width="1" height="1"></canvas></div>
</div>
<table style="background: #222; margin: 10px;">
<thead>
<tr>
<th style="width: 150px;">Offset</th>
<th style="width: 125px;">Bytes</th>
<th style="width: 150px;">Data Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>0x000</td>
<td>4</td>
<td>char[4] = "AAFF"</td>
<td>Signature used to identify the file type.</td>
</tr>
<tr>
<td>0x004</td>
<td>2</td>
<td>unsigned short</td>
<td>Maximum glyph height, including ascenders and descenders.</td>
</tr>
<tr>
<td>0x006</td>
<td>2</td>
<td>unsigned short</td>
<td>Horizontal gap, in pixels, added between adjacent glyphs.</td>
</tr>
<tr>
<td>0x008</td>
<td>2</td>
<td>unsigned short</td>
<td>Width, in pixels, used for the space character.</td>
</tr>
<tr>
<td>0x00A</td>
<td>2</td>
<td>unsigned short</td>
<td>Vertical gap, in pixels, added between two lines of glyphs.</td>
</tr>
<tr>
<td>0x00C</td>
<td>2</td>
<td>unsigned short</td>
<td>Width, in pixels, of glyph 0.</td>
</tr>
<tr>
<td>0x00E</td>
<td>2</td>
<td>unsigned short</td>
<td>Height, in pixels, of glyph 0.</td>
</tr>
<tr>
<td>0x010</td>
<td>4</td>
<td>unsigned long</td>
<td>Offset of glyph 0, relative to the glyph data block at <code>0x080C</code>. If <code>width * height = 0</code>, this can be the offset of the next non-empty glyph.</td>
</tr>
<tr>
<td>0x014</td>
<td>2</td>
<td>unsigned short</td>
<td>Width, in pixels, of glyph 1.</td>
</tr>
<tr>
<td>0x016</td>
<td>2</td>
<td>unsigned short</td>
<td>Height, in pixels, of glyph 1.</td>
</tr>
<tr>
<td>0x018</td>
<td>4</td>
<td>unsigned long</td>
<td>Offset of glyph 1, relative to the glyph data block at <code>0x080C</code>.</td>
</tr>
<tr>
<td>0x01C</td>
<td>(2 + 2 + 4) * (256 - 2) </td>
<td></td>
<td>Descriptions of glyphs 2 through 255, as described above.</td>
</tr>
<tr>
<td>0x80C</td>
<td>GLYPH-0-WIDTH * GLYPH-0-HEIGHT</td>
<td>byte = [0..9]</td>
<td>Pixel data for glyph 0. Pixels are stored left to right, then top to bottom. Each pixel is one byte in the range <code>0</code> through <code>9</code>.</td>
</tr>
<tr>
<td>0x080C + (GLYPH-0-WIDTH * GLYPH-0-HEIGHT)</td>
<td>GLYPH-1-WIDTH * GLYPH-1-HEIGHT</td>
<td>byte = [0..9]</td>
<td>Pixel data for glyph 1.</td>
</tr>
<tr>
<td>0x080C + (GLYPH-0-WIDTH * GLYPH-0-HEIGHT) + (GLYPH-1-WIDTH * GLYPH-1-HEIGHT)</td>
<td>...</td>
<td>byte = [0..9]</td>
<td>Pixel data for glyphs 2 through 255, as described above.</td>
</tr>
</tbody>
</table>
<h3>Parsing notes</h3>
<p>The <code>AAFF</code> signature identifies the file type. After the 12-byte header, the descriptor table always contains 256 entries and therefore always ends at <code>0x080C</code>. Descriptor offsets are relative to this glyph data block, not to the beginning of the file.</p>
<p>Each glyph consumes <code>width * height</code> bytes. A NULL glyph has no pixel data of its own, and its offset can be the same as the next glyph with data, so readers should use width and height to decide whether to read pixels.</p>
<p>AAF stores glyph brightness, not final palette colors. At render time, the engine blends each non-zero pixel value with the selected text color.</p>
<h3>Runtime notes</h3>
<p>In Fallout 2 CE, the AAF loader attempts to load interface fonts named <code>font0.aaf</code>, <code>font1.aaf</code>, and so on. The interface font manager exposes AAF fonts as font ids <code>100</code> through <code>110</code>, separate from the FON text font manager.</p>
<p>When rendering a string, a space character uses the header's space width field instead of the width in glyph descriptor 32. Each character advances by its character width plus the horizontal gap. The line height reported by the engine is the maximum glyph height plus the vertical gap.</p>
<p>Glyph pixels are bottom-aligned within the maximum glyph height. If a glyph is shorter than the maximum height, rendering skips the difference before drawing that glyph's rows.</p>
<h3>AAF and FON comparison</h3>
<table>
<thead>
<tr>
<th style="padding-right: 10px; width: 140px;">Feature</th>
<th>AAF</th>
<th>FON</th>
</tr>
</thead>
<tbody>
<tr>
<td>Typical role</td>
<td>Interface font manager</td>
<td>Low-numbered text font manager</td>
</tr>
<tr>
<td>Typical files</td>
<td><code>font0.aaf</code> and other interface font files</td>
<td><code>font0.fon</code> through <code>font9.fon</code></td>
</tr>
<tr>
<td>Font id range</td>
<td><code>100</code> through <code>110</code> in Fallout 2 CE</td>
<td><code>0</code> through <code>9</code> in Fallout 2 CE</td>
</tr>
<tr>
<td>Glyph slots</td>
<td>Always 256 descriptor slots</td>
<td>Glyph count is stored in the file header</td>
</tr>
<tr>
<td>Pixel data</td>
<td>One byte per pixel; values <code>1</code> through <code>9</code> represent brightness levels</td>
<td>1 bit per pixel; set bits draw the chosen text color</td>
</tr>
<tr>
<td>Integer byte order</td>
<td>Big-endian 16-bit and 32-bit integers</td>
<td>Little-endian 32-bit integers</td>
</tr>
</tbody>
</table>
<h3>Source code</h3>
<a href="https://github.com/alexbatalov/fallout2-ce/blob/main/src/font_manager.cc">Fallout 2 Community Edition AAF interface font manager - C++</a><br/>
<a href="https://github.com/alexbatalov/fallout2-ce/blob/main/src/text_font.cc">Fallout 2 Community Edition FON text font manager - C++</a>
<h2>History</h2>
<p>
2019-12-16 - Ported from <a href="https://falloutmods.fandom.com/wiki/AAF_File_Format">https://falloutmods.fandom.com/wiki/AAF_File_Format</a> by <a href="https://github.com/ghost2238">ghost</a><br/>
Patched by Anchorite (anchorite2001@yandex.ru) <br/>
Created by Noid (noid@888.nu)
</p>
</div>
<script>
(function () {
"use strict";
var HEADER_SIZE = 12;
var GLYPH_COUNT = 256;
var DESCRIPTOR_SIZE = 8;
var DATA_START = HEADER_SIZE + GLYPH_COUNT * DESCRIPTOR_SIZE;
var currentFont = null;
var fileInput = document.getElementById("aaf-file");
var zoomInput = document.getElementById("aaf-zoom");
var fgInput = document.getElementById("aaf-fg");
var bgInput = document.getElementById("aaf-bg");
var previewTextInput = document.getElementById("aaf-preview-text");
var statusEl = document.getElementById("aaf-status");
var previewCanvas = document.getElementById("aaf-preview");
var gridCanvas = document.getElementById("aaf-grid");
function setStatus(message) {
statusEl.textContent = message || "";
}
function parseColor(hex) {
var value = parseInt(hex.slice(1), 16);
return {
r: (value >> 16) & 0xFF,
g: (value >> 8) & 0xFF,
b: value & 0xFF
};
}
function brightnessColor(hex, level) {
var color = parseColor(hex);
var scale = Math.max(0, Math.min(9, level)) / 9;
return "rgb(" +
Math.round(color.r * scale) + "," +
Math.round(color.g * scale) + "," +
Math.round(color.b * scale) + ")";
}
function parseAaf(buffer) {
if (buffer.byteLength < DATA_START) {
throw new Error("File is too small for an AAF header and descriptor table.");
}
var view = new DataView(buffer);
if (view.getUint8(0) !== 0x41 || view.getUint8(1) !== 0x41 || view.getUint8(2) !== 0x46 || view.getUint8(3) !== 0x46) {
throw new Error("Missing AAFF signature.");
}
var maxHeight = view.getUint16(4, false);
var horizontalGap = view.getUint16(6, false);
var spaceWidth = view.getUint16(8, false);
var verticalGap = view.getUint16(10, false);
if (maxHeight === 0 || maxHeight > 512) {
throw new Error("Unreasonable maximum glyph height: " + maxHeight + ".");
}
var glyphs = [];
var dataSize = buffer.byteLength - DATA_START;
var maxPixelValue = 0;
for (var index = 0; index < GLYPH_COUNT; index++) {
var descriptorOffset = HEADER_SIZE + index * DESCRIPTOR_SIZE;
var width = view.getUint16(descriptorOffset, false);
var height = view.getUint16(descriptorOffset + 2, false);
var dataOffset = view.getUint32(descriptorOffset + 4, false);
var glyphSize = width * height;
if (width > 512 || height > 512) {
throw new Error("Unreasonable size for glyph " + index + ": " + width + "x" + height + ".");
}
if (height > maxHeight) {
throw new Error("Glyph " + index + " is taller than the maximum glyph height.");
}
if (glyphSize > 0 && dataOffset + glyphSize > dataSize) {
throw new Error("Pixel data for glyph " + index + " extends past the end of the file.");
}
for (var byteIndex = 0; byteIndex < glyphSize; byteIndex++) {
var pixel = view.getUint8(DATA_START + dataOffset + byteIndex);
if (pixel > maxPixelValue) {
maxPixelValue = pixel;
}
}
glyphs.push({ width: width, height: height, dataOffset: dataOffset });
}
if (maxPixelValue > 9) {
throw new Error("Pixel value " + maxPixelValue + " is outside the expected 0..9 range.");
}
return {
bytes: new Uint8Array(buffer),
maxHeight: maxHeight,
horizontalGap: horizontalGap,
spaceWidth: spaceWidth,
verticalGap: verticalGap,
dataSize: dataSize,
glyphs: glyphs
};
}
function characterWidth(font, ch) {
return ch === 32 ? font.spaceWidth : font.glyphs[ch].width;
}
function drawGlyph(ctx, font, glyphIndex, x, y, zoom, color) {
var glyph = font.glyphs[glyphIndex];
if (!glyph || glyph.width === 0 || glyph.height === 0) {
return;
}
var glyphY = y + (font.maxHeight - glyph.height) * zoom;
for (var gy = 0; gy < glyph.height; gy++) {
for (var gx = 0; gx < glyph.width; gx++) {
var pixel = font.bytes[DATA_START + glyph.dataOffset + gy * glyph.width + gx];
if (pixel !== 0) {
ctx.fillStyle = brightnessColor(color, pixel);
ctx.fillRect(x + gx * zoom, glyphY + gy * zoom, zoom, zoom);
}
}
}
}
function updateSummary(font) {
document.getElementById("aaf-max-height").textContent = font ? font.maxHeight + " px" : "-";
document.getElementById("aaf-horizontal-gap").textContent = font ? font.horizontalGap + " px" : "-";
document.getElementById("aaf-space-width").textContent = font ? font.spaceWidth + " px" : "-";
document.getElementById("aaf-vertical-gap").textContent = font ? font.verticalGap + " px" : "-";
document.getElementById("aaf-data-size").textContent = font ? font.dataSize + " bytes" : "-";
}
function drawPreview(font) {
var zoom = getZoom();
var text = previewTextInput.value || "";
var width = 1;
var height = Math.max(1, (font.maxHeight + font.verticalGap) * zoom);
for (var i = 0; i < text.length; i++) {
width += (characterWidth(font, text.charCodeAt(i) & 0xFF) + font.horizontalGap) * zoom;
}
previewCanvas.width = Math.max(1, width);
previewCanvas.height = height;
var ctx = previewCanvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.fillStyle = bgInput.value;
ctx.fillRect(0, 0, previewCanvas.width, previewCanvas.height);
var x = 0;
for (var index = 0; index < text.length; index++) {
var glyphIndex = text.charCodeAt(index) & 0xFF;
drawGlyph(ctx, font, glyphIndex, x, 0, zoom, fgInput.value);
x += (characterWidth(font, glyphIndex) + font.horizontalGap) * zoom;
}
}
function drawGrid(font) {
var zoom = getZoom();
var maxWidth = font.spaceWidth;
for (var i = 0; i < GLYPH_COUNT; i++) {
maxWidth = Math.max(maxWidth, font.glyphs[i].width);
}
var columns = 16;
var labelHeight = 14;
var cellPadding = 4;
var cellWidth = Math.max(28, maxWidth * zoom + cellPadding * 2);
var cellHeight = font.maxHeight * zoom + labelHeight + cellPadding * 2;
var rows = Math.ceil(GLYPH_COUNT / columns);
gridCanvas.width = columns * cellWidth;
gridCanvas.height = rows * cellHeight;
var ctx = gridCanvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
ctx.fillStyle = bgInput.value;
ctx.fillRect(0, 0, gridCanvas.width, gridCanvas.height);
ctx.font = "10px Verdana, Arial, sans-serif";
ctx.textBaseline = "top";
for (var index = 0; index < GLYPH_COUNT; index++) {
var col = index % columns;
var row = Math.floor(index / columns);
var cellX = col * cellWidth;
var cellY = row * cellHeight;
var glyphX = cellX + cellPadding;
var glyphY = cellY + labelHeight + cellPadding;
ctx.fillStyle = "#333";
ctx.fillRect(cellX, cellY, cellWidth - 1, cellHeight - 1);
ctx.fillStyle = bgInput.value;
ctx.fillRect(cellX + 1, cellY + 1, cellWidth - 3, cellHeight - 3);
ctx.fillStyle = "#f49205";
ctx.fillText(index.toString(), cellX + cellPadding, cellY + 2);
drawGlyph(ctx, font, index, glyphX, glyphY, zoom, fgInput.value);
}
}
function getZoom() {
var zoom = parseInt(zoomInput.value, 10);
if (!Number.isFinite(zoom)) {
zoom = 4;
}
return Math.max(1, Math.min(12, zoom));
}
function redraw() {
if (!currentFont) {
return;
}
zoomInput.value = getZoom();
drawPreview(currentFont);
drawGrid(currentFont);
}
fileInput.addEventListener("change", function () {
var file = fileInput.files && fileInput.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function () {
try {
currentFont = parseAaf(reader.result);
updateSummary(currentFont);
setStatus("Loaded " + file.name + ".");
redraw();
} catch (err) {
currentFont = null;
updateSummary(null);
previewCanvas.width = 1;
previewCanvas.height = 1;
gridCanvas.width = 1;
gridCanvas.height = 1;
setStatus(err.message);
}
};
reader.readAsArrayBuffer(file);
});
zoomInput.addEventListener("input", redraw);
fgInput.addEventListener("input", redraw);
bgInput.addEventListener("input", redraw);
previewTextInput.addEventListener("input", function () {
if (currentFont) {
drawPreview(currentFont);
}
});
})();
</script>
</body>
</html>