basic.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_color color;
  21. dt_color color_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. assert(!dt_init(&ctx, dpy, win));
  31. assert(!dt_load(ctx, &fnt, FONT));
  32. setup_dt();
  33. draw();
  34. XSelectInput(dpy, win, ExposureMask | KeyPressMask);
  35. while (1) {
  36. XNextEvent(dpy, &evt);
  37. switch (evt.type) {
  38. case Expose:
  39. draw();
  40. break;
  41. case KeyPress:
  42. if (XLookupKeysym(&evt.xkey, 0) == XK_Escape)
  43. return 0;
  44. break;
  45. }
  46. }
  47. dt_free(ctx, fnt);
  48. dt_quit(ctx);
  49. XCloseDisplay(dpy);
  50. }
  51. static void setup_x()
  52. {
  53. unsigned long white, black;
  54. dpy = XOpenDisplay(NULL);
  55. white = XWhitePixel(dpy, DefaultScreen(dpy));
  56. black = XBlackPixel(dpy, DefaultScreen(dpy));
  57. win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  58. 700, 500, 0, white, white);
  59. XMapWindow(dpy, win);
  60. gc = XDefaultGC(dpy, 0);
  61. XSetForeground(dpy, gc, black);
  62. }
  63. static void setup_dt()
  64. {
  65. memset(&color, 0, sizeof(color));
  66. color.alpha = 0xFF;
  67. memset(&color_inv, 0, sizeof(color_inv));
  68. color_inv.red = 0xFF;
  69. color_inv.green = 0xFF;
  70. color_inv.blue = 0xFF;
  71. color_inv.alpha = 0xFF;
  72. }
  73. static void draw()
  74. {
  75. dt_bbox bbox;
  76. assert(!dt_draw(ctx, fnt, &color, 10, 50, TEXT));
  77. assert(!dt_box(ctx, fnt, &bbox, TEXT));
  78. XFillRectangle(dpy, win, gc, 10 + bbox.x, 100 + bbox.y, bbox.w, bbox.h);
  79. assert(!dt_draw(ctx, fnt, &color_inv, 10, 100, TEXT));
  80. XFillRectangle(dpy, win, gc, 10 + bbox.x, 150 - fnt->ascent, bbox.w, fnt->height);
  81. assert(!dt_draw(ctx, fnt, &color_inv, 10, 150, TEXT));
  82. XFlush(dpy);
  83. }