x_afftext.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This is the XAffText module, which was originally independent of
  16. libplot. The header file that accompanies it is x_afftext.h.
  17. To use the module independently of libplot, simply do not specify
  18. "-DLIBPLOT" at compile time.
  19. The module supplies two external functions, which are generalizations of
  20. the core X11 function XDrawString: XAffDrawRotString and
  21. XAffDrawAffString. They draw, respectively, a rotated text string and
  22. (more generally) a matrix-transformed text string. In both cases a
  23. specified core X font is used. The rotation angle and transformation
  24. matrix are specified by the user. The matrix is passed as a 4-element
  25. array, with the element ordering convention and sign convention being
  26. those of the Matrix Extension to the XLFD (X Logical Font Description).
  27. `XAffText' is an abbreviation of `X11 affinely transformed text'. The
  28. module was inspired by Alan Richardson's xvertext module for displaying
  29. rotated text strings in X11, using the core X fonts. It works in a
  30. similar way. (It retrieves a bitmap from the X server into an XImage,
  31. transforms the XImage, monochrome pixel by pixel, and sends it back to a
  32. bitmap on the server, for use as a stipple.) But it supports arbitrary
  33. transformation matrices, and pays extra attention to pixel-level
  34. accuracy. It uses integer arithmetic when possible. */
  35. #include "x_afftext.h"
  36. #ifdef DEBUG
  37. #ifdef __cplusplus
  38. #include <cstdio>
  39. #else /* not __cplusplus */
  40. #include <stdio.h>
  41. #endif /* not __cplusplus */
  42. #endif /* DEBUG */
  43. #ifdef LIBPLOT
  44. #include "sys-defines.h" /* plotutils-specific */
  45. #include "extern.h" /* libplot-specific */
  46. #else /* not LIBPLOT */
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <limits.h>
  50. #include <math.h>
  51. #ifndef M_PI
  52. #define M_PI 3.14159265358979323846264
  53. #endif
  54. #define IMAX(a,b) ((a) > (b) ? (a) : (b))
  55. #define IMIN(a,b) ((a) < (b) ? (a) : (b))
  56. #define IROUND(x) ((int) ((x) > 0 ? (x) + 0.5 : (x) - 0.5))
  57. #define _pl_xmalloc malloc
  58. #define _pl_xcalloc calloc
  59. #endif /* not LIBPLOT */
  60. #define XAFF_XPROD(v,a) ((v).x * (a)[0] + (v).y * (a)[2])
  61. #define XAFF_YPROD(v,a) ((v).x * (a)[1] + (v).y * (a)[3])
  62. typedef struct XAffVectorStruct
  63. {
  64. int x, y;
  65. } XAffVector;
  66. typedef struct XAffRealVectorStruct
  67. {
  68. double x, y;
  69. } XAffRealVector;
  70. typedef struct XAffSizeStruct
  71. {
  72. unsigned int x, y;
  73. } XAffSize;
  74. typedef struct XAffAffinedTextStruct
  75. {
  76. Pixmap bitmap; /* depth-1 Pixmap, i.e., bitmap */
  77. XAffSize size; /* bitmap size */
  78. XAffVector origin; /* location of text origin within bitmap */
  79. } XAffAffinedText;
  80. /* forward decls of internal ctors/dtors */
  81. static XImage * XAffCreateXImage (Display *dpy, XAffSize size);
  82. static void XAffFreeXImage (XImage *im);
  83. static XAffAffinedText *XAffCreateAffinedText (Display *dpy, XFontStruct *font, double a[4], const char *text);
  84. static void XAffFreeAffinedText (Display *dpy, XAffAffinedText *afftext);
  85. /* other internal functions */
  86. static int can_use_XDrawString (XFontStruct *font, double a[4], const char *text);
  87. #ifdef DEBUG
  88. static void print_image (const XImage *im_in, XAffSize size);
  89. #endif /* DEBUG */
  90. /**************************************************************************/
  91. /* Create/destroy a depth-1 XImage object */
  92. /**************************************************************************/
  93. static XImage *
  94. XAffCreateXImage (Display *dpy, XAffSize size)
  95. {
  96. XImage *im;
  97. char *data;
  98. if (size.x == 0 || size.y == 0)
  99. return NULL;
  100. data = (char *)_pl_xcalloc(size.y * ((size.x + 7) / 8), 1);
  101. if (data == NULL)
  102. return NULL;
  103. im = XCreateImage (dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
  104. (unsigned int)1, /* depth = 1 */
  105. XYBitmap,
  106. 0, /* origin = 0 */
  107. data,
  108. size.x, size.y,
  109. 8, /* pad: quantum of each scanline */
  110. 0); /* scanlines contigous in memory */
  111. if (im == NULL)
  112. return NULL;
  113. im->bitmap_bit_order = MSBFirst;
  114. im->byte_order = MSBFirst;
  115. return im;
  116. }
  117. static void
  118. XAffFreeXImage (XImage *im)
  119. {
  120. free (im->data);
  121. XFree (im);
  122. }
  123. /**************************************************************************/
  124. /* Create/destroy an affinely transformed text string */
  125. /**************************************************************************/
  126. static XAffAffinedText *
  127. XAffCreateAffinedText (Display *dpy, XFontStruct *font, double a[4], const char *text)
  128. {
  129. XAffAffinedText *afftext = NULL;
  130. GC gc;
  131. XCharStruct bounds;
  132. int direction, font_ascent, font_descent;
  133. XAffSize size_in, size_out;
  134. XAffRealVector corner_in[4];
  135. XAffVector origin_in, origin_out;
  136. XAffVector mincorner, maxcorner;
  137. Pixmap bitmap_in, bitmap_out;
  138. XImage *im_in, *im_out;
  139. int scanline_len_in, scanline_len_out;
  140. double aa[4], a_inverse[4], det;
  141. int i, j;
  142. /* allocate memory for new instance */
  143. afftext = (XAffAffinedText *)_pl_xmalloc(sizeof(XAffAffinedText));
  144. if (!afftext)
  145. return NULL;
  146. /* as passed, a[] is in the format used in the matrix LFD enhancement,
  147. which assumes a right-handed coordinate system; so convert it to X11's
  148. left-hand coordinate system (y grows downward) */
  149. aa[0] = a[0];
  150. aa[1] = -a[1];
  151. aa[2] = -a[2];
  152. aa[3] = a[3];
  153. /* invert transformation matrix */
  154. det = aa[0] * aa[3] - aa[1] * aa[2];
  155. if (det == 0.0)
  156. return NULL; /* don't support singular matrices */
  157. a_inverse[0] = aa[3] / det;
  158. a_inverse[1] = - aa[1] / det;
  159. a_inverse[2] = - aa[2] / det;
  160. a_inverse[3] = aa[0] / det;
  161. /* to include all pixels in text, how large should bitmap be? */
  162. XTextExtents (font, text, strlen (text),
  163. &direction, &font_ascent, &font_descent, &bounds);
  164. /* bitmap size, number-of-pixels by number-of-pixels */
  165. size_in.x = - bounds.lbearing + bounds.rbearing;
  166. size_in.y = bounds.ascent + bounds.descent;
  167. /* within this bitmap, where is `origin' of text string? */
  168. origin_in.x = - bounds.lbearing;
  169. origin_in.y = bounds.ascent;
  170. /* paranoia */
  171. if (size_in.x == 0 || size_in.y == 0)
  172. return NULL;
  173. /* work around a possible bug: some X displays can't create pixmaps that
  174. are only one pixel wide or high */
  175. if (size_in.x == 1)
  176. size_in.x = 2;
  177. if (size_in.y == 1)
  178. size_in.y = 2;
  179. #ifdef DEBUG
  180. fprintf (stderr, "string \"%s\": lbearing=%hd, rbearing=%hd, ascent=%hd, descent=%hd\n", text, bounds.lbearing, bounds.rbearing, bounds.ascent, bounds.descent);
  181. fprintf (stderr, "\tsize=(%u,%u), origin=(%d,%d)\n", size_in.x, size_in.y, origin_in.x, origin_in.y);
  182. #endif
  183. /* create bitmap for text, and lightweight gc */
  184. bitmap_in = XCreatePixmap (dpy, DefaultRootWindow (dpy),
  185. size_in.x, size_in.y, (unsigned int)1);
  186. gc = XCreateGC (dpy, bitmap_in, (unsigned long)0, (XGCValues *)NULL);
  187. XSetBackground (dpy, gc, (unsigned long)0);
  188. XSetFont (dpy, gc, font->fid);
  189. /* clear the bitmap */
  190. XSetForeground (dpy, gc, (unsigned long)0);
  191. XFillRectangle (dpy, bitmap_in, gc, 0, 0, size_in.x, size_in.y);
  192. XSetForeground (dpy, gc, (unsigned long)1);
  193. /* draw text onto bitmap */
  194. XDrawString (dpy, bitmap_in, gc, origin_in.x, origin_in.y,
  195. text, strlen (text));
  196. /* create local image */
  197. im_in = XAffCreateXImage (dpy, size_in);
  198. if (im_in == NULL)
  199. return NULL;
  200. /* copy bitmap to it */
  201. XGetSubImage (dpy, bitmap_in, 0, 0, size_in.x, size_in.y,
  202. (unsigned long)1, XYPixmap, im_in, 0, 0);
  203. im_in->format = XYBitmap;
  204. #ifdef DEBUG
  205. print_image (im_in, size_in);
  206. #endif /* DEBUG */
  207. /* free now-unneeded bitmap */
  208. XFreePixmap (dpy, bitmap_in);
  209. /* vertices of image, in real coordinates, if each pixel is taken to be a
  210. unit square */
  211. corner_in[0].x = -0.5;
  212. corner_in[0].y = -0.5;
  213. corner_in[1].x = (int)size_in.x - 0.5;
  214. corner_in[1].y = -0.5;
  215. corner_in[2].x = (int)size_in.x - 0.5;
  216. corner_in[2].y = (int)size_in.y - 0.5;
  217. corner_in[3].x = -0.5;
  218. corner_in[3].y = (int)size_in.y - 0.5;
  219. /* compute vertices (in integer coordinates) of a rectangular array of
  220. pixels that will snugly hold the affinely transformed image */
  221. mincorner.x = mincorner.y = INT_MAX;
  222. maxcorner.x = maxcorner.y = INT_MIN;
  223. for (i = 0; i < 4; i++)
  224. {
  225. XAffRealVector v_shifted_in;
  226. XAffVector corner_out[4];
  227. v_shifted_in.x = corner_in[i].x - origin_in.x;
  228. v_shifted_in.y = corner_in[i].y - origin_in.y;
  229. corner_out[i].x = IROUND(XAFF_XPROD(v_shifted_in, aa)) + origin_in.x;
  230. corner_out[i].y = IROUND(XAFF_YPROD(v_shifted_in, aa)) + origin_in.y;
  231. mincorner.x = IMIN(mincorner.x, corner_out[i].x);
  232. mincorner.y = IMIN(mincorner.y, corner_out[i].y);
  233. maxcorner.x = IMAX(maxcorner.x, corner_out[i].x);
  234. maxcorner.y = IMAX(maxcorner.y, corner_out[i].y);
  235. }
  236. size_out.x = maxcorner.x - mincorner.x + 1;
  237. size_out.y = maxcorner.y - mincorner.y + 1;
  238. origin_out.x = origin_in.x - mincorner.x;
  239. origin_out.y = origin_in.y - mincorner.y;
  240. /* work around a possible bug: some X displays can't create pixmaps that
  241. are only one pixel wide or high */
  242. if (size_out.x == 1)
  243. size_out.x = 2;
  244. if (size_out.y == 1)
  245. size_out.y = 2;
  246. #ifdef DEBUG
  247. fprintf (stderr, "size_in = (%u,%u)\n", size_in.x, size_in.y);
  248. fprintf (stderr, "size_out = (%u,%u)\n", size_out.x, size_out.y);
  249. fprintf (stderr, "origin_in = (%d,%d)\n", origin_in.x, origin_in.y);
  250. fprintf (stderr, "origin_out = (%d,%d)\n", origin_out.x, origin_out.y);
  251. #endif
  252. /* create 2nd image, to hold affinely transformed text */
  253. im_out = XAffCreateXImage (dpy, size_out);
  254. if (im_out == NULL)
  255. return NULL;
  256. /* copy from 1st image to this new one */
  257. scanline_len_in = (size_in.x + 7) / 8;
  258. scanline_len_out = (size_out.x + 7) / 8;
  259. for (j = 0; j < (int)size_out.y; j++)
  260. {
  261. int scanline_hit;
  262. XAffVector v_in, v_out, v_shifted_out;
  263. scanline_hit = 0;
  264. v_out.y = j;
  265. v_shifted_out.y = v_out.y + mincorner.y - origin_in.y;
  266. for (i = 0; i < (int)size_out.x; i++)
  267. {
  268. v_out.x = i;
  269. v_shifted_out.x = v_out.x + mincorner.x - origin_in.x;
  270. v_in.x = IROUND(XAFF_XPROD(v_shifted_out, a_inverse)) + origin_in.x;
  271. v_in.y = IROUND(XAFF_YPROD(v_shifted_out, a_inverse)) + origin_in.y;
  272. if ((!(v_in.x >= 0)) || (!(v_in.x < (int)size_in.x)) ||
  273. (!(v_in.y >= 0)) || (!(v_in.y < (int)size_in.y)))
  274. {
  275. if (scanline_hit)
  276. /* will be no more hits; so move to next scanline */
  277. break;
  278. else /* move to next position on this scanline */
  279. continue;
  280. }
  281. else
  282. scanline_hit = 1;
  283. if (im_in->data[v_in.y * scanline_len_in + v_in.x / 8]
  284. & (128 >> (v_in.x % 8)))
  285. {
  286. im_out->data[v_out.y * scanline_len_out + v_out.x / 8]
  287. |= (128 >> (v_out.x % 8));
  288. }
  289. }
  290. }
  291. /* free now-unneeded 1st image */
  292. XAffFreeXImage (im_in);
  293. /* create bitmap to hold transformed text */
  294. bitmap_out = XCreatePixmap (dpy, DefaultRootWindow (dpy),
  295. size_out.x, size_out.y, (unsigned int)1);
  296. /* copy transformed text from 2nd image */
  297. XPutImage (dpy, bitmap_out, gc, im_out,
  298. 0, 0, 0, 0, size_out.x, size_out.y);
  299. #ifdef DEBUG
  300. print_image (im_out, size_out);
  301. #endif
  302. /* free 2nd image and GC */
  303. XAffFreeXImage (im_out);
  304. XFreeGC (dpy, gc);
  305. /* fill in data members of instance */
  306. afftext->bitmap = bitmap_out;
  307. afftext->size = size_out;
  308. afftext->origin = origin_out;
  309. return afftext;
  310. }
  311. static void
  312. XAffFreeAffinedText (Display *dpy, XAffAffinedText *afftext)
  313. {
  314. XFreePixmap (dpy, afftext->bitmap);
  315. free (afftext);
  316. }
  317. /**************************************************************************/
  318. /* Draw an affinely transformed text string */
  319. /**************************************************************************/
  320. int
  321. XAffDrawAffString (Display *dpy, Drawable drawable, GC gc, XFontStruct *font, int x, int y, double a[4], const char *text)
  322. {
  323. XAffAffinedText *afftext;
  324. GC our_gc;
  325. if (text == NULL || strlen (text) == 0)
  326. return 0;
  327. if (can_use_XDrawString (font, a, text))
  328. /* a[] must be equal to, or near the identity matrix */
  329. return XDrawString (dpy, drawable, gc, x, y, text, strlen (text));
  330. /* construct annotated bitmap, containing affinely transformed text */
  331. afftext = XAffCreateAffinedText (dpy, font, a, text);
  332. if (afftext == NULL)
  333. return 0;
  334. /* copy gc from user's gc */
  335. our_gc = XCreateGC (dpy, drawable, (unsigned long)0, (XGCValues *)NULL);
  336. XCopyGC (dpy, gc, GCForeground|GCFunction|GCPlaneMask, our_gc);
  337. /* use stipple drawing technique (screen-door patterning) */
  338. XSetFillStyle (dpy, our_gc, FillStippled);
  339. XSetStipple (dpy, our_gc, afftext->bitmap);
  340. XSetTSOrigin (dpy, our_gc,
  341. x - afftext->origin.x, y - afftext->origin.y);
  342. XFillRectangle (dpy, drawable, our_gc,
  343. x - afftext->origin.x, y - afftext->origin.y,
  344. afftext->size.x, afftext->size.y);
  345. /* free resources */
  346. XFreeGC (dpy, our_gc);
  347. XAffFreeAffinedText (dpy, afftext);
  348. return 0;
  349. }
  350. /**************************************************************************/
  351. /* Special case: draw a rotated text string */
  352. /**************************************************************************/
  353. int
  354. XAffDrawRotString (Display *dpy, Drawable drawable, GC gc, XFontStruct *font, int x, int y, double angle, const char *text)
  355. {
  356. double a[4];
  357. /* convert rotation angle to radians */
  358. angle *= (M_PI / 180.0);
  359. /* construct transformation matrix (using the XLFD-matrix-extension sign
  360. convention for the off-diagonal elements) */
  361. a[0] = + cos (angle);
  362. a[1] = + sin (angle);
  363. a[2] = - sin (angle);
  364. a[3] = + cos (angle);
  365. return XAffDrawAffString (dpy, drawable, gc, font, x, y, a, text);
  366. }
  367. /**************************************************************************/
  368. /* Can simply use core XDrawString function rather than transforming the */
  369. /* resulting bitmap? (Yes, if the matrix a[] is near the identity.) */
  370. /**************************************************************************/
  371. static int
  372. can_use_XDrawString (XFontStruct *font, double a[4], const char *text)
  373. {
  374. int direction, font_ascent, font_descent;
  375. XCharStruct bounds;
  376. XAffVector corner_in[4], corner_out[4];
  377. XAffSize size_in;
  378. XAffVector origin_in;
  379. int i, can_do_it = 1;
  380. double aa[4];
  381. /* as passed, a[] is in the format used in the matrix LFD enhancement,
  382. which assumes a right-handed coordinate system; so convert it to X11's
  383. left-hand coordinate system (y grows downward) */
  384. aa[0] = a[0];
  385. aa[1] = -a[1];
  386. aa[2] = -a[2];
  387. aa[3] = a[3];
  388. /* to include all pixels in text, how large should bitmap be? */
  389. XTextExtents (font, text, strlen (text),
  390. &direction, &font_ascent, &font_descent, &bounds);
  391. /* bitmap size, number-of-pixels by number-of-pixels */
  392. size_in.x = - bounds.lbearing + bounds.rbearing;
  393. size_in.y = bounds.ascent + bounds.descent;
  394. /* within this bitmap, where is `origin' of text string? */
  395. origin_in.x = - bounds.lbearing;
  396. origin_in.y = bounds.ascent;
  397. /* corners in integer coordinates, relative to origin */
  398. corner_in[0].x = 0;
  399. corner_in[0].y = 0;
  400. corner_in[1].x = size_in.x - 1;
  401. corner_in[1].y = 0;
  402. corner_in[2].x = size_in.x - 1;
  403. corner_in[2].y = size_in.y - 1;
  404. corner_in[3].x = 0;
  405. corner_in[3].y = size_in.y - 1;
  406. /* compute how corners are transformed by a[] */
  407. for (i = 0; i < 4; i++)
  408. {
  409. XAffVector v_shifted_in;
  410. v_shifted_in.x = corner_in[i].x - origin_in.x;
  411. v_shifted_in.y = corner_in[i].y - origin_in.y;
  412. corner_out[i].x = IROUND(XAFF_XPROD(v_shifted_in, aa)) + origin_in.x;
  413. corner_out[i].y = IROUND(XAFF_YPROD(v_shifted_in, aa)) + origin_in.y;
  414. if (corner_out[i].x != corner_in[i].x
  415. || corner_out[i].y != corner_in[i].y)
  416. /* at least one corner moves, no good, alas */
  417. {
  418. can_do_it = 0;
  419. break;
  420. }
  421. }
  422. return can_do_it;
  423. }
  424. /**************************************************************************/
  425. /* Print an image to stderr (used for debugging, if -DDEBUG is specified)*/
  426. /**************************************************************************/
  427. #ifdef DEBUG
  428. static void
  429. print_image (const XImage *im, XAffSize size)
  430. {
  431. int scanline_len;
  432. int i, j;
  433. scanline_len = (size.x + 7) / 8;
  434. for (j = 0; j < (int)size.y; j++)
  435. {
  436. for (i = 0; i < (int)size.x; i++)
  437. {
  438. if (im->data[j * scanline_len + i / 8] & (128 >> (i % 8)))
  439. fprintf (stderr, "*");
  440. else
  441. fprintf (stderr, " ");
  442. }
  443. fprintf (stderr, "\n");
  444. }
  445. }
  446. #endif /* DEBUG */