test.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Display *dpy;
  11. Window win;
  12. GC gc;
  13. dt_context *ctx;
  14. dt_font *fnt;
  15. dt_style style;
  16. dt_style style_inv;
  17. static void setup_x();
  18. static void setup_dt();
  19. static void draw();
  20. int main()
  21. {
  22. XEvent evt;
  23. _Xdebug = 1;
  24. setup_x();
  25. setup_dt();
  26. draw();
  27. XSelectInput(dpy, win, ExposureMask | KeyPressMask);
  28. while (1) {
  29. XNextEvent(dpy, &evt);
  30. switch (evt.type) {
  31. case Expose:
  32. draw();
  33. break;
  34. case KeyPress:
  35. if (XLookupKeysym(&evt.xkey, 0) == XK_Escape)
  36. return 0;
  37. break;
  38. }
  39. }
  40. }
  41. static void setup_x()
  42. {
  43. unsigned long white, black;
  44. dpy = XOpenDisplay(NULL);
  45. white = XWhitePixel(dpy, DefaultScreen(dpy));
  46. black = XBlackPixel(dpy, DefaultScreen(dpy));
  47. win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  48. 700, 500, 0, white, white);
  49. XMapWindow(dpy, win);
  50. gc = XDefaultGC(dpy, 0);
  51. XSetForeground(dpy, gc, black);
  52. }
  53. static void setup_dt()
  54. {
  55. assert(!dt_init(&ctx, dpy, win));
  56. assert(!dt_load(ctx, &fnt, 16, "/usr/share/fonts/fantasque-sans-mono/FantasqueSansMono-Regular.otf"));
  57. //assert(!dt_load(ctx, &fnt, 16, "/usr/share/fonts/libertine/LinLibertine_R.otf"));
  58. memset(&style, 0, sizeof(style));
  59. memset(&style_inv, 0, sizeof(style_inv));
  60. style_inv.red = 0xFF;
  61. style_inv.green = 0xFF;
  62. style_inv.blue = 0xFF;
  63. }
  64. static void draw()
  65. {
  66. assert(!dt_draw(ctx, fnt, &style, 10, 50, "The quick brown fox jumps over the lazy dog."));
  67. XFillRectangle(dpy, win, gc, 5, 60, 400, 100);
  68. assert(!dt_draw(ctx, fnt, &style_inv, 10, 90, "The quick brown fox jumps over the lazy dog."));
  69. XFlush(dpy);
  70. }