Annotation of parser3/src/classes/image.C, revision 1.18

1.1       paf         1: /** @file
                      2:        Parser: @b image parser class.
                      3: 
1.16      paf         4:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.1       paf         5: 
1.16      paf         6:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1       paf         7: 
1.18    ! paf         8:        $Id: image.C,v 1.17.2.1 2001/04/16 11:26:35 paf Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_config_includes.h"
                     12: 
1.8       paf        13: #include "gif.h"
1.6       paf        14: 
1.1       paf        15: #include "pa_common.h"
                     16: #include "pa_request.h"
                     17: #include "pa_vfile.h"
                     18: #include "pa_vimage.h"
                     19: 
                     20: // global var
                     21: 
                     22: VStateless_class *image_class;
                     23: 
                     24: // helpers
                     25: 
                     26: class Measure_reader {
                     27: public:
1.16      paf        28:        enum { READ_CHUNK_SIZE=0x400 };// 1K
                     29:        typedef size_t(*Func)(void *& buf, size_t limit, void *info);
1.1       paf        30: 
                     31:        Measure_reader(Func afunc, void *ainfo) : 
                     32:                func(afunc), info(ainfo), 
                     33:                chunk(0), offset(0), size(0) {
                     34:        }
                     35: 
                     36:        size_t read(unsigned char *& buf, size_t limit) {
1.3       paf        37:                if(offset+limit>size) // nothing left
                     38:                        if(offset==0 || limit==1) { // only one-byte continuations allowed
                     39:                                size=(*func)(chunk, READ_CHUNK_SIZE, info);
                     40:                                offset=0;
                     41:                        } else
1.16      paf        42:                                return 0;// as if EOF
1.1       paf        43:                if(!size) // EOF
                     44:                        return 0;
                     45:                        
                     46:                // something left
                     47:                size_t read_size=min(offset+limit, size)-offset;
                     48:                buf=((unsigned char *)chunk)+offset;
                     49:                offset+=read_size;
                     50:                return read_size;
                     51:        }
                     52: 
                     53: private:
                     54:        Func func;
                     55:        void *info;
                     56: 
                     57:        void *chunk;
                     58:        size_t offset;
                     59:        size_t size;
                     60: };
                     61: 
                     62: // GIF
                     63: struct GIF_Header {
1.16      paf        64:        char       type[3];         // 'GIF'
1.1       paf        65:        char       version[3];
                     66:        unsigned char       width[2];
                     67:        unsigned char       height[2];
                     68:        char       dif;
                     69:        char       fonColor;
                     70:        char       nulls;
                     71: };
                     72: 
                     73: // JPEG
                     74: struct JFIF_Header {
1.16      paf        75:        char length[2];              // length of JFIF segment marker
                     76:        char identifier[5];          // JFIF identifier
                     77:        char version[2];             // version
                     78:        char units;                  // units X of Y pixel density
                     79:        char xdensity[2];            // X pixel density
                     80:        char ydensity[2];            // X pixel density
                     81:        char xthumbnails;            // width of thumbnails
                     82:        char ythumbnails;            // height of thumbnails
                     83:        char reserved;               // reserved
1.1       paf        84: };
                     85: struct JPG_Frame {
1.16      paf        86:        char length[2];               // length of image marker
                     87:        char data;                    // data precision of bits/sample
                     88:        char height[2];               // image height
                     89:        char width[2];                // image width
                     90:        char numComponents;           // number of color components
1.1       paf        91: };
                     92: 
                     93: //
                     94: 
                     95: inline short bytes_to_int(unsigned char HI, unsigned char LO) {
1.16      paf        96:        return(short)((HI<<8) + LO);
1.1       paf        97: }
                     98: 
                     99: void measure_gif(Pool& pool, const String *origin_string, 
                    100:                         Measure_reader& reader, int& width, int& height) {
                    101: 
                    102:        unsigned char *buf;
                    103:        const int head_size=sizeof(GIF_Header);
                    104:        if(reader.read(buf, head_size)<head_size)
                    105:                PTHROW(0, 0, 
                    106:                        origin_string, 
                    107:                        "broken GIF header - file size is less then %d bytes", head_size);
                    108: 
                    109:        GIF_Header& screenD=*reinterpret_cast<GIF_Header *>(buf);
                    110:        if(strncmp(screenD.type, "GIF", 3)!=0)
                    111:                PTHROW(0, 0, 
                    112:                        origin_string, 
                    113:                        "bad image file - GIF signature not found");    
                    114: 
                    115:        width=bytes_to_int(screenD.width[1], screenD.width[0]);
                    116:        height=bytes_to_int(screenD.height[1], screenD.height[0]);
                    117: }
                    118: 
                    119: void measure_jpeg(Pool& pool, const String *origin_string, 
                    120:                         Measure_reader& reader, int& width, int& height) {
1.2       paf       121:        // JFIF format markers
                    122:        const unsigned char SOI=0xD8;
                    123:        const unsigned char EOI=0xD9;
                    124:        const unsigned char APP0=0xE0;
                    125:        const unsigned char SOF0=0xC0;
                    126:        const unsigned char SOF2=0xC2;
                    127:        const unsigned char COM=0xFE;
                    128: 
1.1       paf       129:        unsigned char *screenD_buf;
1.3       paf       130:        unsigned char *h_buf=0;
1.1       paf       131:        
                    132:        bool flag=false;
                    133:        
                    134:        unsigned char *prefix;
1.18    ! paf       135:        const size_t prefix_size=2;
1.1       paf       136:        if(reader.read(prefix, prefix_size)<prefix_size)
                    137:                PTHROW(0, 0, 
                    138:                        origin_string, 
                    139:                        "broken JPEG file - size is less then %d bytes", prefix_size);
                    140:        
                    141:        if(((unsigned char *)prefix)[1]!=SOI) 
                    142:                PTHROW(0, 0, 
                    143:                        origin_string, 
                    144:                        "broken JPEG file - second byte of header is not 0x%02X", SOI);
                    145:                
                    146:        unsigned char zero=0;
                    147:        unsigned char *marker=&zero;
                    148: 
                    149:        do {
                    150:                while((*marker)!=0xFF)
                    151:                        if(reader.read(marker, sizeof(char))<=0) break;
                    152:                if(reader.read(marker, sizeof(char))<=0) break;
                    153:                switch(*marker) {
                    154:                case EOI:
                    155:                        marker=&zero;
                    156:                        break;
                    157:                case APP0:
                    158:                        if(!flag) {
                    159:                                flag=true;
                    160:                                if(reader.read(screenD_buf, sizeof(JFIF_Header)) < sizeof(JFIF_Header))
                    161:                                        break;
                    162:                                JFIF_Header& screenD=*reinterpret_cast<JFIF_Header *>(screenD_buf);
                    163:                                if((bytes_to_int(screenD.length[0], screenD.length[1]) < 16) ||
1.2       paf       164:                                        strcasecmp(screenD.identifier, "JFIF")) flag=false;
1.1       paf       165:                        }
                    166:                        break;
                    167:                case SOF0:
                    168:                case SOF2:
                    169:                        if(reader.read(h_buf, sizeof(JPG_Frame))<sizeof(JPG_Frame))
                    170:                                flag=false;
                    171:                        break;
                    172:                default: break;
                    173:                }
                    174:        } while(*marker!=EOI);
                    175:        
1.3       paf       176:        if(flag && h_buf) {
1.1       paf       177:                JPG_Frame& h=*reinterpret_cast<JPG_Frame *>(h_buf);
                    178:                width=bytes_to_int(h.width[0], h.width[1]);
                    179:                height=bytes_to_int(h.height[0], h.height[1]);
                    180:        } else
1.16      paf       181:                PTHROW(0, 0, 
                    182:                        origin_string, 
1.1       paf       183:                        "broken JPEG file - APP0 frame not found");                     
                    184: }
                    185: 
                    186: // measure center
                    187: 
                    188: void measure(Pool& pool, const String& file_name, 
                    189:                         Measure_reader& reader, int& width, int& height) {
                    190:        if(const char *cext=strrchr(file_name.cstr(), '.')) {
                    191:                cext++;
                    192:                if(strcasecmp(cext, "GIF")==0)
                    193:                        measure_gif(pool, &file_name, reader, width, height);
                    194:                else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) 
                    195:                        measure_jpeg(pool, &file_name, reader, width, height);
                    196:                else
                    197:                        PTHROW(0, 0, 
                    198:                                &file_name, 
                    199:                                "unhandled image file name extension '%s'", cext);
                    200:        } else
                    201:                PTHROW(0, 0, 
                    202:                        &file_name, 
                    203:                        "can not determine image type - no file name extension");
                    204: }
                    205: 
                    206: // read from somewhere
                    207: 
                    208: struct Read_mem_info {
                    209:        unsigned char *ptr;
                    210:        unsigned char *eof;
                    211: };
                    212: static size_t read_mem(void*& buf, size_t limit, void *info) {
                    213:        Read_mem_info& rmi=*static_cast<Read_mem_info *>(info);
                    214:        buf=rmi.ptr;
                    215:        size_t read_size=min(limit, (size_t)(rmi.eof-rmi.ptr));
                    216:        rmi.ptr+=read_size;
                    217:        return read_size;
                    218: }
                    219: 
                    220: struct Read_disk_info {
                    221:        const String *file_spec;
                    222:        size_t offset;
                    223: };
                    224: static size_t read_disk(void*& buf, size_t limit, void *info) {
                    225:        Read_disk_info& rdi=*static_cast<Read_disk_info *>(info);
                    226:        Pool& pool=rdi.file_spec->pool();
                    227: 
                    228:        size_t read_size;
                    229:        file_read(pool, *rdi.file_spec, 
                    230:                           buf, read_size, 
                    231:                           false/*as_text*/, 
                    232:                           true/*fail_on_read_problem*/, 
                    233:                           rdi.offset, limit);
                    234: 
                    235:        rdi.offset+=read_size;
                    236:        return read_size;
                    237: }
                    238: 
                    239: // methods
                    240: 
                    241: /// ^image:measure[DATA]
