Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
5 enum
6 {
7 Max = 100
8 };
10 Point
11 string(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s)
12 {
13 return _string(dst, pt, src, sp, f, s, nil, 1<<24, dst->clipr, nil, ZP, SoverD);
14 }
16 Point
17 stringop(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, Drawop op)
18 {
19 return _string(dst, pt, src, sp, f, s, nil, 1<<24, dst->clipr, nil, ZP, op);
20 }
22 Point
23 stringn(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, int len)
24 {
25 return _string(dst, pt, src, sp, f, s, nil, len, dst->clipr, nil, ZP, SoverD);
26 }
28 Point
29 stringnop(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, int len, Drawop op)
30 {
31 return _string(dst, pt, src, sp, f, s, nil, len, dst->clipr, nil, ZP, op);
32 }
34 Point
35 runestring(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r)
36 {
37 return _string(dst, pt, src, sp, f, nil, r, 1<<24, dst->clipr, nil, ZP, SoverD);
38 }
40 Point
41 runestringop(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r, Drawop op)
42 {
43 return _string(dst, pt, src, sp, f, nil, r, 1<<24, dst->clipr, nil, ZP, op);
44 }
46 Point
47 runestringn(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r, int len)
48 {
49 return _string(dst, pt, src, sp, f, nil, r, len, dst->clipr, nil, ZP, SoverD);
50 }
52 Point
53 runestringnop(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r, int len, Drawop op)
54 {
55 return _string(dst, pt, src, sp, f, nil, r, len, dst->clipr, nil, ZP, op);
56 }
58 Point
59 _string(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, Rune *r, int len, Rectangle clipr, Image *bg, Point bgp, Drawop op)
60 {
61 int m, n, wid, max;
62 ushort cbuf[Max], *c, *ec;
63 uchar *b;
64 char *subfontname;
65 char **sptr;
66 Rune **rptr;
67 Font *def;
68 Subfont *sf;
70 if(len < 0)
71 sysfatal("libdraw: _string len=%d", len);
73 if(s == nil){
74 s = "";
75 sptr = nil;
76 }else
77 sptr = &s;
78 if(r == nil){
79 r = (Rune*) L"";
80 rptr = nil;
81 }else
82 rptr = &r;
83 sf = nil;
84 while((*s || *r) && len){
85 max = Max;
86 if(len < max)
87 max = len;
88 n = cachechars(f, sptr, rptr, cbuf, max, &wid, &subfontname);
89 if(n > 0){
90 _setdrawop(dst->display, op);
92 m = 47+2*n;
93 if(bg)
94 m += 4+2*4;
95 b = bufimage(dst->display, m);
96 if(b == 0){
97 fprint(2, "string: %r\n");
98 break;
99 }
100 if(bg)
101 b[0] = 'x';
102 else
103 b[0] = 's';
104 BPLONG(b+1, dst->id);
105 BPLONG(b+5, src->id);
106 BPLONG(b+9, f->cacheimage->id);
107 BPLONG(b+13, pt.x);
108 BPLONG(b+17, pt.y+f->ascent);
109 BPLONG(b+21, clipr.min.x);
110 BPLONG(b+25, clipr.min.y);
111 BPLONG(b+29, clipr.max.x);
112 BPLONG(b+33, clipr.max.y);
113 BPLONG(b+37, sp.x);
114 BPLONG(b+41, sp.y);
115 BPSHORT(b+45, n);
116 b += 47;
117 if(bg){
118 BPLONG(b, bg->id);
119 BPLONG(b+4, bgp.x);
120 BPLONG(b+8, bgp.y);
121 b += 12;
123 ec = &cbuf[n];
124 for(c=cbuf; c<ec; c++, b+=2)
125 BPSHORT(b, *c);
126 pt.x += wid;
127 bgp.x += wid;
128 agefont(f);
129 len -= n;
131 if(subfontname){
132 freesubfont(sf);
133 if((sf=_getsubfont(f->display, subfontname)) == 0){
134 def = f->display ? f->display->defaultfont : nil;
135 if(def && f!=def)
136 f = def;
137 else
138 break;
140 /*
141 * must not free sf until cachechars has found it in the cache
142 * and picked up its own reference.
143 */
146 return pt;