Skip to content

Commit 70072ae

Browse files
committed
Refactor backend tests to use provision_fonts callback and font roles
1 parent e9006c8 commit 70072ae

5 files changed

Lines changed: 708 additions & 135 deletions

File tree

demo/demo.cpp

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,13 @@ static void backend_test_reset_surfaces()
922922
glFinish();
923923
}
924924

925+
static void backend_test_finalise_state()
926+
{
927+
ve_fontcache_reset_transient_test_state( &cache );
928+
clear_backend_test_surfaces( true );
929+
glFinish();
930+
}
931+
925932
#ifdef VE_FONTCACHE_FREETYPE_RASTERISATION
926933
static void backend_test_ensure_cpu_atlas_page( size_t atlas_page )
927934
{
@@ -1110,7 +1117,6 @@ static int run_backend_test_mode()
11101117
cache = ve_fontcache();
11111118
ve_fontcache_init( &cache, use_freetype );
11121119
ve_fontcache_configure_snap( &cache, window_size.width, window_size.height );
1113-
load_demo_fonts();
11141120

11151121
std::vector< uint8_t > huge_buffer;
11161122
std::vector< uint8_t > fallback_print_buffer;
@@ -1138,33 +1144,69 @@ static int run_backend_test_mode()
11381144
if ( !use_freetype ) {
11391145
apply_font_fallbacks();
11401146
}
1141-
ve_font_id huge_test_font = load_demo_font( &cache, "fonts/NotoSansJP-Light.otf", huge_buffer, 200.0f );
1142-
if ( huge_test_font < 0 && !use_freetype ) {
1143-
huge_test_font = load_demo_font( &cache, "fonts/OpenSans-Regular.ttf", huge_buffer, 200.0f );
1144-
}
1145-
normalize_demo_font_ids( &huge_test_font );
1146-
1147-
bool fonts_ready =
1148-
print_font >= 0
1149-
&& huge_test_font >= 0;
1150-
if ( !fonts_ready ) {
1151-
printf( "VEFontCache backend tests [%s] failed to load one or more demo fonts.\n", mode_name );
1152-
ve_fontcache_shutdown( &cache );
1153-
return false;
1154-
}
11551147

11561148
clear_backend_test_surfaces( use_freetype ? false : true );
11571149

11581150
ve_fontcache_backend_test_options options;
11591151
options.cache = &cache;
1160-
options.font = print_font;
1161-
options.secondary_font = title_font >= 0 ? title_font : print_font;
1162-
options.small_font = small_font >= 0 ? small_font : print_font;
1163-
options.latin_font = demo_grid3_font >= 0 ? demo_grid3_font : options.secondary_font;
1164-
options.cjk_font = demo_grid2_font >= 0 ? demo_grid2_font : print_font;
1165-
options.huge_font = huge_test_font;
1166-
options.arabic_font = use_freetype ? demo_arabic_font : -1;
1167-
options.hebrew_font = use_freetype ? demo_hebrew_font : -1;
1152+
options.provision_fonts = [&, use_freetype](
1153+
ve_fontcache* target_cache,
1154+
const ve_fontcache_backend_test_font_spec* roles,
1155+
size_t role_count ) -> ve_fontcache_backend_test_font_set {
1156+
ve_fontcache_backend_test_font_set font_set;
1157+
if ( target_cache != &cache ) {
1158+
for ( size_t i = 0; i < role_count; i++ ) {
1159+
font_set.mark_unavailable( roles[ i ].role, "demo test runner expected the active demo cache" );
1160+
}
1161+
return font_set;
1162+
}
1163+
1164+
load_demo_fonts();
1165+
if ( !use_freetype ) {
1166+
apply_font_fallbacks();
1167+
}
1168+
ve_font_id huge_test_font = load_demo_font( &cache, "fonts/NotoSansJP-Light.otf", huge_buffer, 200.0f );
1169+
if ( huge_test_font < 0 && !use_freetype ) {
1170+
huge_test_font = load_demo_font( &cache, "fonts/OpenSans-Regular.ttf", huge_buffer, 200.0f );
1171+
}
1172+
normalize_demo_font_ids( &huge_test_font );
1173+
1174+
const auto assign_role = [&]( ve_fontcache_backend_test_font_role role, ve_font_id id, const char* reason ) {
1175+
if ( id >= 0 ) {
1176+
font_set.set( role, id );
1177+
} else {
1178+
font_set.mark_unavailable( role, reason );
1179+
}
1180+
};
1181+
1182+
assign_role( VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_PRIMARY, print_font, "demo primary font failed to load" );
1183+
assign_role(
1184+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_SECONDARY,
1185+
title_font >= 0 ? title_font : print_font,
1186+
"demo secondary font failed to load" );
1187+
assign_role(
1188+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_SMALL,
1189+
small_font >= 0 ? small_font : print_font,
1190+
"demo small font failed to load" );
1191+
assign_role(
1192+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_LATIN,
1193+
demo_grid3_font >= 0 ? demo_grid3_font : ( title_font >= 0 ? title_font : print_font ),
1194+
"demo latin font failed to load" );
1195+
assign_role(
1196+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_CJK,
1197+
demo_grid2_font >= 0 ? demo_grid2_font : print_font,
1198+
"demo CJK font failed to load" );
1199+
assign_role( VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_HUGE, huge_test_font, "demo huge font failed to load" );
1200+
assign_role(
1201+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_ARABIC,
1202+
use_freetype ? demo_arabic_font : -1,
1203+
use_freetype ? "demo Arabic font failed to load" : "Arabic role is only used in FreeType mode" );
1204+
assign_role(
1205+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_HEBREW,
1206+
use_freetype ? demo_hebrew_font : -1,
1207+
use_freetype ? "demo Hebrew font failed to load" : "Hebrew role is only used in FreeType mode" );
1208+
return font_set;
1209+
};
11681210
options.capabilities.has_present_surface = true;
11691211
options.capabilities.has_target_linear_surface = true;
11701212
#ifdef VE_FONTCACHE_FREETYPE_RASTERISATION
@@ -1193,17 +1235,9 @@ static int run_backend_test_mode()
11931235
cache = ve_fontcache();
11941236
ve_fontcache_init( &cache, use_freetype );
11951237
ve_fontcache_configure_snap( &cache, window_size.width, window_size.height );
1196-
load_demo_fonts();
1197-
if ( !use_freetype ) {
1198-
apply_font_fallbacks();
1199-
}
1200-
ve_font_id refreshed_huge_font = load_demo_font( &cache, "fonts/NotoSansJP-Light.otf", huge_buffer, 200.0f );
1201-
if ( refreshed_huge_font < 0 && !use_freetype ) {
1202-
refreshed_huge_font = load_demo_font( &cache, "fonts/OpenSans-Regular.ttf", huge_buffer, 200.0f );
1203-
}
1204-
normalize_demo_font_ids( &refreshed_huge_font );
12051238
clear_backend_test_surfaces( true );
12061239
};
1240+
options.finalise_test_state = backend_test_finalise_state;
12071241

