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

Function GetTextMetrics

engine/gamesys/src/gamesys/scripts/script_resource.cpp:3523–3563  ·  view source on GitHub ↗

gets the text metrics for a font * * Gets the text metrics from a font * * @name resource.get_text_metrics * @param url [type:hash] the font to get the (unscaled) metrics from * @param text [type:string] text to measure * @param [options] [type:table] A table containing parameters for the text. Supported entries: * * `width` * : [type:number] The width of the text field. Not used if `lin

Source from the content-addressed store, hash-verified

3521 * ```
3522 */
3523static int GetTextMetrics(lua_State* L)
3524{
3525 int top = lua_gettop(L);
3526
3527 dmhash_t path_hash = dmScript::CheckHashOrString(L, 1);
3528
3529 size_t len = 0;
3530 const char* text = luaL_checklstring(L, 2, &len);
3531
3532 dmGameSystem::FontResource* font = (dmGameSystem::FontResource*)CheckResource(L, g_ResourceModule.m_Factory, path_hash, "fontc");;
3533
3534 bool line_break = false;
3535 float leading = 1.0f;
3536 float tracking = 0.0f;
3537 float width = 100000.0f;
3538 if (top >= 3)
3539 {
3540 int table_index = 3;
3541 luaL_checktype(L, table_index, LUA_TTABLE); // Make sure it's actually a table
3542 width = (float)CheckTableNumber(L, table_index, "width", width);
3543 leading = (float)CheckTableNumber(L, table_index, "leading", leading);
3544 tracking = (float)CheckTableNumber(L, table_index, "tracking", tracking);
3545 line_break = CheckTableBoolean(L, table_index, "line_break", line_break);
3546 }
3547
3548 dmRender::HFontMap font_map = dmGameSystem::ResFontGetHandle(font);
3549
3550 TextLayoutSettings settings = {0};
3551 settings.m_Width = width;
3552 settings.m_LineBreak = line_break;
3553 settings.m_Leading = leading;
3554 settings.m_Tracking = tracking;
3555 // legacy options for glyph bank fonts
3556 settings.m_Monospace = dmRender::GetFontMapMonospaced(font_map);
3557 settings.m_Padding = dmRender::GetFontMapPadding(font_map);
3558
3559 dmRender::TextMetrics metrics;
3560 dmRender::GetTextMetrics(font_map, text, &settings, &metrics);
3561 PushTextMetricsTable(L, &metrics);
3562 return 1;
3563}
3564
3565static const luaL_reg Module_methods[] =
3566{

Callers 2

CompLabelGetTextMetricsFunction · 0.50

Calls 11

lua_gettopFunction · 0.85
CheckHashOrStringFunction · 0.85
luaL_checklstringFunction · 0.85
CheckResourceFunction · 0.85
luaL_checktypeFunction · 0.85
CheckTableNumberFunction · 0.85
CheckTableBooleanFunction · 0.85
ResFontGetHandleFunction · 0.85
GetFontMapMonospacedFunction · 0.85
GetFontMapPaddingFunction · 0.85
PushTextMetricsTableFunction · 0.85

Tested by

no test coverage detected