github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/textencoding/symbol.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package textencoding 7 8 import ( 9 "github.com/unidoc/unidoc/common" 10 "github.com/unidoc/unidoc/pdf/core" 11 ) 12 13 // Encoding for Symbol font. 14 type SymbolEncoder struct { 15 } 16 17 func NewSymbolEncoder() SymbolEncoder { 18 encoder := SymbolEncoder{} 19 return encoder 20 } 21 22 // Convert a raw utf8 string (series of runes) to an encoded string (series of character codes) to be used in PDF. 23 func (enc SymbolEncoder) Encode(raw string) string { 24 encoded := []byte{} 25 for _, rune := range raw { 26 code, found := enc.RuneToCharcode(rune) 27 if !found { 28 continue 29 } 30 31 encoded = append(encoded, code) 32 } 33 34 return string(encoded) 35 } 36 37 // Conversion between character code and glyph name. 38 // The bool return flag is true if there was a match, and false otherwise. 39 func (enc SymbolEncoder) CharcodeToGlyph(code byte) (string, bool) { 40 glyph, has := symbolEncodingCharcodeToGlyphMap[code] 41 if !has { 42 common.Log.Debug("Symbol encoding error: unable to find charcode->glyph entry (%v)", code) 43 return "", false 44 } 45 return glyph, true 46 } 47 48 // Conversion between glyph name and character code. 49 // The bool return flag is true if there was a match, and false otherwise. 50 func (enc SymbolEncoder) GlyphToCharcode(glyph string) (byte, bool) { 51 code, found := symbolEncodingGlyphToCharcodeMap[glyph] 52 if !found { 53 common.Log.Debug("Symbol encoding error: unable to find glyph->charcode entry (%s)", glyph) 54 return 0, false 55 } 56 57 return code, found 58 } 59 60 // Convert rune to character code. 61 // The bool return flag is true if there was a match, and false otherwise. 62 func (enc SymbolEncoder) RuneToCharcode(val rune) (byte, bool) { 63 glyph, found := runeToGlyph(val, glyphlistRuneToGlyphMap) 64 if !found { 65 common.Log.Debug("Symbol encoding error: unable to find rune->glyph entry (%v)", val) 66 return 0, false 67 } 68 69 code, found := symbolEncodingGlyphToCharcodeMap[glyph] 70 if !found { 71 common.Log.Debug("Symbol encoding error: unable to find glyph->charcode entry (%s)", glyph) 72 return 0, false 73 } 74 75 return code, true 76 } 77 78 // Convert character code to rune. 79 // The bool return flag is true if there was a match, and false otherwise. 80 func (enc SymbolEncoder) CharcodeToRune(charcode byte) (rune, bool) { 81 glyph, found := symbolEncodingCharcodeToGlyphMap[charcode] 82 if !found { 83 common.Log.Debug("Symbol encoding error: unable to find charcode->glyph entry (%d)", charcode) 84 return 0, false 85 } 86 87 val, found := glyphToRune(glyph, glyphlistGlyphToRuneMap) 88 if !found { 89 return 0, false 90 } 91 92 return val, true 93 } 94 95 // Convert rune to glyph name. 96 // The bool return flag is true if there was a match, and false otherwise. 97 func (enc SymbolEncoder) RuneToGlyph(val rune) (string, bool) { 98 return runeToGlyph(val, glyphlistRuneToGlyphMap) 99 } 100 101 // Convert glyph to rune. 102 // The bool return flag is true if there was a match, and false otherwise. 103 func (enc SymbolEncoder) GlyphToRune(glyph string) (rune, bool) { 104 return glyphToRune(glyph, glyphlistGlyphToRuneMap) 105 } 106 107 // Convert to PDF Object. 108 func (enc SymbolEncoder) ToPdfObject() core.PdfObject { 109 dict := core.MakeDict() 110 dict.Set("Type", core.MakeName("Encoding")) 111 112 // Returning an empty Encoding object with no differences. Indicates that we are using the font's built-in 113 // encoding. 114 return core.MakeIndirectObject(dict) 115 } 116 117 // Charcode to Glyph map (Symbol encoding) 118 var symbolEncodingCharcodeToGlyphMap map[byte]string = map[byte]string{ 119 32: "space", 120 33: "exclam", 121 34: "universal", 122 35: "numbersign", 123 36: "existential", 124 37: "percent", 125 38: "ampersand", 126 39: "suchthat", 127 40: "parenleft", 128 41: "parenright", 129 42: "asteriskmath", 130 43: "plus", 131 44: "comma", 132 45: "minus", 133 46: "period", 134 47: "slash", 135 48: "zero", 136 49: "one", 137 50: "two", 138 51: "three", 139 52: "four", 140 53: "five", 141 54: "six", 142 55: "seven", 143 56: "eight", 144 57: "nine", 145 58: "colon", 146 59: "semicolon", 147 60: "less", 148 61: "equal", 149 62: "greater", 150 63: "question", 151 64: "congruent", 152 65: "Alpha", 153 66: "Beta", 154 67: "Chi", 155 68: "Delta", 156 69: "Epsilon", 157 70: "Phi", 158 71: "Gamma", 159 72: "Eta", 160 73: "Iota", 161 74: "theta1", 162 75: "Kappa", 163 76: "Lambda", 164 77: "Mu", 165 78: "Nu", 166 79: "Omicron", 167 80: "Pi", 168 81: "Theta", 169 82: "Rho", 170 83: "Sigma", 171 84: "Tau", 172 85: "Upsilon", 173 86: "sigma1", 174 87: "Omega", 175 88: "Xi", 176 89: "Psi", 177 90: "Zeta", 178 91: "bracketleft", 179 92: "therefore", 180 93: "bracketright", 181 94: "perpendicular", 182 95: "underscore", 183 96: "radicalex", 184 97: "alpha", 185 98: "beta", 186 99: "chi", 187 100: "delta", 188 101: "epsilon", 189 102: "phi", 190 103: "gamma", 191 104: "eta", 192 105: "iota", 193 106: "phi1", 194 107: "kappa", 195 108: "lambda", 196 109: "mu", 197 110: "nu", 198 111: "omicron", 199 112: "pi", 200 113: "theta", 201 114: "rho", 202 115: "sigma", 203 116: "tau", 204 117: "upsilon", 205 118: "omega1", 206 119: "omega", 207 120: "xi", 208 121: "psi", 209 122: "zeta", 210 123: "braceleft", 211 124: "bar", 212 125: "braceright", 213 126: "similar", 214 160: "Euro", 215 161: "Upsilon1", 216 162: "minute", 217 163: "lessequal", 218 164: "fraction", 219 165: "infinity", 220 166: "florin", 221 167: "club", 222 168: "diamond", 223 169: "heart", 224 170: "spade", 225 171: "arrowboth", 226 172: "arrowleft", 227 173: "arrowup", 228 174: "arrowright", 229 175: "arrowdown", 230 176: "degree", 231 177: "plusminus", 232 178: "second", 233 179: "greaterequal", 234 180: "multiply", 235 181: "proportional", 236 182: "partialdiff", 237 183: "bullet", 238 184: "divide", 239 185: "notequal", 240 186: "equivalence", 241 187: "approxequal", 242 188: "ellipsis", 243 189: "arrowvertex", 244 190: "arrowhorizex", 245 191: "carriagereturn", 246 192: "aleph", 247 193: "Ifraktur", 248 194: "Rfraktur", 249 195: "weierstrass", 250 196: "circlemultiply", 251 197: "circleplus", 252 198: "emptyset", 253 199: "intersection", 254 200: "union", 255 201: "propersuperset", 256 202: "reflexsuperset", 257 203: "notsubset", 258 204: "propersubset", 259 205: "reflexsubset", 260 206: "element", 261 207: "notelement", 262 208: "angle", 263 209: "gradient", 264 210: "registerserif", 265 211: "copyrightserif", 266 212: "trademarkserif", 267 213: "product", 268 214: "radical", 269 215: "dotmath", 270 216: "logicalnot", 271 217: "logicaland", 272 218: "logicalor", 273 219: "arrowdblboth", 274 220: "arrowdblleft", 275 221: "arrowdblup", 276 222: "arrowdblright", 277 223: "arrowdbldown", 278 224: "lozenge", 279 225: "angleleft", 280 226: "registersans", 281 227: "copyrightsans", 282 228: "trademarksans", 283 229: "summation", 284 230: "parenlefttp", 285 231: "parenleftex", 286 232: "parenleftbt", 287 233: "bracketlefttp", 288 234: "bracketleftex", 289 235: "bracketleftbt", 290 236: "bracelefttp", 291 237: "braceleftmid", 292 238: "braceleftbt", 293 239: "braceex", 294 241: "angleright", 295 242: "integral", 296 243: "integraltp", 297 244: "integralex", 298 245: "integralbt", 299 246: "parenrighttp", 300 247: "parenrightex", 301 248: "parenrightbt", 302 249: "bracketrighttp", 303 250: "bracketrightex", 304 251: "bracketrightbt", 305 252: "bracerighttp", 306 253: "bracerightmid", 307 254: "bracerightbt", 308 } 309 310 // Glyph to charcode map (Symbol encoding). 311 var symbolEncodingGlyphToCharcodeMap map[string]byte = map[string]byte{ 312 "space": 32, 313 "exclam": 33, 314 "universal": 34, 315 "numbersign": 35, 316 "existential": 36, 317 "percent": 37, 318 "ampersand": 38, 319 "suchthat": 39, 320 "parenleft": 40, 321 "parenright": 41, 322 "asteriskmath": 42, 323 "plus": 43, 324 "comma": 44, 325 "minus": 45, 326 "period": 46, 327 "slash": 47, 328 "zero": 48, 329 "one": 49, 330 "two": 50, 331 "three": 51, 332 "four": 52, 333 "five": 53, 334 "six": 54, 335 "seven": 55, 336 "eight": 56, 337 "nine": 57, 338 "colon": 58, 339 "semicolon": 59, 340 "less": 60, 341 "equal": 61, 342 "greater": 62, 343 "question": 63, 344 "congruent": 64, 345 "Alpha": 65, 346 "Beta": 66, 347 "Chi": 67, 348 "Delta": 68, 349 "Epsilon": 69, 350 "Phi": 70, 351 "Gamma": 71, 352 "Eta": 72, 353 "Iota": 73, 354 "theta1": 74, 355 "Kappa": 75, 356 "Lambda": 76, 357 "Mu": 77, 358 "Nu": 78, 359 "Omicron": 79, 360 "Pi": 80, 361 "Theta": 81, 362 "Rho": 82, 363 "Sigma": 83, 364 "Tau": 84, 365 "Upsilon": 85, 366 "sigma1": 86, 367 "Omega": 87, 368 "Xi": 88, 369 "Psi": 89, 370 "Zeta": 90, 371 "bracketleft": 91, 372 "therefore": 92, 373 "bracketright": 93, 374 "perpendicular": 94, 375 "underscore": 95, 376 "radicalex": 96, 377 "alpha": 97, 378 "beta": 98, 379 "chi": 99, 380 "delta": 100, 381 "epsilon": 101, 382 "phi": 102, 383 "gamma": 103, 384 "eta": 104, 385 "iota": 105, 386 "phi1": 106, 387 "kappa": 107, 388 "lambda": 108, 389 "mu": 109, 390 "nu": 110, 391 "omicron": 111, 392 "pi": 112, 393 "theta": 113, 394 "rho": 114, 395 "sigma": 115, 396 "tau": 116, 397 "upsilon": 117, 398 "omega1": 118, 399 "omega": 119, 400 "xi": 120, 401 "psi": 121, 402 "zeta": 122, 403 "braceleft": 123, 404 "bar": 124, 405 "braceright": 125, 406 "similar": 126, 407 "Euro": 160, 408 "Upsilon1": 161, 409 "minute": 162, 410 "lessequal": 163, 411 "fraction": 164, 412 "infinity": 165, 413 "florin": 166, 414 "club": 167, 415 "diamond": 168, 416 "heart": 169, 417 "spade": 170, 418 "arrowboth": 171, 419 "arrowleft": 172, 420 "arrowup": 173, 421 "arrowright": 174, 422 "arrowdown": 175, 423 "degree": 176, 424 "plusminus": 177, 425 "second": 178, 426 "greaterequal": 179, 427 "multiply": 180, 428 "proportional": 181, 429 "partialdiff": 182, 430 "bullet": 183, 431 "divide": 184, 432 "notequal": 185, 433 "equivalence": 186, 434 "approxequal": 187, 435 "ellipsis": 188, 436 "arrowvertex": 189, 437 "arrowhorizex": 190, 438 "carriagereturn": 191, 439 "aleph": 192, 440 "Ifraktur": 193, 441 "Rfraktur": 194, 442 "weierstrass": 195, 443 "circlemultiply": 196, 444 "circleplus": 197, 445 "emptyset": 198, 446 "intersection": 199, 447 "union": 200, 448 "propersuperset": 201, 449 "reflexsuperset": 202, 450 "notsubset": 203, 451 "propersubset": 204, 452 "reflexsubset": 205, 453 "element": 206, 454 "notelement": 207, 455 "angle": 208, 456 "gradient": 209, 457 "registerserif": 210, 458 "copyrightserif": 211, 459 "trademarkserif": 212, 460 "product": 213, 461 "radical": 214, 462 "dotmath": 215, 463 "logicalnot": 216, 464 "logicaland": 217, 465 "logicalor": 218, 466 "arrowdblboth": 219, 467 "arrowdblleft": 220, 468 "arrowdblup": 221, 469 "arrowdblright": 222, 470 "arrowdbldown": 223, 471 "lozenge": 224, 472 "angleleft": 225, 473 "registersans": 226, 474 "copyrightsans": 227, 475 "trademarksans": 228, 476 "summation": 229, 477 "parenlefttp": 230, 478 "parenleftex": 231, 479 "parenleftbt": 232, 480 "bracketlefttp": 233, 481 "bracketleftex": 234, 482 "bracketleftbt": 235, 483 "bracelefttp": 236, 484 "braceleftmid": 237, 485 "braceleftbt": 238, 486 "braceex": 239, 487 "angleright": 241, 488 "integral": 242, 489 "integraltp": 243, 490 "integralex": 244, 491 "integralbt": 245, 492 "parenrighttp": 246, 493 "parenrightex": 247, 494 "parenrightbt": 248, 495 "bracketrighttp": 249, 496 "bracketrightex": 250, 497 "bracketrightbt": 251, 498 "bracerighttp": 252, 499 "bracerightmid": 253, 500 "bracerightbt": 254, 501 }