Add font selection to VSE text strips
authorRichard Antalik <richardantalik@gmail.com>
Mon, 14 Jan 2019 05:28:07 +0000 (21:28 -0800)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Wed, 23 Jan 2019 11:07:05 +0000 (12:07 +0100)
Allows users to select a font for text strips in the video sequence editor.

Related: 3610f1fc43d0 Sequencer: refactor clipboard copy to no longer increase user count.

Reviewed by: Brecht

Differential Revision: https://developer.blender.org/D3621

12 files changed:
release/scripts/startup/bl_ui/space_sequencer.py
source/blender/blenfont/BLF_api.h
source/blender/blenfont/intern/blf.c
source/blender/blenfont/intern/blf_internal_types.h
source/blender/blenkernel/BKE_sequencer.h
source/blender/blenkernel/intern/library_query.c
source/blender/blenkernel/intern/seqeffects.c
source/blender/blenkernel/intern/sequencer.c
source/blender/blenloader/intern/readfile.c
source/blender/editors/interface/interface_templates.c
source/blender/makesdna/DNA_sequence_types.h
source/blender/makesrna/intern/rna_sequencer.c

index 47e7b02..8d75f69 100644 (file)
@@ -758,6 +758,7 @@ class SEQUENCER_PT_effect(SequencerButtonsPanel, Panel):
         elif strip.type == 'TEXT':
             col = layout.column()
             col.prop(strip, "text")
+            col.template_ID(strip, "font", open="font.open", unlink="font.unlink")
             col.prop(strip, "font_size")
 
             row = col.row()
index 0a4212f..7a6d002 100644 (file)
@@ -46,6 +46,7 @@ void BLF_default_set(int fontid);
 
 void BLF_cache_clear(void);
 
+/* Loads a font, or returns an already loaded font and increments its reference count. */
 int BLF_load(const char *name) ATTR_NONNULL();
 int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) ATTR_NONNULL();
 
index 75aabf1..5fe1a12 100644 (file)
@@ -184,7 +184,8 @@ int BLF_load(const char *name)
        /* check if we already load this font. */
        i = blf_search(name);
        if (i >= 0) {
-               /*font = global_font[i];*/ /*UNUSED*/
+               font = global_font[i];
+               font->reference_count++;
                return i;
        }
 
@@ -208,6 +209,7 @@ int BLF_load(const char *name)
                return -1;
        }
 
+       font->reference_count = 1;
        global_font[i] = font;
        return i;
 }
@@ -241,6 +243,7 @@ int BLF_load_unique(const char *name)
                return -1;
        }
 
+       font->reference_count = 1;
        global_font[i] = font;
        return i;
 }
@@ -282,6 +285,7 @@ int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size)
                return -1;
        }
 
+       font->reference_count = 1;
        global_font[i] = font;
        return i;
 }
@@ -312,6 +316,7 @@ int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size
                return -1;
        }
 
+       font->reference_count = 1;
        global_font[i] = font;
        return i;
 }
@@ -325,8 +330,13 @@ void BLF_unload(const char *name)
                font = global_font[i];
 
                if (font && (STREQ(font->name, name))) {
-                       blf_font_free(font);
-                       global_font[i] = NULL;
+                       BLI_assert(font->reference_count > 0);
+                       font->reference_count--;
+
+                       if (font->reference_count == 0) {
+                               blf_font_free(font);
+                               global_font[i] = NULL;
+                       }
                }
        }
 }
@@ -335,8 +345,13 @@ void BLF_unload_id(int fontid)
 {
        FontBLF *font = blf_get(fontid);
        if (font) {
-               blf_font_free(font);
-               global_font[fontid] = NULL;
+               BLI_assert(font->reference_count > 0);
+               font->reference_count--;
+
+               if (font->reference_count == 0) {
+                       blf_font_free(font);
+                       global_font[fontid] = NULL;
+               }
        }
 }
 
