PDFCIDFontWidths.java 14.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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.fontengine.font.Font
 *  com.adobe.fontengine.font.FontData
 *  com.adobe.fontengine.font.FontImpl
 *  com.adobe.fontengine.font.FontLoadingException
 *  com.adobe.fontengine.font.InvalidFontException
 *  com.adobe.fontengine.font.PDFFontDescription
 *  com.adobe.fontengine.font.UnsupportedFontException
 *  com.adobe.fontengine.font.opentype.OpenTypeFont
 *  com.adobe.internal.pdftoolkit.core.cos.CosArray
 *  com.adobe.internal.pdftoolkit.core.cos.CosNumeric
 *  com.adobe.internal.pdftoolkit.core.cos.CosObject
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 */
package com.adobe.internal.pdftoolkit.pdf.graphics.font;

import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.FontData;
import com.adobe.fontengine.font.FontImpl;
import com.adobe.fontengine.font.FontLoadingException;
import com.adobe.fontengine.font.InvalidFontException;
import com.adobe.fontengine.font.PDFFontDescription;
import com.adobe.fontengine.font.UnsupportedFontException;
import com.adobe.fontengine.font.opentype.OpenTypeFont;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.GlyphIDHolder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.TreeSet;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class PDFCIDFontWidths
extends PDFCosObject {
    private Font afeFont = null;
    private TreeSet widths;
    private int cachedCID;
    private boolean isCIDCached = false;
    private WidthEntry cachedEntry;
    private CosObject widthObj;

    public void buildWidths(Iterator<GlyphIDHolder> gidIter, PDFFontDescription desc, double dw) throws InvalidFontException, UnsupportedFontException, FontLoadingException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ArrayList<Entry> cidList = new ArrayList<Entry>();
        while (gidIter.hasNext()) {
            int gid = gidIter.next().getGlyphID();
            int cid = desc.getGlyphCid(gid);
            if (this.containsWidth(cid) || (double)Math.round(desc.getAdvance(gid)) == dw) continue;
            cidList.add(new Entry(gid, cid));
        }
        if (cidList.isEmpty()) {
            return;
        }
        Object[] cidArray = new Entry[cidList.size()];
        cidArray = cidList.toArray(cidArray);
        Arrays.sort(cidArray);
        int i = 0;
        double[] widths = new double[cidArray.length];
        int startGID = cidArray[0].gid;
        int startCID = cidArray[0].cid;
        while (i < cidArray.length) {
            int lastCID = startCID;
            int nextGID = 0;
            int nextCID = 0;
            widths[0] = desc.getAdvance(startGID);
            int j = i + 1;
            if (j < cidArray.length) {
                nextGID = cidArray[j].gid;
                nextCID = cidArray[j].cid;
                double nextWidth = desc.getAdvance(nextGID);
                while (nextCID == startCID + (j - i)) {
                    widths[j - i] = nextWidth;
                    lastCID = nextCID;
                    if (++j >= cidArray.length) break;
                    nextGID = cidArray[j].gid;
                    nextCID = cidArray[j].cid;
                    nextWidth = desc.getAdvance(nextGID);
                }
            }
            this.addWidths(widths, startCID, lastCID);
            i = j;
            startCID = nextCID;
            startGID = nextGID;
        }
    }

    private PDFCIDFontWidths(CosObject cosObject) throws PDFInvalidDocumentException {
        super(cosObject);
        this.widthObj = cosObject;
    }

    private PDFCIDFontWidths(CosObject cosObject, Font afeFont) throws PDFInvalidDocumentException {
        super(cosObject);
        this.afeFont = afeFont;
        this.widthObj = cosObject;
    }

    public static PDFCIDFontWidths getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
        if (PDFCosObject.checkNullCosObject(cosObject) == null) {
            return null;
        }
        PDFCIDFontWidths pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFCIDFontWidths.class);
        if (pdfObject == null) {
            pdfObject = new PDFCIDFontWidths(cosObject);
        }
        pdfObject.buildWidthsTable();
        return pdfObject;
    }

    public static PDFCIDFontWidths newInstance(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosArray cosObject = PDFCosObject.newCosArray(pdfDocument);
        PDFCIDFontWidths pdfObject = new PDFCIDFontWidths((CosObject)cosObject);
        pdfObject.widths = new TreeSet();
        pdfObject.widthObj = null;
        return pdfObject;
    }

    public static PDFCIDFontWidths newInstance(PDFDocument pdfDocument, Font afeFont) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosArray cosObject = PDFCosObject.newCosArray(pdfDocument);
        PDFCIDFontWidths pdfObject = new PDFCIDFontWidths((CosObject)cosObject, afeFont);
        pdfObject.widths = new TreeSet();
        pdfObject.widthObj = null;
        pdfObject.buildWidthsTableFromFont();
        return pdfObject;
    }

    public static PDFCIDFontWidths newInstance(PDFDocument pdfDocument, double[] widths, int startCID) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        PDFCIDFontWidths pdfObject = PDFCIDFontWidths.newInstance(pdfDocument);
        pdfObject.addWidths(widths, startCID, startCID + widths.length - 1);
        return pdfObject;
    }

    public void addWidths(double[] widths, int startCID, int endCID) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        CosArray cosWidths = PDFCosObject.newCosArray(this.getPDFDocument());
        for (int i = startCID; i <= endCID; ++i) {
            cosWidths.addDouble(widths[i - startCID]);
        }
        this.getCosArray().addInt(startCID);
        this.getCosArray().add((CosObject)cosWidths);
        this.updateWidthsTable(startCID, cosWidths);
    }

    public void addWidths(ArrayList<Double> widths, int startCID, int endCID) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (!widths.isEmpty()) {
            double[] dwidths = new double[widths.size()];
            for (int i = 0; i < widths.size(); ++i) {
                dwidths[i] = widths.get(i);
            }
            this.addWidths(dwidths, startCID, endCID);
            widths.clear();
        }
    }

    public boolean containsWidth(int cid) {
        if (this.getEntry(cid) != null) {
            return true;
        }
        return false;
    }

    public double getWidth(int cid) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        WidthEntry entry = this.getEntry(cid);
        if (entry != null) {
            return entry.getWidth(cid);
        }
        return 0.0;
    }

    private WidthEntry getEntry(int cid) {
        if (this.isCIDCached && cid == this.cachedCID) {
            return this.cachedEntry;
        }
        for (WidthEntry entry : this.widths) {
            if (!entry.contains(cid)) continue;
            this.isCIDCached = true;
            this.cachedCID = cid;
            this.cachedEntry = entry;
            return entry;
        }
        return null;
    }

    private void buildWidthsTable() {
        if (this.widths == null) {
            this.widths = new TreeSet();
            Iterator iter = this.getCosArray().iterator();
            while (iter.hasNext()) {
                CosObject cosobj = (CosObject)iter.next();
                if (!(cosobj instanceof CosNumeric)) continue;
                int startCID = ((CosNumeric)cosobj).intValue();
                Object item = iter.next();
                if (item instanceof CosArray) {
                    this.widths.add(new WidthEntry(startCID, (CosArray)item));
                    continue;
                }
                int endCID = ((CosNumeric)item).intValue();
                double width = ((CosNumeric)iter.next()).doubleValue();
                this.widths.add(new WidthEntry(startCID, endCID, width));
            }
        }
    }

    private void buildWidthsTableFromFont() {
        try {
            if (this.afeFont != null && ((FontImpl)this.afeFont).getFontData() instanceof OpenTypeFont) {
                OpenTypeFont otFont = (OpenTypeFont)((FontImpl)this.afeFont).getFontData();
                this.widths = new TreeSet();
                PDFFontDescription pdfDesc = this.afeFont.getPDFFontDescription();
                GlyphIterator gidIter = new GlyphIterator(pdfDesc.getNumGlyphs());
                ArrayList<Double> widths = new ArrayList<Double>();
                int startCID = 0;
                int endCID = 0;
                while (gidIter.hasNext()) {
                    int gid = ((GlyphIDHolder)gidIter.next()).getGlyphID();
                    int cid = otFont.getGlyphCid(gid);
                    double width = otFont.getHorizontalAdvance(gid);
                    if (cid != endCID + 1) {
                        this.addWidths(widths, startCID, endCID);
                    }
                    if (width > 0.0) {
                        if (widths.isEmpty()) {
                            startCID = cid;
                        }
                        widths.add(new Double(width));
                    } else {
                        this.addWidths(widths, startCID, endCID);
                    }
                    endCID = cid;
                }
                this.addWidths(widths, startCID, endCID);
            }
        }
        catch (Exception e) {
            // empty catch block
        }
    }

    private void updateWidthsTable(int startCID, CosArray newWidths) {
        if (this.widths == null) {
            this.widths = new TreeSet();
        }
        this.widths.add(new WidthEntry(startCID, newWidths));
    }

    public CosObject toCosArray() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        if (this.widthObj != null) {
            return this.widthObj;
        }
        if (this.widths == null) {
            return null;
        }
        CosArray cosCIDFontWidths = PDFCosObject.newCosArray(this.getPDFDocument());
        for (WidthEntry widthEntry : this.widths) {
            CosArray cosWidths = widthEntry.getWidths();
            if (cosWidths != null) {
                cosCIDFontWidths.addInt(widthEntry.getStartCID());
                cosCIDFontWidths.add((CosObject)cosWidths);
                continue;
            }
            cosCIDFontWidths.addInt(widthEntry.getStartCID());
            cosCIDFontWidths.addInt(widthEntry.getEndCID());
            cosCIDFontWidths.addDouble(widthEntry.getWidth(widthEntry.getStartCID()));
        }
        return cosCIDFontWidths;
    }

    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() {
        }
    }

    private static class GlyphDesc
    implements GlyphIDHolder {
        int gid;

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

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

    static class WidthEntry
    implements Comparable {
        private final int startCID;
        private final int endCID;
        private final CosArray widths;
        private final double width;

        WidthEntry(int startCID, CosArray widths) {
            this.startCID = startCID;
            this.endCID = startCID + widths.size() - 1;
            this.widths = widths;
            this.width = 0.0;
        }

        WidthEntry(int startCID, int endCID, double width) {
            this.startCID = startCID;
            this.endCID = endCID;
            this.widths = null;
            this.width = width;
        }

        public String toString() {
            StringBuilder result = new StringBuilder();
            if (this.widths != null) {
                result.append(this.startCID).append(" [ ");
                Iterator iter = this.widths.iterator();
                while (iter.hasNext()) {
                    result = result.append(((CosNumeric)iter.next()).intValue()).append(" ");
                }
                result.append("]");
            } else {
                result.append(this.startCID).append(" ").append(this.endCID).append(" ");
                result.append(this.width);
            }
            return result.toString();
        }

        int getStartCID() {
            return this.startCID;
        }

        int getEndCID() {
            return this.endCID;
        }

        CosArray getWidths() {
            return this.widths;
        }

        boolean contains(int cid) {
            if (cid >= this.startCID && cid <= this.endCID) {
                return true;
            }
            return false;
        }

        double getWidth(int cid) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
            if (this.widths != null) {
                return this.widths.getDouble(cid - this.startCID);
            }
            return this.width;
        }

        public int compareTo(Object o) {
            int startDiff = this.startCID - ((WidthEntry)o).startCID;
            if (startDiff != 0) {
                return startDiff;
            }
            return this.endCID - ((WidthEntry)o).endCID;
        }
    }

    private static class Entry
    implements Comparable {
        int gid;
        int cid;

        public int compareTo(Object o) {
            return this.cid - ((Entry)o).cid;
        }

        Entry(int gid, int cid) {
            this.gid = gid;
            this.cid = cid;
        }
    }

}