string.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. enum
  5. {
  6. Max = 100
  7. };
  8. Point
  9. string(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s)
  10. {
  11. return _string(dst, pt, src, sp, f, s, nil, 1<<24, dst->clipr, nil, ZP, SoverD);
  12. }
  13. Point
  14. stringop(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, Drawop op)
  15. {
  16. return _string(dst, pt, src, sp, f, s, nil, 1<<24, dst->clipr, nil, ZP, op);
  17. }
  18. Point
  19. stringn(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, int len)
  20. {
  21. return _string(dst, pt, src, sp, f, s, nil, len, dst->clipr, nil, ZP, SoverD);
  22. }
  23. Point
  24. stringnop(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, int len, Drawop op)
  25. {
  26. return _string(dst, pt, src, sp, f, s, nil, len, dst->clipr, nil, ZP, op);
  27. }
  28. Point
  29. runestring(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r)
  30. {
  31. return _string(dst, pt, src, sp, f, nil, r, 1<<24, dst->clipr, nil, ZP, SoverD);
  32. }
  33. Point
  34. runestringop(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r, Drawop op)
  35. {
  36. return _string(dst, pt, src, sp, f, nil, r, 1<<24, dst->clipr, nil, ZP, op);
  37. }
  38. Point
  39. runestringn(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r, int len)
  40. {
  41. return _string(dst, pt, src, sp, f, nil, r, len, dst->clipr, nil, ZP, SoverD);
  42. }
  43. Point
  44. runestringnop(Image *dst, Point pt, Image *src, Point sp, Font *f, Rune *r, int len, Drawop op)
  45. {
  46. return _string(dst, pt, src, sp, f, nil, r, len, dst->clipr, nil, ZP, op);
  47. }
  48. Point
  49. _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)
  50. {
  51. int m, n, wid, max;
  52. ushort cbuf[Max], *c, *ec;
  53. uchar *b;
  54. char *subfontname;
  55. char **sptr;
  56. Rune **rptr;
  57. Font *def;
  58. if(s == nil){
  59. s = "";
  60. sptr = nil;
  61. }else
  62. sptr = &s;
  63. if(r == nil){
  64. r = (Rune*) L"";
  65. rptr = nil;
  66. }else
  67. rptr = &r;
  68. while((*s || *r) && len){
  69. max = Max;
  70. if(len < max)
  71. max = len;
  72. n = cachechars(f, sptr, rptr, cbuf, max, &wid, &subfontname);
  73. if(n > 0){
  74. _setdrawop(dst->display, op);
  75. m = 47+2*n;
  76. if(bg)
  77. m += 4+2*4;
  78. b = bufimage(dst->display, m);
  79. if(b == 0){
  80. fprint(2, "string: %r\n");
  81. break;
  82. }
  83. if(bg)
  84. b[0] = 'x';
  85. else
  86. b[0] = 's';
  87. BPLONG(b+1, dst->id);
  88. BPLONG(b+5, src->id);
  89. BPLONG(b+9, f->cacheimage->id);
  90. BPLONG(b+13, pt.x);
  91. BPLONG(b+17, pt.y+f->ascent);
  92. BPLONG(b+21, clipr.min.x);
  93. BPLONG(b+25, clipr.min.y);
  94. BPLONG(b+29, clipr.max.x);
  95. BPLONG(b+33, clipr.max.y);
  96. BPLONG(b+37, sp.x);
  97. BPLONG(b+41, sp.y);
  98. BPSHORT(b+45, n);
  99. b += 47;
  100. if(bg){
  101. BPLONG(b, bg->id);
  102. BPLONG(b+4, bgp.x);
  103. BPLONG(b+8, bgp.y);
  104. b += 12;
  105. }
  106. ec = &cbuf[n];
  107. for(c=cbuf; c<ec; c++, b+=2)
  108. BPSHORT(b, *c);
  109. pt.x += wid;
  110. bgp.x += wid;
  111. agefont(f);
  112. len -= n;
  113. }
  114. if(subfontname){
  115. if(_getsubfont(f->display, subfontname) == 0){
  116. def = f->display->defaultfont;
  117. if(def && f!=def)
  118. f = def;
  119. else
  120. break;
  121. }
  122. }
  123. }
  124. return pt;
  125. }