# SDL\_ttf load font from memory

**URL:** <https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843>\
**Category:** SDL Development\
**Created:** [June 13, 2021, 3:50am UTC](https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843 "2021-06-13T03:50:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eri0o](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/eri0o/32/4277_2.png) [@eri0o](https://discourse.libsdl.org/u/eri0o)\
**Post date:** [June 13, 2021, 3:50am UTC](https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843/1 "2021-06-13T03:50:30Z")

</div>

I have my own resource management system, I am trying to figure out how load a TTF\_Font directly from memory instead of a file for using with SDL\_ttf, I am a bit lost with it’s API, does anyone knows how to do this?

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [June 13, 2021, 10:15am UTC](https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843/2 "2021-06-13T10:15:22Z")

</div>

You can load font data from memory by doing it like this:

```
// Some font data as a stream of bytes
const char* pFontData[256];

const int FontSize = 16;

// Load the font data into a memory buffer
SDL_RWops* pFontMem = SDL_RWFromConstMem(pFontData, sizeof(pFontData));

if(!pFontMem)
{
	// Some error occurred while trying to read the data, act accordingly to that
}

// Load the font from the memory buffer
TTF_Font* pFont = TTF_OpenFontRW(pFontMem, 1, FontSize);

if(!pFont)
{
	// Some error occurred while trying to load the font, act accordingly to that
}
```

---

<div class="post-metadata">

**Author:** ![eri0o](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/eri0o/32/4277_2.png) [@eri0o](https://discourse.libsdl.org/u/eri0o)\
**Post date:** [June 13, 2021, 2:19pm UTC](https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843/3 "2021-06-13T14:19:49Z")

</div>

Thanks!

SDL ttf creates a new SDL\_Surface. I am trying to figure how to blit/blend this resulting surface to a R8G8B8A8 bitmap in memory now - it’s too soon into my experiment to look to switch to SDL surface in an entire codebase.

Is there any way? I found a blit but it also operates only on SDL surfaces. I also found the surface pixels but I don’t understand what pixel format is supposed to be out of SDL\_ttf renders.

If I knew that SDL\_ttf would always render in 32 bit or it had some way to specify the format I could try to pick the surface pixels and blend with my own functions…

Edit: Found some way but my texts have a black background instead of transparent. Everything else seems to work though…

---

<div class="post-metadata">

**Author:** ![eri0o](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/eri0o/32/4277_2.png) [@eri0o](https://discourse.libsdl.org/u/eri0o)\
**Post date:** [June 14, 2021, 11:10pm UTC](https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843/4 "2021-06-14T23:10:04Z")

</div>

So, I got it working, but I am trying to optimize things a bit now…

When I do the following, and pick the resulting surface, everything works

```auto
glyph = TTF_RenderText_Blended(font, text, sdlColor);
SDL_Surface * surface = SDL_CreateRGBSurfaceWithFormat(0, glyph->w,glyph->h, dest_depth, SDL_PIXELFORMAT_ARGB8888);
SDL_BlitSurface(glyph, nullptr, surface, nullptr);

```

 ![image](https://discourse.libsdl.org/uploads/default/original/2X/b/bf88fc2baee0bbcfa28999efd83ba217265f65d0.jpeg)

But if I don’t do the above, and pick `glyph` directly to show in the screen, skiping the intermediate surface, I get the following

 ![image](https://discourse.libsdl.org/uploads/default/original/2X/f/f5d4a6cfbe6bd4a5531f3654909cc9de479e8adf.jpeg)

As far as I can tell, both should be `SDL_PIXELFORMAT_ARGB8888`, so any idea why the difference happens? (I am picking it’s pixels directly…)

I noticed one surface has `pitch=416` and the other has `pitch=344`, I can’t figure other differences in formats… (looking in the debugger)

---

<div class="post-metadata">

**Author:** ![eri0o](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/eri0o/32/4277_2.png) [@eri0o](https://discourse.libsdl.org/u/eri0o)\
**Post date:** [June 15, 2021, 12:15am UTC](https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843/5 "2021-06-15T00:15:35Z")

</div>

Wuhu, ok, learned what pitch is, now I can properly lookup the pixels!

---

<div class="post-metadata">

**Author:** ![Daniel1985](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@Daniel1985](https://discourse.libsdl.org/u/Daniel1985)\
**Post date:** [June 15, 2021, 10:35am UTC](https://discourse.libsdl.org/t/sdl-ttf-load-font-from-memory/31843/6 "2021-06-15T10:35:56Z")

</div>

So you don’t actually use `SDL_Texture`'s in your code base to render images to the screen? I can see that you’re instead using the `SDL_Surface` directly and render it to the screen with `SDL_BlitSurface()`.

I sadly don’t have much knowledge about the old pipeline anymore, i.e working with-, manipulating and rendering of an SDL\_Surface, since I nowadays use SDL\_Texture in combination with `SDL_RenderCopy*`.

But I’m glad you got it solved in the end.
