1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
621
  
622
  
623
  
624
  
625
  
626
  
627
  
628
  
629
  
630
  
631
  
632
  
633
  
634
  
635
  
636
  
637
  
638
  
639
  
640
  
641
  
642
  
643
  
644
  
645
  
646
  
647
  
648
  
649
  
650
  
651
  
652
  
653
  
654
  
655
  
656
  
657
  
658
  
659
  
660
  
661
  
662
  
663
  
664
  
665
  
666
  
667
  
668
  
669
  
670
  
671
  
672
  
673
  
674
  
675
  
676
  
677
  
678
  
679
  
680
  
681
  
682
  
683
  
684
  
685
  
686
  
687
  
688
  
689
  
690
  
691
  
692
  
693
  
694
  
695
  
696
  
697
  
698
  
699
  
700
  
701
  
702
  
703
  
704
  
705
  
706
  
707
  
708
  
709
  
710
  
711
  
712
  
713
  
714
  
715
  
716
  
717
  
718
  
719
  
720
  
721
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
776
  
777
  
778
  
779
  
780
  
781
  
782
  
783
  
784
  
785
  
786
  
787
  
788
  
789
  
790
  
791
  
792
  
793
  
794
  
795
  
796
  
797
  
798
  
799
  
800
  
801
  
802
  
803
  
804
  
805
  
806
  
807
  
808
  
809
  
810
  
811
  
812
  
813
  
814
  
815
  
816
  
817
  
818
  
819
  
820
  
821
  
822
  
823
  
824
  
825
  
826
  
827
  
828
  
829
  
830
  
831
  
832
  
833
  
834
  
835
  
836
  
837
  
838
  
839
  
840
  
841
  
842
  
843
  
844
  
845
  
846
  
847
  
848
  
849
  
850
  
851
  
852
  
853
  
854
  
855
  
856
  
857
  
858
  
859
  
860
  
861
  
862
  
863
  
864
  
865
  
866
  
867
  
868
  
869
  
870
  
871
  
872
  
873
  
874
  
875
  
876
  
877
  
878
  
879
  
880
  
881
  
882
  
883
  
884
  
885
  
886
  
887
  
888
  
889
  
890
  
891
  
892
  
893
  
894
  
895
  
896
  
897
  
898
  
899
  
900
  
901
  
902
  
903
  
904
  
905
  
906
  
907
  
908
  
909
  
910
  
911
  
912
  
913
  
914
  
915
  
916
  
917
  
918
  
919
  
920
  
921
  
922
  
923
  
924
  
925
  
926
  
927
  
928
  
929
  
930
  
931
  
932
  
933
  
934
  
935
  
936
  
937
  
938
  
939
  
940
  
941
  
942
  
943
  
944
  
945
  
946
  
947
  
948
  
949
  
950
  
951
  
952
  
953
  
954
  
955
  
956
  
957
  
958
  
959
  
960
  
961
  
962
  
963
  
964
  
965
  
966
  
967
  
968
  
969
  
970
  
971
  
972
  
973
  
974
  
975
  
976
  
977
  
978
  
979
  
980
  
981
  
982
  
983
  
984
  
985
  
986
  
987
  
988
  
989
  
990
  
991
  
992
  
993
  
994
  
995
  
996
  
997
  
998
  
999
  
1000
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
string cvs_version="$Id: graphic_text.pike,v 1.37 1997/03/01 13:03:14 per Exp $"; 
#include <module.h> 
inherit "module"; 
inherit "roxenlib"; 
 
#if efun(_static_modules) 
# define map_array Array.map 
import Image; 
#else 
# define image Image 
# define font  Font 
#endif 
 