1.17      paf       242: static void _measure(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       243:        Pool& pool=r.pool();
                    244: 
1.17      paf       245:        Value& data=params->get_no_junction(0, "data must not be code");
1.1       paf       246: 
1.16      paf       247:        void *info;Measure_reader::Func read_func;
1.1       paf       248:        Read_mem_info read_mem_info;
                    249:        Read_disk_info read_disk_info;
                    250:        const String *file_name;
                    251:        if(data.is_string()) {
                    252:                file_name=data.get_string();
                    253:                read_disk_info.file_spec=&r.absolute(*file_name);
                    254:                read_disk_info.offset=0;
1.16      paf       255:                info=&read_disk_info;read_func=read_disk;
1.1       paf       256:        } else {
                    257:                const VFile& vfile=*data.as_vfile();
                    258:                file_name=&static_cast<Value *>(vfile.fields().get(*name_name))->as_string();
                    259:                read_mem_info.ptr=(unsigned char *)vfile.value_ptr();
                    260:                read_mem_info.eof=read_mem_info.ptr+vfile.value_size();
1.16      paf       261:                info=&read_mem_info;read_func=read_mem;
1.1       paf       262:        }
                    263: 
                    264:        Measure_reader reader(read_func, info);
                    265:        int width, height;
                    266:        measure(pool, *file_name, reader, width, height);
                    267: 
1.6       paf       268:        static_cast<VImage *>(r.self)->set(file_name, width, height);
1.1       paf       269: }
                    270: 
