Added loading TTF font from file

Also font API rename to be the same with images.
This commit is contained in:
Dmitry Hrabrov 2017-01-20 15:09:27 +03:00 committed by GitHub
parent 4d957de4a1
commit 8033c400b4
1 changed files with 38 additions and 11 deletions

View File

@ -19,7 +19,8 @@
/* font */ /* font */
typedef struct GdipFont GdipFont; typedef struct GdipFont GdipFont;
NK_API GdipFont* nk_gdipfont_create(const char *name, int size); NK_API GdipFont* nk_gdipfont_create(const char *name, int size);
NK_API GdipFont* nk_gdipfont_create_mem(unsigned char *membuf, int membufSize, int size); NK_API GdipFont* nk_gdipfont_create_from_file(const WCHAR* filename, int size);
NK_API GdipFont* nk_gdipfont_create_from_memory(const void* membuf, int membufSize, int size);
NK_API void nk_gdipfont_del(GdipFont *font); NK_API void nk_gdipfont_del(GdipFont *font);
NK_API struct nk_context* nk_gdip_init(HWND hwnd, unsigned int width, unsigned int height); NK_API struct nk_context* nk_gdip_init(HWND hwnd, unsigned int width, unsigned int height);
@ -272,6 +273,10 @@ GpStatus WINGDIPAPI
GdipPrivateAddMemoryFont(GpFontCollection* fontCollection, GdipPrivateAddMemoryFont(GpFontCollection* fontCollection,
GDIPCONST void* memory, INT length); GDIPCONST void* memory, INT length);
GpStatus WINGDIPAPI
GdipPrivateAddFontFile(GpFontCollection* fontCollection,
GDIPCONST WCHAR* filename);
GpStatus WINGDIPAPI GpStatus WINGDIPAPI
GdipNewPrivateFontCollection(GpFontCollection** fontCollection); GdipNewPrivateFontCollection(GpFontCollection** fontCollection);
@ -280,7 +285,10 @@ GdipDeletePrivateFontCollection(GpFontCollection** fontCollection);
GpStatus WINGDIPAPI GpStatus WINGDIPAPI
GdipGetFontCollectionFamilyList(GpFontCollection* fontCollection, GdipGetFontCollectionFamilyList(GpFontCollection* fontCollection,
INT numSought, GpFontFamily* gpfamilies[], INT* numFound); INT numSought, GpFontFamily* gpfamilies[], INT* numFound);
GpStatus WINGDIPAPI
GdipGetFontCollectionFamilyCount(GpFontCollection* fontCollection, INT* numFound);
/* graphics */ /* graphics */
@ -652,19 +660,38 @@ nk_gdipfont_create(const char *name, int size)
} }
GdipFont* GdipFont*
nk_gdipfont_create_mem(unsigned char *membuf, int membufSize, int size) nk_gdipfont_create_from_collection(int size){
{ GpFontFamily **families;
INT count;
GdipFont *font = (GdipFont*)calloc(1, sizeof(GdipFont)); GdipFont *font = (GdipFont*)calloc(1, sizeof(GdipFont));
GpFontFamily *families[1]; if( GdipGetFontCollectionFamilyCount(gdip.fontCollection, &count) ) return NULL;
INT numFound; families = (GpFontFamily**)calloc(1, sizeof(GpFontFamily*));
if( !families ) return NULL;
if( GdipNewPrivateFontCollection(&gdip.fontCollection) ) return NULL; if( GdipGetFontCollectionFamilyList(gdip.fontCollection, count, families, &count) ) return NULL;
if( GdipPrivateAddMemoryFont(gdip.fontCollection, membuf, membufSize) ) return NULL; if( count < 1 ) return NULL;
if( GdipGetFontCollectionFamilyList(gdip.fontCollection, 1, families, &numFound) ) return NULL; if( GdipCreateFont(families[count-1], (REAL)size, FontStyleRegular, UnitPixel, &font->handle) ) return NULL;
if( GdipCreateFont(families[0], (REAL)size, FontStyleRegular, UnitPixel, &font->handle) ) return NULL; free(families);
return font; 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;
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;
return nk_gdipfont_create_from_collection(size);
}
static float static float
nk_gdipfont_get_text_width(nk_handle handle, float height, const char *text, int len) nk_gdipfont_get_text_width(nk_handle handle, float height, const char *text, int len)
{ {