Font.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include "driver.h"
  5. #include "driverlib.h"
  6. static int font_type = GFONT_STROKE;
  7. static char *encoding;
  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. static void driver_set(const char *name)
  19. {
  20. (*driver->Set_font)(name);
  21. font_type = GFONT_DRIVER;
  22. }
  23. int font_get_type(void)
  24. {
  25. return font_type;
  26. }
  27. const char *font_get_encoding(void)
  28. {
  29. if (!encoding)
  30. encoding = G_store("ISO-8859-1");
  31. return encoding;
  32. }
  33. static void font_list(char ***list, int *count, int verbose)
  34. {
  35. char **fonts;
  36. int num_fonts;
  37. int i;
  38. for (i = 0; ftcap[i].name; i++)
  39. ;
  40. num_fonts = i;
  41. fonts = G_malloc(num_fonts * sizeof(const char *));
  42. for (i = 0; i < num_fonts; i++) {
  43. struct GFONT_CAP *p = &ftcap[i];
  44. if (verbose) {
  45. char buf[GPATH_MAX];
  46. sprintf(buf, "%s|%s|%d|%s|%d|%s|",
  47. p->name, p->longname, p->type,
  48. p->path, p->index, p->encoding);
  49. fonts[i] = G_store(buf);
  50. }
  51. else
  52. fonts[i] = G_store(p->name);
  53. }
  54. *list = fonts;
  55. *count = num_fonts;
  56. }
  57. static void free_font_list(char **fonts, int count)
  58. {
  59. int i;
  60. for (i = 0; i < count; i++)
  61. G_free(fonts[i]);
  62. G_free(fonts);
  63. }
  64. void COM_Set_font(const char *name)
  65. {
  66. int i;
  67. if (G_is_absolute_path(name)) {
  68. if (font_exists(name))
  69. freetype_set(name, 0);
  70. return;
  71. }
  72. for (i = 0; ftcap[i].name; i++) {
  73. struct GFONT_CAP *cap = &ftcap[i];
  74. if (strcmp(name, cap->name) != 0)
  75. continue;
  76. switch (cap->type) {
  77. case GFONT_FREETYPE:
  78. freetype_set(cap->path, cap->index);
  79. COM_Set_encoding(cap->encoding);
  80. break;
  81. case GFONT_STROKE:
  82. stroke_set(cap->name);
  83. break;
  84. }
  85. return;
  86. }
  87. if (driver->Font_info && driver->Set_font) {
  88. char **list = NULL;
  89. int count = 0;
  90. (*driver->Font_info)(&list, &count);
  91. for (i = 0; i < count; i++) {
  92. struct GFONT_CAP cap;
  93. if (!parse_fontcap_entry(&cap, list[i]))
  94. continue;
  95. if (cap.type != GFONT_DRIVER || strcmp(name, cap.name) != 0)
  96. continue;
  97. driver_set(cap.name);
  98. COM_Set_encoding(cap.encoding);
  99. break;
  100. }
  101. free_font_list(list, count);
  102. return;
  103. }
  104. stroke_set("romans");
  105. }
  106. void COM_Set_encoding(const char *enc)
  107. {
  108. if (encoding)
  109. G_free(encoding);
  110. encoding = G_store(enc);
  111. }
  112. void COM_Font_list(char ***list, int *count)
  113. {
  114. font_list(list, count, 0);
  115. if (driver->Font_list)
  116. (*driver->Font_list)(list, count);
  117. }
  118. void COM_Font_info(char ***list, int *count)
  119. {
  120. font_list(list, count, 1);
  121. if (driver->Font_info)
  122. (*driver->Font_info)(list, count);
  123. }