AmendHub

Download

jcs

/

textview

/

tv_store.c

 

(View History)

jcs   TVFindStyle: Avoid GetFontInfo for styles with same font Latest amendment: 93 on 2026-11-05

1 /*
2 * Copyright (c) 2023-2024 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdio.h>
18 #include <stddef.h>
19 #include <string.h>
20 #include "textview.h"
21
22 Handle
23 TVAllocateSlab(struct TVRec *tv, unsigned long size, Handle *first,
24 Handle *last)
25 {
26 Handle slabh;
27 struct TVSlab *slab, *lslab;
28 unsigned short id;
29
30 if (last == NULL || *last == NULL)
31 id = 1;
32 else {
33 HLock(*last);
34 slab = (struct TVSlab *)**last;
35 if (slab->id == USHRT_MAX) {
36 Debugger();
37 return NULL;
38 }
39 id = slab->id + 1;
40 }
41
42 slabh = NewHandleClear(sizeof(struct TVSlab) + size);
43 if (slabh == NULL)
44 /* TODO: try smaller allocations */
45 return NULL;
46
47 HLock(slabh);
48 slab = (struct TVSlab *)*slabh;
49 slab->id = id;
50 slab->size = size;
51
52 if (last != NULL && *last != NULL) {
53 lslab = (struct TVSlab *)**last;
54 lslab->next_slab = slabh;
55 slab->prev_slab = *last;
56 slab->base = lslab->base + lslab->size;
57 }
58
59 if (first != NULL && *first == NULL)
60 *first = slabh;
61 if (last != NULL)
62 *last = slabh;
63
64 /* leave locked */
65
66 return slabh;
67 }
68
69 Handle
70 TVAllocateLineGroup(struct TVRec *tv, short groupn)
71 {
72 if (groupn >= sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))
73 return NULL;
74
75 if (tv->line_groups[groupn] == NULL)
76 tv->line_groups[groupn] = NewHandleClear(TV_LINE_GROUP_SIZE);
77
78 return tv->line_groups[groupn];
79 }
80
81 struct TVSlab *
82 TVGetTextSlab(struct TVRec *tv, unsigned long addr)
83 {
84 Handle slabh;
85 struct TVSlab *slab;
86
87 slabh = tv->text_slab_hint;
88 if (slabh) {
89 HLock(slabh);
90 slab = (struct TVSlab *)*slabh;
91 if (addr >= slab->base && addr < slab->base + slab->size)
92 /* leave locked */
93 return slab;
94 }
95
96 slabh = tv->first_text_slab;
97 while (slabh) {
98 HLock(slabh);
99 slab = (struct TVSlab *)*slabh;
100 if (addr >= slab->base && addr < slab->base + slab->size) {
101 /* leave locked */
102 tv->text_slab_hint = slabh;
103 return slab;
104 }
105
106 slabh = slab->next_slab;
107 }
108
109 return NULL;
110 }
111
112 unsigned char
113 TVCharAt(struct TVRec *tv, unsigned long off)
114 {
115 struct TVSlab *slab;
116
117 if (off >= tv->text_length)
118 return 0;
119
120 slab = TVGetTextSlab(tv, off);
121 if (slab == NULL)
122 return 0;
123
124 return slab->data[off - slab->base];
125 }
126
127 struct TVChunk *
128 TVGetChunk(struct TVRec *tv, TVSlabLocator loc)
129 {
130 Handle slabh;
131 struct TVSlab *slab;
132 char *addr;
133
134 if (loc == 0)
135 return NULL;
136
137 slabh = tv->chunk_slab_hint;
138 if (slabh) {
139 HLock(slabh);
140 slab = (struct TVSlab *)*slabh;
141 if (slab->id != SLABLOC_SLABID(loc))
142 slabh = NULL;
143 }
144 if (slabh == NULL) {
145 slabh = tv->first_chunk_slab;
146 while (slabh) {
147 HLock(slabh);
148 slab = (struct TVSlab *)*slabh;
149 if (slab->id == SLABLOC_SLABID(loc))
150 break;
151 slabh = slab->next_slab;
152 }
153 if (slabh == NULL)
154 return NULL;
155 tv->chunk_slab_hint = slabh;
156 }
157
158 addr = slab->data + SLABLOC_OFFSET(loc);
159 if ((unsigned long)addr % 2 != 0) {
160 Debugger();
161 return NULL;
162 }
163
164 /* keep slab locked */
165
166 return (struct TVChunk *)addr;
167 }
168
169 struct TVChunk *
170 TVCreateChunk(struct TVRec *tv)
171 {
172 Handle slabh;
173 struct TVSlab *slab;
174 struct TVChunk *chunk;
175 TVSlabLocator cloc;
176
177 slabh = tv->last_chunk_slab;
178 HLock(slabh);
179 slab = (struct TVSlab *)*slabh;
180
181 if (slab->size - slab->pos < sizeof(struct TVChunk)) {
182 if ((slabh = TVAllocateSlab(tv, TV_CHUNK_SLAB_SIZE,
183 &tv->first_chunk_slab, &tv->last_chunk_slab)) == NULL)
184 return NULL;
185 slab = (struct TVSlab *)*slabh;
186 }
187
188 SET_SLABLOC_SLABID(cloc, slab->id);
189 SET_SLABLOC_OFFSET(cloc, slab->pos);
190
191 chunk = TVGetChunk(tv, cloc);
192 chunk->loc = cloc;
193 slab->pos += sizeof(struct TVChunk);
194
195 if (!tv->first_chunk)
196 tv->first_chunk = cloc;
197
198 tv->last_chunk = cloc;
199
200 return chunk;
201 }
202
203 struct TVLine *
204 TVGetLine(struct TVRec *tv, unsigned long line)
205 {
206 Handle group;
207 unsigned short groupn, idx;
208
209 if (line == 0)
210 return NULL;
211
212 groupn = (line - 1) / TV_LINE_GROUP_NLINES;
213 if (groupn >= (sizeof(tv->line_groups) / sizeof(tv->line_groups[0])))
214 return NULL;
215
216 idx = line - 1 - (groupn * TV_LINE_GROUP_NLINES);
217
218 group = tv->line_groups[groupn];
219 if (group == NULL && (group = TVAllocateLineGroup(tv, groupn)) == NULL)
220 return NULL;
221
222 HLock(group);
223 /* keep group locked */
224
225 return (struct TVLine *)(*group + (sizeof(struct TVLine) * idx));
226 }
227
228 void
229 TVApplyStyle(struct TVStyle *style)
230 {
231 if (thePort->txFont == style->font && thePort->txFace == style->style &&
232 thePort->txSize == style->size)
233 return;
234
235 TextFont(style->font);
236 TextFace(style->style);
237 TextSize(style->size);
238 }
239
240 struct TVStyle *
241 TVGetStyle(struct TVRec *tv, TVSlabLocator loc)
242 {
243 Handle slabh;
244 struct TVSlab *slab;
245 struct TVStyle *style;
246 short n;
247
248 slabh = tv->style_slab_hint;
249 if (slabh) {
250 HLock(slabh);
251 slab = (struct TVSlab *)*slabh;
252 if (slab->id != SLABLOC_SLABID(loc))
253 slabh = NULL;
254 }
255 if (slabh == NULL)
256 slabh = tv->first_style_slab;
257
258 while (slabh) {
259 HLock(slabh);
260 slab = (struct TVSlab *)*slabh;
261
262 if (slab->id != SLABLOC_SLABID(loc)) {
263 slabh = slab->next_slab;
264 continue;
265 }
266
267 style = (struct TVStyle *)(slab->data + SLABLOC_OFFSET(loc));
268 if (style->loc != loc) {
269 /* locator pointed at bogus style */
270 Debugger();
271 return NULL;
272 }
273 tv->style_slab_hint = slabh;
274 return style;
275 }
276
277 return NULL;
278 }
279
280 struct TVStyle *
281 TVFindStyle(struct TVRec *tv, struct TVStyle *style)
282 {
283 Handle slabh;
284 struct TVSlab *slab;
285 struct TVStyle *tstyle, *same_font;
286 unsigned long size;
287
288 if (style->loc)
289 return TVGetStyle(tv, style->loc);
290
291 same_font = NULL;
292 slabh = tv->first_style_slab;
293 while (slabh) {
294 HLock(slabh);
295 slab = (struct TVSlab *)*slabh;
296 tstyle = (struct TVStyle *)(slab->data);
297
298 iterate_slab:
299 if (tstyle->loc == 0)
300 goto next_slab;
301
302 if (!ARE_STYLES_EQUAL(tstyle, style)) {
303 /* but if it's the same in basic font metrics, use it */
304 if (same_font == NULL && !tstyle->bitmap_data_size &&
305 tstyle->font == style->font &&
306 tstyle->style == style->style &&
307 tstyle->size == style->size)
308 same_font = tstyle;
309 goto next_style;
310 }
311
312 if (tstyle->bitmap_data_size &&
313 memcmp(tstyle->bitmap_data, style->bitmap.baseAddr,
314 tstyle->bitmap_data_size) != 0)
315 goto next_style;
316
317 TVApplyStyle(tstyle);
318 tv->cur_style = tstyle->loc;
319 return tstyle;
320
321 next_style:
322 if (SLABLOC_OFFSET(tstyle->loc) + sizeof(struct TVStyle) +
323 tstyle->bitmap_data_size + sizeof(struct TVStyle) > slab->size)
324 goto next_slab;
325
326 tstyle = (struct TVStyle *)((char *)tstyle +
327 sizeof(struct TVStyle) + tstyle->bitmap_data_size);
328 if (tstyle->loc == 0) {
329 if (slab->next_slab)
330 goto next_slab;
331 goto create_style;
332 }
333 goto iterate_slab;
334
335 next_slab:
336 slabh = slab->next_slab;
337 }
338
339 /* no matching style found, create a new one */
340 create_style:
341 slabh = tv->last_style_slab;
342 HLock(slabh);
343 slab = (struct TVSlab *)*slabh;
344
345 if (slab->pos + sizeof(struct TVStyle) + style->bitmap_data_size >
346 slab->size) {
347 /* need a new slab */
348 size = TV_STYLE_SLAB_SIZE;
349 if (sizeof(struct TVStyle) + style->bitmap_data_size > size)
350 size = sizeof(struct TVStyle) + style->bitmap_data_size;
351 slabh = TVAllocateSlab(tv, size, &tv->first_style_slab,
352 &tv->last_style_slab);
353 if (slabh == NULL)
354 return NULL;
355 slab = (struct TVSlab *)*slabh;
356 }
357
358 tstyle = (struct TVStyle *)(slab->data + slab->pos);
359 SET_SLABLOC_SLABID(tstyle->loc, slab->id);
360 SET_SLABLOC_OFFSET(tstyle->loc, slab->pos);
361
362 /* only copy over the important fields, rest will stay 0 */
363 /* NOTE: keep this in sync with ARE_STYLES_EQUAL */
364 tstyle->font = style->font;
365 tstyle->style = style->style;
366 tstyle->size = style->size;
367 tstyle->align = style->align;
368 tstyle->tag = style->tag;
369 if (style->bitmap_data_size) {
370 tstyle->font_info = style->font_info;
371 tstyle->bitmap_data_size = style->bitmap_data_size;
372 tstyle->bitmap = style->bitmap;
373 memcpy(tstyle->bitmap_data, style->bitmap.baseAddr,
374 style->bitmap_data_size);
375 /* this must be recalculated at draw time once slab is locked */
376 tstyle->bitmap.baseAddr = 0;
377 }
378
379 slab->pos += sizeof(struct TVStyle) + tstyle->bitmap_data_size;
380 if (slab->pos > slab->size) {
381 Debugger();
382 return NULL;
383 }
384
385 TVApplyStyle(tstyle);
386 if (!style->bitmap_data_size && same_font != NULL) {
387 tstyle->font_info = same_font->font_info;
388 memcpy(tstyle->width_cache, same_font->width_cache,
389 sizeof(tstyle->width_cache));
390 } else if (!style->bitmap_data_size || style->font_info.widMax == 0)
391 GetFontInfo(&tstyle->font_info);
392 tv->cur_style = tstyle->loc;
393
394 return tstyle;
395 }