test.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "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. assert(!dt_draw(ctx, fnt, &style, 10, 50, TEXT));
  70. XFillRectangle(dpy, win, gc, 5, 60, 400, 100);
  71. assert(!dt_draw(ctx, fnt, &style_inv, 10, 90, TEXT));
  72. XFlush(dpy);
  73. }