gsgcache.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (C) 1996, 2000 Aladdin Enterprises. 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: gsgcache.c,v 1.4 2005/06/21 16:25:50 igor Exp $ */
  14. /* Glyph data cache methods. */
  15. #include "gx.h"
  16. #include "gserrors.h"
  17. #include "memory_.h"
  18. #include "gsstruct.h"
  19. #include "gsgdata.h"
  20. #include "gsgcache.h"
  21. #include "gxfont.h"
  22. #include "gxfont42.h"
  23. /*
  24. * This implementation hardcodes the type 42 font type.
  25. * We could generalize it, but since CIDFontType 0 uses
  26. * a PS procedure for reading glyphs, it is hardly applicable.
  27. *
  28. * The caching is mostly useful for glyphs with multiple components,
  29. * but CIDFontType 0 has 2 components max, which are relatively seldom.
  30. * Also it is low useful for fonts, which fully loaded into RAM.
  31. * FAPI does not need a caching, because renderer pludins access
  32. * font data through a file handle by own means.
  33. *
  34. * Due to all above, currently the caching is applied
  35. * only while emulating CIDFontType 2 with a True Type file.
  36. */
  37. typedef struct gs_glyph_cache_elem_s gs_glyph_cache_elem;
  38. struct gs_glyph_cache_elem_s {
  39. gs_glyph_data_t gd;
  40. uint glyph_index;
  41. uint lock_count;
  42. gs_glyph_cache_elem *next;
  43. };
  44. gs_public_st_composite(st_glyph_cache_elem, gs_glyph_cache_elem, "gs_glyph_cache_elem",
  45. gs_glyph_cache_elem_enum_ptrs, gs_glyph_cache_elem_reloc_ptrs);
  46. private
  47. ENUM_PTRS_WITH(gs_glyph_cache_elem_enum_ptrs, gs_glyph_cache_elem *e)
  48. {
  49. index --;
  50. if (index < ST_GLYPH_DATA_NUM_PTRS)
  51. return ENUM_USING(st_glyph_data, &e->gd, sizeof(e->gd), index);
  52. return 0;
  53. }
  54. ENUM_PTR(0, gs_glyph_cache_elem, next);
  55. ENUM_PTRS_END
  56. private RELOC_PTRS_WITH(gs_glyph_cache_elem_reloc_ptrs, gs_glyph_cache_elem *e)
  57. {
  58. RELOC_PTR(gs_glyph_cache_elem, next);
  59. RELOC_USING(st_glyph_data, &e->gd, sizeof(e->gd));
  60. } RELOC_PTRS_END
  61. struct gs_glyph_cache_s {
  62. int total_size;
  63. gs_glyph_cache_elem *list;
  64. gs_memory_t *memory;
  65. gs_font_type42 *pfont;
  66. stream *s;
  67. get_glyph_data_from_file read_data;
  68. };
  69. gs_private_st_ptrs4(st_glyph_cache, gs_glyph_cache, "gs_glyph_cache",
  70. gs_glyph_cache_enum_ptrs, gs_glyph_cache_reloc_ptrs, list, memory, pfont, s);
  71. GS_NOTIFY_PROC(gs_glpyh_cache__release);
  72. gs_glyph_cache *
  73. gs_glyph_cache__alloc(gs_font_type42 *pfont, stream *s,
  74. get_glyph_data_from_file read_data)
  75. {
  76. gs_memory_t *mem = pfont->memory->stable_memory;
  77. gs_glyph_cache *gdcache = (gs_glyph_cache *)gs_alloc_struct(mem,
  78. gs_glyph_cache, &st_glyph_cache, "gs_glyph_cache");
  79. if (gdcache == 0)
  80. return 0;
  81. gdcache->total_size = 0;
  82. gdcache->list = NULL;
  83. gdcache->pfont = pfont;
  84. gdcache->s = s;
  85. /*
  86. * The cache elements need to be in stable memory so they don't
  87. * get removed by 'restore' (elements can be created at a different
  88. * save level than the current level)
  89. */
  90. gdcache->memory = mem;
  91. gdcache->read_data = read_data;
  92. gs_font_notify_register((gs_font *)pfont, gs_glyph_cache__release, (void *)gdcache);
  93. return gdcache;
  94. }
  95. int
  96. gs_glyph_cache__release(void *data, void *event)
  97. {
  98. gs_glyph_cache *this = (gs_glyph_cache *)data;
  99. gs_glyph_cache_elem *e = this->list;
  100. gs_font_type42 *pfont = this->pfont;
  101. while (e != NULL) {
  102. gs_glyph_cache_elem *next_e;
  103. next_e = e->next;
  104. e->gd.procs->free(&e->gd, "gs_glyph_cache__release");
  105. gs_free_object(this->memory, e, "gs_glyph_cache_elem__release");
  106. e = next_e;
  107. }
  108. this->list = NULL;
  109. gs_font_notify_unregister((gs_font *)pfont, gs_glyph_cache__release, (void *)this);
  110. gs_free_object(this->memory, this, "gs_glyph_cache__release");
  111. return 0;
  112. }
  113. private gs_glyph_cache_elem **
  114. gs_glyph_cache_elem__locate(gs_glyph_cache *this, uint glyph_index)
  115. { /* If not fond, returns an unlocked element. */
  116. gs_glyph_cache_elem **e = &this->list, **p_unlocked = NULL;
  117. int count = 0; /* debug purpose only */
  118. for (; *e != 0; e = &(*e)->next, count++) {
  119. if ((*e)->glyph_index == glyph_index) {
  120. return e;
  121. }
  122. if ((*e)->lock_count == 0)
  123. p_unlocked = e;
  124. }
  125. return p_unlocked;
  126. }
  127. private inline void
  128. gs_glyph_cache_elem__move_to_head(gs_glyph_cache *this, gs_glyph_cache_elem **pe)
  129. { gs_glyph_cache_elem *e = *pe;
  130. *pe = e->next;
  131. e->next = this->list;
  132. this->list = e;
  133. }
  134. /* Manage the glyph data using the font's allocator. */
  135. private void
  136. gs_glyph_cache_elem__free_data(gs_glyph_data_t *pgd, client_name_t cname)
  137. { gs_glyph_cache_elem *e = (gs_glyph_cache_elem *)pgd->proc_data;
  138. e->lock_count--;
  139. }
  140. private int
  141. gs_glyph_cache_elem__substring(gs_glyph_data_t *pgd, uint offset, uint size)
  142. { gs_glyph_cache_elem *e = (gs_glyph_cache_elem *)pgd->proc_data;
  143. e->lock_count++;
  144. return_error(gs_error_unregistered); /* Unsupported; should not happen. */
  145. }
  146. private const gs_glyph_data_procs_t gs_glyph_cache_elem_procs = {
  147. gs_glyph_cache_elem__free_data, gs_glyph_cache_elem__substring
  148. };
  149. int
  150. gs_get_glyph_data_cached(gs_font_type42 *pfont, uint glyph_index, gs_glyph_data_t *pgd)
  151. { gs_glyph_cache *gdcache = pfont->data.gdcache;
  152. gs_glyph_cache_elem **pe = gs_glyph_cache_elem__locate(gdcache, glyph_index);
  153. gs_glyph_cache_elem *e = NULL;
  154. if (pe == NULL || (*pe)->glyph_index != glyph_index) {
  155. int code;
  156. if (pe != NULL && gdcache->total_size > 32767 /* arbitrary */ &&
  157. (*pe)->lock_count <= 0) {
  158. /* Release the element's data, and move it : */
  159. e = *pe;
  160. gdcache->total_size -= e->gd.bits.size + sizeof(*e);
  161. e->gd.procs->free(&e->gd, "gs_get_glyph_data_cached");
  162. gs_glyph_cache_elem__move_to_head(gdcache, pe);
  163. } else {
  164. /* Allocate new head element. */
  165. e = (gs_glyph_cache_elem *)gs_alloc_struct(gdcache->memory,
  166. gs_glyph_cache_elem, &st_glyph_cache_elem, "gs_glyph_cache_elem");
  167. if (e == NULL)
  168. return_error(gs_error_VMerror);
  169. memset(e, 0, sizeof(*e));
  170. e->next = gdcache->list;
  171. gdcache->list = e;
  172. e->gd.memory = gdcache->memory;
  173. }
  174. /* Load the element's data : */
  175. code = (*gdcache->read_data)(pfont, gdcache->s, glyph_index, &e->gd);
  176. if (code < 0)
  177. return code;
  178. gdcache->total_size += e->gd.bits.size + sizeof(*e);
  179. e->glyph_index = glyph_index;
  180. } else {
  181. /* Move the element : */
  182. e = *pe;
  183. gs_glyph_cache_elem__move_to_head(gdcache, pe);
  184. }
  185. /* Copy data and set procs : */
  186. pgd->bits = e->gd.bits;
  187. pgd->proc_data = e;
  188. pgd->procs = &gs_glyph_cache_elem_procs;
  189. e->lock_count++;
  190. return 0;
  191. }