gsgdata.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (C) 2001 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: gsgdata.c,v 1.3 2002/02/21 22:24:52 giles Exp $ */
  14. /* Support for glyph data access */
  15. #include "memory_.h"
  16. #include "gx.h"
  17. #include "gsgdata.h"
  18. #include "gserrors.h"
  19. #include "gsmatrix.h" /* for gsfont.h */
  20. #include "gsstruct.h"
  21. #include "gxfont.h"
  22. /* GC structure descriptor */
  23. private ENUM_PTRS_WITH(gs_glyph_data_enum_ptrs, gs_glyph_data_t *pgd)
  24. case 0: return ENUM_CONST_BYTESTRING(&pgd->bits);
  25. case 1: return ENUM_OBJ(pgd->proc_data);
  26. ENUM_PTRS_END
  27. private RELOC_PTRS_WITH(gs_glyph_data_reloc_ptrs, gs_glyph_data_t *pgd)
  28. {
  29. RELOC_CONST_BYTESTRING_VAR(pgd->bits);
  30. RELOC_OBJ_VAR(pgd->proc_data);
  31. } RELOC_PTRS_END
  32. gs_public_st_composite(st_glyph_data, gs_glyph_data_t, "gs_glyph_data_t",
  33. gs_glyph_data_enum_ptrs, gs_glyph_data_reloc_ptrs);
  34. /* ------ Client facilities ------ */
  35. /* Replace glyph data by a substring. */
  36. int
  37. gs_glyph_data_substring(gs_glyph_data_t *pgd, uint offset, uint size)
  38. {
  39. if (offset > pgd->bits.size || size > pgd->bits.size - offset)
  40. return_error(gs_error_rangecheck);
  41. return pgd->procs->substring(pgd, offset, size);
  42. }
  43. /* Free the data for a glyph. */
  44. void
  45. gs_glyph_data_free(gs_glyph_data_t *pgd, client_name_t cname)
  46. {
  47. pgd->procs->free(pgd, cname);
  48. gs_glyph_data_from_null(pgd);
  49. }
  50. /* ------ Implementor support ------ */
  51. /* Don't manage the glyph data. */
  52. private void
  53. glyph_data_free_permanent(gs_glyph_data_t *pgd, client_name_t cname)
  54. {
  55. }
  56. private int
  57. glyph_data_substring_permanent(gs_glyph_data_t *pgd, uint offset, uint size)
  58. {
  59. pgd->bits.data += offset;
  60. pgd->bits.size = size;
  61. return 0;
  62. }
  63. /* Manage the glyph data using the font's allocator. */
  64. private void
  65. glyph_data_free_by_font(gs_glyph_data_t *pgd, client_name_t cname)
  66. {
  67. gs_free_const_bytestring(((gs_font *)pgd->proc_data)->memory,
  68. &pgd->bits, cname);
  69. }
  70. private int
  71. glyph_data_substring_by_font(gs_glyph_data_t *pgd, uint offset, uint size)
  72. {
  73. gs_font *const font = pgd->proc_data;
  74. byte *data = (byte *)pgd->bits.data; /* break const */
  75. if (pgd->bits.bytes) /* object, not string */
  76. return glyph_data_substring_permanent(pgd, offset, size);
  77. if (offset > 0)
  78. memmove(data, data + offset, size);
  79. pgd->bits.data =
  80. gs_resize_string(font->memory, data, pgd->bits.size, size,
  81. "glyph_data_substring"); /* shortening, can't fail */
  82. pgd->bits.size = size;
  83. return 0;
  84. }
  85. private const gs_glyph_data_procs_t no_free_procs = {
  86. glyph_data_free_permanent, glyph_data_substring_permanent
  87. };
  88. private const gs_glyph_data_procs_t free_by_font_procs = {
  89. glyph_data_free_by_font, glyph_data_substring_by_font
  90. };
  91. /*
  92. * Initialize glyph data from a string or from bytes. If the font is NULL
  93. * (for glyph data that is part of the font), set the glyph data freeing
  94. * procedure to "do not free"; if the font is not NULL (for just-allocated
  95. * glyph data), set the freeing procedure to "free using the font's
  96. * allocator."
  97. */
  98. void
  99. gs_glyph_data_from_string(gs_glyph_data_t *pgd, const byte *data,
  100. uint size, gs_font *font)
  101. {
  102. gs_bytestring_from_string(&pgd->bits, data, size);
  103. pgd->proc_data = font;
  104. pgd->procs = (font ? &free_by_font_procs : &no_free_procs);
  105. }
  106. void
  107. gs_glyph_data_from_bytes(gs_glyph_data_t *pgd, const byte *bytes,
  108. uint offset, uint size, gs_font *font)
  109. {
  110. gs_bytestring_from_bytes(&pgd->bits, bytes, offset, size);
  111. pgd->proc_data = font;
  112. pgd->procs = (font ? &free_by_font_procs : &no_free_procs);
  113. }
  114. void
  115. gs_glyph_data_from_null(gs_glyph_data_t *pgd)
  116. {
  117. gs_glyph_data_from_string(pgd, NULL, 0, NULL);
  118. }