test.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* See LICENSE file for copyright and license details. */
  2. #include <assert.h>
  3. #include <stdint.h>
  4. #include <X11/Xlib.h>
  5. #include <X11/keysym.h>
  6. #include <X11/extensions/Xrender.h>
  7. #include <ft2build.h>
  8. #include FT_FREETYPE_H
  9. #include "dtext.h"
  10. #define TEXT L"The quick brown fox jumps over the lazy dog."
  11. //#define FONT "/usr/share/fonts/fantasque-sans-mono/FantasqueSansMono-Regular.otf"
  12. #define FONT "/usr/share/fonts/inconsolata/Inconsolata-Regular.ttf"
  13. //#define FONT "/usr/share/fonts/libertine/LinLibertine_R.otf"
  14. Display *dpy;
  15. Window win;
  16. GC gc;
  17. dt_context *ctx;
  18. dt_font *fnt;
  19. dt_style style;
  20. dt_style style_inv;
  21. static void setup_x();
  22. static void setup_dt();
  23. static void draw();
  24. int main()
  25. {
  26. XEvent evt;
  27. _Xdebug = 1;
  28. setup_x();
  29. setup_dt();
  30. draw();
  31. XSelectInput(dpy, win, ExposureMask | KeyPressMask);
  32. while (1) {
  33. XNextEvent(dpy, &evt);
  34. switch (evt.type) {
  35. case Expose:
  36. draw();
  37. break;
  38. case KeyPress:
  39. if (XLookupKeysym(&evt.xkey, 0) == XK_Escape)
  40. return 0;
  41. break;
  42. }
  43. }
  44. }
  45. static void setup_x()
  46. {
  47. unsigned long white, black;
  48. dpy = XOpenDisplay(NULL);
  49. white = XWhitePixel(dpy, DefaultScreen(dpy));
  50. black = XBlackPixel(dpy, DefaultScreen(dpy));
  51. win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  52. 700, 500, 0, white, white);
  53. XMapWindow(dpy, win);
  54. gc = XDefaultGC(dpy, 0);
  55. XSetForeground(dpy, gc, black);
  56. }
  57. static void setup_dt()
  58. {
  59. assert(!dt_init(&ctx, dpy, win));
  60. assert(!dt_load(ctx, &fnt, 16, FONT));
  61. memset(&style, 0, sizeof(style));
  62. memset(&style_inv, 0, sizeof(style_inv));
  63. style_inv.red = 0xFF;
  64. style_inv.green = 0xFF;
  65. style_inv.blue = 0xFF;
  66. }
  67. static void draw()
  68. {
  69. dt_bbox bbox;
  70. assert(!dt_draw(ctx, fnt, &style, 10, 50, TEXT));
  71. assert(!dt_box(fnt, &bbox, TEXT));
  72. XFillRectangle(dpy, win, gc, 10 + bbox.x, 90 + bbox.y, bbox.w, bbox.h);
  73. assert(!dt_draw(ctx, fnt, &style_inv, 10, 90, TEXT));
  74. XFlush(dpy);
  75. }