index 5723f08..8c2e247 100644 (file)
@@ -163,6 +163,9 @@ typedef struct FontBLF {
        /* font name. */
        char *name;
 
+       /* # of times this font was loaded */
+       unsigned int reference_count;
+
        /* filename or NULL. */
        char *filename;
 
index 2f78b36..81f9bbe 100644 (file)
@@ -45,6 +45,7 @@ struct Sequence;
 struct SequenceModifierData;
 struct Stereo3dFormat;
 struct StripElem;
+struct TextVars;
 struct bSound;
 
 struct SeqIndexBuildContext;
@@ -142,7 +143,7 @@ struct SeqEffectHandle {
 
        /* load is called first time after readblenfile in
         * get_sequence_effect automatically */
-       void (*load)(struct Sequence *seq);
+       void (*load)(struct Sequence *seqconst);
 
        /* duplicate */
        void (*copy)(struct Sequence *dst, struct Sequence *src, const int flag);
@@ -298,6 +299,9 @@ void BKE_sequence_effect_speed_rebuild_map(struct Scene *scene, struct Sequence
 struct SeqEffectHandle BKE_sequence_get_effect(struct Sequence *seq);
 int BKE_sequence_effect_get_num_inputs(int seq_type);
 int BKE_sequence_effect_get_supports_mask(int seq_type);
+void BKE_sequencer_text_font_unload(struct TextVars *data, const bool do_id_user);
+void BKE_sequencer_text_font_load(struct TextVars *data, const bool do_id_user);
+
 
 /* **********************************************************************
  * Sequencer editing functions
index 8a64648..0cca90f 100644 (file)
@@ -455,6 +455,11 @@ void BKE_library_foreach_ID_link(Main *bmain, ID *id, LibraryIDLinkCallback call
                                                for (SequenceModifierData *smd = seq->modifiers.first; smd; smd = smd->next) {
                                                        CALLBACK_INVOKE(smd->mask_id, IDWALK_CB_USER);
                                                }
+
+                                               if (seq->type == SEQ_TYPE_TEXT && seq->effectdata) {
+                                                       TextVars *text_data = seq->effectdata;
+                                                       CALLBACK_INVOKE(text_data->text_font, IDWALK_CB_USER);
+                                               }
                                        } SEQ_END;
                                }
 
index 5ac83a6..3cf1ff9 100644 (file)
 #include "MEM_guardedalloc.h"
 
 #include "BLI_math.h" /* windows needs for M_PI */
+#include "BLI_threads.h"
 #include "BLI_utildefines.h"
 #include "BLI_rect.h"
+#include "BLI_path_util.h"
 #include "BLI_string.h"
 
 #include "DNA_scene_types.h"
@@ -47,6 +49,8 @@
 #include "DNA_space_types.h"
 
 #include "BKE_fcurve.h"
+#include "BKE_library.h"
+#include "BKE_main.h"
 #include "BKE_sequencer.h"
 
 #include "IMB_imbuf_types.h"
@@ -3427,6 +3431,7 @@ static ImBuf *do_gaussian_blur_effect(
 }
 
 /*********************** text *************************/
+
 static void init_text_effect(Sequence *seq)
 {
        TextVars *data;
@@ -3435,6 +3440,8 @@ static void init_text_effect(Sequence *seq)
                MEM_freeN(seq->effectdata);
 
        data = seq->effectdata = MEM_callocN(sizeof(TextVars), "textvars");
+       data->text_font = NULL;
+       data->text_blf_id = -1;
        data->text_size = 30;
 
        copy_v4_fl(data->color, 1.0f);
@@ -3447,6 +3454,64 @@ static void init_text_effect(Sequence *seq)
        data->align_y = SEQ_TEXT_ALIGN_Y_BOTTOM;
 }
 
+void BKE_sequencer_text_font_unload(TextVars *data, const bool do_id_user)
+{
+       if (data) {
+               /* Unlink the VFont */
+               if (do_id_user && data->text_font != NULL) {
+                       id_us_min(&data->text_font->id);
+                       data->text_font = NULL;
+               }
+
+               /* Unload the BLF font. */
+               if (data->text_blf_id >= 0) {
+                       BLF_unload_id(data->text_blf_id);
+               }
+       }
+}
+
+void BKE_sequencer_text_font_load(TextVars *data, const bool do_id_user)
+{
+       if (data->text_font != NULL) {
+               if (do_id_user) {
+                       id_us_plus(&data->text_font->id);
+               }
+
+               char path[FILE_MAX];
+               STRNCPY(path, data->text_font->name);
+               BLI_assert(BLI_thread_is_main());
+               BLI_path_abs(path, BKE_main_blendfile_path_from_global());
+
+               data->text_blf_id = BLF_load(path);
+       }
+}
+
+static void free_text_effect(Sequence *seq, const bool do_id_user)
+{
+       TextVars *data = seq->effectdata;
+       BKE_sequencer_text_font_unload(data, do_id_user);
+
+       if (data) {
+               MEM_freeN(data);
+               seq->effectdata = NULL;
+       }
+}
+
+static void load_text_effect(Sequence *seq)
+{
+       TextVars *data = seq->effectdata;
+       BKE_sequencer_text_font_load(data, false);
+}
+
+static void copy_text_effect(Sequence *dst, Sequence *src, const int flag)
+{
+       dst->effectdata = MEM_dupallocN(src->effectdata);
+       TextVars *data = dst->effectdata;
+
+       data->text_blf_id = -1;
+       BKE_sequencer_text_font_load(data, (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0);
+}
+
 static int num_inputs_text(void)
 {
        return 0;
@@ -3473,11 +3538,23 @@ static ImBuf *do_text_effect(
        int height = out->y;
        struct ColorManagedDisplay *display;
        const char *display_device;
-       const int mono = blf_mono_font_render; // XXX
+       int font = blf_mono_font_render;
        int line_height;
        int y_ofs, x, y;
        float proxy_size_comp;
 
+       if (data->text_blf_id == SEQ_FONT_NOT_LOADED) {
+               data->text_blf_id = -1;
+
+               if (data->text_font) {
+                       data->text_blf_id = BLF_load(data->text_font->name);
+               }
+       }
+
+       if (data->text_blf_id >= 0) {
+               font = data->text_blf_id;
+       }
+
        display_device = context->scene->display_settings.display_device;
        display = IMB_colormanagement_display_get_named(display_device);
 
@@ -3493,18 +3570,18 @@ static ImBuf *do_text_effect(
        }
 
        /* set before return */
-       BLF_size(mono, proxy_size_comp * data->text_size, 72);
+       BLF_size(font, proxy_size_comp * data->text_size, 72);
 
-       BLF_enable(mono, BLF_WORD_WRAP);
+       BLF_enable(font, BLF_WORD_WRAP);
 
        /* use max width to enable newlines only */
-       BLF_wordwrap(mono, (data->wrap_width != 0.0f) ? data->wrap_width * width : -1);
+       BLF_wordwrap(font, (data->wrap_width != 0.0f) ? data->wrap_width * width : -1);
 
-       BLF_buffer(mono, out->rect_float, (unsigned char *)out->rect, width, height, out->channels, display);
+       BLF_buffer(font, out->rect_float, (unsigned char *)out->rect, width, height, out->channels, display);
 
-       line_height = BLF_height_max(mono);
+       line_height = BLF_height_max(font);
 
-       y_ofs = -BLF_descender(mono);
+       y_ofs = -BLF_descender(font);
 
        x = (data->loc[0] * width);
        y = (data->loc[1] * height) + y_ofs;
@@ -3521,7 +3598,7 @@ static ImBuf *do_text_effect(
                        rctf rect;
                } wrap;
 
-               BLF_boundbox_ex(mono, data->text, sizeof(data->text), &wrap.rect, &wrap.info);
+               BLF_boundbox_ex(font, data->text, sizeof(data->text), &wrap.rect, &wrap.info);
 
                if (data->align == SEQ_TEXT_ALIGN_X_RIGHT) {
                        x -= BLI_rctf_size_x(&wrap.rect);
@@ -3544,19 +3621,20 @@ static ImBuf *do_text_effect(
        /* BLF_SHADOW won't work with buffers, instead use cheap shadow trick */
        if (data->flag & SEQ_TEXT_SHADOW) {
                int fontx, fonty;
-               fontx = BLF_width_max(mono);
+               fontx = BLF_width_max(font);
                fonty = line_height;
-               BLF_position(mono, x + max_ii(fontx / 25, 1), y + max_ii(fonty / 25, 1), 0.0f);
-               BLF_buffer_col(mono, data->shadow_color);
-               BLF_draw_buffer(mono, data->text, BLF_DRAW_STR_DUMMY_MAX);
+               BLF_position(font, x + max_ii(fontx / 25, 1), y + max_ii(fonty / 25, 1), 0.0f);
+               BLF_buffer_col(font, data->shadow_color);
+               BLF_draw_buffer(font, data->text, BLF_DRAW_STR_DUMMY_MAX);
        }
-       BLF_position(mono, x, y, 0.0f);
-       BLF_buffer_col(mono, data->color);
-       BLF_draw_buffer(mono, data->text, BLF_DRAW_STR_DUMMY_MAX);
 
-       BLF_buffer(mono, NULL, NULL, 0, 0, 0, NULL);
+       BLF_position(font, x, y, 0.0f);
+       BLF_buffer_col(font, data->color);
+       BLF_draw_buffer(font, data->text, BLF_DRAW_STR_DUMMY_MAX);
 
-       BLF_disable(mono, BLF_WORD_WRAP);
+       BLF_buffer(font, NULL, NULL, 0, 0, 0, NULL);
+
+       BLF_disable(font, BLF_WORD_WRAP);
 
        return out;
 }
@@ -3803,8 +3881,9 @@ static struct SeqEffectHandle get_sequence_effect_impl(int seq_type)
                case SEQ_TYPE_TEXT:
                        rval.num_inputs = num_inputs_text;
                        rval.init = init_text_effect;
-                       rval.free = free_effect_default;
-                       rval.copy = copy_effect_default;
+                       rval.free = free_text_effect;
+                       rval.load = load_text_effect;
+                       rval.copy = copy_text_effect;
                        rval.early_out = early_out_text;
                        rval.execute = do_text_effect;
                        break;
@@ -3833,8 +3912,15 @@ struct SeqEffectHandle BKE_sequence_get_blend(Sequence *seq)
        struct SeqEffectHandle rval = {false, false, NULL};
 
        if (seq->blend_mode != 0) {
+               if ((seq->flag & SEQ_EFFECT_NOT_LOADED) != 0) {
+                       /* load the effect first */
+                       rval = get_sequence_effect_impl(seq->type);
+                       rval.load(seq);
+               }
+
                rval = get_sequence_effect_impl(seq->blend_mode);
                if ((seq->flag & SEQ_EFFECT_NOT_LOADED) != 0) {
+                       /* now load the blend and unset unloaded flag */
                        rval.load(seq);
                        seq->flag &= ~SEQ_EFFECT_NOT_LOADED;
                }
index 92e974b..4d79e55 100644 (file)
@@ -396,7 +396,13 @@ static void sequence_clipboard_pointers(Main *bmain, Sequence *seq, void (*callb
        callback(bmain, (ID **)&seq->clip);
        callback(bmain, (ID **)&seq->mask);
        callback(bmain, (ID **)&seq->sound);
+
+       if (seq->type == SEQ_TYPE_TEXT && seq->effectdata) {
+               TextVars *text_data = seq->effectdata;
+               callback(bmain, (ID **)&text_data->text_font);
+       }
 }
+
 /* recursive versions of functions above */
 void BKE_sequencer_base_clipboard_pointers_free(ListBase *seqbase)
 {
index 27bdc1b..9640d44 100644 (file)
@@ -5864,6 +5864,10 @@ static void lib_link_scene(FileData *fd, Main *main)
                                                seq->scene_sound = BKE_sound_add_scene_sound_defaults(sce, seq);
                                        }
                                }
+                               if (seq->type == SEQ_TYPE_TEXT) {
+                                       TextVars *t = seq->effectdata;
+                                       t->text_font = newlibadr_us(fd, sce->id.lib, t->text_font);
+                               }
                                BLI_listbase_clear(&seq->anims);
 
                                lib_link_sequence_modifiers(fd, sce, &seq->modifiers);
@@ -6116,6 +6120,11 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                                s->frameMap = NULL;
                        }
 
+                       if (seq->type == SEQ_TYPE_TEXT) {
+                               TextVars *t = seq->effectdata;
+                               t->text_blf_id = SEQ_FONT_NOT_LOADED;
+                       }
+
                        seq->prop = newdataadr(fd, seq->prop);
                        IDP_DirectLinkGroup_OrFree(&seq->prop, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
 
@@ -9742,6 +9751,11 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
                        if (seq->clip) expand_doit(fd, mainvar, seq->clip);
                        if (seq->mask) expand_doit(fd, mainvar, seq->mask);
                        if (seq->sound) expand_doit(fd, mainvar, seq->sound);
+
+                       if (seq->type == SEQ_TYPE_TEXT && seq->effectdata) {
+                               TextVars *data = seq->effectdata;
+                               expand_doit(fd, mainvar, data->text_font);
+                       }
                } SEQ_END;
        }
 
index 636f0e1..0718a77 100644 (file)
@@ -678,7 +678,7 @@ static void template_ID(
                but = NULL;
 
                if (unlinkop) {
-                       but = uiDefIconButO(block, UI_BTYPE_BUT, unlinkop, WM_OP_INVOKE_REGION_WIN, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL);
+                       but = uiDefIconButO(block, UI_BTYPE_BUT, unlinkop, WM_OP_INVOKE_DEFAULT, ICON_X, 0, 0, UI_UNIT_X, UI_UNIT_Y, NULL);
                        /* so we can access the template from operators, font unlinking needs this */
                        UI_but_funcN_set(but, NULL, MEM_dupallocN(template_ui), NULL);
                }
index 4a1a9c0..6cb3add 100644 (file)
@@ -45,6 +45,7 @@
 #include "DNA_color_types.h"
 #include "DNA_listBase.h"
 #include "DNA_vec_types.h"
+#include "DNA_vfont_types.h"
 
 struct Ipo;
 struct Scene;
@@ -274,13 +275,15 @@ typedef struct GaussianBlurVars {
 
 typedef struct TextVars {
        char text[512];
+       VFont *text_font;
+       int text_blf_id;
        int text_size;
        float color[4], shadow_color[4];
        float loc[2];
        float wrap_width;
        char flag;
        char align, align_y;
-       char pad[5];
+       char pad[1];
 } TextVars;
 
 /* TextVars.flag */
@@ -302,6 +305,8 @@ enum {
        SEQ_TEXT_ALIGN_Y_BOTTOM = 2,
 };
 
+#define SEQ_FONT_NOT_LOADED -2
+
 typedef struct ColorMixVars {
        int blend_effect;    /* value from SEQ_TYPE_XXX enumeration */
        float factor;        /* blend factor [0.0f, 1.0f]           */
index 5057d62..652f2b2 100644 (file)
@@ -32,6 +32,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_sequence_types.h"
 #include "DNA_movieclip_types.h"
+#include "DNA_vfont_types.h"
 
 #include "BLI_math.h"
 
@@ -461,6 +462,19 @@ static void rna_SequenceCrop_update(Main *UNUSED(bmain), Scene *UNUSED(scene), P
        BKE_sequence_invalidate_cache(scene, seq);
 }
 
+static void rna_Sequence_text_font_set(PointerRNA *ptr, PointerRNA ptr_value)
+{
+       Sequence *seq = ptr->data;
+       TextVars *data = seq->effectdata;
+       VFont *value = ptr_value.data;
+
+       BKE_sequencer_text_font_unload(data, true);
+
+       id_us_plus(&value->id);
+       data->text_blf_id = SEQ_FONT_NOT_LOADED;
+       data->text_font = value;
+}
+
 /* name functions that ignore the first two characters */
 static void rna_Sequence_name_get(PointerRNA *ptr, char *value)
 {
@@ -2399,6 +2413,14 @@ static void rna_def_text(StructRNA *srna)
 
        RNA_def_struct_sdna_from(srna, "TextVars", "effectdata");
 
+       prop = RNA_def_property(srna, "font", PROP_POINTER, PROP_NONE);
+       RNA_def_property_pointer_sdna(prop, NULL, "text_font");
+       RNA_def_property_ui_icon(prop, ICON_FILE_FONT, false);
+       RNA_def_property_ui_text(prop, "Font", "Font of the text. Falls back to the UI font by default");
+       RNA_def_property_flag(prop, PROP_EDITABLE);
+       RNA_def_property_pointer_funcs(prop, NULL, "rna_Sequence_text_font_set", NULL, NULL);
+       RNA_def_property_update(prop, NC_SCENE | ND_SEQUENCER, "rna_Sequence_update");
+
        prop = RNA_def_property(srna, "font_size", PROP_INT, PROP_UNSIGNED);
        RNA_def_property_int_sdna(prop, NULL, "text_size");
        RNA_def_property_ui_text(prop, "Size", "Size of the text");