GDI+ multiple fonts support
Each custom loaded font requires own `GpFontCollection`. So there is array of font collections now.
This commit is contained in:
parent
a01977264c
commit
02e6a81625
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Nuklear - 1.32.0 - public domain
|
||||
* Nuklear - 1.40.8 - public domain
|
||||
* no warrenty implied; use at your own risk.
|
||||
* authored from 2015-2017 by Micha Mettke
|
||||
*/
|
||||
@ -388,6 +388,8 @@ GdipMeasureString(
|
||||
GpStatus WINGDIPAPI
|
||||
GdipSetTextRenderingHint(GpGraphics *graphics, TextRenderingHint mode);
|
||||
|
||||
LWSTDAPI_(IStream *) SHCreateMemStream(const BYTE *pInit, _In_ UINT cbInit);
|
||||
|
||||
struct GdipFont
|
||||
{
|
||||
struct nk_user_font nk;
|
||||
@ -403,7 +405,8 @@ static struct {
|
||||
GpPen *pen;
|
||||
GpSolidFill *brush;
|
||||
GpStringFormat *format;
|
||||
GpFontCollection *fontCollection;
|
||||
GpFontCollection *fontCollection[10];
|
||||
INT curFontCollection;
|
||||
|
||||
struct nk_context ctx;
|
||||
} gdip;
|
||||
@ -640,6 +643,21 @@ nk_gdip_load_image_from_memory(const void *membuf, nk_uint membufSize)
|
||||
return nk_image_id(0);
|
||||
|
||||
return nk_gdip_image_to_nk(image);
|
||||
|
||||
#if 1==0
|
||||
GpImage *image = NULL;
|
||||
IStream *pStream = NULL;
|
||||
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, membufSize);
|
||||
LPVOID pImage = GlobalLock(hMem);
|
||||
nk_memcopy(pImage, membuf, membufSize);
|
||||
GlobalUnlock(hMem);
|
||||
|
||||
/* CreateStreamOnHGlobal needs OLE32 in linked libraries list */
|
||||
CreateStreamOnHGlobal(hMem, FALSE, &pStream);
|
||||
GdipLoadImageFromStream(pStream, &image);
|
||||
GlobalFree(hMem);
|
||||
return nk_gdip_image_to_nk(image);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@ -668,36 +686,42 @@ nk_gdipfont_create(const char *name, int size)
|
||||
return font;
|
||||
}
|
||||
|
||||
GpFontCollection*
|
||||
getCurFontCollection(){
|
||||
return gdip.fontCollection[gdip.curFontCollection];
|
||||
}
|
||||
|
||||
GdipFont*
|
||||
nk_gdipfont_create_from_collection(int size){
|
||||
GpFontFamily **families;
|
||||
INT count;
|
||||
GdipFont *font = (GdipFont*)calloc(1, sizeof(GdipFont));
|
||||
if( GdipGetFontCollectionFamilyCount(gdip.fontCollection, &count) ) return NULL;
|
||||
if( GdipGetFontCollectionFamilyCount(getCurFontCollection(), &count) ) return NULL;
|
||||
families = (GpFontFamily**)calloc(1, sizeof(GpFontFamily*));
|
||||
if( !families ) return NULL;
|
||||
if( GdipGetFontCollectionFamilyList(gdip.fontCollection, count, families, &count) ) return NULL;
|
||||
if( GdipGetFontCollectionFamilyList(getCurFontCollection(), count, families, &count) ) return NULL;
|
||||
if( count < 1 ) return NULL;
|
||||
if( GdipCreateFont(families[count-1], (REAL)size, FontStyleRegular, UnitPixel, &font->handle) ) return NULL;
|
||||
free(families);
|
||||
gdip.curFontCollection++;
|
||||
return font;
|
||||
}
|
||||
|
||||
GdipFont*
|
||||
nk_gdipfont_create_from_memory(const void* membuf, int membufSize, int size)
|
||||
{
|
||||
if( !gdip.fontCollection )
|
||||
if( GdipNewPrivateFontCollection(&gdip.fontCollection) ) return NULL;
|
||||
if( GdipPrivateAddMemoryFont(gdip.fontCollection, membuf, membufSize) ) return NULL;
|
||||
if( !getCurFontCollection() )
|
||||
if( GdipNewPrivateFontCollection(&gdip.fontCollection[gdip.curFontCollection]) ) return NULL;
|
||||
if( GdipPrivateAddMemoryFont(getCurFontCollection(), membuf, membufSize) ) return NULL;
|
||||
return nk_gdipfont_create_from_collection(size);
|
||||
}
|
||||
|
||||
GdipFont*
|
||||
nk_gdipfont_create_from_file(const WCHAR* filename, int size)
|
||||
{
|
||||
if( !gdip.fontCollection )
|
||||
if( GdipNewPrivateFontCollection(&gdip.fontCollection) ) return NULL;
|
||||
if( GdipPrivateAddFontFile(gdip.fontCollection, filename) ) return NULL;
|
||||
if( !getCurFontCollection() )
|
||||
if( GdipNewPrivateFontCollection(&gdip.fontCollection[gdip.curFontCollection]) ) return NULL;
|
||||
if( GdipPrivateAddFontFile(getCurFontCollection(), filename) ) return NULL;
|
||||
return nk_gdipfont_create_from_collection(size);
|
||||
}
|
||||
|
||||
@ -822,6 +846,7 @@ nk_gdip_clipbard_copy(nk_handle usr, const char *text, int len)
|
||||
NK_API struct nk_context*
|
||||
nk_gdip_init(HWND hwnd, unsigned int width, unsigned int height)
|
||||
{
|
||||
int i;
|
||||
GdiplusStartupInput startup = { 1, NULL, FALSE, TRUE };
|
||||
GdiplusStartup(&gdip.token, &startup, NULL);
|
||||
|
||||
@ -835,10 +860,13 @@ nk_gdip_init(HWND hwnd, unsigned int width, unsigned int height)
|
||||
StringFormatFlagsMeasureTrailingSpaces | StringFormatFlagsNoWrap |
|
||||
StringFormatFlagsNoClip);
|
||||
|
||||
gdip.fontCollection = NULL;
|
||||
/*gdip.fontCollection = NULL;*/
|
||||
for(i=0; i< sizeof(gdip.fontCollection)/sizeof(gdip.fontCollection[0]); i++)
|
||||
gdip.fontCollection[i] = NULL;
|
||||
nk_init_default(&gdip.ctx, NULL);
|
||||
gdip.ctx.clip.copy = nk_gdip_clipbard_copy;
|
||||
gdip.ctx.clip.paste = nk_gdip_clipbard_paste;
|
||||
gdip.curFontCollection = 0;
|
||||
return &gdip.ctx;
|
||||
}
|
||||
|
||||
@ -1043,7 +1071,10 @@ nk_gdip_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
||||
NK_API void
|
||||
nk_gdip_shutdown(void)
|
||||
{
|
||||
GdipDeletePrivateFontCollection( &gdip.fontCollection );
|
||||
/*GdipDeletePrivateFontCollection( &gdip.fontCollection );*/
|
||||
int i;
|
||||
for(i=0; i< gdip.curFontCollection; i++)
|
||||
GdipDeletePrivateFontCollection( &gdip.fontCollection[i] );
|
||||
GdipDeleteGraphics(gdip.window);
|
||||
GdipDeleteGraphics(gdip.memory);
|
||||
GdipDisposeImage(gdip.bitmap);
|
||||
@ -1056,7 +1087,7 @@ nk_gdip_shutdown(void)
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_gdip_render_gui(enum nk_anti_aliasing AA)
|
||||
nk_gdip_prerender_gui(enum nk_anti_aliasing AA)
|
||||
{
|
||||
const struct nk_command *cmd;
|
||||
|
||||
@ -1140,6 +1171,12 @@ nk_gdip_render_gui(enum nk_anti_aliasing AA)
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NK_API void
|
||||
nk_gdip_render_gui(enum nk_anti_aliasing AA)
|
||||
{
|
||||
nk_gdip_prerender_gui(AA);
|
||||
nk_gdip_blit(gdip.window);
|
||||
nk_clear(&gdip.ctx);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user