/src/ghostpdl/base/gsgdata.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* Support for glyph data access */ |
18 | | |
19 | | #include "memory_.h" |
20 | | #include "gx.h" |
21 | | #include "gsgdata.h" |
22 | | #include "gserrors.h" |
23 | | #include "gsmatrix.h" /* for gsfont.h */ |
24 | | #include "gsstruct.h" |
25 | | #include "gxfont.h" |
26 | | |
27 | | /* GC structure descriptor */ |
28 | 0 | static ENUM_PTRS_WITH(gs_glyph_data_enum_ptrs, gs_glyph_data_t *pgd) |
29 | 0 | case 0: return ENUM_CONST_BYTESTRING(&pgd->bits); |
30 | 0 | case 1: return ENUM_OBJ(pgd->proc_data); |
31 | 0 | ENUM_PTRS_END |
32 | 0 | static RELOC_PTRS_WITH(gs_glyph_data_reloc_ptrs, gs_glyph_data_t *pgd) |
33 | 0 | { |
34 | 0 | RELOC_CONST_BYTESTRING_VAR(pgd->bits); |
35 | 0 | RELOC_OBJ_VAR(pgd->proc_data); |
36 | 0 | } RELOC_PTRS_END |
37 | | gs_public_st_composite(st_glyph_data, gs_glyph_data_t, "gs_glyph_data_t", |
38 | | gs_glyph_data_enum_ptrs, gs_glyph_data_reloc_ptrs); |
39 | | |
40 | | /* ------ Client facilities ------ */ |
41 | | |
42 | | /* Replace glyph data by a substring. */ |
43 | | int |
44 | | gs_glyph_data_substring(gs_glyph_data_t *pgd, uint offset, uint size) |
45 | 0 | { |
46 | 0 | if (offset > pgd->bits.size || size > pgd->bits.size - offset) |
47 | 0 | return_error(gs_error_rangecheck); |
48 | 0 | return pgd->procs->substring(pgd, offset, size); |
49 | 0 | } |
50 | | |
51 | | /* Free the data for a glyph. */ |
52 | | void |
53 | | gs_glyph_data_free(gs_glyph_data_t *pgd, client_name_t cname) |
54 | 173M | { |
55 | 173M | if (pgd != 0) { |
56 | 173M | if (pgd->procs != 0) |
57 | 173M | pgd->procs->free(pgd, cname); |
58 | 173M | gs_glyph_data_from_null(pgd); |
59 | 173M | } |
60 | 173M | } |
61 | | |
62 | | /* ------ Implementor support ------ */ |
63 | | |
64 | | /* Don't manage the glyph data. */ |
65 | | static void |
66 | | glyph_data_free_permanent(gs_glyph_data_t *pgd, client_name_t cname) |
67 | 173M | { |
68 | 173M | } |
69 | | static int |
70 | | glyph_data_substring_permanent(gs_glyph_data_t *pgd, uint offset, uint size) |
71 | 0 | { |
72 | 0 | pgd->bits.data += offset; |
73 | 0 | pgd->bits.size = size; |
74 | 0 | return 0; |
75 | 0 | } |
76 | | |
77 | | /* Manage the glyph data using the font's allocator. */ |
78 | | static void |
79 | | glyph_data_free_by_font(gs_glyph_data_t *pgd, client_name_t cname) |
80 | 0 | { |
81 | 0 | gs_free_const_bytestring(((gs_font *)pgd->proc_data)->memory, |
82 | 0 | &pgd->bits, cname); |
83 | 0 | } |
84 | | static int |
85 | | glyph_data_substring_by_font(gs_glyph_data_t *pgd, uint offset, uint size) |
86 | 0 | { |
87 | 0 | gs_font *const font = pgd->proc_data; |
88 | 0 | byte *data = (byte *)pgd->bits.data; /* break const */ |
89 | |
|
90 | 0 | if (pgd->bits.bytes) /* object, not string */ |
91 | 0 | return glyph_data_substring_permanent(pgd, offset, size); |
92 | 0 | if (offset > 0) |
93 | 0 | memmove(data, data + offset, size); |
94 | 0 | pgd->bits.data = |
95 | 0 | gs_resize_string(font->memory, data, pgd->bits.size, size, |
96 | 0 | "glyph_data_substring"); /* shortening, can't fail */ |
97 | 0 | pgd->bits.size = size; |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | static const gs_glyph_data_procs_t no_free_procs = { |
102 | | glyph_data_free_permanent, glyph_data_substring_permanent |
103 | | }; |
104 | | static const gs_glyph_data_procs_t free_by_font_procs = { |
105 | | glyph_data_free_by_font, glyph_data_substring_by_font |
106 | | }; |
107 | | |
108 | | /* |
109 | | * Initialize glyph data from a string or from bytes. If the font is NULL |
110 | | * (for glyph data that is part of the font), set the glyph data freeing |
111 | | * procedure to "do not free"; if the font is not NULL (for just-allocated |
112 | | * glyph data), set the freeing procedure to "free using the font's |
113 | | * allocator." |
114 | | */ |
115 | | void |
116 | | gs_glyph_data_from_string(gs_glyph_data_t *pgd, const byte *data, |
117 | | uint size, gs_font *font) |
118 | 256M | { |
119 | 256M | gs_bytestring_from_string(&pgd->bits, data, size); |
120 | 256M | pgd->proc_data = font; |
121 | 256M | pgd->procs = (font ? &free_by_font_procs : &no_free_procs); |
122 | 256M | } |
123 | | void |
124 | | gs_glyph_data_from_bytes(gs_glyph_data_t *pgd, const byte *bytes, |
125 | | uint offset, uint size, gs_font *font) |
126 | 129M | { |
127 | 129M | gs_bytestring_from_bytes(&pgd->bits, bytes, offset, size); |
128 | 129M | pgd->proc_data = font; |
129 | 129M | pgd->procs = (font ? &free_by_font_procs : &no_free_procs); |
130 | 129M | } |
131 | | void |
132 | | gs_glyph_data_from_null(gs_glyph_data_t *pgd) |
133 | 216M | { |
134 | 216M | gs_glyph_data_from_string(pgd, NULL, 0, NULL); |
135 | 216M | } |