feat(文字编辑): 修复文字编辑字形映射
build.yaml / build (push) Successful in 2m55s

This commit is contained in:
xiaoqidun committed 2026-10-10 13:11:03 +08:00
1 parent 0f2fb7a842
commit e2c74894f0
4 files changed
+61 -6

No files matched your search

+48 -1
View File
@@ -361,7 +361,11 @@ func (e *Editor) styleTextObjects(objects []GraphicObject, style TextStyle) ([]G
return nil, &EditError{Code: EditUnsupportedObject, Err: fmt.Errorf("object %q requires explicit paragraph layout before changing size", id)}
}
if fontChanged {
text.Font = style.Font
if known {
text.Font = style.Font
} else if err := e.replaceTextFont(text, style.Font); err != nil {
return nil, err
}
}
if sizeChanged {
text.Size = style.Size
@@ -390,6 +394,49 @@ func (e *Editor) styleTextObjects(objects []GraphicObject, style TextStyle) ([]G
return updates, nil
}
// replaceTextFont 更换字体并重建字形索引,保留原文字定位
// 入参: text 文字对象, id 目标字体标识
// 返回: error 错误信息
func (e *Editor) replaceTextFont(text *TextObject, id string) error {
if text.Font == id {
return nil
}
if len(text.CGTransform) == 0 {
text.Font = id
return nil
}
if err := validateTextGlyphs(*text, 1<<16); err != nil {
return err
}
font, err := e.editorFont(id)
if err != nil {
return err
}
var chars []rune
for _, code := range text.TextCode {
chars = append(chars, textCodeRunes(code.Value)...)
}
transforms := append([]CGTransform(nil), text.CGTransform...)
var missing []rune
for i := range transforms {
transform := &transforms[i]
if transform.CodeCount != 1 || transform.GlyphCount != 1 {
return &EditError{Code: EditLayoutRequired, Err: fmt.Errorf("changing the font of combined glyphs requires explicit layout")}
}
char := chars[transform.CodePosition]
glyph := font.GlyphIndex(char)
if glyph == 0 {
missing = append(missing, char)
}
transform.Glyphs = strconv.Itoa(int(glyph))
}
if err := missingGlyphError(id, missing); err != nil {
return err
}
text.Font, text.CGTransform = id, transforms
return nil
}
// breakTextLines 优先使用Unicode断行机会,过长词仅在字素边界折行,不丢弃空白
// 入参: runes 段落字符, advances 含字距的字符步进, width 本地排版宽度, wrap 是否自动折行, spacing 附加字距, indent 首行缩进
// 返回: [][2]int 各行的字符起止索引,左闭右开