array register_module() 
{ 
  return ({ MODULE_LOCATION | MODULE_PARSER, 
              "Graphics text", 
              "Makes a few new tags, which render text to gifs " 
              "using the image module in pike. \n<p>" 
              "<b>&lt;gh1&gt;</b> to <b>&lt;gh6&gt;:</b> Headers<br>\n" 
              "<b>&lt;gh&gt;:</b> Header<br>\n" 
              "<b>&lt;gtext&gt;:</b> Graphical text<br>\n" 
              "<b>&lt;anfang&gt;:</b> Make the first character to a graphical one. Not all that usefull, really.<br>\n" 
              "<br>\n" 
              "<b>Common arguments:</b>\n <pre>" 
              " bg=color        Use this background, default taken from the\n" 
              "                 &lt;body&gt; tag, if any\n" 
              " fg=color        Use this foreground, default taken from the\n" 
              "                 &lt;body&gt; tag, if any\n" 
              " font=fnt        Use this font. The fonts can be found in the\n" 
              "                 directory specified in the configuration\n" 
              "                 interface.\n" 
              "                 If no font is specified, the one from the\n" 
              "                 define 'font' is used, or if there is no\n" 
              "                 define, the default font will be used.\n" 
              " scale=float     Scale to this font, mostly useful in the &lt;gtext&gt;\n" 
              "                 tag, will not work at all in the &lt;gh[number]&gt;\n" 
              "                 tags.\n" 
              " 2 3 4 5 6       Short for scale=1.0/([number]*0.6)\n" 
              " notrans         Do _not_ make the background color transparent\n" 
              " split           Make each word into a separate gif image\n" 
              " href=url        Link the image to the specified URL\n" 
              "                 The 'link' color of the document will be\n" 
              "                 used as the default foreground of the text\n" 
              " quant=cols      Use this number of colors\n" 
              " magic[=message] Modifier to href, more flashy links\n" 
              "                 Does <b>not</b> work with 'split'\n" 
              " fuzz[=color]    Apply the 'glow' effect to the result\n" 
              " fs              Use floyd-steinberg dithering\n" 
              " border=int,col. Draw an border (width is the first argument\n" 
              "                 in the specified color\n" 
              " spacing=int     Add this amount of spacing around the text\n" 
              " xspacing=int    like spacing, but only horizontal\n" 
              " yspacing=int    like spacing, but only vertical\n" 
              " size=int,int    Use this (absolute) size\n" 
              " xsize=int       Use this (absolute) size\n" 
              " ysize=int       Use this (absolute) size\n" 
              " bevel=int       Draw a bevel box (width is the argument)\n" 
              " pressed         Invert the \"direction\" of the bevel box\n" 
              " talign=dir      Justify the text to the left, right, or center\n" 
              " textbox=al,col. Use 'al' as opaque value to draw a box below\n" 
              "                 the text with the specified color.\n" 
              " xpad=X%         Increase padding between characters with X%\n" 
              " xpad=Y%         Increase padding between lines with Y%\n" 
              " shadow=int,dist Draw a drop-shadow (variable distance/intensity)\n" 
              " bshadow=dist    Draw a blured drop-shadow (variable distance)\n" 
              " glow=color      Draw a 'glow' outline around the text.\n" 
              " opaque=0-100%   Draw with more or less opaque text (100%\n" 
              "                 is default)\n" 
              " rotate=ang(deg.)Rotate the finished image\n" 
              " background=file Use the specifed file as a background\n" 
              " texture=file    Use the specified file as text texture\n" 
              " turbulence=args args is: frequency,color;freq,col;freq,col\n" 
              "                 Apply a turbulence filter, and use this as the\n" 
              "                 background.\n" 
              " maxlen=arg      The maximum length of the rendered text will be\n" 
              "                 the specified argument. The default is 300, this\n" 
              "                 is used to safeguard against mistakes like\n"        
              "&lt;gh1&gt;&lt;/gh&gt;, which would otherwise parse the whole\n" 
              "document.\n" 
              "\n" 
              "<b>Arguments passed on the the &lt;a&gt; tag (if href is specified):</b>\n " 
              " target=...\n" 
              " onClick=...\n" 
              "</pre>\n", 
              0, 
              1, 
              }); 
} 
 
 
array (string) list_fonts() 
{ 
  array fnts; 
  catch(fnts = get_dir("fonts/32/") - ({".",".."})); 
  if(!fnts) 
  { 
    report_error("Failed to find any fonts in 'fonts/32/'. No default font.\n"); 
    return ({}); 
  } 
  return fnts; 
} 
 
void create() 
{ 
 
  defvar("deflen", 300, "Default maximum text-length", TYPE_INT, 
         "The module will, per default, not try to render texts " 
         "longer than this. This is a safeguard for things like " 
         "&lt;gh1&gt;&lt;/gh&gt;, which would otherwise parse the" 
         " whole document. This can be overrided with maxlen=... in the " 
         "tag."); 
 
  defvar("location", "/gtext/", "Mountpoint", TYPE_LOCATION, 
         "The URL-prefix for the anfang characters."); 
 
  defvar("cols", 16, "Default number of colors per image", TYPE_INT_LIST, 
         "The default number of colors to use. 16 seems to be enough. " 
         "The size of the image depends on the number of colors", 
         ({ 1,2,3,4,5,6,7,8,10,16,32,64,128,256 })); 
   
  defvar("default_size", 32, "Default font size", TYPE_INT_LIST, 
         "The default size for the font. This is used for the 'base' size, " 
         "and can be scaled up or down in the tags.", 
         ({ 16, 32, 64 })); 
 
  defvar("default_font", "urw_itc_avant_garde-demi-r", "Default font", 
         TYPE_STRING_LIST, 
         "The default font. The 'font dir' will be prepended to the path", 
         list_fonts()); 
} 
 
string query_location() { return query("location"); } 
 
mapping (string:object) fonts = ([]); 
 
object(font) load_font(string name, string justification, int xs, int ys) 
{ 
  object fnt = font(); 
 
  if(!name) name=""; 
  if(sscanf(name, "%*s/%*s") != 2) 
    name=QUERY(default_size)+"/"+name; 
 
  name = "fonts/" + name; 
 
