diff options
Diffstat (limited to 'source/blender/editors/curve/editfont.c')
-rw-r--r-- | source/blender/editors/curve/editfont.c | 226 |
1 files changed, 98 insertions, 128 deletions
diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c index ea6b6154be8..7c1fe0cadf0 100644 --- a/source/blender/editors/curve/editfont.c +++ b/source/blender/editors/curve/editfont.c @@ -51,6 +51,7 @@ #include "BKE_curve.h" #include "BKE_depsgraph.h" #include "BKE_font.h" +#include "BKE_library.h" #include "BKE_main.h" #include "BKE_object.h" #include "BKE_report.h" @@ -344,50 +345,18 @@ static bool font_paste_utf8(bContext *C, const char *str, const size_t str_len) static int paste_from_file(bContext *C, ReportList *reports, const char *filename) { Object *obedit = CTX_data_edit_object(C); - FILE *fp; char *strp; - int filelen; + size_t filelen; int retval; - fp = BLI_fopen(filename, "r"); - - if (!fp) { + strp = BLI_file_read_text_as_mem(filename, 1, &filelen); + if (strp == NULL) { BKE_reportf(reports, RPT_ERROR, "Failed to open file '%s'", filename); return OPERATOR_CANCELLED; } + strp[filelen] = 0; - fseek(fp, 0L, SEEK_END); - - errno = 0; - filelen = ftell(fp); - if (filelen == -1) { - goto fail; - } - - if (filelen <= MAXTEXT) { - strp = MEM_mallocN(filelen + 4, "tempstr"); - - fseek(fp, 0L, SEEK_SET); - - /* fread() instead of read(), because windows read() converts text - * to DOS \r\n linebreaks, causing double linebreaks in the 3d text */ - errno = 0; - filelen = fread(strp, 1, filelen, fp); - if (filelen == -1) { - MEM_freeN(strp); - goto fail; - } - - strp[filelen] = 0; - } - else { - strp = NULL; - } - - fclose(fp); - - - if (strp && font_paste_utf8(C, strp, filelen)) { + if (font_paste_utf8(C, strp, filelen)) { text_update_edited(C, obedit, FO_EDIT); retval = OPERATOR_FINISHED; @@ -397,18 +366,9 @@ static int paste_from_file(bContext *C, ReportList *reports, const char *filenam retval = OPERATOR_CANCELLED; } - if (strp) { - MEM_freeN(strp); - } + MEM_freeN(strp); return retval; - - - /* failed to seek or read */ -fail: - BKE_reportf(reports, RPT_ERROR, "Failed to read file '%s', %s", filename, strerror(errno)); - fclose(fp); - return OPERATOR_CANCELLED; } static int paste_from_file_exec(bContext *C, wmOperator *op) @@ -449,66 +409,9 @@ void FONT_OT_text_paste_from_file(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; /* properties */ - WM_operator_properties_filesel(ot, FILE_TYPE_FOLDER | FILE_TYPE_TEXT, FILE_SPECIAL, FILE_OPENFILE, - WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); -} - - -/* -------------------------------------------------------------------- */ -/* Paste From Clipboard */ - -static int paste_from_clipboard(bContext *C, ReportList *reports) -{ - Object *obedit = CTX_data_edit_object(C); - char *strp; - int filelen; - int retval; - - strp = WM_clipboard_text_get(false, &filelen); - if (strp == NULL) { - BKE_report(reports, RPT_ERROR, "Clipboard empty"); - return OPERATOR_CANCELLED; - } - - if ((filelen <= MAXTEXT) && font_paste_utf8(C, strp, filelen)) { - text_update_edited(C, obedit, FO_EDIT); - retval = OPERATOR_FINISHED; - } - else { - BKE_report(reports, RPT_ERROR, "Clipboard too long"); - retval = OPERATOR_CANCELLED; - } - MEM_freeN(strp); - - return retval; -} - -static int paste_from_clipboard_exec(bContext *C, wmOperator *op) -{ - int retval; - - retval = paste_from_clipboard(C, op->reports); - - return retval; -} - -void FONT_OT_text_paste_from_clipboard(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Paste Clipboard"; - ot->description = "Paste contents from system clipboard"; - ot->idname = "FONT_OT_text_paste_from_clipboard"; - - /* api callbacks */ - ot->exec = paste_from_clipboard_exec; - ot->poll = ED_operator_editfont; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - WM_operator_properties_filesel(ot, FILE_TYPE_FOLDER | FILE_TYPE_TEXT, FILE_SPECIAL, FILE_OPENFILE, - WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); + WM_operator_properties_filesel( + ot, FILE_TYPE_FOLDER | FILE_TYPE_TEXT, FILE_SPECIAL, FILE_OPENFILE, + WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); } /******************* text to object operator ********************/ @@ -538,7 +441,7 @@ static void txt_add_object(bContext *C, TextLine *firstline, int totline, const cu = obedit->data; cu->vfont = BKE_vfont_builtin_get(); - cu->vfont->id.us++; + id_us_plus(&cu->vfont->id); for (tmp = firstline, a = 0; nbytes < MAXTEXT && a < totline; tmp = tmp->next, a++) { size_t nchars_line, nbytes_line; @@ -799,10 +702,21 @@ static void copy_selection(Object *obedit) if (BKE_vfont_select_get(obedit, &selstart, &selend)) { Curve *cu = obedit->data; EditFont *ef = cu->editfont; - - memcpy(ef->copybuf, ef->textbuf + selstart, ((selend - selstart) + 1) * sizeof(wchar_t)); - ef->copybuf[(selend - selstart) + 1] = 0; - memcpy(ef->copybufinfo, ef->textbufinfo + selstart, ((selend - selstart) + 1) * sizeof(CharInfo)); + char *buf = NULL; + wchar_t *text_buf; + size_t len_utf8; + + /* internal clipboard (for style) */ + BKE_vfont_clipboard_set(ef->textbuf + selstart, ef->textbufinfo + selstart, selend - selstart + 1); + BKE_vfont_clipboard_get(&text_buf, NULL, &len_utf8, NULL); + + /* system clipboard */ + buf = MEM_mallocN(len_utf8 + 1, __func__); + if (buf) { + BLI_strncpy_wchar_as_utf8(buf, text_buf, len_utf8 + 1); + WM_clipboard_text_set(buf, false); + MEM_freeN(buf); + } } } @@ -864,11 +778,13 @@ void FONT_OT_text_cut(wmOperatorType *ot) static bool paste_selection(Object *obedit, ReportList *reports) { - Curve *cu = obedit->data; - EditFont *ef = cu->editfont; - int len = wcslen(ef->copybuf); + wchar_t *text_buf; + CharInfo *info_buf; + size_t len; + + BKE_vfont_clipboard_get(&text_buf, &info_buf, NULL, &len); - if (font_paste_wchar(obedit, ef->copybuf, len, ef->copybufinfo)) { + if (font_paste_wchar(obedit, text_buf, len, info_buf)) { return true; } else { @@ -880,13 +796,68 @@ static bool paste_selection(Object *obedit, ReportList *reports) static int paste_text_exec(bContext *C, wmOperator *op) { Object *obedit = CTX_data_edit_object(C); + int retval; + size_t len_utf8; + wchar_t *text_buf; + + /* Store both clipboards as utf8 for comparison, + * Give priority to the internal 'vfont' clipboard with its 'CharInfo' text styles + * as long as its synchronized with the systems clipboard. */ + struct { + char *buf; + int len; + } clipboard_system = {NULL}, clipboard_vfont = {NULL}; + + clipboard_system.buf = WM_clipboard_text_get(false, &clipboard_system.len); - if (!paste_selection(obedit, op->reports)) + if (clipboard_system.buf == NULL) { return OPERATOR_CANCELLED; + } - text_update_edited(C, obedit, FO_EDIT); + BKE_vfont_clipboard_get(&text_buf, NULL, &len_utf8, NULL); - return OPERATOR_FINISHED; + if (text_buf) { + clipboard_vfont.buf = MEM_mallocN(len_utf8 + 1, __func__); + + if (clipboard_vfont.buf == NULL) { + MEM_freeN(clipboard_system.buf); + return OPERATOR_CANCELLED; + } + + BLI_strncpy_wchar_as_utf8(clipboard_vfont.buf, text_buf, len_utf8 + 1); + } + + if (clipboard_vfont.buf && STREQ(clipboard_vfont.buf, clipboard_system.buf)) { + retval = paste_selection(obedit, op->reports) ? OPERATOR_FINISHED : OPERATOR_CANCELLED; + } + else { + if ((clipboard_system.len <= MAXTEXT) && + font_paste_utf8(C, clipboard_system.buf, clipboard_system.len)) + { + text_update_edited(C, obedit, FO_EDIT); + retval = OPERATOR_FINISHED; + } + else { + BKE_report(op->reports, RPT_ERROR, "Clipboard too long"); + retval = OPERATOR_CANCELLED; + } + + /* free the existent clipboard buffer */ + BKE_vfont_clipboard_free(); + } + + if (retval != OPERATOR_CANCELLED) { + text_update_edited(C, obedit, FO_EDIT); + } + + /* cleanup */ + if (clipboard_vfont.buf) { + MEM_freeN(clipboard_vfont.buf); + } + + MEM_freeN(clipboard_system.buf); + + return retval; } void FONT_OT_text_paste(wmOperatorType *ot) @@ -1509,7 +1480,7 @@ void FONT_OT_textbox_remove(wmOperatorType *ot) /***************** editmode enter/exit ********************/ -void make_editText(Object *obedit) +void ED_curve_editfont_make(Object *obedit) { Curve *cu = obedit->data; EditFont *ef = cu->editfont; @@ -1520,8 +1491,6 @@ void make_editText(Object *obedit) ef->textbuf = MEM_callocN((MAXTEXT + 4) * sizeof(wchar_t), "texteditbuf"); ef->textbufinfo = MEM_callocN((MAXTEXT + 4) * sizeof(CharInfo), "texteditbufinfo"); - ef->copybuf = MEM_callocN((MAXTEXT + 4) * sizeof(wchar_t), "texteditcopybuf"); - ef->copybufinfo = MEM_callocN((MAXTEXT + 4) * sizeof(CharInfo), "texteditcopybufinfo"); } /* Convert the original text to wchar_t */ @@ -1545,7 +1514,7 @@ void make_editText(Object *obedit) BKE_vfont_select_clamp(obedit); } -void load_editText(Object *obedit) +void ED_curve_editfont_load(Object *obedit) { Curve *cu = obedit->data; EditFont *ef = cu->editfont; @@ -1574,7 +1543,7 @@ void load_editText(Object *obedit) cu->selend = ef->selend; } -void free_editText(Object *obedit) +void ED_curve_editfont_free(Object *obedit) { BKE_curve_editfont_free((Curve *)obedit->data); } @@ -1727,7 +1696,7 @@ static int font_open_exec(bContext *C, wmOperator *op) if (pprop->prop) { /* when creating new ID blocks, use is already 1, but RNA * pointer se also increases user, so this compensates it */ - font->id.us--; + id_us_min(&font->id); RNA_id_pointer_create(&font->id, &idptr); RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr); @@ -1784,8 +1753,9 @@ void FONT_OT_open(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; /* properties */ - WM_operator_properties_filesel(ot, FILE_TYPE_FOLDER | FILE_TYPE_FTFONT, FILE_SPECIAL, FILE_OPENFILE, - WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); + WM_operator_properties_filesel( + ot, FILE_TYPE_FOLDER | FILE_TYPE_FTFONT, FILE_SPECIAL, FILE_OPENFILE, + WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); } /******************* delete operator *********************/ @@ -1885,7 +1855,7 @@ void undo_push_font(bContext *C, const char *name) /** * TextBox selection */ -bool mouse_font(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle) +bool ED_curve_editfont_select_pick(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle) { Object *obedit = CTX_data_edit_object(C); Curve *cu = obedit->data; |