Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenfont/intern/blf_thumbs.c
| Show All 26 Lines | |||||
| #include "blf_internal.h" | #include "blf_internal.h" | ||||
| #include "blf_internal_types.h" | #include "blf_internal_types.h" | ||||
| #include "BLF_api.h" | #include "BLF_api.h" | ||||
| #include "BLI_strict_flags.h" | #include "BLI_strict_flags.h" | ||||
| bool BLF_draw_font_string(const char *fontpath, | |||||
| const char *string, | |||||
| int xpos, | |||||
| int ypos, | |||||
| float width, | |||||
| int textheight, | |||||
| const uchar text_col[4]) | |||||
| { | |||||
| /* While listing font directories this function can be called simultaneously from a | |||||
| * greater number of threads than we want the FreeType cache to keep open at a time. Therefore | |||||
| * pass own FT_Library to font creation so that it is not managed by the FreeType cache system. | |||||
| */ | |||||
| FT_Library ft_library; | |||||
| if (FT_Init_FreeType(&ft_library) != FT_Err_Ok) { | |||||
| return false; | |||||
| } | |||||
| /* Create a new blender font obj and fill it with default values */ | |||||
| FontBLF *font = blf_font_new_ex("font_text", fontpath, NULL, 0, ft_library); | |||||
| if (!font) { | |||||
| FT_Done_FreeType(ft_library); | |||||
| return false; | |||||
| } | |||||
| /* TrueType table with bits to quickly test most unicode block coverage. */ | |||||
| TT_OS2 *os2_table = (TT_OS2 *)FT_Get_Sfnt_Table(font->face, FT_SFNT_OS2); | |||||
| if (!os2_table || !(os2_table->ulUnicodeRange1 & TT_UCR_BASIC_LATIN)) { | |||||
| blf_font_free(font); | |||||
| FT_Done_FreeType(ft_library); | |||||
| return false; | |||||
| } | |||||
| if (font->face->charmap->encoding != FT_ENCODING_UNICODE) { | |||||
| blf_font_free(font); | |||||
| FT_Done_FreeType(ft_library); | |||||
| return false; | |||||
| } | |||||
| /* Some fonts lie about the scripts they support. */ | |||||
| if (FT_Get_Char_Index(font->face, U'A') == 0) { | |||||
| blf_font_free(font); | |||||
| FT_Done_FreeType(ft_library); | |||||
| return false; | |||||
| } | |||||
| /* Set FT font size. Just for measuring, will not be rendered. */ | |||||
| FT_Set_Pixel_Sizes(font->face, 256, 0); | |||||
| int height = (font->face->size->metrics.ascender - font->face->size->metrics.descender) >> 6; | |||||
| float font_size = 256.0f / (float)height * (float)textheight * 1.2f; | |||||
| blf_font_size(font, font_size); | |||||
| font->flags = BLF_CLIPPING; | |||||
| copy_v4_v4_uchar(font->color, text_col); | |||||
| font->pos[0] = xpos; | |||||
| font->pos[1] = ypos - (int)(textheight * 0.85f); | |||||
| font->clip_rec.xmin = xpos; | |||||
| font->clip_rec.xmax = xpos + (int)width; | |||||
| font->clip_rec.ymin = ypos - textheight; | |||||
| font->clip_rec.ymax = ypos; | |||||
| blf_font_draw(font, string, strlen(string), NULL); | |||||
| blf_font_free(font); | |||||
| FT_Done_FreeType(ft_library); | |||||
| return true; | |||||
| } | |||||
| /* Maximum length of text sample in char32_t, including NULL terminator. */ | /* Maximum length of text sample in char32_t, including NULL terminator. */ | ||||
| #define BLF_SAMPLE_LEN 5 | #define BLF_SAMPLE_LEN 5 | ||||
| typedef struct UnicodeSample { | typedef struct UnicodeSample { | ||||
| char32_t sample[BLF_SAMPLE_LEN]; | char32_t sample[BLF_SAMPLE_LEN]; | ||||
| int field; /* ‘OS/2’ table ulUnicodeRangeX field (1-4). */ | int field; /* ‘OS/2’ table ulUnicodeRangeX field (1-4). */ | ||||
| FT_ULong mask; /* ‘OS/2’ table ulUnicodeRangeX bit mask. */ | FT_ULong mask; /* ‘OS/2’ table ulUnicodeRangeX bit mask. */ | ||||
| } UnicodeSample; | } UnicodeSample; | ||||
| ▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines | |||||