1.4       paf       271: struct Attrib_info {
                    272:        String *tag;
                    273:        Hash *skip;
                    274: };
1.3       paf       275: static void append_attrib_pair(const Hash::Key& key, Hash::Val *val, void *info) {
1.4       paf       276:        Attrib_info& ai=*static_cast<Attrib_info *>(info);
                    277: 
                    278:        if(ai.skip && ai.skip->get(key))
                    279:                return;
                    280: 
1.3       paf       281:        Value& value=*static_cast<Value *>(val);
                    282:        // src="a.gif" width=123 ismap[=-1]
1.4       paf       283:        *ai.tag << " " << key;
1.6       paf       284:        if(value.is_string() || value.as_double()>=0)
                    285:                *ai.tag << "=\"" << value.as_string() << "\"";
1.3       paf       286: }
                    287: /// ^image.html[]
                    288: /// ^image.html[hash]
1.17      paf       289: static void _html(Request& r, const String& method_name, MethodParams *params) {
1.3       paf       290:        Pool& pool=r.pool();
                    291: 
                    292:        String tag(pool);
                    293:        tag << "<img";
1.4       paf       294: 
1.18    ! paf       295:        const Hash& fields=static_cast<VImage *>(r.self)->fields();
1.5       paf       296:        Hash *attribs=0;
1.4       paf       297: 
1.11      paf       298:        if(params->size())
1.17      paf       299:                if(attribs=params->get(0).get_hash()) {
1.4       paf       300:                        Attrib_info attrib_info={&tag, 0};
1.5       paf       301:                        attribs->for_each(append_attrib_pair, &attrib_info);
1.4       paf       302:                } else
1.16      paf       303:                        PTHROW(0, 0, 
                    304:                                &method_name, 
1.5       paf       305:                                "attributes must be must be hash");
1.4       paf       306: 
1.5       paf       307:        Attrib_info attrib_info={&tag, attribs};
1.4       paf       308:        fields.for_each(append_attrib_pair, &attrib_info);
1.6       paf       309:        tag << " />";
1.3       paf       310:        r.write_pass_lang(tag);
                    311: }
