Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview/ogcard: add utility to draw rounded rectangles

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by tangled.org 0250a132 a5aa3f1e

+56
+56
appview/ogcard/card.go
··· 257 257 return textWidth, err 258 258 } 259 259 260 + func (c *Card) FontHeight(sizePt float64) int { 261 + ft := freetype.NewContext() 262 + ft.SetDPI(72) 263 + ft.SetFont(c.Font) 264 + ft.SetFontSize(sizePt) 265 + return ft.PointToFixed(sizePt).Ceil() 266 + } 267 + 268 + func (c *Card) TextWidth(text string, sizePt float64) int { 269 + face := truetype.NewFace(c.Font, &truetype.Options{Size: sizePt, DPI: 72}) 270 + lineWidth := font.MeasureString(face, text) 271 + textWidth := lineWidth.Ceil() 272 + return textWidth 273 + } 274 + 260 275 // DrawBoldText draws bold text by rendering multiple times with slight offsets 261 276 func (c *Card) DrawBoldText(text string, x, y int, textColor color.Color, sizePt float64, valign VAlign, halign HAlign) (int, error) { 262 277 // Draw the text multiple times with slight offsets to create bold effect ··· 596 581 // DrawRect draws a rect with the given color 597 582 func (c *Card) DrawRect(startX, startY, endX, endY int, color color.Color) { 598 583 draw.Draw(c.Img, image.Rect(startX, startY, endX, endY), &image.Uniform{color}, image.Point{}, draw.Src) 584 + } 585 + 586 + // drawRoundedRect draws a filled rounded rectangle on the given card 587 + func (card *Card) DrawRoundedRect(x, y, width, height, cornerRadius int, fillColor color.RGBA) { 588 + cardBounds := card.Img.Bounds() 589 + for py := y; py < y+height; py++ { 590 + for px := x; px < x+width; px++ { 591 + // calculate distance from corners 592 + dx := 0 593 + dy := 0 594 + 595 + // check which corner region we're in 596 + if px < x+cornerRadius && py < y+cornerRadius { 597 + // top-left corner 598 + dx = x + cornerRadius - px 599 + dy = y + cornerRadius - py 600 + } else if px >= x+width-cornerRadius && py < y+cornerRadius { 601 + // top-right corner 602 + dx = px - (x + width - cornerRadius - 1) 603 + dy = y + cornerRadius - py 604 + } else if px < x+cornerRadius && py >= y+height-cornerRadius { 605 + // bottom-left corner 606 + dx = x + cornerRadius - px 607 + dy = py - (y + height - cornerRadius - 1) 608 + } else if px >= x+width-cornerRadius && py >= y+height-cornerRadius { 609 + // Bottom-right corner 610 + dx = px - (x + width - cornerRadius - 1) 611 + dy = py - (y + height - cornerRadius - 1) 612 + } 613 + 614 + // if we're in a corner, check if we're within the radius 615 + inCorner := (dx > 0 || dy > 0) 616 + withinRadius := dx*dx+dy*dy <= cornerRadius*cornerRadius 617 + 618 + // draw pixel if not in corner, or in corner and within radius 619 + // check bounds relative to the card's image bounds 620 + if (!inCorner || withinRadius) && px >= 0 && px < cardBounds.Dx() && py >= 0 && py < cardBounds.Dy() { 621 + card.Img.Set(px+cardBounds.Min.X, py+cardBounds.Min.Y, fillColor) 622 + } 623 + } 624 + } 599 625 }