fbglyph.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. *
  3. * Copyright © 1998 Keith Packard
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and its
  6. * documentation for any purpose is hereby granted without fee, provided that
  7. * the above copyright notice appear in all copies and that both that
  8. * copyright notice and this permission notice appear in supporting
  9. * documentation, and that the name of Keith Packard not be used in
  10. * advertising or publicity pertaining to distribution of the software without
  11. * specific, written prior permission. Keith Packard makes no
  12. * representations about the suitability of this software for any purpose. It
  13. * is provided "as is" without express or implied warranty.
  14. *
  15. * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17. * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. * PERFORMANCE OF THIS SOFTWARE.
  22. */
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include "fb.h"
  27. #include <X11/fonts/fontstruct.h>
  28. #include "dixfontstr.h"
  29. #define dummyScreen screenInfo.screens[0]
  30. Bool
  31. fbGlyphIn(RegionPtr pRegion, int x, int y, int width, int height)
  32. {
  33. BoxRec box;
  34. BoxPtr pExtents = REGION_EXTENTS(pRegion);
  35. /*
  36. * Check extents by hand to avoid 16 bit overflows
  37. */
  38. if (x < (int) pExtents->x1)
  39. return FALSE;
  40. if ((int) pExtents->x2 < x + width)
  41. return FALSE;
  42. if (y < (int) pExtents->y1)
  43. return FALSE;
  44. if ((int) pExtents->y2 < y + height)
  45. return FALSE;
  46. box.x1 = x;
  47. box.x2 = x + width;
  48. box.y1 = y;
  49. box.y2 = y + height;
  50. return RECT_IN_REGION(pRegion, &box) == rgnIN;
  51. }
  52. #define WRITE1(d,n,fg) ((d)[n] = (CARD8) fg)
  53. #define WRITE2(d,n,fg) (*(CARD16 *) &(d[n]) = (CARD16) fg)
  54. #define WRITE4(d,n,fg) (*(CARD32 *) &(d[n]) = (CARD32) fg)
  55. #if FB_UNIT == 6 && IMAGE_BYTE_ORDER == LSBFirst
  56. #define WRITE8(d) (*(FbBits *) &(d[0]) = fg)
  57. #else
  58. #define WRITE8(d) WRITE4(d,0,_ABCA), WRITE4(d,4,_BCAB)
  59. #endif
  60. /*
  61. * This is a bit tricky, but it's brief. Write 12 bytes worth
  62. * of dest, which is four pixels, at a time. This gives constant
  63. * code for each pattern as they're always aligned the same
  64. *
  65. * a b c d a b c d a b c d bytes
  66. * A B C A B C A B C A B C pixels
  67. *
  68. * f0 f1 f2
  69. * A B C A B C A B C A B C pixels LSB
  70. * C A B C A B C A B C A B pixels MSB
  71. *
  72. * LSB MSB
  73. * A f0 f1
  74. * B f1 f2
  75. * C f2 f0
  76. * A B f0 f2
  77. * B C f1 f0
  78. * C A f2 f1
  79. * A B C A f0 f1
  80. * B C A B f1 f2
  81. * C A B C f2 f0
  82. */
  83. #undef _A
  84. #undef _B
  85. #undef _C
  86. #undef _AB
  87. #undef _BC
  88. #undef _CA
  89. #undef _ABCA
  90. #undef _BCAB
  91. #undef _CABC
  92. #if IMAGE_BYTE_ORDER == MSBFirst
  93. #define _A f1
  94. #define _B f2
  95. #define _C f0
  96. #define _AB f2
  97. #define _BC f0
  98. #define _CA f1
  99. #define _ABCA f1
  100. #define _BCAB f2
  101. #define _CABC f0
  102. #define CASE(a,b,c,d) ((a << 3) | (b << 2) | (c << 1) | d)
  103. #else
  104. #define _A f0
  105. #define _B f1
  106. #define _C f2
  107. #define _AB f0
  108. #define _BC f1
  109. #define _CA f2
  110. #define _ABCA f0
  111. #define _BCAB f1
  112. #define _CABC f2
  113. #define CASE(a,b,c,d) (a | (b << 1) | (c << 2) | (d << 3))
  114. #endif
  115. void
  116. fbGlyph24(FbBits * dstBits,
  117. FbStride dstStride,
  118. int dstBpp, FbStip * stipple, FbBits fg, int x, int height)
  119. {
  120. int lshift;
  121. FbStip bits;
  122. CARD8 *dstLine;
  123. CARD8 *dst;
  124. FbStip f0, f1, f2;
  125. int n;
  126. int shift;
  127. f0 = fg;
  128. f1 = FbRot24(f0, 16);
  129. f2 = FbRot24(f0, 8);
  130. dstLine = (CARD8 *) dstBits;
  131. dstLine += (x & ~3) * 3;
  132. dstStride *= (sizeof(FbBits) / sizeof(CARD8));
  133. shift = x & 3;
  134. lshift = 4 - shift;
  135. while (height--) {
  136. bits = *stipple++;
  137. n = lshift;
  138. dst = dstLine;
  139. while (bits) {
  140. switch (FbStipMoveLsb(FbLeftStipBits(bits, n), 4, n)) {
  141. case CASE(0, 0, 0, 0):
  142. break;
  143. case CASE(1, 0, 0, 0):
  144. WRITE2(dst, 0, _AB);
  145. WRITE1(dst, 2, _C);
  146. break;
  147. case CASE(0, 1, 0, 0):
  148. WRITE1(dst, 3, _A);
  149. WRITE2(dst, 4, _BC);
  150. break;
  151. case CASE(1, 1, 0, 0):
  152. WRITE4(dst, 0, _ABCA);
  153. WRITE2(dst, 4, _BC);
  154. break;
  155. case CASE(0, 0, 1, 0):
  156. WRITE2(dst, 6, _AB);
  157. WRITE1(dst, 8, _C);
  158. break;
  159. case CASE(1, 0, 1, 0):
  160. WRITE2(dst, 0, _AB);
  161. WRITE1(dst, 2, _C);
  162. WRITE2(dst, 6, _AB);
  163. WRITE1(dst, 8, _C);
  164. break;
  165. case CASE(0, 1, 1, 0):
  166. WRITE1(dst, 3, _A);
  167. WRITE4(dst, 4, _BCAB);
  168. WRITE1(dst, 8, _C);
  169. break;
  170. case CASE(1, 1, 1, 0):
  171. WRITE8(dst);
  172. WRITE1(dst, 8, _C);
  173. break;
  174. case CASE(0, 0, 0, 1):
  175. WRITE1(dst, 9, _A);
  176. WRITE2(dst, 10, _BC);
  177. break;
  178. case CASE(1, 0, 0, 1):
  179. WRITE2(dst, 0, _AB);
  180. WRITE1(dst, 2, _C);
  181. WRITE1(dst, 9, _A);
  182. WRITE2(dst, 10, _BC);
  183. break;
  184. case CASE(0, 1, 0, 1):
  185. WRITE1(dst, 3, _A);
  186. WRITE2(dst, 4, _BC);
  187. WRITE1(dst, 9, _A);
  188. WRITE2(dst, 10, _BC);
  189. break;
  190. case CASE(1, 1, 0, 1):
  191. WRITE4(dst, 0, _ABCA);
  192. WRITE2(dst, 4, _BC);
  193. WRITE1(dst, 9, _A);
  194. WRITE2(dst, 10, _BC);
  195. break;
  196. case CASE(0, 0, 1, 1):
  197. WRITE2(dst, 6, _AB);
  198. WRITE4(dst, 8, _CABC);
  199. break;
  200. case CASE(1, 0, 1, 1):
  201. WRITE2(dst, 0, _AB);
  202. WRITE1(dst, 2, _C);
  203. WRITE2(dst, 6, _AB);
  204. WRITE4(dst, 8, _CABC);
  205. break;
  206. case CASE(0, 1, 1, 1):
  207. WRITE1(dst, 3, _A);
  208. WRITE4(dst, 4, _BCAB);
  209. WRITE4(dst, 8, _CABC);
  210. break;
  211. case CASE(1, 1, 1, 1):
  212. WRITE8(dst);
  213. WRITE4(dst, 8, _CABC);
  214. break;
  215. }
  216. bits = FbStipLeft(bits, n);
  217. n = 4;
  218. dst += 12;
  219. }
  220. dstLine += dstStride;
  221. }
  222. }
  223. void
  224. fbPolyGlyphBlt(DrawablePtr pDrawable,
  225. GCPtr pGC,
  226. int x,
  227. int y,
  228. unsigned int nglyph, CharInfoPtr * ppci, pointer pglyphBase)
  229. {
  230. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  231. CharInfoPtr pci;
  232. unsigned char *pglyph; /* pointer bits in glyph */
  233. int gx, gy;
  234. int gWidth, gHeight; /* width and height of glyph */
  235. FbStride gStride; /* stride of glyph */
  236. void (*glyph) (FbBits *, FbStride, int, FbStip *, FbBits, int, int);
  237. FbBits *dst = 0;
  238. FbStride dstStride = 0;
  239. int dstBpp = 0;
  240. int dstXoff = 0, dstYoff = 0;
  241. glyph = 0;
  242. if (pGC->fillStyle == FillSolid && pPriv->and == 0) {
  243. fbGetDrawable(pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  244. switch (dstBpp) {
  245. case 8:
  246. glyph = fbGlyph8;
  247. break;
  248. case 16:
  249. glyph = fbGlyph16;
  250. break;
  251. case 24:
  252. glyph = fbGlyph24;
  253. break;
  254. case 32:
  255. glyph = fbGlyph32;
  256. break;
  257. }
  258. }
  259. x += pDrawable->x;
  260. y += pDrawable->y;
  261. while (nglyph--) {
  262. pci = *ppci++;
  263. pglyph = FONTGLYPHBITS(pglyphBase, pci);
  264. gWidth = GLYPHWIDTHPIXELS(pci);
  265. gHeight = GLYPHHEIGHTPIXELS(pci);
  266. if (gWidth && gHeight) {
  267. gx = x + pci->metrics.leftSideBearing;
  268. gy = y - pci->metrics.ascent;
  269. if (glyph && gWidth <= sizeof(FbStip) * 8 &&
  270. fbGlyphIn(fbGetCompositeClip(pGC), gx, gy, gWidth, gHeight)) {
  271. (*glyph) (dst + (gy + dstYoff) * dstStride,
  272. dstStride,
  273. dstBpp,
  274. (FbStip *) pglyph, pPriv->xor, gx + dstXoff, gHeight);
  275. }
  276. else
  277. {
  278. gStride = GLYPHWIDTHBYTESPADDED(pci) / sizeof(FbStip);
  279. fbPushImage(pDrawable,
  280. pGC,
  281. (FbStip *) pglyph,
  282. gStride, 0, gx, gy, gWidth, gHeight);
  283. }
  284. }
  285. x += pci->metrics.characterWidth;
  286. }
  287. }
  288. void
  289. fbImageGlyphBlt(DrawablePtr pDrawable,
  290. GCPtr pGC,
  291. int x,
  292. int y,
  293. unsigned int nglyph, CharInfoPtr * ppciInit, pointer pglyphBase)
  294. {
  295. FbGCPrivPtr pPriv = fbGetGCPrivate(pGC);
  296. CharInfoPtr *ppci;
  297. CharInfoPtr pci;
  298. unsigned char *pglyph; /* pointer bits in glyph */
  299. int gWidth, gHeight; /* width and height of glyph */
  300. FbStride gStride; /* stride of glyph */
  301. Bool opaque;
  302. int n;
  303. int gx, gy;
  304. void (*glyph) (FbBits *, FbStride, int, FbStip *, FbBits, int, int);
  305. FbBits *dst = 0;
  306. FbStride dstStride = 0;
  307. int dstBpp = 0;
  308. int dstXoff = 0, dstYoff = 0;
  309. glyph = 0;
  310. if (pPriv->and == 0) {
  311. fbGetDrawable(pDrawable, dst, dstStride, dstBpp, dstXoff, dstYoff);
  312. switch (dstBpp) {
  313. case 8:
  314. glyph = fbGlyph8;
  315. break;
  316. case 16:
  317. glyph = fbGlyph16;
  318. break;
  319. case 24:
  320. glyph = fbGlyph24;
  321. break;
  322. case 32:
  323. glyph = fbGlyph32;
  324. break;
  325. }
  326. }
  327. x += pDrawable->x;
  328. y += pDrawable->y;
  329. if (TERMINALFONT(pGC->font)
  330. && !glyph
  331. ) {
  332. opaque = TRUE;
  333. }
  334. else {
  335. int xBack, widthBack;
  336. int yBack, heightBack;
  337. ppci = ppciInit;
  338. n = nglyph;
  339. widthBack = 0;
  340. while (n--)
  341. widthBack += (*ppci++)->metrics.characterWidth;
  342. xBack = x;
  343. if (widthBack < 0) {
  344. xBack += widthBack;
  345. widthBack = -widthBack;
  346. }
  347. yBack = y - FONTASCENT(pGC->font);
  348. heightBack = FONTASCENT(pGC->font) + FONTDESCENT(pGC->font);
  349. fbSolidBoxClipped(pDrawable,
  350. fbGetCompositeClip(pGC),
  351. xBack,
  352. yBack,
  353. xBack + widthBack,
  354. yBack + heightBack,
  355. fbAnd(GXcopy, pPriv->bg, pPriv->pm),
  356. fbXor(GXcopy, pPriv->bg, pPriv->pm));
  357. opaque = FALSE;
  358. }
  359. ppci = ppciInit;
  360. while (nglyph--) {
  361. pci = *ppci++;
  362. pglyph = FONTGLYPHBITS(pglyphBase, pci);
  363. gWidth = GLYPHWIDTHPIXELS(pci);
  364. gHeight = GLYPHHEIGHTPIXELS(pci);
  365. if (gWidth && gHeight) {
  366. gx = x + pci->metrics.leftSideBearing;
  367. gy = y - pci->metrics.ascent;
  368. if (glyph && gWidth <= sizeof(FbStip) * 8 &&
  369. fbGlyphIn(fbGetCompositeClip(pGC), gx, gy, gWidth, gHeight)) {
  370. (*glyph) (dst + (gy + dstYoff) * dstStride,
  371. dstStride,
  372. dstBpp,
  373. (FbStip *) pglyph, pPriv->fg, gx + dstXoff, gHeight);
  374. }
  375. else
  376. {
  377. gStride = GLYPHWIDTHBYTESPADDED(pci) / sizeof(FbStip);
  378. fbPutXYImage(pDrawable,
  379. fbGetCompositeClip(pGC),
  380. pPriv->fg,
  381. pPriv->bg,
  382. pPriv->pm,
  383. GXcopy,
  384. opaque,
  385. gx,
  386. gy,
  387. gWidth, gHeight, (FbStip *) pglyph, gStride, 0);
  388. }
  389. }
  390. x += pci->metrics.characterWidth;
  391. }
  392. }