12081242
ve_fontcache_backend_test_result result = ve_fontcache_backend_test_run( options );
12091243
printf(

demo/demo_dx11.cpp

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,13 @@ static void backend_test_reset_surfaces()
15871587
g_context->Flush();
15881588
}
15891589

1590+
static void backend_test_finalise_state()
1591+
{
1592+
ve_fontcache_reset_transient_test_state( &cache );
1593+
clear_backend_test_surfaces( true );
1594+
g_context->Flush();
1595+
}
1596+
15901597
#ifdef VE_FONTCACHE_FREETYPE_RASTERISATION
15911598
static void dx11_update_r8_texture_region_from_greyscale( ID3D11Texture2D* texture, UINT texture_height, int x, int y, int w, int h, const uint8_t* pixels )
15921599
{
@@ -1746,7 +1753,6 @@ static int run_backend_test_mode()
17461753
cache = ve_fontcache();
17471754
ve_fontcache_init( &cache, use_freetype );
17481755
ve_fontcache_configure_snap( &cache, window_size.width, window_size.height );
1749-
load_demo_fonts();
17501756

17511757
std::vector< uint8_t > huge_buffer;
17521758
std::vector< uint8_t > fallback_print_buffer;
@@ -1774,33 +1780,69 @@ static int run_backend_test_mode()
17741780
if ( !use_freetype ) {
17751781
apply_font_fallbacks();
17761782
}
1777-
ve_font_id huge_test_font = load_demo_font( &cache, "fonts/NotoSansJP-Light.otf", huge_buffer, 200.0f );
1778-
if ( huge_test_font < 0 && !use_freetype ) {
1779-
huge_test_font = load_demo_font( &cache, "fonts/OpenSans-Regular.ttf", huge_buffer, 200.0f );
1780-
}
1781-
normalize_demo_font_ids( &huge_test_font );
1782-
1783-
bool fonts_ready =
1784-
print_font >= 0
1785-
&& huge_test_font >= 0;
1786-
if ( !fonts_ready ) {
1787-
std::printf( "VEFontCache backend tests [%s] failed to load one or more demo fonts.\n", mode_name );
1788-
ve_fontcache_shutdown( &cache );
1789-
return false;
1790-
}
17911783

17921784
clear_backend_test_surfaces( use_freetype ? false : true );
17931785

17941786
ve_fontcache_backend_test_options options;
17951787
options.cache = &cache;
1796-
options.font = print_font;
1797-
options.secondary_font = title_font >= 0 ? title_font : print_font;
1798-
options.small_font = small_font >= 0 ? small_font : print_font;
1799-
options.latin_font = demo_grid3_font >= 0 ? demo_grid3_font : options.secondary_font;
1800-
options.cjk_font = demo_grid2_font >= 0 ? demo_grid2_font : print_font;
1801-
options.huge_font = huge_test_font;
1802-
options.arabic_font = use_freetype ? demo_arabic_font : -1;
1803-
options.hebrew_font = use_freetype ? demo_hebrew_font : -1;
1788+
options.provision_fonts = [&, use_freetype](
1789+
ve_fontcache* target_cache,
1790+
const ve_fontcache_backend_test_font_spec* roles,
1791+
size_t role_count ) -> ve_fontcache_backend_test_font_set {
1792+
ve_fontcache_backend_test_font_set font_set;
1793+
if ( target_cache != &cache ) {
1794+
for ( size_t i = 0; i < role_count; i++ ) {
1795+
font_set.mark_unavailable( roles[ i ].role, "demo test runner expected the active demo cache" );
1796+
}
1797+
return font_set;
1798+
}
1799+
1800+
load_demo_fonts();
1801+
if ( !use_freetype ) {
1802+
apply_font_fallbacks();
1803+
}
1804+
ve_font_id huge_test_font = load_demo_font( &cache, "fonts/NotoSansJP-Light.otf", huge_buffer, 200.0f );
1805+
if ( huge_test_font < 0 && !use_freetype ) {
1806+
huge_test_font = load_demo_font( &cache, "fonts/OpenSans-Regular.ttf", huge_buffer, 200.0f );
1807+
}
1808+
normalize_demo_font_ids( &huge_test_font );
1809+
1810+
const auto assign_role = [&]( ve_fontcache_backend_test_font_role role, ve_font_id id, const char* reason ) {
1811+
if ( id >= 0 ) {
1812+
font_set.set( role, id );
1813+
} else {
1814+
font_set.mark_unavailable( role, reason );
1815+
}
1816+
};
1817+
1818+
assign_role( VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_PRIMARY, print_font, "demo primary font failed to load" );
1819+
assign_role(
1820+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_SECONDARY,
1821+
title_font >= 0 ? title_font : print_font,
1822+
"demo secondary font failed to load" );
1823+
assign_role(
1824+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_SMALL,
1825+
small_font >= 0 ? small_font : print_font,
1826+
"demo small font failed to load" );
1827+
assign_role(
1828+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_LATIN,
1829+
demo_grid3_font >= 0 ? demo_grid3_font : ( title_font >= 0 ? title_font : print_font ),
1830+
"demo latin font failed to load" );
1831+
assign_role(
1832+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_CJK,
1833+
demo_grid2_font >= 0 ? demo_grid2_font : print_font,
1834+
"demo CJK font failed to load" );
1835+
assign_role( VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_HUGE, huge_test_font, "demo huge font failed to load" );
1836+
assign_role(
1837+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_ARABIC,
1838+
use_freetype ? demo_arabic_font : -1,
1839+
use_freetype ? "demo Arabic font failed to load" : "Arabic role is only used in FreeType mode" );
1840+
assign_role(
1841+
VE_FONTCACHE_BACKEND_TEST_FONT_ROLE_HEBREW,
1842+
use_freetype ? demo_hebrew_font : -1,
1843+
use_freetype ? "demo Hebrew font failed to load" : "Hebrew role is only used in FreeType mode" );
1844+
return font_set;
1845+
};
18041846
options.capabilities.has_present_surface = true;
18051847
options.capabilities.has_target_linear_surface = true;
18061848
#ifdef VE_FONTCACHE_FREETYPE_RASTERISATION
@@ -1829,17 +1871,9 @@ static int run_backend_test_mode()
18291871
cache = ve_fontcache();
18301872
ve_fontcache_init( &cache, use_freetype );
18311873
ve_fontcache_configure_snap( &cache, window_size.width, window_size.height );
1832-
load_demo_fonts();
1833-
if ( !use_freetype ) {
1834-
apply_font_fallbacks();
1835-
}
1836-
ve_font_id refreshed_huge_font = load_demo_font( &cache, "fonts/NotoSansJP-Light.otf", huge_buffer, 200.0f );
1837-
if ( refreshed_huge_font < 0 && !use_freetype ) {
1838-
refreshed_huge_font = load_demo_font( &cache, "fonts/OpenSans-Regular.ttf", huge_buffer, 200.0f );
1839-
}
1840-
normalize_demo_font_ids( &refreshed_huge_font );
18411874
clear_backend_test_surfaces( true );
18421875
};
1876+
options.finalise_test_state = backend_test_finalise_state;
18431877

18441878
ve_fontcache_backend_test_result result = ve_fontcache_backend_test_run( options );
18451879
std::printf(

0 commit comments

Comments
 (0)