2
0

test_fonts_alias.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * (c) Copyright 1993-2012 The Open Group
  5. * (c) Copyright 2012-2022 CDE Project contributors, see
  6. * CONTRIBUTORS for details
  7. *
  8. * These libraries and programs are free software; you can
  9. * redistribute them and/or modify them under the terms of the GNU
  10. * Lesser General Public License as published by the Free Software
  11. * Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. * These libraries and programs are distributed in the hope that
  15. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  16. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  17. * PURPOSE. See the GNU Lesser General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with these libraries and programs; if not, write
  22. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  23. * Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <X11/Xlib.h>
  29. #define BUF_SIZE 1024
  30. int main(void) {
  31. int ret = 0;
  32. int line_num = 0;
  33. int npaths;
  34. char cwd[BUF_SIZE];
  35. char *font_path_list[BUF_SIZE];
  36. char **font_path_list_orig = NULL;
  37. char *line = NULL;
  38. size_t len = 0;
  39. ssize_t line_len;
  40. if (!getcwd(cwd, BUF_SIZE)) {
  41. fprintf(stderr, "Cannot get current working directory.\n");
  42. ret = -1;
  43. goto done;
  44. }
  45. font_path_list[0] = cwd;
  46. Display *display = XOpenDisplay(NULL);
  47. if (display == NULL) {
  48. fprintf(stderr, "Cannot open display.\n");
  49. ret = -1;
  50. goto done;
  51. }
  52. font_path_list_orig = XGetFontPath(display, &npaths);
  53. for (int i = 0; i < npaths; ++i)
  54. font_path_list[i + 1] = font_path_list_orig[i];
  55. XSetFontPath(display, font_path_list, ++npaths);
  56. while ((line_len = getline(&line, &len, stdin)) != -1) {
  57. int i;
  58. char *font_name = NULL;
  59. size_t font_name_len = 0;
  60. ++line_num;
  61. for (i = 0; i < line_len; ++i) {
  62. ++font_name_len;
  63. if (!font_name && line[i] == '"') {
  64. font_name = &line[i];
  65. font_name_len = 0;
  66. }
  67. else if (font_name && line[i] == '"') break;
  68. }
  69. if (i >= line_len || line_len < 3) {
  70. fprintf(stderr, "Corrupted file.\n");
  71. ret = -1;
  72. goto done;
  73. }
  74. *(++font_name + --font_name_len) = '\0';
  75. XFontStruct *font_struct = XLoadQueryFont(display, font_name);
  76. if (!font_struct) {
  77. fprintf(stderr, "Bad alias: %d: %s\n", line_num, font_name);
  78. ret = -1;
  79. }
  80. if (font_struct) XFreeFont(display, font_struct);
  81. }
  82. done:
  83. if (font_path_list_orig) {
  84. XSetFontPath(display, font_path_list_orig, --npaths);
  85. XFreeFontPath(font_path_list_orig);
  86. }
  87. if (line) free(line);
  88. if (display) XCloseDisplay(display);
  89. return ret;
  90. }