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

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

E-mail: