mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-05 19:30:30 +08:00
textencoding: simplify code of IdentityEncoder
This commit is contained in:
parent
991aa2727a
commit
d06cbae6c5
@ -7,8 +7,9 @@ package textencoding
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/unidoc/unidoc/common"
|
|
||||||
"github.com/unidoc/unidoc/pdf/core"
|
"github.com/unidoc/unidoc/pdf/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,17 +50,11 @@ func (enc IdentityEncoder) CharcodeToGlyph(code uint16) (string, bool) {
|
|||||||
// GlyphToCharcode returns the character code matching glyph `glyph`.
|
// GlyphToCharcode returns the character code matching glyph `glyph`.
|
||||||
// The bool return flag is true if there was a match, and false otherwise.
|
// The bool return flag is true if there was a match, and false otherwise.
|
||||||
func (enc IdentityEncoder) GlyphToCharcode(glyph string) (uint16, bool) {
|
func (enc IdentityEncoder) GlyphToCharcode(glyph string) (uint16, bool) {
|
||||||
// String with "uniXXXX" format where XXXX is the hexcode.
|
r, ok := enc.GlyphToRune(glyph)
|
||||||
if len(glyph) == 7 && glyph[0:3] == "uni" {
|
if !ok {
|
||||||
var unicode uint16
|
return 0, false
|
||||||
n, err := fmt.Sscanf(glyph, "uni%X", &unicode)
|
|
||||||
if n == 1 && err == nil {
|
|
||||||
return enc.RuneToCharcode(rune(unicode))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return enc.RuneToCharcode(r)
|
||||||
common.Log.Debug("Symbol encoding error: unable to find glyph->charcode entry (%s)", glyph)
|
|
||||||
return 0, false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RuneToCharcode converts rune `r` to a PDF character code.
|
// RuneToCharcode converts rune `r` to a PDF character code.
|
||||||
@ -77,7 +72,7 @@ func (enc IdentityEncoder) CharcodeToRune(code uint16) (rune, bool) {
|
|||||||
// RuneToGlyph returns the glyph name for rune `r`.
|
// RuneToGlyph returns the glyph name for rune `r`.
|
||||||
// The bool return flag is true if there was a match, and false otherwise.
|
// The bool return flag is true if there was a match, and false otherwise.
|
||||||
func (enc IdentityEncoder) RuneToGlyph(r rune) (string, bool) {
|
func (enc IdentityEncoder) RuneToGlyph(r rune) (string, bool) {
|
||||||
if r == 0x20 {
|
if r == ' ' {
|
||||||
return "space", true
|
return "space", true
|
||||||
}
|
}
|
||||||
glyph := fmt.Sprintf("uni%.4X", r)
|
glyph := fmt.Sprintf("uni%.4X", r)
|
||||||
@ -88,14 +83,16 @@ func (enc IdentityEncoder) RuneToGlyph(r rune) (string, bool) {
|
|||||||
// The bool return flag is true if there was a match, and false otherwise.
|
// The bool return flag is true if there was a match, and false otherwise.
|
||||||
func (enc IdentityEncoder) GlyphToRune(glyph string) (rune, bool) {
|
func (enc IdentityEncoder) GlyphToRune(glyph string) (rune, bool) {
|
||||||
// String with "uniXXXX" format where XXXX is the hexcode.
|
// String with "uniXXXX" format where XXXX is the hexcode.
|
||||||
if len(glyph) == 7 && glyph[0:3] == "uni" {
|
if glyph == "space" {
|
||||||
unicode := uint16(0)
|
return ' ', true
|
||||||
n, err := fmt.Sscanf(glyph, "uni%X", &unicode)
|
} else if !strings.HasPrefix(glyph, "uni") || len(glyph) != 7 {
|
||||||
if n == 1 && err == nil {
|
return 0, false
|
||||||
return rune(unicode), true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0, false
|
r, err := strconv.ParseUint(glyph[3:], 16, 16)
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return rune(r), true
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToPdfObject returns a nil as it is not truly a PDF object and should not be attempted to store in file.
|
// ToPdfObject returns a nil as it is not truly a PDF object and should not be attempted to store in file.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user