1.8       paf       312: 
1.16      paf       313: static gdImage *load(Request& r, const String& method_name, 
                    314:                                         const String& file_name){
                    315:        Pool& pool=r.pool();
                    316: 
                    317:        const char *file_name_cstr=r.absolute(file_name).cstr(String::UL_FILE_NAME);
                    318:        if(FILE *f=fopen(file_name_cstr, "rb")) {
                    319:                gdImage& image=*new(pool) gdImage(pool);
                    320:                image.CreateFromGif(f);
                    321:                fclose(f);
                    322:                return &image;
                    323:        } else {
                    324:                PTHROW(0, 0, 
                    325:                        &method_name, 
                    326:                        "can not open '%s'", file_name_cstr);
                    327:                return 0;
                    328:        }
                    329: }
                    330: 
                    331: 
1.6       paf       332: /// ^image.load[background.gif]
1.17      paf       333: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       334:        Pool& pool=r.pool();
                    335: 
1.17      paf       336:        Value& vfile_name=params->get_no_junction(0, "file name must not be code");
1.6       paf       337:        const String& file_name=vfile_name.as_string();
                    338: 
1.16      paf       339:        gdImage& image=*load(r, method_name, file_name);
                    340:        int width=image.SX();
                    341:        int height=image.SY();
                    342:        static_cast<VImage *>(r.self)->set(&file_name, width, height, &image);
1.6       paf       343: }
                    344: 
                    345: /// ^image.create[width;height] bgcolor=white
                    346: /// ^image.create[width;height;bgcolor]
1.17      paf       347: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       348:        Pool& pool=r.pool();
                    349: 
1.17      paf       350:        int width=(int)r.process(params->get(0)).as_double();
                    351:        int height=(int)r.process(params->get(1)).as_double();
1.8       paf       352:        int bgcolor_value=0xffFFff;
1.6       paf       353:        if(params->size()>2)
1.17      paf       354:                bgcolor_value=(int)r.process(params->get(2)).as_double();
1.15      paf       355:        gdImage& image=*new(pool) gdImage(pool);
                    356:        image.Create(width, height);
                    357:        image.FilledRectangle(0, 0, width-1, height-1, image.Color(bgcolor_value));
                    358:        static_cast<VImage *>(r.self)->set(0, width, height, &image);
1.6       paf       359: }
                    360: 
                    361: /// ^image.gif[]
1.17      paf       362: static void _gif(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       363:        Pool& pool=r.pool();
                    364: 
1.9       paf       365:        gdImage *image=static_cast<VImage *>(r.self)->image;
1.8       paf       366:        if(!image)
1.16      paf       367:                PTHROW(0, 0, 
                    368:                        &method_name, 
1.12      paf       369:                        "does not contain an image");
1.6       paf       370: 
                    371:        // could _ but don't thing it's wise to use $image.src for vfile.name
1.10      paf       372: 
1.16      paf       373:        String out(pool); image->Gif(out);
1.6       paf       374:        
                    375:        VFile& vfile=*new(pool) VFile(pool);
                    376:        String& image_gif=*new(pool) String(pool, "image/gif");
1.14      paf       377:        vfile.set(false/*not tainted*/, out.cstr(), out.size(), 0, &image_gif);
1.6       paf       378: 
                    379:        r.write_no_lang(vfile);
                    380: }
                    381: 
