basic.c 2.3 KB

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