pike.git / src / modules / Image / font.c

version» Context lines:

pike.git/src/modules/Image/font.c:87:    *! int len    *! len * (short char, short value)    *! @}    *!    *! @seealso    *! @[Image], @[Image.Image]    */      #include "image_machine.h"    - #include <sys/types.h> +    #include <sys/stat.h>      #ifdef HAVE_FCNTL_H   #include <fcntl.h>   #endif      #ifdef HAVE_SYS_FCNTL_H   #include <sys/fcntl.h>   #endif   
pike.git/src/modules/Image/font.c:119:      #include "stralloc.h"   #include "object.h"   #include "interpret.h"   #include "svalue.h"   #include "threads.h"   #include "builtin_functions.h"   #include "module_support.h"      #include "image.h" - #ifdef HAVE_UNISTD_H - #include <unistd.h> - #endif +    #ifdef HAVE_MMAP   #include <sys/mman.h>   #endif    - #include "dmalloc.h" -  +    #include "fdlib.h"      #include "bignum.h"         #define sp Pike_sp      extern struct program *font_program;   extern struct program *image_program;   
pike.git/src/modules/Image/font.c:168:    struct _char    {    unsigned long width; /* character rectangle has this width in pixels */    unsigned long spacing; /* pixels to next character */    unsigned char *pixels; /* character rectangle */    } charinfo [1]; /* many!! */   };      /***************** init & exit *********************************/    - static INLINE void free_font_struct(struct font *font) + static inline void free_font_struct(struct font *font)   {    if (font)    {    if (font->mem && font->mem!=image_default_font)    {   #ifdef HAVE_MMAP -  if (font->mmaped_size) { +  if (font->mmaped_size)    munmap(font->mem,font->mmaped_size); -  } else +    #else    free(font->mem);   #endif -  font->mem = NULL; +     }    free(font);    }   }    -  + #ifdef PIKE_NULL_IS_SPECIAL   static void init_font_struct(struct object *UNUSED(o))   {    THIS=NULL;   } -  + #endif      static void exit_font_struct(struct object *UNUSED(obj))   {    free_font_struct(THIS); -  THIS=NULL; +    }      /***************** internals ***********************************/    - static INLINE int char_space(struct font *this, INT32 c) + static inline int char_space(struct font *this, INT32 c)   {    if(c==0x20) -  return DOUBLE_TO_INT((double)(this->height*this->xspacing_scale)/4.5); +  return (int)((double)(this->height*this->xspacing_scale)/4.5);    else if(c==0x20+128) -  return DOUBLE_TO_INT((this->height*this->xspacing_scale)/18); -  return DOUBLE_TO_INT(this->charinfo[c].spacing*this->xspacing_scale); +  return (int)((this->height*this->xspacing_scale)/18); +  return (int)(this->charinfo[c].spacing*this->xspacing_scale);   }    - static INLINE int char_width(struct font *this, INT32 c) + static inline int char_width(struct font *this, INT32 c)   {    if(c==0x20 || c==0x20+128) return 0;    return this->charinfo[c].width;   }    - static INLINE ptrdiff_t my_read(int fd, void *t, size_t towrite) + static inline ptrdiff_t my_read(int fd, void *t, size_t towrite)   {    ptrdiff_t res;    while((res = fd_read(fd, t, towrite)) < 0)    {    switch(errno)    {    case EAGAIN: case EINTR:    continue;       default:    res = 0;    return 0;    }    }    return res;   }    - static INLINE off_t file_size(int fd) + static inline off_t file_size(int fd)   {    PIKE_STAT_T tmp;    if((!fd_fstat(fd, &tmp)) &&    ( tmp.st_mode & S_IFMT) == S_IFREG)    return (off_t)tmp.st_size;    return -1;   }    - static INLINE void write_char(struct _char *ci, + static inline void write_char(struct _char *ci,    rgb_group *pos,    INT32 xsize,    INT32 height)   {    rgb_group *nl;    INT32 x,y;    unsigned char *p;    p=ci->pixels;       for (y=height; y>0; y--)
pike.git/src/modules/Image/font.c:301:    unsigned INT32 cookie;    unsigned INT32 version;    unsigned INT32 chars;    unsigned INT32 height;    unsigned INT32 baseline;    unsigned INT32 o[1];    } *fh = NULL;   #ifdef HAVE_MMAP    size_t mmaped_size = 0;   #endif -  size_t size = 0; +  int size = 0;    char *filename = NULL;    -  get_all_args("Image.Font->load()", args, ".%s", &filename); +  get_all_args(NULL, args, ".%s", &filename);       if (!filename)    {    fh = (struct file_head *)image_default_font;    size = IMAGE_DEFAULT_FONT_SIZE;    } else {    int fd = -1;       do    {   #ifdef FONT_DEBUG    fprintf(stderr,"FONT open '%s'\n",sp[-args].u.string->str);   #endif    fd = fd_open(filename,fd_RDONLY,0);    if (errno == EINTR) check_threads_etc();    } while(fd < 0 && errno == EINTR);       if (fd >= 0)    { -  size = (size_t) file_size(fd); +  size = file_size(fd);    if (size > 0)    {    THREADS_ALLOW();   #ifdef HAVE_MMAP    fh = (struct file_head *)    mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);    if (fh != (struct file_head *)MAP_FAILED)    mmaped_size = size;    else    {   #endif -  fh = (struct file_head *)xalloc(size); +  fh = xalloc(size);   #ifdef FONT_DEBUG    fprintf(stderr,"FONT Malloced %p (%d)\n", fh, size);   #endif    if (fh && (!my_read(fd, fh, size)))    {    free(fh);    fh = NULL;    }   #ifdef HAVE_MMAP    }
pike.git/src/modules/Image/font.c:424:   #endif    new_font->chars = num_chars;    new_font->xspacing_scale = 1.0;    new_font->yspacing_scale = 1.0;    new_font->justification = J_LEFT;    new_font->height=ntohl(fh->height);    new_font->baseline=ntohl(fh->baseline);       for (i=0; i<num_chars; i++)    { -  if (i*sizeof(INT32)<size -  && ntohl(fh->o[i])<size +  if ((i*sizeof(INT32) + OFFSETOF(file_head, o))<(size_t)size +  && ntohl(fh->o[i])<(size_t)size    && ! ( ntohl(fh->o[i]) % 4) ) /* must be aligned */    {    ch = (struct char_head*)    (((char *)(fh)) + ntohl(fh->o[i]));    new_font->charinfo[i].width = ntohl(ch->width);    new_font->charinfo[i].spacing = ntohl(ch->spacing);    new_font->charinfo[i].pixels = ch->data;    }    else /* illegal <tm> offset or illegal align */    {
pike.git/src/modules/Image/font.c:525:    Pike_error("font->write: no font loaded\n");       if (args==0)    {    push_empty_string();    args=1;    }       maxwidth2=1;    -  width_of=(int *)xalloc((args+1)*sizeof(int)); +  width_of=xalloc((args+1)*sizeof(int));    SET_ONERROR(err, free, width_of);       for (j=0; j<args; j++)    {    int max;    if (TYPEOF(sp[j-args]) != T_STRING) -  bad_arg_error("font->write",sp-args,args,0,"",sp-args, -  "Bad arguments to font->write()\n"); +  SIMPLE_ARG_TYPE_ERROR("write",1,"string");       xsize = max = 1;    to_write_len = sp[j-args].u.string->len;    switch(sp[j-args].u.string->size_shift)    {    case 0:    to_write0 = STR0(sp[j-args].u.string);    for (i = 0; i < to_write_len; i++)    {    if (to_write0[i] < (INT32)this->chars)
pike.git/src/modules/Image/font.c:578:    {    if ((unsigned INT32)to_write2[i] < this->chars)    {    if (xsize+char_width(this,to_write2[i]) > max)    max=xsize+char_width(this,to_write2[i]);    xsize += char_space(this,to_write2[i]);    if (xsize > max) max=xsize;    }    }    break; - #ifdef PIKE_DEBUG -  default: -  Pike_fatal("Illegal shift size!\n"); - #endif +     }    width_of[j]=max;    if (max>maxwidth2) maxwidth2=max;    }       o = clone_object(image_program,0);    img = ((struct image*)o->storage);    img->xsize = maxwidth2;    if(args>1) -  img->ysize = DOUBLE_TO_INT(this->height+ +  img->ysize = (int)(this->height+    ((double)this->height*(double)(args-1)*    (double)this->yspacing_scale)+1);    else    img->ysize = this->height;    img->rgb.r=img->rgb.g=img->rgb.b=255;    img->img=malloc(img->xsize*img->ysize*sizeof(rgb_group)+RGB_VEC_PAD);       if (!img->img) {    free_object(o);    SIMPLE_OUT_OF_MEMORY_ERROR("write",    img->xsize*img->ysize*sizeof(rgb_group)+1);    }    -  MEMSET(img->img,0,img->xsize*img->ysize*sizeof(rgb_group)); +  memset(img->img,0,img->xsize*img->ysize*sizeof(rgb_group));       for (j=0; j<args; j++)    {    to_write_len = sp[j-args].u.string->len;    switch(this->justification)    {    case J_LEFT: xsize = 0; break;    case J_RIGHT: xsize = img->xsize-width_of[j]-1; break;    case J_CENTER: xsize = img->xsize/2-width_of[j]/2-1; break;    }
pike.git/src/modules/Image/font.c:637:    * when we have Image.FreeType, I bluntly disable it instead.    * /mast */    for (i = 0; i < to_write_len; i++)    {    c=*(to_write0++);    if (c < (INT32)this->chars)    {    if(char_width(this,c))    write_char(this->charinfo+c,    (img->img+xsize)+ -  (img->xsize*DOUBLE_TO_INT(j*this->height* +  (img->xsize*(int)(j*this->height*    this->yspacing_scale)),    img->xsize,    this->height);    xsize += char_space(this, c);    }    }    /* THREADS_DISALLOW(); */    break;    case 1:    to_write1 = STR1(sp[j-args].u.string);    /* THREADS_ALLOW(); */    for (i = 0; i < to_write_len; i++)    {    c=*(to_write1++);    if (c < (INT32)this->chars)    {    if(char_width(this,c))    write_char(this->charinfo+c,    (img->img+xsize)+ -  (img->xsize*DOUBLE_TO_INT(j*this->height* +  (img->xsize*(int)(j*this->height*    this->yspacing_scale)),    img->xsize,    this->height);    xsize += char_space(this, c);    }    }    /* THREADS_DISALLOW(); */    break;    case 2:    to_write2 = STR2(sp[j-args].u.string);    /* THREADS_ALLOW(); */    for (i = 0; i < to_write_len; i++)    {    c=*(to_write2++);    if (c < (INT32)this->chars)    {    if(char_width(this,c))    write_char(this->charinfo+c,    (img->img+xsize)+ -  (img->xsize*DOUBLE_TO_INT(j*this->height* +  (img->xsize*(int)(j*this->height*    this->yspacing_scale)),    img->xsize,    this->height);    xsize += char_space(this, c);    }    }    /* THREADS_DISALLOW(); */    break; - #ifdef PIKE_DEBUG -  default: -  Pike_fatal("Illegal shift size!\n"); - #endif +     }    }    CALL_AND_UNSET_ONERROR(err);       pop_n_elems(args);    push_object(o);   }      /*! @decl int height()    *! Returns the font height.
pike.git/src/modules/Image/font.c:754:    }       for (j=0; j<args; j++)    {    int max;    p_wchar0 *to_write0;    p_wchar1 *to_write1;    p_wchar2 *to_write2;    ptrdiff_t to_write_len;    if (TYPEOF(sp[j-args]) != T_STRING) -  bad_arg_error("font->text_extents",sp-args,args,0,"",sp-args, -  "Bad arguments to font->text_extents()\n"); +  SIMPLE_ARG_TYPE_ERROR("text_extents",1,"string");       xsize = max = 1;    to_write_len = sp[j-args].u.string->len;    switch(sp[j-args].u.string->size_shift)    {    case 0:    to_write0 = STR0(sp[j-args].u.string);    for (i = 0; i < to_write_len; i++)    {    if (xsize+char_width(THIS,to_write0[i]) > max)
pike.git/src/modules/Image/font.c:791:    case 2:    to_write2 = STR2(sp[j-args].u.string);    for (i = 0; i < to_write_len; i++)    {    if (xsize+char_width(THIS,to_write2[i]) > max)    max=xsize+char_width(THIS,to_write2[i]);    xsize += char_space(THIS,to_write2[i]);    if (xsize > max) max=xsize;    }    break; - #ifdef PIKE_DEBUG -  default: -  Pike_fatal("Illegal shift size!\n"); - #endif +     }    if (max>maxwidth2) maxwidth2=max;    }       pop_n_elems(args);    push_int(maxwidth2); -  push_int64(DO_NOT_WARN((INT64)(args * THIS->height * THIS->yspacing_scale))); +  push_int64((INT64)(args * THIS->height * THIS->yspacing_scale));    f_aggregate(2);   }         /*! @decl void set_xspacing_scale(float scale)    *! @decl void set_yspacing_scale(float scale)    *!    *! Set spacing scale to write characters closer    *! or more far away. This does not change scale    *! of character, only the space between them.    */      void font_set_xspacing_scale(INT32 args)   {    FLOAT_TYPE f;       if(!THIS) Pike_error("font->set_xspacing_scale(): No font loaded.\n"); -  get_all_args("set_xspaxing_scale", args, "%f", &f); +  get_all_args(NULL, args, "%f", &f);       if(f < 0.0) f=0.1;    THIS->xspacing_scale = (double)f; -  pop_n_elems(args); +    }         void font_set_yspacing_scale(INT32 args)   {    FLOAT_TYPE f;       if(!THIS) Pike_error("font->set_yspacing_scale(): No font loaded.\n"); -  get_all_args("set_yspacing_scale", args, "%f", &f); +  get_all_args(NULL, args, "%f", &f);       if(f <= 0.0) f=0.1;    THIS->yspacing_scale = (double)f; -  pop_n_elems(args); +    }         /*! @decl int baseline()    *!    *! Returns font baseline (pixels from top)    *!    *! @seealso    *! @[height], @[text_extents]    */
pike.git/src/modules/Image/font.c:863:    else    push_int(0);   }      /*! @decl void center()    *!    *! @fixme    *! Document this function.    */    - void font_set_center(INT32 args) + void font_set_center(INT32 UNUSED(args))   { -  pop_n_elems(args); +     if(THIS) THIS->justification=J_CENTER;   }      /*! @decl void right()    *!    *! @fixme    *! Document this function.    */    - void font_set_right(INT32 args) + void font_set_right(INT32 UNUSED(args))   { -  pop_n_elems(args); +     if(THIS) THIS->justification=J_RIGHT;   }      /*! @decl void left()    *!    *! @fixme    *! Document this function.    */    - void font_set_left(INT32 args) + void font_set_left(INT32 UNUSED(args))   { -  pop_n_elems(args); +     if(THIS) THIS->justification=J_LEFT;   }         /***************** global init etc *****************************/      /*      int load(string filename); // load font file, true is success   object write(string text); // new image object
pike.git/src/modules/Image/font.c:951:       /* function(void:void) */    ADD_FUNCTION("center", font_set_center,tFunc(tVoid,tVoid), 0);       /* function(void:void) */    ADD_FUNCTION("left", font_set_left,tFunc(tVoid,tVoid), 0);       /* function(void:void) */    ADD_FUNCTION("right", font_set_right,tFunc(tVoid,tVoid), 0);    + #ifdef PIKE_NULL_IS_SPECIAL    set_init_callback(init_font_struct); -  + #endif    set_exit_callback(exit_font_struct);   }      void exit_image_font(void)   {   }