PDFFontUtils.java 53.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.agl.util.ULocale
 *  com.adobe.fontengine.FontEngineException
 *  com.adobe.fontengine.font.CoolTypeScript
 *  com.adobe.fontengine.font.Font
 *  com.adobe.fontengine.font.FontData
 *  com.adobe.fontengine.font.FontException
 *  com.adobe.fontengine.font.FontLoadingException
 *  com.adobe.fontengine.font.InvalidFontException
 *  com.adobe.fontengine.font.PDFFontDescription
 *  com.adobe.fontengine.font.ROS
 *  com.adobe.fontengine.font.UnsupportedFontException
 *  com.adobe.fontengine.font.cff.CFFFont
 *  com.adobe.fontengine.font.cff.CIDKeyedFont
 *  com.adobe.fontengine.font.cff.NameKeyedFont
 *  com.adobe.fontengine.font.opentype.Cmap
 *  com.adobe.fontengine.font.opentype.Cmap$CmapSelector
 *  com.adobe.fontengine.font.opentype.GlyphNames
 *  com.adobe.fontengine.font.opentype.OpenTypeFont
 *  com.adobe.fontengine.font.opentype.Post
 *  com.adobe.fontengine.font.type1.Type1Font
 *  com.adobe.fontengine.fontmanagement.FontResolutionPriority
 *  com.adobe.fontengine.fontmanagement.postscript.PostscriptFontDescription
 *  com.adobe.fontengine.inlineformatting.FallbackFontSet
 *  com.adobe.fontengine.inlineformatting.css20.CSS20Attribute
 *  com.adobe.fontengine.inlineformatting.css20.CSS20Attribute$CSSStretchValue
 *  com.adobe.fontengine.inlineformatting.css20.CSS20Attribute$CSSStyleValue
 *  com.adobe.fontengine.inlineformatting.css20.CSS20Attribute$CSSVariantValue
 *  com.adobe.fontengine.inlineformatting.css20.CSS20Attribute$CSSWeightValue
 *  com.adobe.fontengine.inlineformatting.css20.CSS20FontSet
 *  com.adobe.internal.pdftoolkit.core.cos.CosArray
 *  com.adobe.internal.pdftoolkit.core.cos.CosDictionary
 *  com.adobe.internal.pdftoolkit.core.cos.CosDocument
 *  com.adobe.internal.pdftoolkit.core.cos.CosName
 *  com.adobe.internal.pdftoolkit.core.cos.CosNumeric
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions
 *  com.adobe.internal.pdftoolkit.core.cos.CosStream
 *  com.adobe.internal.pdftoolkit.core.cos.CosString
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFConfigurationException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFFontException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 *  com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet
 *  com.adobe.internal.pdftoolkit.core.fontset.impl.PDFFontSetImpl
 *  com.adobe.internal.pdftoolkit.core.types.ASDictionary
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 *  com.adobe.internal.pdftoolkit.core.types.ASNumber
 *  com.adobe.internal.pdftoolkit.core.types.ASRectangle
 *  com.adobe.internal.pdftoolkit.core.types.ASString
 */
package com.adobe.internal.pdftoolkit.pdf.graphics.font.impl;