  if(!fnt->load( name )) 
  { 
    perror("Failed to load the font "+name+", using the default font.\n"); 
    if(!fnt->load("fonts/"+QUERY(default_size) +"/"+ QUERY(default_font))) 
      error("Failed to load the default font\n"); 
  } 
  catch { 
    if(justification=="right") fnt->right(); 
    if(justification=="center") fnt->center(); 
    if(xs)fnt->set_x_spacing((100.0+(float)xs)/100.0); 
    if(ys)fnt->set_y_spacing((100.0+(float)ys)/100.0); 
  }; 
  return fnt; 
} 
 
static private mapping (int:mapping(string:mixed)) cached_args = ([ ]); 
 
#define MAX(a,b) ((a)<(b)?(b):(a)) 
 
static private mapping (int:array(array(int))) matrixes = ([]); 
array (array(int)) make_matrix(int size) 
{ 
  if(matrixes[size]) return matrixes[size]; 
  array res; 
  int i; 
  int j; 
  res = map_array(allocate(size), lambda(int s, int size){ 
    return allocate(size); }, size); 
 
  for(i=0; i<size; i++) 
    for(j=0; j<size; j++) 
      res[i][j] = (int)MAX((float)size/2.0-sqrt((size/2-i)*(size/2-i) + (size/2-j)*(size/2-j)),0); 
  return matrixes[size] = res; 
} 
 
string fix_relative(string file, object id) 
{ 
  if(file != "" && file[0] == '/') return file; 
  file = combine_path(dirname(id->not_query) + "/",  file); 
  return file; 
} 
 
object last_image;      // Cache the last image for a while. 
string last_image_name; 
object (image) load_image(string f,object id) 
{ 
  if(last_image_name==f) return last_image; 
  string data; 
  object file; 
  object img = image(); 
 
  if(!(data=roxen->try_get_file(fix_relative(f, id),id))) 
    if(!(file=open(f,"r")) || (!(data=file->read(0x7fffffff)))) 
      return 0; 
   
  if(!img->frompnm(data) && !img->fromgif(data)) return 0; 
 
  last_image_name=f; 
  last_image=img; 
  call_out(lambda(){last_image=0;last_image_name=0;}, 0); 
  return img->copy(); 
} 
 
object (image) blur(object (image) img, int amnt) 
{ 
  img->setcolor(0,0,0); 
  img = img->autocrop(amnt, 0,0,0,0, 0,0,0); 
 
  for(int i=0; i<amnt; i++)  
    img = img->apply_matrix( make_matrix((int)sqrt(img->ysize()+20))); 
  return img; 
} 
 
object (image) outline(object (image) on, object (image) with, 
                       array (int) color, int radie, int x, int y) 
{ 
  object foo = image(with->xsize(), with->ysize(), @color); 
   
  int steps=10; 
  for(int j=0; j<=steps; j++) 
    on->paste_mask(foo, with,(int)(0.5+x-(sin((float)j/steps*3.145*2)*radie)), 
                   (int)(0.5+y-(cos((float)j/steps*3.145*2)*radie))); 
  foo=0; 
  return on; 
} 
 
array white = ({ 255,255,255 }); 
array lgrey = ({ 200,200,200 }); 
array grey = ({ 128,128,128 }); 
array black = ({ 0,0,0 }); 
 
array wwwb = ({ lgrey,lgrey,grey,black }); 
object (image) bevel(object (image) in, int width, int|void invert) 
{ 
  int h=in->ysize(); 
  int w=in->xsize(); 
 
  object corner = image(width+1,width+1); 
  object corner2 = image(width+1,width+1); 
  object pix = image(1,1); 
 
  for(int i=-1; i<=width; i++) { 
    corner->line(i,width-i,i,-1, @white); 
    corner2->setpixel(width-i, width-i, @white); 
    in->paste_alpha(pix, 185, w - width + i+1, h - width + i+1); 
  } 
 
  if(!invert) 
  { 
    in->paste_alpha(image(width,h-width*2,@white), 160, 0, width); 
    in->paste_alpha(image(width,h-width*2,@black), 128, in->xsize()-width, width); 
    in->paste_alpha(image(w-width,width,@white), 160, 0, 0); 
    in->paste_alpha(image(w-width,width,@black), 128, width, in->ysize()-width); 
  } else  { 
    corner=corner->invert(); 
    corner2=corner2->invert(); 
    in->paste_alpha(image(width,h-width*2,@black), 160, 0, width); 
    in->paste_alpha(image(width,h-width*2,@white), 128, in->xsize()-width, width); 
    in->paste_alpha(image(w-width,width,@black), 160, 0, 0); 
    in->paste_alpha(image(w-width,width,@white), 128, width, in->ysize()-width); 
  } 
 
  in->paste_mask(corner, corner->color(95,95,95), in->xsize()-width,-1); 
  in->paste_mask(corner, corner->invert()->color(128,128,128), 
                 in->xsize()-width,-1); 
  in->paste_mask(corner, corner->color(95,95,95), -1, in->ysize()-width); 
  in->paste_mask(corner, corner->invert()->color(128,128,128), 
                 -1, in->ysize()-width); 
  in->paste_mask(corner2, corner2->color(70,70,70), -1, -1); 
 
  corner = corner2 = pix = 0; 
 
