From 16e4eeb9c0260fb93f7b2cf0b4890ba6dbccae36 Mon Sep 17 00:00:00 2001 From: Philipp Oeser Date: Thu, 28 Sep 2023 11:46:52 +0200 Subject: [PATCH] Text objects: make CharInfo mat_nr zero-based For text objects, the CharInfo mat_nr material index used to start at 1 (not at zero like for meshes or nurbs). Code was mostly considering this (but not in all places, so material index handling (removing/moving) could still go wrong. As an alternative to !109746 (where it was made sure all places would make the right assumption about mat_nr starting at 1), this PR now changes the mat_nr to be zero-based. This is more in line with other places handling material indices. Versioning code is in place to properly convert old files. Fixes #109491 Pull Request: https://projects.blender.org/blender/blender/pulls/112954 --- source/blender/blenkernel/BKE_blender_version.h | 2 +- source/blender/blenkernel/intern/curve.cc | 6 +----- source/blender/blenkernel/intern/vfont.cc | 6 +++--- .../blender/blenloader/intern/versioning_400.cc | 16 ++++++++++++++++ source/blender/editors/curve/editfont.cc | 9 +++------ source/blender/editors/render/render_shading.cc | 2 +- source/blender/makesdna/DNA_curve_types.h | 1 - source/blender/makesrna/intern/rna_curve.cc | 4 ++-- 8 files changed, 27 insertions(+), 19 deletions(-) diff --git a/source/blender/blenkernel/BKE_blender_version.h b/source/blender/blenkernel/BKE_blender_version.h index ab64945f45e..43ad961a726 100644 --- a/source/blender/blenkernel/BKE_blender_version.h +++ b/source/blender/blenkernel/BKE_blender_version.h @@ -29,7 +29,7 @@ extern "C" { /* Blender file format version. */ #define BLENDER_FILE_VERSION BLENDER_VERSION -#define BLENDER_FILE_SUBVERSION 30 +#define BLENDER_FILE_SUBVERSION 31 /* Minimum Blender version that supports reading file written with the current * version. Older Blender versions will test this and cancel loading the file, showing a warning to diff --git a/source/blender/blenkernel/intern/curve.cc b/source/blender/blenkernel/intern/curve.cc index f967deeea67..b976e211e32 100644 --- a/source/blender/blenkernel/intern/curve.cc +++ b/source/blender/blenkernel/intern/curve.cc @@ -5427,11 +5427,7 @@ void BKE_curve_material_remap(Curve *cu, const uint *remap, uint remap_len) } for (i = 0; i <= charinfo_len; i++) { - if (strinfo[i].mat_nr > 0) { - strinfo[i].mat_nr -= 1; - MAT_NR_REMAP(strinfo[i].mat_nr); - strinfo[i].mat_nr += 1; - } + MAT_NR_REMAP(strinfo[i].mat_nr); } } else { diff --git a/source/blender/blenkernel/intern/vfont.cc b/source/blender/blenkernel/intern/vfont.cc index 2100e437bed..966fd6dbab2 100644 --- a/source/blender/blenkernel/intern/vfont.cc +++ b/source/blender/blenkernel/intern/vfont.cc @@ -448,8 +448,8 @@ static void build_underline(Curve *cu, nu2->bezt = nullptr; nu2->knotsu = nu2->knotsv = nullptr; nu2->charidx = charidx + 1000; - if (mat_nr > 0) { - nu2->mat_nr = mat_nr - 1; + if (mat_nr >= 0) { + nu2->mat_nr = mat_nr; } nu2->pntsu = 4; nu2->pntsv = 1; @@ -538,7 +538,7 @@ void BKE_vfont_build_char(Curve *cu, nu2->flag = CU_SMOOTH; nu2->charidx = charidx; if (info->mat_nr > 0) { - nu2->mat_nr = info->mat_nr - 1; + nu2->mat_nr = info->mat_nr; } else { nu2->mat_nr = 0; diff --git a/source/blender/blenloader/intern/versioning_400.cc b/source/blender/blenloader/intern/versioning_400.cc index 694304d2c1f..316337b29a0 100644 --- a/source/blender/blenloader/intern/versioning_400.cc +++ b/source/blender/blenloader/intern/versioning_400.cc @@ -18,6 +18,7 @@ #include "DNA_brush_types.h" #include "DNA_camera_types.h" #include "DNA_defaults.h" +#include "DNA_curve_types.h" #include "DNA_light_types.h" #include "DNA_lightprobe_types.h" #include "DNA_modifier_types.h" @@ -42,6 +43,7 @@ #include "BKE_armature.h" #include "BKE_attribute.h" +#include "BKE_curve.h" #include "BKE_effect.h" #include "BKE_grease_pencil.hh" #include "BKE_idprop.hh" @@ -1614,6 +1616,20 @@ void blo_do_versions_400(FileData *fd, Library * /*lib*/, Main *bmain) } } + if (!MAIN_VERSION_FILE_ATLEAST(bmain, 400, 31)) { + LISTBASE_FOREACH (Curve *, curve, &bmain->curves) { + const int curvetype = BKE_curve_type_get(curve); + if (curvetype == OB_FONT) { + CharInfo *info = curve->strinfo; + for (int i = curve->len_char32 - 1; i >= 0; i--, info++) { + if (info->mat_nr > 0) { + /** CharInfo mat_nr used to start at 1, unlike mesh & nurbs, now zero-based. */ + info->mat_nr--; + } + } + } + } + } /** * Versioning code until next subversion bump goes here. * diff --git a/source/blender/editors/curve/editfont.cc b/source/blender/editors/curve/editfont.cc index 91b52887683..2f93b60832b 100644 --- a/source/blender/editors/curve/editfont.cc +++ b/source/blender/editors/curve/editfont.cc @@ -385,7 +385,7 @@ static int insert_into_textbuf(Object *obedit, uintptr_t c) ef->textbuf[ef->pos] = c; ef->textbufinfo[ef->pos] = cu->curinfo; ef->textbufinfo[ef->pos].kern = 0.0f; - ef->textbufinfo[ef->pos].mat_nr = obedit->actcol; + ef->textbufinfo[ef->pos].mat_nr = obedit->actcol - 1; ef->pos++; ef->len++; @@ -418,10 +418,7 @@ static void text_update_edited(bContext *C, Object *obedit, const eEditFontMode cu->curinfo = ef->textbufinfo[ef->pos ? ef->pos - 1 : 0]; if (obedit->totcol > 0) { - obedit->actcol = cu->curinfo.mat_nr; - - /* since this array is calloc'd, it can be 0 even though we try ensure - * (mat_nr > 0) almost everywhere */ + obedit->actcol = cu->curinfo.mat_nr + 1; if (obedit->actcol < 1) { obedit->actcol = 1; } @@ -1851,7 +1848,7 @@ static void font_cursor_set_apply(bContext *C, const wmEvent *event) cu->curinfo = ef->textbufinfo[ef->pos ? ef->pos - 1 : 0]; if (ob->totcol > 0) { - ob->actcol = cu->curinfo.mat_nr; + ob->actcol = cu->curinfo.mat_nr + 1; if (ob->actcol < 1) { ob->actcol = 1; } diff --git a/source/blender/editors/render/render_shading.cc b/source/blender/editors/render/render_shading.cc index 2ee32a84f7a..8191d91664e 100644 --- a/source/blender/editors/render/render_shading.cc +++ b/source/blender/editors/render/render_shading.cc @@ -347,7 +347,7 @@ static int material_slot_assign_exec(bContext *C, wmOperator * /*op*/) if (ef && BKE_vfont_select_get(ob, &selstart, &selend)) { for (i = selstart; i <= selend; i++) { changed = true; - ef->textbufinfo[i].mat_nr = mat_nr_active + 1; + ef->textbufinfo[i].mat_nr = mat_nr_active; } } } diff --git a/source/blender/makesdna/DNA_curve_types.h b/source/blender/makesdna/DNA_curve_types.h index 81c4e60980e..6b60b6ebf28 100644 --- a/source/blender/makesdna/DNA_curve_types.h +++ b/source/blender/makesdna/DNA_curve_types.h @@ -157,7 +157,6 @@ typedef struct Nurb { typedef struct CharInfo { float kern; - /** Index start at 1, unlike mesh & nurbs. */ short mat_nr; char flag; char _pad[1]; diff --git a/source/blender/makesrna/intern/rna_curve.cc b/source/blender/makesrna/intern/rna_curve.cc index 9624bb59512..242f45db4ef 100644 --- a/source/blender/makesrna/intern/rna_curve.cc +++ b/source/blender/makesrna/intern/rna_curve.cc @@ -318,13 +318,13 @@ static void rna_Curve_material_index_range( static int rna_ChariInfo_material_index_get(PointerRNA *ptr) { CharInfo *info = static_cast(ptr->data); - return info->mat_nr ? info->mat_nr - 1 : 0; + return info->mat_nr ? info->mat_nr : 0; } static void rna_ChariInfo_material_index_set(PointerRNA *ptr, int value) { CharInfo *info = static_cast(ptr->data); - info->mat_nr = value + 1; + info->mat_nr = value; } static void rna_Curve_active_textbox_index_range(