test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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:16"
  12. #define FONT "/usr/share/fonts/inconsolata/Inconsolata-Regular.ttf:16;" \
  13. "/usr/share/fonts/powerline-symbols/PowerlineSymbols.otf:14"
  14. //#define FONT "/usr/share/fonts/libertine/LinLibertine_R.otf:16"
  15. Display *dpy;
  16. Window win;
  17. GC gc;
  18. dt_context *ctx;
  19. dt_font *fnt;
  20. dt_style style;
  21. dt_style style_inv;
  22. static void setup_x();
  23. static void setup_dt();
  24. static void draw();
  25. int main()
  26. {
  27. XEvent evt;
  28. _Xdebug = 1;
  29. setup_x();
  30. setup_dt();
  31. draw();
  32. XSelectInput(dpy, win, ExposureMask | KeyPressMask);
  33. while (1) {
  34. XNextEvent(dpy, &evt);
  35. switch (evt.type) {
  36. case Expose:
  37. draw();
  38. break;
  39. case KeyPress:
  40. if (XLookupKeysym(&evt.xkey, 0) == XK_Escape)
  41. return 0;
  42. break;
  43. }
  44. }
  45. }
  46. static void setup_x()
  47. {
  48. unsigned long white, black;
  49. dpy = XOpenDisplay(NULL);
  50. white = XWhitePixel(dpy, DefaultScreen(dpy));
  51. black = XBlackPixel(dpy, DefaultScreen(dpy));
  52. win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  53. 700, 500, 0, white, white);
  54. XMapWindow(dpy, win);
  55. gc = XDefaultGC(dpy, 0);
  56. XSetForeground(dpy, gc, black);
  57. }
  58. static void setup_dt()
  59. {
  60. assert(!dt_init(&ctx, dpy, win));
  61. assert(!dt_load(ctx, &fnt, FONT));
  62. memset(&style, 0, sizeof(style));
  63. memset(&style_inv, 0, sizeof(style_inv));
  64. style_inv.red = 0xFF;
  65. style_inv.green = 0xFF;
  66. style_inv.blue = 0xFF;
  67. }
  68. static void draw()
  69. {
  70. dt_bbox bbox;
  71. assert(!dt_draw(ctx, fnt, &style, 10, 50, TEXT));
  72. assert(!dt_box(ctx, fnt, &bbox, TEXT));
  73. XFillRectangle(dpy, win, gc, 10 + bbox.x, 100 + bbox.y, bbox.w, bbox.h);
  74. assert(!dt_draw(ctx, fnt, &style_inv, 10, 100, TEXT));
  75. XFillRectangle(dpy, win, gc, 10 + bbox.x, 150 - fnt->ascent, bbox.w, fnt->height);
  76. assert(!dt_draw(ctx, fnt, &style_inv, 10, 150, TEXT));
  77. XFlush(dpy);
  78. }