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

version» Context lines:

pike.git/src/modules/Image/font.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id$ +    */      #include "global.h"      #define SPACE_CHAR 'i'      extern const unsigned char image_default_font[]; - #define IMAGE_DEFAULT_FONT_SIZE 30596 + #define IMAGE_DEFAULT_FONT_SIZE 30560         /*! @module Image    */      /*! @class Font    *!    *! Short technical documentation on a font file:    *! This object adds the text-drawing and -creation    *! capabilities of the @[Image] module.
pike.git/src/modules/Image/font.c:88:    *! 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:120:      #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:188:    } else   #else    free(font->mem);   #endif    font->mem = NULL;    }    free(font);    }   }    - static void init_font_struct(struct object *o) + static void init_font_struct(struct object *UNUSED(o))   {    THIS=NULL;   }    - static void exit_font_struct(struct object *obj) + 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)   {    if(c==0x20)
pike.git/src/modules/Image/font.c:302:    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("load", 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:425:   #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:489:   }      /*! @decl void create(string filename)    *! Loads a font file to this font object.    *! Similar to @[load()].    */      void font_create(INT32 args)   {    font_load(args); -  pop_stack(); +    }      /*! @decl Image.Image write(string text, string ... more_text_lines)    *! Writes some text; thus creating an image object    *! that can be used as mask or as a complete picture.    *! One or more text lines may be provided.    *!    *! @seealso    *! @[text_extents], @[load], @[Image.Image->paste_mask], @[Image.Image->paste_alpha_color]   */
pike.git/src/modules/Image/font.c:526:    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_BAD_ARG_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:570:    max=xsize+char_width(this,to_write1[i]);    xsize += char_space(this,to_write1[i]);    if (xsize > max) max=xsize;    }    }    break;    case 2:    to_write2 = STR2(sp[j-args].u.string);    for (i = 0; i < to_write_len; i++)    { -  if (to_write2[i] < (unsigned INT32)this->chars) +  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+
pike.git/src/modules/Image/font.c:606:    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:687:    (img->img+xsize)+    (img->xsize*DOUBLE_TO_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:755:    }       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_BAD_ARG_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:792:    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.
pike.git/src/modules/Image/font.c:824:      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);       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);       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:864:    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