Files
neoLegacy-mac/tests/test_reconstructed_swf_fontcache.py
2026-07-13 10:33:12 -05:00

732 lines
36 KiB
Python

import ctypes
import os
from pathlib import Path
import shlex
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
SOURCE = RECONSTRUCTED / "swf_fontcache.c"
HEADER = RECONSTRUCTED / "swf_fontcache.h"
SUPPORT = ROOT / "tests/support/swf_fontcache_test_support.c"
VARIANTS = {
name: f"IGGY_RECONSTRUCT_SWF_FONTCACHE_{name.upper()}"
for name in ("durango", "orbis", "ps3", "vita")
}
if requested := os.environ.get("IGGY_TEST_VARIANTS"):
selected = {name.strip() for name in requested.split(",") if name.strip()}
VARIANTS = {name: define for name, define in VARIANTS.items()
if name in selected}
OFFSETS = {
"durango": {"texture": 0x468, "width": 0x470, "height": 0x474,
"rows": 0x478, "row_count": 0x1E78, "config_width": 0x1EA4,
"config_height": 0x1EA8, "oversample": 0x1EAC,
"ratio_low": 0x1EB0, "ratio_high": 0x1EB4, "frame": 0x2878},
"orbis": {"texture": 0x468, "width": 0x470, "height": 0x474,
"rows": 0x478, "row_count": 0x1E78, "config_width": 0x1EA4,
"config_height": 0x1EA8, "oversample": 0x1EAC,
"ratio_low": 0x1EB0, "ratio_high": 0x1EB4, "frame": 0x2878},
"ps3": {"texture": 0x318, "width": 0x31C, "height": 0x320,
"rows": 0x324, "row_count": 0x1924, "config_width": 0x1948,
"config_height": 0x194C, "oversample": 0x1950,
"ratio_low": 0x1954, "ratio_high": 0x1958, "frame": 0x2198},
"vita": {"texture": 0x318, "width": 0x31C, "height": 0x320,
"rows": 0x324, "row_count": 0x1924, "config_width": 0x1948,
"config_height": 0x194C, "oversample": 0x1950,
"ratio_low": 0x1954, "ratio_high": 0x1958, "frame": 0x2198},
}
class FontCacheCharacter(ctypes.Structure):
_fields_ = [
("font", ctypes.c_void_p), ("glyph", ctypes.c_int32),
("scale_x", ctypes.c_float), ("scale_y", ctypes.c_float),
]
class BitmapCacheRecord(ctypes.Structure):
_fields_ = [
("glyph_owner", ctypes.c_void_p), ("glyph_next", ctypes.c_void_p),
("row_owner", ctypes.c_void_p), ("row_next", ctypes.c_void_p),
("uv", ctypes.c_float * 4), ("scale_x", ctypes.c_float),
("scale_y", ctypes.c_float), ("rectangle", ctypes.c_float * 4),
("last_frame", ctypes.c_int32), ("width", ctypes.c_uint16),
("height", ctypes.c_uint16), ("alternate_key", ctypes.c_float),
("range_low", ctypes.c_float), ("range_high", ctypes.c_float),
("slot", ctypes.c_int16), ("reserved", ctypes.c_uint16),
]
class FontCacheRow(ctypes.Structure):
_fields_ = [
("reserved_links", ctypes.c_size_t * 2),
("record_owner", ctypes.c_void_p), ("record_next", ctypes.c_void_p),
("reserved", ctypes.c_uint8 * 0x40),
("y", ctypes.c_uint16), ("height", ctypes.c_uint16),
("cursor_x", ctypes.c_uint16), ("reserved_tail", ctypes.c_uint16),
]
class ReconstructedSwfFontcacheTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
missing = [str(path) for path in (SOURCE, HEADER, SUPPORT) if not path.exists()]
if missing:
raise AssertionError("missing fontcache runtime inputs: " + ", ".join(missing))
cls.temp = tempfile.TemporaryDirectory()
build = Path(os.environ.get("IGGY_TEST_BUILD_DIR", cls.temp.name))
build.mkdir(parents=True, exist_ok=True)
cls.libs = {}
for platform, define in VARIANTS.items():
output = build / f"swf_fontcache_{platform}.dylib"
result = subprocess.run([
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
"-Werror", f"-D{define}=1", "-I", str(RECONSTRUCTED),
str(SOURCE), str(SUPPORT),
*shlex.split(os.environ.get("IGGY_TEST_CFLAGS", "")),
"-o", str(output),
], capture_output=True, text=True)
if result.returncode:
raise AssertionError((platform, result.stdout + result.stderr))
library = ctypes.CDLL(str(output))
cls.configure(library)
cls.libs[platform] = library
@classmethod
def tearDownClass(cls):
cls.temp.cleanup()
@staticmethod
def configure(lib):
lib.fontcache_test_reset.argtypes = []
lib.fontcache_test_disable_gdraw.argtypes = []
lib.fontcache_test_disable_gdraw_end.argtypes = []
lib.iggy_fontcache_init.argtypes = [ctypes.c_void_p]
lib.iggy_fontcache_flush.argtypes = [ctypes.c_void_p]
lib.iggy_fontcache_set_oversample.argtypes = [ctypes.c_void_p, ctypes.c_int32]
lib.iggy_fontcache_contains_old_characters.argtypes = [ctypes.c_void_p]
lib.iggy_fontcache_contains_old_characters.restype = ctypes.c_int32
lib.iggy_fontcache_get_existing_if_any.argtypes = [
ctypes.c_void_p, ctypes.POINTER(FontCacheCharacter)]
lib.iggy_fontcache_get_existing_if_any.restype = ctypes.c_void_p
lib.iggy_fontcache_try_cache_character.argtypes = [
ctypes.c_void_p, ctypes.POINTER(FontCacheCharacter)]
lib.iggy_fontcache_try_cache_character.restype = ctypes.c_int32
def setUp(self):
for lib in self.libs.values():
lib.fontcache_test_reset()
@staticmethod
def integer(lib, name):
return ctypes.c_int.in_dll(lib, name).value
@staticmethod
def player(platform, width=64, height=64, frame=10):
player = (ctypes.c_uint8 * 0x4000)()
offsets = OFFSETS[platform]
ctypes.c_int32.from_buffer(player, offsets["config_width"]).value = width
ctypes.c_int32.from_buffer(player, offsets["config_height"]).value = height
ctypes.c_int32.from_buffer(player, offsets["frame"]).value = frame
ctypes.c_float.from_buffer(player, offsets["ratio_low"]).value = 0.5
ctypes.c_float.from_buffer(player, offsets["ratio_high"]).value = 2.0
return player
@staticmethod
def texture_value(platform, width=64, low=0x12345678):
if platform in ("ps3", "vita"):
return (width << 32) | low
return low
@staticmethod
def font(heads, glyph_count=1):
font = (ctypes.c_uint8 * 0x100)()
ctypes.c_int32.from_buffer(font, 0x34).value = glyph_count
ctypes.c_void_p.from_buffer(font, 0x40).value = ctypes.addressof(heads)
return font
@staticmethod
def install_provider(lib, font, with_range=False):
ctypes.c_void_p.from_buffer(font, 0xE0).value = ctypes.cast(
lib.fontcache_test_bitmap_provider, ctypes.c_void_p).value
if with_range:
ctypes.c_void_p.from_buffer(font, 0xE8).value = ctypes.cast(
lib.fontcache_test_range_provider, ctypes.c_void_p).value
@staticmethod
def initialize_row(player, platform, index, y, height, cursor):
row = FontCacheRow.from_buffer(
player, OFFSETS[platform]["rows"] +
index * ctypes.sizeof(FontCacheRow))
row.record_owner = ctypes.addressof(row)
row.record_next = ctypes.addressof(row)
row.y = y
row.height = height
row.cursor_x = cursor
return row
def test_init_success_failure_and_idempotence(self):
for platform, lib in self.libs.items():
player = self.player(platform, 64, 32)
offsets = OFFSETS[platform]
texture = self.texture_value(platform, 64)
ctypes.c_size_t.in_dll(lib, "fontcache_test_end_texture").value = texture
lib.iggy_fontcache_init(player)
self.assertEqual(ctypes.c_size_t.from_buffer(
player, offsets["texture"]).value, texture, platform)
self.assertEqual(ctypes.c_int32.from_buffer(
player, offsets["width"]).value, 64, platform)
self.assertEqual(ctypes.c_int32.from_buffer(
player, offsets["height"]).value, 32, platform)
self.assertEqual(self.integer(lib, "fontcache_test_begin_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_end_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_stats_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_temp_free_calls"), 1,
platform)
lib.fontcache_test_reset()
no_gdraw = self.player(platform)
lib.fontcache_test_disable_gdraw()
lib.iggy_fontcache_init(no_gdraw)
self.assertEqual(self.integer(lib, "fontcache_test_temp_alloc_calls"),
1, platform)
self.assertEqual(self.integer(lib, "fontcache_test_begin_calls"), 0,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_stats_calls"), 1,
platform)
lib.fontcache_test_reset()
no_end = self.player(platform)
lib.fontcache_test_disable_gdraw_end()
lib.iggy_fontcache_init(no_end)
self.assertEqual(self.integer(lib, "fontcache_test_begin_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_end_calls"), 0,
platform)
lib.iggy_fontcache_init(player)
self.assertEqual(self.integer(lib, "fontcache_test_begin_calls"), 1,
platform)
lib.fontcache_test_reset()
failed = self.player(platform)
ctypes.c_int.in_dll(
lib, "fontcache_test_begin_result").value = 0
lib.iggy_fontcache_init(failed)
failed_texture = ctypes.c_size_t.from_buffer(
failed, offsets["texture"]).value
self.assertEqual(failed_texture & 0xFFFFFFFF, 0, platform)
self.assertEqual(self.integer(lib, "fontcache_test_begin_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_end_calls"), 0,
platform)
lib.fontcache_test_reset()
no_temp = self.player(platform)
ctypes.c_int.in_dll(
lib, "fontcache_test_temp_alloc_result").value = 0
lib.iggy_fontcache_init(no_temp)
self.assertEqual(self.integer(lib, "fontcache_test_begin_calls"), 0,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_stats_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_temp_free_calls"), 1,
platform)
def test_flush_unlinks_glyph_and_row_lists_and_frees_texture(self):
for platform, lib in self.libs.items():
player = self.player(platform)
offsets = OFFSETS[platform]
texture = self.texture_value(platform)
ctypes.c_size_t.from_buffer(player, offsets["texture"]).value = texture
ctypes.c_int32.from_buffer(player, offsets["row_count"]).value = 1
row = FontCacheRow.from_buffer(player, offsets["rows"])
first = BitmapCacheRecord()
second = BitmapCacheRecord()
heads = (ctypes.c_void_p * 1)()
heads[0] = ctypes.addressof(first)
first.glyph_owner = ctypes.addressof(heads)
first.glyph_next = ctypes.addressof(second)
second.glyph_owner = (ctypes.addressof(first) +
BitmapCacheRecord.glyph_next.offset)
first.row_next = ctypes.addressof(second)
second.row_next = ctypes.addressof(row)
row.record_owner = ctypes.addressof(second)
row.record_next = ctypes.addressof(first)
lib.iggy_fontcache_flush(player)
self.assertIsNone(heads[0], platform)
self.assertEqual(self.integer(lib, "fontcache_test_free_alloc_calls"), 2,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_free_texture_calls"),
1, platform)
self.assertEqual(ctypes.c_size_t.from_buffer(
player, offsets["texture"]).value & 0xFFFFFFFF, 0, platform)
self.assertEqual(ctypes.c_int32.from_buffer(
player, offsets["row_count"]).value, 0, platform)
def test_oversample_flushes_and_sets_exact_ratio_bits(self):
for platform, lib in self.libs.items():
player = self.player(platform)
offsets = OFFSETS[platform]
ctypes.c_size_t.from_buffer(player, offsets["texture"]).value = \
self.texture_value(platform)
lib.iggy_fontcache_set_oversample(player, 7)
self.assertEqual(ctypes.c_int32.from_buffer(
player, offsets["oversample"]).value, 1, platform)
self.assertEqual(ctypes.c_uint32.from_buffer(
player, offsets["ratio_low"]).value, 0x3F68BA2E, platform)
self.assertEqual(ctypes.c_uint32.from_buffer(
player, offsets["ratio_high"]).value, 0x3F8CCCCD, platform)
self.assertEqual(self.integer(lib, "fontcache_test_free_texture_calls"),
1, platform)
lib.iggy_fontcache_set_oversample(player, 0)
self.assertEqual(ctypes.c_int32.from_buffer(
player, offsets["oversample"]).value, 0, platform)
self.assertEqual(ctypes.c_uint32.from_buffer(
player, offsets["ratio_high"]).value, 0x3F800000, platform)
def test_normal_and_alternate_lookup_boundaries_update_frame(self):
for platform, lib in self.libs.items():
player = self.player(platform, frame=77)
normal = BitmapCacheRecord(scale_x=1.0, scale_y=6.0)
alternate = BitmapCacheRecord(
alternate_key=1.0, range_low=4.0, range_high=5.0)
normal.glyph_next = ctypes.addressof(alternate)
heads = (ctypes.c_void_p * 1)(ctypes.addressof(normal))
font = self.font(heads)
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.0, 3.0)
found = lib.iggy_fontcache_get_existing_if_any(
player, ctypes.byref(character))
self.assertEqual(found, ctypes.addressof(normal), platform)
self.assertEqual(normal.last_frame, 77, platform)
normal.scale_x = 100.0
normal.scale_y = 100.0
character.scale_x = 4.0
found = lib.iggy_fontcache_get_existing_if_any(
player, ctypes.byref(character))
self.assertEqual(found, ctypes.addressof(alternate), platform)
character.scale_x = 5.0
self.assertEqual(lib.iggy_fontcache_get_existing_if_any(
player, ctypes.byref(character)), ctypes.addressof(alternate),
platform)
character.scale_x = 5.01
self.assertIsNone(lib.iggy_fontcache_get_existing_if_any(
player, ctypes.byref(character)), platform)
def test_contains_old_characters_checks_every_row_record(self):
for platform, lib in self.libs.items():
player = self.player(platform, frame=22)
offsets = OFFSETS[platform]
ctypes.c_int32.from_buffer(player, offsets["row_count"]).value = 1
row = FontCacheRow.from_buffer(player, offsets["rows"])
row.record_next = ctypes.addressof(row)
self.assertEqual(lib.iggy_fontcache_contains_old_characters(player), 0,
platform)
record = BitmapCacheRecord(last_frame=22,
row_next=ctypes.addressof(row))
row.record_next = ctypes.addressof(record)
self.assertEqual(lib.iggy_fontcache_contains_old_characters(player), 0,
platform)
record.last_frame = 21
self.assertEqual(lib.iggy_fontcache_contains_old_characters(player), 1,
platform)
def test_invalid_and_empty_characters_are_not_cached(self):
for platform, lib in self.libs.items():
player = self.player(platform)
heads = (ctypes.c_void_p * 3)()
font = self.font(heads, glyph_count=1)
ctypes.c_void_p.from_buffer(font, 0x40).value = \
ctypes.addressof(heads) + ctypes.sizeof(ctypes.c_void_p)
invalid = FontCacheCharacter(ctypes.addressof(font), -1, 1.0, 1.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(invalid)), 1, platform)
invalid.glyph = 1
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(invalid)), 1, platform)
ctypes.c_int.in_dll(lib, "fontcache_test_glyph_empty").value = 1
invalid.glyph = 0
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(invalid)), 1, platform)
self.assertEqual(self.integer(lib, "fontcache_test_malloc_calls"), 0,
platform)
def test_direct_bitmap_provider_allocates_row_then_cache_hits(self):
for platform, lib in self.libs.items():
player = self.player(platform, frame=45)
texture = self.texture_value(platform, 64, 0x9876)
ctypes.c_size_t.in_dll(lib, "fontcache_test_end_texture").value = texture
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
ctypes.c_void_p.from_buffer(font, 0xE0).value = ctypes.cast(
lib.fontcache_test_bitmap_provider, ctypes.c_void_p).value
ctypes.c_void_p.from_buffer(font, 0xE8).value = ctypes.cast(
lib.fontcache_test_range_provider, ctypes.c_void_p).value
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.0, 3.0)
result = lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character))
self.assertEqual(result, 0, platform)
self.assertEqual(self.integer(lib, "fontcache_test_provider_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_range_provider_calls"),
1, platform)
self.assertEqual(self.integer(lib, "fontcache_test_update_calls"), 1,
platform)
self.assertEqual(ctypes.c_int32.from_buffer(
player, OFFSETS[platform]["row_count"]).value, 1, platform)
row = FontCacheRow.from_buffer(player, OFFSETS[platform]["rows"])
self.assertEqual((row.y, row.height, row.cursor_x), (1, 6, 4),
platform)
self.assertTrue(heads[0], platform)
record = BitmapCacheRecord.from_address(heads[0])
self.assertEqual((record.width, record.height, record.slot), (2, 3, 7),
platform)
self.assertEqual(record.last_frame, 45, platform)
self.assertAlmostEqual(record.range_low, 1.0, places=6, msg=platform)
self.assertAlmostEqual(record.range_high, 4.0, places=6, msg=platform)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 0, platform)
self.assertEqual(self.integer(lib, "fontcache_test_provider_calls"), 1,
platform)
no_heads = (ctypes.c_void_p * 1)()
no_heads_font = self.font(no_heads)
ctypes.c_void_p.from_buffer(no_heads_font, 0x40).value = 0
missing = FontCacheCharacter(
ctypes.addressof(no_heads_font), 0, 1.0, 1.0)
self.assertIsNone(lib.iggy_fontcache_get_existing_if_any(
player, ctypes.byref(missing)), platform)
def test_provider_slot_sign_extension_and_native_callback_abi(self):
for platform, lib in self.libs.items():
player = self.player(platform)
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
self.install_provider(lib, font)
ctypes.c_int.in_dll(
lib, "fontcache_test_provider_slot").value = 0xFF
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.5, 3.75)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 0, platform)
record = BitmapCacheRecord.from_address(heads[0])
self.assertEqual(record.slot, -1, platform)
self.assertAlmostEqual(ctypes.c_float.in_dll(
lib, "fontcache_test_last_provider_scale").value,
3.75, places=6, msg=platform)
def test_provider_rejections_and_allocation_failures(self):
for platform, lib in self.libs.items():
for condition in ("no_pixels", "zero_width", "zero_height"):
lib.fontcache_test_reset()
player = self.player(platform)
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
self.install_provider(lib, font)
if condition == "no_pixels":
ctypes.c_int.in_dll(
lib, "fontcache_test_provider_has_pixels").value = 0
elif condition == "zero_width":
ctypes.c_int.in_dll(
lib, "fontcache_test_provider_width").value = 0
else:
ctypes.c_int.in_dll(
lib, "fontcache_test_provider_height").value = 0
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.0, 3.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 1, (platform, condition))
self.assertEqual(self.integer(lib, "fontcache_test_malloc_calls"),
0, (platform, condition))
for failure_call in (1, 2):
lib.fontcache_test_reset()
player = self.player(platform)
ctypes.c_size_t.in_dll(
lib, "fontcache_test_end_texture").value = \
self.texture_value(platform)
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
self.install_provider(lib, font)
ctypes.c_int.in_dll(
lib, "fontcache_test_malloc_fail_on").value = failure_call
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.0, 3.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 1,
(platform, failure_call))
self.assertIsNone(heads[0], (platform, failure_call))
if failure_call == 2:
self.assertEqual(self.integer(
lib, "fontcache_test_free_alloc_calls"), 1, platform)
@staticmethod
def outline_path():
path = (ctypes.c_float * 12)(
0, 0, 0, 0, 0, 0,
2, 3, -1, 4, 0, 0)
raw = (ctypes.c_uint8 * ctypes.sizeof(path)).from_buffer(path)
raw[6 * ctypes.sizeof(ctypes.c_float) + 16] = 3
return path
def test_substituted_outline_provider_fallback_raster_and_cleanup(self):
for platform, lib in self.libs.items():
player = self.player(platform, width=128, height=128)
ctypes.c_int32.from_buffer(
player, OFFSETS[platform]["oversample"]).value = 1
ctypes.c_size_t.in_dll(
lib, "fontcache_test_end_texture").value = \
self.texture_value(platform, 128)
path = self.outline_path()
ctypes.c_size_t.in_dll(
lib, "fontcache_test_shape_path").value = ctypes.addressof(path)
ctypes.c_int.in_dll(lib, "fontcache_test_shape_count").value = 2
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
ctypes.c_void_p.from_buffer(font, 0xC8).value = 1
self.install_provider(lib, font)
ctypes.c_int.in_dll(lib, "fontcache_test_provider_result").value = 0
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.0, 3.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 0, platform)
self.assertEqual(self.integer(lib, "fontcache_test_provider_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_get_shape_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_rasterize_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_free_shape_calls"),
1, platform)
record = BitmapCacheRecord.from_address(heads[0])
self.assertEqual(record.alternate_key, 0.0, platform)
self.assertAlmostEqual(record.scale_x, 2.0, places=6, msg=platform)
self.assertAlmostEqual(record.scale_y, 3.0, places=6, msg=platform)
raster_stride = self.integer(
lib, "fontcache_test_last_raster_stride")
allocation_base = (ctypes.c_size_t * 16).in_dll(
lib, "fontcache_test_freed_allocations")[0]
raster_pixels = ctypes.c_size_t.in_dll(
lib, "fontcache_test_last_raster_pixels").value
self.assertEqual(
raster_pixels, allocation_base + 2 * (raster_stride + 1),
platform)
lib.fontcache_test_reset()
empty_heads = (ctypes.c_void_p * 1)()
empty_font = self.font(empty_heads)
ctypes.c_void_p.from_buffer(empty_font, 0xC8).value = 1
empty = FontCacheCharacter(
ctypes.addressof(empty_font), 0, 1.0, 1.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
self.player(platform), ctypes.byref(empty)), 1, platform)
self.assertEqual(self.integer(lib, "fontcache_test_free_shape_calls"),
1, platform)
lib.fontcache_test_reset()
ctypes.c_size_t.in_dll(
lib, "fontcache_test_shape_path").value = ctypes.addressof(path)
ctypes.c_int.in_dll(lib, "fontcache_test_shape_count").value = 2
ctypes.c_int.in_dll(
lib, "fontcache_test_malloc_fail_on").value = 1
failed_heads = (ctypes.c_void_p * 1)()
failed_font = self.font(failed_heads)
ctypes.c_void_p.from_buffer(failed_font, 0xC8).value = 1
failed_character = FontCacheCharacter(
ctypes.addressof(failed_font), 0, 2.0, 3.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
self.player(platform), ctypes.byref(failed_character)), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_free_shape_calls"),
1, platform)
def test_embedded_outline_and_missing_embedded_sources(self):
for platform, lib in self.libs.items():
player = self.player(platform, width=128, height=128)
ctypes.c_size_t.in_dll(
lib, "fontcache_test_end_texture").value = \
self.texture_value(platform, 128)
path = self.outline_path()
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
ctypes.c_int32.from_buffer(font, 0x18).value = 1
root = (ctypes.c_uint8 * 0x40)()
table = (ctypes.c_uint8 * 0x20)()
source = (ctypes.c_uint8 * 0x30)()
ctypes.c_void_p.from_buffer(font, 0).value = ctypes.addressof(root)
ctypes.c_void_p.from_buffer(root, 0x30).value = ctypes.addressof(table)
ctypes.c_void_p.from_buffer(table, 0x18).value = ctypes.addressof(source)
ctypes.c_void_p.from_buffer(source, 0x10).value = ctypes.addressof(path)
ctypes.c_int32.from_buffer(source, 0x18).value = 2
character = FontCacheCharacter(
ctypes.addressof(font), 0, 1.0, 1.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 0, platform)
self.assertEqual(self.integer(lib, "fontcache_test_rasterize_calls"), 1,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_get_shape_calls"), 0,
platform)
self.assertEqual(self.integer(lib, "fontcache_test_free_shape_calls"), 0,
platform)
for missing in ("root", "table", "source"):
lib.fontcache_test_reset()
missing_heads = (ctypes.c_void_p * 1)()
missing_font = self.font(missing_heads)
ctypes.c_int32.from_buffer(missing_font, 0x18).value = 1
missing_root = (ctypes.c_uint8 * 0x40)()
missing_table = (ctypes.c_uint8 * 0x20)()
if missing != "root":
ctypes.c_void_p.from_buffer(missing_font, 0).value = \
ctypes.addressof(missing_root)
if missing == "source":
ctypes.c_void_p.from_buffer(missing_root, 0x30).value = \
ctypes.addressof(missing_table)
missing_character = FontCacheCharacter(
ctypes.addressof(missing_font), 0, 1.0, 1.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
self.player(platform), ctypes.byref(missing_character)), 1,
(platform, missing))
def test_row_selection_bottom_cap_and_eviction_rejections(self):
for platform in (name for name in ("durango", "orbis")
if name in self.libs):
lib = self.libs[platform]
offsets = OFFSETS[platform]
player = self.player(platform)
ctypes.c_size_t.from_buffer(player, offsets["texture"]).value = 0x1111
ctypes.c_int32.from_buffer(player, offsets["width"]).value = 64
ctypes.c_int32.from_buffer(player, offsets["height"]).value = 64
ctypes.c_int32.from_buffer(player, offsets["row_count"]).value = 2
first = self.initialize_row(player, platform, 0, 1, 7, 1)
second = self.initialize_row(player, platform, 1, 10, 5, 1)
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
self.install_provider(lib, font)
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.0, 3.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 0, platform)
self.assertEqual((first.cursor_x, second.cursor_x), (1, 4), platform)
lib.fontcache_test_reset()
player = self.player(platform)
ctypes.c_size_t.from_buffer(player, offsets["texture"]).value = 0x2222
ctypes.c_int32.from_buffer(player, offsets["width"]).value = 64
ctypes.c_int32.from_buffer(player, offsets["height"]).value = 64
ctypes.c_int32.from_buffer(player, offsets["row_count"]).value = 1
self.initialize_row(player, platform, 0, 1, 9, 1)
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
self.install_provider(lib, font)
character.font = ctypes.addressof(font)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 0, platform)
self.assertEqual(ctypes.c_int32.from_buffer(
player, offsets["row_count"]).value, 2, platform)
lib.fontcache_test_reset()
player = self.player(platform)
ctypes.c_size_t.from_buffer(player, offsets["texture"]).value = 0x3333
ctypes.c_int32.from_buffer(player, offsets["width"]).value = 64
ctypes.c_int32.from_buffer(player, offsets["height"]).value = 64
ctypes.c_int32.from_buffer(player, offsets["row_count"]).value = 4
rows = [
self.initialize_row(player, platform, 0, 1, 3, 64),
self.initialize_row(player, platform, 1, 10, 8, 64),
self.initialize_row(player, platform, 2, 20, 4, 64),
self.initialize_row(player, platform, 3, 63, 4, 64),
]
records = [
BitmapCacheRecord(last_frame=4, width=2),
BitmapCacheRecord(last_frame=4, width=2),
BitmapCacheRecord(last_frame=4, width=1),
BitmapCacheRecord(last_frame=4, width=8),
]
for row, record in zip(rows, records):
row.record_next = ctypes.addressof(record)
record.row_next = ctypes.addressof(row)
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
self.install_provider(lib, font)
character.font = ctypes.addressof(font)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 2, platform)
def test_full_atlas_reuses_old_record_and_reports_full_without_candidate(self):
for platform in (name for name in ("durango", "orbis")
if name in self.libs):
lib = self.libs[platform]
player = self.player(platform, width=8, height=8, frame=5)
offsets = OFFSETS[platform]
ctypes.c_size_t.from_buffer(player, offsets["texture"]).value = 0x9999
ctypes.c_int32.from_buffer(player, offsets["width"]).value = 8
ctypes.c_int32.from_buffer(player, offsets["height"]).value = 8
ctypes.c_int32.from_buffer(player, offsets["row_count"]).value = 64
rows = []
for index in range(64):
row = FontCacheRow.from_buffer(
player, offsets["rows"] + index * ctypes.sizeof(FontCacheRow))
row.record_next = ctypes.addressof(row)
row.cursor_x = 8
rows.append(row)
stale = BitmapCacheRecord(
last_frame=4, width=2, height=3,
row_next=ctypes.addressof(rows[0]))
old_head = ctypes.c_void_p(ctypes.addressof(stale))
stale.glyph_owner = ctypes.addressof(old_head)
rows[0].height = 4
rows[0].record_next = ctypes.addressof(stale)
heads = (ctypes.c_void_p * 1)()
font = self.font(heads)
ctypes.c_void_p.from_buffer(font, 0xE0).value = ctypes.cast(
lib.fontcache_test_bitmap_provider, ctypes.c_void_p).value
character = FontCacheCharacter(
ctypes.addressof(font), 0, 2.0, 3.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(character)), 0, platform)
self.assertEqual(heads[0], ctypes.addressof(stale), platform)
self.assertIsNone(old_head.value, platform)
fresh_heads = (ctypes.c_void_p * 1)()
fresh_font = self.font(fresh_heads)
ctypes.c_void_p.from_buffer(fresh_font, 0xE0).value = ctypes.cast(
lib.fontcache_test_bitmap_provider, ctypes.c_void_p).value
fresh = FontCacheCharacter(
ctypes.addressof(fresh_font), 0, 2.0, 3.0)
self.assertEqual(lib.iggy_fontcache_try_cache_character(
player, ctypes.byref(fresh)), 2, platform)
small = self.player(platform, width=4, height=4)
ctypes.c_size_t.from_buffer(small, offsets["texture"]).value = 0x7777
ctypes.c_int32.from_buffer(small, offsets["width"]).value = 4
ctypes.c_int32.from_buffer(small, offsets["height"]).value = 4
self.assertEqual(lib.iggy_fontcache_try_cache_character(
small, ctypes.byref(fresh)), 1, platform)
if __name__ == "__main__":
unittest.main()