# How do you dynamically create a UFont at Runtime in c++?

**URL:** <https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643>\
**Category:** Rendering\
**Tags:** font, fonts, unreal-engine, CPP, question\
**Created:** [September 28, 2017, 9:54am UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643 "2017-09-28T09:54:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanimalsOnParade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DanimalsOnParade](https://forums.unrealengine.com/u/DanimalsOnParade)\
**Post date:** [September 28, 2017, 9:54am UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643/1 "2017-09-28T09:54:18Z")

</div>

How do you dynamically create a UFont object at runtime from a .ttf font in c++? Assuming I can get the .ttf font into memory as bytes, TArray, how can I create UFontFace from that and plug it into a UFont?

I want my users to be able to upload custom .ttf fonts in addition to the ones that I provide for maximum customization.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [September 28, 2017, 1:54pm UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643/2 "2017-09-28T13:54:45Z")

</div>

It’s simpler if you can write the font to disk and avoid dealing with `UFontFace`.

`FLegacySlateFontInfoCache::GetSystemFont` provides an example of this, and you’d want to do something similar but populate the `CompositeFont` inside your `UFont` (you’d also need to make sure that `FontCacheType` is set to `EFontCacheType::Runtime` on your `UFont`).

If you do want to use a `UFontFace` then you’d need to populate its `FontFaceData` via a call to `FFontFaceData::MakeFontFaceData` passing in your raw TTF data (you’d also need to make sure that `LoadingPolicy` is set to `EFontLoadingPolicy::Inline` on your `UFontFace`). It’s then just a case of using that `UFontFace` as the asset on the `FFontData` for the `CompositeFont` inside your `UFont`.

---

<div class="post-metadata">

**Author:** ![DanimalsOnParade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DanimalsOnParade](https://forums.unrealengine.com/u/DanimalsOnParade)\
**Post date:** [September 29, 2017, 1:47am UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643/3 "2017-09-29T01:47:43Z")

</div>

Thanks for your reply!

I’m working with the HTML5 platform, so I don’t think writing to disk is an option here.

My understanding of what you suggested for the raw TTF data is -

1. Create an FFontFaceData struct using FFontFaceData::MakeFontFaceData( .ttf file as bytes TArray ). Will I need to strip any headers or anything out of the .ttf file as bytes?
2. Create an FFontFaceDataRef using the FFontFaceData from step 1.
3. Create a new UFontFace object and set it’s public member variable FontFaceData to the FFontFaceDataRef from step 2.

From here I’m unsure on how you would make an FCompositeFont from the UFontFace. How do you do that?

Once I have the FCompositeFont, then I can just create a new UFont object and set the UFont’s CompositeFont to the newly created FCompositeFont? Then the UFont will be ready to use in UMG and such?

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [September 29, 2017, 1:33pm UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643/4 "2017-09-29T13:33:15Z")

</div>

UFontFace\* FontFace = NewObject(…);  
FontFace-\>LoadingPolicy = EFontLoadingPolicy::Inline;  
FontFace-\>FontFaceData = FFontFaceData::MakeFontFaceData(…);

```
UFont* Font = NewObject<UFont>(...);
Font->FontCacheType = EFontCacheType::Runtime;
FTypefaceEntry& TypefaceEntry = Font->CompositeFont.DefaultTypeface.Fonts[Font->CompositeFont.DefaultTypeface.Fonts.AddDefaulted()];
TypefaceEntry.Font = FFontData(FontFace);

```

Something like that. Note that at that point in time nothing is keeping your font from being GC’d, but if you use it with a UMG widget then the widget will keep it alive (there are also other ways to keep an object alive).

Also note that `MakeFontFaceData` takes an r-value reference, so you’ll either need to `MoveTemp` or `CopyTemp` your data into it (depending on whether you still need your source array afterwards).

> Will I need to strip any headers or anything out of the .ttf file as bytes?

No, it’s just raw TTF/OTF payload data. FreeType is what actually makes use of the data.

---

<div class="post-metadata">

**Author:** ![DanimalsOnParade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DanimalsOnParade](https://forums.unrealengine.com/u/DanimalsOnParade)\
**Post date:** [September 30, 2017, 5:30am UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643/5 "2017-09-30T05:30:06Z")

</div>

This worked! Thank you so much!!

---

<div class="post-metadata">

**Author:** ![MichaelWion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MichaelWion](https://forums.unrealengine.com/u/MichaelWion)\
**Post date:** [February 24, 2019, 4:48am UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643/6 "2019-02-24T04:48:54Z")

</div>

For anyone running into GC issues, try this:

```
// After making the font
FontFace->AddToRoot();
Font->AddToRoot();

...

// Later on, when exiting your game or releasing resources
FontFace->RemoveFromRoot();
FontFace->ConditionalBeginDestroy();
Font->RemoveFromRoot();
Font->ConditionalBeginDestroy();
```

---

<div class="post-metadata">

**Author:** ![MD9CH](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MD9CH](https://forums.unrealengine.com/u/MD9CH)\
**Post date:** [November 17, 2024, 9:01pm UTC](https://forums.unrealengine.com/t/how-do-you-dynamically-create-a-ufont-at-runtime-in-c/405643/7 "2024-11-17T21:01:48Z")

</div>

im try to make ttf to ufont do you know how to ?
