Annotation of parser3/src/classes/image.C, revision 1.66
1.1 paf 1: /** @file
2: Parser: @b image parser class.
3:
1.65 paf 4: Copyright(c) 2001, 2002 ArtLebedev Group(http://www.artlebedev.com)
1.66 ! paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6:
1.66 ! paf 7: $Id: image.C,v 1.65 2002/02/08 07:27:40 paf Exp $
1.31 parser 8: */
9:
10: /*
11: jpegsize: gets the width and height (in pixels) of a jpeg file
12: Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995
13: modified slightly by alex@ed.ac.uk
14: and further still by rjray@uswest.com
15: optimization and general re-write from tmetro@vl.com
16: from perl by paf@design.ru
1.1 paf 17: */
18:
19: #include "pa_config_includes.h"
20:
1.8 paf 21: #include "gif.h"
1.6 paf 22:
1.1 paf 23: #include "pa_common.h"
24: #include "pa_request.h"
25: #include "pa_vfile.h"
26: #include "pa_vimage.h"
27:
1.22 paf 28: // defines
1.1 paf 29:
1.22 paf 30: #define IMAGE_CLASS_NAME "image"
31:
32: // class
33:
34: class MImage : public Methoded {
35: public: // VStateless_class
36: Value *create_new_value(Pool& pool) { return new(pool) VImage(pool); }
37:
38: public:
39: MImage(Pool& pool);
1.24 paf 40:
41: public: // Methoded
1.22 paf 42: bool used_directly() { return true; }
43:
44: };
1.1 paf 45:
46: // helpers
47:
1.43 parser 48: #ifndef DOXYGEN
1.1 paf 49: class Measure_reader {
50: public:
1.45 parser 51: enum { READ_CHUNK_SIZE=0x400*20 };// 20K
1.16 paf 52: typedef size_t(*Func)(void *& buf, size_t limit, void *info);
1.1 paf 53:
54: Measure_reader(Func afunc, void *ainfo) :
55: func(afunc), info(ainfo),
56: chunk(0), offset(0), size(0) {
57: }
58:
1.31 parser 59: size_t read(void *&buf, size_t limit) {
1.3 paf 60: if(offset+limit>size) // nothing left
61: if(offset==0 || limit==1) { // only one-byte continuations allowed
62: size=(*func)(chunk, READ_CHUNK_SIZE, info);
63: offset=0;
64: } else
1.16 paf 65: return 0;// as if EOF
1.1 paf 66: if(!size) // EOF
67: return 0;
68:
69: // something left
70: size_t read_size=min(offset+limit, size)-offset;
71: buf=((unsigned char *)chunk)+offset;
72: offset+=read_size;
73: return read_size;
74: }
75:
76: private:
77: Func func;
78: void *info;
79:
80: void *chunk;
81: size_t offset;
82: size_t size;
83: };
1.43 parser 84: #endif
1.1 paf 85:
1.21 paf 86: /// GIF file header
1.1 paf 87: struct GIF_Header {
1.16 paf 88: char type[3]; // 'GIF'
1.1 paf 89: char version[3];
90: unsigned char width[2];
91: unsigned char height[2];
92: char dif;
93: char fonColor;
94: char nulls;
95: };
96:
1.31 parser 97: /// JPEG record head
98: struct JPG_Segment_head {
99: unsigned char marker;
100: unsigned char code;
101: unsigned char length[2];
1.1 paf 102: };
1.21 paf 103: /// JPEG frame header
1.31 parser 104: struct JPG_Size_segment_body {
1.27 parser 105: char data; //< data precision of bits/sample
1.31 parser 106: unsigned char height[2]; //< image height
107: unsigned char width[2]; //< image width
1.27 parser 108: char numComponents; //< number of color components
1.1 paf 109: };
110:
111: //
112:
1.33 parser 113: inline short x_endian_to_int(unsigned char L, unsigned char H) {
114: return(short)((H<<8) + L);
115: }
116:
117: inline short big_endian_to_int(unsigned char b[2]) {
118: return x_endian_to_int(b[1], b[0]);
119: }
120:
121: inline short little_endian_to_int(unsigned char b[2]) {
122: return x_endian_to_int(b[0], b[1]);
1.1 paf 123: }
124:
125: void measure_gif(Pool& pool, const String *origin_string,
126: Measure_reader& reader, int& width, int& height) {
127:
1.31 parser 128: void *buf;
1.1 paf 129: const int head_size=sizeof(GIF_Header);
130: if(reader.read(buf, head_size)<head_size)
1.57 parser 131: throw Exception(0, 0,
1.1 paf 132: origin_string,
1.34 parser 133: "not GIF file - too small");
1.31 parser 134: GIF_Header *head=(GIF_Header *)buf;
1.1 paf 135:
1.31 parser 136: if(strncmp(head->type, "GIF", 3)!=0)
1.57 parser 137: throw Exception(0, 0,
1.1 paf 138: origin_string,
1.44 parser 139: "not GIF file - wrong signature");
1.1 paf 140:
1.33 parser 141: width=little_endian_to_int(head->width);
142: height=little_endian_to_int(head->height);
1.1 paf 143: }
144:
1.58 parser 145: /// @test remove ugly mech in reader - 20K limit
1.1 paf 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.57 parser 156: throw Exception(0, 0,
1.1 paf 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.57 parser 162: throw Exception(0, 0,
1.1 paf 163: origin_string,
1.44 parser 164: "not JPEG file - wrong signature");
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.57 parser 197: throw Exception(0, 0,
1.16 paf 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) {
1.60 paf 206: if(const char *cext=strrchr(file_name.cstr(String::UL_FILE_SPEC), '.')) {
1.1 paf 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
1.57 parser 213: throw Exception(0, 0,
1.1 paf 214: &file_name,
215: "unhandled image file name extension '%s'", cext);
216: } else
1.57 parser 217: throw Exception(0, 0,
1.1 paf 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:
1.49 parser 297: // skip user-specified and internal(starting with "line-") attributes
298: if(ai.skip && ai.skip->get(key) || key.pos("line-")==0)
1.4 paf 299: return;
300:
1.3 paf 301: Value& value=*static_cast<Value *>(val);
302: // src="a.gif" width=123 ismap[=-1]
1.4 paf 303: *ai.tag << " " << key;
1.26 parser 304: if(value.is_string() || value.as_int()>=0)
1.6 paf 305: *ai.tag << "=\"" << value.as_string() << "\"";
1.3 paf 306: }
1.17 paf 307: static void _html(Request& r, const String& method_name, MethodParams *params) {
1.3 paf 308: Pool& pool=r.pool();
309:
310: String tag(pool);
311: tag << "<img";
1.4 paf 312:
1.18 paf 313: const Hash& fields=static_cast<VImage *>(r.self)->fields();
1.5 paf 314: Hash *attribs=0;
1.4 paf 315:
1.39 parser 316: if(params->size()) {
1.52 parser 317: Value &vattribs=r.process(params->get(0),
318: 0/*no name*/,
319: false/*don't intercept string*/);
1.39 parser 320: if(vattribs.is_defined()) // allow 'void'
1.59 parser 321: if(attribs=vattribs.get_hash(&method_name)) {
1.39 parser 322: Attrib_info attrib_info={&tag, 0};
323: attribs->for_each(append_attrib_pair, &attrib_info);
324: } else
1.57 parser 325: throw Exception(0, 0,
1.39 parser 326: &method_name,
327: "attributes must be hash");
328: }
1.4 paf 329:
1.5 paf 330: Attrib_info attrib_info={&tag, attribs};
1.4 paf 331: fields.for_each(append_attrib_pair, &attrib_info);
1.6 paf 332: tag << " />";
1.3 paf 333: r.write_pass_lang(tag);
334: }
1.8 paf 335:
1.16 paf 336: static gdImage *load(Request& r, const String& method_name,
337: const String& file_name){
338: Pool& pool=r.pool();
339:
1.42 parser 340: const char *file_name_cstr=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
1.16 paf 341: if(FILE *f=fopen(file_name_cstr, "rb")) {
342: gdImage& image=*new(pool) gdImage(pool);
1.23 paf 343: bool ok=image.CreateFromGif(f);
1.16 paf 344: fclose(f);
1.23 paf 345: if(!ok)
1.57 parser 346: throw Exception(0, 0,
1.23 paf 347: &file_name,
348: "is not in GIF format");
1.16 paf 349: return ℑ
350: } else {
1.57 parser 351: throw Exception(0, 0,
1.16 paf 352: &method_name,
353: "can not open '%s'", file_name_cstr);
354: return 0;
355: }
356: }
357:
358:
1.17 paf 359: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.6 paf 360: Pool& pool=r.pool();
361:
1.44 parser 362: const String& file_name=params->as_string(0, "file name must not be code");
1.6 paf 363:
1.16 paf 364: gdImage& image=*load(r, method_name, file_name);
365: int width=image.SX();
366: int height=image.SY();
367: static_cast<VImage *>(r.self)->set(&file_name, width, height, &image);
1.6 paf 368: }
369:
1.17 paf 370: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.6 paf 371: Pool& pool=r.pool();
372:
1.51 parser 373: int width=params->as_int(0, "width must be int", r);
374: int height=params->as_int(1, "height must be int", r);
1.8 paf 375: int bgcolor_value=0xffFFff;
1.6 paf 376: if(params->size()>2)
1.51 parser 377: bgcolor_value=params->as_int(2, "color must be int", r);
1.15 paf 378: gdImage& image=*new(pool) gdImage(pool);
379: image.Create(width, height);
380: image.FilledRectangle(0, 0, width-1, height-1, image.Color(bgcolor_value));
381: static_cast<VImage *>(r.self)->set(0, width, height, &image);
1.6 paf 382: }
383:
1.17 paf 384: static void _gif(Request& r, const String& method_name, MethodParams *params) {
1.6 paf 385: Pool& pool=r.pool();
386:
1.9 paf 387: gdImage *image=static_cast<VImage *>(r.self)->image;
1.8 paf 388: if(!image)
1.57 parser 389: throw Exception(0, 0,
1.16 paf 390: &method_name,
1.12 paf 391: "does not contain an image");
1.6 paf 392:
393: // could _ but don't thing it's wise to use $image.src for vfile.name
1.10 paf 394:
1.16 paf 395: String out(pool); image->Gif(out);
1.6 paf 396:
397: VFile& vfile=*new(pool) VFile(pool);
1.41 parser 398: Value *content_type=new(pool) VString(*new(pool) String(pool, "image/gif"));
1.20 paf 399: vfile.set(false/*not tainted*/,
1.61 paf 400: out.cstr(), out.size(), 0, content_type);
1.6 paf 401:
402: r.write_no_lang(vfile);
403: }
404:
1.17 paf 405: static void _line(Request& r, const String& method_name, MethodParams *params) {
1.12 paf 406: Pool& pool=r.pool();
407:
408: gdImage *image=static_cast<VImage *>(r.self)->image;
409: if(!image)
1.57 parser 410: throw Exception(0, 0,
1.16 paf 411: &method_name,
1.12 paf 412: "does not contain an image");
413:
414: image->Line(
1.51 parser 415: params->as_int(0, "x0 must be int", r),
416: params->as_int(1, "y0 must be int", r),
417: params->as_int(2, "x1 must be int", r),
418: params->as_int(3, "y1 must be int", r),
419: image->Color(params->as_int(4, "color must be int", r)));
1.13 paf 420: }
421:
1.17 paf 422: static void _fill(Request& r, const String& method_name, MethodParams *params) {
1.13 paf 423: Pool& pool=r.pool();
424:
425: gdImage *image=static_cast<VImage *>(r.self)->image;
426: if(!image)
1.57 parser 427: throw Exception(0, 0,
1.16 paf 428: &method_name,
1.13 paf 429: "does not contain an image");
1.12 paf 430:
1.13 paf 431: image->Fill(
1.51 parser 432: params->as_int(0, "x must be int", r),
433: params->as_int(1, "y must be int", r),
434: image->Color(params->as_int(2, "color must be int", r)));
1.13 paf 435: }
436:
1.17 paf 437: static void _rectangle(Request& r, const String& method_name, MethodParams *params) {
1.13 paf 438: Pool& pool=r.pool();
439:
440: gdImage *image=static_cast<VImage *>(r.self)->image;
441: if(!image)
1.57 parser 442: throw Exception(0, 0,
1.16 paf 443: &method_name,
1.13 paf 444: "does not contain an image");
445:
446: image->Rectangle(
1.51 parser 447: params->as_int(0, "x0 must be int", r),
448: params->as_int(1, "y0 must be int", r),
449: params->as_int(2, "x1 must be int", r),
450: params->as_int(3, "y1 must be int", r),
451: image->Color(params->as_int(4, "color must be int", r)));
1.13 paf 452: }
453:
1.17 paf 454: static void _bar(Request& r, const String& method_name, MethodParams *params) {
1.13 paf 455: Pool& pool=r.pool();
456:
457: gdImage *image=static_cast<VImage *>(r.self)->image;
458: if(!image)
1.57 parser 459: throw Exception(0, 0,
1.16 paf 460: &method_name,
1.13 paf 461: "does not contain an image");
462:
463: image->FilledRectangle(
1.51 parser 464: params->as_int(0, "x0 must be int", r),
465: params->as_int(1, "y0 must be int", r),
466: params->as_int(2, "x1 must be int", r),
467: params->as_int(3, "y1 must be int", r),
468: image->Color(params->as_int(4, "color must be int", r)));
1.13 paf 469: }
470:
1.44 parser 471: #ifndef DOXYGEN
472: static void add_point(Array::Item *value, void *info) {
473: Array& row=*static_cast<Array *>(value);
474: gdImage::Point **p=static_cast<gdImage::Point **>(info);
475:
476: (**p).x=row.get_string(0)->as_int();
477: (**p).y=row.get_string(1)->as_int();
478: (*p)++;
479: }
480: #endif
1.17 paf 481: static void _replace(Request& r, const String& method_name, MethodParams *params) {
1.13 paf 482: Pool& pool=r.pool();
483:
484: gdImage *image=static_cast<VImage *>(r.self)->image;
485: if(!image)
1.57 parser 486: throw Exception(0, 0,
1.16 paf 487: &method_name,
1.13 paf 488: "does not contain an image");
489:
1.44 parser 490: Table *table=params->as_no_junction(2, "coordinates must not be code").get_table();
491: if(!table)
1.57 parser 492: throw Exception(0, 0,
1.44 parser 493: &method_name,
494: "coordinates must be table");
1.13 paf 495:
1.44 parser 496: gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
497: gdImage::Point *add_p=all_p;
498: table->for_each(add_point, &add_p);
499: image->FilledPolygonReplaceColor(all_p, table->size(),
1.51 parser 500: image->Color(params->as_int(0, "src color must be int", r)),
501: image->Color(params->as_int(1, "dest color must be int", r)));
1.13 paf 502: }
503:
1.44 parser 504: static void _polyline(Request& r, const String& method_name, MethodParams *params) {
1.13 paf 505: Pool& pool=r.pool();
506:
507: gdImage *image=static_cast<VImage *>(r.self)->image;
508: if(!image)
1.57 parser 509: throw Exception(0, 0,
1.16 paf 510: &method_name,
1.13 paf 511: "does not contain an image");
512:
1.44 parser 513: Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
514: if(!table)
1.57 parser 515: throw Exception(0, 0,
1.44 parser 516: &method_name,
517: "coordinates must be table");
518:
519: gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
520: gdImage::Point *add_p=all_p;
521: table->for_each(add_point, &add_p);
522: image->Polygon(all_p, table->size(),
1.51 parser 523: image->Color(params->as_int(0, "color must be int", r)),
1.44 parser 524: false/*not closed*/);
525: }
526:
527: static void _polygon(Request& r, const String& method_name, MethodParams *params) {
528: Pool& pool=r.pool();
529:
530: gdImage *image=static_cast<VImage *>(r.self)->image;
531: if(!image)
1.57 parser 532: throw Exception(0, 0,
1.16 paf 533: &method_name,
1.44 parser 534: "does not contain an image");
1.13 paf 535:
1.44 parser 536: Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
537: if(!table)
1.57 parser 538: throw Exception(0, 0,
1.44 parser 539: &method_name,
540: "coordinates must be table");
541:
542: gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
543: gdImage::Point *add_p=all_p;
544: table->for_each(add_point, &add_p);
545: image->Polygon(all_p, table->size(),
1.51 parser 546: image->Color(params->as_int(0, "color must be int", r)));
1.13 paf 547: }
548:
1.17 paf 549: static void _polybar(Request& r, const String& method_name, MethodParams *params) {
1.13 paf 550: Pool& pool=r.pool();
551:
552: gdImage *image=static_cast<VImage *>(r.self)->image;
553: if(!image)
1.57 parser 554: throw Exception(0, 0,
1.16 paf 555: &method_name,
1.13 paf 556: "does not contain an image");
557:
1.44 parser 558: Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
559: if(!table)
1.57 parser 560: throw Exception(0, 0,
1.44 parser 561: &method_name,
562: "coordinates must be table");
1.13 paf 563:
1.44 parser 564: gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
565: gdImage::Point *add_p=all_p;
566: table->for_each(add_point, &add_p);
567: image->FilledPolygon(all_p, table->size(),
1.51 parser 568: image->Color(params->as_int(0, "color must be int", r)));
1.12 paf 569: }
570:
1.16 paf 571: // font
572:
573: #define Y(y)(y+index*height+1)
1.21 paf 574:
575: /// simple gdImage-based font storage & text output
1.16 paf 576: class Font : public Pooled {
577: public:
578:
1.38 parser 579: const static int letter_spacing;
1.35 parser 580: int height; ///< Font heigth
581: int monospace; ///< Default char width
582: int spacebarspace; ///< spacebar width
1.16 paf 583: gdImage& ifont;
584: const String& alphabet;
585:
586: Font(Pool& pool,
587: const String& aalphabet,
1.35 parser 588: gdImage& aifont, int aheight, int amonospace, int aspacebarspace) : Pooled(pool),
1.16 paf 589: alphabet(aalphabet),
1.35 parser 590: height(aheight), monospace(amonospace), spacebarspace(aspacebarspace),
1.16 paf 591: ifont(aifont) {
592: }
593:
594: /* ******************************** char ********************************** */
595:
596: int index_of(char ch) {
597: if(ch==' ') return -1;
598: return alphabet.pos(&ch, 1);
599: }
600:
601: int index_width(int index) {
602: if(index<0)
1.35 parser 603: return spacebarspace;
1.16 paf 604: int tr=ifont.GetTransparent();
1.35 parser 605: for(int x=ifont.SX()-1; x>=0; x--) {
1.16 paf 606: for(int y=0; y<height-1; y++)
607: if(ifont.GetPixel(x, Y(y))!=tr)
1.35 parser 608: return x+1;
1.16 paf 609: }
610: return 0;
611: }
612:
613: void index_display(gdImage& image, int x, int y, int index){
614: if(index>=0)
615: ifont.Copy(image, x, y, 0, Y(0), index_width(index), height-1);
616: }
617:
618: /* ******************************** string ********************************** */
1.47 parser 619:
620: int string_width(const String& s){
1.61 paf 621: const char *cstr=s.cstr();
1.16 paf 622: int result=0;
623: for(; *cstr; cstr++)
624: result+=index_width(index_of(*cstr));
625: return result;
626: }
627:
628: void string_display(gdImage& image, int x, int y, const String& s){
1.61 paf 629: const char *cstr=s.cstr();
1.16 paf 630: if(cstr) for(; *cstr; cstr++) {
631: int index=index_of(*cstr);
632: index_display(image, x, y, index);
1.38 parser 633: x+=letter_spacing + (monospace ? monospace : index_width(index));
1.16 paf 634: }
635: }
636:
637: };
1.38 parser 638: const int Font::letter_spacing=1;
1.35 parser 639:
1.17 paf 640: static void _font(Request& r, const String& method_name, MethodParams *params) {
1.16 paf 641: Pool& pool=r.pool();
642:
1.37 parser 643: const String& alphabet=params->as_string(0, "alphabet must not be code");
644: gdImage& image=*load(r, method_name, params->as_string(1, "file_name must not be code"));
1.51 parser 645: int spacebar_width=params->as_int(2, "spacebar_width must be int", r);
1.37 parser 646: int monospace_width;
647: if(params->size()>3) {
1.51 parser 648: monospace_width=params->as_int(3, "monospace_width must be int", r);
1.37 parser 649: if(!monospace_width)
650: monospace_width=image.SX();
651: } else
652: monospace_width=0;
1.16 paf 653:
1.37 parser 654: if(!alphabet.size())
1.57 parser 655: throw Exception(0, 0,
1.37 parser 656: &method_name,
657: "alphabet must not be empty");
658:
1.16 paf 659: static_cast<VImage *>(r.self)->font=new(pool) Font(pool,
1.37 parser 660: alphabet,
1.36 parser 661: image,
1.37 parser 662: image.SY() / alphabet.size(), monospace_width, spacebar_width);
1.16 paf 663: }
664:
1.17 paf 665: static void _text(Request& r, const String& method_name, MethodParams *params) {
1.16 paf 666: Pool& pool=r.pool();
667:
1.51 parser 668: int x=params->as_int(0, "x must be int", r);
669: int y=params->as_int(1, "y must be int", r);
1.36 parser 670: const String& s=params->as_string(2, "text must not be code");
1.16 paf 671:
672: VImage& vimage=*static_cast<VImage *>(r.self);
673: if(vimage.image)
674: if(vimage.font)
675: vimage.font->string_display(*vimage.image, x, y, s);
676: else
1.57 parser 677: throw Exception(0, 0,
1.16 paf 678: &method_name,
679: "set the font first");
680: else
1.57 parser 681: throw Exception(0, 0,
1.16 paf 682: &method_name,
683: "does not contain an image");
684: }
685:
1.47 parser 686: static void _length(Request& r, const String& method_name, MethodParams *params) {
687: Pool& pool=r.pool();
688:
689: const String& s=params->as_string(0, "text must not be code");
690:
691: VImage& vimage=*static_cast<VImage *>(r.self);
692: if(vimage.image)
693: if(vimage.font) {
694: VInt& result=*new(pool) VInt(pool, vimage.font->string_width(s));
695: result.set_name(method_name);
696: r.write_assign_lang(result);
697: } else
1.57 parser 698: throw Exception(0, 0,
1.47 parser 699: &method_name,
700: "set the font first");
701: else
1.57 parser 702: throw Exception(0, 0,
1.47 parser 703: &method_name,
704: "does not contain an image");
705: }
706:
1.48 parser 707: static void _arc(Request& r, const String& method_name, MethodParams *params) {
708: Pool& pool=r.pool();
709:
710: gdImage *image=static_cast<VImage *>(r.self)->image;
711: if(!image)
1.57 parser 712: throw Exception(0, 0,
1.48 parser 713: &method_name,
714: "does not contain an image");
715:
716: image->Arc(
1.51 parser 717: params->as_int(0, "center_x must be int", r),
718: params->as_int(1, "center_y must be int", r),
719: params->as_int(2, "width must be int", r),
720: params->as_int(3, "height must be int", r),
721: params->as_int(4, "start degrees must be int", r),
722: params->as_int(5, "end degrees must be int", r),
723: image->Color(params->as_int(6, "cx must be int", r)));
1.48 parser 724: }
725:
1.49 parser 726: static void _sector(Request& r, const String& method_name, MethodParams *params) {
727: Pool& pool=r.pool();
728:
729: gdImage *image=static_cast<VImage *>(r.self)->image;
730: if(!image)
1.57 parser 731: throw Exception(0, 0,
1.49 parser 732: &method_name,
733: "does not contain an image");
734:
735: image->Sector(
1.51 parser 736: params->as_int(0, "center_x must be int", r),
737: params->as_int(1, "center_y must be int", r),
738: params->as_int(2, "width must be int", r),
739: params->as_int(3, "height must be int", r),
740: params->as_int(4, "start degrees must be int", r),
741: params->as_int(5, "end degrees must be int", r),
742: image->Color(params->as_int(6, "color must be int", r)));
1.49 parser 743: }
744:
1.48 parser 745: static void _circle(Request& r, const String& method_name, MethodParams *params) {
746: Pool& pool=r.pool();
747:
748: gdImage *image=static_cast<VImage *>(r.self)->image;
749: if(!image)
1.57 parser 750: throw Exception(0, 0,
1.48 parser 751: &method_name,
752: "does not contain an image");
753:
1.51 parser 754: int size=params->as_int(2, "radius must be int", r)*2;
1.48 parser 755: image->Arc(
1.51 parser 756: params->as_int(0, "center_x must be int", r),
757: params->as_int(1, "center_y must be int", r),
1.50 parser 758: size, //w
759: size, //h
1.48 parser 760: 0, //s
761: 360, //e
1.51 parser 762: image->Color(params->as_int(3, "color must be int", r)));
1.48 parser 763: }
764:
1.53 parser 765: gdImage& as_image(Pool& pool, const String& method_name, MethodParams *params,
766: int index, const char *msg) {
767: Value& value=params->as_no_junction(index, msg);
768:
769: if(strcmp(value.type(), VIMAGE_TYPE)!=0)
1.57 parser 770: throw Exception(0, 0,
1.53 parser 771: &method_name,
772: msg);
773:
774: gdImage *src=static_cast<VImage *>(&value)->image;
775: if(!src)
1.57 parser 776: throw Exception(0, 0,
1.53 parser 777: &method_name,
778: msg);
779:
780: return *src;
781: }
782:
783: static void _copy(Request& r, const String& method_name, MethodParams *params) {
784: Pool& pool=r.pool();
785:
786: gdImage *dest=static_cast<VImage *>(r.self)->image;
787: if(!dest)
1.57 parser 788: throw Exception(0, 0,
1.53 parser 789: &method_name,
790: "self does not contain an image");
791:
792: gdImage& src=as_image(pool, method_name, params, 0, "src must be image");
793:
794: int sx=params->as_int(1, "src_x must be int", r);
795: int sy=params->as_int(2, "src_y must be int", r);
796: int sw=params->as_int(3, "src_w must be int", r);
797: int sh=params->as_int(4, "src_h must be int", r);
798: int dx=params->as_int(5, "dest_x must be int", r);
799: int dy=params->as_int(6, "dest_y must be int", r);
800: if(params->size()>1+2+2+2) {
801: int dw=params->as_int(1+2+2+2, "dest_w must be int", r);
1.56 parser 802: int dh=(int)(params->size()>1+2+2+2+1?
803: params->as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
804: int tolerance=params->size()>1+2+2+2+2?
805: params->as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 806:
1.56 parser 807: src.CopyResampled(*dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 808: } else
1.54 parser 809: src.Copy(*dest, dx, dy, sx, sy, sw, sh);
1.53 parser 810: }
811:
812:
1.22 paf 813: // constructor
814:
815: MImage::MImage(Pool& apool) : Methoded(apool) {
816: set_name(*NEW String(pool(), IMAGE_CLASS_NAME));
817:
818:
1.1 paf 819: // ^image:measure[DATA]
1.22 paf 820: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3 paf 821:
1.25 paf 822: // ^image.html[]
823: // ^image.html[hash]
1.22 paf 824: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 825:
1.25 paf 826: // ^image.load[background.gif]
1.22 paf 827: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 828:
1.25 paf 829: // ^image.create[width;height] bgcolor=white
830: // ^image.create[width;height;bgcolor]
1.22 paf 831: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 832:
1.25 paf 833: // ^image.gif[]
1.22 paf 834: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 0);
1.12 paf 835:
1.25 paf 836: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 837: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 838:
1.25 paf 839: // ^image.fill(x;y;color)
1.22 paf 840: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 841:
1.25 paf 842: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 843: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 844:
1.25 paf 845: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 846: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 847:
1.44 parser 848: // ^image.replace(color-source;color-dest)[table x:y]
849: add_native_method("replace", Method::CT_DYNAMIC, _replace, 3, 3);
850:
851: // ^image.polyline(color)[table x:y]
852: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 853:
1.44 parser 854: // ^image.polygon(color)[table x:y]
855: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 856:
1.44 parser 857: // ^image.polybar(color)[table x:y]
858: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 859:
1.36 parser 860: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
861: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;width)
862: add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
1.16 paf 863:
1.25 paf 864: // ^image.text(x;y)[text]
1.22 paf 865: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 866:
867: // ^image.ngth[text]
868: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 869:
1.48 parser 870: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
871: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 872:
873: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
874: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 875:
876: // ^image.circle(center x;center y;r;color)
877: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
878:
1.56 parser 879: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
880: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.22 paf 881: }
882:
883: // global variable
884:
885: Methoded *image_class;
886:
887: // creator
888:
889: Methoded *MImage_create(Pool& pool) {
890: return image_class=new(pool) MImage(pool);
1.1 paf 891: }
E-mail: