basic.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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(&style, 0, sizeof(style));
  66. memset(&style_inv, 0, sizeof(style_inv));
  67. style_inv.red = 0xFF;
  68. style_inv.green = 0xFF;
  69. style_inv.blue = 0xFF;
  70. }
  71. static void draw()
  72. {
  73. dt_bbox bbox;
  74. assert(!dt_draw(ctx, fnt, &style, 10, 50, TEXT));
  75. assert(!dt_box(ctx, fnt, &bbox, TEXT));
  76. XFillRectangle(dpy, win, gc, 10 + bbox.x, 100 + bbox.y, bbox.w, bbox.h);
  77. assert(!dt_draw(ctx, fnt, &style_inv, 10, 100, TEXT));
  78. XFillRectangle(dpy, win, gc, 10 + bbox.x, 150 - fnt->ascent, bbox.w, fnt->height);
  79. assert(!dt_draw(ctx, fnt, &style_inv, 10, 150, TEXT));
  80. XFlush(dpy);
  81. }