textencoding: simplify code of IdentityEncoder

This commit is contained in:
Denys Smirnov 2018-10-16 02:45:21 +03:00
parent 991aa2727a
commit d06cbae6c5

View File

@ -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,18 +50,12 @@ 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
n, err := fmt.Sscanf(glyph, "uni%X", &unicode)
if n == 1 && err == nil {
return enc.RuneToCharcode(rune(unicode))
}
}
common.Log.Debug("Symbol encoding error: unable to find glyph->charcode entry (%s)", glyph)
return 0, false return 0, false
} }
return enc.RuneToCharcode(r)
}
// RuneToCharcode converts rune `r` to a PDF character code. // RuneToCharcode converts rune `r` to a PDF character code.
// 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.
@ -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,15 +83,17 @@ 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 rune(unicode), true
}
}
return 0, false 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.
func (enc IdentityEncoder) ToPdfObject() core.PdfObject { func (enc IdentityEncoder) ToPdfObject() core.PdfObject {