From 8830cfe5416084caf98fa7c7981a0c337f35548b Mon Sep 17 00:00:00 2001 From: Yuki Hashimoto Date: Mon, 9 Aug 2021 14:27:35 +1000 Subject: 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 --- source/blender/editors/curve/editfont.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) (limited to 'source') 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; -- cgit v1.2.3