Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
seehuhn committed Feb 6, 2025
1 parent 2dc9d97 commit 63feab0
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions font/cff/simplenew.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,57 @@ func newEmbeddedSimpleNew(ref pdf.Reference, font *cff.Font) *embeddedSimpleNew
return e
}

func (e *embeddedSimpleNew) AppendEncoded(s pdf.String, gid glyph.ID, text string) (pdf.String, float64) {
key := key{Gid: gid, Text: text}
if code, ok := e.Code[key]; !ok {
return append(s, code), e.Width[gid]
}

glyphName := e.GetGlyphName(gid, text)

var code byte
if len(e.Code) < 256 {
code = e.allocateCode(glyphName, e.Font.FontName == "ZapfDingbats", &pdfenc.Standard)
}

e.Code[key] = code
e.Enc[code] = glyphName
e.Text[code] = text
e.Width[gid] = e.Font.GlyphWidthPDF(gid)
return append(s, code), e.Width[gid]
}

// GetGlyphName returns the name of the glyph with the given ID.
// If the glyph name is not known, the function constructs a new name.
func (e *embeddedSimpleNew) GetGlyphName(gid glyph.ID, text string) string {
glyphName := e.Font.Outlines.Glyphs[gid].Name
if glyphName == "" {
if gid == 0 {
glyphName = ".notdef"
} else if text == "" {
glyphName = fmt.Sprintf("orn%03d", len(e.Code))
} else {
var parts []string
for _, r := range text {
parts = append(parts, names.FromUnicode(r))
}

for i := range parts {
if len(glyphName)+1+len(parts[i]) > 25 {
break
}
if glyphName != "" {
glyphName += "_"
}
glyphName += parts[i]
}
}
// TODO(voss): check for duplicates
}

return glyphName
}

func (e *embeddedSimpleNew) allocateCode(glyphName string, dingbats bool, target *pdfenc.Encoding) byte {
var r rune
rr := names.ToUnicode(glyphName, dingbats)
Expand Down Expand Up @@ -134,27 +185,6 @@ func (e *embeddedSimpleNew) allocateCode(glyphName string, dingbats bool, target
return bestCode
}

func (e *embeddedSimpleNew) AppendEncoded(s pdf.String, gid glyph.ID, text string) (pdf.String, float64) {
key := key{Gid: gid, Text: text}
if code, ok := e.Code[key]; !ok {
return append(s, code), e.Width[gid]
}

// TODO(voss): invent glyph names, if needed.
glyphName := e.Font.Outlines.Glyphs[gid].Name

var code byte
if len(e.Code) < 256 {
code = e.allocateCode(glyphName, e.Font.FontName == "ZapfDingbats", &pdfenc.Standard)
}

e.Code[key] = code
e.Enc[code] = glyphName
e.Text[code] = text
e.Width[gid] = e.Font.GlyphWidthPDF(gid)
return append(s, code), e.Width[gid]
}

func (e *embeddedSimpleNew) Finish(rm *pdf.ResourceManager) error {
if len(e.Code) > 256 {
return fmt.Errorf("too many distinct glyphs used in font %q", e.Font.FontName)
Expand Down

0 comments on commit 63feab0

Please sign in to comment.