2017-07-05 23:10:57 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package textencoding
|
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
import (
|
|
|
|
"github.com/unidoc/unidoc/common"
|
2018-07-24 21:32:02 +10:00
|
|
|
"github.com/unidoc/unidoc/pdf/core"
|
2018-06-27 12:25:59 +10:00
|
|
|
)
|
2017-07-05 23:10:57 +00:00
|
|
|
|
|
|
|
type TextEncoder interface {
|
2018-06-27 12:25:59 +10:00
|
|
|
// String returns a string that describes the TextEncoder instance.
|
|
|
|
String() string
|
|
|
|
|
|
|
|
// Encode converts the Go unicode string `raw` to a PDF encoded string.
|
2017-07-05 23:10:57 +00:00
|
|
|
Encode(raw string) string
|
2017-07-10 15:17:46 +00:00
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
// CharcodeToGlyph returns the glyph name for character code `code`.
|
2017-07-10 15:17:46 +00:00
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
2017-09-01 13:20:51 +00:00
|
|
|
CharcodeToGlyph(code uint16) (string, bool)
|
2017-07-10 15:17:46 +00:00
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
// GlyphToCharcode returns the PDF character code corresponding to glyph name `glyph`.
|
2017-07-10 15:17:46 +00:00
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
2017-09-01 13:20:51 +00:00
|
|
|
GlyphToCharcode(glyph string) (uint16, bool)
|
2017-07-10 15:17:46 +00:00
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
// RuneToCharcode returns the PDF character code corresponding to rune `r`.
|
2017-07-10 15:17:46 +00:00
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
2018-06-27 12:25:59 +10:00
|
|
|
// This is usually implemented as RuneToGlyph->GlyphToCharcode
|
|
|
|
RuneToCharcode(r rune) (uint16, bool)
|
2017-07-10 15:17:46 +00:00
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
// CharcodeToRune returns the rune corresponding to character code `code`.
|
2017-07-10 15:17:46 +00:00
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
2018-06-27 12:25:59 +10:00
|
|
|
// This is usually implemented as CharcodeToGlyph->GlyphToRune
|
|
|
|
CharcodeToRune(code uint16) (rune, bool)
|
2017-07-10 15:17:46 +00:00
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
// RuneToGlyph returns the glyph name for rune `r`.
|
2017-07-10 15:17:46 +00:00
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
2018-06-27 12:25:59 +10:00
|
|
|
RuneToGlyph(r rune) (string, bool)
|
2017-07-10 15:17:46 +00:00
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
// GlyphToRune returns the rune corresponding to glyph name `glyph`.
|
2017-07-10 15:17:46 +00:00
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
|
|
|
GlyphToRune(glyph string) (rune, bool)
|
|
|
|
|
2018-06-27 12:25:59 +10:00
|
|
|
// ToPdfObject returns a PDF Object that represents the encoding.
|
2018-07-24 21:32:02 +10:00
|
|
|
ToPdfObject() core.PdfObject
|
2018-06-27 12:25:59 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience functions
|
|
|
|
|
|
|
|
// doEncode converts a Go unicode string `raw` to a PDF encoded string using the encoder `enc`.
|
|
|
|
func doEncode(enc TextEncoder, raw string) string {
|
|
|
|
encoded := []byte{}
|
|
|
|
for _, r := range raw {
|
|
|
|
code, found := enc.RuneToCharcode(r)
|
|
|
|
if !found {
|
2018-08-17 08:41:35 +10:00
|
|
|
common.Log.Debug("Failed to map rune to charcode for rune 0x%04x", r)
|
2018-06-27 12:25:59 +10:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
encoded = append(encoded, byte(code))
|
|
|
|
}
|
|
|
|
return string(encoded)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doRuneToCharcode converts rune `r` to a PDF character code.
|
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
|
|
|
func doRuneToCharcode(enc TextEncoder, r rune) (uint16, bool) {
|
|
|
|
g, ok := enc.RuneToGlyph(r)
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return enc.GlyphToCharcode(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
// doCharcodeToRune converts PDF character code `code` to a rune.
|
|
|
|
// The bool return flag is true if there was a match, and false otherwise.
|
|
|
|
func doCharcodeToRune(enc TextEncoder, code uint16) (rune, bool) {
|
|
|
|
g, ok := enc.CharcodeToGlyph(code)
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return enc.GlyphToRune(g)
|
2017-07-05 23:10:57 +00:00
|
|
|
}
|