MCPcopy Create free account
hub / github.com/defold/defold / AddFont

Function AddFont

engine/gamesys/src/gamesys/scripts/script_font.cpp:74–110  ·  view source on GitHub ↗

* associates a ttf resource to a .fontc file. * @note The ttf font is loaded via the resource system. There are a few ways it can be accessed: * - It was already loaded in the resource system * - It is bundled via our game data * - It is accessible via a live update mount * * @note The reference count will increase for the .ttf font * * * @name font.add_font * @param fontc [t

Source from the content-addressed store, hash-verified

72 * ```
73 */
74static int AddFont(lua_State* L)
75{
76 DM_LUA_STACK_CHECK(L, 0);
77
78 dmhash_t fontc_path_hash = dmScript::CheckHashOrString(L, 1);
79 // TODO: If it's a string, pass a string to the function to allow for explicit loading of the resource
80 const char* ttf_path = 0;
81 dmhash_t ttf_path_hash = 0;
82 if (lua_isstring(L, 2))
83 {
84 ttf_path = luaL_checkstring(L, 2);
85 }
86 else
87 {
88 ttf_path_hash = dmScript::CheckHash(L, 2);
89 }
90
91 dmGameSystem::FontResource* resource;
92 dmResource::Result r = dmResource::GetWithExt(g_ResourceFactory, fontc_path_hash, EXT_HASH_FONTC, (void**)&resource);
93 if (dmResource::RESULT_OK != r)
94 {
95 return DM_LUA_ERROR("Failed to get font %s: %d", dmHashReverseSafe64(fontc_path_hash), r);
96 }
97
98 if (ttf_path)
99 r = dmGameSystem::ResFontAddFontByPath(g_ResourceFactory, resource, ttf_path);
100 else
101 r = dmGameSystem::ResFontAddFontByPathHash(g_ResourceFactory, resource, ttf_path_hash);
102 dmResource::Release(g_ResourceFactory, resource);
103
104 if (dmResource::RESULT_OK != r)
105 {
106 return DM_LUA_ERROR("Failed to add font '%s' to font collection '%s'", dmHashReverseSafe64(ttf_path_hash), dmHashReverseSafe64(fontc_path_hash));
107 }
108
109 return 0;
110}
111
112/*#
113 * associates a ttf resource to a .fontc file

Callers 2

SetupGuiSceneFunction · 0.50
CompGuiSetPropertyFunction · 0.50

Calls 8

CheckHashOrStringFunction · 0.85
lua_isstringFunction · 0.85
CheckHashFunction · 0.85
GetWithExtFunction · 0.85
dmHashReverseSafe64Function · 0.85
ResFontAddFontByPathFunction · 0.85
ResFontAddFontByPathHashFunction · 0.85
ReleaseFunction · 0.50

Tested by

no test coverage detected