From e753fc11ca503fa322c8d2f4a033714c89a5b15d Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 29 Nov 2022 02:12:30 -0500 Subject: [PATCH] Implemented awful fallback bold and italic --- backends/x/draw.go | 128 +++++++++++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 50 deletions(-) diff --git a/backends/x/draw.go b/backends/x/draw.go index e46d186..37d80d6 100644 --- a/backends/x/draw.go +++ b/backends/x/draw.go @@ -130,17 +130,18 @@ func (backend *Backend) drawRune ( backend.boundsOfCell(x, y)) } - // cue a series of pointless optimizations - alphaMask, isAlpha := mask.(*image.Alpha) - if isAlpha { - backend.sprayRuneMaskAlpha ( - alphaMask, destinationRectangle, - maskPoint, foreground, background) - } else { + // alphaMask, isAlpha := mask.(*image.Alpha) + // if isAlpha { + // backend.sprayRuneMaskAlpha ( + // alphaMask, destinationRectangle, + // maskPoint, foreground, background) + // } else { backend.sprayRuneMask ( mask, destinationRectangle, - maskPoint, foreground, background) - } + maskPoint, foreground, background, + runeStyle & stone.StyleItalic > 0, + runeStyle & stone.StyleBold > 0) + // } } // underline @@ -161,54 +162,81 @@ func (backend *Backend) sprayRuneMask ( maskPoint image.Point, fill xgraphics.BGRA, background xgraphics.BGRA, + italic bool, + bold bool, ) { maxX := bounds.Max.X - bounds.Min.X maxY := bounds.Max.Y - bounds.Min.Y for y := 0; y < maxY; y ++ { - for x := 0; x < maxX; x ++ { - _, _, _, - alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA() - backend.canvas.SetBGRA ( - x + bounds.Min.X, - y + bounds.Min.Y - backend.metrics.descent, - xgraphics.BlendBGRA ( - background, - xgraphics.BGRA { - R: fill.R, - G: fill.G, - B: fill.B, - A: uint8(alpha >> 8), - })) - }} + var previousAlpha uint32 + offset := 0 + if italic { + offset = (maxY - y) / 4 + } + for x := 0; x < maxX; x ++ { + _, _, _, + alpha := mask.At(x + maskPoint.X, y + maskPoint.Y).RGBA() + currentAlpha := alpha + if bold && previousAlpha > alpha { + alpha = previousAlpha + } + backend.canvas.SetBGRA ( + x + bounds.Min.X + offset, + y + bounds.Min.Y - backend.metrics.descent, + xgraphics.BlendBGRA ( + background, + xgraphics.BGRA { + R: fill.R, + G: fill.G, + B: fill.B, + A: uint8(alpha >> 8), + })) + previousAlpha = currentAlpha + } + + if bold { + backend.canvas.SetBGRA ( + bounds.Max.X + offset, + y + bounds.Min.Y - backend.metrics.descent, + xgraphics.BlendBGRA ( + background, + xgraphics.BGRA { + R: fill.R, + G: fill.G, + B: fill.B, + A: uint8(previousAlpha >> 8), + })) + } + } } -func (backend *Backend) sprayRuneMaskAlpha ( - mask *image.Alpha, - bounds image.Rectangle, - maskPoint image.Point, - fill xgraphics.BGRA, - background xgraphics.BGRA, -) { - maxX := bounds.Max.X - bounds.Min.X - maxY := bounds.Max.Y - bounds.Min.Y - - for y := 0; y < maxY; y ++ { - for x := 0; x < maxX; x ++ { - alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A - backend.canvas.SetBGRA ( - x + bounds.Min.X, - y + bounds.Min.Y - backend.metrics.descent, - xgraphics.BlendBGRA ( - background, - xgraphics.BGRA { - R: fill.R, - G: fill.G, - B: fill.B, - A: alpha, - })) - }} -} +// func (backend *Backend) sprayRuneMaskAlpha ( + // mask *image.Alpha, + // bounds image.Rectangle, + // maskPoint image.Point, + // fill xgraphics.BGRA, + // background xgraphics.BGRA, +// ) { + // maxX := bounds.Max.X - bounds.Min.X + // maxY := bounds.Max.Y - bounds.Min.Y + // + // for y := 0; y < maxY; y ++ { + // for x := 0; x < maxX; x ++ { + // alpha := mask.AlphaAt(x + maskPoint.X, y + maskPoint.Y).A + // backend.canvas.SetBGRA ( + // x + bounds.Min.X, + // y + bounds.Min.Y - backend.metrics.descent, + // xgraphics.BlendBGRA ( + // background, + // xgraphics.BGRA { + // R: fill.R, + // G: fill.G, + // B: fill.B, + // A: alpha, + // })) + // }} +// } func fillRectangle ( source image.Image,