  return in; 
} 
 
 
object (image) make_text_image(mapping args, object font, string text,object id) 
{ 
  object (image) text_alpha=font->write(@(text/"\n")); 
  int xoffset=0, yoffset=0; 
 
  if(!text_alpha->xsize() || !text_alpha->ysize()) 
    text_alpha = image(10,10, 0,0,0); 
   
//  perror("Making image of '%s', args=%O\n", text, args); 
 
  if(int op=((((int)args->opaque)*255)/100)) // Transparent text... 
    text_alpha=text_alpha->color(op,op,op); 
 
  int txsize=text_alpha->xsize(); 
  int tysize=text_alpha->ysize(); // Size of the text, in pixels.  
 
  int xsize=txsize; // image size, in pixels 
  int ysize=tysize; 
 
//  perror("Xsize=%d; ysize=%d\n",xsize,ysize); 
 
  if(args->bevel) 
  { 
    xoffset += (int)args->bevel; 
    yoffset += (int)args->bevel; 
    xsize += ((int)args->bevel)*2; 
    ysize += ((int)args->bevel)*2; 
  } 
 
  if(args->spacing) 
  { 
    xoffset += (int)args->spacing; 
    yoffset += (int)args->spacing; 
    xsize += ((int)args->spacing)*2; 
    ysize += ((int)args->spacing)*2; 
  } 
 
  if(args->yspacing) 
  { 
    yoffset += (int)args->yspacing; 
    ysize += ((int)args->yspacing)*2; 
  } 
 
  if(args->shadow) 
  { 
    xsize+=((int)(args->shadow/",")[-1])+2; 
    ysize+=((int)(args->shadow/",")[-1])+2; 
  } 
 
  if(args->bshadow) 
  { 
    xsize+=(int)args->bshadow+3; 
    ysize+=(int)args->bshadow+3; 
  } 
 
  if(args->move) 
  { 
    int dx,dy; 
    sscanf(args->move, "%d,%d", dx, dy); 
    xoffset += dx; 
    yoffset += dy; 
  } 
 
  if(args->xspacing) 
  { 
    xoffset += (int)args->xspacing; 
    xsize += ((int)args->xspacing)*2; 
  } 
 
  if(args->border) 
  { 
    xoffset += (int)args->border; 
    yoffset += (int)args->border; 
    xsize += ((int)args->border)*2; 
    ysize += ((int)args->border)*2; 
  } 
 
   
  array (int) bgcolor = parse_color(args->bg); 
  array (int) fgcolor = parse_color(args->fg); 
 
  object background,foreground; 
 
 
  if(args->texture)    foreground = load_image(args->texture,id); 
 
  if(args->background) 
  { 
    background = load_image(args->background,id); 
    xsize = background->xsize(); 
    ysize = background->ysize(); 
    switch(lower_case(args->talign||"left")) 
    { 
     case "center": 
      xoffset = (xsize/2 - txsize/2); 
      yoffset = (ysize/2 - tysize/2); 
      break; 
     case "right": 
      xoffset = (xsize - txsize); 
      break; 
     case "left": 
    } 
  } else 
    background = image(xsize, ysize, @bgcolor); 
 
  if(args->border) 
  { 
    int b = (int)args->border; 
    background->setcolor(@parse_color((args->border/",")[-1])); 
 
    for(--b;b>=0;b--) 
    { 
      // upper left -- upper right 
      background->line(b,b, xsize-b-1, b); 
 
      // lower left -- lower right 
      background->line(b,ysize-b-1, xsize-b-1, ysize-b-1); 
 
      // upper left -- lower left 
      background->line(b,b,   b, ysize-b-1); 
      // upper right -- lower right 
      background->line(xsize-b-1,b, xsize-b-1, ysize-b-1); 
    } 
  } 
   
  background->setcolor(@bgcolor); 
 
  if(args->size || args->xsize || args->ysize) 
  { 
    int xs=background->xsize(), ys=background->ysize(); 
    if(args->size) { xs=(int)args->size; ys=(int)(args->size/",")[-1]; } 
    if(args->xsize) xs=(int)args->xsize;  
    if(args->ysize) ys=(int)args->ysize; 
    background = background->copy(0,0,xs,ys); 
  } 
 
   
  if(args->turbulence) 
  { 
    array (float|array(int)) arg=({}); 
    foreach((args->turbulence/";"),  string s) 
    { 
      array q= s/","; 
      if(sizeof(q)<2) args+=({ ((float)s)||0.2, ({ 255,255,255 }) }); 
      arg+=({ ((float)q[0])||0.2, parse_color(q[1]) }); 
    } 
    background=background->turbulence(arg); 
  } 
   
 
  if(args->bevel) 
    background = bevel(background,(int)args->bevel,!!args->pressed); 
 