1.12      paf       382: /// ^image.line(x0;y0;x1;y1;color)
1.17      paf       383: static void _line(Request& r, const String& method_name, MethodParams *params) {
1.12      paf       384:        Pool& pool=r.pool();
                    385: 
                    386:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    387:        if(!image)
1.16      paf       388:                PTHROW(0, 0, 
                    389:                        &method_name, 
1.12      paf       390:                        "does not contain an image");
                    391: 
                    392:        image->Line(
1.17      paf       393:                (int)r.process(params->get(0)).as_double(), 
                    394:                (int)r.process(params->get(1)).as_double(), 
                    395:                (int)r.process(params->get(2)).as_double(), 
                    396:                (int)r.process(params->get(3)).as_double(), 
                    397:                image->Color((int)r.process(params->get(4)).as_double()));
1.13      paf       398: }
                    399: 
                    400: /// ^image.fill(x;y;color)
1.17      paf       401: static void _fill(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       402:        Pool& pool=r.pool();
                    403: 
                    404:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    405:        if(!image)
1.16      paf       406:                PTHROW(0, 0, 
                    407:                        &method_name, 
1.13      paf       408:                        "does not contain an image");
1.12      paf       409: 
1.13      paf       410:        image->Fill(
1.17      paf       411:                (int)r.process(params->get(0)).as_double(), 
                    412:                (int)r.process(params->get(1)).as_double(), 
                    413:                image->Color((int)r.process(params->get(2)).as_double()));
1.13      paf       414: }
                    415: 
                    416: /// ^image.rectangle(x0;y0;x1;y1;color)
1.17      paf       417: static void _rectangle(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       418:        Pool& pool=r.pool();
                    419: 
                    420:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    421:        if(!image)
1.16      paf       422:                PTHROW(0, 0, 
                    423:                        &method_name, 
1.13      paf       424:                        "does not contain an image");
                    425: 
                    426:        image->Rectangle(
1.17      paf       427:                (int)r.process(params->get(0)).as_double(), 
                    428:                (int)r.process(params->get(1)).as_double(), 
                    429:                (int)r.process(params->get(2)).as_double(), 
                    430:                (int)r.process(params->get(3)).as_double(), 
                    431:                image->Color((int)r.process(params->get(4)).as_double()));
1.13      paf       432: }
                    433: 
                    434: /// ^image.bar(x0;y0;x1;y1;color)
1.17      paf       435: static void _bar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       436:        Pool& pool=r.pool();
                    437: 
                    438:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    439:        if(!image)
1.16      paf       440:                PTHROW(0, 0, 
                    441:                        &method_name, 
1.13      paf       442:                        "does not contain an image");
                    443: 
                    444:        image->FilledRectangle(
1.17      paf       445:                (int)r.process(params->get(0)).as_double(), 
                    446:                (int)r.process(params->get(1)).as_double(), 
                    447:                (int)r.process(params->get(2)).as_double(), 
                    448:                (int)r.process(params->get(3)).as_double(), 
                    449:                image->Color((int)r.process(params->get(4)).as_double()));
1.13      paf       450: }
                    451: 
                    452: /// ^image.replace(color-source;color-dest)(x;y)... point coord pairs
1.17      paf       453: static void _replace(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       454:        Pool& pool=r.pool();
                    455: 
                    456:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    457:        if(!image)
1.16      paf       458:                PTHROW(0, 0, 
                    459:                        &method_name, 
1.13      paf       460:                        "does not contain an image");
                    461: 
                    462:        if((params->size()-2)%2) // I see your thoughts, but that's more readable
1.16      paf       463:                PTHROW(0, 0, 
                    464:                        &method_name, 
1.13      paf       465:                        "y coordinate missing");
                    466: 
                    467:        int n=(params->size()-2)/2;
                    468:        
                    469:        gdImage::Point *p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*n);
                    470:        for(int i=0; i<n; i++) {
1.17      paf       471:                p[i].x=(int)r.process(params->get(2+i*2+0)).as_double();
                    472:                p[i].y=(int)r.process(params->get(2+i*2+1)).as_double();
1.13      paf       473:        }
1.16      paf       474:        image->FilledPolygonReplaceColor(p, n, 
1.17      paf       475:                image->Color((int)r.process(params->get(0)).as_double()), // src color
                    476:                image->Color((int)r.process(params->get(1)).as_double()));// dest color
1.13      paf       477: }
                    478: 
                    479: /// ^image.polygon(color)(x;y)... point coord pairs
