Skip to content

Commit f4f77e4

Browse files
committed
fix wrapping issue on glyph slots, refactor glyph_advance check.
1 parent a2532bd commit f4f77e4

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

shared-module/lvfontio/OnDiskFont.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -226,23 +226,22 @@ static bool load_font_header(lvfontio_ondiskfont_t *self, FIL *file, size_t *max
226226
// Throw away the bitmap bits.
227227
read_bits(file, self->header.bits_per_pixel * bbox_w * bbox_h, &byte_val, &remaining_bits, NULL);
228228

229-
// Ignore zero-advance glyphs when inferring the terminal cell width.
230-
// Some fonts include placeholders/control glyphs with zero advance,
231-
// which would otherwise skew default_advance_width too small.
232-
if (glyph_advance > 0) {
233-
if (advances[0] == glyph_advance) {
234-
advance_count[0]++;
235-
} else if (advances[1] == glyph_advance) {
236-
advance_count[1]++;
237-
} else if (advance_count[0] == 0) {
238-
advances[0] = glyph_advance;
239-
advance_count[0] = 1;
240-
} else if (advance_count[1] == 0) {
241-
advances[1] = glyph_advance;
242-
advance_count[1] = 1;
243-
} else {
244-
break;
245-
}
229+
if (glyph_advance == 0) {
230+
// Ignore zero-advance glyphs when inferring the terminal cell width.
231+
// Some fonts include placeholders/control glyphs with zero advance,
232+
// which would otherwise skew default_advance_width too small.
233+
} else if (advances[0] == glyph_advance) {
234+
advance_count[0]++;
235+
} else if (advances[1] == glyph_advance) {
236+
advance_count[1]++;
237+
} else if (advance_count[0] == 0) {
238+
advances[0] = glyph_advance;
239+
advance_count[0] = 1;
240+
} else if (advance_count[1] == 0) {
241+
advances[1] = glyph_advance;
242+
advance_count[1] = 1;
243+
} else {
244+
break;
246245
}
247246
cid++;
248247
}
@@ -592,12 +591,12 @@ int16_t common_hal_lvfontio_ondiskfont_cache_glyph(lvfontio_ondiskfont_t *self,
592591
self->reference_counts[existing_slot]++;
593592

594593
// Check if this is a full-width character by looking for a second slot
595-
// with the same codepoint right after this one
596-
bool cached_is_full_width = existing_slot + 1 < self->max_glyphs &&
597-
self->codepoints[existing_slot + 1] == codepoint;
594+
// with the same codepoint right after this one, wrapping at the end.
595+
uint16_t next_slot = (existing_slot + 1) % self->max_glyphs;
596+
bool cached_is_full_width = self->codepoints[next_slot] == codepoint;
598597

599598
if (cached_is_full_width) {
600-
self->reference_counts[existing_slot + 1]++;
599+
self->reference_counts[next_slot]++;
601600
}
602601

603602
if (is_full_width != NULL) {
@@ -756,10 +755,13 @@ static bool slot_has_active_full_width_partner(lvfontio_ondiskfont_t *self, uint
756755
}
757756

758757
// Don't evict one half of a full-width pair while the other half is still in use.
759-
if (slot > 0 && self->codepoints[slot - 1] == codepoint && self->reference_counts[slot - 1] > 0) {
758+
uint16_t prev_slot = (slot + self->max_glyphs - 1) % self->max_glyphs;
759+
uint16_t next_slot = (slot + 1) % self->max_glyphs;
760+
761+
if (self->codepoints[prev_slot] == codepoint && self->reference_counts[prev_slot] > 0) {
760762
return true;
761763
}
762-
if (slot + 1 < self->max_glyphs && self->codepoints[slot + 1] == codepoint && self->reference_counts[slot + 1] > 0) {
764+
if (self->codepoints[next_slot] == codepoint && self->reference_counts[next_slot] > 0) {
763765
return true;
764766
}
765767

0 commit comments

Comments
 (0)