  if(args->textbox) // Draw a text-box on the background. 
  { 
    int alpha,border; 
    string bg; 
    sscanf(args->textbox, "%d,%s", alpha, bg); 
    sscanf(bg,"%s,%d", bg,border); 
    background->paste_alpha(image(txsize+border*2,tysize+border*2, 
                                  @parse_color(bg)), 
                            255-(alpha*255/100),xoffset-border,yoffset-border); 
  } 
 
   
  if(args->shadow) 
  { 
    int sd = ((int)args->shadow+10)*2; 
    int sdist = ((int)(args->shadow/",")[-1])+2; 
    object ta = text_alpha->copy(); 
    ta = ta->color(256-sd,256-sd,256-sd); 
    background->paste_mask(image(txsize,tysize),ta,xoffset+sdist, yoffset+sdist); 
  } 
 
#define MIN(x,y) ((x)<(y)?(x):(y)) 
 
  if(args->bshadow) 
  { 
    int sdist = (int)(args->bshadow)+1; 
    int xs,ys; 
    xs = text_alpha->xsize()+sdist*2+4; 
    ys = text_alpha->ysize()+sdist*2+4; 
    object ta = image(xs,ys); 
    ta->paste(text_alpha,sdist,sdist); 
    ta = blur(ta, MIN((sdist/2),1))->color(256,256,256); 
    background->paste_mask(image(xs,ys),ta,xoffset, yoffset); 
  } 
 
  if(args->glow) 
  { 
    int amnt = (int)(args->glow/",")[-1]+2; 
    array (int) blurc = parse_color((args->glow/",")[0]); 
    background->paste_mask(image(txsize+amnt*2,tysize*2, @blurc), 
                           blur(text_alpha, amnt), 
                           xoffset-amnt, yoffset-amnt); 
  } 
   
  if(args->chisel) 
    foreground=text_alpha->apply_matrix( ({ ({8,1,0}), 
                                            ({1,0,-1}), 
                                              ({0,-1,-8}) }), 128,128,128, 15 ) 
      ->color(@fgcolor); 
   
 
  if(!foreground)  foreground=image(txsize, tysize, @fgcolor); 
  if(args->textscale) 
  { 
    string c1="black",c2="black",c3="black",c4="black"; 
    sscanf(args->textscale, "%s,%s,%s,%s", c1, c2, c3, c4); 
    foreground->tuned_box(0,0, txsize,tysize, 
                          ({parse_color(c1),parse_color(c2),parse_color(c3), 
                              parse_color(c4)})); 
  } 
  if(args->outline) 
    outline(background, text_alpha, parse_color((args->outline/",")[0]), 
            ((int)(args->outline/",")[-1])+1, xoffset, yoffset); 
 
  background->paste_mask(foreground, text_alpha, xoffset, yoffset); 
 
  foreground = text_alpha = 0; 
 
 
  if(args->scale) 
    if((float)args->scale <= 2.0) 
      background = background->scale((float)args->scale); 
 
  if(args->rotate) 
  { 
    string c; 
    if(sscanf(args->rotate, "%*d,%s", c)==2) 
       background->setcolor(@parse_color(c)); 
    else 
       background->setcolor(@bgcolor); 
    background = background->rotate((float)args->rotate); 
  } 
 
  if(args->crop) background = background->autocrop(); 
   
  return background; 
} 
 
string base_key = "gtext:"+roxen->current_configuration->name; 
 
//void print_colors(array from) 
//{ 
//  for(int i=0; i<sizeof(from); i++) 
//    perror("%d: %s\n", i, color_name(from[i])); 
//} 
 
int number=(time(1)/10) % 1000, _start=time(1)/10 % 1000; 
 
array(int)|string write_text(int _args, string text, int size, 
                             object id) 
{ 
  object img; 
  mapping args = cached_args[_args]; 
  if(!args) { 
    args=(["fg":"black","bg":"white"]); 
    text="Please reload this page"; 
  } 
 
  string key = base_key+(_args-_start); 
 
  text = replace(text, ({ "&lt;", "&gt;", "&amp;" }), ({ "<", ">", "&" })); 
 
  // Check the cache first.. 
  if(mixed data = cache_lookup(key, text)) 
  { 
    if(args->nocache) // Remove from cache. Very usable for access counters 
      cache_remove(key, text); 
    if(size) return data[1]; 
    return data[0]; 
  } 
  //  Nothing found in the cache. Generate a new image. 
 
 
 
  string fkey = args->font+"/"+args->talign+"/"+args->xpad+"/"+args->ypad; 
  data = cache_lookup("fonts", fkey); 
  if(!data) 
  {  
    data = load_font(args->font, lower_case(args->talign||"left"),(int)args->xpad,(int)args->ypad); 
    cache_set("fonts", fkey, data); 
  } 
 
  // Fonts and such are now initialized. 
 
  img = make_text_image(args,data,text,id); 
 
  // Now we have the image in 'img', or nothing. 
 
  if(!img) return 0; 
   
  int q = (int)args->quant||(args->background||args->texture?250:QUERY(cols)); 
 
  if(q>255) q=255; 
  if(q<3) q=3; 
 
