[Bf-blender-cvs] [8830cfe5416] master: Fix text object inserting multiple characters with a selection

Yuki Hashimoto noreply at git.blender.org
Mon Aug 9 06:32:56 CEST 2021


Commit: 8830cfe5416084caf98fa7c7981a0c337f35548b
Author: Yuki Hashimoto
Date:   Mon Aug 9 14:27:35 2021 +1000
Branches: master
https://developer.blender.org/rB8830cfe5416084caf98fa7c7981a0c337f35548b

Fix text object inserting multiple characters with a selection

`bpy.ops.font.text_insert(text="multiple characters")` wasn't working.

When the text is selected does not correctly insert multiple characters.

- When the text was selected from left to right,
  the cursor only move one position next to the selected text.
- When the text is selected from right to left,
  a part of the selected text remain.

Ref D12161

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

M	source/blender/editors/curve/editfont.c

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

diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c
index e43e4194c51..39fb2882e4b 100644
--- a/source/blender/editors/curve/editfont.c
+++ b/source/blender/editors/curve/editfont.c
@@ -439,37 +439,27 @@ static void text_update_edited(bContext *C, Object *obedit, int mode)
   WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
 }
 
-static int kill_selection(Object *obedit, int ins) /* 1 == new character */
+static int kill_selection(Object *obedit, int ins) /* ins == new character len */
 {
   Curve *cu = obedit->data;
   EditFont *ef = cu->editfont;
   int selend, selstart, direction;
-  int offset = 0;
   int getfrom;
 
   direction = BKE_vfont_select_get(obedit, &selstart, &selend);
   if (direction) {
     int size;
-    if (ins) {
-      offset = 1;
-    }
     if (ef->pos >= selstart) {
-      ef->pos = selstart + offset;
+      ef->pos = selstart + ins;
     }
     if ((direction == -1) && ins) {
-      selstart++;
-      selend++;
-    }
-    getfrom = selend + offset;
-    if (ins == 0) {
-      getfrom++;
-    }
-    size = (ef->len * sizeof(*ef->textbuf)) - (selstart * sizeof(*ef->textbuf)) +
-           (offset * sizeof(*ef->textbuf));
-    memmove(ef->textbuf + selstart, ef->textbuf + getfrom, size);
-    memmove(ef->textbufinfo + selstart,
-            ef->textbufinfo + getfrom,
-            ((ef->len - selstart) + offset) * sizeof(CharInfo));
+      selstart += ins;
+      selend += ins;
+    }
+    getfrom = selend + 1;
+    size = ef->len - selend; /* This is equivalent to: `(ef->len - getfrom) + 1(null)`. */
+    memmove(ef->textbuf + selstart, ef->textbuf + getfrom, sizeof(*ef->textbuf) * size);
+    memmove(ef->textbufinfo + selstart, ef->textbufinfo + getfrom, sizeof(CharInfo) * size);
     ef->len -= ((selend - selstart) + 1);
     ef->selstart = ef->selend = 0;
   }
@@ -1650,7 +1640,7 @@ static int insert_text_exec(bContext *C, wmOperator *op)
   MEM_freeN(inserted_text);
   MEM_freeN(inserted_utf8);
 
-  kill_selection(obedit, 1);
+  kill_selection(obedit, len);
   text_update_edited(C, obedit, FO_EDIT);
 
   return OPERATOR_FINISHED;



More information about the Bf-blender-cvs mailing list