mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
Implement standard encoding with SimpleEncoder
This commit is contained in:
parent
4661975304
commit
e6bf6f511a
@ -44,7 +44,6 @@ func newFontFileFromPdfObject(obj PdfObject) (*fontFile, error) {
|
||||
d := streamObj.PdfObjectDictionary
|
||||
data, err := DecodeStream(streamObj)
|
||||
if err != nil {
|
||||
common.Log.Error("err=%v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -53,7 +52,7 @@ func newFontFileFromPdfObject(obj PdfObject) (*fontFile, error) {
|
||||
fontfile.subtype = subtype
|
||||
if subtype == "Type1C" {
|
||||
// XXX: TODO Add Type1C support
|
||||
common.Log.Error("Type1C fonts are currently not supported")
|
||||
common.Log.Debug("Type1C fonts are currently not supported")
|
||||
return nil, ErrFontNotSupported
|
||||
}
|
||||
}
|
||||
@ -114,6 +113,7 @@ func (fontfile *fontFile) parseAsciiPart(data []byte) error {
|
||||
// fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~^^^~~~~~~~~~~~~~~~~~~~~~~~")
|
||||
// fmt.Printf("data=%s\n", string(data))
|
||||
// fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~!!!~~~~~~~~~~~~~~~~~~~~~~~")
|
||||
|
||||
// The start of a FontFile looks like
|
||||
// %!PS-AdobeFont-1.0: MyArial 003.002
|
||||
// %%Title: MyArial
|
||||
@ -136,14 +136,15 @@ func (fontfile *fontFile) parseAsciiPart(data []byte) error {
|
||||
return ErrRequiredAttributeMissing
|
||||
}
|
||||
|
||||
encodingName, ok := keyValues["Encoding"]
|
||||
if ok {
|
||||
encoder, err := textencoding.NewSimpleTextEncoder(encodingName, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fontfile.encoder = encoder
|
||||
}
|
||||
// encodingName, ok := keyValues["Encoding"]
|
||||
// !@#$ I am not sure why we don't do this
|
||||
// if ok {
|
||||
// encoder, err := textencoding.NewSimpleTextEncoder(encodingName, nil)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// fontfile.encoder = encoder
|
||||
// }
|
||||
if encodingSection != "" {
|
||||
encodings, err := getEncodings(encodingSection)
|
||||
if err != nil {
|
||||
@ -151,12 +152,11 @@ func (fontfile *fontFile) parseAsciiPart(data []byte) error {
|
||||
}
|
||||
encoder, err := textencoding.NewCustomSimpleTextEncoder(encodings, nil)
|
||||
if err != nil {
|
||||
// XXX: !@#$ We need to fix all these error
|
||||
common.Log.Error("UNKOWN GLYPH: err=%v", err)
|
||||
// XXX: !@#$ We need to fix all these errors
|
||||
common.Log.Error("UNKNOWN GLYPH: err=%v", err)
|
||||
return nil
|
||||
}
|
||||
fontfile.encoder = encoder
|
||||
common.Log.Debug("encoder=%s", encoder)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -172,6 +172,9 @@ func (fontfile *fontFile) parseAsciiPart(data []byte) error {
|
||||
// data = decoded
|
||||
// }
|
||||
// decoded := decodeEexec(data)
|
||||
// fmt.Println(":::::::::::::::::::::<<>>:::::::::::::::::::::")
|
||||
// fmt.Printf("%s\n", string(decoded))
|
||||
// fmt.Println(":::::::::::::::::::::<><>:::::::::::::::::::::")
|
||||
// return nil
|
||||
// }
|
||||
|
||||
@ -216,9 +219,13 @@ func getAsciiSections(data []byte) (keySection, encodingSection string, err erro
|
||||
return
|
||||
}
|
||||
|
||||
// /Users/pcadmin/testdata/invoice61781040.pdf has \r line endings
|
||||
var reEndline = regexp.MustCompile(`[\n\r]+`)
|
||||
|
||||
// getKeyValues returns the map encoded in `data`.
|
||||
func getKeyValues(data string) map[string]string {
|
||||
lines := strings.Split(data, "\n")
|
||||
// lines := strings.Split(data, "\n")
|
||||
lines := reEndline.Split(data, -1)
|
||||
keyValues := map[string]string{}
|
||||
for _, line := range lines {
|
||||
matches := reKeyVal.FindStringSubmatch(line)
|
||||
|
@ -29,7 +29,7 @@ var (
|
||||
|
||||
// SimpleEncoder represents a 1 byte encoding
|
||||
type SimpleEncoder struct {
|
||||
baseName string
|
||||
baseName string
|
||||
baseEncoding map[uint16]rune
|
||||
differences map[byte]string
|
||||
codeToGlyph map[uint16]string
|
||||
@ -54,11 +54,10 @@ func NewCustomSimpleTextEncoder(encoding map[uint16]string, differences map[byte
|
||||
return newSimpleTextEncoder(baseEncoding, baseName, differences)
|
||||
}
|
||||
|
||||
// Encode converts a Go unicode string `raw` to a PDF encoded string.
|
||||
// ApplyDifferences applies the encoding delta `differences` to `se`.
|
||||
func (se *SimpleEncoder) ApplyDifferences(differences map[byte]string) {
|
||||
se.differences = differences
|
||||
se.makeEncoder()
|
||||
common.Log.Debug("$$$$ %s", se)
|
||||
}
|
||||
|
||||
// NewSimpleTextEncoder returns a SimpleEncoder based on predefined encoding `baseName` and
|
||||
@ -179,8 +178,6 @@ func (se SimpleEncoder) ToPdfObject() PdfObject {
|
||||
dict.Set("Type", MakeName("Encoding"))
|
||||
dict.Set("BaseEncoding", MakeName(se.baseName))
|
||||
dict.Set("Differences", MakeArray(ToFontDifferences(se.differences)...))
|
||||
|
||||
// Return an empty Encoding object
|
||||
return MakeIndirectObject(dict)
|
||||
}
|
||||
|
||||
|
@ -5,501 +5,7 @@
|
||||
|
||||
package textencoding
|
||||
|
||||
import (
|
||||
"github.com/unidoc/unidoc/common"
|
||||
. "github.com/unidoc/unidoc/pdf/core"
|
||||
)
|
||||
|
||||
// Encoding for Symbol font.
|
||||
type SymbolEncoder struct {
|
||||
}
|
||||
|
||||
func NewSymbolEncoder() SymbolEncoder {
|
||||
encoder := SymbolEncoder{}
|
||||
return encoder
|
||||
}
|
||||
|
||||
// String returns a string that describes `se`.
|
||||
func (se SymbolEncoder) String() string {
|
||||
return "SymbolEncoder"
|
||||
}
|
||||
|
||||
// Encode converts the Go unicode string `raw` to a PDF encoded string.
|
||||
func (enc SymbolEncoder) Encode(raw string) string {
|
||||
encoded := []byte{}
|
||||
for _, rune := range raw {
|
||||
code, found := enc.RuneToCharcode(rune)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
encoded = append(encoded, byte(code))
|
||||
}
|
||||
|
||||
return string(encoded)
|
||||
}
|
||||
|
||||
// CharcodeToGlyph returns the glyph name for character code `code`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc SymbolEncoder) CharcodeToGlyph(code uint16) (string, bool) {
|
||||
glyph, has := symbolEncodingCharcodeToGlyphMap[code]
|
||||
if !has {
|
||||
common.Log.Debug("Symbol encoding error: unable to find charcode->glyph entry (%v)", code)
|
||||
return "", false
|
||||
}
|
||||
return glyph, true
|
||||
}
|
||||
|
||||
// GlyphToCharcode returns the PDF character code corresponding to glyph name `glyph`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc SymbolEncoder) GlyphToCharcode(glyph string) (uint16, bool) {
|
||||
code, found := symbolEncodingGlyphToCharcodeMap[glyph]
|
||||
if !found {
|
||||
common.Log.Debug("Symbol encoding error: unable to find glyph->charcode entry (%s)", glyph)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return code, found
|
||||
}
|
||||
|
||||
// RuneToCharcode returns the PDF character code corresponding to rune `r`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc SymbolEncoder) RuneToCharcode(r rune) (uint16, bool) {
|
||||
glyph, found := runeToGlyph(r, glyphlistRuneToGlyphMap)
|
||||
if !found {
|
||||
common.Log.Debug("Symbol encoding error: unable to find rune->glyph entry (%+q)", r)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
code, found := symbolEncodingGlyphToCharcodeMap[glyph]
|
||||
if !found {
|
||||
common.Log.Debug("Symbol encoding error: unable to find glyph->charcode entry (%s)", glyph)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return code, true
|
||||
}
|
||||
|
||||
// CharcodeToRune returns the rune corresponding to character code `code`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc SymbolEncoder) CharcodeToRune(code uint16) (rune, bool) {
|
||||
glyph, found := symbolEncodingCharcodeToGlyphMap[code]
|
||||
if !found {
|
||||
common.Log.Debug("Symbol encoding error: unable to find charcode->glyph entry (%d)", code)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
val, found := glyphToRune(glyph, glyphlistGlyphToRuneMap)
|
||||
if !found {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return val, true
|
||||
}
|
||||
|
||||
// RuneToGlyph returns the glyph name for rune `r`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc SymbolEncoder) RuneToGlyph(val rune) (string, bool) {
|
||||
return runeToGlyph(val, glyphlistRuneToGlyphMap)
|
||||
}
|
||||
|
||||
// GlyphToRune returns the rune corresponding to glyph name `glyph`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc SymbolEncoder) GlyphToRune(glyph string) (rune, bool) {
|
||||
return glyphToRune(glyph, glyphlistGlyphToRuneMap)
|
||||
}
|
||||
|
||||
// ToPdfObject returns a PDF Object that represents `enc`.
|
||||
func (enc SymbolEncoder) ToPdfObject() PdfObject {
|
||||
dict := MakeDict()
|
||||
dict.Set("Type", MakeName("Encoding"))
|
||||
|
||||
// Returning an empty Encoding object with no differences. Indicates that we are using the
|
||||
// font's built-in encoding.
|
||||
return MakeIndirectObject(dict)
|
||||
}
|
||||
|
||||
// Charcode to Glyph map (Symbol encoding)
|
||||
var symbolEncodingCharcodeToGlyphMap map[uint16]string = map[uint16]string{
|
||||
32: "space",
|
||||
33: "exclam",
|
||||
34: "universal",
|
||||
35: "numbersign",
|
||||
36: "existential",
|
||||
37: "percent",
|
||||
38: "ampersand",
|
||||
39: "suchthat",
|
||||
40: "parenleft",
|
||||
41: "parenright",
|
||||
42: "asteriskmath",
|
||||
43: "plus",
|
||||
44: "comma",
|
||||
45: "minus",
|
||||
46: "period",
|
||||
47: "slash",
|
||||
48: "zero",
|
||||
49: "one",
|
||||
50: "two",
|
||||
51: "three",
|
||||
52: "four",
|
||||
53: "five",
|
||||
54: "six",
|
||||
55: "seven",
|
||||
56: "eight",
|
||||
57: "nine",
|
||||
58: "colon",
|
||||
59: "semicolon",
|
||||
60: "less",
|
||||
61: "equal",
|
||||
62: "greater",
|
||||
63: "question",
|
||||
64: "congruent",
|
||||
65: "Alpha",
|
||||
66: "Beta",
|
||||
67: "Chi",
|
||||
68: "Delta",
|
||||
69: "Epsilon",
|
||||
70: "Phi",
|
||||
71: "Gamma",
|
||||
72: "Eta",
|
||||
73: "Iota",
|
||||
74: "theta1",
|
||||
75: "Kappa",
|
||||
76: "Lambda",
|
||||
77: "Mu",
|
||||
78: "Nu",
|
||||
79: "Omicron",
|
||||
80: "Pi",
|
||||
81: "Theta",
|
||||
82: "Rho",
|
||||
83: "Sigma",
|
||||
84: "Tau",
|
||||
85: "Upsilon",
|
||||
86: "sigma1",
|
||||
87: "Omega",
|
||||
88: "Xi",
|
||||
89: "Psi",
|
||||
90: "Zeta",
|
||||
91: "bracketleft",
|
||||
92: "therefore",
|
||||
93: "bracketright",
|
||||
94: "perpendicular",
|
||||
95: "underscore",
|
||||
96: "radicalex",
|
||||
97: "alpha",
|
||||
98: "beta",
|
||||
99: "chi",
|
||||
100: "delta",
|
||||
101: "epsilon",
|
||||
102: "phi",
|
||||
103: "gamma",
|
||||
104: "eta",
|
||||
105: "iota",
|
||||
106: "phi1",
|
||||
107: "kappa",
|
||||
108: "lambda",
|
||||
109: "mu",
|
||||
110: "nu",
|
||||
111: "omicron",
|
||||
112: "pi",
|
||||
113: "theta",
|
||||
114: "rho",
|
||||
115: "sigma",
|
||||
116: "tau",
|
||||
117: "upsilon",
|
||||
118: "omega1",
|
||||
119: "omega",
|
||||
120: "xi",
|
||||
121: "psi",
|
||||
122: "zeta",
|
||||
123: "braceleft",
|
||||
124: "bar",
|
||||
125: "braceright",
|
||||
126: "similar",
|
||||
160: "Euro",
|
||||
161: "Upsilon1",
|
||||
162: "minute",
|
||||
163: "lessequal",
|
||||
164: "fraction",
|
||||
165: "infinity",
|
||||
166: "florin",
|
||||
167: "club",
|
||||
168: "diamond",
|
||||
169: "heart",
|
||||
170: "spade",
|
||||
171: "arrowboth",
|
||||
172: "arrowleft",
|
||||
173: "arrowup",
|
||||
174: "arrowright",
|
||||
175: "arrowdown",
|
||||
176: "degree",
|
||||
177: "plusminus",
|
||||
178: "second",
|
||||
179: "greaterequal",
|
||||
180: "multiply",
|
||||
181: "proportional",
|
||||
182: "partialdiff",
|
||||
183: "bullet",
|
||||
184: "divide",
|
||||
185: "notequal",
|
||||
186: "equivalence",
|
||||
187: "approxequal",
|
||||
188: "ellipsis",
|
||||
189: "arrowvertex",
|
||||
190: "arrowhorizex",
|
||||
191: "carriagereturn",
|
||||
192: "aleph",
|
||||
193: "Ifraktur",
|
||||
194: "Rfraktur",
|
||||
195: "weierstrass",
|
||||
196: "circlemultiply",
|
||||
197: "circleplus",
|
||||
198: "emptyset",
|
||||
199: "intersection",
|
||||
200: "union",
|
||||
201: "propersuperset",
|
||||
202: "reflexsuperset",
|
||||
203: "notsubset",
|
||||
204: "propersubset",
|
||||
205: "reflexsubset",
|
||||
206: "element",
|
||||
207: "notelement",
|
||||
208: "angle",
|
||||
209: "gradient",
|
||||
210: "registerserif",
|
||||
211: "copyrightserif",
|
||||
212: "trademarkserif",
|
||||
213: "product",
|
||||
214: "radical",
|
||||
215: "dotmath",
|
||||
216: "logicalnot",
|
||||
217: "logicaland",
|
||||
218: "logicalor",
|
||||
219: "arrowdblboth",
|
||||
220: "arrowdblleft",
|
||||
221: "arrowdblup",
|
||||
222: "arrowdblright",
|
||||
223: "arrowdbldown",
|
||||
224: "lozenge",
|
||||
225: "angleleft",
|
||||
226: "registersans",
|
||||
227: "copyrightsans",
|
||||
228: "trademarksans",
|
||||
229: "summation",
|
||||
230: "parenlefttp",
|
||||
231: "parenleftex",
|
||||
232: "parenleftbt",
|
||||
233: "bracketlefttp",
|
||||
234: "bracketleftex",
|
||||
235: "bracketleftbt",
|
||||
236: "bracelefttp",
|
||||
237: "braceleftmid",
|
||||
238: "braceleftbt",
|
||||
239: "braceex",
|
||||
241: "angleright",
|
||||
242: "integral",
|
||||
243: "integraltp",
|
||||
244: "integralex",
|
||||
245: "integralbt",
|
||||
246: "parenrighttp",
|
||||
247: "parenrightex",
|
||||
248: "parenrightbt",
|
||||
249: "bracketrighttp",
|
||||
250: "bracketrightex",
|
||||
251: "bracketrightbt",
|
||||
252: "bracerighttp",
|
||||
253: "bracerightmid",
|
||||
254: "bracerightbt",
|
||||
}
|
||||
|
||||
// Glyph to charcode map (Symbol encoding).
|
||||
var symbolEncodingGlyphToCharcodeMap map[string]uint16 = map[string]uint16{
|
||||
"space": 32,
|
||||
"exclam": 33,
|
||||
"universal": 34,
|
||||
"numbersign": 35,
|
||||
"existential": 36,
|
||||
"percent": 37,
|
||||
"ampersand": 38,
|
||||
"suchthat": 39,
|
||||
"parenleft": 40,
|
||||
"parenright": 41,
|
||||
"asteriskmath": 42,
|
||||
"plus": 43,
|
||||
"comma": 44,
|
||||
"minus": 45,
|
||||
"period": 46,
|
||||
"slash": 47,
|
||||
"zero": 48,
|
||||
"one": 49,
|
||||
"two": 50,
|
||||
"three": 51,
|
||||
"four": 52,
|
||||
"five": 53,
|
||||
"six": 54,
|
||||
"seven": 55,
|
||||
"eight": 56,
|
||||
"nine": 57,
|
||||
"colon": 58,
|
||||
"semicolon": 59,
|
||||
"less": 60,
|
||||
"equal": 61,
|
||||
"greater": 62,
|
||||
"question": 63,
|
||||
"congruent": 64,
|
||||
"Alpha": 65,
|
||||
"Beta": 66,
|
||||
"Chi": 67,
|
||||
"Delta": 68,
|
||||
"Epsilon": 69,
|
||||
"Phi": 70,
|
||||
"Gamma": 71,
|
||||
"Eta": 72,
|
||||
"Iota": 73,
|
||||
"theta1": 74,
|
||||
"Kappa": 75,
|
||||
"Lambda": 76,
|
||||
"Mu": 77,
|
||||
"Nu": 78,
|
||||
"Omicron": 79,
|
||||
"Pi": 80,
|
||||
"Theta": 81,
|
||||
"Rho": 82,
|
||||
"Sigma": 83,
|
||||
"Tau": 84,
|
||||
"Upsilon": 85,
|
||||
"sigma1": 86,
|
||||
"Omega": 87,
|
||||
"Xi": 88,
|
||||
"Psi": 89,
|
||||
"Zeta": 90,
|
||||
"bracketleft": 91,
|
||||
"therefore": 92,
|
||||
"bracketright": 93,
|
||||
"perpendicular": 94,
|
||||
"underscore": 95,
|
||||
"radicalex": 96,
|
||||
"alpha": 97,
|
||||
"beta": 98,
|
||||
"chi": 99,
|
||||
"delta": 100,
|
||||
"epsilon": 101,
|
||||
"phi": 102,
|
||||
"gamma": 103,
|
||||
"eta": 104,
|
||||
"iota": 105,
|
||||
"phi1": 106,
|
||||
"kappa": 107,
|
||||
"lambda": 108,
|
||||
"mu": 109,
|
||||
"nu": 110,
|
||||
"omicron": 111,
|
||||
"pi": 112,
|
||||
"theta": 113,
|
||||
"rho": 114,
|
||||
"sigma": 115,
|
||||
"tau": 116,
|
||||
"upsilon": 117,
|
||||
"omega1": 118,
|
||||
"omega": 119,
|
||||
"xi": 120,
|
||||
"psi": 121,
|
||||
"zeta": 122,
|
||||
"braceleft": 123,
|
||||
"bar": 124,
|
||||
"braceright": 125,
|
||||
"similar": 126,
|
||||
"Euro": 160,
|
||||
"Upsilon1": 161,
|
||||
"minute": 162,
|
||||
"lessequal": 163,
|
||||
"fraction": 164,
|
||||
"infinity": 165,
|
||||
"florin": 166,
|
||||
"club": 167,
|
||||
"diamond": 168,
|
||||
"heart": 169,
|
||||
"spade": 170,
|
||||
"arrowboth": 171,
|
||||
"arrowleft": 172,
|
||||
"arrowup": 173,
|
||||
"arrowright": 174,
|
||||
"arrowdown": 175,
|
||||
"degree": 176,
|
||||
"plusminus": 177,
|
||||
"second": 178,
|
||||
"greaterequal": 179,
|
||||
"multiply": 180,
|
||||
"proportional": 181,
|
||||
"partialdiff": 182,
|
||||
"bullet": 183,
|
||||
"divide": 184,
|
||||
"notequal": 185,
|
||||
"equivalence": 186,
|
||||
"approxequal": 187,
|
||||
"ellipsis": 188,
|
||||
"arrowvertex": 189,
|
||||
"arrowhorizex": 190,
|
||||
"carriagereturn": 191,
|
||||
"aleph": 192,
|
||||
"Ifraktur": 193,
|
||||
"Rfraktur": 194,
|
||||
"weierstrass": 195,
|
||||
"circlemultiply": 196,
|
||||
"circleplus": 197,
|
||||
"emptyset": 198,
|
||||
"intersection": 199,
|
||||
"union": 200,
|
||||
"propersuperset": 201,
|
||||
"reflexsuperset": 202,
|
||||
"notsubset": 203,
|
||||
"propersubset": 204,
|
||||
"reflexsubset": 205,
|
||||
"element": 206,
|
||||
"notelement": 207,
|
||||
"angle": 208,
|
||||
"gradient": 209,
|
||||
"registerserif": 210,
|
||||
"copyrightserif": 211,
|
||||
"trademarkserif": 212,
|
||||
"product": 213,
|
||||
"radical": 214,
|
||||
"dotmath": 215,
|
||||
"logicalnot": 216,
|
||||
"logicaland": 217,
|
||||
"logicalor": 218,
|
||||
"arrowdblboth": 219,
|
||||
"arrowdblleft": 220,
|
||||
"arrowdblup": 221,
|
||||
"arrowdblright": 222,
|
||||
"arrowdbldown": 223,
|
||||
"lozenge": 224,
|
||||
"angleleft": 225,
|
||||
"registersans": 226,
|
||||
"copyrightsans": 227,
|
||||
"trademarksans": 228,
|
||||
"summation": 229,
|
||||
"parenlefttp": 230,
|
||||
"parenleftex": 231,
|
||||
"parenleftbt": 232,
|
||||
"bracketlefttp": 233,
|
||||
"bracketleftex": 234,
|
||||
"bracketleftbt": 235,
|
||||
"bracelefttp": 236,
|
||||
"braceleftmid": 237,
|
||||
"braceleftbt": 238,
|
||||
"braceex": 239,
|
||||
"angleright": 241,
|
||||
"integral": 242,
|
||||
"integraltp": 243,
|
||||
"integralex": 244,
|
||||
"integralbt": 245,
|
||||
"parenrighttp": 246,
|
||||
"parenrightex": 247,
|
||||
"parenrightbt": 248,
|
||||
"bracketrighttp": 249,
|
||||
"bracketrightex": 250,
|
||||
"bracketrightbt": 251,
|
||||
"bracerighttp": 252,
|
||||
"bracerightmid": 253,
|
||||
"bracerightbt": 254,
|
||||
func NewSymbolEncoder() SimpleEncoder {
|
||||
enc, _ := NewSimpleTextEncoder("SymbolEncoding", nil)
|
||||
return enc
|
||||
}
|
||||
|
@ -149,5 +149,5 @@ func (enc TrueTypeFontEncoder) GlyphToRune(glyph string) (rune, bool) {
|
||||
|
||||
// ToPdfObject returns a nil as it is not truly a PDF object and should not be attempted to store in file.
|
||||
func (enc TrueTypeFontEncoder) ToPdfObject() PdfObject {
|
||||
return nil
|
||||
return MakeNull()
|
||||
}
|
||||
|
@ -5,543 +5,7 @@
|
||||
|
||||
package textencoding
|
||||
|
||||
import (
|
||||
"github.com/unidoc/unidoc/common"
|
||||
. "github.com/unidoc/unidoc/pdf/core"
|
||||
)
|
||||
|
||||
// Encoding for ZapfDingbats font.
|
||||
type ZapfDingbatsEncoder struct {
|
||||
}
|
||||
|
||||
func NewZapfDingbatsEncoder() ZapfDingbatsEncoder {
|
||||
return ZapfDingbatsEncoder{}
|
||||
}
|
||||
|
||||
// String returns a string that describes `enc`.
|
||||
func (enc ZapfDingbatsEncoder) String() string {
|
||||
return "ZapfDingbatsEncoder"
|
||||
}
|
||||
|
||||
// Encode converts the Go unicode string `raw` to a PDF encoded string.
|
||||
func (enc ZapfDingbatsEncoder) Encode(raw string) string {
|
||||
encoded := []byte{}
|
||||
for _, rune := range raw {
|
||||
code, found := enc.RuneToCharcode(rune)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
|
||||
encoded = append(encoded, byte(code))
|
||||
}
|
||||
|
||||
return string(encoded)
|
||||
}
|
||||
|
||||
// CharcodeToGlyph returns the glyph name for character code `code`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc ZapfDingbatsEncoder) CharcodeToGlyph(code uint16) (string, bool) {
|
||||
glyph, has := zapfDingbatsEncodingCharcodeToGlyphMap[code]
|
||||
if !has {
|
||||
common.Log.Debug("ZapfDingbats encoding error: unable to find charcode->glyph entry (%v)", code)
|
||||
return "", false
|
||||
}
|
||||
return glyph, true
|
||||
}
|
||||
|
||||
// GlyphToCharcode returns the PDF character code corresponding to glyph name `glyph`.
|
||||
// Conversion between glyph name and character code.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc ZapfDingbatsEncoder) GlyphToCharcode(glyph string) (uint16, bool) {
|
||||
code, found := zapfDingbatsEncodingGlyphToCharcodeMap[glyph]
|
||||
if !found {
|
||||
common.Log.Debug("ZapfDingbats encoding error: unable to find glyph->charcode entry (%s)", glyph)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return code, found
|
||||
}
|
||||
|
||||
// RuneToCharcode returns the PDF character code corresponding to rune `r`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc ZapfDingbatsEncoder) RuneToCharcode(val rune) (uint16, bool) {
|
||||
glyph, found := enc.RuneToGlyph(val)
|
||||
if !found {
|
||||
common.Log.Debug("ZapfDingbats encoding error: unable to find rune->glyph entry (%v)", val)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
code, found := zapfDingbatsEncodingGlyphToCharcodeMap[glyph]
|
||||
if !found {
|
||||
common.Log.Debug("ZapfDingbats encoding error: unable to find glyph->charcode entry (%s)", glyph)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return code, true
|
||||
}
|
||||
|
||||
// CharcodeToRune returns the rune corresponding to character code `code`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc ZapfDingbatsEncoder) CharcodeToRune(charcode uint16) (rune, bool) {
|
||||
glyph, found := zapfDingbatsEncodingCharcodeToGlyphMap[charcode]
|
||||
if !found {
|
||||
common.Log.Debug("ZapfDingbats encoding error: unable to find charcode->glyph entry (%d)", charcode)
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return enc.GlyphToRune(glyph)
|
||||
}
|
||||
|
||||
// RuneToCharcode returns the PDF character code corresponding to rune `r`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc ZapfDingbatsEncoder) RuneToGlyph(val rune) (string, bool) {
|
||||
// Seek in the zapfdingbats list first.
|
||||
glyph, found := runeToGlyph(val, zapfdingbatsRuneToGlyphMap)
|
||||
if !found {
|
||||
// Then revert to glyphlist if not found.
|
||||
glyph, found = runeToGlyph(val, glyphlistRuneToGlyphMap)
|
||||
if !found {
|
||||
common.Log.Debug("ZapfDingbats encoding error: unable to find rune->glyph entry (%v)", val)
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
return glyph, true
|
||||
}
|
||||
|
||||
// GlyphToRune returns the rune corresponding to glyph name `glyph`.
|
||||
// The bool return flag is true if there was a match, and false otherwise.
|
||||
func (enc ZapfDingbatsEncoder) GlyphToRune(glyph string) (rune, bool) {
|
||||
// Seek in the zapfdingbats list first.
|
||||
val, found := glyphToRune(glyph, zapfdingbatsGlyphToRuneMap)
|
||||
if !found {
|
||||
// Then revert to glyphlist if not found.
|
||||
val, found = glyphToRune(glyph, glyphlistGlyphToRuneMap)
|
||||
if !found {
|
||||
common.Log.Debug("Symbol encoding error: unable to find glyph->rune entry (%v)", glyph)
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
return val, true
|
||||
}
|
||||
|
||||
// ToPdfObject returns a PDF Object that represents the encoding.
|
||||
func (enc ZapfDingbatsEncoder) ToPdfObject() PdfObject {
|
||||
dict := MakeDict()
|
||||
dict.Set("Type", MakeName("Encoding"))
|
||||
|
||||
// Returning an empty Encoding object with no differences. Indicates that we are using the
|
||||
// font's built-in encoding.
|
||||
return MakeIndirectObject(dict)
|
||||
}
|
||||
|
||||
var zapfDingbatsEncodingCharcodeToGlyphMap = map[uint16]string{
|
||||
32: "space",
|
||||
33: "a1",
|
||||
34: "a2",
|
||||
35: "a202",
|
||||
36: "a3",
|
||||
37: "a4",
|
||||
38: "a5",
|
||||
39: "a119",
|
||||
40: "a118",
|
||||
41: "a117",
|
||||
42: "a11",
|
||||
43: "a12",
|
||||
44: "a13",
|
||||
45: "a14",
|
||||
46: "a15",
|
||||
47: "a16",
|
||||
48: "a105",
|
||||
49: "a17",
|
||||
50: "a18",
|
||||
51: "a19",
|
||||
52: "a20",
|
||||
53: "a21",
|
||||
54: "a22",
|
||||
55: "a23",
|
||||
56: "a24",
|
||||
57: "a25",
|
||||
58: "a26",
|
||||
59: "a27",
|
||||
60: "a28",
|
||||
61: "a6",
|
||||
62: "a7",
|
||||
63: "a8",
|
||||
64: "a9",
|
||||
65: "a10",
|
||||
66: "a29",
|
||||
67: "a30",
|
||||
68: "a31",
|
||||
69: "a32",
|
||||
70: "a33",
|
||||
71: "a34",
|
||||
72: "a35",
|
||||
73: "a36",
|
||||
74: "a37",
|
||||
75: "a38",
|
||||
76: "a39",
|
||||
77: "a40",
|
||||
78: "a41",
|
||||
79: "a42",
|
||||
80: "a43",
|
||||
81: "a44",
|
||||
82: "a45",
|
||||
83: "a46",
|
||||
84: "a47",
|
||||
85: "a48",
|
||||
86: "a49",
|
||||
87: "a50",
|
||||
88: "a51",
|
||||
89: "a52",
|
||||
90: "a53",
|
||||
91: "a54",
|
||||
92: "a55",
|
||||
93: "a56",
|
||||
94: "a57",
|
||||
95: "a58",
|
||||
96: "a59",
|
||||
97: "a60",
|
||||
98: "a61",
|
||||
99: "a62",
|
||||
100: "a63",
|
||||
101: "a64",
|
||||
102: "a65",
|
||||
103: "a66",
|
||||
104: "a67",
|
||||
105: "a68",
|
||||
106: "a69",
|
||||
107: "a70",
|
||||
108: "a71",
|
||||
109: "a72",
|
||||
110: "a73",
|
||||
111: "a74",
|
||||
112: "a203",
|
||||
113: "a75",
|
||||
114: "a204",
|
||||
115: "a76",
|
||||
116: "a77",
|
||||
117: "a78",
|
||||
118: "a79",
|
||||
119: "a81",
|
||||
120: "a82",
|
||||
121: "a83",
|
||||
122: "a84",
|
||||
123: "a97",
|
||||
124: "a98",
|
||||
125: "a99",
|
||||
126: "a100",
|
||||
128: "a89",
|
||||
129: "a90",
|
||||
130: "a93",
|
||||
131: "a94",
|
||||
132: "a91",
|
||||
133: "a92",
|
||||
134: "a205",
|
||||
135: "a85",
|
||||
136: "a206",
|
||||
137: "a86",
|
||||
138: "a87",
|
||||
139: "a88",
|
||||
140: "a95",
|
||||
141: "a96",
|
||||
161: "a101",
|
||||
162: "a102",
|
||||
163: "a103",
|
||||
164: "a104",
|
||||
165: "a106",
|
||||
166: "a107",
|
||||
167: "a108",
|
||||
168: "a112",
|
||||
169: "a111",
|
||||
170: "a110",
|
||||
171: "a109",
|
||||
172: "a120",
|
||||
173: "a121",
|
||||
174: "a122",
|
||||
175: "a123",
|
||||
176: "a124",
|
||||
177: "a125",
|
||||
178: "a126",
|
||||
179: "a127",
|
||||
180: "a128",
|
||||
181: "a129",
|
||||
182: "a130",
|
||||
183: "a131",
|
||||
184: "a132",
|
||||
185: "a133",
|
||||
186: "a134",
|
||||
187: "a135",
|
||||
188: "a136",
|
||||
189: "a137",
|
||||
190: "a138",
|
||||
191: "a139",
|
||||
192: "a140",
|
||||
193: "a141",
|
||||
194: "a142",
|
||||
195: "a143",
|
||||
196: "a144",
|
||||
197: "a145",
|
||||
198: "a146",
|
||||
199: "a147",
|
||||
200: "a148",
|
||||
201: "a149",
|
||||
202: "a150",
|
||||
203: "a151",
|
||||
204: "a152",
|
||||
205: "a153",
|
||||
206: "a154",
|
||||
207: "a155",
|
||||
208: "a156",
|
||||
209: "a157",
|
||||
210: "a158",
|
||||
211: "a159",
|
||||
212: "a160",
|
||||
213: "a161",
|
||||
214: "a163",
|
||||
215: "a164",
|
||||
216: "a196",
|
||||
217: "a165",
|
||||
218: "a192",
|
||||
219: "a166",
|
||||
220: "a167",
|
||||
221: "a168",
|
||||
222: "a169",
|
||||
223: "a170",
|
||||
224: "a171",
|
||||
225: "a172",
|
||||
226: "a173",
|
||||
227: "a162",
|
||||
228: "a174",
|
||||
229: "a175",
|
||||
230: "a176",
|
||||
231: "a177",
|
||||
232: "a178",
|
||||
233: "a179",
|
||||
234: "a193",
|
||||
235: "a180",
|
||||
236: "a199",
|
||||
237: "a181",
|
||||
238: "a200",
|
||||
239: "a182",
|
||||
241: "a201",
|
||||
242: "a183",
|
||||
243: "a184",
|
||||
244: "a197",
|
||||
245: "a185",
|
||||
246: "a194",
|
||||
247: "a198",
|
||||
248: "a186",
|
||||
249: "a195",
|
||||
250: "a187",
|
||||
251: "a188",
|
||||
252: "a189",
|
||||
253: "a190",
|
||||
254: "a191",
|
||||
}
|
||||
|
||||
var zapfDingbatsEncodingGlyphToCharcodeMap = map[string]uint16{
|
||||
"space": 32,
|
||||
"a1": 33,
|
||||
"a2": 34,
|
||||
"a202": 35,
|
||||
"a3": 36,
|
||||
"a4": 37,
|
||||
"a5": 38,
|
||||
"a119": 39,
|
||||
"a118": 40,
|
||||
"a117": 41,
|
||||
"a11": 42,
|
||||
"a12": 43,
|
||||
"a13": 44,
|
||||
"a14": 45,
|
||||
"a15": 46,
|
||||
"a16": 47,
|
||||
"a105": 48,
|
||||
"a17": 49,
|
||||
"a18": 50,
|
||||
"a19": 51,
|
||||
"a20": 52,
|
||||
"a21": 53,
|
||||
"a22": 54,
|
||||
"a23": 55,
|
||||
"a24": 56,
|
||||
"a25": 57,
|
||||
"a26": 58,
|
||||
"a27": 59,
|
||||
"a28": 60,
|
||||
"a6": 61,
|
||||
"a7": 62,
|
||||
"a8": 63,
|
||||
"a9": 64,
|
||||
"a10": 65,
|
||||
"a29": 66,
|
||||
"a30": 67,
|
||||
"a31": 68,
|
||||
"a32": 69,
|
||||
"a33": 70,
|
||||
"a34": 71,
|
||||
"a35": 72,
|
||||
"a36": 73,
|
||||
"a37": 74,
|
||||
"a38": 75,
|
||||
"a39": 76,
|
||||
"a40": 77,
|
||||
"a41": 78,
|
||||
"a42": 79,
|
||||
"a43": 80,
|
||||
"a44": 81,
|
||||
"a45": 82,
|
||||
"a46": 83,
|
||||
"a47": 84,
|
||||
"a48": 85,
|
||||
"a49": 86,
|
||||
"a50": 87,
|
||||
"a51": 88,
|
||||
"a52": 89,
|
||||
"a53": 90,
|
||||
"a54": 91,
|
||||
"a55": 92,
|
||||
"a56": 93,
|
||||
"a57": 94,
|
||||
"a58": 95,
|
||||
"a59": 96,
|
||||
"a60": 97,
|
||||
"a61": 98,
|
||||
"a62": 99,
|
||||
"a63": 100,
|
||||
"a64": 101,
|
||||
"a65": 102,
|
||||
"a66": 103,
|
||||
"a67": 104,
|
||||
"a68": 105,
|
||||
"a69": 106,
|
||||
"a70": 107,
|
||||
"a71": 108,
|
||||
"a72": 109,
|
||||
"a73": 110,
|
||||
"a74": 111,
|
||||
"a203": 112,
|
||||
"a75": 113,
|
||||
"a204": 114,
|
||||
"a76": 115,
|
||||
"a77": 116,
|
||||
"a78": 117,
|
||||
"a79": 118,
|
||||
"a81": 119,
|
||||
"a82": 120,
|
||||
"a83": 121,
|
||||
"a84": 122,
|
||||
"a97": 123,
|
||||
"a98": 124,
|
||||
"a99": 125,
|
||||
"a100": 126,
|
||||
"a89": 128,
|
||||
"a90": 129,
|
||||
"a93": 130,
|
||||
"a94": 131,
|
||||
"a91": 132,
|
||||
"a92": 133,
|
||||
"a205": 134,
|
||||
"a85": 135,
|
||||
"a206": 136,
|
||||
"a86": 137,
|
||||
"a87": 138,
|
||||
"a88": 139,
|
||||
"a95": 140,
|
||||
"a96": 141,
|
||||
"a101": 161,
|
||||
"a102": 162,
|
||||
"a103": 163,
|
||||
"a104": 164,
|
||||
"a106": 165,
|
||||
"a107": 166,
|
||||
"a108": 167,
|
||||
"a112": 168,
|
||||
"a111": 169,
|
||||
"a110": 170,
|
||||
"a109": 171,
|
||||
"a120": 172,
|
||||
"a121": 173,
|
||||
"a122": 174,
|
||||
"a123": 175,
|
||||
"a124": 176,
|
||||
"a125": 177,
|
||||
"a126": 178,
|
||||
"a127": 179,
|
||||
"a128": 180,
|
||||
"a129": 181,
|
||||
"a130": 182,
|
||||
"a131": 183,
|
||||
"a132": 184,
|
||||
"a133": 185,
|
||||
"a134": 186,
|
||||
"a135": 187,
|
||||
"a136": 188,
|
||||
"a137": 189,
|
||||
"a138": 190,
|
||||
"a139": 191,
|
||||
"a140": 192,
|
||||
"a141": 193,
|
||||
"a142": 194,
|
||||
"a143": 195,
|
||||
"a144": 196,
|
||||
"a145": 197,
|
||||
"a146": 198,
|
||||
"a147": 199,
|
||||
"a148": 200,
|
||||
"a149": 201,
|
||||
"a150": 202,
|
||||
"a151": 203,
|
||||
"a152": 204,
|
||||
"a153": 205,
|
||||
"a154": 206,
|
||||
"a155": 207,
|
||||
"a156": 208,
|
||||
"a157": 209,
|
||||
"a158": 210,
|
||||
"a159": 211,
|
||||
"a160": 212,
|
||||
"a161": 213,
|
||||
"a163": 214,
|
||||
"a164": 215,
|
||||
"a196": 216,
|
||||
"a165": 217,
|
||||
"a192": 218,
|
||||
"a166": 219,
|
||||
"a167": 220,
|
||||
"a168": 221,
|
||||
"a169": 222,
|
||||
"a170": 223,
|
||||
"a171": 224,
|
||||
"a172": 225,
|
||||
"a173": 226,
|
||||
"a162": 227,
|
||||
"a174": 228,
|
||||
"a175": 229,
|
||||
"a176": 230,
|
||||
"a177": 231,
|
||||
"a178": 232,
|
||||
"a179": 233,
|
||||
"a193": 234,
|
||||
"a180": 235,
|
||||
"a199": 236,
|
||||
"a181": 237,
|
||||
"a200": 238,
|
||||
"a182": 239,
|
||||
"a201": 241,
|
||||
"a183": 242,
|
||||
"a184": 243,
|
||||
"a197": 244,
|
||||
"a185": 245,
|
||||
"a194": 246,
|
||||
"a198": 247,
|
||||
"a186": 248,
|
||||
"a195": 249,
|
||||
"a187": 250,
|
||||
"a188": 251,
|
||||
"a189": 252,
|
||||
"a190": 253,
|
||||
"a191": 254,
|
||||
func NewZapfDingbatsEncoder() SimpleEncoder {
|
||||
enc, _ := NewSimpleTextEncoder("ZapfDingbatsEncoding", nil)
|
||||
return enc
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user