merge with/from trunk at r35190
[blender.git] / source / blender / blenkernel / intern / font.c
index 501de66800010c4e6738e631fa192ba2fd9f386d..05988605b4c825d5b3b02e478dc4f3e7251df7e5 100644 (file)
@@ -41,6 +41,7 @@
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_vfontdata.h"
+#include "BLI_utildefines.h"
 
 #include "DNA_packedFile_types.h"
 #include "DNA_curve_types.h"
@@ -49,9 +50,7 @@
 #include "DNA_object_types.h"
 
 #include "BKE_utildefines.h"
-
 #include "BKE_packedFile.h"
-
 #include "BKE_library.h"
 #include "BKE_font.h"
 #include "BKE_global.h"
@@ -97,13 +96,14 @@ chtoutf8(unsigned long c, char *o)
 void
 wcs2utf8s(char *dst, wchar_t *src)
 {
-       char ch[5];
+       /* NULL terminator not needed */
+       char ch[4];
 
        while(*src)
        {
-               memset(ch, 0, 5);
+               memset(ch, 0, sizeof(ch));
                chtoutf8(*src++, ch);
-               strcat(dst, ch);
+               dst= strncat(dst, ch, sizeof(ch));
        }
 }
 
@@ -124,31 +124,27 @@ wcsleninu8(wchar_t *src)
 }
 
 static int
