diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-04-08 03:10:17 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-04-08 03:38:19 +0300 |
commit | 8c539b0ab521d442d88c70243c04cbb9e40fe412 (patch) | |
tree | 58b1ffb19b1117404698a6705a87406722400b01 /source/blender/blenfont | |
parent | c56c493cf403b63ec46b90023cdc8137275a310f (diff) |
Font preview for file browser
D1002 by @plasmasolutions, with own refactoring.
Note, needed to do a bad-level call here (IMB -> BLF)
Also can't use the BLF API directly because its not thread-safe.
So keep the function isolated (blf_thumbs.c).
Diffstat (limited to 'source/blender/blenfont')
-rw-r--r-- | source/blender/blenfont/BLF_api.h | 6 | ||||
-rw-r--r-- | source/blender/blenfont/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_thumbs.c | 104 |
3 files changed, 111 insertions, 0 deletions
diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h index 206345582b2..09734bd9754 100644 --- a/source/blender/blenfont/BLF_api.h +++ b/source/blender/blenfont/BLF_api.h @@ -186,6 +186,12 @@ char **BLF_dir_get(int *ndir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /* Free the data return by BLF_dir_get. */ void BLF_dir_free(char **dirs, int count) ATTR_NONNULL(); +/* blf_thumbs.c */ +void BLF_thumb_preview( + const char *filename, const char **draw_str, const unsigned char draw_str_lines, + const float font_color[4], const int font_size, + unsigned char *buf, int w, int h, int channels) ATTR_NONNULL(); + #ifdef DEBUG void BLF_state_print(int fontid); #endif diff --git a/source/blender/blenfont/CMakeLists.txt b/source/blender/blenfont/CMakeLists.txt index 392a9ede100..2f10c3580e0 100644 --- a/source/blender/blenfont/CMakeLists.txt +++ b/source/blender/blenfont/CMakeLists.txt @@ -47,6 +47,7 @@ set(SRC intern/blf_font.c intern/blf_glyph.c intern/blf_lang.c + intern/blf_thumbs.c intern/blf_translation.c intern/blf_util.c diff --git a/source/blender/blenfont/intern/blf_thumbs.c b/source/blender/blenfont/intern/blf_thumbs.c new file mode 100644 index 00000000000..afda6a382f9 --- /dev/null +++ b/source/blender/blenfont/intern/blf_thumbs.c @@ -0,0 +1,104 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Thomas Beck + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/blenfont/intern/blf_thumbs.c + * \ingroup blf + * + * Utility function to generate font preview images. + * + * Isolate since this needs to be called by #ImBuf code (bad level call). + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <ft2build.h> + +#include FT_FREETYPE_H + +#include "BLI_utildefines.h" +#include "BLI_listbase.h" +#include "BLI_rect.h" +#include "BLI_threads.h" + +#include "blf_internal.h" +#include "blf_internal_types.h" + +#include "BLF_api.h" + +#include "BLI_strict_flags.h" + +/** + * This function is used for generating thumbnail previews. + * + * \note called from a thread, so it bypasses the normal BLF_* api (which isn't thread-safe). + */ +void BLF_thumb_preview( + const char *filename, + const char **draw_str, const unsigned char draw_str_lines, + const float font_color[4], const int font_size, + unsigned char *buf, int w, int h, int channels) +{ + const unsigned int dpi = 72; + const int font_size_min = 6; + int font_size_curr; + /* shrink 1/th each line */ + int font_shrink = 4; + + FontBLF* font; + int i; + + /* Create a new blender font obj and fill it with default values */ + font = blf_font_new("thumb_font", filename); + if (!font) { + printf("Info: Can't load font '%s', no preview possible\n", filename); + return; + } + + /* Would be done via the BLF API, but we're not using a fontid here */ + font->buf_info.cbuf = buf; + font->buf_info.ch = channels; + font->buf_info.w = w; + font->buf_info.h = h; + + /* Always create the image with a white font, + * the caller can theme how it likes */ + memcpy(font->buf_info.col, font_color, sizeof(font->buf_info.col)); + font->pos[1] = (float)h; + + font_size_curr = font_size; + + for (i = 0; i < draw_str_lines; i++) { + blf_font_size(font, (unsigned int)MAX2(font_size_min, font_size_curr), dpi); + + /* decrease font size each time */ + font_size_curr -= (font_size_curr / font_shrink); + font_shrink += 1; + + font->pos[1] -= font->glyph_cache->ascender * 1.1f; + + blf_font_buffer(font, draw_str[i]); + } + + blf_font_free(font); +} |