From 2fb992ed4eef8dc8a33ac91ac288ffd0a36c7913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20Andersson?= Date: Sun, 4 Apr 2021 10:23:20 +0200 Subject: [PATCH] GRAPHICS: Clip Mac font glyphs to the destination surface This was probably causing instabilities in the MacVenture engine. The engine is pretty broken at the moment, and the command window is so small that text doesn't fit in it at all. Hopefully this will make the remaining errors easier to debug... some day. --- graphics/fonts/macfont.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/graphics/fonts/macfont.cpp b/graphics/fonts/macfont.cpp index 7677a460bd7..d0c3c6d36c8 100644 --- a/graphics/fonts/macfont.cpp +++ b/graphics/fonts/macfont.cpp @@ -355,14 +355,31 @@ void MacFONTFont::drawChar(Surface *dst, uint32 chr, int x, int y, uint32 color) assert(dst != 0); assert(dst->format.bytesPerPixel == 1 || dst->format.bytesPerPixel == 2 || dst->format.bytesPerPixel == 4); + if (x > dst->w || y > dst->h) + return; + const MacGlyph *glyph = findGlyph(chr); if (!glyph || glyph->width == 0) return; - for (uint16 i = 0; i < _data._fRectHeight; i++) { + if (x + glyph->bitmapWidth < 0 || y + _data._fRectHeight < 0) + return; + + // Make sure we do not draw outside the surface + uint16 yStart = (y < 0) ? -y : 0; + uint16 yStop = _data._fRectHeight; + uint16 xStart = (x < 0) ? -x : 0; + uint16 xStop = glyph->bitmapWidth; + + if (y + _data._fRectHeight >= dst->h) + yStop = dst->h - y; + if (x + glyph->bitmapWidth >= dst->w) + xStop = dst->w - x; + + for (uint16 i = yStart; i < yStop; i++) { byte *srcRow = _data._bitImage + i * _data._rowWords; - for (uint16 j = 0; j < glyph->bitmapWidth; j++) { + for (uint16 j = xStart; j < xStop; j++) { uint16 bitmapOffset = glyph->bitmapOffset + j; if (srcRow[bitmapOffset / 8] & (1 << (7 - (bitmapOffset % 8)))) {