Files
openchamber-android-native/third_party/mygo/internal/text/stroke.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

86 lines
2.3 KiB
Go

package text
import "math"
// strokeBitmap returns the coverage of a stroke along the edge of the
// coverage of b, reaching r pixels to each side of it, round at corners:
// what b dilated by r covers and b eroded by r does not. An edge pixel's
// coverage places the edge within it, so that the stroke's edges are as
// smooth as the glyph's. Color glyphs stroke the edge of their alpha.
func strokeBitmap(b bitmap, r float32) bitmap {
pad := int(math.Ceil(float64(r))) + 1
w, h := b.w+2*pad, b.h+2*pad
cov := make([]float32, b.w*b.h)
for i := range cov {
switch {
case b.color:
cov[i] = float32(b.pix[4*i+3]) / 255
case b.subpixel:
p := b.pix[4*i:]
cov[i] = float32(max(p[0], p[1], p[2])) / 255
default:
cov[i] = float32(b.pix[i]) / 255
}
}
// The pixels a pixel's coverage reaches: within r of its edge, which
// is up to a pixel from its center.
type offset struct {
dx, dy int
d float32
}
// Nearest first, as the coverage of a pixel saturates soonest: by
// their squared distances, counted.
reach := r + 1
maxSq := 2 * pad * pad
counts := make([]int, maxSq+2)
for dy := -pad; dy <= pad; dy++ {
for dx := -pad; dx <= pad; dx++ {
counts[dx*dx+dy*dy+1]++
}
}
for i := 1; i < len(counts); i++ {
counts[i] += counts[i-1]
}
kernel := make([]offset, counts[len(counts)-1])
for dy := -pad; dy <= pad; dy++ {
for dx := -pad; dx <= pad; dx++ {
sq := dx*dx + dy*dy
kernel[counts[sq]] = offset{dx, dy, float32(math.Sqrt(float64(sq)))}
counts[sq]++
}
}
for len(kernel) > 0 && kernel[len(kernel)-1].d >= reach {
kernel = kernel[:len(kernel)-1]
}
at := func(x, y int) float32 {
if x < 0 || y < 0 || x >= b.w || y >= b.h {
return 0
}
return cov[y*b.w+x]
}
pix := make([]byte, w*h)
for y := range h {
for x := range w {
// out is what the dilated glyph covers, in what the eroded
// background does.
var out, in float32
for _, k := range kernel {
c := at(x-pad+k.dx, y-pad+k.dy)
if c > 0 {
out = max(out, min(r+c-k.d, 1))
}
if c < 1 {
in = max(in, min(r+1-c-k.d, 1))
}
if out >= 1 && in >= 1 {
break
}
}
if v := out + in - 1; v > 0 {
pix[y*w+x] = uint8(v*255 + 0.5)
}
}
}
return bitmap{left: b.left - pad, top: b.top - pad, w: w, h: h, pix: pix}
}