rebuild iggy's native stb_truetype wrapper
This commit is contained in:
@@ -360,3 +360,19 @@ foreach(platform IN ITEMS durango orbis ps3 vita)
|
||||
target_compile_options(${target} PRIVATE /Oi-)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
foreach(platform IN ITEMS durango orbis ps3 vita)
|
||||
string(TOUPPER ${platform} platform_upper)
|
||||
set(target iggy_reconstructed_stb_truetype_wrapper_${platform})
|
||||
add_library(${target} STATIC stb_truetype_wrapper.c)
|
||||
target_compile_definitions(
|
||||
${target}
|
||||
PRIVATE IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_${platform_upper}
|
||||
)
|
||||
configure_iggy_reconstructed_target(${target} c_std_11)
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
|
||||
target_compile_options(${target} PRIVATE -fno-builtin)
|
||||
elseif(MSVC)
|
||||
target_compile_options(${target} PRIVATE /Oi-)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
@@ -238,6 +238,27 @@ paths. The 32/64-bit font-record and glyph-record layouts, per-platform
|
||||
inlining differences, ownership, and the exact 25-import surface match all
|
||||
four shipping builds; the SWF/AVM2 text assets remain the caller.
|
||||
|
||||
`stb_truetype_wrapper.c` restores RAD's vintage build of the stb_truetype
|
||||
library that the font provider and font cache call for glyph outlines and
|
||||
rasterization. Its four variants expose all 32 `iggy_tt_*` entry points (font
|
||||
directory init, cmap glyph lookup, scale and h/v metrics, glyph box and
|
||||
bounding box, glyph-empty test, glyph shape extraction and free, the
|
||||
active-edge rasterizer, glyph and codepoint bitmap makers, baked-font atlas
|
||||
and quad helpers, kerning stubs, UTF-8/UTF-16 name matching, and user-data
|
||||
tagging) over the imported `GSwfMallocTruetype`/`iggy_free_alloc` allocator.
|
||||
It reconstructs the vintage fixed-point active-edge fill (1024-scaled edges,
|
||||
next-pointer-threaded active list, integer coverage accumulation with
|
||||
wraparound and no clamp), the no-start-off simple-glyph vertex machine with
|
||||
int16 coordinate wraparound and integer curve midpoints, the conditional
|
||||
compound-glyph argument parsing, the searchRange cmap format-4 search and the
|
||||
u16-count format-12 lookup, and the iterative curve tessellation — matching
|
||||
RAD's decompile rather than modern stb_truetype. Platform math dispatch keeps
|
||||
durango/vita on double `ceil`/`floor`/`sqrt`, ps3 on `ceilf`/`floorf` plus
|
||||
inline `fsqrts`, and orbis on branchless truncation and `__builtin_sqrtf` so it
|
||||
emits no libm imports; the 32/64-bit font and vertex layouts and the exact
|
||||
per-platform import surface match all four shipping builds. The SWF/AVM2 text
|
||||
pipeline remains the caller.
|
||||
|
||||
Configure this directory directly with CMake to build the reconstructed units.
|
||||
Every added module must compile independently and have behavioral tests before
|
||||
it is listed in `modules.tsv` as complete.
|
||||
|
||||
@@ -49,3 +49,7 @@ iggy_font_provider 43 33 durango complete
|
||||
iggy_font_provider 50 33 orbis complete
|
||||
iggy_font_provider 49 33 ps3 complete
|
||||
iggy_font_provider 50 33 vita complete
|
||||
stb_truetype_wrapper 42 32 durango complete
|
||||
stb_truetype_wrapper 37 32 orbis complete
|
||||
stb_truetype_wrapper 35 32 ps3 complete
|
||||
stb_truetype_wrapper 36 32 vita complete
|
||||
|
||||
|
180
iggy-decompiled/src/reconstructed/stb_truetype_wrapper.c
Normal file
180
iggy-decompiled/src/reconstructed/stb_truetype_wrapper.c
Normal file
@@ -0,0 +1,180 @@
|
||||
/* Reconstructed translation unit for RAD's vintage stb_truetype wrapper. */
|
||||
/* Source-equivalent recovery of c:\projects\g\src\flash\stb_truetype_wrapper.c */
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "stb_truetype_wrapper.h"
|
||||
|
||||
#if (defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO) + \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_ORBIS) + \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_PS3) + \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_VITA)) != 1
|
||||
#error "select exactly one stb_truetype_wrapper target"
|
||||
#endif
|
||||
|
||||
#if defined(__clang__) || defined(__GNUC__)
|
||||
#define IGGY_USED __attribute__((used))
|
||||
#else
|
||||
#define IGGY_USED
|
||||
#endif
|
||||
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_ORBIS)
|
||||
#define IGGY_TT_64BIT 1
|
||||
#else
|
||||
#define IGGY_TT_64BIT 0
|
||||
#endif
|
||||
|
||||
static const char IGGY_USED source_path[] =
|
||||
"c:\\projects\\g\\src\\flash\\stb_truetype_wrapper.c";
|
||||
|
||||
extern int64_t GSwfMallocTruetype(intptr_t, int);
|
||||
extern void iggy_free_alloc(intptr_t, void *);
|
||||
extern void *memcpy(void *, const void *, size_t);
|
||||
extern void *memset(void *, int, size_t);
|
||||
|
||||
#if !defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO)
|
||||
extern size_t strlen(const char *);
|
||||
#endif
|
||||
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_VITA)
|
||||
extern int __divsi3(int, int);
|
||||
#endif
|
||||
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_VITA)
|
||||
extern double floor(double);
|
||||
extern double ceil(double);
|
||||
extern double sqrt(double);
|
||||
#elif defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_PS3)
|
||||
extern float floorf(float);
|
||||
extern float ceilf(float);
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#pragma weak GSwfMallocTruetype
|
||||
#pragma weak iggy_free_alloc
|
||||
#endif
|
||||
|
||||
enum {
|
||||
TT_vmove = 1,
|
||||
TT_vline = 2,
|
||||
TT_vcurve = 3
|
||||
};
|
||||
|
||||
typedef struct TTFontInfo {
|
||||
#if IGGY_TT_64BIT
|
||||
uint64_t userdata_value;
|
||||
uint64_t data_value;
|
||||
#else
|
||||
uint32_t userdata_value;
|
||||
uint32_t data_value;
|
||||
#endif
|
||||
int32_t fontstart;
|
||||
uint32_t numGlyphs;
|
||||
int32_t loca;
|
||||
int32_t head;
|
||||
int32_t glyf;
|
||||
int32_t hhea;
|
||||
int32_t hmtx;
|
||||
int32_t os2;
|
||||
int32_t index_map;
|
||||
int32_t indexToLocFormat;
|
||||
} TTFontInfo;
|
||||
|
||||
#define TT_USERDATA(info) ((void *)(uintptr_t)((info)->userdata_value))
|
||||
#define TT_DATA(info) ((const uint8_t *)(uintptr_t)((info)->data_value))
|
||||
|
||||
typedef struct TTPoint {
|
||||
float x;
|
||||
float y;
|
||||
} TTPoint;
|
||||
|
||||
_Static_assert(sizeof(IggyTTVertex) == 0x18, "vertex layout");
|
||||
_Static_assert(sizeof(IggyTTBakedChar) == 0x14, "baked-char layout");
|
||||
_Static_assert(sizeof(IggyTTAlignedQuad) == 0x20, "quad layout");
|
||||
_Static_assert(offsetof(TTFontInfo, data_value) ==
|
||||
(IGGY_TT_64BIT ? 0x08 : 0x04),
|
||||
"font data offset");
|
||||
_Static_assert(offsetof(TTFontInfo, numGlyphs) ==
|
||||
(IGGY_TT_64BIT ? 0x14 : 0x0c),
|
||||
"font glyph-count offset");
|
||||
_Static_assert(offsetof(TTFontInfo, indexToLocFormat) ==
|
||||
(IGGY_TT_64BIT ? 0x34 : 0x2c),
|
||||
"font loca-format offset");
|
||||
|
||||
static uint16_t tt_u16(const uint8_t *p) {
|
||||
return (uint16_t)(((uint16_t)p[0] << 8) | p[1]);
|
||||
}
|
||||
|
||||
static int16_t tt_s16(const uint8_t *p) {
|
||||
return (int16_t)tt_u16(p);
|
||||
}
|
||||
|
||||
static uint32_t tt_u32(const uint8_t *p) {
|
||||
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
|
||||
((uint32_t)p[2] << 8) | p[3];
|
||||
}
|
||||
|
||||
static int32_t tt_round_down(float value) {
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_VITA)
|
||||
return (int32_t)floor((double)value);
|
||||
#elif defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_PS3)
|
||||
return (int32_t)floorf(value);
|
||||
#else
|
||||
const int32_t truncated = (int32_t)value;
|
||||
return truncated - (value < (float)truncated);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t tt_round_up(float value) {
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_VITA)
|
||||
return (int32_t)ceil((double)value);
|
||||
#elif defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_PS3)
|
||||
return (int32_t)ceilf(value);
|
||||
#else
|
||||
const int32_t truncated = (int32_t)value;
|
||||
return truncated + (value > (float)truncated);
|
||||
#endif
|
||||
}
|
||||
|
||||
static float tt_sqrt(float value) {
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO) || \
|
||||
defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_VITA)
|
||||
return (float)sqrt((double)value);
|
||||
#elif defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_PS3)
|
||||
#if defined(__powerpc__) || defined(__ppc__) || defined(__PPC__)
|
||||
float result;
|
||||
__asm__("fsqrts %0, %1" : "=f"(result) : "f"(value));
|
||||
return result;
|
||||
#else
|
||||
return __builtin_sqrtf(value);
|
||||
#endif
|
||||
#else
|
||||
return __builtin_sqrtf(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t tt_div(int32_t numerator, int32_t denominator) {
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_VITA)
|
||||
return __divsi3(numerator, denominator);
|
||||
#else
|
||||
return numerator / denominator;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void *tt_alloc(void *userdata, int32_t size) {
|
||||
return (void *)(intptr_t)GSwfMallocTruetype((intptr_t)userdata, size);
|
||||
}
|
||||
|
||||
static void tt_free(void *userdata, void *block) {
|
||||
iggy_free_alloc((intptr_t)userdata, block);
|
||||
}
|
||||
|
||||
#include "stb_truetype_wrapper_font.inc"
|
||||
#include "stb_truetype_wrapper_shape.inc"
|
||||
#include "stb_truetype_wrapper_raster.inc"
|
||||
#include "stb_truetype_wrapper_bitmap.inc"
|
||||
103
iggy-decompiled/src/reconstructed/stb_truetype_wrapper.h
Normal file
103
iggy-decompiled/src/reconstructed/stb_truetype_wrapper.h
Normal file
@@ -0,0 +1,103 @@
|
||||
#ifndef IGGY_RECONSTRUCTED_STB_TRUETYPE_WRAPPER_H
|
||||
#define IGGY_RECONSTRUCTED_STB_TRUETYPE_WRAPPER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct IggyTTVertex {
|
||||
float x;
|
||||
float y;
|
||||
float cx;
|
||||
float cy;
|
||||
uint8_t type;
|
||||
int8_t padding;
|
||||
uint16_t f0;
|
||||
uint16_t f1;
|
||||
uint16_t line;
|
||||
} IggyTTVertex;
|
||||
|
||||
typedef struct IggyTTBitmap {
|
||||
int32_t w;
|
||||
int32_t h;
|
||||
int32_t stride;
|
||||
uint8_t *pixels;
|
||||
} IggyTTBitmap;
|
||||
|
||||
typedef struct IggyTTBakedChar {
|
||||
uint16_t x0;
|
||||
uint16_t y0;
|
||||
uint16_t x1;
|
||||
uint16_t y1;
|
||||
float xoff;
|
||||
float yoff;
|
||||
float xadvance;
|
||||
} IggyTTBakedChar;
|
||||
|
||||
typedef struct IggyTTAlignedQuad {
|
||||
float x0;
|
||||
float y0;
|
||||
float s0;
|
||||
float t0;
|
||||
float x1;
|
||||
float y1;
|
||||
float s1;
|
||||
float t1;
|
||||
} IggyTTAlignedQuad;
|
||||
|
||||
int32_t iggy_tt_BakeFontBitmap(const uint8_t *, int32_t, float, uint8_t *,
|
||||
int32_t, int32_t, int32_t, int32_t,
|
||||
IggyTTBakedChar *);
|
||||
int32_t iggy_tt_CompareUTF8toUTF16_bigendian(const char *, int32_t,
|
||||
const char *, int32_t);
|
||||
int32_t iggy_tt_FindGlyphIndex(void *, int32_t);
|
||||
int32_t iggy_tt_FindMatchingFont(const uint8_t *, const char *, int32_t);
|
||||
void *iggy_tt_FlattenCurves(const IggyTTVertex *, int32_t, float, int32_t **,
|
||||
int32_t *, void *);
|
||||
void iggy_tt_FreeBitmap(void *, void *);
|
||||
void iggy_tt_FreeShape(void *, void *);
|
||||
void iggy_tt_GetBakedQuad(const IggyTTBakedChar *, int32_t, int32_t, int32_t,
|
||||
float *, float *, IggyTTAlignedQuad *, int32_t);
|
||||
uint8_t *iggy_tt_GetCodepointBitmap(void *, float, float, int32_t, int32_t *,
|
||||
int32_t *, int32_t *, int32_t *);
|
||||
void iggy_tt_GetCodepointBitmapBox(void *, int32_t, float, float, int32_t *,
|
||||
int32_t *, int32_t *, int32_t *);
|
||||
int32_t iggy_tt_GetCodepointBox(void *, int32_t, int32_t *, int32_t *,
|
||||
int32_t *, int32_t *);
|
||||
void iggy_tt_GetCodepointHMetrics(void *, int32_t, int32_t *, int32_t *);
|
||||
int32_t iggy_tt_GetCodepointKernAdvance(void *, int32_t, int32_t);
|
||||
int32_t iggy_tt_GetCodepointShape(void *, int32_t, void *);
|
||||
void iggy_tt_GetFontBoundingBox(void *, int32_t *, int32_t *, int32_t *,
|
||||
int32_t *);
|
||||
const char *iggy_tt_GetFontNameString(void *, int32_t *, int32_t, int32_t,
|
||||
int32_t, int32_t);
|
||||
int32_t iggy_tt_GetFontOffsetForIndex(const uint8_t *, int32_t);
|
||||
void iggy_tt_GetFontVMetrics(void *, int32_t *, int32_t *, int32_t *);
|
||||
uint8_t *iggy_tt_GetGlyphBitmap(void *, float, float, int32_t, int32_t *,
|
||||
int32_t *, int32_t *, int32_t *);
|
||||
void iggy_tt_GetGlyphBitmapBox(void *, int32_t, float, float, int32_t *,
|
||||
int32_t *, int32_t *, int32_t *);
|
||||
int32_t iggy_tt_GetGlyphBox(void *, int32_t, int32_t *, int32_t *, int32_t *,
|
||||
int32_t *);
|
||||
void iggy_tt_GetGlyphHMetrics(void *, int32_t, int32_t *, int32_t *);
|
||||
int32_t iggy_tt_GetGlyphKernAdvance(void *, int32_t, int32_t);
|
||||
int32_t iggy_tt_GetGlyphShape(void *, int32_t, void *);
|
||||
int32_t iggy_tt_InitFont(void *, const uint8_t *, int32_t);
|
||||
int32_t iggy_tt_IsGlyphEmpty(void *, int32_t);
|
||||
void iggy_tt_MakeCodepointBitmap(void *, uint8_t *, int32_t, int32_t, int32_t,
|
||||
float, float, int32_t);
|
||||
void iggy_tt_MakeGlyphBitmap(void *, uint8_t *, int32_t, int32_t, int32_t,
|
||||
float, float, int32_t);
|
||||
void iggy_tt_Rasterize(IggyTTBitmap *, float, const float *, int32_t, float,
|
||||
float, int32_t, int32_t, int32_t, void *);
|
||||
float iggy_tt_ScaleForMappingEmToPixels(void *, float);
|
||||
float iggy_tt_ScaleForPixelHeight(void *, float);
|
||||
void iggy_tt_SetUserData(void *, void *);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,178 @@
|
||||
void iggy_tt_GetGlyphBitmapBox(void *font, int32_t glyph, float scale_x,
|
||||
float scale_y, int32_t *x0, int32_t *y0,
|
||||
int32_t *x1, int32_t *y1) {
|
||||
int32_t gx0;
|
||||
int32_t gy0;
|
||||
int32_t gx1;
|
||||
int32_t gy1;
|
||||
if (!iggy_tt_GetGlyphBox(font, glyph, &gx0, &gy0, &gx1, &gy1)) {
|
||||
if (x0 != NULL) *x0 = 0;
|
||||
if (y0 != NULL) *y0 = 0;
|
||||
if (x1 != NULL) *x1 = 0;
|
||||
if (y1 != NULL) *y1 = 0;
|
||||
return;
|
||||
}
|
||||
if (x0 != NULL) *x0 = tt_round_down((float)gx0 * scale_x);
|
||||
if (y0 != NULL) *y0 = tt_round_down((float)-gy1 * scale_y);
|
||||
if (x1 != NULL) *x1 = tt_round_up((float)gx1 * scale_x);
|
||||
if (y1 != NULL) *y1 = tt_round_up((float)-gy0 * scale_y);
|
||||
}
|
||||
|
||||
void iggy_tt_GetCodepointBitmapBox(void *font, int32_t codepoint,
|
||||
float scale_x, float scale_y, int32_t *x0,
|
||||
int32_t *y0, int32_t *x1, int32_t *y1) {
|
||||
iggy_tt_GetGlyphBitmapBox(font, iggy_tt_FindGlyphIndex(font, codepoint),
|
||||
scale_x, scale_y, x0, y0, x1, y1);
|
||||
}
|
||||
|
||||
void iggy_tt_MakeGlyphBitmap(void *font, uint8_t *output, int32_t out_w,
|
||||
int32_t out_h, int32_t out_stride, float scale_x,
|
||||
float scale_y, int32_t glyph) {
|
||||
TTFontInfo *info = (TTFontInfo *)font;
|
||||
IggyTTVertex *vertices = NULL;
|
||||
IggyTTBitmap bitmap;
|
||||
int32_t x0;
|
||||
int32_t y0;
|
||||
const int32_t count = iggy_tt_GetGlyphShape(font, glyph, &vertices);
|
||||
iggy_tt_GetGlyphBitmapBox(font, glyph, scale_x, scale_y, &x0, &y0, NULL,
|
||||
NULL);
|
||||
bitmap.w = out_w;
|
||||
bitmap.h = out_h;
|
||||
bitmap.stride = out_stride;
|
||||
bitmap.pixels = output;
|
||||
if (out_w != 0 && out_h != 0)
|
||||
iggy_tt_Rasterize(&bitmap, 0.35f, (const float *)vertices, count, scale_x,
|
||||
scale_y, x0, y0, 1, TT_USERDATA(info));
|
||||
tt_free(TT_USERDATA(info), vertices);
|
||||
}
|
||||
|
||||
void iggy_tt_MakeCodepointBitmap(void *font, uint8_t *output, int32_t out_w,
|
||||
int32_t out_h, int32_t out_stride,
|
||||
float scale_x, float scale_y,
|
||||
int32_t codepoint) {
|
||||
iggy_tt_MakeGlyphBitmap(font, output, out_w, out_h, out_stride, scale_x,
|
||||
scale_y, iggy_tt_FindGlyphIndex(font, codepoint));
|
||||
}
|
||||
|
||||
uint8_t *iggy_tt_GetGlyphBitmap(void *font, float scale_x, float scale_y,
|
||||
int32_t glyph, int32_t *width, int32_t *height,
|
||||
int32_t *x_offset, int32_t *y_offset) {
|
||||
TTFontInfo *info = (TTFontInfo *)font;
|
||||
IggyTTVertex *vertices = NULL;
|
||||
uint8_t *pixels = NULL;
|
||||
IggyTTBitmap bitmap;
|
||||
const int32_t count = iggy_tt_GetGlyphShape(font, glyph, &vertices);
|
||||
int32_t x0;
|
||||
int32_t y0;
|
||||
int32_t x1;
|
||||
int32_t y1;
|
||||
int32_t w;
|
||||
int32_t h;
|
||||
if (scale_x == 0.0f) scale_x = scale_y;
|
||||
if (scale_y == 0.0f) scale_y = scale_x;
|
||||
if (scale_x == 0.0f) return NULL;
|
||||
iggy_tt_GetGlyphBitmapBox(font, glyph, scale_x, scale_y, &x0, &y0, &x1,
|
||||
&y1);
|
||||
w = x1 - x0;
|
||||
h = y1 - y0;
|
||||
if (width != NULL) *width = w;
|
||||
if (height != NULL) *height = h;
|
||||
if (x_offset != NULL) *x_offset = x0;
|
||||
if (y_offset != NULL) *y_offset = y0;
|
||||
if (w != 0 && h != 0) {
|
||||
pixels = (uint8_t *)tt_alloc(TT_USERDATA(info), w * h);
|
||||
if (pixels != NULL) {
|
||||
bitmap.w = w;
|
||||
bitmap.h = h;
|
||||
bitmap.stride = w;
|
||||
bitmap.pixels = pixels;
|
||||
iggy_tt_Rasterize(&bitmap, 0.35f, (const float *)vertices, count, scale_x,
|
||||
scale_y, x0, y0, 1, TT_USERDATA(info));
|
||||
}
|
||||
}
|
||||
iggy_free_alloc((intptr_t)TT_USERDATA(info), vertices);
|
||||
return pixels;
|
||||
}
|
||||
|
||||
uint8_t *iggy_tt_GetCodepointBitmap(void *font, float scale_x, float scale_y,
|
||||
int32_t codepoint, int32_t *width,
|
||||
int32_t *height, int32_t *x_offset,
|
||||
int32_t *y_offset) {
|
||||
return iggy_tt_GetGlyphBitmap(font, scale_x, scale_y,
|
||||
iggy_tt_FindGlyphIndex(font, codepoint), width,
|
||||
height, x_offset, y_offset);
|
||||
}
|
||||
|
||||
void iggy_tt_FreeBitmap(void *bitmap, void *userdata) {
|
||||
iggy_free_alloc((intptr_t)userdata, bitmap);
|
||||
}
|
||||
|
||||
int32_t iggy_tt_BakeFontBitmap(const uint8_t *data, int32_t offset,
|
||||
float font_height, uint8_t *pixels,
|
||||
int32_t pixel_width, int32_t bitmap_height,
|
||||
int32_t first_char, int32_t num_chars,
|
||||
IggyTTBakedChar *chars) {
|
||||
TTFontInfo info;
|
||||
float scale;
|
||||
int32_t x = 1;
|
||||
int32_t y = 1;
|
||||
int32_t bottom = 1;
|
||||
int32_t i;
|
||||
(void)iggy_tt_InitFont(&info, data, offset);
|
||||
memset(pixels, 0, (size_t)(pixel_width * bitmap_height));
|
||||
scale = iggy_tt_ScaleForPixelHeight(&info, font_height);
|
||||
for (i = 0; i < num_chars; ++i) {
|
||||
const int32_t glyph = iggy_tt_FindGlyphIndex(&info, first_char + i);
|
||||
int32_t advance;
|
||||
int32_t left_bearing;
|
||||
int32_t x0;
|
||||
int32_t y0;
|
||||
int32_t x1;
|
||||
int32_t y1;
|
||||
int32_t w;
|
||||
int32_t h;
|
||||
iggy_tt_GetGlyphHMetrics(&info, glyph, &advance, &left_bearing);
|
||||
(void)left_bearing;
|
||||
iggy_tt_GetGlyphBitmapBox(&info, glyph, scale, scale, &x0, &y0, &x1, &y1);
|
||||
w = x1 - x0;
|
||||
h = y1 - y0;
|
||||
if (x + w + 1 >= pixel_width) {
|
||||
y = bottom;
|
||||
x = 1;
|
||||
}
|
||||
if (y + h + 1 >= bitmap_height) return -i;
|
||||
iggy_tt_MakeGlyphBitmap(&info, pixels + x + y * pixel_width, w, h,
|
||||
pixel_width, scale, scale, glyph);
|
||||
chars[i].x0 = (uint16_t)x;
|
||||
chars[i].y0 = (uint16_t)y;
|
||||
chars[i].x1 = (uint16_t)(x + w);
|
||||
chars[i].y1 = (uint16_t)(y + h);
|
||||
chars[i].xadvance = scale * (float)advance;
|
||||
chars[i].xoff = (float)x0;
|
||||
chars[i].yoff = (float)y0;
|
||||
x += w + 2;
|
||||
if (y + h + 2 > bottom) bottom = y + h + 2;
|
||||
}
|
||||
return bottom;
|
||||
}
|
||||
|
||||
void iggy_tt_GetBakedQuad(const IggyTTBakedChar *chars, int32_t pixel_width,
|
||||
int32_t pixel_height, int32_t char_index,
|
||||
float *x_position, float *y_position,
|
||||
IggyTTAlignedQuad *quad, int32_t opengl_fillrule) {
|
||||
const IggyTTBakedChar *baked = chars + char_index;
|
||||
const float bias = opengl_fillrule ? 0.0f : -0.5f;
|
||||
const float inverse_width = 1.0f / (float)pixel_width;
|
||||
const float inverse_height = 1.0f / (float)pixel_height;
|
||||
const float x = (float)tt_round_down(*x_position + baked->xoff + 0.5f) + bias;
|
||||
const float y = (float)tt_round_down(*y_position + baked->yoff + 0.5f) + bias;
|
||||
quad->x0 = x;
|
||||
quad->y0 = y;
|
||||
quad->x1 = x + (float)(baked->x1 - baked->x0);
|
||||
quad->y1 = y + (float)(baked->y1 - baked->y0);
|
||||
quad->s0 = (float)baked->x0 * inverse_width;
|
||||
quad->t0 = (float)baked->y0 * inverse_width;
|
||||
quad->s1 = (float)baked->x1 * inverse_height;
|
||||
quad->t1 = (float)baked->y1 * inverse_height;
|
||||
*x_position += baked->xadvance;
|
||||
}
|
||||
387
iggy-decompiled/src/reconstructed/stb_truetype_wrapper_font.inc
Normal file
387
iggy-decompiled/src/reconstructed/stb_truetype_wrapper_font.inc
Normal file
@@ -0,0 +1,387 @@
|
||||
static int32_t tt_is_font(const uint8_t *data) {
|
||||
const uint32_t tag = tt_u32(data);
|
||||
return tag == 0x00010000u || tag == 0x4f54544fu ||
|
||||
tag == 0x31000000u || tag == 0x74797031u;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_GetFontOffsetForIndex(const uint8_t *data, int32_t index) {
|
||||
if (tt_is_font(data)) {
|
||||
return index == 0 ? 0 : -1;
|
||||
}
|
||||
if (tt_u32(data) != 0x74746366u ||
|
||||
(tt_u32(data + 4) != 0x00010000u &&
|
||||
tt_u32(data + 4) != 0x00020000u) ||
|
||||
index >= (int32_t)tt_u32(data + 8)) {
|
||||
return -1;
|
||||
}
|
||||
return (int32_t)tt_u32(data + 12 + index * 4);
|
||||
}
|
||||
|
||||
static int32_t tt_find_table(const uint8_t *data, int32_t fontstart,
|
||||
uint32_t tag) {
|
||||
const int32_t count = (int32_t)tt_u16(data + fontstart + 4);
|
||||
const uint8_t *record = data + fontstart + 12;
|
||||
int32_t i;
|
||||
for (i = 0; i < count; ++i, record += 16) {
|
||||
if (tt_u32(record) == tag) {
|
||||
return (int32_t)tt_u32(record + 8);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_InitFont(void *font, const uint8_t *data, int32_t fontstart) {
|
||||
TTFontInfo *info = (TTFontInfo *)font;
|
||||
int32_t cmap;
|
||||
int32_t maxp;
|
||||
int32_t i;
|
||||
|
||||
info->data_value = (uintptr_t)data;
|
||||
info->fontstart = fontstart;
|
||||
cmap = tt_find_table(data, fontstart, 0x636d6170u);
|
||||
info->loca = tt_find_table(data, fontstart, 0x6c6f6361u);
|
||||
info->head = tt_find_table(data, fontstart, 0x68656164u);
|
||||
info->glyf = tt_find_table(data, fontstart, 0x676c7966u);
|
||||
info->hhea = tt_find_table(data, fontstart, 0x68686561u);
|
||||
info->hmtx = tt_find_table(data, fontstart, 0x686d7478u);
|
||||
info->os2 = tt_find_table(data, fontstart, 0x4f532f32u);
|
||||
if (cmap == 0 || info->loca == 0 || info->head == 0 || info->glyf == 0 ||
|
||||
info->hhea == 0 || info->hmtx == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
maxp = tt_find_table(data, fontstart, 0x6d617870u);
|
||||
info->numGlyphs = maxp != 0 ? tt_u16(data + maxp + 4) : 0xffffu;
|
||||
info->index_map = 0;
|
||||
for (i = 0; i < (int32_t)tt_u16(data + cmap + 2); ++i) {
|
||||
const uint8_t *record = data + cmap + 4 + i * 8;
|
||||
const uint16_t platform = tt_u16(record);
|
||||
const uint16_t encoding = tt_u16(record + 2);
|
||||
if (platform == 3 && (encoding == 1 || encoding == 10)) {
|
||||
info->index_map = cmap + (int32_t)tt_u32(record + 4);
|
||||
}
|
||||
}
|
||||
if (info->index_map == 0) {
|
||||
return 0;
|
||||
}
|
||||
info->indexToLocFormat = tt_u16(data + info->head + 0x32);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void iggy_tt_SetUserData(void *font, void *userdata) {
|
||||
((TTFontInfo *)font)->userdata_value = (uintptr_t)userdata;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_FindGlyphIndex(void *font, int32_t codepoint) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const uint8_t *map = TT_DATA(info) + info->index_map;
|
||||
const uint16_t format = tt_u16(map);
|
||||
if (format == 0) {
|
||||
return codepoint < (int32_t)tt_u16(map + 2) - 6 ? map[6 + codepoint] : 0;
|
||||
}
|
||||
if (format == 2) {
|
||||
return 0;
|
||||
}
|
||||
if (format == 4) {
|
||||
const int32_t segcount = tt_u16(map + 6) >> 1;
|
||||
const uint8_t *end_count = map + 14;
|
||||
const uint8_t *start_count = end_count + segcount * 2 + 2;
|
||||
const uint8_t *id_delta = start_count + segcount * 2;
|
||||
const uint8_t *id_range = id_delta + segcount * 2;
|
||||
uint16_t search_range = tt_u16(map + 8) >> 1;
|
||||
uint16_t entry_selector = tt_u16(map + 10);
|
||||
const uint16_t range_shift = tt_u16(map + 12) >> 1;
|
||||
const uint8_t *search = end_count;
|
||||
int32_t segment;
|
||||
uint16_t glyph;
|
||||
if (codepoint > 0xffff) {
|
||||
return 0;
|
||||
}
|
||||
if (tt_u16(search + range_shift * 2) <= codepoint)
|
||||
search += range_shift * 2;
|
||||
search -= 2;
|
||||
while (entry_selector != 0) {
|
||||
search_range >>= 1;
|
||||
if (tt_u16(search + search_range * 2) < codepoint)
|
||||
search += search_range * 2;
|
||||
--entry_selector;
|
||||
}
|
||||
segment = (int32_t)((search + 2 - end_count) >> 1);
|
||||
if (codepoint < tt_u16(start_count + segment * 2)) {
|
||||
return 0;
|
||||
}
|
||||
if (tt_u16(id_range + segment * 2) == 0) {
|
||||
return (codepoint + tt_s16(id_delta + segment * 2)) & 0xffff;
|
||||
}
|
||||
glyph = tt_u16(id_range + segment * 2 +
|
||||
tt_u16(id_range + segment * 2) +
|
||||
(codepoint - tt_u16(start_count + segment * 2)) * 2);
|
||||
return glyph;
|
||||
}
|
||||
if (format == 6) {
|
||||
const int32_t first = tt_u16(map + 6);
|
||||
const int32_t count = tt_u16(map + 8);
|
||||
return codepoint >= first && codepoint < first + count
|
||||
? tt_u16(map + 10 + (codepoint - first) * 2)
|
||||
: 0;
|
||||
}
|
||||
if (format == 12) {
|
||||
int32_t low = 0;
|
||||
int32_t high = tt_u16(map + 6);
|
||||
while (low <= high) {
|
||||
const int32_t middle = low + ((high - low) >> 1);
|
||||
const uint8_t *group = map + 16 + middle * 12;
|
||||
if ((uint32_t)codepoint < tt_u32(group)) {
|
||||
high = middle - 1;
|
||||
} else if ((uint32_t)codepoint > tt_u32(group + 4)) {
|
||||
low = middle + 1;
|
||||
} else {
|
||||
return (int32_t)(tt_u32(group + 8) +
|
||||
(uint32_t)codepoint - tt_u32(group));
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
float iggy_tt_ScaleForPixelHeight(void *font, float height) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const int32_t ascent = tt_s16(TT_DATA(info) + info->hhea + 4);
|
||||
const int32_t descent = tt_s16(TT_DATA(info) + info->hhea + 6);
|
||||
return height / (float)(ascent - descent);
|
||||
}
|
||||
|
||||
float iggy_tt_ScaleForMappingEmToPixels(void *font, float pixels) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
return pixels / (float)tt_u16(TT_DATA(info) + info->head + 0x12);
|
||||
}
|
||||
|
||||
void iggy_tt_GetFontVMetrics(void *font, int32_t *ascent, int32_t *descent,
|
||||
int32_t *line_gap) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const uint8_t *data = TT_DATA(info);
|
||||
if (info->os2 != 0 && tt_s16(data + info->os2) >= 1) {
|
||||
if (ascent != NULL) {
|
||||
*ascent = tt_s16(data + info->os2 + 0x4a);
|
||||
}
|
||||
if (descent != NULL) {
|
||||
*descent = -tt_s16(data + info->os2 + 0x4c);
|
||||
}
|
||||
if (line_gap != NULL) {
|
||||
*line_gap = tt_s16(data + info->os2 + 0x48);
|
||||
}
|
||||
} else {
|
||||
if (ascent != NULL) {
|
||||
*ascent = tt_s16(data + info->hhea + 4);
|
||||
}
|
||||
if (descent != NULL) {
|
||||
*descent = tt_s16(data + info->hhea + 6);
|
||||
}
|
||||
if (line_gap != NULL) {
|
||||
*line_gap = tt_s16(data + info->hhea + 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void iggy_tt_GetFontBoundingBox(void *font, int32_t *x0, int32_t *y0,
|
||||
int32_t *x1, int32_t *y1) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const uint8_t *box = TT_DATA(info) + info->head + 0x24;
|
||||
*x0 = tt_s16(box);
|
||||
*y0 = tt_s16(box + 2);
|
||||
*x1 = tt_s16(box + 4);
|
||||
*y1 = tt_s16(box + 6);
|
||||
}
|
||||
|
||||
void iggy_tt_GetGlyphHMetrics(void *font, int32_t glyph, int32_t *advance,
|
||||
int32_t *left_bearing) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const int32_t long_metrics = tt_u16(TT_DATA(info) + info->hhea + 0x22);
|
||||
if (glyph < long_metrics) {
|
||||
if (advance != NULL)
|
||||
*advance = tt_u16(TT_DATA(info) + info->hmtx + glyph * 4);
|
||||
if (left_bearing != NULL)
|
||||
*left_bearing = tt_s16(TT_DATA(info) + info->hmtx + glyph * 4 + 2);
|
||||
} else {
|
||||
if (advance != NULL)
|
||||
*advance = tt_u16(TT_DATA(info) + info->hmtx + (long_metrics - 1) * 4);
|
||||
if (left_bearing != NULL)
|
||||
*left_bearing = tt_s16(TT_DATA(info) + info->hmtx + long_metrics * 4 +
|
||||
(glyph - long_metrics) * 2);
|
||||
}
|
||||
}
|
||||
|
||||
void iggy_tt_GetCodepointHMetrics(void *font, int32_t codepoint,
|
||||
int32_t *advance, int32_t *left_bearing) {
|
||||
iggy_tt_GetGlyphHMetrics(font, iggy_tt_FindGlyphIndex(font, codepoint),
|
||||
advance, left_bearing);
|
||||
}
|
||||
|
||||
int32_t iggy_tt_GetGlyphKernAdvance(void *font, int32_t glyph1,
|
||||
int32_t glyph2) {
|
||||
(void)font;
|
||||
(void)glyph1;
|
||||
(void)glyph2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_GetCodepointKernAdvance(void *font, int32_t codepoint1,
|
||||
int32_t codepoint2) {
|
||||
(void)font;
|
||||
(void)codepoint1;
|
||||
(void)codepoint2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *iggy_tt_GetFontNameString(void *font, int32_t *length,
|
||||
int32_t platform, int32_t encoding,
|
||||
int32_t language, int32_t name_id) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const uint8_t *data = TT_DATA(info);
|
||||
const int32_t table = tt_find_table(data, info->fontstart, 0x6e616d65u);
|
||||
int32_t i;
|
||||
if (table == 0) return NULL;
|
||||
for (i = 0; i < tt_u16(data + table + 2); ++i) {
|
||||
const uint8_t *record = data + table + 6 + i * 12;
|
||||
if (tt_u16(record) == platform && tt_u16(record + 2) == encoding &&
|
||||
tt_u16(record + 4) == language && tt_u16(record + 6) == name_id) {
|
||||
*length = tt_u16(record + 8);
|
||||
return (const char *)(data + table + tt_u16(data + table + 4) +
|
||||
tt_u16(record + 10));
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int32_t tt_utf8_prefix(const char *s1, int32_t len1, const uint8_t *s2,
|
||||
int32_t len2) {
|
||||
int32_t i = 0;
|
||||
while (len2 != 0) {
|
||||
const uint16_t ch = tt_u16(s2);
|
||||
if (ch < 0x80) {
|
||||
if (len1 <= i || (uint8_t)s1[i] != ch) return -1;
|
||||
++i;
|
||||
} else if (ch < 0x800) {
|
||||
if (len1 <= i + 1 || (uint8_t)s1[i] != (ch >> 6) + 0xc0 ||
|
||||
(uint8_t)s1[i + 1] != (ch & 0x3f) + 0x80)
|
||||
return -1;
|
||||
i += 2;
|
||||
} else if (ch >= 0xd800 && ch < 0xdc00) {
|
||||
const uint32_t codepoint =
|
||||
(uint32_t)ch * 0x400 + tt_u16(s2 + 2) + 0xfca02400u;
|
||||
if (len1 <= i + 3 ||
|
||||
(uint8_t)s1[i] != (codepoint >> 18) + 0xf0 ||
|
||||
(uint8_t)s1[i + 1] != ((codepoint >> 12) & 0x3f) + 0x80 ||
|
||||
(uint8_t)s1[i + 2] != ((codepoint >> 6) & 0x3f) + 0x80 ||
|
||||
(uint8_t)s1[i + 3] != (codepoint & 0x3f) + 0x80)
|
||||
return -1;
|
||||
i += 4;
|
||||
s2 += 2;
|
||||
len2 -= 2;
|
||||
} else {
|
||||
if ((ch >= 0xdc00 && ch < 0xe000) || len1 <= i + 2 ||
|
||||
(uint8_t)s1[i] != (ch >> 12) + 0xe0 ||
|
||||
(uint8_t)s1[i + 1] != ((ch >> 6) & 0x3f) + 0x80 ||
|
||||
(uint8_t)s1[i + 2] != (ch & 0x3f) + 0x80)
|
||||
return -1;
|
||||
i += 3;
|
||||
}
|
||||
s2 += 2;
|
||||
len2 -= 2;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_CompareUTF8toUTF16_bigendian(const char *s1, int32_t len1,
|
||||
const char *s2, int32_t len2) {
|
||||
return tt_utf8_prefix(s1, len1, (const uint8_t *)s2, len2) == len1;
|
||||
}
|
||||
|
||||
static int32_t tt_match_pair(const uint8_t *data, int32_t table,
|
||||
const char *name, int32_t name_len,
|
||||
int32_t target_id, int32_t next_id) {
|
||||
const int32_t count = tt_u16(data + table + 2);
|
||||
const int32_t storage = table + tt_u16(data + table + 4);
|
||||
int32_t i;
|
||||
for (i = 0; i < count; ++i) {
|
||||
const uint8_t *record = data + table + 6 + i * 12;
|
||||
const int32_t platform = tt_u16(record);
|
||||
const int32_t encoding = tt_u16(record + 2);
|
||||
if ((platform == 0 ||
|
||||
(platform == 3 && (encoding == 1 || encoding == 10))) &&
|
||||
tt_u16(record + 6) == target_id) {
|
||||
const int32_t bytes = tt_u16(record + 8);
|
||||
const uint8_t *text = data + storage + tt_u16(record + 10);
|
||||
const int32_t prefix = tt_utf8_prefix(name, name_len, text, bytes);
|
||||
int32_t has_pair = 0;
|
||||
if (prefix < 0) continue;
|
||||
if (next_id >= 0 && i + 1 < count) {
|
||||
const uint8_t *next = record + 12;
|
||||
const int32_t next_bytes = tt_u16(next + 8);
|
||||
has_pair = tt_u16(next + 6) == next_id &&
|
||||
tt_u16(next) == platform &&
|
||||
tt_u16(next + 2) == encoding &&
|
||||
tt_u16(next + 4) == tt_u16(record + 4) && next_bytes != 0;
|
||||
if (has_pair) {
|
||||
if (prefix < name_len && name[prefix] == ' ' &&
|
||||
tt_utf8_prefix(name + prefix + 1, name_len - prefix - 1,
|
||||
data + storage + tt_u16(next + 10), next_bytes) ==
|
||||
name_len - prefix - 1) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!has_pair && prefix == name_len) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t tt_name_length(const char *name);
|
||||
|
||||
static int32_t tt_matches(const uint8_t *data, int32_t offset,
|
||||
const char *name, int32_t flags) {
|
||||
const int32_t name_len = tt_name_length(name);
|
||||
int32_t table;
|
||||
if (!tt_is_font(data + offset) ||
|
||||
(flags != 0 &&
|
||||
((data[tt_find_table(data, offset, 0x68656164u) + 0x2d] ^ flags) &
|
||||
7) != 0)) {
|
||||
return 0;
|
||||
}
|
||||
table = tt_find_table(data, offset, 0x6e616d65u);
|
||||
if (table == 0) return 0;
|
||||
if (flags == 0) {
|
||||
if (tt_match_pair(data, table, name, name_len, 16, 17) ||
|
||||
tt_match_pair(data, table, name, name_len, 1, 2)) {
|
||||
return 1;
|
||||
}
|
||||
} else if (tt_match_pair(data, table, name, name_len, 16, -1) ||
|
||||
tt_match_pair(data, table, name, name_len, 1, -1)) {
|
||||
return 1;
|
||||
}
|
||||
return tt_match_pair(data, table, name, name_len, 3, -1);
|
||||
}
|
||||
|
||||
static int32_t tt_name_length(const char *name) {
|
||||
#if defined(IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO)
|
||||
int32_t result = 0;
|
||||
while (name[result] != '\0') ++result;
|
||||
return result;
|
||||
#else
|
||||
return (int32_t)strlen(name);
|
||||
#endif
|
||||
}
|
||||
|
||||
int32_t iggy_tt_FindMatchingFont(const uint8_t *data, const char *name,
|
||||
int32_t flags) {
|
||||
int32_t index = 0;
|
||||
for (;;) {
|
||||
const int32_t offset = iggy_tt_GetFontOffsetForIndex(data, index);
|
||||
if (offset < 0) return -1;
|
||||
if (tt_matches(data, offset, name, flags)) return offset;
|
||||
++index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
typedef struct TTPointBuffer {
|
||||
TTPoint *points;
|
||||
int32_t count;
|
||||
int32_t capacity;
|
||||
void *userdata;
|
||||
} TTPointBuffer;
|
||||
|
||||
typedef struct TTEdge {
|
||||
int32_t x0;
|
||||
int32_t y0;
|
||||
int32_t x1;
|
||||
int32_t y1;
|
||||
int32_t direction;
|
||||
struct TTEdge *next;
|
||||
} TTEdge;
|
||||
|
||||
_Static_assert(sizeof(TTEdge) == (IGGY_TT_64BIT ? 0x20 : 0x18),
|
||||
"raster edge layout");
|
||||
|
||||
static void tt_add_point(TTPointBuffer *buffer, float x, float y) {
|
||||
if (buffer->count == buffer->capacity) {
|
||||
const int32_t capacity = buffer->capacity * 2;
|
||||
TTPoint *points = (TTPoint *)tt_alloc(
|
||||
buffer->userdata, capacity * (int32_t)sizeof(*points));
|
||||
memcpy(points, buffer->points,
|
||||
(size_t)buffer->count * sizeof(*buffer->points));
|
||||
tt_free(buffer->userdata, buffer->points);
|
||||
buffer->points = points;
|
||||
buffer->capacity = capacity;
|
||||
}
|
||||
buffer->points[buffer->count].x = x;
|
||||
buffer->points[buffer->count].y = y;
|
||||
++buffer->count;
|
||||
}
|
||||
|
||||
static void tt_tesselate_curve(TTPointBuffer *buffer, float x0, float y0,
|
||||
float x1, float y1, float x2, float y2,
|
||||
float flatness_squared) {
|
||||
float stack_x0[16], stack_y0[16], stack_x1[16];
|
||||
float stack_y1[16], stack_x2[16], stack_y2[16];
|
||||
int32_t stack_count = 0;
|
||||
for (;;) {
|
||||
const float mx = (x1 * 2.0f + x0 + x2) * 0.25f;
|
||||
const float my = (y1 * 2.0f + y0 + y2) * 0.25f;
|
||||
const float dx = (x0 + x2) * 0.5f - mx;
|
||||
const float dy = (y0 + y2) * 0.5f - my;
|
||||
if (stack_count < 16 && flatness_squared < dx * dx + dy * dy) {
|
||||
stack_x0[stack_count] = mx;
|
||||
stack_y0[stack_count] = my;
|
||||
stack_x1[stack_count] = (x1 + x2) * 0.5f;
|
||||
stack_y1[stack_count] = (y1 + y2) * 0.5f;
|
||||
stack_x2[stack_count] = x2;
|
||||
stack_y2[stack_count] = y2;
|
||||
++stack_count;
|
||||
x1 = (x0 + x1) * 0.5f;
|
||||
y1 = (y0 + y1) * 0.5f;
|
||||
x2 = mx;
|
||||
y2 = my;
|
||||
continue;
|
||||
}
|
||||
tt_add_point(buffer, x2, y2);
|
||||
if (--stack_count < 0) return;
|
||||
x0 = stack_x0[stack_count];
|
||||
y0 = stack_y0[stack_count];
|
||||
x1 = stack_x1[stack_count];
|
||||
y1 = stack_y1[stack_count];
|
||||
x2 = stack_x2[stack_count];
|
||||
y2 = stack_y2[stack_count];
|
||||
}
|
||||
}
|
||||
|
||||
void *iggy_tt_FlattenCurves(const IggyTTVertex *vertices, int32_t num_vertices,
|
||||
float flatness, int32_t **contour_lengths,
|
||||
int32_t *num_contours, void *userdata) {
|
||||
TTPointBuffer buffer;
|
||||
int32_t contours = 0;
|
||||
int32_t contour = -1;
|
||||
int32_t contour_start = 0;
|
||||
int32_t i;
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
const float flatness_squared = flatness * flatness;
|
||||
for (i = 0; i < num_vertices; ++i) {
|
||||
if (vertices[i].type == TT_vmove) ++contours;
|
||||
}
|
||||
*num_contours = contours;
|
||||
if (contours == 0) return NULL;
|
||||
*contour_lengths = (int32_t *)tt_alloc(
|
||||
userdata, contours * (int32_t)sizeof(**contour_lengths));
|
||||
if (*contour_lengths == NULL) {
|
||||
*num_contours = 0;
|
||||
return NULL;
|
||||
}
|
||||
buffer.points = (TTPoint *)tt_alloc(userdata, 256 * (int32_t)sizeof(TTPoint));
|
||||
buffer.count = 0;
|
||||
buffer.capacity = 256;
|
||||
buffer.userdata = userdata;
|
||||
if (buffer.points == NULL) {
|
||||
tt_free(userdata, buffer.points);
|
||||
tt_free(userdata, *contour_lengths);
|
||||
*contour_lengths = NULL;
|
||||
*num_contours = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_vertices; ++i) {
|
||||
const IggyTTVertex *vertex = vertices + i;
|
||||
if (vertex->type == TT_vmove) {
|
||||
if (contour >= 0) {
|
||||
(*contour_lengths)[contour] = buffer.count - contour_start;
|
||||
}
|
||||
++contour;
|
||||
contour_start = buffer.count;
|
||||
x = vertex->x;
|
||||
y = vertex->y;
|
||||
tt_add_point(&buffer, x, y);
|
||||
} else if (vertex->type == TT_vline) {
|
||||
x = vertex->x;
|
||||
y = vertex->y;
|
||||
tt_add_point(&buffer, x, y);
|
||||
} else if (vertex->type == TT_vcurve) {
|
||||
tt_tesselate_curve(&buffer, x, y, vertex->cx, vertex->cy, vertex->x,
|
||||
vertex->y, flatness_squared);
|
||||
x = vertex->x;
|
||||
y = vertex->y;
|
||||
}
|
||||
}
|
||||
(*contour_lengths)[contour] = buffer.count - contour_start;
|
||||
return buffer.points;
|
||||
}
|
||||
|
||||
static void tt_swap_edges(TTEdge *a, TTEdge *b) {
|
||||
const TTEdge edge = *a;
|
||||
*a = *b;
|
||||
*b = edge;
|
||||
}
|
||||
|
||||
static void tt_sort_edges(TTEdge *edges, int32_t count) {
|
||||
int32_t i;
|
||||
if (count >= 13) {
|
||||
const int32_t middle = count >> 1;
|
||||
int32_t pivot_index;
|
||||
int32_t partition = 0;
|
||||
if (edges[1].y0 < edges[middle].y0) {
|
||||
pivot_index = edges[count - 2].y0 <= edges[middle].y0
|
||||
? (edges[count - 2].y0 <= edges[1].y0 ? 1 : count - 2)
|
||||
: middle;
|
||||
} else {
|
||||
pivot_index = edges[1].y0 < edges[count - 2].y0
|
||||
? 1
|
||||
: (edges[middle].y0 < edges[count - 2].y0 ? count - 2
|
||||
: middle);
|
||||
}
|
||||
tt_swap_edges(edges + pivot_index, edges + count - 1);
|
||||
for (i = 0; i < count - 1; ++i) {
|
||||
if (edges[i].y0 < edges[count - 1].y0) {
|
||||
tt_swap_edges(edges + i, edges + partition++);
|
||||
}
|
||||
}
|
||||
tt_swap_edges(edges + partition, edges + count - 1);
|
||||
tt_sort_edges(edges, partition);
|
||||
tt_sort_edges(edges + partition + 1, count - partition - 1);
|
||||
return;
|
||||
}
|
||||
for (i = 1; i < count; ++i) {
|
||||
const TTEdge edge = edges[i];
|
||||
int32_t j = i;
|
||||
while (j > 0 && edge.y0 < edges[j - 1].y0) {
|
||||
edges[j] = edges[j - 1];
|
||||
--j;
|
||||
}
|
||||
edges[j] = edge;
|
||||
}
|
||||
}
|
||||
|
||||
static void tt_fill_active_edges(uint8_t *scanline, int32_t width,
|
||||
TTEdge *edge, int32_t weight) {
|
||||
uint32_t start_x = 0;
|
||||
uint32_t winding = 0;
|
||||
while (edge != NULL) {
|
||||
if (winding == 0) {
|
||||
start_x = (uint32_t)edge->x0;
|
||||
winding = (uint32_t)edge->direction;
|
||||
} else {
|
||||
const uint32_t end_x = (uint32_t)edge->x0;
|
||||
winding += (uint32_t)edge->direction;
|
||||
if (winding == 0) {
|
||||
int32_t first = (int32_t)start_x >> 10;
|
||||
const int32_t last = (int32_t)end_x >> 10;
|
||||
if (first < width && last >= 0) {
|
||||
if (first == last) {
|
||||
scanline[first] += (uint8_t)((int32_t)((end_x - start_x) *
|
||||
(uint32_t)weight) >>
|
||||
10);
|
||||
} else {
|
||||
int32_t fill_end = width;
|
||||
if (first < 0) {
|
||||
first = 0;
|
||||
} else {
|
||||
scanline[first] +=
|
||||
(uint8_t)((int32_t)((0x400 - (start_x & 0x3ff)) *
|
||||
(uint32_t)weight) >>
|
||||
10);
|
||||
++first;
|
||||
}
|
||||
if (last < width) {
|
||||
scanline[last] +=
|
||||
(uint8_t)((int32_t)((end_x & 0x3ff) *
|
||||
(uint32_t)weight) >>
|
||||
10);
|
||||
fill_end = last;
|
||||
}
|
||||
while (first < fill_end) scanline[first++] += (uint8_t)weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
edge = edge->next;
|
||||
}
|
||||
}
|
||||
|
||||
static void tt_rasterize_sorted_edges(IggyTTBitmap *bitmap, TTEdge *edges,
|
||||
int32_t edge_count, int32_t vsubsample,
|
||||
int32_t x_off, int32_t y_off) {
|
||||
TTEdge *active = NULL;
|
||||
TTEdge *edge = edges;
|
||||
int32_t scan_y = y_off * vsubsample * 0x400 + 0x200;
|
||||
int32_t y;
|
||||
edges[edge_count].y0 = ((bitmap->h + y_off) * vsubsample + 1) * 0x400;
|
||||
for (y = 0; y < bitmap->h; ++y) {
|
||||
uint8_t *scanline = bitmap->pixels + y * bitmap->stride;
|
||||
int32_t sample;
|
||||
memset(scanline, 0, (size_t)bitmap->w);
|
||||
for (sample = 0; sample < vsubsample; ++sample) {
|
||||
TTEdge **link = &active;
|
||||
int32_t previous_x = INT32_MIN;
|
||||
int32_t needs_sort = 0;
|
||||
while (*link != NULL) {
|
||||
TTEdge *current = *link;
|
||||
if (scan_y < current->y1) {
|
||||
current->x0 += current->x1;
|
||||
if (current->x0 < previous_x) needs_sort = 1;
|
||||
previous_x = current->x0;
|
||||
link = ¤t->next;
|
||||
} else {
|
||||
*link = current->next;
|
||||
}
|
||||
}
|
||||
while (needs_sort) {
|
||||
TTEdge *current = active;
|
||||
link = &active;
|
||||
needs_sort = 0;
|
||||
while (current != NULL && current->next != NULL) {
|
||||
TTEdge *next = current->next;
|
||||
if (next->x0 < current->x0) {
|
||||
needs_sort = 1;
|
||||
current->next = next->next;
|
||||
next->next = current;
|
||||
*link = next;
|
||||
}
|
||||
link = ¤t->next;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
while (edge->y0 <= scan_y) {
|
||||
if (scan_y < edge->y1) {
|
||||
edge->next = NULL;
|
||||
edge->x1 = tt_div((edge->x1 - edge->x0) * 0x400,
|
||||
edge->y1 - edge->y0);
|
||||
edge->x0 += ((scan_y - edge->y0) * edge->x1 >> 10) - x_off * 0x400;
|
||||
if (active == NULL) {
|
||||
active = edge;
|
||||
} else if (edge->x0 < active->x0) {
|
||||
edge->next = active;
|
||||
active = edge;
|
||||
} else {
|
||||
TTEdge *insert = active;
|
||||
while (insert->next != NULL && insert->next->x0 < edge->x0)
|
||||
insert = insert->next;
|
||||
edge->next = insert->next;
|
||||
insert->next = edge;
|
||||
}
|
||||
}
|
||||
++edge;
|
||||
}
|
||||
if (active != NULL)
|
||||
tt_fill_active_edges(scanline, bitmap->w, active,
|
||||
tt_div(0xff, vsubsample));
|
||||
scan_y += 0x400;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void tt_rasterize(IggyTTBitmap *bitmap, const TTPoint *points,
|
||||
const int32_t *contour_lengths, int32_t contours,
|
||||
float scale_x, float scale_y, int32_t x_off,
|
||||
int32_t y_off, int32_t invert, void *userdata) {
|
||||
const int32_t vsubsample = bitmap->h < 8 ? 15 : 5;
|
||||
int32_t total_points = 0;
|
||||
int32_t edge_count = 0;
|
||||
int32_t contour;
|
||||
int32_t point_base = 0;
|
||||
TTEdge *edges;
|
||||
if (invert) scale_y = -scale_y;
|
||||
for (contour = 0; contour < contours; ++contour)
|
||||
total_points += contour_lengths[contour];
|
||||
edges = (TTEdge *)tt_alloc(userdata,
|
||||
(total_points + 1) * (int32_t)sizeof(*edges));
|
||||
if (edges == NULL) return;
|
||||
for (contour = 0; contour < contours; ++contour) {
|
||||
const int32_t count = contour_lengths[contour];
|
||||
int32_t i;
|
||||
for (i = 0; i < count; ++i) {
|
||||
const TTPoint *a =
|
||||
points + point_base + (i == 0 ? count - 1 : i - 1);
|
||||
const TTPoint *b = points + point_base + i;
|
||||
const TTPoint *top;
|
||||
const TTPoint *bottom;
|
||||
int32_t direction;
|
||||
if (a->y == b->y) continue;
|
||||
direction = -1;
|
||||
top = b;
|
||||
bottom = a;
|
||||
if ((!invert && a->y < b->y) || (invert && b->y < a->y)) {
|
||||
direction = 1;
|
||||
top = a;
|
||||
bottom = b;
|
||||
}
|
||||
edges[edge_count].x0 =
|
||||
(int32_t)(scale_x * top->x * 1024.0f);
|
||||
edges[edge_count].y0 = (int32_t)(scale_y * top->y *
|
||||
(float)vsubsample * 1024.0f);
|
||||
edges[edge_count].x1 =
|
||||
(int32_t)(scale_x * bottom->x * 1024.0f);
|
||||
edges[edge_count].y1 = (int32_t)(scale_y * bottom->y *
|
||||
(float)vsubsample * 1024.0f);
|
||||
edges[edge_count].direction = direction;
|
||||
++edge_count;
|
||||
}
|
||||
point_base += count;
|
||||
}
|
||||
tt_sort_edges(edges, edge_count);
|
||||
tt_rasterize_sorted_edges(bitmap, edges, edge_count, vsubsample, x_off,
|
||||
y_off);
|
||||
tt_free(userdata, edges);
|
||||
}
|
||||
|
||||
void iggy_tt_Rasterize(IggyTTBitmap *bitmap, float flatness,
|
||||
const float *vertices_data, int32_t num_vertices,
|
||||
float scale_x, float scale_y, int32_t x_off,
|
||||
int32_t y_off, int32_t invert, void *userdata) {
|
||||
const IggyTTVertex *vertices = (const IggyTTVertex *)vertices_data;
|
||||
int32_t *contour_lengths = NULL;
|
||||
int32_t contours = 0;
|
||||
TTPoint *points;
|
||||
const float scale = scale_x < scale_y ? scale_x : scale_y;
|
||||
if (flatness < 0.125f) flatness = 0.125f;
|
||||
points = (TTPoint *)iggy_tt_FlattenCurves(
|
||||
vertices, num_vertices, flatness / scale, &contour_lengths, &contours,
|
||||
userdata);
|
||||
if (points != NULL) {
|
||||
tt_rasterize(bitmap, points, contour_lengths, contours, scale_x, scale_y,
|
||||
x_off, y_off, invert, userdata);
|
||||
tt_free(userdata, contour_lengths);
|
||||
tt_free(userdata, points);
|
||||
}
|
||||
}
|
||||
263
iggy-decompiled/src/reconstructed/stb_truetype_wrapper_shape.inc
Normal file
263
iggy-decompiled/src/reconstructed/stb_truetype_wrapper_shape.inc
Normal file
@@ -0,0 +1,263 @@
|
||||
static int32_t tt_glyf_offset(const TTFontInfo *info, int32_t glyph) {
|
||||
int32_t start;
|
||||
int32_t end;
|
||||
if (glyph >= (int32_t)info->numGlyphs) return -1;
|
||||
if (info->indexToLocFormat == 0) {
|
||||
start = tt_u16(TT_DATA(info) + info->loca + glyph * 2) * 2;
|
||||
end = tt_u16(TT_DATA(info) + info->loca + glyph * 2 + 2) * 2;
|
||||
} else if (info->indexToLocFormat == 1) {
|
||||
start = (int32_t)tt_u32(TT_DATA(info) + info->loca + glyph * 4);
|
||||
end = (int32_t)tt_u32(TT_DATA(info) + info->loca + glyph * 4 + 4);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
return start == end ? -1 : info->glyf + start;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_GetGlyphBox(void *font, int32_t glyph, int32_t *x0,
|
||||
int32_t *y0, int32_t *x1, int32_t *y1) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const int32_t offset = tt_glyf_offset(info, glyph);
|
||||
if (offset < 0) return 0;
|
||||
if (x0 != NULL) *x0 = tt_s16(TT_DATA(info) + offset + 2);
|
||||
if (y0 != NULL) *y0 = tt_s16(TT_DATA(info) + offset + 4);
|
||||
if (x1 != NULL) *x1 = tt_s16(TT_DATA(info) + offset + 6);
|
||||
if (y1 != NULL) *y1 = tt_s16(TT_DATA(info) + offset + 8);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_GetCodepointBox(void *font, int32_t codepoint, int32_t *x0,
|
||||
int32_t *y0, int32_t *x1, int32_t *y1) {
|
||||
return iggy_tt_GetGlyphBox(font, iggy_tt_FindGlyphIndex(font, codepoint),
|
||||
x0, y0, x1, y1);
|
||||
}
|
||||
|
||||
int32_t iggy_tt_IsGlyphEmpty(void *font, int32_t glyph) {
|
||||
const TTFontInfo *info = (const TTFontInfo *)font;
|
||||
const int32_t offset = tt_glyf_offset(info, glyph);
|
||||
return offset < 0 || tt_s16(TT_DATA(info) + offset) == 0;
|
||||
}
|
||||
|
||||
static void tt_set_vertex(IggyTTVertex *vertex, uint8_t type, float x, float y,
|
||||
float cx, float cy) {
|
||||
vertex->x = x;
|
||||
vertex->y = y;
|
||||
vertex->cx = cx;
|
||||
vertex->cy = cy;
|
||||
vertex->type = type;
|
||||
}
|
||||
|
||||
static int32_t tt_simple_shape(const TTFontInfo *info, int32_t offset,
|
||||
int32_t contours, IggyTTVertex **result) {
|
||||
const uint8_t *data = TT_DATA(info);
|
||||
const uint8_t *end_points = data + offset + 10;
|
||||
const int32_t points = tt_u16(end_points + (contours - 1) * 2) + 1;
|
||||
const uint8_t *p = end_points + contours * 2;
|
||||
IggyTTVertex *vertices;
|
||||
IggyTTVertex *decoded;
|
||||
int32_t flag_count = 0;
|
||||
uint8_t flag = 0;
|
||||
int32_t i;
|
||||
int16_t x = 0;
|
||||
int16_t y = 0;
|
||||
int32_t output_count = 0;
|
||||
int32_t next_move = 0;
|
||||
int32_t contour = 0;
|
||||
int32_t was_off = 0;
|
||||
int16_t sx = 0;
|
||||
int16_t sy = 0;
|
||||
int16_t cx = 0;
|
||||
int16_t cy = 0;
|
||||
|
||||
p += 2 + tt_u16(p);
|
||||
vertices = (IggyTTVertex *)tt_alloc(TT_USERDATA(info),
|
||||
(points + contours) *
|
||||
(int32_t)sizeof(*vertices));
|
||||
if (vertices == NULL) return 0;
|
||||
decoded = vertices + contours;
|
||||
|
||||
for (i = 0; i < points; ++i) {
|
||||
if (flag_count == 0) {
|
||||
flag = *p++;
|
||||
if ((flag & 8) != 0) flag_count = *p++;
|
||||
} else {
|
||||
--flag_count;
|
||||
}
|
||||
decoded[i].type = flag;
|
||||
}
|
||||
for (i = 0; i < points; ++i) {
|
||||
flag = decoded[i].type;
|
||||
if ((flag & 2) != 0) {
|
||||
x = (int16_t)(x + ((flag & 16) != 0 ? *p : -(int32_t)*p));
|
||||
++p;
|
||||
} else if ((flag & 16) == 0) {
|
||||
x = (int16_t)(x + tt_s16(p));
|
||||
p += 2;
|
||||
}
|
||||
decoded[i].x = (float)x;
|
||||
}
|
||||
for (i = 0; i < points; ++i) {
|
||||
flag = decoded[i].type;
|
||||
if ((flag & 4) != 0) {
|
||||
y = (int16_t)(y + ((flag & 32) != 0 ? *p : -(int32_t)*p));
|
||||
++p;
|
||||
} else if ((flag & 32) == 0) {
|
||||
y = (int16_t)(y + tt_s16(p));
|
||||
p += 2;
|
||||
}
|
||||
decoded[i].y = (float)y;
|
||||
}
|
||||
|
||||
for (i = 0; i < points; ++i) {
|
||||
const int16_t px = (int16_t)(int32_t)decoded[i].x;
|
||||
const int16_t py = (int16_t)(int32_t)decoded[i].y;
|
||||
if (i == next_move) {
|
||||
if (i != 0) {
|
||||
tt_set_vertex(vertices + output_count++,
|
||||
was_off ? TT_vcurve : TT_vline, (float)sx, (float)sy,
|
||||
was_off ? (float)cx : 0.0f,
|
||||
was_off ? (float)cy : 0.0f);
|
||||
}
|
||||
tt_set_vertex(vertices + output_count++, TT_vmove, (float)px,
|
||||
(float)py, 0.0f, 0.0f);
|
||||
was_off = 0;
|
||||
next_move = tt_u16(end_points + contour * 2) + 1;
|
||||
++contour;
|
||||
sx = px;
|
||||
sy = py;
|
||||
} else if ((decoded[i].type & 1) == 0) {
|
||||
if (was_off) {
|
||||
tt_set_vertex(vertices + output_count++, TT_vcurve,
|
||||
(float)(int16_t)(((int32_t)cx + px) >> 1),
|
||||
(float)(int16_t)(((int32_t)cy + py) >> 1), (float)cx,
|
||||
(float)cy);
|
||||
}
|
||||
cx = px;
|
||||
cy = py;
|
||||
was_off = 1;
|
||||
} else {
|
||||
if (was_off) {
|
||||
tt_set_vertex(vertices + output_count++, TT_vcurve, (float)px,
|
||||
(float)py, (float)cx, (float)cy);
|
||||
} else {
|
||||
tt_set_vertex(vertices + output_count++, TT_vline, (float)px,
|
||||
(float)py, 0.0f, 0.0f);
|
||||
}
|
||||
was_off = 0;
|
||||
}
|
||||
}
|
||||
if (points != 0) {
|
||||
tt_set_vertex(vertices + output_count++, was_off ? TT_vcurve : TT_vline,
|
||||
(float)sx, (float)sy, was_off ? (float)cx : 0.0f,
|
||||
was_off ? (float)cy : 0.0f);
|
||||
}
|
||||
*result = vertices;
|
||||
return output_count;
|
||||
}
|
||||
|
||||
static int32_t tt_compound_shape(TTFontInfo *info, int32_t offset,
|
||||
IggyTTVertex **result) {
|
||||
const uint8_t *p = TT_DATA(info) + offset + 10;
|
||||
IggyTTVertex *vertices = NULL;
|
||||
int32_t count = 0;
|
||||
uint16_t flags;
|
||||
do {
|
||||
const int32_t glyph = tt_u16(p + 2);
|
||||
float mtx0 = 1.0f;
|
||||
float mtx1 = 0.0f;
|
||||
float mtx2 = 0.0f;
|
||||
float mtx3 = 1.0f;
|
||||
float dx = 0.0f;
|
||||
float dy = 0.0f;
|
||||
float m;
|
||||
float n;
|
||||
IggyTTVertex *component = NULL;
|
||||
IggyTTVertex *joined;
|
||||
int32_t component_count;
|
||||
int32_t i;
|
||||
flags = tt_u16(p);
|
||||
p += 4;
|
||||
if ((flags & 1) != 0) {
|
||||
if ((flags & 2) != 0) {
|
||||
dx = (float)tt_s16(p);
|
||||
dy = (float)tt_s16(p + 2);
|
||||
p += 4;
|
||||
}
|
||||
} else {
|
||||
if ((flags & 2) != 0) {
|
||||
dx = (float)(int8_t)p[0];
|
||||
dy = (float)(int8_t)p[1];
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
if ((flags & 8) != 0) {
|
||||
mtx0 = mtx3 = (float)tt_s16(p) / 16384.0f;
|
||||
p += 2;
|
||||
} else if ((flags & 0x40) != 0) {
|
||||
mtx0 = (float)tt_s16(p) / 16384.0f;
|
||||
mtx3 = (float)tt_s16(p + 2) / 16384.0f;
|
||||
p += 4;
|
||||
} else if ((flags & 0x80) != 0) {
|
||||
mtx0 = (float)tt_s16(p) / 16384.0f;
|
||||
mtx1 = (float)tt_s16(p + 2) / 16384.0f;
|
||||
mtx2 = (float)tt_s16(p + 4) / 16384.0f;
|
||||
mtx3 = (float)tt_s16(p + 6) / 16384.0f;
|
||||
p += 8;
|
||||
}
|
||||
component_count = iggy_tt_GetGlyphShape(info, glyph, &component);
|
||||
if (component_count == 0 || component == NULL) continue;
|
||||
m = tt_sqrt(mtx0 * mtx0 + mtx1 * mtx1);
|
||||
n = tt_sqrt(mtx2 * mtx2 + mtx3 * mtx3);
|
||||
for (i = 0; i < component_count; ++i) {
|
||||
float x = component[i].x;
|
||||
float y = component[i].y;
|
||||
component[i].x = m * (mtx0 * x + mtx2 * y + dx);
|
||||
component[i].y = n * (mtx1 * x + mtx3 * y + dy);
|
||||
x = component[i].cx;
|
||||
y = component[i].cy;
|
||||
component[i].cx = m * (mtx0 * x + mtx2 * y + dx);
|
||||
component[i].cy = n * (mtx1 * x + mtx3 * y + dy);
|
||||
}
|
||||
joined = (IggyTTVertex *)tt_alloc(
|
||||
TT_USERDATA(info),
|
||||
(count + component_count) * (int32_t)sizeof(*joined));
|
||||
if (joined == NULL) {
|
||||
if (vertices != NULL) tt_free(TT_USERDATA(info), vertices);
|
||||
if (component == NULL) return 0;
|
||||
tt_free(TT_USERDATA(info), component);
|
||||
return 0;
|
||||
}
|
||||
if (count != 0) memcpy(joined, vertices, (size_t)count * sizeof(*joined));
|
||||
memcpy(joined + count, component,
|
||||
(size_t)component_count * sizeof(*joined));
|
||||
if (vertices != NULL) tt_free(TT_USERDATA(info), vertices);
|
||||
tt_free(TT_USERDATA(info), component);
|
||||
vertices = joined;
|
||||
count += component_count;
|
||||
} while ((flags & 0x20) != 0);
|
||||
*result = vertices;
|
||||
return count;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_GetGlyphShape(void *font, int32_t glyph, void *vertices_out) {
|
||||
TTFontInfo *info = (TTFontInfo *)font;
|
||||
IggyTTVertex **result = (IggyTTVertex **)vertices_out;
|
||||
const int32_t offset = tt_glyf_offset(info, glyph);
|
||||
int32_t contours;
|
||||
*result = NULL;
|
||||
if (offset < 0) return 0;
|
||||
contours = tt_s16(TT_DATA(info) + offset);
|
||||
if (contours > 0) return tt_simple_shape(info, offset, contours, result);
|
||||
if (contours == -1) return tt_compound_shape(info, offset, result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t iggy_tt_GetCodepointShape(void *font, int32_t codepoint,
|
||||
void *vertices_out) {
|
||||
return iggy_tt_GetGlyphShape(font, iggy_tt_FindGlyphIndex(font, codepoint),
|
||||
vertices_out);
|
||||
}
|
||||
|
||||
void iggy_tt_FreeShape(void *font, void *vertices) {
|
||||
iggy_free_alloc((intptr_t)TT_USERDATA((TTFontInfo *)font), vertices);
|
||||
}
|
||||
129
tests/support/stb_truetype_wrapper_test_support.c
Normal file
129
tests/support/stb_truetype_wrapper_test_support.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Host harness for the reconstructed stb_truetype_wrapper translation unit.
|
||||
*
|
||||
* stb_truetype_wrapper.c is RAD's build of stb_truetype with Iggy allocation
|
||||
* (GSwfMallocTruetype / iggy_free_alloc). The 32 iggy_tt_* exports are owned
|
||||
* by the module itself once it exists and are NOT defined here.
|
||||
*
|
||||
* Only the two genuine external imports that are not host-libc are stubbed:
|
||||
* - GSwfMallocTruetype(intptr_t player, int size) -> returns pointer value
|
||||
* - iggy_free_alloc(intptr_t player, void *block)
|
||||
* Signatures match iggy-decompiled/src/reconstructed/iggy_allocation.h so a
|
||||
* later link against the real allocation object remains ABI-compatible.
|
||||
*
|
||||
* memcpy / memset / ceil / ceilf / floor / floorf / sqrt / strlen are
|
||||
* deliberately NOT stubbed -- they resolve against the host C library,
|
||||
* matching the iggy_font_provider harness convention. The host dylib is a
|
||||
* plain macOS build, not a cross-compile, so whichever subset the eventual
|
||||
* reconstructed source calls simply binds to host libc.
|
||||
*
|
||||
* Conventions mirror tests/support/iggy_font_provider_test_support.c:
|
||||
* - counters / last-args give runtime tests an observable channel,
|
||||
* - a bump arena backs the malloc stub (no real free).
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
enum { STB_TT_TEST_MAX = 64 };
|
||||
|
||||
static size_t gswf_malloc_count;
|
||||
static size_t free_alloc_count;
|
||||
static int fail_next_malloc;
|
||||
static intptr_t last_malloc_player;
|
||||
static int last_malloc_size;
|
||||
static void *last_malloc_ptr;
|
||||
static intptr_t last_free_player;
|
||||
static void *last_free_ptr;
|
||||
|
||||
static uint8_t malloc_pool[1 << 18];
|
||||
static size_t malloc_pool_used;
|
||||
static void *malloc_blocks[STB_TT_TEST_MAX];
|
||||
static size_t malloc_block_count;
|
||||
|
||||
static void *pool_alloc(size_t size) {
|
||||
size_t aligned = (size + 15u) & ~(size_t)15u;
|
||||
if (malloc_block_count == STB_TT_TEST_MAX ||
|
||||
malloc_pool_used + aligned > sizeof(malloc_pool)) {
|
||||
return 0;
|
||||
}
|
||||
void *result = malloc_pool + malloc_pool_used;
|
||||
malloc_pool_used += aligned;
|
||||
malloc_blocks[malloc_block_count++] = result;
|
||||
memset(result, 0, size);
|
||||
return result;
|
||||
}
|
||||
|
||||
void stb_truetype_wrapper_test_reset(void) {
|
||||
gswf_malloc_count = 0;
|
||||
free_alloc_count = 0;
|
||||
fail_next_malloc = 0;
|
||||
last_malloc_player = 0;
|
||||
last_malloc_size = 0;
|
||||
last_malloc_ptr = 0;
|
||||
last_free_player = 0;
|
||||
last_free_ptr = 0;
|
||||
malloc_pool_used = 0;
|
||||
malloc_block_count = 0;
|
||||
memset(malloc_blocks, 0, sizeof(malloc_blocks));
|
||||
}
|
||||
|
||||
void stb_truetype_wrapper_test_fail_next_malloc(void) {
|
||||
fail_next_malloc = 1;
|
||||
}
|
||||
|
||||
size_t stb_truetype_wrapper_test_gswf_malloc_count(void) {
|
||||
return gswf_malloc_count;
|
||||
}
|
||||
|
||||
size_t stb_truetype_wrapper_test_free_alloc_count(void) {
|
||||
return free_alloc_count;
|
||||
}
|
||||
|
||||
intptr_t stb_truetype_wrapper_test_last_malloc_player(void) {
|
||||
return last_malloc_player;
|
||||
}
|
||||
|
||||
int stb_truetype_wrapper_test_last_malloc_size(void) {
|
||||
return last_malloc_size;
|
||||
}
|
||||
|
||||
void *stb_truetype_wrapper_test_last_malloc_ptr(void) {
|
||||
return last_malloc_ptr;
|
||||
}
|
||||
|
||||
intptr_t stb_truetype_wrapper_test_last_free_player(void) {
|
||||
return last_free_player;
|
||||
}
|
||||
|
||||
void *stb_truetype_wrapper_test_last_free_ptr(void) {
|
||||
return last_free_ptr;
|
||||
}
|
||||
|
||||
/* Exact ABI from iggy_allocation.h. */
|
||||
int64_t GSwfMallocTruetype(intptr_t player, int size) {
|
||||
++gswf_malloc_count;
|
||||
last_malloc_player = player;
|
||||
last_malloc_size = size;
|
||||
if (fail_next_malloc) {
|
||||
fail_next_malloc = 0;
|
||||
last_malloc_ptr = 0;
|
||||
return 0;
|
||||
}
|
||||
if (size <= 0) {
|
||||
last_malloc_ptr = 0;
|
||||
return 0;
|
||||
}
|
||||
last_malloc_ptr = pool_alloc((size_t)size);
|
||||
return (int64_t)(intptr_t)last_malloc_ptr;
|
||||
}
|
||||
|
||||
void iggy_free_alloc(intptr_t player, void *block) {
|
||||
++free_alloc_count;
|
||||
last_free_player = player;
|
||||
last_free_ptr = block;
|
||||
/* Bump-arena: no real free. */
|
||||
(void)player;
|
||||
(void)block;
|
||||
}
|
||||
485
tests/test_reconstructed_stb_truetype_wrapper.py
Normal file
485
tests/test_reconstructed_stb_truetype_wrapper.py
Normal file
@@ -0,0 +1,485 @@
|
||||
"""Runtime tests for the reconstructed stb_truetype_wrapper translation unit.
|
||||
|
||||
RED STATE: stb_truetype_wrapper.c / stb_truetype_wrapper.h do not exist yet, so
|
||||
setUpClass raises "missing stb_truetype_wrapper runtime inputs". Every
|
||||
assertion below is grounded in a specific line of
|
||||
iggy-decompiled/raw/durango/stb_truetype_wrapper.c (cited inline) so a faithful
|
||||
DURANGO reconstruction turns these green.
|
||||
|
||||
This module is RAD's build of stb_truetype (public upstream algorithm) wrapped
|
||||
with Iggy allocation (GSwfMallocTruetype / iggy_free_alloc). The 13 exports
|
||||
already consumed by reconstructed iggy_font_provider.c and swf_fontcache.c lock
|
||||
the call-site ABIs; those sibling declarations win over bare Ghidra prototypes
|
||||
where the decompiler dropped a second argument (IsGlyphEmpty / FreeShape).
|
||||
|
||||
The dylib is built for the 64-bit host with
|
||||
IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO selected, matching the sibling
|
||||
iggy_font_provider / iggy_allocation runtime-test pattern.
|
||||
"""
|
||||
|
||||
import ctypes
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
||||
SOURCE = RECONSTRUCTED / "stb_truetype_wrapper.c"
|
||||
HEADER = RECONSTRUCTED / "stb_truetype_wrapper.h"
|
||||
SUPPORT = ROOT / "tests/support/stb_truetype_wrapper_test_support.c"
|
||||
|
||||
# Font-info buffer size covers InitFont writes through +0x34 (raw/durango:1749-
|
||||
# 1801) with headroom for any additional cached fields.
|
||||
FONT_INFO_BYTES = 0x80
|
||||
UNITS_PER_EM = 1024
|
||||
GLYPH_ADVANCE = 100
|
||||
GLYPH_LSB = 5
|
||||
GLYPH_XMIN, GLYPH_YMIN, GLYPH_XMAX, GLYPH_YMAX = 0, 0, 100, 100
|
||||
FONT_XMIN, FONT_YMIN, FONT_XMAX, FONT_YMAX = 0, -20, 100, 80
|
||||
# Player / userdata tag threaded via SetUserData into *font (raw/durango:1985-
|
||||
# 1988) and read as the first GSwfMallocTruetype argument (raw/durango:1317).
|
||||
USER_TAG = 0xC0FFEE42
|
||||
|
||||
|
||||
def _be16(value):
|
||||
return struct.pack(">H", value & 0xFFFF)
|
||||
|
||||
|
||||
def _be_i16(value):
|
||||
return struct.pack(">h", value)
|
||||
|
||||
|
||||
def _be32(value):
|
||||
return struct.pack(">I", value & 0xFFFFFFFF)
|
||||
|
||||
|
||||
def _align4(data):
|
||||
pad = (4 - (len(data) % 4)) % 4
|
||||
return data + b"\x00" * pad
|
||||
|
||||
|
||||
def _build_simple_glyph():
|
||||
"""One-contour closed box: (0,0)-(100,0)-(100,100)-(0,100).
|
||||
|
||||
Layout matches the simple-glyf records stbtt__GetGlyphShape walks
|
||||
(raw/durango GetGlyphShape ~1035+).
|
||||
"""
|
||||
data = _be_i16(1) # numberOfContours
|
||||
data += _be_i16(GLYPH_XMIN) + _be_i16(GLYPH_YMIN)
|
||||
data += _be_i16(GLYPH_XMAX) + _be_i16(GLYPH_YMAX)
|
||||
data += _be16(3) # endPtsOfContours[0] = last point index
|
||||
data += _be16(0) # instructionLength
|
||||
# Four on-curve points; absolute int16 deltas (flag = ON_CURVE only).
|
||||
for _ in range(4):
|
||||
data += bytes([0x01])
|
||||
data += _be_i16(0) + _be_i16(0) # -> (0, 0)
|
||||
data += _be_i16(100) + _be_i16(0) # -> (100, 0)
|
||||
data += _be_i16(0) + _be_i16(100) # -> (100, 100)
|
||||
data += _be_i16(-100) + _be_i16(0) # -> (0, 100)
|
||||
if len(data) % 2:
|
||||
data += b"\x00"
|
||||
return data
|
||||
|
||||
|
||||
def build_minimal_ttf(*, include_cmap=True, map_a_to_glyph=1):
|
||||
"""Craft a minimal, InitFont-parseable sfnt blob.
|
||||
|
||||
InitFont (raw/durango:1731-1805) requires non-zero cmap/loca/head/glyf/
|
||||
hhea/hmtx table offsets via stbtt__find_table, then a platform-3 encoding
|
||||
1-or-10 cmap subtable. Checksums are ignored (lookup is tag/offset only).
|
||||
"""
|
||||
maxp = _be32(0x00010000) + _be16(2) # version 1.0, numGlyphs=2
|
||||
|
||||
head = bytearray(54)
|
||||
head[12:16] = _be32(0x5F0F3CF5) # magicNumber
|
||||
head[18:20] = _be16(UNITS_PER_EM) # unitsPerEm @ +0x12
|
||||
head[36:38] = _be_i16(FONT_XMIN)
|
||||
head[38:40] = _be_i16(FONT_YMIN)
|
||||
head[40:42] = _be_i16(FONT_XMAX)
|
||||
head[42:44] = _be_i16(FONT_YMAX)
|
||||
head[50:52] = _be16(0) # indexToLocFormat=0 (short) @ +0x32
|
||||
head = bytes(head)
|
||||
|
||||
hhea = bytearray(36)
|
||||
hhea[4:6] = _be_i16(80) # ascent
|
||||
hhea[6:8] = _be_i16(-20) # descent
|
||||
hhea[8:10] = _be_i16(0) # lineGap
|
||||
hhea[34:36] = _be16(2) # numberOfHMetrics @ +0x22
|
||||
hhea = bytes(hhea)
|
||||
|
||||
# glyph 0: advance 0 / lsb 0; glyph 1: advance/lsb under test.
|
||||
hmtx = (_be16(0) + _be_i16(0)
|
||||
+ _be16(GLYPH_ADVANCE) + _be_i16(GLYPH_LSB))
|
||||
|
||||
g1 = _build_simple_glyph()
|
||||
# short loca: glyph0 empty (loca[0]==loca[1]), glyph1 is g1.
|
||||
loca = _be16(0) + _be16(0) + _be16(len(g1) // 2)
|
||||
glyf = g1
|
||||
|
||||
if include_cmap:
|
||||
# Format-0 subtable (raw/durango FindGlyphIndex format-0 path:183-188).
|
||||
glyph_ids = bytearray(256)
|
||||
if 0 <= map_a_to_glyph <= 255:
|
||||
glyph_ids[0x41] = map_a_to_glyph # 'A'
|
||||
subtable = _be16(0) + _be16(6 + 256) + _be16(0) + bytes(glyph_ids)
|
||||
cmap = _be16(0) + _be16(1) # version, numEncodingRecords
|
||||
cmap += _be16(3) + _be16(1) + _be32(12) # MS Unicode BMP
|
||||
cmap += subtable
|
||||
else:
|
||||
cmap = None
|
||||
|
||||
tables = {
|
||||
b"glyf": glyf,
|
||||
b"head": head,
|
||||
b"hhea": hhea,
|
||||
b"hmtx": hmtx,
|
||||
b"loca": loca,
|
||||
b"maxp": maxp,
|
||||
}
|
||||
if cmap is not None:
|
||||
tables[b"cmap"] = cmap
|
||||
|
||||
# sfnt offset table + directory (tags sorted alphabetically).
|
||||
ordered = sorted(tables.items(), key=lambda item: item[0])
|
||||
num_tables = len(ordered)
|
||||
# searchRange / entrySelector / rangeShift: valid-looking, not parsed by
|
||||
# InitFont beyond the directory walk in stbtt__find_table.
|
||||
search_range = 1
|
||||
entry_selector = 0
|
||||
while (search_range << 1) <= num_tables:
|
||||
search_range <<= 1
|
||||
entry_selector += 1
|
||||
search_range *= 16
|
||||
range_shift = num_tables * 16 - search_range
|
||||
|
||||
offset_table = (
|
||||
_be32(0x00010000)
|
||||
+ _be16(num_tables)
|
||||
+ _be16(search_range)
|
||||
+ _be16(entry_selector)
|
||||
+ _be16(range_shift)
|
||||
)
|
||||
dir_size = num_tables * 16
|
||||
cursor = len(offset_table) + dir_size
|
||||
directory = b""
|
||||
payloads = b""
|
||||
for tag, body in ordered:
|
||||
body = _align4(body)
|
||||
directory += tag + _be32(0) + _be32(cursor) + _be32(len(body))
|
||||
payloads += body
|
||||
cursor += len(body)
|
||||
return offset_table + directory + payloads
|
||||
|
||||
|
||||
class StbttBitmap(ctypes.Structure):
|
||||
"""stbtt__bitmap layout on 64-bit host (w/h/stride + pointer)."""
|
||||
_fields_ = [
|
||||
("w", ctypes.c_int32),
|
||||
("h", ctypes.c_int32),
|
||||
("stride", ctypes.c_int32),
|
||||
("_pad", ctypes.c_int32),
|
||||
("pixels", ctypes.c_void_p),
|
||||
]
|
||||
|
||||
|
||||
class ReconstructedStbTruetypeWrapperTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
missing = [str(path) for path in (SOURCE, HEADER, SUPPORT)
|
||||
if not path.exists()]
|
||||
if missing:
|
||||
raise AssertionError(
|
||||
"missing stb_truetype_wrapper runtime inputs: "
|
||||
+ ", ".join(missing))
|
||||
cls.temp = tempfile.TemporaryDirectory()
|
||||
output = Path(cls.temp.name) / "stb_truetype_wrapper.dylib"
|
||||
build = subprocess.run([
|
||||
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
|
||||
"-Werror", "-Wl,-undefined,dynamic_lookup",
|
||||
"-DIGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_DURANGO=1",
|
||||
"-I", str(RECONSTRUCTED),
|
||||
str(SOURCE), str(SUPPORT), "-o", str(output),
|
||||
], capture_output=True, text=True)
|
||||
if build.returncode:
|
||||
raise AssertionError(build.stdout + build.stderr)
|
||||
cls.lib = ctypes.CDLL(str(output))
|
||||
lib = cls.lib
|
||||
|
||||
# Harness observability.
|
||||
lib.stb_truetype_wrapper_test_reset.argtypes = []
|
||||
lib.stb_truetype_wrapper_test_fail_next_malloc.argtypes = []
|
||||
lib.stb_truetype_wrapper_test_gswf_malloc_count.restype = ctypes.c_size_t
|
||||
lib.stb_truetype_wrapper_test_free_alloc_count.restype = ctypes.c_size_t
|
||||
lib.stb_truetype_wrapper_test_last_malloc_player.restype = ctypes.c_void_p
|
||||
lib.stb_truetype_wrapper_test_last_malloc_size.restype = ctypes.c_int
|
||||
lib.stb_truetype_wrapper_test_last_malloc_ptr.restype = ctypes.c_void_p
|
||||
lib.stb_truetype_wrapper_test_last_free_player.restype = ctypes.c_void_p
|
||||
lib.stb_truetype_wrapper_test_last_free_ptr.restype = ctypes.c_void_p
|
||||
|
||||
# ABIs locked by iggy_font_provider.c ~41-54 + swf_fontcache.c ~81.
|
||||
# FreeShape / IsGlyphEmpty are 2-arg despite 1-arg raw prototypes
|
||||
# (raw/durango:459 / 1812 -- Ghidra artifact; trust sibling call sites).
|
||||
lib.iggy_tt_InitFont.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int32]
|
||||
lib.iggy_tt_InitFont.restype = ctypes.c_int32
|
||||
lib.iggy_tt_FindGlyphIndex.argtypes = [ctypes.c_void_p, ctypes.c_int32]
|
||||
lib.iggy_tt_FindGlyphIndex.restype = ctypes.c_int32
|
||||
lib.iggy_tt_SetUserData.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
|
||||
lib.iggy_tt_ScaleForMappingEmToPixels.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_float]
|
||||
lib.iggy_tt_ScaleForMappingEmToPixels.restype = ctypes.c_float
|
||||
lib.iggy_tt_GetCodepointHMetrics.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_int32,
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32)]
|
||||
lib.iggy_tt_GetGlyphHMetrics.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_int32,
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32)]
|
||||
lib.iggy_tt_GetGlyphBox.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_int32,
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32),
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32)]
|
||||
lib.iggy_tt_GetGlyphBox.restype = ctypes.c_int32
|
||||
lib.iggy_tt_GetFontBoundingBox.argtypes = [
|
||||
ctypes.c_void_p,
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32),
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32)]
|
||||
lib.iggy_tt_IsGlyphEmpty.argtypes = [ctypes.c_void_p, ctypes.c_int32]
|
||||
lib.iggy_tt_IsGlyphEmpty.restype = ctypes.c_int32
|
||||
lib.iggy_tt_GetGlyphShape.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_int32, ctypes.c_void_p]
|
||||
lib.iggy_tt_GetGlyphShape.restype = ctypes.c_int32
|
||||
lib.iggy_tt_FreeShape.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
|
||||
lib.iggy_tt_Rasterize.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_float, ctypes.c_void_p, ctypes.c_int32,
|
||||
ctypes.c_float, ctypes.c_float, ctypes.c_int32, ctypes.c_int32,
|
||||
ctypes.c_int32, ctypes.c_void_p]
|
||||
lib.iggy_tt_GetFontOffsetForIndex.argtypes = [
|
||||
ctypes.c_void_p, ctypes.c_int32]
|
||||
lib.iggy_tt_GetFontOffsetForIndex.restype = ctypes.c_int32
|
||||
lib.iggy_tt_GetFontVMetrics.argtypes = [
|
||||
ctypes.c_void_p,
|
||||
ctypes.POINTER(ctypes.c_int32), ctypes.POINTER(ctypes.c_int32),
|
||||
ctypes.POINTER(ctypes.c_int32)]
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.temp.cleanup()
|
||||
|
||||
def setUp(self):
|
||||
self.lib.stb_truetype_wrapper_test_reset()
|
||||
self._font_info = (ctypes.c_uint8 * FONT_INFO_BYTES)()
|
||||
self.font = ctypes.addressof(self._font_info)
|
||||
self._ttf = None
|
||||
self._ttf_addr = 0
|
||||
|
||||
def load_font(self, ttf_bytes, offset=0):
|
||||
self._ttf = (ctypes.c_uint8 * len(ttf_bytes)).from_buffer_copy(ttf_bytes)
|
||||
self._ttf_addr = ctypes.addressof(self._ttf)
|
||||
return self.lib.iggy_tt_InitFont(self.font, self._ttf_addr, offset)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# InitFont / table directory
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_init_font_succeeds_on_minimal_valid_ttf(self):
|
||||
# raw/durango:1764-1802 all six required tables present + MS Unicode
|
||||
# cmap subtable selected -> return 1; indexToLocFormat cached @ +0x34.
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
# data pointer stored at font+8 (raw/durango:1750).
|
||||
stored = ctypes.c_void_p.from_address(self.font + 8).value
|
||||
self.assertEqual(stored, self._ttf_addr)
|
||||
|
||||
def test_init_font_fails_when_cmap_missing(self):
|
||||
# raw/durango:1764-1765 if any of cmap/loca/head/glyf/hhea/hmtx is 0
|
||||
# the function returns 0.
|
||||
ttf = build_minimal_ttf(include_cmap=False)
|
||||
self.assertEqual(self.load_font(ttf), 0)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# cmap / metrics / scale
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_find_glyph_index_format0_mapped_and_unmapped(self):
|
||||
# raw/durango:183-188 format-0: glyphIds[codepoint] when in range,
|
||||
# else 0. 'A' (0x41) maps to glyph 1; 'B' is unmapped -> 0.
|
||||
ttf = build_minimal_ttf(map_a_to_glyph=1)
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
self.assertEqual(self.lib.iggy_tt_FindGlyphIndex(self.font, 0x41), 1)
|
||||
self.assertEqual(self.lib.iggy_tt_FindGlyphIndex(self.font, 0x42), 0)
|
||||
|
||||
def test_scale_for_mapping_em_to_pixels_divides_by_units_per_em(self):
|
||||
# raw/durango:1942-1955 returns pixels / unitsPerEm, reading
|
||||
# unitsPerEm live from head+0x12 each call (not a cached field).
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
scale = self.lib.iggy_tt_ScaleForMappingEmToPixels(self.font, 32.0)
|
||||
self.assertAlmostEqual(scale, 32.0 / UNITS_PER_EM, places=6)
|
||||
|
||||
def test_glyph_and_codepoint_hmetrics(self):
|
||||
# raw/durango:975+ GetGlyphHMetrics reads hmtx; GetCodepointHMetrics
|
||||
# (raw/durango:570-578) is FindGlyphIndex + GetGlyphHMetrics.
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
advance = ctypes.c_int32()
|
||||
lsb = ctypes.c_int32()
|
||||
self.lib.iggy_tt_GetGlyphHMetrics(
|
||||
self.font, 1, ctypes.byref(advance), ctypes.byref(lsb))
|
||||
self.assertEqual(advance.value, GLYPH_ADVANCE)
|
||||
self.assertEqual(lsb.value, GLYPH_LSB)
|
||||
|
||||
advance.value = -1
|
||||
lsb.value = -1
|
||||
self.lib.iggy_tt_GetCodepointHMetrics(
|
||||
self.font, 0x41, ctypes.byref(advance), ctypes.byref(lsb))
|
||||
self.assertEqual(advance.value, GLYPH_ADVANCE)
|
||||
self.assertEqual(lsb.value, GLYPH_LSB)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Bounding boxes / empty glyph
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_get_glyph_box_and_null_output_tolerance(self):
|
||||
# raw/durango:940-968 success returns 1 and fills non-null outputs;
|
||||
# each of x0/y0/x1/y1 is gated on != NULL so null outs are tolerated.
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
x0 = ctypes.c_int32()
|
||||
y0 = ctypes.c_int32()
|
||||
x1 = ctypes.c_int32()
|
||||
y1 = ctypes.c_int32()
|
||||
self.assertEqual(
|
||||
self.lib.iggy_tt_GetGlyphBox(
|
||||
self.font, 1,
|
||||
ctypes.byref(x0), ctypes.byref(y0),
|
||||
ctypes.byref(x1), ctypes.byref(y1)),
|
||||
1)
|
||||
self.assertEqual(
|
||||
(x0.value, y0.value, x1.value, y1.value),
|
||||
(GLYPH_XMIN, GLYPH_YMIN, GLYPH_XMAX, GLYPH_YMAX))
|
||||
# Null-output path: still succeeds, no crash (raw/durango:952-967).
|
||||
self.assertEqual(
|
||||
self.lib.iggy_tt_GetGlyphBox(
|
||||
self.font, 1, None, None, None, None),
|
||||
1)
|
||||
|
||||
def test_get_font_bounding_box_from_head(self):
|
||||
# raw/durango:609-629 reads xMin..yMax from head table offsets
|
||||
# +0x24..+0x2b relative to head (stored absolute via head offset).
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
x0 = ctypes.c_int32()
|
||||
y0 = ctypes.c_int32()
|
||||
x1 = ctypes.c_int32()
|
||||
y1 = ctypes.c_int32()
|
||||
self.lib.iggy_tt_GetFontBoundingBox(
|
||||
self.font,
|
||||
ctypes.byref(x0), ctypes.byref(y0),
|
||||
ctypes.byref(x1), ctypes.byref(y1))
|
||||
self.assertEqual(
|
||||
(x0.value, y0.value, x1.value, y1.value),
|
||||
(FONT_XMIN, FONT_YMIN, FONT_XMAX, FONT_YMAX))
|
||||
|
||||
def test_is_glyph_empty_true_for_zero_length_loca_false_for_contour(self):
|
||||
# raw/durango:1812-1823 empty when GetGlyfOffset < 0 (loca[i]==loca[i+1])
|
||||
# or numberOfContours == 0. Glyph 0 is empty loca; glyph 1 has a contour.
|
||||
# 2-arg ABI from iggy_font_provider (not the 1-arg raw prototype).
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
self.assertTrue(self.lib.iggy_tt_IsGlyphEmpty(self.font, 0))
|
||||
self.assertFalse(self.lib.iggy_tt_IsGlyphEmpty(self.font, 1))
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Shape + allocator interaction
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_get_glyph_shape_and_allocator_tag_through_set_user_data(self):
|
||||
# raw/durango:1985-1988 SetUserData writes *font = tag.
|
||||
# raw/durango:1317 GetGlyphShape calls GSwfMallocTruetype(*param_1, ...).
|
||||
# raw/durango:459-463 FreeShape (2-arg ABI) frees via iggy_free_alloc
|
||||
# with the same tag. Flow: InitFont -> SetUserData -> GetGlyphShape
|
||||
# -> FreeShape; assert tag/size/ptr counters.
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
self.lib.iggy_tt_SetUserData(self.font, USER_TAG)
|
||||
|
||||
vertices = ctypes.c_void_p()
|
||||
nverts = self.lib.iggy_tt_GetGlyphShape(
|
||||
self.font, 1, ctypes.byref(vertices))
|
||||
self.assertGreater(nverts, 0)
|
||||
self.assertTrue(vertices.value)
|
||||
self.assertGreaterEqual(
|
||||
self.lib.stb_truetype_wrapper_test_gswf_malloc_count(), 1)
|
||||
self.assertEqual(
|
||||
self.lib.stb_truetype_wrapper_test_last_malloc_player(), USER_TAG)
|
||||
self.assertGreater(
|
||||
self.lib.stb_truetype_wrapper_test_last_malloc_size(), 0)
|
||||
|
||||
free_before = self.lib.stb_truetype_wrapper_test_free_alloc_count()
|
||||
self.lib.iggy_tt_FreeShape(self.font, vertices)
|
||||
self.assertGreaterEqual(
|
||||
self.lib.stb_truetype_wrapper_test_free_alloc_count(),
|
||||
free_before + 1)
|
||||
self.assertEqual(
|
||||
self.lib.stb_truetype_wrapper_test_last_free_player(), USER_TAG)
|
||||
self.assertEqual(
|
||||
self.lib.stb_truetype_wrapper_test_last_free_ptr(), vertices.value)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Rasterize coverage (not pixel-exact)
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
def test_rasterize_simple_shape_produces_nonzero_coverage(self):
|
||||
# raw/durango:1902-1935 FlattenCurves + stbtt__rasterize; allocates
|
||||
# working buffers via GSwfMallocTruetype(userdata, ...) and frees them.
|
||||
# Assert basic coverage: not all-zero, dimensions match the target.
|
||||
# Path uses the GetGlyphShape vertex buffer (same as MakeGlyphBitmap
|
||||
# path at raw/durango:1891).
|
||||
ttf = build_minimal_ttf()
|
||||
self.assertEqual(self.load_font(ttf), 1)
|
||||
self.lib.iggy_tt_SetUserData(self.font, USER_TAG)
|
||||
|
||||
vertices = ctypes.c_void_p()
|
||||
nverts = self.lib.iggy_tt_GetGlyphShape(
|
||||
self.font, 1, ctypes.byref(vertices))
|
||||
self.assertGreater(nverts, 0)
|
||||
|
||||
width, height, stride = 32, 32, 32
|
||||
pixels = (ctypes.c_uint8 * (stride * height))()
|
||||
target = StbttBitmap(
|
||||
w=width, h=height, stride=stride, _pad=0,
|
||||
pixels=ctypes.addressof(pixels))
|
||||
|
||||
malloc_before = self.lib.stb_truetype_wrapper_test_gswf_malloc_count()
|
||||
self.lib.iggy_tt_Rasterize(
|
||||
ctypes.byref(target),
|
||||
0.35, # flatness (raw uses ~0.35f = __real_3eb33333)
|
||||
vertices, nverts,
|
||||
0.25, 0.25, # scale so 100-unit box fits in 32px
|
||||
# MakeGlyphBitmap derives off_y = -(int)ceil(ymax * scale_y)
|
||||
# (raw/durango:1880-1892); with invert=1 the negated scale_y maps
|
||||
# glyph y [0,100] to rows [-25,0], so off_y=-25 lands it on canvas.
|
||||
0, -25,
|
||||
1, # invert flag (MakeGlyphBitmap passes 1)
|
||||
USER_TAG,
|
||||
)
|
||||
# Rasterize itself allocates flatten buffers (raw/durango:1930-1933).
|
||||
self.assertGreaterEqual(
|
||||
self.lib.stb_truetype_wrapper_test_gswf_malloc_count(),
|
||||
malloc_before)
|
||||
|
||||
nonzero = sum(1 for b in pixels if b != 0)
|
||||
self.assertGreater(
|
||||
nonzero, 0,
|
||||
"Rasterize of a closed box contour should ink some coverage")
|
||||
self.assertEqual(target.w, width)
|
||||
self.assertEqual(target.h, height)
|
||||
|
||||
self.lib.iggy_tt_FreeShape(self.font, vertices)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
308
tests/test_reconstructed_stb_truetype_wrapper_cross.py
Normal file
308
tests/test_reconstructed_stb_truetype_wrapper_cross.py
Normal file
@@ -0,0 +1,308 @@
|
||||
"""Cross-platform contract tests for reconstructed stb_truetype_wrapper.
|
||||
|
||||
Mirrors tests/test_reconstructed_iggy_font_provider_cross.py. RED until
|
||||
stb_truetype_wrapper.c / stb_truetype_wrapper.h (and their CMake / modules.tsv
|
||||
rows) exist; the retail-evidence assertions run now and prove the nm/strings
|
||||
parsing is correct.
|
||||
|
||||
How EXPECTED_* were derived (all four .obj under
|
||||
work/iggy-native-decomp/<platform>-objects/stb_truetype_wrapper.obj):
|
||||
|
||||
EXPECTED_FUNCTIONS = `llvm-nm --extern-only --defined-only <obj>` T-tagged
|
||||
names, leading '.' stripped on ps3. IDENTICAL 32-name
|
||||
set on durango/orbis/ps3/vita. Retail *function*
|
||||
counts differ (42/37/35/36) because each platform
|
||||
inlines a different number of file-static helpers;
|
||||
the exported ABI surface is invariant (same pattern as
|
||||
iggy_font_provider's 33-export / 43-50 source counts).
|
||||
|
||||
EXPECTED_GLOBALS = empty. No real named data globals are common across
|
||||
all four platforms. Durango emits R-tagged MSVC
|
||||
/GF string-literal / float-pool symbols ("cmap",
|
||||
"head", __real@...) which are compiler artifacts, not
|
||||
source-level globals, and clang will not reproduce
|
||||
them. Ps3 emits D-tagged .opd function descriptors
|
||||
for every export (excluded here, same as font_provider).
|
||||
|
||||
EXPECTED_IMPORTS = universal core intersection of real non-scaffolding
|
||||
imports: GSwfMallocTruetype, iggy_free_alloc, memcpy,
|
||||
memset. Unlike iggy_font_provider (one uniform 25-
|
||||
import set), this module's libm/strlen imports GENUINELY
|
||||
differ per platform -- see PLATFORM_IMPORTS.
|
||||
|
||||
PLATFORM_IMPORTS = per-platform extras on top of the core, verified from
|
||||
retail `llvm-nm --undefined-only` (normalized: strip
|
||||
leading '.', strip '__imp_'):
|
||||
durango: ceil, floor, sqrt
|
||||
orbis: strlen
|
||||
(no ceil/floor/sqrt -- -O2 inlines them as
|
||||
roundsd/sqrtsd on x86_64)
|
||||
ps3: ceilf, floorf, strlen
|
||||
(float-precision; no sqrt -- PowerPC emits
|
||||
hardware fsqrt)
|
||||
vita: ceil, floor, sqrt, strlen
|
||||
This same durango+vita double / ps3 float / orbis-
|
||||
none floor/ceil split already exists as precedent in
|
||||
reconstructed swf_fontcache.c (~lines 75-92).
|
||||
|
||||
RUNTIME_ALLOWLISTS = pure toolchain scaffolding residual after subtracting
|
||||
core + platform extras:
|
||||
durango: __GSHandlerCheck, __security_check_cookie,
|
||||
__security_cookie, _fltused
|
||||
orbis: _GLOBAL_OFFSET_TABLE_
|
||||
ps3: {} (empty)
|
||||
vita: __aeabi_unwind_cpp_pr0,
|
||||
__aeabi_unwind_cpp_pr1, __divsi3,
|
||||
__stack_chk_fail, __stack_chk_guard
|
||||
|
||||
PROVENANCE strings (via `strings` on each retail .obj) also differ:
|
||||
durango: NONE (no path, no bare filename -- same
|
||||
fact as dtoa.obj on durango; zlib.obj on
|
||||
the same platform DOES embed a path, so
|
||||
this is a per-object fact)
|
||||
orbis: c:\\projects\\g\\src\\flash\\
|
||||
stb_truetype_wrapper.c
|
||||
ps3: bare "stb_truetype_wrapper.c" only
|
||||
vita: bare "stb_truetype_wrapper.c" only
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
|
||||
SOURCE = RECONSTRUCTED / "stb_truetype_wrapper.c"
|
||||
HEADER = RECONSTRUCTED / "stb_truetype_wrapper.h"
|
||||
LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm")
|
||||
VARIANTS = {
|
||||
"durango": ("clang", "x86_64-pc-windows-msvc"),
|
||||
"orbis": ("clang", "x86_64-unknown-freebsd"),
|
||||
"vita": ("clang", "armv7a-unknown-linux-gnueabihf"),
|
||||
}
|
||||
PS3_CLANG = Path("/opt/homebrew/opt/llvm/bin/clang")
|
||||
if PS3_CLANG.exists():
|
||||
VARIANTS["ps3"] = (str(PS3_CLANG), "powerpc-unknown-linux-gnu")
|
||||
|
||||
EXPECTED_FUNCTIONS = {
|
||||
"iggy_tt_BakeFontBitmap",
|
||||
"iggy_tt_CompareUTF8toUTF16_bigendian",
|
||||
"iggy_tt_FindGlyphIndex",
|
||||
"iggy_tt_FindMatchingFont",
|
||||
"iggy_tt_FlattenCurves",
|
||||
"iggy_tt_FreeBitmap",
|
||||
"iggy_tt_FreeShape",
|
||||
"iggy_tt_GetBakedQuad",
|
||||
"iggy_tt_GetCodepointBitmap",
|
||||
"iggy_tt_GetCodepointBitmapBox",
|
||||
"iggy_tt_GetCodepointBox",
|
||||
"iggy_tt_GetCodepointHMetrics",
|
||||
"iggy_tt_GetCodepointKernAdvance",
|
||||
"iggy_tt_GetCodepointShape",
|
||||
"iggy_tt_GetFontBoundingBox",
|
||||
"iggy_tt_GetFontNameString",
|
||||
"iggy_tt_GetFontOffsetForIndex",
|
||||
"iggy_tt_GetFontVMetrics",
|
||||
"iggy_tt_GetGlyphBitmap",
|
||||
"iggy_tt_GetGlyphBitmapBox",
|
||||
"iggy_tt_GetGlyphBox",
|
||||
"iggy_tt_GetGlyphHMetrics",
|
||||
"iggy_tt_GetGlyphKernAdvance",
|
||||
"iggy_tt_GetGlyphShape",
|
||||
"iggy_tt_InitFont",
|
||||
"iggy_tt_IsGlyphEmpty",
|
||||
"iggy_tt_MakeCodepointBitmap",
|
||||
"iggy_tt_MakeGlyphBitmap",
|
||||
"iggy_tt_Rasterize",
|
||||
"iggy_tt_ScaleForMappingEmToPixels",
|
||||
"iggy_tt_ScaleForPixelHeight",
|
||||
"iggy_tt_SetUserData",
|
||||
}
|
||||
# No common real named data globals across the four platforms.
|
||||
EXPECTED_GLOBALS = set()
|
||||
|
||||
# Universal non-scaffolding imports present on all four retail objects.
|
||||
EXPECTED_IMPORTS = {
|
||||
"GSwfMallocTruetype",
|
||||
"iggy_free_alloc",
|
||||
"memcpy",
|
||||
"memset",
|
||||
}
|
||||
# Per-platform libm / strlen extras (not forced into a single uniform set).
|
||||
PLATFORM_IMPORTS = {
|
||||
"durango": {"ceil", "floor", "sqrt"},
|
||||
"orbis": {"strlen"},
|
||||
"ps3": {"ceilf", "floorf", "strlen"},
|
||||
"vita": {"ceil", "floor", "sqrt", "strlen"},
|
||||
}
|
||||
RUNTIME_ALLOWLISTS = {
|
||||
"durango": {"__GSHandlerCheck", "__security_check_cookie",
|
||||
"__security_cookie", "_fltused"},
|
||||
"orbis": {"_GLOBAL_OFFSET_TABLE_"},
|
||||
"ps3": set(),
|
||||
"vita": {"__aeabi_unwind_cpp_pr0", "__aeabi_unwind_cpp_pr1", "__divsi3",
|
||||
"__stack_chk_fail", "__stack_chk_guard"},
|
||||
}
|
||||
|
||||
|
||||
def normalized_symbol(name):
|
||||
name = name.removeprefix(".")
|
||||
name = name.removeprefix("__imp_")
|
||||
if name == "_setjmp":
|
||||
return "setjmp"
|
||||
return name
|
||||
|
||||
|
||||
class ReconstructedStbTruetypeWrapperCrossTests(unittest.TestCase):
|
||||
def test_native_provenance_counts_source_path_and_inventory(self):
|
||||
# (function count, raw line count) per platform, from
|
||||
# iggy_<platform>.modules.tsv column 3 and `wc -l` on the raw .c.
|
||||
expected = {
|
||||
"durango": (42, 2874), "orbis": (37, 2338),
|
||||
"ps3": (35, 2691), "vita": (36, 2729),
|
||||
}
|
||||
for platform, (functions, lines) in expected.items():
|
||||
modules = ROOT / (
|
||||
f"iggy-decompiled/symbols/iggy_{platform}.modules.tsv")
|
||||
row = next(
|
||||
line for line in modules.read_text(encoding="utf-8").splitlines()
|
||||
if line.startswith("stb_truetype_wrapper\t"))
|
||||
self.assertEqual(int(row.split("\t")[2]), functions, platform)
|
||||
raw = ROOT / (
|
||||
f"iggy-decompiled/raw/{platform}/stb_truetype_wrapper.c")
|
||||
self.assertEqual(
|
||||
len(raw.read_text(encoding="utf-8").splitlines()),
|
||||
lines, platform)
|
||||
obj = ROOT / (
|
||||
f"work/iggy-native-decomp/{platform}-objects/"
|
||||
f"stb_truetype_wrapper.obj")
|
||||
symbols = subprocess.run(
|
||||
["nm", "-g", str(obj)], capture_output=True, text=True)
|
||||
self.assertEqual(symbols.returncode, 0, (platform, symbols.stderr))
|
||||
normalized = symbols.stdout.replace(" .", " ")
|
||||
for symbol in EXPECTED_FUNCTIONS | EXPECTED_GLOBALS:
|
||||
self.assertIn(symbol, normalized, (platform, symbol))
|
||||
strings = subprocess.run(
|
||||
["strings", str(obj)], capture_output=True, text=True)
|
||||
# Per-platform provenance: durango has none; orbis has the full
|
||||
# Windows path; ps3/vita only embed the bare compile-unit name.
|
||||
if platform == "durango":
|
||||
# Verified: strings | grep -i stb_truetype_wrapper.c is empty
|
||||
# on this object (same absence as dtoa.obj on durango).
|
||||
self.assertNotIn(
|
||||
"stb_truetype_wrapper.c", strings.stdout, platform)
|
||||
elif platform == "orbis":
|
||||
self.assertIn(
|
||||
r"c:\projects\g\src\flash\stb_truetype_wrapper.c",
|
||||
strings.stdout, platform)
|
||||
else:
|
||||
self.assertIn(
|
||||
"stb_truetype_wrapper.c", strings.stdout, platform)
|
||||
|
||||
if SOURCE.exists():
|
||||
cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text(
|
||||
encoding="utf-8")
|
||||
self.assertIn(
|
||||
"iggy_reconstructed_stb_truetype_wrapper_${platform}", cmake)
|
||||
self.assertIn(
|
||||
"add_library(${target} STATIC stb_truetype_wrapper.c)", cmake)
|
||||
self.assertIn(
|
||||
"IGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_${platform_upper}",
|
||||
cmake)
|
||||
inventory = (RECONSTRUCTED / "modules.tsv").read_text(
|
||||
encoding="utf-8")
|
||||
# Exports are invariant (32) but retail source_functions vary
|
||||
# (42/37/35/36), so inventory uses per-platform rows exactly like
|
||||
# iggy_font_provider.
|
||||
for platform, functions in (
|
||||
("durango", 42), ("orbis", 37),
|
||||
("ps3", 35), ("vita", 36)):
|
||||
self.assertIn(
|
||||
f"stb_truetype_wrapper\t{functions}\t32\t"
|
||||
f"{platform}\tcomplete",
|
||||
inventory)
|
||||
|
||||
def test_exact_exports_globals_normalized_imports_and_c_cpp_abi(self):
|
||||
self.assertTrue(SOURCE.exists(), f"missing {SOURCE}")
|
||||
self.assertTrue(HEADER.exists(), f"missing {HEADER}")
|
||||
# Reconstructed source should embed the canonical Windows path string
|
||||
# (same convention as sibling modules); orbis retail embeds it.
|
||||
self.assertIn(
|
||||
r"c:\\projects\\g\\src\\flash\\stb_truetype_wrapper.c",
|
||||
SOURCE.read_text(encoding="utf-8"))
|
||||
|
||||
function_pointers = "".join(
|
||||
f" (GenericFn)&{name},\n"
|
||||
for name in sorted(EXPECTED_FUNCTIONS))
|
||||
probe_text = (
|
||||
'#include <stdint.h>\n#include "stb_truetype_wrapper.h"\n'
|
||||
'typedef void (*GenericFn)(void);\n'
|
||||
'static GenericFn tt_abis[] = {\n' + function_pointers + '};\n'
|
||||
'int use_abis(void) {\n'
|
||||
' for (unsigned i = 0; '
|
||||
'i < sizeof(tt_abis) / sizeof(tt_abis[0]); ++i) {\n'
|
||||
' if (!tt_abis[i]) return 0;\n'
|
||||
' }\n'
|
||||
' return 1;\n'
|
||||
'}\n')
|
||||
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
temporary = Path(directory)
|
||||
for platform, (compiler, target) in VARIANTS.items():
|
||||
extra = ["-mthumb"] if platform == "vita" else []
|
||||
output = temporary / f"stb_truetype_wrapper_{platform}.o"
|
||||
build = subprocess.run([
|
||||
compiler, "-target", target, "-ffreestanding",
|
||||
"-fno-builtin",
|
||||
"-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
|
||||
*extra,
|
||||
f"-DIGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_"
|
||||
f"{platform.upper()}=1",
|
||||
"-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output),
|
||||
], capture_output=True, text=True)
|
||||
self.assertEqual(build.returncode, 0, (platform, build.stderr))
|
||||
if LLVM_NM.exists():
|
||||
defined = subprocess.run([
|
||||
str(LLVM_NM), "--extern-only", "--defined-only",
|
||||
str(output),
|
||||
], capture_output=True, text=True)
|
||||
undefined = subprocess.run([
|
||||
str(LLVM_NM), "--undefined-only", str(output),
|
||||
], capture_output=True, text=True)
|
||||
names = {normalized_symbol(parts[-1])
|
||||
for line in defined.stdout.splitlines()
|
||||
if len(parts := line.split()) >= 2 and
|
||||
parts[-2].upper() in {"T", "D", "B", "C"}}
|
||||
imports = {normalized_symbol(line.split()[-1])
|
||||
for line in undefined.stdout.splitlines()
|
||||
if line.split()}
|
||||
self.assertEqual(
|
||||
names, EXPECTED_FUNCTIONS | EXPECTED_GLOBALS, platform)
|
||||
expected_imports = (
|
||||
EXPECTED_IMPORTS | PLATFORM_IMPORTS[platform])
|
||||
self.assertEqual(
|
||||
imports - RUNTIME_ALLOWLISTS[platform],
|
||||
expected_imports, (platform, imports))
|
||||
|
||||
for language, standard in (("c", "c11"), ("c++", "c++17")):
|
||||
suffix = "c" if language == "c" else "cpp"
|
||||
probe = temporary / (
|
||||
f"stb_truetype_wrapper_abi_{platform}.{suffix}")
|
||||
probe.write_text(probe_text, encoding="utf-8")
|
||||
syntax = subprocess.run([
|
||||
compiler, "-target", target, "-ffreestanding",
|
||||
"-fsyntax-only", f"-std={standard}",
|
||||
"-Wall", "-Wextra", "-Werror", *extra,
|
||||
f"-DIGGY_RECONSTRUCT_STB_TRUETYPE_WRAPPER_"
|
||||
f"{platform.upper()}=1",
|
||||
"-I", str(RECONSTRUCTED), str(probe),
|
||||
], capture_output=True, text=True)
|
||||
self.assertEqual(
|
||||
syntax.returncode, 0,
|
||||
(platform, language, syntax.stderr))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user