  // Quantify 
  if(!args->fs) 
  { 
//  print_colors(img->select_colors(q-1)+({parse_color(args->bg)})); 
    img = img->map_closest(img->select_colors(q-1)+({parse_color(args->bg)})); 
  } 
  // place in cache. 
  if(args->fs) 
    data=({ img->togif_fs(@(args->notrans?({}):parse_color(args->bg))), 
            ({img->xsize(),img->ysize()})}); 
  else 
    data=({ img->togif(@(args->notrans?({}):parse_color(args->bg))), 
            ({img->xsize(),img->ysize()})}); 
  img=0; 
 
  cache_set(key, text, data); 
  if(size) return data[1]; 
  return data[0]; 
} 
 
   
mapping find_file(string f, object rid) 
{ 
  string id; 
  sscanf(f,"%d/%s", id, f); 
  return http_string_answer(write_text((int)id,f,0,rid), "image/gif"); 
} 
 
string quote(string in) 
{ 
  string res=""; 
  for(int i=0; i<strlen(in); i++) 
    switch(in[i]) 
    { 
     case 'a'..'z': 
     case 'A'..'Z': 
     case '0'..'9': 
     case '.': 
     case ',': 
     case '!': 
      res += in[i..i]; 
      break; 
     default: 
      res += sprintf("%%%02x", in[i]); 
    } 
  return res; 
} 
 
 
int find_or_insert(mapping find) 
{ 
  array a = indices(cached_args); 
  array b = values(cached_args); 
  int i; 
  for(i=0; i<sizeof(a); i++) 
    if(equal(find, b[i])) { 
//    perror("Found old args..\n"); 
      return a[i]; 
    } 
 
//  perror("Using new args..\n%O\n", find); 
 
  cached_args[number]=find; 
  return number++; 
} 
 
 
string magic_javascript_header(object id) 
{ 
  if(!id->supports->javascript || !id->supports->images) return ""; 
  return 
    ("\n<script>\n" 
     "function img_act(ri,hi,txt)\n" 
     "{\n" 
     "  document.images[ri].src = hi.src;\n" 
     "  setTimeout(\"top.window.status = '\"+txt+\"'\", 100);\n" 
     "}\n" 
     "</script>\n"); 
} 
 
 
string magic_image(string url, int xs, int ys, string sn, 
                   string image_1, string image_2, string alt, 
                   string mess,object id,string input,string extra_args) 
{ 
  if(!id->supports->images) return alt; 
  if(!id->supports->javascript) 
    return (!input)? 
      ("<a "+extra_args+"href=\""+url+"\"><img _parsed=1 src="+image_1+" name="+ 
       sn+" border=0 alt=\""+alt+"\" ></a>\n"): 
    ("<input type=image "+extra_args+" src="+image_1+" name="+input+">"); 
 
  return 
    ("<script>\n" 
     " "+sn+"l = new Image("+xs+", "+ys+");"+sn+"l.src = \""+image_1+"\";\n" 
     " "+sn+"h = new Image("+xs+", "+ys+");"+sn+"h.src = \""+image_2+"\";\n" 
     "</script>\n"+ 
     ("<a "+extra_args+"href=\""+url+"\" "+ 
      (input?"onClick='document.forms[0].submit();' ":"") 
      +"onMouseover=\"img_act('"+sn+"',"+sn+"h,'"+(mess||url)+"');\"\n" 
      "onMouseout='document.images[\""+sn+"\"].src = "+sn+"l.src;'><img " 
      "_parsed=1 width="+xs+" height="+ys+" src="+image_1+" name="+sn+ 
      " border=0 alt=\""+alt+"\" ></a>\n")); 
} 
 
 
string extra_args(mapping in) 
{ 
  string s=""; 
  foreach(indices(in), string i) 
  { 
    switch(i) 
    { 
     case "target": 
     case "hspace": 
     case "vspace": 
     case "onclick": 
      s+=i+"='"+in[i]+"' "; 
      m_delete(in, i); 
      break; 
    } 
  } 
  return s; 
} 
 
string tag_gtext_id(string t, mapping arg, 
                    object id, object foo, mapping defines) 
{ 
  int short=!!arg->short; 
  m_delete(arg, "short"); m_delete(arg, "maxlen"); 
  m_delete(arg,"magic");  m_delete(arg,"submit"); 
  extra_args(arg);        m_delete(arg,"split"); 
  if(defines->fg && !arg->fg) arg->fg=defines->fg; 
  if(defines->bg && !arg->bg) arg->bg=defines->bg; 
  if(!arg->font) arg->font=defines->font||QUERY(default_font); 
 
  int num = find_or_insert( arg ); 
 
  if(!short) 
    return query_location()+num+"/"; 
  else 
    return (string)num; 
} 
 
string tag_graphicstext(string t, mapping arg, string contents, 
                        object id, object foo, mapping defines) 
{ 
// Allow <accessed> and others inside <gtext>. 
   
#if efun(_static_modules) 
  contents = parse_rxml(contents, id, foo, defines); 
#else 
  contents = parse_rxml(contents, id, foo); 
#endif 
 
  string pre, post, defalign, gt, rest, magic; 
  int i, split; 
 
