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

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

E-mail: