/* * Copyright (c) 2023-2024 joshua stein * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include #include #include #include "textview.h" Handle TVAllocateSlab(struct TVRec *tv, unsigned long size, Handle *first, Handle *last) { Handle slabh; struct TVSlab *slab, *lslab; unsigned short id; if (last == NULL || *last == NULL) id = 1; else { HLock(*last); slab = (struct TVSlab *)**last; if (slab->id == USHRT_MAX) { Debugger(); return NULL; } id = slab->id + 1; } slabh = NewHandleClear(sizeof(struct TVSlab) + size); if (slabh == NULL) /* TODO: try smaller allocations */ return NULL; HLock(slabh); slab = (struct TVSlab *)*slabh; slab->id = id; slab->size = size; if (last != NULL && *last != NULL) { lslab = (struct TVSlab *)**last; lslab->next_slab = slabh; slab->prev_slab = *last; slab->base = lslab->base + lslab->size; } if (first != NULL && *first == NULL) *first = slabh; if (last != NULL) *last = slabh; /* leave locked */ return slabh; } Handle TVAllocateLineGroup(struct TVRec *tv, short groupn) { if (groupn >= sizeof(tv->line_groups) / sizeof(tv->line_groups[0])) return NULL; if (tv->line_groups[groupn] == NULL) tv->line_groups[groupn] = NewHandleClear(TV_LINE_GROUP_SIZE); return tv->line_groups[groupn]; } struct TVSlab * TVGetTextSlab(struct TVRec *tv, unsigned long addr) { Handle slabh; struct TVSlab *slab; slabh = tv->text_slab_hint; if (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; if (addr >= slab->base && addr < slab->base + slab->size) /* leave locked */ return slab; } slabh = tv->first_text_slab; while (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; if (addr >= slab->base && addr < slab->base + slab->size) { /* leave locked */ tv->text_slab_hint = slabh; return slab; } slabh = slab->next_slab; } return NULL; } unsigned char TVCharAt(struct TVRec *tv, unsigned long off) { struct TVSlab *slab; if (off >= tv->text_length) return 0; slab = TVGetTextSlab(tv, off); if (slab == NULL) return 0; return slab->data[off - slab->base]; } struct TVChunk * TVGetChunk(struct TVRec *tv, TVSlabLocator loc) { Handle slabh; struct TVSlab *slab; char *addr; if (loc == 0) return NULL; slabh = tv->chunk_slab_hint; if (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; if (slab->id != SLABLOC_SLABID(loc)) slabh = NULL; } if (slabh == NULL) { slabh = tv->first_chunk_slab; while (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; if (slab->id == SLABLOC_SLABID(loc)) break; slabh = slab->next_slab; } if (slabh == NULL) return NULL; tv->chunk_slab_hint = slabh; } addr = slab->data + SLABLOC_OFFSET(loc); if ((unsigned long)addr % 2 != 0) { Debugger(); return NULL; } /* keep slab locked */ return (struct TVChunk *)addr; } struct TVChunk * TVCreateChunk(struct TVRec *tv) { Handle slabh; struct TVSlab *slab; struct TVChunk *chunk; TVSlabLocator cloc; slabh = tv->last_chunk_slab; HLock(slabh); slab = (struct TVSlab *)*slabh; if (slab->size - slab->pos < sizeof(struct TVChunk)) { if ((slabh = TVAllocateSlab(tv, TV_CHUNK_SLAB_SIZE, &tv->first_chunk_slab, &tv->last_chunk_slab)) == NULL) return NULL; slab = (struct TVSlab *)*slabh; } SET_SLABLOC_SLABID(cloc, slab->id); SET_SLABLOC_OFFSET(cloc, slab->pos); chunk = TVGetChunk(tv, cloc); chunk->loc = cloc; slab->pos += sizeof(struct TVChunk); if (!tv->first_chunk) tv->first_chunk = cloc; tv->last_chunk = cloc; return chunk; } struct TVLine * TVGetLine(struct TVRec *tv, unsigned long line) { Handle group; unsigned short groupn, idx; if (line == 0) return NULL; groupn = (line - 1) / TV_LINE_GROUP_NLINES; if (groupn >= (sizeof(tv->line_groups) / sizeof(tv->line_groups[0]))) return NULL; idx = line - 1 - (groupn * TV_LINE_GROUP_NLINES); group = tv->line_groups[groupn]; if (group == NULL && (group = TVAllocateLineGroup(tv, groupn)) == NULL) return NULL; HLock(group); /* keep group locked */ return (struct TVLine *)(*group + (sizeof(struct TVLine) * idx)); } void TVApplyStyle(struct TVStyle *style) { if (thePort->txFont == style->font && thePort->txFace == style->style && thePort->txSize == style->size) return; TextFont(style->font); TextFace(style->style); TextSize(style->size); } struct TVStyle * TVGetStyle(struct TVRec *tv, TVSlabLocator loc) { Handle slabh; struct TVSlab *slab; struct TVStyle *style; short n; slabh = tv->style_slab_hint; if (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; if (slab->id != SLABLOC_SLABID(loc)) slabh = NULL; } if (slabh == NULL) slabh = tv->first_style_slab; while (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; if (slab->id != SLABLOC_SLABID(loc)) { slabh = slab->next_slab; continue; } style = (struct TVStyle *)(slab->data + SLABLOC_OFFSET(loc)); if (style->loc != loc) { /* locator pointed at bogus style */ Debugger(); return NULL; } tv->style_slab_hint = slabh; return style; } return NULL; } struct TVStyle * TVFindStyle(struct TVRec *tv, struct TVStyle *style) { Handle slabh; struct TVSlab *slab; struct TVStyle *tstyle, *same_font; unsigned long size; if (style->loc) return TVGetStyle(tv, style->loc); same_font = NULL; slabh = tv->first_style_slab; while (slabh) { HLock(slabh); slab = (struct TVSlab *)*slabh; tstyle = (struct TVStyle *)(slab->data); iterate_slab: if (tstyle->loc == 0) goto next_slab; if (!ARE_STYLES_EQUAL(tstyle, style)) { /* but if it's the same in basic font metrics, use it */ if (same_font == NULL && !tstyle->bitmap_data_size && tstyle->font == style->font && tstyle->style == style->style && tstyle->size == style->size) same_font = tstyle; goto next_style; } if (tstyle->bitmap_data_size && memcmp(tstyle->bitmap_data, style->bitmap.baseAddr, tstyle->bitmap_data_size) != 0) goto next_style; TVApplyStyle(tstyle); tv->cur_style = tstyle->loc; return tstyle; next_style: if (SLABLOC_OFFSET(tstyle->loc) + sizeof(struct TVStyle) + tstyle->bitmap_data_size + sizeof(struct TVStyle) > slab->size) goto next_slab; tstyle = (struct TVStyle *)((char *)tstyle + sizeof(struct TVStyle) + tstyle->bitmap_data_size); if (tstyle->loc == 0) { if (slab->next_slab) goto next_slab; goto create_style; } goto iterate_slab; next_slab: slabh = slab->next_slab; } /* no matching style found, create a new one */ create_style: slabh = tv->last_style_slab; HLock(slabh); slab = (struct TVSlab *)*slabh; if (slab->pos + sizeof(struct TVStyle) + style->bitmap_data_size > slab->size) { /* need a new slab */ size = TV_STYLE_SLAB_SIZE; if (sizeof(struct TVStyle) + style->bitmap_data_size > size) size = sizeof(struct TVStyle) + style->bitmap_data_size; slabh = TVAllocateSlab(tv, size, &tv->first_style_slab, &tv->last_style_slab); if (slabh == NULL) return NULL; slab = (struct TVSlab *)*slabh; } tstyle = (struct TVStyle *)(slab->data + slab->pos); SET_SLABLOC_SLABID(tstyle->loc, slab->id); SET_SLABLOC_OFFSET(tstyle->loc, slab->pos); /* only copy over the important fields, rest will stay 0 */ /* NOTE: keep this in sync with ARE_STYLES_EQUAL */ tstyle->font = style->font; tstyle->style = style->style; tstyle->size = style->size; tstyle->align = style->align; tstyle->tag = style->tag; if (style->bitmap_data_size) { tstyle->font_info = style->font_info; tstyle->bitmap_data_size = style->bitmap_data_size; tstyle->bitmap = style->bitmap; memcpy(tstyle->bitmap_data, style->bitmap.baseAddr, style->bitmap_data_size); /* this must be recalculated at draw time once slab is locked */ tstyle->bitmap.baseAddr = 0; } slab->pos += sizeof(struct TVStyle) + tstyle->bitmap_data_size; if (slab->pos > slab->size) { Debugger(); return NULL; } TVApplyStyle(tstyle); if (!style->bitmap_data_size && same_font != NULL) { tstyle->font_info = same_font->font_info; memcpy(tstyle->width_cache, same_font->width_cache, sizeof(tstyle->width_cache)); } else if (!style->bitmap_data_size || style->font_info.widMax == 0) GetFontInfo(&tstyle->font_info); tv->cur_style = tstyle->loc; return tstyle; }