 // No images here, let's generate an alternative.. 
  if(!id->supports->images || id->prestate->noimages) 
  { 
    if(!arg->split) contents=replace(contents,"\n", "\n<br>\n"); 
    if(arg->submit) return "<input type=submit name=\""+(arg->name+".x")+"\" value=\""+contents+"\">"; 
    switch(t) 
    { 
     case "gtext": 
     case "anfang": 
      if(arg->href) 
        return "<a href=\""+arg->href+"\">"+contents+"</a>"; 
      return contents; 
     default: 
      if(sscanf(t, "%s%d", t, i)==2) 
        rest="<h"+i+">"+contents+"</h"+i+">"; 
      else 
        rest="<h1>"+contents+"</h1>"; 
      if(arg->href) 
        return "<a href=\""+arg->href+"\">"+rest+"</a>"; 
      return rest; 
       
    } 
  } 
 
  contents = contents[..((int)arg->maxlen||QUERY(deflen))]; 
  m_delete(arg, "maxlen"); 
 
  if(arg->magic) 
  { 
    magic=arg->magic; 
    m_delete(arg,"magic"); 
  } 
 
  int input; 
  if(arg->submit) 
  { 
    input=1; 
    m_delete(arg,"submit"); 
  } 
   
  string lp, url, ea; 
 
  ea = extra_args(arg); 
 
  // Modify the 'arg' mapping... 
  if(arg->href) 
  { 
    url = arg->href; 
    lp = "<a href=\""+arg->href+"\" "+ea+">"; 
    if(!arg->fg) arg->fg=defines->link||"#0000ff"; 
    m_delete(arg, "href"); 
  } 
 
  if(defines->fg && !arg->fg) arg->fg=defines->fg; 
  if(defines->bg && !arg->bg) arg->bg=defines->bg; 
  if(!arg->font) arg->font=defines->font||QUERY(default_font); 
 
  if(arg->split) 
  { 
    split=1; 
    m_delete(arg,"split"); 
  } 
 
  // Support for <gh 2> like things. 
  for(i=2; i<10; i++)  
    if(arg[(string)i]) 
    { 
      arg->scale = 1.0 / ((float)i*0.6); 
      m_delete(arg, (string)i); 
      break; 
    } 
 
  // Support for <gh1> like things. 
  if(sscanf(t, "%s%d", t, i)==2) 
    if(i > 1) arg->scale = 1.0 / ((float)i*0.6); 
 
  string na = arg->name, al=arg->align; 
  m_delete(arg, "name"); m_delete(arg, "align"); 
 
  // Now the 'args' mapping is modified enough.. 
  int num = find_or_insert( arg ); 
 
  gt=contents; 
  rest=""; 
 
  if(split) 
  { 
    string word; 
    array res = ({}); 
    string pre = query_location()+num+"/"; 
 
    if(lp) res+=({ lp }); 
     
    gt=replace(gt, "\n", " "); 
     
    foreach(gt/" "-({""}), word) 
    { 
      array size = write_text(num,word,1,id); 
      res += ({ "<img _parsed=1 border=0 alt=\""+replace(word,"\"","'") 
                  +"\" src=\'"+pre+quote(word)+"\' width="+ 
                  size[0]+" height="+size[1]+" "+ea+">\n" 
                  }); 
    } 
    if(lp) res+=({ "</a>" }); 
    return res*""; 
  } 
   
  switch(t) 
  { 
   case "gh1": case "gh2": case "gh3": case "gh4": 
   case "gh5": case "gh6": case "gh7": 
   case "gh": pre="<p>"; post="<br>"; defalign="top"; break; 
   case "gtext": 
    pre="";  post=""; defalign="bottom"; 
    break; 
   case "anfang": 
    gt=contents[0..0]; rest=contents[1..]; 
    pre="<br clear=left>"; post=""; defalign="left"; 
    break; 
  } 
 
  array size = write_text(num,gt,1,id); 
 
  if(magic) 
  { 
    string res = ""; 
    if(!arg->fg) arg->fg=defines->link||"#0000ff"; 
    arg = mkmapping(indices(arg), values(arg)); 
    if(arg->fuzz) 
    { 
      if(arg->fuzz != "fuzz") 
        arg->glow = arg->fuzz; 
      else 
        arg->glow = arg->fg; 
    } 
    arg->fg = defines->alink||"#ff0000"; 
    if(arg->bevel) arg->pressed=1; 
 
    int num2 = find_or_insert(arg); 
    array size = write_text(num2,gt,1,id); 
 
    if(!defines->magic_java) res = magic_javascript_header(id); 
    defines->magic_java="yes"; 
 
    return res + 
      magic_image(url||"", size[0], size[1], "i"+(defines->mi++), 
                  query_location()+num+"/"+quote(gt), 
                  query_location()+num2+"/"+quote(gt), 
                  replace(gt, "\"","'"),(magic=="magic"?0:magic), 
                  id,input?na||"submit":0,ea); 
  } 
  if(input) 
    return (pre+"<input type=image name=\""+na+"\" border=0 alt=\""+ 
            replace(gt,"\"","'")+"\" src="+query_location()+num+"/"+quote(gt) 
            +" align="+(al || defalign)+ea+ 
            " width="+size[0]+" height="+size[1]+">"+rest+post); 
  return (pre+(lp?lp:"")+ 
          "<img _parsed=1 border=0  alt=\""+replace(gt,"\"","'")+"\" src="+ 
          query_location()+num+"/"+quote(gt)+" "+ea 
          +" align="+(al || defalign)+ 
          " width="+size[0]+" height="+size[1]+">"+rest+(lp?"</a>":"")+post); 
} 
 
