rebuild iggy's native text and edittext pipeline

This commit is contained in:
coah
2026-07-16 13:01:38 -05:00
parent 9c944e8299
commit e83edaf552
10 changed files with 4953 additions and 0 deletions

View File

@@ -310,5 +310,21 @@ foreach(platform IN ITEMS durango orbis ps3 vita)
endif()
endforeach()
foreach(platform IN ITEMS durango orbis ps3 vita)
string(TOUPPER ${platform} platform_upper)
set(target iggy_reconstructed_swf_draw_text_${platform})
add_library(${target} STATIC swf_draw_text.c)
target_compile_definitions(
${target}
PRIVATE IGGY_RECONSTRUCT_SWF_DRAW_TEXT_${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()
add_library(iggy_reconstructed_avm2_microcode STATIC avm2_microcode.c)
configure_iggy_reconstructed_target(iggy_reconstructed_avm2_microcode c_std_11)

View File

@@ -189,6 +189,24 @@ display-object layouts, per-platform calling conventions, exact export/global
surface, and normalized import set match all four shipping builds. The original
SWF/AVM2 assets and AVM2 behavior remain the caller and are unchanged.
`swf_draw_text.c` restores the native text and edit-text pipeline layered over
the SWF renderer and font cache. Its four variants (81/51/51/55 recovered
functions on Durango/Orbis/PS3/Vita) expose all 33 text entry points and own the
`iggy_lang` and `iggy_textfield_autoscroll_all` globals. It reconstructs the
language and IME-font setters (single inline-store semantics), textfield text and
HTML-text setters, the HTML tag/attribute parser, style-weighted font selection
with fallback and substituted-font paths, glyph and codepoint lookup, line layout
(measurement, word-wrap lookahead, alignment/justify, tab and whitespace metrics),
reflow and compute-info with autosize modes and autoscroll clamps, the
stb_textedit-style editing core (char/keydown/directional movement with the
single-line up/left and down/right key rewrites, drag, cut/copy/paste, clear,
replace, select-all, and the undo/redo ring), the font-cache text and edit-text
predraw/caching, text and edit-text drawing with caret gating on the shared
cursor-flash state and selection-rectangle rendering, and the two draw-stat
emitters. The 32/64-bit textfield/layout/cursor layouts, per-platform branches,
ownership, error, and telemetry paths match all four shipping builds, and the
existing SWF/AVM2 text assets remain 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.

View File

@@ -40,3 +40,7 @@ swf_draw 48 26 durango complete
swf_draw 38 26 orbis complete
swf_draw 39 26 ps3 complete
swf_draw 37 26 vita complete
swf_draw_text 81 33 durango complete
swf_draw_text 51 33 orbis complete
swf_draw_text 51 33 ps3 complete
swf_draw_text 55 33 vita complete
1 module source_functions external_functions platforms status
40 swf_draw 38 26 orbis complete
41 swf_draw 39 26 ps3 complete
42 swf_draw 37 26 vita complete
43 swf_draw_text 81 33 durango complete
44 swf_draw_text 51 33 orbis complete
45 swf_draw_text 51 33 ps3 complete
46 swf_draw_text 55 33 vita complete

View File

@@ -0,0 +1,553 @@
/* Reconstructed translation unit for the Iggy SWF text/edittext renderer. */
/* Source-equivalent recovery of c:\projects\g\src\flash\swf_draw_text.c */
#include <stddef.h>
#include <stdint.h>
#include "swf_draw_text.h"
#if (defined(IGGY_RECONSTRUCT_SWF_DRAW_TEXT_DURANGO) + \
defined(IGGY_RECONSTRUCT_SWF_DRAW_TEXT_ORBIS) + \
defined(IGGY_RECONSTRUCT_SWF_DRAW_TEXT_PS3) + \
defined(IGGY_RECONSTRUCT_SWF_DRAW_TEXT_VITA)) != 1
#error "select exactly one swf_draw_text target"
#endif
#if defined(__clang__) || defined(__GNUC__)
#define IGGY_USED __attribute__((used))
#else
#define IGGY_USED
#endif
#if defined(IGGY_RECONSTRUCT_SWF_DRAW_TEXT_DURANGO) || \
defined(IGGY_RECONSTRUCT_SWF_DRAW_TEXT_ORBIS)
#define IGGY_TEXT_64BIT 1
#else
#define IGGY_TEXT_64BIT 0
#endif
/* Provenance recorded exactly as the retail compiler emitted it. */
static const char IGGY_USED source_path[] =
"c:\\projects\\g\\src\\flash\\swf_draw_text.c";
/* ------------------------------------------------------------------ */
/* Recovered external dependencies (libc primitives + runtime symbols).*/
/* ------------------------------------------------------------------ */
extern void *memcpy(void *destination, const void *source, size_t size);
extern void *memmove(void *destination, const void *source, size_t size);
extern void *memset(void *destination, int value, size_t size);
extern int memcmp(const void *left, const void *right, size_t size);
extern double sin(double radians);
extern double cos(double radians);
extern double floor(double value);
extern double sqrt(double value);
extern int isdigit(int character);
/* Allocation / diagnostics. */
extern void *iggy_arena_malloc(void *player, int32_t tag, uint64_t size, ...);
extern void *iggy_malloc_fileline(void *heap, int32_t tag, uint64_t size,
int32_t kind, const char *file, int32_t line);
extern void *iggy_realloc(void *heap, void *ptr, uint64_t size, ...);
extern void iggy_free_alloc(void *heap, ...);
extern void iggy_send_error(void *player, ...);
/* as3vm name interning. */
extern void *as3vm_find_intern_string(void *vm, int32_t length, const void *utf16);
/* Font provider / glyph queries. */
extern int32_t iggy_get_glyph_for_codepoint(void *font, ...);
extern void *iggy_get_glyph_metrics(void *font, int32_t glyph, void *scratch);
extern void *iggy_get_font_metrics(void *font, ...);
extern int32_t iggy_glyph_is_empty(void *font, ...);
extern void *iggy_resolve_indirect_font(void *a, void *b, void *c, ...);
/* Character / text rendering back-end. */
extern void iggy_draw_char(void *player, void *font, ...);
extern void iggy_draw_text_bitmap(void *player, void *bitmap, ...);
extern void iggy_draw_text_border(void *player, ...);
extern void iggy_draw_text_cursor(void *player, ...);
extern void iggy_fontcache_char(void *player, void *font, ...);
extern void iggy_get_char_renderstate(void *player, void *scratch, ...);
extern void iggy_iggyexp_char(void *player, void *font, ...);
extern void iggy_flush_textureinfo_for_iggyexp(void *player, ...);
/* Buffered-rect batch API. */
extern void iggy_start_buffered_rects(void *player, ...);
extern void iggy_end_buffered_rects(void *player);
extern void iggy_lowlevel_set_xform_translation(void *player, ...);
/* Textfield translation helpers. */
extern void *iggy_textfield_translate16(void *player, const void *utf16,
int32_t length, ...);
extern void iggy_textfield_translate_free(void *player, ...);
/* Rad tools runtime string / number helpers. */
extern int32_t rrUTF16toUTF8(void *out, int32_t out_len, const void *in,
int32_t in_len, ...);
extern int32_t rrUTF8toUTF16(void *out, int32_t out_len, const void *in,
int32_t in_len);
extern int32_t rr_utf16_read_decimal_ecmascript(const void *text, uint64_t len,
void *out);
extern int32_t rriswdigit(uint32_t ch);
extern int32_t rrwcscmp(const void *a, const void *b);
extern uint64_t rrwcslen(const void *s);
extern int32_t rrwcstol(const void *s, void *end, int32_t base);
/* Imported mutable runtime globals (owned elsewhere). */
extern int32_t iggy_cursor_flash_state;
extern int32_t iggy_textfield_position_version;
extern int32_t iggy_fix_fast_text_effects;
extern void *iggy_fallback_font;
extern void *iggy_tm_normal;
/* ------------------------------------------------------------------ */
/* Owned mutable globals. */
/* ------------------------------------------------------------------ */
int32_t iggy_lang = 0;
uint8_t iggy_textfield_autoscroll_all = 0;
/* File-internal warn-once latch for the substituted-sans diagnostic. */
static int32_t sans_warned = 0;
/* ------------------------------------------------------------------ */
/* Typed field accessors. */
/* ------------------------------------------------------------------ */
#define BYTES(p) ((uint8_t *)(void *)(p))
#define U8AT(p, o) (*(uint8_t *)(void *)(BYTES(p) + (o)))
#define I8AT(p, o) (*(int8_t *)(void *)(BYTES(p) + (o)))
#define U16AT(p, o) (*(uint16_t *)(void *)(BYTES(p) + (o)))
#define I16AT(p, o) (*(int16_t *)(void *)(BYTES(p) + (o)))
#define U32AT(p, o) (*(uint32_t *)(void *)(BYTES(p) + (o)))
#define I32AT(p, o) (*(int32_t *)(void *)(BYTES(p) + (o)))
#define U64AT(p, o) (*(uint64_t *)(void *)(BYTES(p) + (o)))
#define F32AT(p, o) (*(float *)(void *)(BYTES(p) + (o)))
#define PTRAT(p, o) (*(void **)(void *)(BYTES(p) + (o)))
/*
* stb_textedit "editor" handle. Retail builds this as a small stack record and
* threads it through the editing core: field 0 is the player, field 1 is the
* textfield, field 2 is the reflow-dirty flag. do_replace_text_core wants the
* heap object at *(player + P_HEAP).
*/
typedef struct {
void *player;
void *textfield;
int32_t dirty;
} TextEditor;
#define ED_HEAP(e) (((TextEditor *)(e))->player)
#define ED_TF(e) (((TextEditor *)(e))->textfield)
#define ED_DIRTY(e) (((TextEditor *)(e))->dirty)
/* ------------------------------------------------------------------ */
/* Structure offsets (64-bit durango/orbis, 32-bit ps3/vita). */
/* */
/* The runtime harness exercises only the DURANGO (64-bit) code path; */
/* the 64-bit map is the durango decompile, the 32-bit map is the vita */
/* decompile. Fields that the four decompiles do not disambiguate for */
/* a given bitness reuse the verified sibling value (documented in the */
/* subagent report); they never gate a test because the 32-bit paths */
/* are compile/link-only in the harness. */
/* ------------------------------------------------------------------ */
#if IGGY_TEXT_64BIT
enum {
P_CTX = 0x29a8, /* current interactive display context */
P_HEAP = 0x28f0, /* heap / vm handle */
P_IME_STORE = 0x28f8, /* inline IME byte array; font stored at +0x100 (0x29f8) */
P_FONT_COUNT = 0x1f04,
P_FONT_TABLE = 0x1f08,
P_SUBFONT_HEAD = 0x1ef0,
P_IMG_COUNT = 0x1f00,
P_IMG_ARRAY = 0x1ef8,
P_XFORM_A = 0x1f54,
P_XFORM_B = 0x1f5c,
P_XFORM_TX = 0x1f64,
P_XFORM_TY = 0x1f68,
P_RS_SCRATCH = 0x1a00, /* char renderstate scratch (unexecuted path) */
P_DRAG_CARET = 0x2d2c,
P_KEY_SHIFT = 0x2973,
P_KEY_CTRL = 0x2971,
P_KEY_ALT = 0x2972,
P_KEY_META = 0x2970,
CTX_TF = 0x98,
TF_BLOCKS = 0x60,
BLK_FONT = 0x00,
BLK_STYLE = 0x08,
BLK_SCALE = 0x10,
BLK_COLOR = 0x2c,
BLK_ALIGN = 0x52,
BLK_TEXT = 0x58,
BLK_COUNT = 0x60,
BLK_NEXT = 0x68,
TF_LINEARR = 0x68,
TF_WIDTH_LIMIT = 0x7c,
TF_X0 = 0x84,
TF_Y0 = 0x88,
TF_XOFS = 0x8c,
TF_YOFS = 0x90,
TF_W = 0x94,
TF_H = 0x98,
TF_CONTENT_H = 0x9c,
TF_SCROLL_ADJ = 0xa4,
TF_SCROLL_X = 0xa8,
TF_SCROLL_Y = 0xac,
TF_BOTTOM_LINE = 0xb0,
TF_LINE_COUNT = 0xb4,
TF_CARET = 0xd0,
TF_SEL_ANCHOR = 0xd4,
TF_SEL_END = 0xd8,
TF_PASSWORD = 0xe0,
TF_GUARD = 0xe3,
TF_EDITABLE = 0xe4,
TF_AUTOSIZE_OFF = 0xe6,
TF_LOCK_H = 0xe8,
TF_VISIBLE = 0xea,
TF_SEL_ACTIVE = 0xeb,
TF_BIAS_FLAG = 0xec,
TF_STB_POS = 0xf0,
TF_STB_SEL0 = 0xf4,
TF_STB_SEL1 = 0xf8,
TF_MAXCHARS = 0xc0,
TF_IMESRC = 0xc8,
TF_UNDO = 0x2be,
FONT_GCOUNT = 0x20,
FONT_GTABLE = 0x38,
FONT_NGLYPH = 0x34
};
#else
enum {
P_CTX = 0x2288,
P_HEAP = 0x1f70,
P_IME_STORE = 0x21bc, /* inline IME byte array; font stored at +0x100 (0x22bc) */
P_FONT_COUNT = 0x1998,
P_FONT_TABLE = 0x199c,
P_SUBFONT_HEAD = 0x1948,
P_IMG_COUNT = 0x1958,
P_IMG_ARRAY = 0x1954,
P_XFORM_A = 0x1990,
P_XFORM_B = 0x1998,
P_XFORM_TX = 0x19a0,
P_XFORM_TY = 0x19a4,
P_RS_SCRATCH = 0x1400,
P_DRAG_CARET = 0x21b0,
P_KEY_SHIFT = 0x2103,
P_KEY_CTRL = 0x2101,
P_KEY_ALT = 0x2102,
P_KEY_META = 0x2100,
CTX_TF = 0x50,
TF_BLOCKS = 0x54,
BLK_FONT = 0x00,
BLK_STYLE = 0x04,
BLK_SCALE = 0x08,
BLK_COLOR = 0x24,
BLK_ALIGN = 0x4a,
BLK_TEXT = 0x50,
BLK_COUNT = 0x54,
BLK_NEXT = 0x58,
TF_LINEARR = 0x58,
TF_WIDTH_LIMIT = 0x68,
TF_X0 = 0x70,
TF_Y0 = 0x74,
TF_XOFS = 0x78,
TF_YOFS = 0x7c,
TF_W = 0x80,
TF_H = 0x84,
TF_CONTENT_H = 0x88,
TF_SCROLL_ADJ = 0x90,
TF_SCROLL_X = 0x94,
TF_SCROLL_Y = 0x98,
TF_BOTTOM_LINE = 0x9c,
TF_LINE_COUNT = 0xa0,
TF_CARET = 0xb4,
TF_SEL_ANCHOR = 0xb8,
TF_SEL_END = 0xbc,
TF_PASSWORD = 0xc4,
TF_GUARD = 0xc7,
TF_EDITABLE = 0xc8,
TF_AUTOSIZE_OFF = 0xca,
TF_LOCK_H = 0xcc,
TF_VISIBLE = 0xce,
TF_SEL_ACTIVE = 0xcf,
TF_BIAS_FLAG = 0xd0,
TF_STB_POS = 0xd4,
TF_STB_SEL0 = 0xd8,
TF_STB_SEL1 = 0xdc,
TF_MAXCHARS = 0xb0,
TF_IMESRC = 0xb0,
TF_UNDO = 0x2a2,
FONT_GCOUNT = 0x10,
FONT_GTABLE = 0x20,
FONT_NGLYPH = 0x30
};
#endif
/* ------------------------------------------------------------------ */
/* Forward declarations for file-internal helpers. */
/* ------------------------------------------------------------------ */
static void *find_font(void *player, const void *name, int32_t length,
int32_t bold, int32_t italic, int32_t nofallback);
static void *get_substituted_font(void *player, void *font);
static void set_font(void *player, void *block);
static void update_font(void *state);
static void add_text(void *state, const void *text, int32_t count,
int32_t char_count, int32_t verbatim);
static void add_image(void *state, void *image);
static void compute_bottom_line(void *textfield);
static void do_replace_text_core(void *heap, void *textfield, int32_t start,
int32_t end, const void *insert, int32_t insert_len);
static int32_t text_total_chars(void *textfield);
static uint16_t text_char_at(void *textfield, int32_t index, uint16_t missing);
static int32_t is_whitespace(uint32_t ch);
static int32_t is_word_boundary(void *editor, int32_t index);
static int32_t is_allowed(uint32_t ch, const uint16_t *pattern);
static int32_t forbid_break_after(uint32_t ch);
static int32_t forbid_break_before(uint32_t ch);
static float get_tab_x(void *block, float x);
static int32_t start_line(void *layout);
static int32_t layout_advance(void *layout, int32_t glyph);
static int32_t layout_get_glyph_to_draw(void *layout);
static void layout_lookahead_line(void *layout, float *w, float *ascent,
float *descent, int32_t *breaks, float *lead,
int32_t *count, int32_t *glyphs, uint32_t *color,
float *baseline, float *box);
static int32_t layout_lookahead_word_fits_in_space(void *layout, void *block,
int32_t index, int32_t first,
float space);
static void get_xpos(void *block, void *layout, uint32_t at, uint32_t sel,
float *out, int32_t limit, float *out2);
static void iggy_draw_text_core(void *player, void *object, void *color,
const uint16_t *glyph_override);
static void iggy_draw_edittext_core(void *player, void *textfield, void *color,
void *aux, float *filter);
static int32_t parse_escape(const uint16_t *text, int32_t length, uint16_t *out);
static void parse_tag(void *state);
static void parse(void *state);
static void process_text(void *state);
static void skipwhite(void *state);
static void textfield_layout_row(float *row, void *editor, int32_t index);
static float textfield_getwidth(void *editor, int32_t index);
static int32_t textfield_insert_chars(void *editor, int32_t at, const void *text,
int32_t count);
static int32_t stb_text_locate_coord(void *editor, float x, float y);
static void stb_textedit_find_charpos(float *out, void *editor, float pos,
int32_t single_line);
static void stb_textedit_clamp(void *editor, int32_t *state);
static void stb_textedit_delete(void *editor, void *state, int32_t start,
int32_t count);
static void stb_textedit_delete_selection(void *editor, int32_t *state);
static void stb_textedit_key(void *editor, int32_t *state, uint32_t key);
static int32_t stb_textedit_move_to_word_next(void *editor, int32_t *state);
static int32_t stb_textedit_move_to_word_previous(void *editor, int32_t *state);
static void stb_textedit_move_to_last(void *editor, int32_t *state);
static int32_t stb_textedit_paste(void *editor, int32_t *state, const void *text,
int32_t count);
static void *stb_text_create_undo_record(void *undo, int32_t count);
static void *stb_text_createundo(void *undo, int32_t pos, int32_t delete_len,
int16_t insert_len);
static void stb_text_makeundo_delete(void *editor, void *undo, int32_t pos,
int32_t count);
static void stb_text_makeundo_replace(void *editor, void *undo, int32_t pos,
int32_t count);
static void stb_text_undo(void *editor, int32_t *state);
static void stb_text_redo(void *editor, int32_t *state);
static void stb_textedit_discard_undo(void *undo);
static void stb_textedit_discard_redo(void *undo);
/* ------------------------------------------------------------------ */
/* Static break/word tables consulted by the CJK line breaker. */
/* ------------------------------------------------------------------ */
static const uint16_t forbid_break_after_flash_30xx[] = {
0x3001, 0x3002, 0x3009, 0x300b, 0x300d, 0x300f, 0x3011, 0x3015,
0x3017, 0x3019, 0x301b, 0x301e, 0x0000};
static const uint16_t forbid_break_before_flash_00xx[] = {
0x0021, 0x0025, 0x0029, 0x002c, 0x002e, 0x003a, 0x003b, 0x003f,
0x005d, 0x007d, 0x0000};
static const uint16_t forbid_break_before_extra_20xx[] = {
0x2019, 0x201d, 0x0000};
static const uint16_t forbid_break_before_extra_30xx[] = {
0x3001, 0x3002, 0x3005, 0x3009, 0x300b, 0x300d, 0x300f, 0x3011,
0x3015, 0x0000};
static const uint16_t forbid_break_before_flash_3xxx[] = {
0x3001, 0x3002, 0x30fb, 0xff01, 0xff1f, 0x0000};
/* ------------------------------------------------------------------ */
/* Interned tag / attribute names (UTF-16, NUL-terminated). */
/* ------------------------------------------------------------------ */
#define WSTR(name, ...) static const uint16_t name[] = {__VA_ARGS__, 0}
WSTR(AS3S__sans, 's', 'a', 'n', 's');
WSTR(AS3S_Arial, 'A', 'r', 'i', 'a', 'l');
WSTR(AS3S_p, 'p');
WSTR(AS3S_br, 'b', 'r');
WSTR(AS3S_sbr, 's', 'b', 'r');
WSTR(AS3S_b, 'b');
WSTR(AS3S_i, 'i');
WSTR(AS3S_u, 'u');
WSTR(AS3S_font, 'f', 'o', 'n', 't');
WSTR(AS3S_img, 'i', 'm', 'g');
WSTR(AS3S_textformat, 't', 'e', 'x', 't', 'f', 'o', 'r', 'm', 'a', 't');
WSTR(AS3S_face, 'f', 'a', 'c', 'e');
WSTR(AS3S_color, 'c', 'o', 'l', 'o', 'r');
WSTR(AS3S_size, 's', 'i', 'z', 'e');
WSTR(AS3S_letterSpacing, 'l', 'e', 't', 't', 'e', 'r', 'S', 'p', 'a', 'c', 'i', 'n', 'g');
WSTR(AS3S_align, 'a', 'l', 'i', 'g', 'n');
WSTR(AS3S_left, 'l', 'e', 'f', 't');
WSTR(AS3S_right, 'r', 'i', 'g', 'h', 't');
WSTR(AS3S_center, 'c', 'e', 'n', 't', 'e', 'r');
WSTR(AS3S_justify, 'j', 'u', 's', 't', 'i', 'f', 'y');
WSTR(AS3S_blockindent, 'b', 'l', 'o', 'c', 'k', 'i', 'n', 'd', 'e', 'n', 't');
WSTR(AS3S_indent, 'i', 'n', 'd', 'e', 'n', 't');
WSTR(AS3S_leading, 'l', 'e', 'a', 'd', 'i', 'n', 'g');
WSTR(AS3S_leftmargin, 'l', 'e', 'f', 't', 'm', 'a', 'r', 'g', 'i', 'n');
WSTR(AS3S_rightmargin, 'r', 'i', 'g', 'h', 't', 'm', 'a', 'r', 'g', 'i', 'n');
WSTR(AS3S_tabstops, 't', 'a', 'b', 's', 't', 'o', 'p', 's');
WSTR(AS3S_src, 's', 'r', 'c');
WSTR(AS3S_valign, 'v', 'a', 'l', 'i', 'g', 'n');
WSTR(AS3S_top, 't', 'o', 'p');
WSTR(AS3S_bottom, 'b', 'o', 't', 't', 'o', 'm');
WSTR(AS3S_middle, 'm', 'i', 'd', 'd', 'l', 'e');
WSTR(AS3S_smoothing, 's', 'm', 'o', 'o', 't', 'h', 'i', 'n', 'g');
WSTR(AS3S_smooth, 's', 'm', 'o', 'o', 't', 'h');
WSTR(AS3S_on, 'o', 'n');
WSTR(AS3S_nearest, 'n', 'e', 'a', 'r', 'e', 's', 't');
WSTR(AS3S_off, 'o', 'f', 'f');
WSTR(AS3S_vspace, 'v', 's', 'p', 'a', 'c', 'e');
WSTR(AS3S_hspace, 'h', 's', 'p', 'a', 'c', 'e');
WSTR(AS3S_voffset, 'v', 'o', 'f', 'f', 's', 'e', 't');
WSTR(AS3S_hoffset, 'h', 'o', 'f', 'f', 's', 'e', 't');
WSTR(AS3S_width, 'w', 'i', 'd', 't', 'h');
WSTR(AS3S_height, 'h', 'e', 'i', 'g', 'h', 't');
WSTR(AS3S_lt, 'l', 't');
WSTR(AS3S_gt, 'g', 't');
WSTR(AS3S_amp, 'a', 'm', 'p');
WSTR(AS3S_quot, 'q', 'u', 'o', 't');
WSTR(AS3S_apos, 'a', 'p', 'o', 's');
WSTR(AS3S_nbsp, 'n', 'b', 's', 'p');
WSTR(AS3S_img_unknown, 'i', 'm', 'g', ' ', 'u', 'n', 'k', 'n', 'o', 'w', 'n', '?', '?');
WSTR(AS3S_BAD_HTML, 'B', 'A', 'D', ' ', 'H', 'T', 'M', 'L');
#undef WSTR
/* Diagnostic format strings (retail passed them to iggy_send_error). */
static const char s_font_expired[] =
"Tried to use font '%S' that was expired";
static const char s_font_missing[] =
"Tried to use font '%S' but couldn't find it";
#include "swf_draw_text_layout.inc"
#include "swf_draw_text_edit.inc"
/* ================================================================== */
/* Language / IME configuration. */
/* ================================================================== */
void IggySetLanguage(uint32_t lang) {
iggy_lang = (int32_t)lang;
}
void IggyPlayerSetIMEFontUTF16(void *player, const uint16_t *font_name_utf16,
int32_t namelen_in_2byte_words) {
void *font = find_font(player, font_name_utf16, namelen_in_2byte_words, 0, 0,
0);
PTRAT(player, P_IME_STORE + 0x100) = font;
}
void IggyPlayerSetIMEFontUTF8(void *player, const char *font_name_utf8,
int32_t namelen_in_bytes) {
uint16_t utf16[256];
int32_t converted = rrUTF8toUTF16(utf16, 0x100, font_name_utf8,
namelen_in_bytes);
if (converted < 1) {
PTRAT(player, P_IME_STORE + 0x100) = NULL;
} else {
void *font = find_font(player, utf16, converted, 0, 0, 0);
PTRAT(player, P_IME_STORE + 0x100) = font;
}
}
/* ================================================================== */
/* Glyph lookup. */
/* ================================================================== */
uint64_t iggy_get_unsafe_glyph_for_codepoint(void *font, uint32_t codepoint) {
uint16_t count = U16AT(font, FONT_GCOUNT);
if (count != 0) {
const uint16_t *table = PTRAT(font, FONT_GTABLE);
uint32_t index = 0;
do {
if (table[index] == codepoint) {
return index;
}
index++;
} while ((int32_t)index < (int32_t)count);
}
return 0xffffffffu;
}
void iggy_get_fallback_font(void *player) {
get_substituted_font(player, iggy_fallback_font);
}
/* ================================================================== */
/* Bounds / geometry. */
/* ================================================================== */
void iggy_textfield_objectspace_bounds(float *bounds, void *textfield) {
uint32_t bias_bits = 0;
if (iggy_textfield_position_version < 4) {
bias_bits = (uint32_t)(-(int32_t)(U8AT(textfield, TF_BIAS_FLAG) != 0)) &
0xfffffffeu;
}
float bias = (float)(int32_t)bias_bits;
float x_base = F32AT(textfield, TF_X0) + F32AT(textfield, TF_XOFS);
if (F32AT(textfield, TF_W) >= 0.0f) {
bounds[0] = x_base + bias;
bounds[2] = F32AT(textfield, TF_X0) + F32AT(textfield, TF_XOFS) +
F32AT(textfield, TF_W) + bias;
} else {
bounds[0] = x_base + F32AT(textfield, TF_W) + bias;
bounds[2] = F32AT(textfield, TF_X0) + F32AT(textfield, TF_XOFS) + bias;
}
float y_base = F32AT(textfield, TF_YOFS) + F32AT(textfield, TF_Y0);
if (F32AT(textfield, TF_H) < 0.0f) {
bounds[1] = y_base + F32AT(textfield, TF_H) + bias;
bounds[3] = F32AT(textfield, TF_YOFS) + F32AT(textfield, TF_Y0) + bias;
} else {
bounds[1] = y_base + bias;
bounds[3] = F32AT(textfield, TF_YOFS) + F32AT(textfield, TF_Y0) +
F32AT(textfield, TF_H) + bias;
}
}
void iggy_textfield_select_all(void *display_object) {
void *tf = PTRAT(display_object, CTX_TF);
int32_t total = 0;
U8AT(tf, TF_SEL_ACTIVE) = 1;
U32AT(tf, TF_SEL_ANCHOR) = 0;
for (void *line = PTRAT(tf, TF_BLOCKS); line != NULL;
line = PTRAT(line, BLK_NEXT)) {
total += I32AT(line, BLK_COUNT);
}
I32AT(tf, TF_CARET) = total;
I32AT(tf, TF_SEL_END) = total;
}
void iggy_textfield_set_height(void *player, void *textfield, uint32_t height) {
(void)player;
if (U8AT(textfield, TF_LOCK_H) == 0) {
U32AT(textfield, TF_H) = height;
compute_bottom_line(textfield);
}
}

View File

@@ -0,0 +1,81 @@
#ifndef IGGY_RECONSTRUCTED_SWF_DRAW_TEXT_H
#define IGGY_RECONSTRUCTED_SWF_DRAW_TEXT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Reconstructed public surface of c:\projects\g\src\flash\swf_draw_text.c.
*
* The translation unit owns exactly two mutable data symbols and exports the
* 33 entry points that retail's swf_draw_text.obj exposes on every shipping
* platform (durango / orbis / ps3 / vita). Everything else the module needs -
* the renderer back-end, the font provider, the allocator, the rad-tools
* string helpers and the language/version/cursor globals - is imported.
*/
/* Owned mutable globals. */
extern int32_t iggy_lang;
extern uint8_t iggy_textfield_autoscroll_all;
/* Language / IME configuration. */
void IggySetLanguage(uint32_t lang);
void IggyPlayerSetIMEFontUTF16(void *player, const uint16_t *font_name_utf16,
int32_t namelen_in_2byte_words);
void IggyPlayerSetIMEFontUTF8(void *player, const char *font_name_utf8,
int32_t namelen_in_bytes);
/* Textfield content mutation. */
void iggy_set_textfield_text(void *player, void *textfield, const void *utf16,
int32_t length, int32_t append);
void iggy_set_textfield_htmltext(void *player, void *textfield,
const uint16_t *html, int32_t length);
void iggy_destroy_textfield_text(void *heap, void *textfield);
void iggy_do_replace_text(void);
/* Layout / geometry. */
void iggy_textfield_reflow(void *player, void *textfield);
void iggy_textfield_compute_info(void *player, void *textfield, int32_t *info);
void iggy_textfield_update_styles(void *player, void *textfield);
void iggy_textfield_objectspace_bounds(float *bounds, void *textfield);
void iggy_textfield_set_width(void *player, void *textfield, uint32_t width);
void iggy_textfield_set_height(void *player, void *textfield, uint32_t height);
void iggy_textfield_select_all(void *display_object);
/* Interactive editing. */
int32_t iggy_edittext_char(void *player, uint32_t codepoint);
int32_t iggy_edittext_keydown(void *player, uint32_t key);
void iggy_edittext_down(void *player, void *display_object, float x, float y);
void iggy_edittext_drag(void *player, void *display_object, float x, float y);
void iggy_edittext_cut(void *player);
int32_t iggy_edittext_copy_utf16(void *player, void *out, int32_t capacity);
int32_t iggy_edittext_copy_utf8(void *player, void *out, int32_t capacity);
void iggy_edittext_paste_utf16(void *player, const void *utf16, int32_t length);
void iggy_edittext_clear(void *player, void *textfield);
/* Glyph / font lookup. */
uint64_t iggy_get_unsafe_glyph_for_codepoint(void *font, uint32_t codepoint);
void iggy_get_fallback_font(void *player);
/* Rendering. */
void iggy_draw_text(void *player, void *object, void *color, void *filters,
uint32_t filter_count);
void iggy_draw_edittext(void);
void iggy_draw_edittext_filters(void *player, void *object, void *color,
void *matrix, void *filters, uint32_t filter_count);
void iggy_predraw_edittext(void *player, void *textfield, void *color);
void iggy_fontcache_text(void *player, void *textfield, float *matrix, void *a,
void *b);
void iggy_fontcache_edittext(void *player, void *textfield, float *matrix,
void *a, void *b);
void iggy_send_draw_text_stats(void *player, void *object, void *textfield);
void iggy_send_draw_edittext_stats(void *player, void *object, void *textfield);
#ifdef __cplusplus
}
#endif
#endif /* IGGY_RECONSTRUCTED_SWF_DRAW_TEXT_H */

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,198 @@
/*
* Host harness for the reconstructed swf_draw_text translation unit.
*
* swf_draw_text.c imports a large surface of not-yet-reconstructed renderer,
* font-provider, allocation and runtime symbols. This file provides host
* stubs for those imports so the DURANGO variant can be built into a loadable
* dylib and exercised for the behaviours that ARE fully recovered from retail
* (see tests/test_reconstructed_swf_draw_text.py).
*
* Conventions mirror tests/support/swf_draw_test_support.c:
* - Imported mutable globals get real backing storage here (the module OWNS
* iggy_lang / iggy_textfield_autoscroll_all, so those are NOT defined here).
* - An event log + reset hook gives runtime tests an observable channel.
* - Function stubs are benign; the runtime tests only drive code paths that
* do not depend on their return values.
*
* libc primitives (memcpy/memmove/memcmp/memset and the math/ctype calls
* sin/cos/floor/sqrt/isdigit) resolve against the host C library and are
* deliberately NOT stubbed here.
*/
#include <stdint.h>
#include <string.h>
/* ---- Imported mutable globals (module-external, owned by the harness). ---- */
int32_t iggy_cursor_flash_state;
int32_t iggy_textfield_position_version;
void *iggy_fallback_font;
void *iggy_tm_normal;
/* ---- Observable test channel. ---- */
int swf_draw_text_test_events[32];
int swf_draw_text_test_event_count;
void swf_draw_text_test_reset(void) {
memset(swf_draw_text_test_events, 0, sizeof(swf_draw_text_test_events));
swf_draw_text_test_event_count = 0;
iggy_cursor_flash_state = 0;
iggy_textfield_position_version = 0;
iggy_fallback_font = 0;
iggy_tm_normal = 0;
}
static void note(int event) {
if (swf_draw_text_test_event_count <
(int)(sizeof(swf_draw_text_test_events) / sizeof(swf_draw_text_test_events[0]))) {
swf_draw_text_test_events[swf_draw_text_test_event_count++] = event;
}
}
/* ---- Allocation / iggy_main plumbing. ---- */
static uint8_t swf_draw_text_test_arena[1 << 16];
static size_t swf_draw_text_test_arena_used;
static void *bump(size_t size) {
size = (size + 15u) & ~(size_t)15u;
if (swf_draw_text_test_arena_used + size > sizeof(swf_draw_text_test_arena)) {
return 0;
}
void *result = swf_draw_text_test_arena + swf_draw_text_test_arena_used;
swf_draw_text_test_arena_used += size;
return result;
}
void *iggy_arena_malloc(void *arena, uint64_t size) {
(void)arena;
note(1);
return bump((size_t)size);
}
void *iggy_malloc_fileline(void *heap, int32_t tag, uint64_t size, int32_t kind,
const char *file, int32_t line) {
(void)heap; (void)tag; (void)kind; (void)file; (void)line;
note(2);
return bump((size_t)size);
}
void *iggy_realloc(void *heap, void *ptr, uint64_t size) {
(void)heap; (void)ptr;
note(3);
return bump((size_t)size);
}
void iggy_free_alloc(void *heap, void *ptr) {
(void)heap; (void)ptr;
note(4);
}
void iggy_send_error(void *player, ...) {
(void)player;
note(5);
}
/* ---- as3vm name interning. ---- */
void *as3vm_find_intern_string(void *vm, const void *utf16, int32_t length) {
(void)vm; (void)utf16; (void)length;
return 0;
}
/* ---- Font provider / glyph queries. ---- */
uint64_t iggy_get_glyph_for_codepoint(void *font, uint32_t codepoint) {
(void)font; (void)codepoint;
return (uint64_t)-1;
}
void *iggy_get_glyph_metrics(void *font, uint32_t glyph) {
(void)font; (void)glyph;
return 0;
}
void *iggy_get_font_metrics(void *font) { (void)font; return 0; }
int32_t iggy_glyph_is_empty(void *font, uint32_t glyph) {
(void)font; (void)glyph;
return 1;
}
void *iggy_resolve_indirect_font(void *player, void *font) {
(void)player; (void)font;
return font;
}
/* ---- Character / text rendering back-end. ---- */
void iggy_draw_char(void *player, void *font, float size, int32_t glyph, float x,
float y, const void *color, const void *xform) {
(void)player; (void)font; (void)size; (void)glyph; (void)x; (void)y;
(void)color; (void)xform;
note(6);
}
void iggy_draw_text_bitmap(void *player, void *bitmap, const void *color,
float a, float b, float c, float d, int32_t flags) {
(void)player; (void)bitmap; (void)color; (void)a; (void)b; (void)c; (void)d;
(void)flags;
note(7);
}
void iggy_draw_text_border(void *player, const void *rect, const void *color) {
(void)player; (void)rect; (void)color;
note(8);
}
void iggy_draw_text_cursor(void *player, const void *rect, const void *color) {
(void)player; (void)rect; (void)color;
note(9);
}
void iggy_fontcache_char(void *player, void *font, float size, int32_t glyph) {
(void)player; (void)font; (void)size; (void)glyph;
note(10);
}
void iggy_get_char_renderstate(void *player, void *state) {
(void)player; (void)state;
}
void iggy_iggyexp_char(void *player, void *exporter, int32_t glyph) {
(void)player; (void)exporter; (void)glyph;
}
void iggy_fix_fast_text_effects(void *player, void *state) {
(void)player; (void)state;
}
void iggy_flush_textureinfo_for_iggyexp(void *player) { (void)player; }
/* ---- Buffered-rect batch API. ---- */
void iggy_start_buffered_rects(void *player) { (void)player; note(11); }
void iggy_end_buffered_rects(void *player) { (void)player; note(12); }
void iggy_lowlevel_set_xform_translation(void *player, float tx, float ty) {
(void)player; (void)tx; (void)ty;
}
/* ---- Text field translation helpers. ---- */
void *iggy_textfield_translate16(void *player, const void *utf16, int32_t length) {
(void)player; (void)utf16; (void)length;
return 0;
}
void iggy_textfield_translate_free(void *player, void *translated) {
(void)player; (void)translated;
}
/* ---- Rad tools runtime string / number helpers. ---- */
int32_t rrUTF16toUTF8(char *out, int32_t out_len, const uint16_t *in, int32_t in_len) {
(void)out; (void)out_len; (void)in; (void)in_len;
return 0;
}
int32_t rrUTF8toUTF16(uint16_t *out, int32_t out_len, const char *in, int32_t in_len) {
(void)out; (void)out_len; (void)in; (void)in_len;
return 0;
}
int32_t rr_utf16_read_decimal_ecmascript(const uint16_t **cursor, const uint16_t *end) {
(void)cursor; (void)end;
return 0;
}
int32_t rriswdigit(uint32_t ch) { return ch >= '0' && ch <= '9'; }
int32_t rrwcscmp(const uint16_t *a, const uint16_t *b) {
while (*a && *a == *b) { ++a; ++b; }
return (int32_t)*a - (int32_t)*b;
}
uint64_t rrwcslen(const uint16_t *s) {
const uint16_t *p = s;
while (*p) ++p;
return (uint64_t)(p - s);
}
long rrwcstol(const uint16_t *s, uint16_t **end, int32_t base) {
(void)base;
if (end) *end = (uint16_t *)s;
return 0;
}

