25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
3.0 KiB

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