inline string ns_color(array (int) col) 
{ 
  if(!arrayp(col)||sizeof(col)!=3) 
    return "#000000"; 
  return sprintf("#%02x%02x%02x", col[0],col[1],col[2]); 
} 
 
 
string make_args(mapping in) 
{ 
  array a=indices(in), b=values(in); 
  for(int i=0; i<sizeof(a); i++) 
    if(lower_case(b[i])!=a[i]) 
      if(search(b,"\"")==-1) 
        a[i]+="=\""+b[i]+"\""; 
      else 
        a[i]+="='"+b[i]+"'"; 
  return a*" "; 
} 
 
string tag_body(string t, mapping args, object id, object file, 
                mapping defines) 
{ 
  int cols,changed; 
  if(args->bgcolor||args->text||args->link||args->alink 
     ||args->background||args->vlink) 
    cols=1; 
 
#define FIX(Y,Z,X) do{if(!args->Y){if(cols){defines->X=Z;args->Y=Z;changed=1;}}else{defines->X=args->Y;if(args->Y[0]!='#'){args->Y=ns_color(parse_color(args->Y));changed=1;}}}while(0) 
 
  if(!search(id->client*"","Mosaic")) 
  { 
    FIX(bgcolor,"#bfbfbf",bg); 
    FIX(text,   "#000000",fg); 
    FIX(link,   "#0000b0",link); 
    FIX(alink,  "#3f0f7b",alink); 
    FIX(vlink,  "#ff0000",vlink); 
  } else { 
    FIX(bgcolor,"#c0c0c0",bg); 
    FIX(text,   "#000000",fg); 
    FIX(link,   "#0000ee",link); 
    FIX(alink,  "#ff0000",alink); 
    FIX(vlink,  "#551a8b",vlink); 
  } 
  if(changed) return ("<body "+make_args(args)+">"); 
} 
 
 
string tag_fix_color(string tagname, mapping args, object id, object file, 
                     mapping defines) 
{ 
  int changed; 
 
  if(!id->misc->colors) 
    id->misc->colors = ({ ({ defines->fg, defines->bg, tagname }) }); 
  else 
    id->misc->colors += ({ ({ defines->fg, defines->bg, tagname }) }); 
//  perror("Push color "+tagname+"\n"); 
#define FIX(X,Y) if(args->X){defines->Y=args->X;if(args->X[0]!='#'){args->X=ns_color(parse_color(args->X));changed = 1;}} 
 
  FIX(bgcolor,bg); 
  FIX(text,fg); 
  FIX(color,fg); 
  if(changed)return ("<"+tagname+" "+make_args(args)+">"); 
  return 0; 
} 
 
string pop_color(string tagname,mapping args,object id,object file, 
                 mapping defines) 
{ 
  array c = id->misc->colors; 
  sscanf(tagname, "/%s", tagname); 
  while(c && sizeof(c)) 
  { 
    if(c[-1][2]==tagname) 
    { 
      defines->fg = c[-1][0]; 
      defines->bg = c[-1][1]; 
//      perror("Pop color "+tagname+" ("+ 
//         (sizeof(id->misc->colors)-sizeof(c)+1)+")\n"); 
      break; 
    } 
    c = c[..sizeof(c)-2]; 
  } 
  id->misc->colors = c; 
} 
 
mapping query_tag_callers() 
{ 
  return ([ 
    "body":tag_body, 
    "gtext-id":tag_gtext_id, 
    "font":tag_fix_color, 
    "table":tag_fix_color, 
    "tr":tag_fix_color, 
    "td":tag_fix_color, 
    "layer":tag_fix_color, 
    "ilayer":tag_fix_color, 
 
    "/td":pop_color, 
    "/tr":pop_color, 
    "/font":pop_color, 
    "/body":pop_color, 
    "/table":pop_color, 
    "/layer":pop_color, 
    "/ilayer":pop_color, 
  ]); 
} 
 
 
mapping query_container_callers() 
{ 
  return ([ "anfang":tag_graphicstext, 
            "gh":tag_graphicstext, 
            "gh1":tag_graphicstext, "gh2":tag_graphicstext, 
            "gh3":tag_graphicstext, "gh4":tag_graphicstext, 
            "gh5":tag_graphicstext, "gh6":tag_graphicstext, 
            "gh7":tag_graphicstext, "gh8":tag_graphicstext, 
            "gh9":tag_graphicstext, "gh10":tag_graphicstext, 
            "gh11":tag_graphicstext,"gh12":tag_graphicstext, 
            "gtext":tag_graphicstext, ]); 
}