You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

113 rivejä
3.0 KiB

  1. /*
  2. * font libcaca font test program
  3. * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #if !defined(__KERNEL__)
  16. # if defined(HAVE_ENDIAN_H)
  17. # include <endian.h>
  18. # endif
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "caca.h"
  24. int main(int argc, char *argv[])
  25. {
  26. caca_canvas_t *cv;
  27. caca_display_t *dp;
  28. caca_font_t *f;
  29. caca_dither_t *d;
  30. uint8_t *buf;
  31. unsigned int w, h;
  32. char const * const * fonts;
  33. /* Create a canvas */
  34. cv = caca_create_canvas(8, 2);
  35. if(cv == NULL)
  36. {
  37. printf("Can't create canvas\n");
  38. return -1;
  39. }
  40. /* Draw stuff on our canvas */
  41. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
  42. caca_put_str(cv, 0, 0, "ABcde");
  43. caca_set_color_ansi(cv, CACA_LIGHTRED, CACA_BLACK);
  44. caca_put_str(cv, 5, 0, "\\o/");
  45. caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
  46. caca_put_str(cv, 0, 1, "&$âøÿØ?!");
  47. /* Load a libcaca internal font */
  48. fonts = caca_get_font_list();
  49. if(fonts[0] == NULL)
  50. {
  51. fprintf(stderr, "error: libcaca was compiled without any fonts\n");
  52. return -1;
  53. }
  54. f = caca_load_font(fonts[0], 0);
  55. if(f == NULL)
  56. {
  57. fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
  58. return -1;
  59. }
  60. /* Create our bitmap buffer (32-bit ARGB) */
  61. w = caca_get_canvas_width(cv) * caca_get_font_width(f);
  62. h = caca_get_canvas_height(cv) * caca_get_font_height(f);
  63. buf = malloc(4 * w * h);
  64. /* Render the canvas onto our image buffer */
  65. caca_render_canvas(cv, f, buf, w, h, 4 * w);
  66. /* Just for fun, render the image using libcaca */
  67. caca_set_canvas_size(cv, 80, 32);
  68. dp = caca_create_display(cv);
  69. {
  70. #if defined(HAVE_ENDIAN_H)
  71. if(__BYTE_ORDER == __BIG_ENDIAN)
  72. #else
  73. /* This is compile-time optimised with at least -O1 or -Os */
  74. uint32_t const tmp = 0x12345678;
  75. if(*(uint8_t const *)&tmp == 0x12)
  76. #endif
  77. d = caca_create_dither(32, w, h, 4 * w,
  78. 0xff0000, 0xff00, 0xff, 0xff000000);
  79. else
  80. d = caca_create_dither(32, w, h, 4 * w,
  81. 0xff00, 0xff0000, 0xff000000, 0xff);
  82. }
  83. caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
  84. caca_get_canvas_height(cv), d, buf);
  85. caca_refresh_display(dp);
  86. caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
  87. /* Free everything */
  88. caca_free_display(dp);
  89. free(buf);
  90. caca_free_dither(d);
  91. caca_free_font(f);
  92. caca_free_canvas(cv);
  93. return 0;
  94. }