github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/textencoding/zapfdingbats.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 ZapfDingbats font.
    14  type ZapfDingbatsEncoder struct {
    15  }
    16  
    17  func NewZapfDingbatsEncoder() ZapfDingbatsEncoder {
    18  	encoder := ZapfDingbatsEncoder{}
    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 ZapfDingbatsEncoder) 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 ZapfDingbatsEncoder) CharcodeToGlyph(code byte) (string, bool) {
    40  	glyph, has := zapfDingbatsEncodingCharcodeToGlyphMap[code]
    41  	if !has {
    42  		common.Log.Debug("ZapfDingbats 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 ZapfDingbatsEncoder) GlyphToCharcode(glyph string) (byte, bool) {
    51  	code, found := zapfDingbatsEncodingGlyphToCharcodeMap[glyph]
    52  	if !found {
    53  		common.Log.Debug("ZapfDingbats 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 ZapfDingbatsEncoder) RuneToCharcode(val rune) (byte, bool) {
    63  	glyph, found := enc.RuneToGlyph(val)
    64  	if !found {
    65  		common.Log.Debug("ZapfDingbats encoding error: unable to find rune->glyph entry (%v)", val)
    66  		return 0, false
    67  	}
    68  
    69  	code, found := zapfDingbatsEncodingGlyphToCharcodeMap[glyph]
    70  	if !found {
    71  		common.Log.Debug("ZapfDingbats 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 ZapfDingbatsEncoder) CharcodeToRune(charcode byte) (rune, bool) {
    81  	glyph, found := zapfDingbatsEncodingCharcodeToGlyphMap[charcode]
    82  	if !found {
    83  		common.Log.Debug("ZapfDingbats encoding error: unable to find charcode->glyph entry (%d)", charcode)
    84  		return 0, false
    85  	}
    86  
    87  	return enc.GlyphToRune(glyph)
    88  }
    89  
    90  // Convert rune to glyph name.
    91  // The bool return flag is true if there was a match, and false otherwise.
    92  func (enc ZapfDingbatsEncoder) RuneToGlyph(val rune) (string, bool) {
    93  	// Seek in the zapfdingbats list first.
    94  	glyph, found := runeToGlyph(val, zapfdingbatsRuneToGlyphMap)
    95  	if !found {
    96  		// Then revert to glyphlist if not found.
    97  		glyph, found = runeToGlyph(val, glyphlistRuneToGlyphMap)
    98  		if !found {
    99  			common.Log.Debug("ZapfDingbats encoding error: unable to find rune->glyph entry (%v)", val)
   100  			return "", false
   101  		}
   102  	}
   103  
   104  	return glyph, true
   105  }
   106  
   107  // Convert glyph to rune.
   108  // The bool return flag is true if there was a match, and false otherwise.
   109  func (enc ZapfDingbatsEncoder) GlyphToRune(glyph string) (rune, bool) {
   110  	// Seek in the zapfdingbats list first.
   111  	val, found := glyphToRune(glyph, zapfdingbatsGlyphToRuneMap)
   112  	if !found {
   113  		// Then revert to glyphlist if not found.
   114  		val, found = glyphToRune(glyph, glyphlistGlyphToRuneMap)
   115  		if !found {
   116  			common.Log.Debug("Symbol encoding error: unable to find glyph->rune entry (%v)", glyph)
   117  			return 0, false
   118  		}
   119  	}
   120  
   121  	return val, true
   122  }
   123  
   124  // Convert to PDF Object.
   125  func (enc ZapfDingbatsEncoder) ToPdfObject() core.PdfObject {
   126  	dict := core.MakeDict()
   127  	dict.Set("Type", core.MakeName("Encoding"))
   128  
   129  	// Returning an empty Encoding object with no differences. Indicates that we are using the font's built-in
   130  	// encoding.
   131  	return core.MakeIndirectObject(dict)
   132  }
   133  
   134  var zapfDingbatsEncodingCharcodeToGlyphMap = map[byte]string{
   135  	32:  "space",
   136  	33:  "a1",
   137  	34:  "a2",
   138  	35:  "a202",
   139  	36:  "a3",
   140  	37:  "a4",
   141  	38:  "a5",
   142  	39:  "a119",
   143  	40:  "a118",
   144  	41:  "a117",
   145  	42:  "a11",
   146  	43:  "a12",
   147  	44:  "a13",
   148  	45:  "a14",
   149  	46:  "a15",
   150  	47:  "a16",
   151  	48:  "a105",
   152  	49:  "a17",
   153  	50:  "a18",
   154  	51:  "a19",
   155  	52:  "a20",
   156  	53:  "a21",
   157  	54:  "a22",
   158  	55:  "a23",
   159  	56:  "a24",
   160  	57:  "a25",
   161  	58:  "a26",
   162  	59:  "a27",
   163  	60:  "a28",
   164  	61:  "a6",
   165  	62:  "a7",
   166  	63:  "a8",
   167  	64:  "a9",
   168  	65:  "a10",
   169  	66:  "a29",
   170  	67:  "a30",
   171  	68:  "a31",
   172  	69:  "a32",
   173  	70:  "a33",
   174  	71:  "a34",
   175  	72:  "a35",
   176  	73:  "a36",
   177  	74:  "a37",
   178  	75:  "a38",
   179  	76:  "a39",
   180  	77:  "a40",
   181  	78:  "a41",
   182  	79:  "a42",
   183  	80:  "a43",
   184  	81:  "a44",
   185  	82:  "a45",
   186  	83:  "a46",
   187  	84:  "a47",
   188  	85:  "a48",
   189  	86:  "a49",
   190  	87:  "a50",
   191  	88:  "a51",
   192  	89:  "a52",
   193  	90:  "a53",
   194  	91:  "a54",
   195  	92:  "a55",
   196  	93:  "a56",
   197  	94:  "a57",
   198  	95:  "a58",
   199  	96:  "a59",
   200  	97:  "a60",
   201  	98:  "a61",
   202  	99:  "a62",
   203  	100: "a63",
   204  	101: "a64",
   205  	102: "a65",
   206  	103: "a66",
   207  	104: "a67",
   208  	105: "a68",
   209  	106: "a69",
   210  	107: "a70",
   211  	108: "a71",
   212  	109: "a72",
   213  	110: "a73",
   214  	111: "a74",
   215  	112: "a203",
   216  	113: "a75",
   217  	114: "a204",
   218  	115: "a76",
   219  	116: "a77",
   220  	117: "a78",
   221  	118: "a79",
   222  	119: "a81",
   223  	120: "a82",
   224  	121: "a83",
   225  	122: "a84",
   226  	123: "a97",
   227  	124: "a98",
   228  	125: "a99",
   229  	126: "a100",
   230  	128: "a89",
   231  	129: "a90",
   232  	130: "a93",
   233  	131: "a94",
   234  	132: "a91",
   235  	133: "a92",
   236  	134: "a205",
   237  	135: "a85",
   238  	136: "a206",
   239  	137: "a86",
   240  	138: "a87",
   241  	139: "a88",
   242  	140: "a95",
   243  	141: "a96",
   244  	161: "a101",
   245  	162: "a102",
   246  	163: "a103",
   247  	164: "a104",
   248  	165: "a106",
   249  	166: "a107",
   250  	167: "a108",
   251  	168: "a112",
   252  	169: "a111",
   253  	170: "a110",
   254  	171: "a109",
   255  	172: "a120",
   256  	173: "a121",
   257  	174: "a122",
   258  	175: "a123",
   259  	176: "a124",
   260  	177: "a125",
   261  	178: "a126",
   262  	179: "a127",
   263  	180: "a128",
   264  	181: "a129",
   265  	182: "a130",
   266  	183: "a131",
   267  	184: "a132",
   268  	185: "a133",
   269  	186: "a134",
   270  	187: "a135",
   271  	188: "a136",
   272  	189: "a137",
   273  	190: "a138",
   274  	191: "a139",
   275  	192: "a140",
   276  	193: "a141",
   277  	194: "a142",
   278  	195: "a143",
   279  	196: "a144",
   280  	197: "a145",
   281  	198: "a146",
   282  	199: "a147",
   283  	200: "a148",
   284  	201: "a149",
   285  	202: "a150",
   286  	203: "a151",
   287  	204: "a152",
   288  	205: "a153",
   289  	206: "a154",
   290  	207: "a155",
   291  	208: "a156",
   292  	209: "a157",
   293  	210: "a158",
   294  	211: "a159",
   295  	212: "a160",
   296  	213: "a161",
   297  	214: "a163",
   298  	215: "a164",
   299  	216: "a196",
   300  	217: "a165",
   301  	218: "a192",
   302  	219: "a166",
   303  	220: "a167",
   304  	221: "a168",
   305  	222: "a169",
   306  	223: "a170",
   307  	224: "a171",
   308  	225: "a172",
   309  	226: "a173",
   310  	227: "a162",
   311  	228: "a174",
   312  	229: "a175",
   313  	230: "a176",
   314  	231: "a177",
   315  	232: "a178",
   316  	233: "a179",
   317  	234: "a193",
   318  	235: "a180",
   319  	236: "a199",
   320  	237: "a181",
   321  	238: "a200",
   322  	239: "a182",
   323  	241: "a201",
   324  	242: "a183",
   325  	243: "a184",
   326  	244: "a197",
   327  	245: "a185",
   328  	246: "a194",
   329  	247: "a198",
   330  	248: "a186",
   331  	249: "a195",
   332  	250: "a187",
   333  	251: "a188",
   334  	252: "a189",
   335  	253: "a190",
   336  	254: "a191",
   337  }
   338  
   339  var zapfDingbatsEncodingGlyphToCharcodeMap = map[string]byte{
   340  	"space": 32,
   341  	"a1":    33,
   342  	"a2":    34,
   343  	"a202":  35,
   344  	"a3":    36,
   345  	"a4":    37,
   346  	"a5":    38,
   347  	"a119":  39,
   348  	"a118":  40,
   349  	"a117":  41,
   350  	"a11":   42,
   351  	"a12":   43,
   352  	"a13":   44,
   353  	"a14":   45,
   354  	"a15":   46,
   355  	"a16":   47,
   356  	"a105":  48,
   357  	"a17":   49,
   358  	"a18":   50,
   359  	"a19":   51,
   360  	"a20":   52,
   361  	"a21":   53,
   362  	"a22":   54,
   363  	"a23":   55,
   364  	"a24":   56,
   365  	"a25":   57,
   366  	"a26":   58,
   367  	"a27":   59,
   368  	"a28":   60,
   369  	"a6":    61,
   370  	"a7":    62,
   371  	"a8":    63,
   372  	"a9":    64,
   373  	"a10":   65,
   374  	"a29":   66,
   375  	"a30":   67,
   376  	"a31":   68,
   377  	"a32":   69,
   378  	"a33":   70,
   379  	"a34":   71,
   380  	"a35":   72,
   381  	"a36":   73,
   382  	"a37":   74,
   383  	"a38":   75,
   384  	"a39":   76,
   385  	"a40":   77,
   386  	"a41":   78,
   387  	"a42":   79,
   388  	"a43":   80,
   389  	"a44":   81,
   390  	"a45":   82,
   391  	"a46":   83,
   392  	"a47":   84,
   393  	"a48":   85,
   394  	"a49":   86,
   395  	"a50":   87,
   396  	"a51":   88,
   397  	"a52":   89,
   398  	"a53":   90,
   399  	"a54":   91,
   400  	"a55":   92,
   401  	"a56":   93,
   402  	"a57":   94,
   403  	"a58":   95,
   404  	"a59":   96,
   405  	"a60":   97,
   406  	"a61":   98,
   407  	"a62":   99,
   408  	"a63":   100,
   409  	"a64":   101,
   410  	"a65":   102,
   411  	"a66":   103,
   412  	"a67":   104,
   413  	"a68":   105,
   414  	"a69":   106,
   415  	"a70":   107,
   416  	"a71":   108,
   417  	"a72":   109,
   418  	"a73":   110,
   419  	"a74":   111,
   420  	"a203":  112,
   421  	"a75":   113,
   422  	"a204":  114,
   423  	"a76":   115,
   424  	"a77":   116,
   425  	"a78":   117,
   426  	"a79":   118,
   427  	"a81":   119,
   428  	"a82":   120,
   429  	"a83":   121,
   430  	"a84":   122,
   431  	"a97":   123,
   432  	"a98":   124,
   433  	"a99":   125,
   434  	"a100":  126,
   435  	"a89":   128,
   436  	"a90":   129,
   437  	"a93":   130,
   438  	"a94":   131,
   439  	"a91":   132,
   440  	"a92":   133,
   441  	"a205":  134,
   442  	"a85":   135,
   443  	"a206":  136,
   444  	"a86":   137,
   445  	"a87":   138,
   446  	"a88":   139,
   447  	"a95":   140,
   448  	"a96":   141,
   449  	"a101":  161,
   450  	"a102":  162,
   451  	"a103":  163,
   452  	"a104":  164,
   453  	"a106":  165,
   454  	"a107":  166,
   455  	"a108":  167,
   456  	"a112":  168,
   457  	"a111":  169,
   458  	"a110":  170,
   459  	"a109":  171,
   460  	"a120":  172,
   461  	"a121":  173,
   462  	"a122":  174,
   463  	"a123":  175,
   464  	"a124":  176,
   465  	"a125":  177,
   466  	"a126":  178,
   467  	"a127":  179,
   468  	"a128":  180,
   469  	"a129":  181,
   470  	"a130":  182,
   471  	"a131":  183,
   472  	"a132":  184,
   473  	"a133":  185,
   474  	"a134":  186,
   475  	"a135":  187,
   476  	"a136":  188,
   477  	"a137":  189,
   478  	"a138":  190,
   479  	"a139":  191,
   480  	"a140":  192,
   481  	"a141":  193,
   482  	"a142":  194,
   483  	"a143":  195,
   484  	"a144":  196,
   485  	"a145":  197,
   486  	"a146":  198,
   487  	"a147":  199,
   488  	"a148":  200,
   489  	"a149":  201,
   490  	"a150":  202,
   491  	"a151":  203,
   492  	"a152":  204,
   493  	"a153":  205,
   494  	"a154":  206,
   495  	"a155":  207,
   496  	"a156":  208,
   497  	"a157":  209,
   498  	"a158":  210,
   499  	"a159":  211,
   500  	"a160":  212,
   501  	"a161":  213,
   502  	"a163":  214,
   503  	"a164":  215,
   504  	"a196":  216,
   505  	"a165":  217,
   506  	"a192":  218,
   507  	"a166":  219,
   508  	"a167":  220,
   509  	"a168":  221,
   510  	"a169":  222,
   511  	"a170":  223,
   512  	"a171":  224,
   513  	"a172":  225,
   514  	"a173":  226,
   515  	"a162":  227,
   516  	"a174":  228,
   517  	"a175":  229,
   518  	"a176":  230,
   519  	"a177":  231,
   520  	"a178":  232,
   521  	"a179":  233,
   522  	"a193":  234,
   523  	"a180":  235,
   524  	"a199":  236,
   525  	"a181":  237,
   526  	"a200":  238,
   527  	"a182":  239,
   528  	"a201":  241,
   529  	"a183":  242,
   530  	"a184":  243,
   531  	"a197":  244,
   532  	"a185":  245,
   533  	"a194":  246,
   534  	"a198":  247,
   535  	"a186":  248,
   536  	"a195":  249,
   537  	"a187":  250,
   538  	"a188":  251,
   539  	"a189":  252,
   540  	"a190":  253,
   541  	"a191":  254,
   542  }