View File

@@ -0,0 +1,172 @@
"""Runtime tests for the reconstructed swf_draw_text translation unit.
RED STATE: swf_draw_text.c / swf_draw_text.h do not exist yet, so setUpClass
raises "missing swf_draw_text runtime inputs". Every assertion below is
grounded in a specific line of iggy-decompiled/raw/durango/swf_draw_text.c
(cited inline) so a faithful DURANGO reconstruction turns these green.
Struct offsets are the 64-bit durango offsets read directly from the raw
decompilation; the dylib is built for the 64-bit host with the
IGGY_RECONSTRUCT_SWF_DRAW_TEXT_DURANGO code path selected, exactly like the
sibling swf_draw runtime test.
"""
import ctypes
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
SOURCE = RECONSTRUCTED / "swf_draw_text.c"
HEADER = RECONSTRUCTED / "swf_draw_text.h"
SUPPORT = ROOT / "tests/support/swf_draw_text_test_support.c"
class ReconstructedSwfDrawTextTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
missing = [str(path) for path in (SOURCE, HEADER, SUPPORT)
if not path.exists()]
if missing:
raise AssertionError(
"missing swf_draw_text runtime inputs: " + ", ".join(missing))
cls.temp = tempfile.TemporaryDirectory()
output = Path(cls.temp.name) / "swf_draw_text.dylib"
build = subprocess.run([
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
"-Werror", "-Wl,-undefined,dynamic_lookup",
"-DIGGY_RECONSTRUCT_SWF_DRAW_TEXT_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))
cls.lib.swf_draw_text_test_reset.argtypes = []
cls.lib.IggySetLanguage.argtypes = [ctypes.c_uint32]
cls.lib.iggy_edittext_clear.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
cls.lib.iggy_get_unsafe_glyph_for_codepoint.argtypes = [
ctypes.c_void_p, ctypes.c_uint32]
cls.lib.iggy_get_unsafe_glyph_for_codepoint.restype = ctypes.c_uint64
cls.lib.iggy_textfield_objectspace_bounds.argtypes = [
ctypes.POINTER(ctypes.c_float), ctypes.c_void_p]
cls.lib.iggy_textfield_select_all.argtypes = [ctypes.c_void_p]
cls.lib.iggy_textfield_set_height.argtypes = [
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint32]
@classmethod
def tearDownClass(cls):
cls.temp.cleanup()
def setUp(self):
self.lib.swf_draw_text_test_reset()
def test_set_language_writes_global(self):
# raw/durango:64-68 IggySetLanguage(lang){ iggy_lang = lang; }
for value in (0, 1, 2, 7):
self.lib.IggySetLanguage(value)
self.assertEqual(
ctypes.c_int32.in_dll(self.lib, "iggy_lang").value, value)
def test_get_unsafe_glyph_linear_search_returns_index_or_minus_one(self):
# raw/durango:2494-2532 scans (ushort*)(font+0x38) up to
# (ushort)(font+0x20) entries, returning the matching index else
# 0xffffffff.
font = (ctypes.c_uint8 * 0x40)()
table = (ctypes.c_uint16 * 3)(65, 66, 67)
ctypes.c_uint16.from_buffer(font, 0x20).value = 3
ctypes.c_void_p.from_buffer(font, 0x38).value = ctypes.addressof(table)
for codepoint, expected in ((65, 0), (66, 1), (67, 2), (99, 0xffffffff)):
self.assertEqual(
self.lib.iggy_get_unsafe_glyph_for_codepoint(font, codepoint),
expected, codepoint)
# count == 0 short-circuits before touching the table -> not found.
ctypes.c_uint16.from_buffer(font, 0x20).value = 0
self.assertEqual(
self.lib.iggy_get_unsafe_glyph_for_codepoint(font, 65), 0xffffffff)
def test_edittext_clear_writes_fixed_layout_defaults(self):
# raw/durango:1849-1857 iggy_edittext_clear sets a fixed set of fields.
field = (ctypes.c_uint8 * 0x300)()
player = (ctypes.c_uint8 * 0x40)()
ctypes.c_uint8.from_buffer(field, 0xe3).value = 0
self.lib.iggy_edittext_clear(player, field)
self.assertEqual(ctypes.c_uint32.from_buffer(field, 0x2be).value, 0x140000)
self.assertEqual(ctypes.c_uint32.from_buffer(field, 0x2c2).value, 0x630000)
self.assertEqual(ctypes.c_uint32.from_buffer(field, 0xfc).value, 0x10000)
self.assertEqual(ctypes.c_uint32.from_buffer(field, 0xf0).value, 0)
self.assertEqual(ctypes.c_uint64.from_buffer(field, 0xf4).value, 0)
# (field+0x100) = (field+0xe3 == 0); here 0xe3 == 0 -> 1.
self.assertEqual(ctypes.c_uint8.from_buffer(field, 0x100).value, 1)
# Flip the guard byte: 0x100 becomes 0.
self.lib.swf_draw_text_test_reset()
field2 = (ctypes.c_uint8 * 0x300)()
ctypes.c_uint8.from_buffer(field2, 0xe3).value = 1
self.lib.iggy_edittext_clear(player, field2)
self.assertEqual(ctypes.c_uint8.from_buffer(field2, 0x100).value, 0)
def test_objectspace_bounds_math_matches_retail(self):
# raw/durango:3152-3184. With position_version < 4 and (tf+0xec)==0 the
# integer bias fVar4 is 0; width/height sign selects which edge gets the
# extent added.
field = (ctypes.c_uint8 * 0x120)()
for offset, value in ((0x84, 10.0), (0x8c, 5.0), (0x94, 20.0),
(0x88, 3.0), (0x90, 4.0), (0x98, 30.0)):
ctypes.c_float.from_buffer(field, offset).value = value
ctypes.c_int32.in_dll(
self.lib, "iggy_textfield_position_version").value = 0
ctypes.c_uint8.from_buffer(field, 0xec).value = 0
bounds = (ctypes.c_float * 4)()
self.lib.iggy_textfield_objectspace_bounds(bounds, field)
# x0=0x84+0x8c=15 ; x1=+0x94=35 ; y0=0x90+0x88=7 ; y1=+0x98=37.
self.assertEqual(tuple(bounds), (15.0, 7.0, 35.0, 37.0))
# (tf+0xec)!=0 with version<4 -> bias = (int)0xfffffffe = -2 everywhere.
ctypes.c_uint8.from_buffer(field, 0xec).value = 1
biased = (ctypes.c_float * 4)()
self.lib.iggy_textfield_objectspace_bounds(biased, field)
self.assertEqual(tuple(biased), (13.0, 5.0, 33.0, 35.0))
# version >= 4 gates the bias back off even with (tf+0xec)!=0.
ctypes.c_int32.in_dll(
self.lib, "iggy_textfield_position_version").value = 4
gated = (ctypes.c_float * 4)()
self.lib.iggy_textfield_objectspace_bounds(gated, field)
self.assertEqual(tuple(gated), (15.0, 7.0, 35.0, 37.0))
def test_select_all_spans_every_line(self):
# raw/durango:3508-3524. Follows (obj+0x98) to the textfield, sets the
# selection-active flag, zeroes the anchor and sums (line+0x60) over the
# (line+0x68) list into both the caret and selection-end fields.
outer = (ctypes.c_uint8 * 0x120)()
field = (ctypes.c_uint8 * 0x120)()
line = (ctypes.c_uint8 * 0x80)()
ctypes.c_void_p.from_buffer(outer, 0x98).value = ctypes.addressof(field)
ctypes.c_void_p.from_buffer(field, 0x60).value = ctypes.addressof(line)
ctypes.c_int32.from_buffer(line, 0x60).value = 7 # chars on this line
# line+0x68 (next) stays 0 -> single-line list.
self.lib.iggy_textfield_select_all(outer)
self.assertEqual(ctypes.c_uint8.from_buffer(field, 0xeb).value, 1)
self.assertEqual(ctypes.c_int32.from_buffer(field, 0xd4).value, 0)
self.assertEqual(ctypes.c_int32.from_buffer(field, 0xd0).value, 7)
self.assertEqual(ctypes.c_int32.from_buffer(field, 0xd8).value, 7)
def test_set_height_stores_height_when_unlocked(self):
# raw/durango:3532-3543. When (tf+0xe8)==0 the height is written to
# (tf+0x98) and compute_bottom_line runs (raw/durango:243-284, which for
# a zero (tf+0xb4) line count simply clears (tf+0xb0)).
field = (ctypes.c_uint8 * 0x120)()
player = (ctypes.c_uint8 * 0x40)()
ctypes.c_uint8.from_buffer(field, 0xe8).value = 0
ctypes.c_int32.from_buffer(field, 0xb4).value = 0
self.lib.iggy_textfield_set_height(player, field, 999)
self.assertEqual(ctypes.c_uint32.from_buffer(field, 0x98).value, 999)
self.assertEqual(ctypes.c_uint32.from_buffer(field, 0xb0).value, 0)
# Locked (tf+0xe8)!=0 -> no write, height unchanged.
ctypes.c_uint8.from_buffer(field, 0xe8).value = 1
self.lib.iggy_textfield_set_height(player, field, 123)
self.assertEqual(ctypes.c_uint32.from_buffer(field, 0x98).value, 999)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,218 @@
"""Cross-platform contract tests for the reconstructed swf_draw_text module.
Mirrors tests/test_reconstructed_swf_draw_cross.py. RED until swf_draw_text.c /
swf_draw_text.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/swf_draw_text.obj):
EXPECTED_FUNCTIONS = `nm -g <obj> | grep ' T '`, '.'-stripped, identical on
durango/orbis/ps3/vita -> the 33 exported entry points.
EXPECTED_GLOBALS = the D/B/C data symbols this TU OWNS. `nm -g` on the
durango/orbis/vita objects lists exactly iggy_lang (C/B,
4 bytes) and iggy_textfield_autoscroll_all (C/B, 1 byte);
on ps3 the .opd function descriptors also appear as 'D'
but the only real .bss data are iggy_lang and
iggy_textfield_autoscroll_all. Note iggy_cursor_flash_state,
iggy_fallback_font, iggy_tm_normal and
iggy_textfield_position_version are IMPORTED (U), so they
are EXPECTED_IMPORTS, not globals.
EXPECTED_IMPORTS = intersection of `nm ' U '` (normalized: strip leading
'.', strip '__imp_') across all four retail objects
(39 iggy_/rr_/as3vm/mem* symbols, platform-invariant),
PLUS the libm/ctype calls the clang reconstruction emits.
Empirically (clang -ffreestanding -fno-builtin -O2, all
four targets) sin, cos, floor, sqrt and isdigit lower to
those exact uniform names, so they are part of the
invariant contract. Retail's platform-specific lowerings
(_Sin/_Sinx/floorf, _Getpctype/_Ctype/__ctype_ptr) and
toolchain scaffolding live in RUNTIME_ALLOWLISTS instead.
"""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
SOURCE = RECONSTRUCTED / "swf_draw_text.c"
HEADER = RECONSTRUCTED / "swf_draw_text.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 = {
"IggyPlayerSetIMEFontUTF16", "IggyPlayerSetIMEFontUTF8", "IggySetLanguage",
"iggy_destroy_textfield_text", "iggy_do_replace_text", "iggy_draw_edittext",
"iggy_draw_edittext_filters", "iggy_draw_text", "iggy_edittext_char",
"iggy_edittext_clear", "iggy_edittext_copy_utf16", "iggy_edittext_copy_utf8",
"iggy_edittext_cut", "iggy_edittext_down", "iggy_edittext_drag",
"iggy_edittext_keydown", "iggy_edittext_paste_utf16", "iggy_fontcache_edittext",
"iggy_fontcache_text", "iggy_get_fallback_font",
"iggy_get_unsafe_glyph_for_codepoint", "iggy_predraw_edittext",
"iggy_send_draw_edittext_stats", "iggy_send_draw_text_stats",
"iggy_set_textfield_htmltext", "iggy_set_textfield_text",
"iggy_textfield_compute_info", "iggy_textfield_objectspace_bounds",
"iggy_textfield_reflow", "iggy_textfield_select_all",
"iggy_textfield_set_height", "iggy_textfield_set_width",
"iggy_textfield_update_styles",
}
EXPECTED_GLOBALS = {
"iggy_lang", "iggy_textfield_autoscroll_all",
}
EXPECTED_IMPORTS = {
# 39 platform-invariant imports (intersection of retail ` U ` symbols).
"as3vm_find_intern_string", "iggy_arena_malloc", "iggy_cursor_flash_state",
"iggy_draw_char", "iggy_draw_text_bitmap", "iggy_draw_text_border",
"iggy_draw_text_cursor", "iggy_end_buffered_rects", "iggy_fallback_font",
"iggy_fix_fast_text_effects", "iggy_flush_textureinfo_for_iggyexp",
"iggy_fontcache_char", "iggy_free_alloc", "iggy_get_char_renderstate",
"iggy_get_font_metrics", "iggy_get_glyph_for_codepoint",
"iggy_get_glyph_metrics", "iggy_glyph_is_empty", "iggy_iggyexp_char",
"iggy_lowlevel_set_xform_translation", "iggy_malloc_fileline", "iggy_realloc",
"iggy_resolve_indirect_font", "iggy_send_error", "iggy_start_buffered_rects",
"iggy_textfield_position_version", "iggy_textfield_translate16",
"iggy_textfield_translate_free", "iggy_tm_normal", "memcmp", "memcpy",
"memmove", "memset", "rrUTF16toUTF8", "rrUTF8toUTF16",
"rr_utf16_read_decimal_ecmascript", "rriswdigit", "rrwcscmp", "rrwcslen",
"rrwcstol",
# libm / ctype that clang lowers uniformly across all four targets.
"sin", "cos", "floor", "sqrt", "isdigit",
}
RUNTIME_ALLOWLISTS = {
"durango": {"__GSHandlerCheck", "__ImageBase", "__chkstk",
"__report_rangecheckfailure", "__security_check_cookie",
"__security_cookie", "_fltused"},
"orbis": {"_GLOBAL_OFFSET_TABLE_", "strcpy", "strlen"},
"ps3": {"strcpy", "strlen"},
"vita": {"__aeabi_unwind_cpp_pr0", "__aeabi_unwind_cpp_pr1",
"__stack_chk_fail", "__stack_chk_guard", "abs", "strcpy", "strlen"},
}
def normalized_symbol(name):
name = name.removeprefix(".")
name = name.removeprefix("__imp_")
if name == "_setjmp":
return "setjmp"
return name
class ReconstructedSwfDrawTextCrossTests(unittest.TestCase):
def test_native_provenance_counts_source_path_and_inventory(self):
expected = {
"durango": (81, 7203), "orbis": (51, 7845),
"ps3": (51, 7908), "vita": (55, 7948),
}
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("swf_draw_text\t"))
self.assertEqual(int(row.split("\t")[2]), functions, platform)
raw = ROOT / f"iggy-decompiled/raw/{platform}/swf_draw_text.c"
self.assertEqual(len(raw.read_text(encoding="utf-8").splitlines()),
lines, platform)
obj = ROOT / f"work/iggy-native-decomp/{platform}-objects/swf_draw_text.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)
self.assertIn(r"c:\projects\g\src\flash\swf_draw_text.c",
strings.stdout, platform)
if SOURCE.exists():
cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text(encoding="utf-8")
self.assertIn("iggy_reconstructed_swf_draw_text_${platform}", cmake)
self.assertIn("add_library(${target} STATIC swf_draw_text.c)", cmake)
self.assertIn("IGGY_RECONSTRUCT_SWF_DRAW_TEXT_${platform_upper}", cmake)
inventory = (RECONSTRUCTED / "modules.tsv").read_text(encoding="utf-8")
for platform, functions in (("durango", 81), ("orbis", 51),
("ps3", 51), ("vita", 55)):
self.assertIn(
f"swf_draw_text\t{functions}\t33\t{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}")
self.assertIn(r"c:\\projects\\g\\src\\flash\\swf_draw_text.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 "swf_draw_text.h"\n'
'typedef void (*GenericFn)(void);\n'
'static GenericFn text_abis[] = {\n' + function_pointers + '};\n'
'int use_abis(void) {\n'
' (void)&iggy_lang;\n'
' (void)&iggy_textfield_autoscroll_all;\n'
' for (unsigned i = 0; i < sizeof(text_abis) / sizeof(text_abis[0]); ++i) {\n'
' if (!text_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"swf_draw_text_{platform}.o"
build = subprocess.run([
compiler, "-target", target, "-ffreestanding", "-fno-builtin",
"-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
*extra, f"-DIGGY_RECONSTRUCT_SWF_DRAW_TEXT_{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)
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"swf_draw_text_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_SWF_DRAW_TEXT_{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()