[Bf-blender-cvs] [d753726ce71] blender2.7: Add font selection to VSE text strips

Richard Antalik noreply at git.blender.org
Wed Jan 23 12:10:13 CET 2019


Commit: d753726ce71260296abdccd953fcf95e53b6502f
Author: Richard Antalik
Date:   Sun Jan 13 21:28:07 2019 -0800
Branches: blender2.7
https://developer.blender.org/rBd753726ce71260296abdccd953fcf95e53b6502f

Add font selection to VSE text strips

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

===================================================================

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

===================================================================

diff --git a/release/scripts/startup/bl_ui/space_sequencer.py b/release/scripts/startup/bl_ui/space_sequencer.py
index 47e7b02211a..8d75f695e88 100644
--- a/release/scripts/startup/bl_ui/space_sequencer.py
+++ b/release/scripts/startup/bl_ui/space_sequencer.py
@@ -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()
diff --git a/source/blender/blenfont/BLF_api.h b/source/blender/blenfont/BLF_api.h
index 0a4212ff233..7a6d00299c9 100644
--- a/source/blender/blenfont/BLF_api.h
+++ b/source/blender/blenfont/BLF_api.h
@@ -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();
 
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c
index 75aabf1f713..5fe1a12d7a3 100644
--- a/source/blender/blenfont/intern/blf.c
+++ b/source/blender/blenfont/intern/blf.c
@@ -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;
+		}
 	}
 }
 
diff --git a/source/blender/blenfont/intern/blf_internal_types.h b/source/blender/blenfont/intern/blf_internal_types.h
index 5723f08d44b..8c2e24724f0 100644
--- a/source/blender/blenfont/intern/blf_internal_types.h
+++ b/source/blender/blenfont/intern/blf_internal_types.h
@@ -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;
 
diff --git a/source/blender/blenkernel/BKE_sequencer.h b/source/blender/blenkernel/BKE_sequencer.h
index 2f78b363333..81f9bbebe7d 100644
--- a/source/blender/blenkernel/BKE_sequencer.h
+++ b/source/blender/blenkernel/BKE_sequencer.h
@@ -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
diff --git a/source/blender/blenkernel/intern/library_query.c b/source/blender/blenkernel/intern/library_query.c
index 8a646484e2c..0cca90f3160 100644
--- a/source/blender/blenkernel/intern/library_query.c
+++ b/source/blender/blenkernel/intern/library_query.c
@@ -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;
 				}
 
diff --git a/source/blender/blenkernel/intern/seqeffects.c b/source/blender/blenkernel/intern/seqeffects.c
index 5ac83a65bb6..3cf1ff9e210 100644
--- a/source/blender/blenkernel/intern/seqeffects.c
+++ b/source/blender/blenkernel/intern/seqeffects.c
@@ -37,8 +37,10 @@
 #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) {
 		i

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list