1.17      paf       480: static void _polygon(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       481:        Pool& pool=r.pool();
                    482: 
                    483:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    484:        if(!image)
1.16      paf       485:                PTHROW(0, 0, 
                    486:                        &method_name, 
1.13      paf       487:                        "does not contain an image");
                    488: 
                    489:        if((params->size()-1)%2) // [I see..] see now?
1.16      paf       490:                PTHROW(0, 0, 
                    491:                        &method_name, 
1.13      paf       492:                        "y coordinate missing");
                    493: 
                    494:        int n=(params->size()-1)/2;
                    495:        
                    496:        gdImage::Point *p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*n);
                    497:        for(int i=0; i<n; i++) {
1.17      paf       498:                p[i].x=(int)r.process(params->get(1+i*2+0)).as_double();
                    499:                p[i].y=(int)r.process(params->get(1+i*2+1)).as_double();
1.13      paf       500:        }
1.16      paf       501:        image->Polygon(p, n, 
1.17      paf       502:                image->Color((int)r.process(params->get(0)).as_double()));
1.13      paf       503: }
                    504: 
                    505: /// ^image.polybar(color)(x;y)... point coord pairs
1.17      paf       506: static void _polybar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       507:        Pool& pool=r.pool();
                    508: 
                    509:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    510:        if(!image)
1.16      paf       511:                PTHROW(0, 0, 
                    512:                        &method_name, 
1.13      paf       513:                        "does not contain an image");
                    514: 
                    515:        if((params->size()-1)%2) // [I see..] see now?
1.16      paf       516:                PTHROW(0, 0, 
                    517:                        &method_name, 
1.13      paf       518:                        "y coordinate missing");
                    519: 
                    520:        int n=(params->size()-1)/2;
                    521:        
                    522:        gdImage::Point *p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*n);
                    523:        for(int i=0; i<n; i++) {
1.17      paf       524:                p[i].x=(int)r.process(params->get(1+i*2+0)).as_double();
                    525:                p[i].y=(int)r.process(params->get(1+i*2+1)).as_double();
1.13      paf       526:        }
1.16      paf       527:        image->FilledPolygon(p, n, 
1.17      paf       528:                image->Color((int)r.process(params->get(0)).as_double()));
1.12      paf       529: }
                    530: 
1.16      paf       531: // font
                    532: 
                    533: #define Y(y)(y+index*height+1)
                    534: class Font : public Pooled {
                    535: public:
                    536:        
                    537:        int height;         /* Font heigth */
                    538:        int space;          /* Default char width */
                    539:        gdImage& ifont;
                    540:        const String& alphabet;
                    541:        
                    542:        Font(Pool& pool, 
                    543:                const String& aalphabet, 
                    544:                gdImage& aifont, int aheight, int aspace) : Pooled(pool), 
                    545:                alphabet(aalphabet), 
                    546:                height(aheight), space(aspace), 
                    547:                ifont(aifont) {
                    548:        }
                    549:        
                    550:        /* ******************************** char ********************************** */
                    551:        
                    552:        int index_of(char ch) {
                    553:                if(ch==' ') return -1;
                    554:                return alphabet.pos(&ch, 1);
                    555:        }
                    556:        
                    557:        int index_width(int index) {
                    558:                if(index<0)
                    559:                        index=index_of('.');
                    560:                if(index<0) 
                    561:                        return 0;
                    562:                int tr=ifont.GetTransparent();
                    563:                for(int x=ifont.SX()-1; x>0; x--) {
                    564:                        for(int y=0; y<height-1; y++)
                    565:                                if(ifont.GetPixel(x, Y(y))!=tr) 
                    566:                                        return x+2;
                    567:                }
                    568:                return 0;
                    569:        }
                    570:        
                    571:        void index_display(gdImage& image, int x, int y, int index){
                    572:                if(index>=0) 
                    573:                        ifont.Copy(image, x, y, 0, Y(0), index_width(index), height-1);
                    574:        }
                    575:        
                    576:        /* ******************************** string ********************************** */
                    577:        /*
                    578:        int string_width(const char *cstr){
                    579:                int result=0;
                    580:                for(; *cstr; cstr++)
                    581:                        result+=index_width(index_of(*cstr));
                    582:                return result;
                    583:        }
                    584:        */
                    585:        
                    586:        void string_display(gdImage& image, int x, int y, const String& s){
                    587:                const char *cstr=s.cstr(String::UL_AS_IS);
                    588:                if(cstr) for(; *cstr; cstr++) {
                    589:                        int index=index_of(*cstr);
                    590:                        index_display(image, x, y, index);
                    591:                        x+=space ? space : index_width(index);
                    592:                }
                    593:        }
                    594:        
                    595: };
                    596: /// ^image.font[alPHAbet;font-file-name.gif](height)
                    597: /// ^image.font[alPHAbet;font-file-name.gif](height;width)
