font.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. G_debug(2, "font_list: num_fonts=%d", num_fonts);
  42. fonts = G_malloc(num_fonts * sizeof(const char *));
  43. for (i = 0; i < num_fonts; i++) {
  44. struct GFONT_CAP *p = &ftcap[i];
  45. G_debug(4, "font: %d (%s)", i, p->name);
  46. if (verbose) {
  47. char buf[GPATH_MAX];
  48. sprintf(buf, "%s|%s|%d|%s|%d|%s|",
  49. p->name, p->longname, p->type,
  50. p->path, p->index, p->encoding);
  51. fonts[i] = G_store(buf);
  52. }
  53. else
  54. fonts[i] = G_store(p->name);
  55. }
  56. *list = fonts;
  57. *count = num_fonts;
  58. }
  59. static void free_font_list(char **fonts, int count)
  60. {
  61. int i;
  62. for (i = 0; i < count; i++)
  63. G_free(fonts[i]);
  64. G_free(fonts);
  65. }
  66. void COM_Set_font(const char *name)
  67. {
  68. int i;
  69. if (G_is_absolute_path(name)) {
  70. if (font_exists(name))
  71. freetype_set(name, 0);
  72. return;
  73. }
  74. for (i = 0; ftcap[i].name; i++) {
  75. struct GFONT_CAP *cap = &ftcap[i];
  76. if (strcmp(name, cap->name) != 0)
  77. continue;
  78. switch (cap->type) {
  79. case GFONT_FREETYPE:
  80. freetype_set(cap->path, cap->index);
  81. COM_Set_encoding(cap->encoding);
  82. break;
  83. case GFONT_STROKE:
  84. stroke_set(cap->name);
  85. break;
  86. }
  87. return;
  88. }
  89. if (driver->Font_info && driver->Set_font) {
  90. char **list = NULL;
  91. int count = 0;
  92. (*driver->Font_info)(&list, &count);
  93. for (i = 0; i < count; i++) {
  94. struct GFONT_CAP cap;
  95. if (!parse_fontcap_entry(&cap, list[i]))
  96. continue;
  97. if (cap.type != GFONT_DRIVER || strcmp(name, cap.name) != 0)
  98. continue;
  99. driver_set(cap.name);
  100. COM_Set_encoding(cap.encoding);
  101. break;
  102. }
  103. free_font_list(list, count);
  104. return;
  105. }
  106. stroke_set("romans");
  107. }
  108. void COM_Set_encoding(const char *enc)
  109. {
  110. if (encoding)
  111. G_free(encoding);
  112. encoding = G_store(enc);
  113. }
  114. void COM_Font_list(char ***list, int *count)
  115. {
  116. font_list(list, count, 0);
  117. if (driver->Font_list)
  118. (*driver->Font_list)(list, count);
  119. }
  120. void COM_Font_info(char ***list, int *count)
  121. {
  122. font_list(list, count, 1);
  123. if (driver->Font_info)
  124. (*driver->Font_info)(list, count);
  125. }