Font.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include "driver.h"
  5. #include "driverlib.h"
  6. char *encoding;
  7. static int font_type = GFONT_STROKE;
  8. static void stroke_set(const char *filename)
  9. {
  10. if (font_init(filename) == 0)
  11. font_type = GFONT_STROKE;
  12. }
  13. static void freetype_set(const char *filename, int index)
  14. {
  15. if (font_init_freetype(filename, index) == 0)
  16. font_type = GFONT_FREETYPE;
  17. }
  18. void COM_Set_font(const char *name)
  19. {
  20. if (driver->Set_font) {
  21. (*driver->Set_font)(name);
  22. return;
  23. }
  24. if (G_is_absolute_path(name)) {
  25. if (!font_exists(name))
  26. return;
  27. freetype_set(name, 0);
  28. }
  29. else {
  30. int i;
  31. /* check if freetype font is available in freetypecap */
  32. for (i = 0; ftcap[i].name; i++)
  33. if (strcmp(name, ftcap[i].name) == 0) {
  34. switch (ftcap[i].type) {
  35. case GFONT_FREETYPE:
  36. freetype_set(ftcap[i].path, ftcap[i].index);
  37. COM_Set_encoding(ftcap[i].encoding);
  38. break;
  39. case GFONT_STROKE:
  40. stroke_set(ftcap[i].name);
  41. break;
  42. }
  43. return;
  44. }
  45. stroke_set("romans");
  46. }
  47. }
  48. void COM_Set_encoding(const char *enc)
  49. {
  50. if (encoding)
  51. G_free(encoding);
  52. encoding = G_store(enc);
  53. }
  54. int font_is_freetype(void)
  55. {
  56. return font_type == GFONT_FREETYPE;
  57. }
  58. void font_list(char ***list, int *count, int verbose)
  59. {
  60. char **fonts;
  61. int num_fonts;
  62. int i;
  63. for (i = 0; ftcap[i].name; i++) ;
  64. num_fonts = i;
  65. fonts = G_malloc(num_fonts * sizeof(const char *));
  66. for (i = 0; i < num_fonts; i++) {
  67. struct GFONT_CAP *p = &ftcap[i];
  68. if (verbose) {
  69. char buf[GPATH_MAX];
  70. sprintf(buf, "%s|%s|%d|%s|%d|%s|",
  71. p->name, p->longname, p->type,
  72. p->path, p->index, p->encoding);
  73. fonts[i] = G_store(buf);
  74. }
  75. else
  76. fonts[i] = G_store(p->name);
  77. }
  78. *list = fonts;
  79. *count = num_fonts;
  80. }
  81. void COM_Font_list(char ***list, int *count)
  82. {
  83. if (driver->Font_list) {
  84. (*driver->Font_list)(list, count);
  85. return;
  86. }
  87. font_list(list, count, 0);
  88. }
  89. void COM_Font_info(char ***list, int *count)
  90. {
  91. if (driver->Font_info) {
  92. (*driver->Font_info)(list, count);
  93. return;
  94. }
  95. font_list(list, count, 1);
  96. }
  97. void free_font_list(char **fonts, int count)
  98. {
  99. int i;
  100. for (i = 0; i < count; i++)
  101. G_free(fonts[i]);
  102. G_free(fonts);
  103. }