Files
FontMM/golang/internal/wght/wght.go
T

532 lines
16 KiB
Go

// Package wght 提供字体配置 XML 的字重范围覆写逻辑
package wght
import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
)
// 标准 9 档字重
var Weights = []int{100, 200, 300, 400, 500, 600, 700, 800, 900}
// axisStyle 描述家族内 <font> 条目的轴配置风格
type axisStyle int
const (
// axisFull: sans-serif 全轴 (ital/wdth/wght)
axisFull axisStyle = iota
// axisWghtOnly: sys-sans-en / zh-* 仅 wght 轴
axisWghtOnly
)
// familySpec 描述一个需要覆写字重的家族及其条目格式
type familySpec struct {
slot string // 所属逻辑槽位: "en" | "hans" | "hant"
openTag string // 家族起始标签 (用于定位与生成)
fontFile string // 家族引用的字体文件
postScript string // postScriptName 属性 (无则为空)
serifFallback bool // 是否包含 fallbackFor="serif" 的 400 条目 (zh-Hans/zh-Hant)
axis axisStyle
}
// 所有生效家族: 英文 (sans-serif / sys-sans-en) 与中文 (zh-Hans / zh-Hant) 都要覆盖,
// 否则中文或英文单独使用时字重映射不生效 (issue #7)
var FamilySpecs = []familySpec{
{slot: "en", openTag: `<family name="sans-serif">`, fontFile: "SysFont-Regular.ttf", axis: axisFull},
{slot: "en", openTag: `<family name="sys-sans-en">`, fontFile: "SysSans-En-Regular.ttf", postScript: "OPlusSansEn", axis: axisWghtOnly},
{slot: "hans", openTag: `<family lang="zh-Hans">`, fontFile: "SysSans-Hans-Regular.ttf", postScript: "OPPO_Sans_4.0_SC", serifFallback: true, axis: axisWghtOnly},
{slot: "hant", openTag: `<family lang="zh-Hant,zh-Bopo">`, fontFile: "SysSans-Hant-Regular.ttf", postScript: "OPPO_Sans_4.0_TC", serifFallback: true, axis: axisWghtOnly},
}
// 主配置文件名 (唯一源) 与纯净备份路径
const (
SourceXMLRel = "system/etc/fonts.xml"
BackupXMLRel = "backup/fonts.xml"
)
// 派生配置 (与 fonts.xml 内容一致, 由 -sync 复制生成)
var DerivedXMLRel = []string{
"system/etc/fonts_base.xml",
"system/etc/fonts_ule.xml",
"system/etc/font_fallback.xml",
"system/system_ext/etc/fonts_base.xml",
"system/system_ext/etc/fonts_ule.xml",
}
// SlotMode 描述槽位当前字体模式 ("single" | "multi")
type SlotMode string
const (
SlotModeSingle SlotMode = "single"
SlotModeMulti SlotMode = "multi"
)
// MultiSlotConfig 描述一个生效后的多字重配置 (支持 hant/en 回退到 hans)
type MultiSlotConfig struct {
SourceSlot string // 实际使用的字体来源槽位: "hans" | "hant" | "en"
Available []int // 已选择的字重列表 (升序, 100..900)
}
// FamilyConfig 描述某家族最终生效的模式 (单字体覆写 或 多字重静态映射)
type FamilyConfig struct {
Multi *MultiSlotConfig // 非 nil 时按多字重生成, 忽略 wght 轴覆写
}
// NearestWeight 从已选字重列表中挑选最接近 target 的档位。
// 距离相等时: target >= 400 偏向更重档位 (如 400 取 500、600 取 700),
// target < 400 偏向更轻档位 (如 300 取 200)。
func NearestWeight(available []int, target int) int {
if len(available) == 0 {
return 400
}
best := available[0]
bestDist := absInt(best - target)
for _, w := range available[1:] {
d := absInt(w - target)
if d < bestDist || (d == bestDist && ((target >= 400 && w > best) || (target < 400 && w < best))) {
best = w
bestDist = d
}
}
return best
}
func absInt(x int) int {
if x < 0 {
return -x
}
return x
}
func slotCapName(slot string) string {
switch slot {
case "hans":
return "Hans"
case "hant":
return "Hant"
case "en":
return "En"
default:
return slot
}
}
// multiFontFile 返回多字重模式下指定已解析字重对应的系统字体文件名。
// 400 复用原有的 Regular 占位文件 (避免重复复制); 其他档位使用 FontMM-<Slot>-<W>.ttf。
func multiFontFile(spec familySpec, sourceSlot string, resolvedWeight int) string {
if resolvedWeight == 400 {
if sourceSlot == spec.slot {
return spec.fontFile
}
// 繁体或英文回退到简体多字重时直接指向简体的 Regular, 避免重复文件与多余缓存
if sourceSlot == "hans" {
return "SysSans-Hans-Regular.ttf"
}
return fmt.Sprintf("SysSans-%s-Regular.ttf", slotCapName(sourceSlot))
}
return fmt.Sprintf("FontMM-%s-%d.ttf", slotCapName(sourceSlot), resolvedWeight)
}
// 平均分配: weight w 对应的 axis 值 (400 固定, 两端 floor 线性插值)
func AveragedAxis(min, max, w int) int {
if w == 400 {
return 400
}
if w < 400 {
return min + (400-min)*(w-100)/300
}
return 400 + (max-400)*(w-400)/500
}
// 生成 sans-serif 家族段 (mode 1/2/3), 使用标准 9 档字重
func GenerateFamily(min, max, mode int, customMap map[int]int) string {
return generateFamily(FamilySpecs[0], Weights, min, max, mode, customMap)
}
// 生成单条 <font> 条目 (按家族风格)
func fontEntryFor(spec familySpec, weight, axisWght int) string {
if spec.axis == axisFull {
return fmt.Sprintf(` <font weight="%d" style="normal">%s
<axis tag="ital" stylevalue="0" />
<axis tag="wdth" stylevalue="100" />
<axis tag="wght" stylevalue="%d" />
</font>`, weight, spec.fontFile, axisWght)
}
return fmt.Sprintf(` <font weight="%d" style="normal" postScriptName="%s" >%s
<axis tag="wght" stylevalue="%d"/>
</font>`, weight, spec.postScript, spec.fontFile, axisWght)
}
// 生成 zh-Hans/zh-Hant 家族的 fallbackFor="serif" 400 条目
func serifEntry(spec familySpec, fontFile string) string {
return fmt.Sprintf(` <font weight="400" style="normal" fallbackFor="serif"
postScriptName="%s">%s
</font>`, spec.postScript, fontFile)
}
// 生成单条静态多字重 <font> 条目 (不带 <axis tag="wght"/>)
func staticFontEntryFor(spec familySpec, weight int, fontFile string) string {
if spec.postScript != "" {
return fmt.Sprintf(` <font weight="%d" style="normal" postScriptName="%s" >%s</font>`,
weight, spec.postScript, fontFile)
}
return fmt.Sprintf(` <font weight="%d" style="normal">%s</font>`, weight, fontFile)
}
// 生成多字重家族段: 缺失档位按 NearestWeight 指向最接近的已选字重文件
func generateMultiFamily(spec familySpec, weights []int, multi MultiSlotConfig) string {
var lines []string
for _, w := range weights {
resolved := NearestWeight(multi.Available, w)
file := multiFontFile(spec, multi.SourceSlot, resolved)
lines = append(lines, staticFontEntryFor(spec, w, file))
if spec.serifFallback && w == 400 {
lines = append(lines, serifEntry(spec, file))
}
}
return " " + spec.openTag + "\n" + strings.Join(lines, "\n") + "\n </family>"
}
// 生成家族段 (mode 1=裁切 2=平均 3=自定义映射)
// weights 为声明字重集 (取自原家族, 保持设备 XML 一致性)
func generateFamily(spec familySpec, weights []int, min, max, mode int, customMap map[int]int) string {
ws := weights
if mode == 3 && len(customMap) > 0 {
ws = make([]int, 0, len(customMap))
for w := range customMap {
ws = append(ws, w)
}
sort.Ints(ws)
}
var lines []string
for _, w := range ws {
if mode == 1 && (w < min || w > max) {
continue
}
axis := w
if mode == 2 {
axis = AveragedAxis(min, max, w)
} else if mode == 3 {
if v, ok := customMap[w]; ok {
axis = v
} else {
axis = AveragedAxis(min, max, w)
}
}
lines = append(lines, fontEntryFor(spec, w, axis))
if spec.serifFallback && w == 400 {
lines = append(lines, serifEntry(spec, spec.fontFile))
}
}
return " " + spec.openTag + "\n" + strings.Join(lines, "\n") + "\n </family>"
}
// 在完整 XML 中覆写指定家族段; 家族不存在时原样返回
func replaceFamily(xml string, spec familySpec, mode, min, max int, customMap map[int]int, multi *MultiSlotConfig) string {
start := findFamilyStart(xml, spec.openTag)
if start < 0 {
return xml
}
end := matchFamilyClose(xml, start+len(spec.openTag))
if end < 0 {
return xml
}
block := xml[start:end]
if multi == nil && mode == 0 {
return xml
}
// 字重集选择: 多字重 / 裁切保留原家族声明字重 (兼容设备 XML, 含 1000),
// 平均分配固定 9 档完整粗细, 自定义映射由映射文件的键决定
weights := Weights
if multi != nil || mode == 1 {
if ex := extractWeights(block); len(ex) > 0 {
weights = ex
}
}
// 整行替换 (含行首缩进与闭合标签后的换行), 保持与原有段一致的缩进
lineStart := start
for lineStart > 0 && xml[lineStart-1] != '\n' {
lineStart--
}
lineEnd := end
for lineEnd < len(xml) && xml[lineEnd] != '\n' {
lineEnd++
}
if lineEnd < len(xml) {
lineEnd++ // 吃掉换行
}
var generated string
if multi != nil {
generated = generateMultiFamily(spec, weights, *multi)
} else {
generated = generateFamily(spec, weights, min, max, mode, customMap)
}
return xml[:lineStart] + generated + "\n" + xml[lineEnd:]
}
// 覆写全部生效家族 (mode 0 原样返回)
func ApplyWghtMode(xml string, mode, min, max int, customMap map[int]int) string {
return ApplyWghtAndMulti(xml, mode, min, max, customMap, nil)
}
// ApplyWghtAndMulti 按槽位生效配置同时处理单字体字重覆写与多字重静态映射。
// slotMulti 的键为槽位名 ("hans" / "hant" / "en"), 非 nil 的槽位按多字重生成, 否则走单字体覆写。
func ApplyWghtAndMulti(xml string, mode, min, max int, customMap map[int]int, slotMulti map[string]*MultiSlotConfig) string {
result := xml
for _, spec := range FamilySpecs {
var multi *MultiSlotConfig
if slotMulti != nil {
multi = slotMulti[spec.slot]
}
if multi == nil && mode == 0 {
continue
}
result = replaceFamily(result, spec, mode, min, max, customMap, multi)
}
return result
}
// 查找 startTag 的位置 (跳过 XML 注释, 避免误匹配注释内的文本)
func findFamilyStart(xml, startTag string) int {
searchFrom := 0
for {
i := strings.Index(xml[searchFrom:], startTag)
if i < 0 {
return -1
}
i += searchFrom
open := strings.LastIndex(xml[:i], "<!--")
if open < 0 {
return i
}
if rel := strings.Index(xml[open:], "-->"); rel < 0 || open+rel+3 > i {
// 命中位置在注释内, 继续向后找
searchFrom = i + 1
continue
}
return i
}
}
// 从紧随 <family ...> 之后的位置开始, 找到配对的 </family> (含嵌套),
// 返回其结束位置 (含标签); 未找到返回 -1
func matchFamilyClose(xml string, start int) int {
depth := 1
i := start
for i < len(xml) {
// 跳过注释
if strings.HasPrefix(xml[i:], "<!--") {
if rel := strings.Index(xml[i:], "-->"); rel >= 0 {
i += rel + len("-->")
continue
}
}
// 找下一个 '<'
lt := strings.IndexByte(xml[i:], '<')
if lt < 0 {
return -1
}
i += lt
if strings.HasPrefix(xml[i:], "</family>") {
depth--
if depth == 0 {
return i + len("</family>")
}
i += len("</family>")
continue
}
if strings.HasPrefix(xml[i:], "<family") {
gt := strings.IndexByte(xml[i:], '>')
if gt < 0 {
return -1
}
// 自闭合 <family .../> 不增加嵌套深度
if xml[i+gt-1] != '/' {
depth++
}
i += gt + 1
continue
}
// 其他标签: 跳到 '>' 之后
gt := strings.IndexByte(xml[i:], '>')
if gt < 0 {
return -1
}
i += gt + 1
}
return -1
}
var weightRe = regexp.MustCompile(`<font weight="(\d+)"`)
// 提取家族段内声明字重 (去重并升序), 保留设备 XML 的字重集
func extractWeights(block string) []int {
var ws []int
seen := map[int]bool{}
for _, m := range weightRe.FindAllStringSubmatch(block, -1) {
if n, err := strconv.Atoi(m[1]); err == nil && !seen[n] {
ws = append(ws, n)
seen[n] = true
}
}
sort.Ints(ws)
return ws
}
// 读取自定义映射文件 (每行 "weight axis")
func ReadCustomMap(path string) (map[int]int, error) {
m := map[int]int{}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Fields(line)
if len(parts) != 2 {
continue
}
w, err1 := strconv.Atoi(parts[0])
v, err2 := strconv.Atoi(parts[1])
if err1 == nil && err2 == nil {
m[w] = v
}
}
return m, nil
}
// ReadSlotModes 读取 FONTS/slot-modes.txt (格式: slot=single|multi), 缺失时全部默认 single
func ReadSlotModes(xmlDir string) map[string]SlotMode {
modes := map[string]SlotMode{
"hans": SlotModeSingle,
"hant": SlotModeSingle,
"en": SlotModeSingle,
}
data, err := os.ReadFile(filepath.Join(xmlDir, "FONTS", "slot-modes.txt"))
if err != nil {
return modes
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
idx := strings.IndexByte(line, '=')
if idx <= 0 {
continue
}
k := strings.TrimSpace(line[:idx])
v := strings.TrimSpace(line[idx+1:])
if _, ok := modes[k]; !ok {
continue
}
if v == string(SlotModeMulti) {
modes[k] = SlotModeMulti
} else if v == string(SlotModeSingle) {
modes[k] = SlotModeSingle
}
}
return modes
}
func fileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
// scanMultiWeights 扫描 FONTS/multi/<slot>/<w>.ttf 中已设置的字重档位 (升序)
func scanMultiWeights(xmlDir, slot string) []int {
var out []int
for _, w := range Weights {
p := filepath.Join(xmlDir, "FONTS", "multi", slot, fmt.Sprintf("%d.ttf", w))
if fileExists(p) {
out = append(out, w)
}
}
return out
}
// ResolveMultiConfigs 根据 FONTS/slot-modes.txt 与 FONTS/ 实际字体计算各槽位多字重配置。
// hant/en 在自身模式下无字体时自动回退到 hans 的实际生效模式。
func ResolveMultiConfigs(xmlDir string) map[string]*MultiSlotConfig {
modes := ReadSlotModes(xmlDir)
result := map[string]*MultiSlotConfig{}
var hansMulti *MultiSlotConfig
if modes["hans"] == SlotModeMulti {
if ws := scanMultiWeights(xmlDir, "hans"); len(ws) > 0 {
hansMulti = &MultiSlotConfig{SourceSlot: "hans", Available: ws}
result["hans"] = hansMulti
}
}
for _, slot := range []string{"hant", "en"} {
if modes[slot] == SlotModeMulti {
if ws := scanMultiWeights(xmlDir, slot); len(ws) > 0 {
result[slot] = &MultiSlotConfig{SourceSlot: slot, Available: ws}
continue
}
// 多字重模式下 0 个字重时回退到 hans
if hansMulti != nil {
result[slot] = hansMulti
}
continue
}
// 单字体模式下未设置 <slot>.ttf 时回退到 hans
if !fileExists(filepath.Join(xmlDir, "FONTS", slot+".ttf")) && hansMulti != nil {
result[slot] = hansMulti
}
}
return result
}
// ApplyToDir 覆写模块根目录下的主配置 fonts.xml; sync 为 true 时复制到各派生配置
// 优先从 backup/fonts.xml 读取纯净模板 (存在时), 保证 single/multi 往返切换幂等。
// 返回覆写/同步的文件总数
func ApplyToDir(xmlDir string, mode, min, max int, customMap map[int]int, sync bool) (int, error) {
src := filepath.Join(xmlDir, SourceXMLRel)
currentData, err := os.ReadFile(src)
if err != nil {
return 0, fmt.Errorf("读取 %s 失败: %w", SourceXMLRel, err)
}
baseData := currentData
backupPath := filepath.Join(xmlDir, BackupXMLRel)
if b, err := os.ReadFile(backupPath); err == nil && len(b) > 0 {
baseData = b
}
slotMulti := ResolveMultiConfigs(xmlDir)
replaced := ApplyWghtAndMulti(string(baseData), mode, min, max, customMap, slotMulti)
changed := 0
if replaced != string(currentData) {
if err := os.WriteFile(src, []byte(replaced), 0o644); err != nil {
return changed, fmt.Errorf("写入失败 %s: %w", SourceXMLRel, err)
}
changed++
fmt.Printf("[✓] 已覆写: %s\n", SourceXMLRel)
} else {
fmt.Printf("[-] 无变化: %s\n", SourceXMLRel)
}
if sync {
for _, rel := range DerivedXMLRel {
p := filepath.Join(xmlDir, rel)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return changed, fmt.Errorf("创建目录失败 %s: %w", filepath.Dir(p), err)
}
if err := os.WriteFile(p, []byte(replaced), 0o644); err != nil {
return changed, fmt.Errorf("同步失败 %s: %w", rel, err)
}
changed++
fmt.Printf("[✓] 已同步: %s\n", rel)
}
}
return changed, nil
}