Files
openchamber-android-native/third_party/mygo/internal/darwin/ideographs.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

95 lines
2.9 KiB
Go

//go:build darwin && !ios
package darwin
import (
"sync"
"github.com/ebitengine/purego"
)
var (
coreTextOnce sync.Once
ctFontCreateWithName func(name id, size float64, matrix uintptr) uintptr
ctFontCopyPostScriptName func(font uintptr) id
ctFontGetGlyphsForCharacters func(font uintptr, chars, glyphs *uint16, count int) bool
)
// ideographBlocks are where RadicalIdeographs looks for the ideograph of a
// radical's glyph, in order: CJK Unified Ideographs, then Extension A.
var ideographBlocks = [...]struct{ first, last uint16 }{{0x4E00, 0x9FFF}, {0x3400, 0x4DBF}}
// RadicalIdeographs returns the ideographs that the installed font of the
// PostScript name draws with the glyphs of the radicals, as PingFang SC
// draws ⼈ (U+2F08) with the glyph of 人: for each radical whose glyph an
// ideograph has too, the first such ideograph of CJK Unified Ideographs,
// else of Extension A. It returns nil when no font has the name. It may be
// called from any goroutine.
func RadicalIdeographs(font string, radicals []rune) map[rune]rune {
load()
coreTextOnce.Do(func() {
lib := mustDlopen("/System/Library/Frameworks/CoreText.framework/CoreText")
purego.RegisterLibFunc(&ctFontCreateWithName, lib, "CTFontCreateWithName")
purego.RegisterLibFunc(&ctFontCopyPostScriptName, lib, "CTFontCopyPostScriptName")
purego.RegisterLibFunc(&ctFontGetGlyphsForCharacters, lib, "CTFontGetGlyphsForCharacters")
})
var found map[rune]rune
withPool(func() {
f := ctFontCreateWithName(nsString(font), 12, 0)
if f == 0 {
return
}
defer cfRelease(f)
// CoreText stands in another font for a name it does not know.
name := ctFontCopyPostScriptName(f)
if name == 0 {
return
}
same := goString(name) == font
cfRelease(uintptr(name))
if !same {
return
}
chars := make([]uint16, 0, len(radicals))
for _, r := range radicals {
if r > 0 && r <= 0xFFFF {
chars = append(chars, uint16(r))
}
}
if len(chars) == 0 {
return
}
// The radicals of each glyph, which may be several: Songti SC draws
// ⼝ (U+2F1D) and ⼜ (U+2F1C) as 口.
glyphs := make([]uint16, len(chars))
ctFontGetGlyphsForCharacters(f, &chars[0], &glyphs[0], len(chars))
wanted := map[uint16][]rune{}
for i, g := range glyphs {
if g != 0 {
wanted[g] = append(wanted[g], rune(chars[i]))
}
}
found = map[rune]rune{}
for _, b := range ideographBlocks {
if len(wanted) == 0 {
break
}
chars = make([]uint16, int(b.last-b.first)+1)
for i := range chars {
chars[i] = b.first + uint16(i)
}
glyphs = make([]uint16, len(chars))
// It reports whether every character has a glyph, which few
// fonts give them all; the others get glyph 0.
ctFontGetGlyphsForCharacters(f, &chars[0], &glyphs[0], len(chars))
for i, g := range glyphs {
for _, r := range wanted[g] {
found[r] = rune(chars[i])
}
delete(wanted, g)
}
}
})
return found
}