1.17      paf       598: static void _font(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       599:        Pool& pool=r.pool();
                    600: 
1.17      paf       601:        Value& valphabet=params->get_no_junction(0, "alphabet must not be code");
                    602:        Value& file_name=params->get_no_junction(1, "file_name must not be code");
                    603:        int height=(int)r.process(params->get(2)).as_double();
1.16      paf       604: 
1.17      paf       605:        int width=params->size()>3?(int)r.process(params->get(3)).as_double():0;
1.16      paf       606: 
                    607:        static_cast<VImage *>(r.self)->font=new(pool) Font(pool, 
                    608:                valphabet.as_string(), 
                    609:                *load(r, method_name, file_name.as_string()), 
                    610:                height, width);
                    611: }
                    612: 
                    613: /// ^image.text(x;y)[text]
1.17      paf       614: static void _text(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       615:        Pool& pool=r.pool();
                    616: 
1.17      paf       617:        int x=(int)r.process(params->get(0)).as_double();
                    618:        int y=(int)r.process(params->get(1)).as_double();
                    619:        const String& s=r.process(params->get(2)).as_string();
1.16      paf       620: 
                    621:        VImage& vimage=*static_cast<VImage *>(r.self);
                    622:        if(vimage.image)
                    623:                if(vimage.font)
                    624:                        vimage.font->string_display(*vimage.image, x, y, s);
                    625:                else
                    626:                        PTHROW(0, 0,
                    627:                                &method_name,
                    628:                                "set the font first");
                    629:        else
                    630:                PTHROW(0, 0, 
                    631:                        &method_name, 
                    632:                        "does not contain an image");
                    633: }
                    634: 
1.1       paf       635: // initialize
                    636: void initialize_image_class(Pool& pool, VStateless_class& vclass) {
                    637:        // ^image:measure[DATA]
                    638:        vclass.add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3       paf       639: 
                    640:        /// ^image.html[]
                    641:        /// ^image.html[hash]
                    642:        vclass.add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6       paf       643: 
                    644:        /// ^image.load[background.gif]
1.8       paf       645:        vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6       paf       646: 
                    647:        /// ^image.create[width;height] bgcolor=white
                    648:        /// ^image.create[width;height;bgcolor]
                    649:        vclass.add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
                    650: 
                    651:        /// ^image.gif[]
1.14      paf       652:        vclass.add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 0);
1.12      paf       653: 
                    654:        /// ^image.line(x0;y0;x1;y1;color)
                    655:        vclass.add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13      paf       656: 
                    657:        /// ^image.fill(x;y;color)
                    658:        vclass.add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
                    659: 
                    660:        /// ^image.rectangle(x0;y0;x1;y1;color)
                    661:        vclass.add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
                    662: 
                    663:        /// ^image.bar(x0;y0;x1;y1;color)
                    664:        vclass.add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
                    665: 
                    666:        /// ^image.replace(color-source;color-dest)(x;y)... point coord pairs
                    667:        vclass.add_native_method("replace", Method::CT_DYNAMIC, _replace, 2+3*2, 2+100*2);
                    668: 
                    669:        /// ^image.polygon(color)(x;y)... point coord pairs
                    670:        vclass.add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 1+3*2, 1+100*2);
                    671: 
                    672:        /// ^image.polybar(color)(x;y)... point coord pairs
                    673:        vclass.add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 1+3*2, 1+100*2);
                    674: 
1.16      paf       675:     /// ^image.font[alPHAbet;font-file-name.gif](height)
                    676:     /// ^image.font[alPHAbet;font-file-name.gif](height;width)
                    677:        vclass.add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
                    678: 
                    679:     /// ^image.text(x;y)[text]
                    680:        vclass.add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
                    681:        
1.1       paf       682: }

E-mail: