zfontenum.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2003 artofcode LLC. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: zfontenum.c,v 1.4 2004/08/04 19:36:13 stefan Exp $ */
  14. /* this is the ps interpreter interface to the native font
  15. enumeration code. it calls the platform-specific routines
  16. to obtain an additional set of entries that can be added
  17. to the Fontmap to reference fonts stored on the system.
  18. */
  19. #include "memory_.h"
  20. #include "string_.h"
  21. #include <stdlib.h>
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"
  25. #include "gsmalloc.h"
  26. #include "ialloc.h"
  27. #include "iname.h"
  28. #include "iutil.h"
  29. #include "store.h"
  30. #include "gp.h"
  31. typedef struct fontenum_s {
  32. char *fontname, *path;
  33. struct fontenum_s *next;
  34. } fontenum_t;
  35. /* .getnativefonts [ [<name> <path>] ... ] */
  36. private int
  37. z_fontenum(i_ctx_t *i_ctx_p)
  38. {
  39. os_ptr op = osp;
  40. void *enum_state;
  41. int code = 0;
  42. int e,elements;
  43. char *fontname, *path;
  44. fontenum_t *r, *results;
  45. ref array;
  46. uint length;
  47. byte *string;
  48. enum_state = gp_enumerate_fonts_init(imemory);
  49. if (enum_state == NULL) {
  50. /* put false on the stack and return */
  51. push(1);
  52. make_bool(op, false);
  53. return code;
  54. }
  55. r = results = gs_malloc(imemory->non_gc_memory, 1, sizeof(fontenum_t), "fontenum list");
  56. elements = 0;
  57. while((code = gp_enumerate_fonts_next(enum_state, &fontname, &path )) > 0) {
  58. if (fontname == NULL || path == NULL) {
  59. gp_enumerate_fonts_free(enum_state);
  60. return_error(e_ioerror);
  61. }
  62. length = strlen(fontname) + 1;
  63. r->fontname = gs_malloc(imemory->non_gc_memory, length, 1, "native font name");
  64. memcpy(r->fontname, fontname, length);
  65. length = strlen(path) + 1;
  66. r->path = gs_malloc(imemory->non_gc_memory, length, 1, "native font path");
  67. memcpy(r->path, path, length);
  68. r->next = gs_malloc(imemory->non_gc_memory, 1, sizeof(fontenum_t), "fontenum list");
  69. r = r->next;
  70. elements += 1;
  71. }
  72. gp_enumerate_fonts_free(enum_state);
  73. code = ialloc_ref_array(&array, a_all | icurrent_space, elements, "native fontmap");
  74. r = results;
  75. for (e = 0; e < elements; e++) {
  76. ref mapping;
  77. code = ialloc_ref_array(&mapping, a_all | icurrent_space, 2, "native font mapping");
  78. length = strlen(r->fontname);
  79. string = ialloc_string(length, "native font name");
  80. if (string == NULL)
  81. return_error(e_VMerror);
  82. memcpy(string, r->fontname, length);
  83. make_string(&(mapping.value.refs[0]), a_all | icurrent_space, length, string);
  84. length = strlen(r->path);
  85. string = ialloc_string(length, "native font path");
  86. if (string == NULL)
  87. return_error(e_VMerror);
  88. memcpy(string, r->path, length);
  89. make_string(&(mapping.value.refs[1]), a_all | icurrent_space, length, string);
  90. ref_assign(&(array.value.refs[e]), &mapping);
  91. results = r;
  92. r = r->next;
  93. gs_free(imemory->non_gc_memory,
  94. results->fontname, strlen(results->fontname) + 1, 1, "native font name");
  95. gs_free(imemory->non_gc_memory,
  96. results->path, strlen(results->path) + 1, 1, "native font path");
  97. gs_free(imemory->non_gc_memory,
  98. results, 1, sizeof(fontenum_t), "fontenum list");
  99. }
  100. push(2);
  101. ref_assign(op-1, &array);
  102. make_bool(op, true);
  103. return code;
  104. }
  105. /* match the above routines to their postscript filter names
  106. this is how our 'private' routines get called externally */
  107. const op_def zfontenum_op_defs[] = {
  108. {"0.getnativefonts", z_fontenum},
  109. op_def_end(0)
  110. };