string.c 3.5 KB

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