import com.adobe.agl.util.ULocale;
import com.adobe.fontengine.FontEngineException;
import com.adobe.fontengine.font.CoolTypeScript;
import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.FontData;
import com.adobe.fontengine.font.FontException;
import com.adobe.fontengine.font.FontLoadingException;
import com.adobe.fontengine.font.InvalidFontException;
import com.adobe.fontengine.font.PDFFontDescription;
import com.adobe.fontengine.font.ROS;
import com.adobe.fontengine.font.UnsupportedFontException;
import com.adobe.fontengine.font.cff.CFFFont;
import com.adobe.fontengine.font.cff.CIDKeyedFont;
import com.adobe.fontengine.font.cff.NameKeyedFont;
import com.adobe.fontengine.font.opentype.Cmap;
import com.adobe.fontengine.font.opentype.GlyphNames;
import com.adobe.fontengine.font.opentype.OpenTypeFont;
import com.adobe.fontengine.font.opentype.Post;
import com.adobe.fontengine.font.type1.Type1Font;
import com.adobe.fontengine.fontmanagement.FontResolutionPriority;
import com.adobe.fontengine.fontmanagement.postscript.PostscriptFontDescription;
import com.adobe.fontengine.inlineformatting.FallbackFontSet;
import com.adobe.fontengine.inlineformatting.css20.CSS20Attribute;
import com.adobe.fontengine.inlineformatting.css20.CSS20FontSet;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFConfigurationException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFFontException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet;
import com.adobe.internal.pdftoolkit.core.fontset.impl.PDFFontSetImpl;
import com.adobe.internal.pdftoolkit.core.types.ASDictionary;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASNumber;
import com.adobe.internal.pdftoolkit.core.types.ASRectangle;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.filters.PDFFilterFlate;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.GlyphIDHolder;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFCIDFont;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFCIDFontWidths;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFCIDSystemInfo;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFCMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFCosFontDescriptor;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFont;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFontDescriptor;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFontFile;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFontSimple;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFontType0;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFontType3;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFSimpleFontEncoding;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFToUnicodeCMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFType0FontEncoding;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFWritingMode;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.encodings.MacRomanEncoding;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.encodings.StandardEncoding;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.CMapResourceBuilder;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.PDFCMapUtils;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.StandardFontUtils;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public class PDFFontUtils {
    private PDFFontUtils() {
    }

    public static Locale getLocaleFromUnicode(byte[] unicodeBytes) {
        if (unicodeBytes.length == 2) {
            if (unicodeBytes[0] == 6 && unicodeBytes[1] >= 0 && unicodeBytes[1] <= 255) {
                return PDFFontSet.ARABIC;
            }
            if (unicodeBytes[0] == 5 && unicodeBytes[1] >= 144 && unicodeBytes[1] <= 255) {
                return PDFFontSet.HEBREW;
            }
            if (unicodeBytes[0] == 14 && unicodeBytes[1] >= 0 && unicodeBytes[1] <= 127) {
                return PDFFontSet.THAI;
            }
        }
        return null;
    }

    public static PDFFont createFontForEditting(PDFDocument pdfDoc, String fontName, boolean embedFont, boolean opentypeOK) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException {
        PostscriptFontDescription fontDescription = new PostscriptFontDescription(fontName);
        PDFFontSetImpl fontset = (PDFFontSetImpl)pdfDoc.getCosDocument().getOptions().getFontSet();
        Font afeFont = null;
        if (fontset != null) {
            afeFont = fontset.getPSFont(fontDescription, PDFDocument.ROOT_LOCALE, false);
        }
        try {
            return PDFFontUtils.createFontForEditing(pdfDoc, afeFont, embedFont, opentypeOK);
        }
        catch (PDFInvalidParameterException e) {
            throw new PDFInvalidDocumentException((Throwable)e);
        }
    }

    public static PDFFont createFontForEditing(PDFDocument pdfDoc, Font afeFont, boolean embedFont, boolean opentypeOK) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException, PDFInvalidParameterException {
        if (afeFont == null) {
            return null;
        }
        try {
            PDFFont theFont = afeFont.getPDFFontDescription().getROS() != null ? PDFFontUtils.createCompositeFont(pdfDoc, afeFont, embedFont, opentypeOK) : PDFFontUtils.createSimpleFont(pdfDoc, afeFont, embedFont, opentypeOK);
            return theFont;
        }
        catch (FontEngineException e) {
            throw new PDFFontException((Throwable)e);
        }
    }

    public static PDFFont createFontForEditing(PDFDocument pdfDoc, String typeFace, String posture, String weight, double pointSize, Locale locale, boolean embedFont, boolean opentypeOK) throws FontException, PDFFontException, PDFConfigurationException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
        Font afeFont = PDFFontUtils.doCSSLookup(pdfDoc, typeFace, posture, weight, pointSize, locale);
        PDFFont pdfFontToAdd = PDFFontUtils.createFontForEditing(pdfDoc, afeFont, embedFont, opentypeOK);
        return pdfFontToAdd;
    }

    private static Font doCSSLookup(PDFDocument pdfDoc, String typeFace, String posture, String weight, double pointSize, Locale locale) throws FontException, PDFFontException, PDFConfigurationException {
        CSS20FontSet cssFontSet = null;
        PDFFontSetImpl pdfFontSet = PDFFontUtils.determineFallbackFontSet(pdfDoc);
        if (pdfFontSet != null) {
            cssFontSet = pdfFontSet.getCSS20FontSet();
            cssFontSet.setFallbackFonts(pdfFontSet.getFallbackFontSet());
        }
        if (cssFontSet == null) {
            throw new PDFConfigurationException("Null CSS Fontset encountered");
        }
        cssFontSet.setResolutionPriority(FontResolutionPriority.INTELLIGENT);
        CSS20Attribute att = new CSS20Attribute(new String[]{typeFace}, CSS20Attribute.CSSStyleValue.parse((String)posture), CSS20Attribute.CSSVariantValue.NORMAL, CSS20Attribute.CSSStretchValue.NORMAL, CSS20Attribute.CSSWeightValue.parse((String)weight), pointSize);
        return cssFontSet.findFont(att, ULocale.forLocale((Locale)locale));
    }

    private static PDFFontSetImpl determineFallbackFontSet(PDFDocument pdfDoc) throws PDFConfigurationException {
        PDFFontSet pdfFontSet = pdfDoc.getCosDocument().getOptions().getFontSet();
        if (pdfFontSet == null) {
            throw new PDFConfigurationException("Fontset not available via the Document Open Options");
        }
        return (PDFFontSetImpl)pdfFontSet;
    }

    private static PDFFont createSimpleFont(PDFDocument pdfDoc, Font afeFont, boolean embedFont, boolean opentypeOK) throws FontEngineException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException, PDFInvalidParameterException {
        PDFFontFile fontFile;
        PDFFontDescription pdfDesc = afeFont.getPDFFontDescription();
        ASName baseFont = ASName.create((String)pdfDesc.getPostscriptName());
        PDFFontSimple simpleFont = PDFFontSimple.newInstance(pdfDoc, baseFont, pdfDesc.pdfFontIsTrueType() ? ASName.create((String)"TrueType") : ASName.create((String)"Type1"));
        PDFFontDescriptor fontDescriptor = PDFFontDescriptor.newInstance(pdfDoc, baseFont, pdfDesc);
        if ((fontDescriptor.getFlags() & 32) != 0) {
            simpleFont.setEncoding(PDFSimpleFontEncoding.newInstance(pdfDoc, ASName.k_WinAnsiEncoding));
        }
        if (embedFont && (fontFile = PDFFontFile.newInstance(pdfDoc, opentypeOK, afeFont)) != null) {
            PDFFilterFlate filter = PDFFilterFlate.newInstance(pdfDoc, null);
            fontFile.setFilter(filter);
            if (pdfDesc.pdfFontIsTrueType()) {
                fontDescriptor.setFontFile2(fontFile);
            } else {
                try {
                    fontFile.setEmbeddedFontType(opentypeOK ? PDFFontFile.EmbeddedFontType.OpenType : PDFFontFile.EmbeddedFontType.Type1C);
                }
                catch (PDFInvalidParameterException e) {
                    // empty catch block
                }
                fontDescriptor.setFontFile3(fontFile);
            }
        }
        simpleFont.setFontDescriptor(fontDescriptor);
        simpleFont.resetAFEFont();
        simpleFont.buildWidths(32, 255, afeFont);
        return simpleFont;
    }

    private static PDFFont createCompositeFont(PDFDocument pdfDoc, Font afeFont, boolean embedFont, boolean opentypeOK) throws FontEngineException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException, PDFInvalidParameterException {
        PDFFontFile fontFile;
        PDFFontDescription pdfDesc = afeFont.getPDFFontDescription();
        ASName baseFont = ASName.create((String)pdfDesc.getPostscriptName());
        PDFFontType0 compFont = (PDFFontType0)PDFFontType0.newInstance(pdfDoc, baseFont);
        compFont.setEncoding(PDFType0FontEncoding.newInstance(pdfDoc, ASName.create((String)"UniJIS-UTF16-H")));
        PDFCIDFont cidFont = PDFCIDFont.newInstance(pdfDoc, baseFont, ASName.k_CIDFontType0);
        compFont.setDescendantFont(cidFont);
        cidFont.setCIDSystemInfo(PDFCIDSystemInfo.newInstance(pdfDoc, new ASString(pdfDesc.getROS().registry), new ASString(pdfDesc.getROS().ordering), pdfDesc.getROS().supplement));
        PDFFontDescriptor fontDescriptor = PDFFontDescriptor.newInstance(pdfDoc, baseFont, pdfDesc);
        cidFont.setFontDescriptor(fontDescriptor);
        if (embedFont && (fontFile = PDFFontFile.newInstance(pdfDoc, opentypeOK, afeFont)) != null) {
            try {
                fontFile.setEmbeddedFontType(opentypeOK ? PDFFontFile.EmbeddedFontType.OpenType : PDFFontFile.EmbeddedFontType.CIDFontType0C);
            }
            catch (PDFInvalidParameterException e) {
                // empty catch block
            }
            PDFFilterFlate filter = PDFFilterFlate.newInstance(pdfDoc, null);
            fontFile.setFilter(filter);
            fontDescriptor.setFontFile3(fontFile);
        }
        PDFCIDFontWidths cidFontWidths = PDFCIDFontWidths.newInstance(pdfDoc);
        cidFontWidths.buildWidths(new GlyphIterator(pdfDesc.getNumGlyphs()), pdfDesc, 1000.0);
        cidFont.setW(cidFontWidths);
        return compFont;
    }

    public static PDFFontFile getFontFileFromFontDescriptor(PDFFontDescriptor pdfFontDescriptor) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (pdfFontDescriptor == null) {
            return null;
        }
        PDFFontFile fontFile = pdfFontDescriptor.getFontFile();
        if (fontFile == null) {
            fontFile = pdfFontDescriptor.getFontFile2();
        }
        if (fontFile == null) {
            fontFile = pdfFontDescriptor.getFontFile3();
        }
        return fontFile;
    }

    public static PDFFontDescriptor getPDFFontDescriptor(PDFFont pdfFont) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (pdfFont instanceof PDFFontType0) {
            return ((PDFFontType0)pdfFont).getDescendantFont().getFontDescriptor();
        }
        if (pdfFont instanceof PDFFontSimple) {
            return ((PDFFontSimple)pdfFont).getFontDescriptor();
        }
        return null;
    }

    public static boolean isFontDataTrueType(FontData fontData) {
        return fontData instanceof OpenTypeFont && ((OpenTypeFont)fontData).getCFFFont() == null;
    }

    public static boolean isCMapIdentity(PDFType0FontEncoding cMap) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        boolean isCMapIdentity = false;
        if (cMap.isPredefinedCMap()) {
            ASName cMapName = cMap.getCMapName();
            isCMapIdentity = cMapName == ASName.k_Identity_H || cMapName == ASName.k_Identity_V;
        }
        return isCMapIdentity;
    }

    public static boolean setWModeInEmbeddedCMap(PDFFontType0 type0Font) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFType0FontEncoding cMap = type0Font.getEncoding();
        if (cMap.getCosObject().getType() == 7) {
            PDFWritingMode cMapStreamWMode = CMapResourceBuilder.getWMode(cMap.getCosStream());
            CosStream cmapObj = (CosStream)cMap.getCosObject();
            cmapObj.put(ASName.k_WMode, cMapStreamWMode.equals((Object)PDFWritingMode.VERTICAL) ? 1 : 0);
            return true;
        }
        return false;
    }

    public static boolean isFontEmbedded(PDFFont font) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFFontFile file;
        if (font instanceof PDFFontType3) {
            return true;
        }
        PDFFontDescriptor fdesc = font.getFontDescriptor();
        if (fdesc != null && (file = PDFFontUtils.getFontFileFromFontDescriptor(fdesc)) != null) {
            return true;
        }
        return false;
    }

    public static boolean isSubsetFontName(String baseFontName) {
        if (baseFontName == null) {
            return false;
        }
        if (baseFontName.length() < 8) {
            return false;
        }
        for (int i = 0; i < 6; ++i) {
            if (Character.isUpperCase(baseFontName.charAt(i))) continue;
            return false;
        }
        return baseFontName.charAt(6) == '+';
    }

    public static boolean isSubsetFont(PDFFont font) throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
        String fontName = PDFFontUtils.getBaseFontName(font);
        if (fontName == null) {
            return false;
        }
        return PDFFontUtils.isSubsetFontName(fontName);
    }

    public static String getActualBaseFontName(PDFFont font) throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
        String baseFontName = PDFFontUtils.getBaseFontName(font);
        if (baseFontName != null) {
            if (PDFFontUtils.isSubsetFontName(baseFontName)) {
                return baseFontName.substring(7);
            }
            return baseFontName;
        }
        return null;
    }

    private static String getBaseFontName(PDFFont font) throws PDFIOException, PDFSecurityException, PDFInvalidDocumentException {
        String fontName;
        if (font == null) {
            return null;
        }
        if (font instanceof PDFFontType0) {
            PDFFontType0 type0Font = (PDFFontType0)font;
            PDFCIDFont cidFont = type0Font.getDescendantFont();
            fontName = cidFont.getBaseFont().asString();
        } else {
            ASName asname = font.getBaseFont();
            if (asname == null) {
                return null;
            }
            fontName = asname.asString();
        }
        return fontName;
    }

    public static boolean isOpenTypeFontEmbedded(PDFFont font) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ASName ffType;
        PDFFontFile ffile;
        if (font == null) {
            return false;
        }
        PDFFontDescriptor fdesc = font.getFontDescriptor();
        if (fdesc != null && (ffile = fdesc.getFontFile3()) != null && (ffType = ffile.getSubtype()) == ASName.create((String)"OpenType")) {
            return true;
        }
        return false;
    }

    public static boolean isGlyphNotDef(PDFFont font, int charcode) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException, InvalidFontException, UnsupportedFontException, FontLoadingException {
        int gid = font.charCode2gid(charcode);
        Font afeFont = font.getAFEFont();
        if (afeFont == null) {
            return false;
        }
        PDFFontDescription desc = afeFont.getPDFFontDescription();
        if (desc == null) {
            return false;
        }
        if (font instanceof PDFFontType0) {
            return gid == -1;
        }
        if (gid < 0) {
            return true;
        }
        return ".notdef".equals(desc.getGlyphName(gid));
    }

    public static ASName generateSubsetFontName(Object fontSubset, String basefontName) {
        try {
            ASName subsetfontName = ASName.create((String)basefontName);
            if (!PDFFontUtils.isSubsetFontName(basefontName)) {
                int iCode = fontSubset.hashCode() > 0 ? fontSubset.hashCode() : - fontSubset.hashCode();
                int A = 65;
                byte[] tag = new byte[7];
                for (int j = 0; j < 6; ++j) {
                    tag[j] = (byte)(A + iCode % 26);
                    iCode /= 26;
                }
                tag[6] = 43;
                String subsetFontTag = new String(tag, "US-ASCII");
                subsetfontName = ASName.create((String)(subsetFontTag + basefontName));
            }
            return subsetfontName;
        }
        catch (UnsupportedEncodingException e) {
            throw new RuntimeException("US-ASCII encoding not supported.", e);
        }
    }

    public static PDFCIDFont createPDFCIDFont(PDFFontFile fontFile, PDFFontType0 font, PDFFontDescription afeFontDesc, int dw, PDFCIDFontWidths pdfCIDFontWidths, ASName subsetFontName, PDFFontDescriptor fontDescriptor) throws UnsupportedFontException, InvalidFontException, PDFIOException, PDFSecurityException, PDFInvalidDocumentException, PDFInvalidParameterException {
        PDFCIDFont cidFont;
        PDFDocument pdfDocument = fontFile.getPDFDocument();
        if (!afeFontDesc.pdfFontIsTrueType()) {
            try {
                fontFile.setEmbeddedFontType(PDFFontFile.EmbeddedFontType.CIDFontType0C);
            }
            catch (PDFInvalidParameterException e) {
                // empty catch block
            }
            fontDescriptor.setFontFile3(fontFile);
            cidFont = PDFCIDFont.newInstance(pdfDocument, subsetFontName, ASName.k_CIDFontType0);
        } else {
            fontDescriptor.setFontFile2(fontFile);
            cidFont = PDFCIDFont.newInstance(pdfDocument, subsetFontName, ASName.k_CIDFontType2);
        }
        cidFont.setDW(dw);
        if (pdfCIDFontWidths != null) {
            cidFont.setW(pdfCIDFontWidths);
        }
        cidFont.setFontDescriptor(fontDescriptor);
        return cidFont;
    }

    public static PDFFontDescriptor generatePDFFontDescriptor(PDFFont font, PDFFontDescription afeFontDesc) throws PDFIOException, PDFSecurityException, UnsupportedFontException, PDFInvalidDocumentException, InvalidFontException {
        PDFFontDescriptor fontDesc;
        ASName baseFont;
        PDFFontSimple simpleFont;
        if (font instanceof PDFFontSimple && PDFFontUtils.isStandardFont(baseFont = (simpleFont = (PDFFontSimple)font).getBaseFont()) && (fontDesc = PDFFontSimple.getStandardFontFontDescriptor(baseFont)) != null) {
            PDFCosFontDescriptor pdfCosDesc = fontDesc.getPDFCosDescriptor();
            CosObject fontDescCosObj = null;
            if (pdfCosDesc != null) {
                fontDescCosObj = fontDesc.getPDFCosDescriptor().getCosObject();
            } else {
                ASDictionary fontDescMap = fontDesc.getPDFDictDescriptor();
                CosDocument cosDoc = font.getPDFDocument().getCosDocument();
                if (fontDescMap != null) {
                    LinkedHashMap<ASName, CosNumeric> newFontDescMap = new LinkedHashMap<ASName, CosNumeric>();
                    for (Map.Entry entry : fontDescMap.entrySet()) {
                        ASName key = (ASName)entry.getKey();
                        Object value = entry.getValue();
                        CosNumeric valueObj = null;
                        if (value instanceof ASNumber) {
                            valueObj = cosDoc.createCosNumeric(((ASNumber)value).numberValue());
                        } else if (value instanceof ASRectangle) {
                            double[] val = ((ASRectangle)value).getValues();
                            valueObj = cosDoc.createCosArray();
                            for (int i = 0; i < val.length; ++i) {
                                ((CosArray)valueObj).add(i, (CosObject)cosDoc.createCosNumeric(val[i]));
                            }
                        } else if (value instanceof ASName) {
                            valueObj = cosDoc.createCosName((ASName)value);
                        }
                        if (value instanceof String) {
                            valueObj = cosDoc.createCosString((String)value);
                        }
                        newFontDescMap.put(key, valueObj);
                    }
                    fontDescCosObj = font.getPDFDocument().getCosDocument().createCosDictionary(newFontDescMap);
                }
            }
            if (fontDescCosObj != null) {
                return PDFFontDescriptor.getInstance(fontDescCosObj);
            }
        }
        PDFFontDescriptor fontDescriptor = null;
        PDFFontDescriptor oldFontDesc = font.getFontDescriptor();
        if (oldFontDesc != null) {
            fontDescriptor = PDFFontDescriptor.newInstance(oldFontDesc, font.getPDFDocument());
        } else {
            int flags = 0;
            FontData afeFontData = font.getAFEFontData();
            if (afeFontDesc.isAllCapFont()) {
                flags &= 65536;
            }
            if (afeFontDesc.isSerifFont()) {
                flags &= 2;
            }
            if (afeFontDesc.isSmallCapFont()) {
                flags &= 131072;
            }
            if (!afeFontData.getCoolTypeProportionalRomanFromFontProperties()) {
                flags &= 1;
            }
            if (!afeFontData.isSymbolic()) {
                flags &= 32;
            }
            fontDescriptor = PDFFontDescriptor.newInstance(font.getPDFDocument(), null, afeFontDesc, flags);
        }
        return fontDescriptor;
    }

    public static int getSimpleFontGidFromCharCode(int charCode, String glyphName, PDFSimpleFontEncoding encoding, FontData fontData, PDFFontDescriptor fontDescriptor) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException {
        try {
            if (fontData instanceof Type1Font) {
                Type1Font t1Font = (Type1Font)fontData;
                if (glyphName != null) {
                    return t1Font.glyphName2gid(glyphName);
                }
                return t1Font.charCode2gid(charCode);
            }
            if (fontData instanceof NameKeyedFont) {
                NameKeyedFont nameKeyedFont = (NameKeyedFont)fontData;
                if (glyphName != null) {
                    return nameKeyedFont.glyphName2gid(glyphName);
                }
                return nameKeyedFont.charCode2gid(charCode);
            }
            if (fontData instanceof OpenTypeFont) {
                if (((OpenTypeFont)fontData).getCFFFont() != null) {
                    if ((fontData = ((OpenTypeFont)fontData).getCFFFont()) instanceof NameKeyedFont) {
                        NameKeyedFont nameKeyedFont = (NameKeyedFont)fontData;
                        if (glyphName != null) {
                            return nameKeyedFont.glyphName2gid(glyphName);
                        }
                        return nameKeyedFont.charCode2gid(charCode);
                    }
                } else {
                    OpenTypeFont otFont = (OpenTypeFont)fontData;
                    if (encoding == null || fontDescriptor != null && (fontDescriptor.getFlags() & 4) != 0) {
                        if (otFont.cmap != null) {
                            int threeZeroOffset = otFont.cmap.probe(3, 0);
                            if (threeZeroOffset != -1) {
                                int gid = otFont.cmap.getMapping(threeZeroOffset, charCode);
                                if (gid != 0) {
                                    return gid;
                                }
                                gid = otFont.cmap.getMapping(threeZeroOffset, 61440 + charCode);
                                if (gid != 0) {
                                    return gid;
                                }
                                gid = otFont.cmap.getMapping(threeZeroOffset, 61696 + charCode);
                                if (gid != 0) {
                                    return gid;
                                }
                                gid = otFont.cmap.getMapping(threeZeroOffset, 61952 + charCode);
                                if (gid != 0) {
                                    return gid;
                                }
                                return 0;
                            }
                            int oneZeroOffset = otFont.cmap.probe(1, 0);
                            if (oneZeroOffset != -1) {
                                return otFont.cmap.getMapping(oneZeroOffset, charCode);
                            }
                        }
                        return 0;
                    }
                    if ((encoding.isMacRomanEncoding() || encoding.isWinAnsiEncoding() || fontDescriptor != null) && (fontDescriptor.getFlags() & 32) != 0) {
                        glyphName = encoding.getGlyphName(charCode);
                        if (glyphName == null && !encoding.isMacRomanEncoding() && !encoding.isWinAnsiEncoding()) {
                            glyphName = StandardEncoding.getEncoding().getGlyphName(charCode);
                        }
                        if (glyphName == null) {
                            return 0;
                        }
                        if (otFont.cmap != null) {
                            int threeOneOffset = otFont.cmap.probe(3, 1);
                            if (threeOneOffset != -1) {
                                int[] usvs = GlyphNames.resolveAGNCNameIntoArray((String)glyphName, (boolean)false);
                                if (usvs.length == 1) {
                                    return otFont.cmap.getMapping(threeOneOffset, usvs[0]);
                                }
                                if (otFont.post != null) {
                                    return otFont.post.glyphName2gid(glyphName);
                                }
                                return 0;
                            }
                            int oneZeroOffset = otFont.cmap.probe(1, 0);
                            if (oneZeroOffset != -1) {
                                for (int i = 0; i < 256; ++i) {
                                    if (!glyphName.equals(MacRomanEncoding.getGlyphNameInAugmentedMacRomanEncoding(i))) continue;
                                    return otFont.cmap.getMapping(oneZeroOffset, i);
                                }
                            }
                            if (otFont.post != null) {
                                return otFont.post.glyphName2gid(glyphName);
                            }
                            return 0;
                        }
                        return 0;
                    }
                }
                return 0;
            }
            return 0;
        }
        catch (FontEngineException e) {
            throw new PDFFontException((Throwable)e);
        }
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public static /* varargs */ double[] getGlyphWidth(boolean defaultMissingWidth, PDFFontSimple fontSimple, int ... charCodes) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        double[] widths = new double[charCodes.length];
        double missingWidth = -1.0;
        CosArray widthsArray = fontSimple.getCosDictionary().getCosArray(ASName.k_Widths);
        int firstChar = fontSimple.getFirstChar();
        int lastChar = fontSimple.getLastChar();
        if (widthsArray != null) {
            double commonWidth = widthsArray.size() == 1 ? widthsArray.getDouble(0) : -1.0;
            int i = 0;
            while (i < charCodes.length) {
                if (charCodes[i] >= firstChar && charCodes[i] <= lastChar && charCodes[i] - firstChar < widthsArray.size()) {
                    widths[i] = commonWidth >= 0.0 ? commonWidth : widthsArray.getDouble(charCodes[i] - firstChar);
                } else {
                    missingWidth = PDFFontUtils.getWidthForOutOfRangeCharCode(defaultMissingWidth, fontSimple, widths, missingWidth, i, charCodes);
                }
                ++i;
            }
            return widths;
        }
        if (!PDFFontUtils.isStandardFont(fontSimple.getBaseFont())) return widths;
        int[] standardWidths = fontSimple.getStandardFontWidths();
        int i = 0;
        while (i < charCodes.length) {
            if (charCodes[i] >= firstChar && charCodes[i] <= lastChar) {
                widths[i] = standardWidths.length == 1 ? (double)standardWidths[0] : (double)standardWidths[charCodes[i]];
                if (widths[i] == 0.0) {
                    widths[i] = PDFFontUtils.getGlyphWidthFromEmbeddedFontFile(charCodes[i], fontSimple);
                }
            } else {
                missingWidth = PDFFontUtils.getWidthForOutOfRangeCharCode(defaultMissingWidth, fontSimple, widths, missingWidth, i, charCodes);
            }
            ++i;
        }
        return widths;
    }

    private static /* varargs */ double getWidthForOutOfRangeCharCode(boolean defaultMissingWidth, PDFFontSimple fontSimple, double[] widths, double missingWidth, int charCodeIndex, int ... charCodes) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (defaultMissingWidth) {
            PDFFontDescriptor fontDescr;
            if (missingWidth < 0.0 && (fontDescr = fontSimple.getFontDescriptor()) != null) {
                missingWidth = fontDescr.getMissingWidth();
            }
            widths[charCodeIndex] = missingWidth;
        } else {
            widths[charCodeIndex] = PDFFontUtils.getGlyphWidthFromEmbeddedFontFile(charCodes[charCodeIndex], fontSimple);
        }
        return missingWidth;
    }

    public static boolean isStandardFont(ASName fontName) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        return StandardFontUtils.standardFontsNames.containsKey((Object)fontName);
    }

    public static int charCode2gid(int charCode, PDFFontType0 pdfFont, FontData fontData) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException {
        try {
            if (fontData instanceof OpenTypeFont) {
                PDFCIDFont descendantFont = pdfFont.getDescendantFont();
                int numGlyphs = fontData.getNumGlyphs();
                if (descendantFont.getSubType() == ASName.k_CIDFontType2) {
                    if (descendantFont.getFontDescriptor() != null && descendantFont.getFontDescriptor().containsEmbeddedFont()) {
                        PDFType0FontEncoding encoding = pdfFont.getEncoding();
                        int cid = encoding.getCID(charCode);
                        int gid = descendantFont.cid2gid(cid);
                        if (numGlyphs <= gid) {
                            return -1;
                        }
                        return gid;
                    }
                    return PDFFontUtils.charCode2gidForUnembeddedCIDType2(charCode, pdfFont, (OpenTypeFont)fontData, fontData.getCoolTypeScript().toInt());
                }
                if (((OpenTypeFont)fontData).getCFFFont() instanceof CIDKeyedFont) {
                    PDFType0FontEncoding encoding = pdfFont.getEncoding();
                    int cid = encoding.getCID(charCode);
                    return ((CIDKeyedFont)((OpenTypeFont)fontData).getCFFFont()).cid2gid(cid);
                }
            } else if (fontData instanceof CIDKeyedFont) {
                PDFType0FontEncoding encoding = pdfFont.getEncoding();
                int cid = encoding.getCID(charCode);
                return ((CIDKeyedFont)fontData).cid2gid(cid);
            }
            return 0;
        }
        catch (FontEngineException e) {
            throw new PDFFontException((Throwable)e);
        }
    }

    public static int getMinCharCodeBytes(PDFFontType0 font) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFType0FontEncoding fontEnc = font.getEncoding();
        PDFCMap pdfCMap = fontEnc != null ? fontEnc.getPDFCMap() : null;
        return pdfCMap == null ? 2 : pdfCMap.getMinBytesNeeded();
    }

    private static int charCode2gidForUnembeddedCIDType2(int charCode, PDFFontType0 pdfFont, OpenTypeFont otFont, int script) throws UnsupportedFontException, InvalidFontException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        Cmap cmapTable = otFont.cmap;
        if (cmapTable != null) {
            FontType2CmapSelector selector = new FontType2CmapSelector(script);
            cmapTable.enumerateCmaps((Cmap.CmapSelector)selector);
            int offset = cmapTable.getOffset(selector.getSelectedCmapIndex());
            if (selector.selectedUnicodeCmap()) {
                List vals;
                try {
                    vals = (List)pdfFont.getCharCodes(PDFCMapUtils.numToByteArray(charCode, PDFFontUtils.getMinCharCodeBytes(pdfFont)), true).get(0);
                }
                catch (Exception e) {
                    return cmapTable.getMapping(offset, charCode);
                }
                int[] unicode = (int[])vals.get(1);
                if (unicode != null && unicode.length > 0) {
                    return cmapTable.getMapping(offset, unicode[0]);
                }
            } else {
                return cmapTable.getMapping(offset, charCode);
            }
        }
        return 0;
    }

    public static List getCharCodes(byte[] bytes, boolean fetchUnicode, boolean validateCharCodeToCIDMapping, PDFFontType0 pdfFont, boolean ignoreByteLengthError) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFType0FontEncoding fontEnc = pdfFont.getEncoding();
        PDFCMap pdfCMap = null;
        ASName cmapName = fontEnc.getCMapName();
        int bytePos = 0;
        ArrayList charCodes = new ArrayList();
        byte[] charCodeBytes = null;
        boolean found = false;
        int minBytesNeeded = 1;
        int byteCount = 0;
        int maxBytesAllowed = 1;
        if (cmapName == ASName.k_Identity_H || cmapName == ASName.k_Identity_V) {
            int maxIndex = bytes.length;
            if (bytes.length % 2 > 0) {
                if (ignoreByteLengthError) {
                    --maxIndex;
                } else {
                    throw new PDFInvalidDocumentException("Number of bytes needed for identity CMap is multiple of 2");
                }
            }
            for (int i = 0; i < maxIndex; i += 2) {
                charCodeBytes = new byte[]{bytes[i], bytes[i + 1]};
                int[] unicodes = null;
                if (fetchUnicode) {
                    long cid = PDFCMapUtils.getCharCode(charCodeBytes);
                    PDFToUnicodeCMap toUnicodeCMap = pdfFont.getToUnicodeCMap();
                    if (toUnicodeCMap == null) {
                        PDFCIDSystemInfo sysInfo = pdfFont.getDescendantFont().getCIDSystemInfo();
                        if (pdfCMap == null) {
                            pdfCMap = fontEnc.getPDFCMap();
                        }
                        if ((unicodes = pdfCMap.getUnicodeIdentity(sysInfo.getRegistry().toString(), sysInfo.getOrdering().toString(), charCodeBytes)) == null) {
                            throw new PDFInvalidDocumentException("Could not fond unicode values for charcodes " + cmapName.asString());
                        }
                    } else {
                        char[] unicodeChars = toUnicodeCMap.toUnicode((int)cid);
                        unicodes = new int[unicodeChars.length];
                        for (int iUnicode = 0; iUnicode < unicodeChars.length; ++iUnicode) {
                            unicodes[iUnicode] = unicodeChars[iUnicode];
                        }
                    }
                }
                ArrayList<byte[]> charCodeWithUnicode = new ArrayList<byte[]>(2);
                charCodeWithUnicode.add(charCodeBytes);
                if (fetchUnicode) {
                    charCodeWithUnicode.add(unicodes);
                }
                charCodes.add(charCodeWithUnicode);
            }
            return charCodes;
        }
        if (pdfCMap == null) {
            pdfCMap = fontEnc.getPDFCMap();
        }
        minBytesNeeded = pdfCMap.getMinBytesNeeded();
        maxBytesAllowed = pdfCMap.getMaxBytesAllowed();
        byteCount = minBytesNeeded;
        while (bytePos < bytes.length) {
            int cid;
            charCodeBytes = new byte[byteCount];
            int posToshift = 0;
            for (int iByte = 0; iByte < byteCount; ++iByte) {
                if (bytePos + iByte >= bytes.length) {
                    ++posToshift;
                    continue;
                }
                charCodeBytes[iByte] = bytes[bytePos + iByte];
            }
            if (posToshift != 0) {
                PDFFontUtils.shiftArrayContentsToRight(charCodeBytes, posToshift);
            }
            if (fetchUnicode) {
                int[] unicodes = null;
                PDFToUnicodeCMap toUnicodeCMap = pdfFont.getToUnicodeCMap();
                if (toUnicodeCMap == null) {
                    unicodes = pdfCMap.getUnicode(charCodeBytes);
                } else {
                    char[] unicodeChars = toUnicodeCMap.toUnicode((int)PDFCMapUtils.getCharCode(charCodeBytes));
                    unicodes = new int[unicodeChars.length];
                    for (int iUnicode = 0; iUnicode < unicodeChars.length; ++iUnicode) {
                        unicodes[iUnicode] = Character.codePointAt(unicodeChars, iUnicode);
                    }
                }
                if (unicodes != null) {
                    int cid2;
                    if (validateCharCodeToCIDMapping && (cid2 = pdfCMap.getCID(charCodeBytes)) == -1) {
                        found = false;
                        if (++byteCount <= bytes.length && byteCount <= maxBytesAllowed) continue;
                        break;
                    }
                    bytePos += byteCount;
                    ArrayList<int[]> charCodeWithUnicode = new ArrayList<int[]>();
                    charCodeWithUnicode.add(charCodeBytes);
                    charCodeWithUnicode.add(unicodes);
                    charCodes.add(charCodeWithUnicode);
                    byteCount = minBytesNeeded;
                    found = true;
                    continue;
                }
                found = false;
                if (++byteCount <= bytes.length && byteCount <= maxBytesAllowed) continue;
                break;
            }
            if (validateCharCodeToCIDMapping && (cid = pdfCMap.getCID(charCodeBytes)) == -1) {
                found = false;
                if (++byteCount <= bytes.length && byteCount <= maxBytesAllowed) continue;
                break;
            }
            bytePos += byteCount;
            ArrayList<byte[]> charCodeWithUnicode = new ArrayList<byte[]>();
            charCodeWithUnicode.add(charCodeBytes);
            charCodes.add(charCodeWithUnicode);
            byteCount = minBytesNeeded;
            found = true;
        }
        if (!found) {
            PDFFontUtils.throwError(cmapName, bytes, bytePos, --byteCount);
        }
        return charCodes;
    }

    private static void throwError(ASName cmapName, byte[] bytes, int bytePos, int byteCount) throws PDFInvalidDocumentException {
        StringBuilder errMsg = new StringBuilder("Bytes ");
        for (int i = bytePos; i < byteCount && i < bytes.length; ++i) {
            errMsg.append(bytes[i]).append(' ');
        }
        errMsg.append(" is not defined in CMap ").append(cmapName.asString());
        throw new PDFInvalidDocumentException(errMsg.toString());
    }

    private static void shiftArrayContentsToRight(byte[] array, int posToShift) {
        if (array.length <= 1 || posToShift > array.length) {
            return;
        }
        for (int i = array.length - 1; i >= 0; --i) {
            array[i] = i >= posToShift ? array[i - posToShift] : 0;
        }
    }

    public static OpenTypeCmapSubTableType getOpenTypeCmapSubTableType(PDFFontSimple font) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFSimpleFontEncoding encoding = font.getEncoding();
        PDFFontDescriptor fontDescriptor = font.getFontDescriptor();
        if (encoding == null || fontDescriptor != null && (fontDescriptor.getFlags() & 4) != 0) {
            return OpenTypeCmapSubTableType.ThreeZero;
        }
        if (encoding.isMacRomanEncoding() || encoding.isWinAnsiEncoding() || fontDescriptor != null && (fontDescriptor.getFlags() & 32) != 0) {
            return OpenTypeCmapSubTableType.ThreeOne;
        }
        return null;
    }

    public static int charCode2CodePoint(int charCode, PDFFontSimple pdfFont) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, InvalidFontException, UnsupportedFontException {
        FontData fontData = pdfFont.getAFEFontData();
        if (fontData == null) {
            return 0;
        }
        if (fontData instanceof OpenTypeFont) {
            OpenTypeFont otFont = (OpenTypeFont)fontData;
            OpenTypeCmapSubTableType openTypeCmapSubTableType = PDFFontUtils.getOpenTypeCmapSubTableType(pdfFont);
            if (openTypeCmapSubTableType == OpenTypeCmapSubTableType.ThreeZero) {
                if (otFont.cmap != null) {
                    int threeZeroOffset = otFont.cmap.probe(3, 0);
                    if (threeZeroOffset != -1) {
                        if (otFont.cmap.getMapping(threeZeroOffset, charCode) != 0) {
                            return charCode;
                        }
                        if (otFont.cmap.getMapping(threeZeroOffset, 61440 + charCode) != 0) {
                            return 61440 + charCode;
                        }
                        if (otFont.cmap.getMapping(threeZeroOffset, 61696 + charCode) != 0) {
                            return 61696 + charCode;
                        }
                        if (otFont.cmap.getMapping(threeZeroOffset, 61952 + charCode) != 0) {
                            return 61952 + charCode;
                        }
                        return 0;
                    }
                    int oneZeroOffset = otFont.cmap.probe(1, 0);
                    if (oneZeroOffset != -1) {
                        return charCode;
                    }
                }
                return 0;
            }
            if (openTypeCmapSubTableType == OpenTypeCmapSubTableType.ThreeOne) {
                PDFSimpleFontEncoding encoding = pdfFont.getEncoding();
                String glyphName = encoding.getGlyphName(charCode);
                if (glyphName == null && !encoding.isMacRomanEncoding() && !encoding.isWinAnsiEncoding()) {
                    glyphName = StandardEncoding.getEncoding().getGlyphName(charCode);
                }
                if (glyphName == null) {
                    return 0;
                }
                if (otFont.cmap != null) {
                    int threeOneOffset = otFont.cmap.probe(3, 1);
                    if (threeOneOffset != -1) {
                        int[] usvs = GlyphNames.resolveAGNCNameIntoArray((String)glyphName, (boolean)false);
                        if (usvs.length == 1) {
                            return usvs[0];
                        }
                        return 0;
                    }
                    int oneZeroOffset = otFont.cmap.probe(1, 0);
                    if (oneZeroOffset != -1) {
                        for (int i = 0; i < 256; ++i) {
                            if (!glyphName.equals(MacRomanEncoding.getGlyphNameInAugmentedMacRomanEncoding(i))) continue;
                            return i;
                        }
                        return 0;
                    }
                }
                return 0;
            }
            return 0;
        }
        return 0;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public static String charCode2glyphName(int charCode, PDFFontSimple pdfFont, FontData fontData) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFFontException {
        if (fontData == null) {
            return null;
        }
        String glyphName = null;
        PDFSimpleFontEncoding encoding = pdfFont.getEncoding();
        if (fontData instanceof Type1Font || fontData instanceof NameKeyedFont || fontData instanceof OpenTypeFont && ((OpenTypeFont)fontData).getCFFFont() != null) {
            glyphName = encoding != null ? encoding.getGlyphName(charCode, pdfFont.getBaseFont(), PDFFontUtils.isFontEmbedded(pdfFont)) : pdfFont.getGlyphNameFromImplicitBaseEncoding(charCode);
        }
        if (glyphName != null) return glyphName;
        if (fontData instanceof Type1Font) {
            return ((Type1Font)fontData).charCode2GlyphName(charCode);
        }
        if (fontData instanceof NameKeyedFont) {
            try {
                int gid = ((NameKeyedFont)fontData).charCode2gid(charCode);
                if (gid < 0) return glyphName;
                return ((NameKeyedFont)fontData).getGlyphName(gid);
            }
            catch (FontEngineException e) {
                throw new PDFFontException((Throwable)e);
            }
        }
        if (!(fontData instanceof OpenTypeFont)) return glyphName;
        if (((OpenTypeFont)fontData).getCFFFont() == null) return glyphName;
        if (!((fontData = ((OpenTypeFont)fontData).getCFFFont()) instanceof NameKeyedFont)) return glyphName;
        try {
            int gid = ((NameKeyedFont)fontData).charCode2gid(charCode);
            if (gid < 0) return glyphName;
            return ((NameKeyedFont)fontData).getGlyphName(gid);
        }
        catch (FontEngineException e) {
            throw new PDFInvalidDocumentException((Throwable)e);
        }
    }

    public static double getGlyphWidthFromEmbeddedFontFile(int charCode, PDFFontSimple pdfFont) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        double width = 0.0;
        try {
            FontData fdata;
            if (pdfFont.getBaseFont() != null && (fdata = pdfFont.getAFEFontData()) != null) {
                int glyphId = fdata.getGlyphForChar(charCode);
                width = fdata.getHorizontalAdvance(glyphId);
                double unitesPerEmX = fdata.getUnitsPerEmX();
                if (unitesPerEmX != 0.0) {
                    return width * 1000.0 / unitesPerEmX;
                }
            }
        }
        catch (InvalidFontException e) {
            throw new PDFIOException((Throwable)e);
        }
        catch (UnsupportedFontException e) {
            throw new PDFIOException((Throwable)e);
        }
        return width;
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    public static enum OpenTypeCmapSubTableType {
        ThreeZero,
        ThreeOne;
        

        private OpenTypeCmapSubTableType() {
        }
    }

    private static class FontType2CmapSelector
    implements Cmap.CmapSelector {
        private boolean unicodeMSSelected = false;
        private int lastMScmap = -1;
        private int lastUnicmap = -1;
        private int lastMaccmap = -1;
        private final int writingScript;

        FontType2CmapSelector(int writingScript) {
            this.writingScript = writingScript;
        }

        public void cmapFound(int platformID, int platformEncoding, int index) throws InvalidFontException, UnsupportedFontException {
            if (platformID == 3) {
                this.unicodeMSSelected = platformEncoding == 1 || platformEncoding == 10;
                this.lastMScmap = index;
            } else if (platformID == 0) {
                this.lastUnicmap = index;
            } else if (platformID == 1 && platformEncoding == this.writingScript) {
                this.lastMaccmap = index;
            }
        }

        int getSelectedCmapIndex() {
            if (this.lastMScmap != -1) {
                return this.lastMScmap;
            }
            if (this.lastUnicmap != -1) {
                return this.lastUnicmap;
            }
            return this.lastMaccmap;
        }

        boolean selectedUnicodeCmap() {
            if (this.lastMScmap != -1) {
                return this.unicodeMSSelected;
            }
            return this.lastUnicmap != -1;
        }
    }

    private static class GlyphDesc
    implements GlyphIDHolder {
        int gid;

        GlyphDesc(int gid) {
            this.gid = gid;
        }

        public int getGlyphID() {
            return this.gid;
        }
    }

    private static class GlyphIterator
    implements Iterator {
        int numGlyphs;
        int currGlyph;

        GlyphIterator(int numGlyphs) {
            this.numGlyphs = numGlyphs;
            this.currGlyph = 0;
        }

        public boolean hasNext() {
            return this.currGlyph < this.numGlyphs;
        }

        public Object next() {
            return new GlyphDesc(this.currGlyph++);
        }

        public void remove() {
        }
    }

}