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
This commit is contained in:
Philipp Oeser
2023-09-28 11:46:52 +02:00
committed by Philipp Oeser
parent 9e79487c4f
commit 16e4eeb9c0
8 changed files with 27 additions and 19 deletions

View File

@@ -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

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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.
*

View File

@@ -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;
}

View File

@@ -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;
}
}
}

View File

@@ -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];

View File

@@ -318,13 +318,13 @@ static void rna_Curve_material_index_range(
static int rna_ChariInfo_material_index_get(PointerRNA *ptr)
{
CharInfo *info = static_cast<CharInfo *>(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<CharInfo *>(ptr->data);
info->mat_nr = value + 1;
info->mat_nr = value;
}
static void rna_Curve_active_textbox_index_range(