-utf8slen(char *src)
+utf8slen(const char *strc)
 {
-       int size = 0, index = 0;
-       unsigned char c;
-       
-       c = src[index++];
-       while(c)
-       {    
-               if((c & 0x80) == 0)
-               {
-                       index += 0;
-               }
-               else if((c & 0xe0) == 0xe0)
-               {
-                       index += 2;
-               }
-               else
-               {
-                       index += 1;
+       int len=0;
+
+       while(*strc) {
+               if ((*strc & 0xe0) == 0xc0) {
+                       if((strc[1] & 0x80) && (strc[1] & 0x40) == 0x00)
+                               strc++;
+               } else if ((*strc & 0xf0) == 0xe0) {
+                       if((strc[1] & strc[2] & 0x80) && ((strc[1] | strc[2]) & 0x40) == 0x00)
+                               strc += 2;
+               } else if ((*strc & 0xf8) == 0xf0) {
+                       if((strc[1] & strc[2] & strc[3] & 0x80) && ((strc[1] | strc[2] | strc[3]) & 0x40) == 0x00)
+                               strc += 3;
                }
-               size += 1;
-               c = src[index++];               
+
+               strc++;
+               len++;
        }
-       
-       return size;
+
+       return len;
 }
 
 
@@ -212,7 +208,7 @@ int utf8towchar(wchar_t *w, char *c)
 /* The vfont code */
 void free_vfont(struct VFont *vf)
 {
-       if (vf == 0) return;
+       if (vf == NULL) return;
 
        if (vf->data) {
                while(vf->data->characters.first)
@@ -305,7 +301,7 @@ static VFontData *vfont_get_data(VFont *vfont)
        if (!vfont->data) {
                PackedFile *pf;
                
-               if (BLI_streq(vfont->name, "<builtin>")) {
+               if (strcmp(vfont->name, FO_BUILTIN_NAME)==0) {
                        pf= get_builtin_packedfile();
                } else {
                        if (vfont->packedfile) {
@@ -342,7 +338,7 @@ static VFontData *vfont_get_data(VFont *vfont)
                        if(!pf) {
                                printf("Font file doesn't exist: %s\n", vfont->name);
 
-                               strcpy(vfont->name, "<builtin>");
+                               strcpy(vfont->name, FO_BUILTIN_NAME);
                                pf= get_builtin_packedfile();
                        }
                }
@@ -358,7 +354,7 @@ static VFontData *vfont_get_data(VFont *vfont)
        return vfont->data;     
 }
 
-VFont *load_vfont(char *name)
+VFont *load_vfont(const char *name)
 {
        char filename[FILE_MAXFILE];
        VFont *vfont= NULL;
@@ -367,15 +363,15 @@ VFont *load_vfont(char *name)
        int is_builtin;
        struct TmpFont *tmpfnt;
        
-       if (BLI_streq(name, "<builtin>")) {
-               strcpy(filename, name);
+       if (strcmp(name, FO_BUILTIN_NAME)==0) {
+               BLI_strncpy(filename, name, sizeof(filename));
                
                pf= get_builtin_packedfile();
                is_builtin= 1;
        } else {
                char dir[FILE_MAXDIR];
                
-               strcpy(dir, name);
+               BLI_strncpy(dir, name, sizeof(dir));
                BLI_splitdirstring(dir, filename);
 
                pf= newPackedFile(NULL, name);
@@ -394,7 +390,7 @@ VFont *load_vfont(char *name)
 
                        /* if there's a font name, use it for the ID name */
                        if (strcmp(vfd->name, "")!=0) {
-                               BLI_strncpy(vfont->id.name+2, vfd->name, 21);
+                               BLI_strncpy(vfont->id.name+2, vfd->name, sizeof(vfont->id.name)-2);
                        }
                        BLI_strncpy(vfont->name, name, sizeof(vfont->name));
 
@@ -403,8 +399,8 @@ VFont *load_vfont(char *name)
                                vfont->packedfile = pf;
                        }
                        
-                       // Do not add <builtin> to temporary listbase
-                       if(strcmp(filename, "<builtin>"))
+                       // Do not add FO_BUILTIN_NAME to temporary listbase
+                       if(strcmp(filename, FO_BUILTIN_NAME))
                        {
                                tmpfnt= (struct TmpFont *) MEM_callocN(sizeof(struct TmpFont), "temp_font");
                                tmpfnt->pf= tpf;
@@ -443,10 +439,10 @@ VFont *get_builtin_font(void)
        VFont *vf;
        
        for (vf= G.main->vfont.first; vf; vf= vf->id.next)
-               if (BLI_streq(vf->name, "<builtin>"))
+               if (strcmp(vf->name, FO_BUILTIN_NAME)==0)
                        return vf;
        
-       return load_vfont("<builtin>");
+       return load_vfont(FO_BUILTIN_NAME);
 }
 
 static VChar *find_vfont_char(VFontData *vfd, intptr_t character)
@@ -480,7 +476,7 @@ static void build_underline(Curve *cu, float x1, float y1, float x2, float y2, i
        nu2->flagu = CU_NURB_CYCLIC;
 
        bp = (BPoint*)MEM_callocN(4 * sizeof(BPoint),"underline_bp"); 
-       if (bp == 0){
+       if (bp == NULL){
                MEM_freeN(nu2);
                return;
        }
@@ -514,11 +510,12 @@ static void buildchar(Curve *cu, unsigned long character, CharInfo *info, float
        float *fp, fsize, shear, x, si, co;
        VFontData *vfd = NULL;
        VChar *che = NULL;
-       int i, sel=0;
+       int i;
 
        vfd= vfont_get_data(which_vfont(cu, info));     
        if (!vfd) return;
 
+       /*
        if (cu->selend < cu->selstart) {
                if ((charidx >= (cu->selend)) && (charidx <= (cu->selstart-2)))
                        sel= 1;
@@ -527,6 +524,7 @@ static void buildchar(Curve *cu, unsigned long character, CharInfo *info, float
                if ((charidx >= (cu->selstart-1)) && (charidx <= (cu->selend-1)))
                        sel= 1;
        }
+       */
 
        /* make a copy at distance ofsx,ofsy with shear*/
        fsize= cu->fsize;
@@ -546,10 +544,10 @@ static void buildchar(Curve *cu, unsigned long character, CharInfo *info, float
                bezt1 = nu1->bezt;
                if (bezt1){
                        nu2 =(Nurb*) MEM_mallocN(sizeof(Nurb),"duplichar_nurb");
-                       if (nu2 == 0) break;
+                       if (nu2 == NULL) break;
                        memcpy(nu2, nu1, sizeof(struct Nurb));
                        nu2->resolu= cu->resolu;
-                       nu2->bp = 0;
+                       nu2->bp = NULL;
                        nu2->knotsu = nu2->knotsv = NULL;
                        nu2->flag= CU_SMOOTH;
                        nu2->charidx = charidx;
@@ -564,7 +562,7 @@ static void buildchar(Curve *cu, unsigned long character, CharInfo *info, float
                        i = nu2->pntsu;
 
                        bezt2 = (BezTriple*)MEM_mallocN(i * sizeof(BezTriple),"duplichar_bezt2"); 
-                       if (bezt2 == 0){
+                       if (bezt2 == NULL){
                                MEM_freeN(nu2);
                                break;
                        }
@@ -672,7 +670,7 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
        VFont *vfont, *oldvfont;
        VFontData *vfd= NULL;
        Curve *cu;
-       CharInfo *info, *custrinfo;
+       CharInfo *info = NULL, *custrinfo;
        TextBox *tb;
        VChar *che;
        struct chartrans *chartransdata=NULL, *ct;
@@ -688,18 +686,18 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
        /* renark: do calculations including the trailing '\0' of a string
           because the cursor can be at that location */
 
-       if(ob->type!=OB_FONT) return 0;
+       if(ob->type!=OB_FONT) return NULL;
 
        // Set font data
        cu= (Curve *) ob->data;
        vfont= cu->vfont;
        
-       if(cu->str == NULL) return 0;
-       if(vfont == NULL) return 0;
+       if(cu->str == NULL) return NULL;
+       if(vfont == NULL) return NULL;
 
        // Create unicode string
        utf8len = utf8slen(cu->str);
-       tmp = mem = MEM_callocN(((utf8len + 1) * sizeof(wchar_t)), "convertedmem");
+       mem = MEM_callocN(((utf8len + 1) * sizeof(wchar_t)), "convertedmem");
        
        utf8towchar(mem, cu->str);
 
@@ -725,7 +723,7 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
        if(!vfd) {
                if(mem)
                        MEM_freeN(mem); 
-               return 0;
+               return NULL;
        }
 
        /* calc offset and rotation of each char */
@@ -760,7 +758,6 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
        for (i = 0 ; i<=slen ; i++) {
        makebreak:
                // Characters in the list
-               che = vfd->characters.first;
                info = &(custrinfo[i]);
                ascii = mem[i];
                if(info->flag & CU_CHINFO_SMALLCAPS) {
@@ -779,10 +776,10 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
 
                /*
                 * The character wasn't in the current curve base so load it
-                * But if the font is <builtin> then do not try loading since
+                * But if the font is FO_BUILTIN_NAME then do not try loading since
                 * whole font is in the memory already
                 */
-               if(che == NULL && strcmp(vfont->name, "<builtin>"))     {
+               if(che == NULL && strcmp(vfont->name, FO_BUILTIN_NAME)) {
                        BLI_vfontchar_from_freetypefont(vfont, ascii);
                }
 
@@ -790,11 +787,11 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
                che= find_vfont_char(vfd, ascii);
 
                /* No VFont found */
-               if (vfont==0) {
+               if (vfont==NULL) {
                        if(mem)
                                MEM_freeN(mem);
                        MEM_freeN(chartransdata);
-                       return 0;
+                       return NULL;
                }
 
                if (vfont != oldvfont) {
@@ -807,7 +804,7 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
                        if(mem)
                                MEM_freeN(mem);
                        MEM_freeN(chartransdata);
-                       return 0;
+                       return NULL;
                }
 
                twidth = char_width(cu, che, info);
@@ -862,12 +859,15 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
                                yof= cu->yof + tb->y/cu->fsize;
                        }
 
+                       /* XXX, has been unused for years, need to check if this is useful, r4613 r5282 - campbell */
+#if 0
                        if(ascii == '\n' || ascii == '\r')
                                xof = cu->xof;
                        else
                                xof= cu->xof + (tb->x/cu->fsize);
-
+#else
                        xof= cu->xof + (tb->x/cu->fsize);
+#endif
                        lnr++;
                        cnr= 0;
                        wsnr= 0;
@@ -1042,10 +1042,9 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
                                che= find_vfont_char(vfd, ascii);
        
                                twidth = char_width(cu, che, info);
-                               
-                               dtime= distfac*0.35f*twidth;    /* why not 0.5? */
-                               dtime= distfac*0.5f*twidth;     /* why not 0.5? */
-                               
+
+                               dtime= distfac*0.5f*twidth;
+
                                ctime= timeofs + distfac*( ct->xof - minx);
                                CLAMP(ctime, 0.0, 1.0);
 
@@ -1146,16 +1145,14 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
                return NULL;
        }
 
-       if(mode==0) {
+       if(mode == FO_EDIT) {
                /* make nurbdata */
-               unsigned long cha;
-               
                freeNurblist(&cu->nurb);
                
                ct= chartransdata;
                if (cu->sepchar==0) {
                        for (i= 0; i<slen; i++) {
-                               cha = (uintptr_t) mem[i];
+                               unsigned long cha = (uintptr_t) mem[i];
                                info = &(custrinfo[i]);
                                if (info->mat_nr > (ob->totcol)) {
                                        /* printf("Error: Illegal material index (%d) in text object, setting to 0\n", info->mat_nr); */
@@ -1193,8 +1190,12 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
                                ascii = mem[i];
                                info = &(custrinfo[i]);
                                if (cu->sepchar == (i+1)) {
-                                       float vecyo[3]= {ct->xof, ct->yof, 0.0f};
-                                       
+                                       float vecyo[3];
+
+                                       vecyo[0]= ct->xof;
+                                       vecyo[1]= ct->yof;
+                                       vecyo[2]= 0.0f;
+
                                        mem[0] = ascii;
                                        mem[1] = 0;
                                        custrinfo[0]= *info;
@@ -1218,7 +1219,5 @@ struct chartrans *BKE_text_to_curve(Scene *scene, Object *ob, int mode)
                MEM_freeN(mem);
 
        MEM_freeN(chartransdata);
-       return 0;
+       return NULL;
 }
-
-