IndicFormatter.java 19.8 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.agl.util.ULocale
 */
package com.adobe.fontengine.inlineformatting.infontformatting;

import com.adobe.agl.util.ULocale;
import com.adobe.fontengine.CharUtil;
import com.adobe.fontengine.font.FontData;
import com.adobe.fontengine.font.InvalidFontException;
import com.adobe.fontengine.font.UnsupportedFontException;
import com.adobe.fontengine.font.opentype.Gdef;
import com.adobe.fontengine.font.opentype.Gpos;
import com.adobe.fontengine.font.opentype.Gsub;
import com.adobe.fontengine.font.opentype.OTSelector;
import com.adobe.fontengine.font.opentype.OTSelectorOnElementAttribute;
import com.adobe.fontengine.font.opentype.OpenTypeFont;
import com.adobe.fontengine.font.opentype.Tag;
import com.adobe.fontengine.inlineformatting.AttributedRun;
import com.adobe.fontengine.inlineformatting.ElementAttribute;
import com.adobe.fontengine.inlineformatting.infontformatting.GenericFormatter;
import com.adobe.fontengine.inlineformatting.infontformatting.InFontFormatter;
import com.adobe.fontengine.inlineformatting.infontformatting.LookupsCache;
import java.util.ArrayList;

abstract class IndicFormatter
extends GenericFormatter {
    private static final ElementAttribute indicShape = new ElementAttribute("indicShape");
    private static final OTSelector everywhere = new OTSelector();
    private static final OTSelector akhnSelector = new OTSelector(){

        public boolean isApplied(AttributedRun run, int position) {
            Object v = run.getElementStyle(position, indicShape);
            return v != Shape.explicitVirama && v != Shape.half;
        }
    };
    private static final OTSelector rphfSelector = new OTSelector(){

        public boolean isApplied(AttributedRun run, int position) {
            Object v = run.getElementStyle(position, indicShape);
            return v == Shape.rephCons || v == Shape.rephVowel;
        }
    };
    private static final OTSelector blwfSelector = new OTSelectorOnElementAttribute(indicShape, Shape.subjoins, true);
    private static final OTSelector pstfSelector = new OTSelectorOnElementAttribute(indicShape, Shape.postjoins, true);
    private static final OTSelector halfSelector = new OTSelector(){

        public boolean isApplied(AttributedRun run, int position) {
            Object v = run.getElementStyle(position, indicShape);
            return v != Shape.explicitVirama;
        }
    };
    static final int[] gsubFeatures = new int[]{Tag.feature_nukt, Tag.feature_akhn, Tag.feature_rphf, Tag.feature_blwf, Tag.feature_half, Tag.feature_vatu, Tag.feature_pres, Tag.feature_abvs, Tag.feature_blws, Tag.feature_psts, Tag.feature_haln, Tag.feature_pstf};
    static final int[] gposFeatures = new int[]{Tag.feature_abvm, Tag.feature_blwm, Tag.feature_dist};

    IndicFormatter() {
    }

    protected abstract int nukta();

    protected abstract int virama();

    protected abstract int splitVowelsAndNormalize(AttributedRun var1, int var2, int var3);

    protected abstract Position getPosition(int var1);

    protected abstract boolean isConsonant(int var1);

    protected abstract boolean hasNukta(int var1);

    protected abstract int removeNukta(int var1);

    protected abstract boolean isMark(int var1);

    protected abstract boolean isIndependentVowel(int var1);

    protected abstract Shape rephLike(int var1);

    protected abstract boolean subjoins(int var1);

    protected abstract boolean postjoins(int var1);

    protected abstract boolean postjoinsIndependentVowels(int var1);

    private int processAksaraMarks(AttributedRun run, int start, int limit, Aksara aksara) {
        aksara.firstMark = start;
        while (start < limit && this.isMark(run.elementAt(start))) {
            ++start;
        }
        aksara.limit = start;
        return start;
    }

    private int processAksaras(AttributedRun run, int start, int limit, OpenTypeFont font) throws InvalidFontException, UnsupportedFontException {
        Aksara aksara = new Aksara();
        while (start < limit) {
            aksara.reset();
            aksara.start = start;
            if (this.isConsonant(run.elementAt(start))) {
                while (start < limit && this.isConsonant(run.elementAt(start))) {
                    AksaraPart part = new AksaraPart();
                    part.shape = Shape.live;
                    if (this.hasNukta(run.elementAt(start))) {
                        part.nuktaCount = 1;
                        part.usv = this.removeNukta(run.elementAt(start));
                    } else {
                        part.nuktaCount = 0;
                        part.usv = run.elementAt(start);
                    }
                    ++start;
                    while (start < limit && this.nukta() == run.elementAt(start)) {
                        ++part.nuktaCount;
                        ++start;
                    }
                    if (start < limit && this.virama() == run.elementAt(start)) {
                        part.shape = Shape.dead;
                        if (++start < limit && 8205 == run.elementAt(start)) {
                            part.shape = Shape.half;
                            ++start;
                        } else if (start < limit && 8204 == run.elementAt(start)) {
                            part.shape = Shape.explicitVirama;
                            ++start;
                        }
                    }
                    aksara.parts.add(part);
                    if (part.shape != Shape.live) continue;
                    break;
                }
                if (aksara.parts.size() > 1) {
                    AksaraPart firstPart = (AksaraPart)aksara.parts.get(0);
                    Shape rephShape = this.rephLike(firstPart.usv);
                    if (rephShape != Shape.any && firstPart.shape == Shape.dead) {
                        firstPart.shape = rephShape;
                    }
                }
                for (int i = 1; i < aksara.parts.size(); ++i) {
                    AksaraPart part = (AksaraPart)aksara.parts.get(i);
                    AksaraPart prevPart = (AksaraPart)aksara.parts.get(i - 1);
                    if (!this.subjoins(part.usv) || part.shape != Shape.dead && part.shape != Shape.live || prevPart.shape != Shape.dead) continue;
                    if (part.shape == Shape.live) {
                        prevPart.shape = Shape.live;
                    }
                    part.shape = Shape.subjoins;
                }
                if (aksara.parts.size() > 1) {
                    AksaraPart part = (AksaraPart)aksara.parts.get(aksara.parts.size() - 1);
                    AksaraPart prevPart = (AksaraPart)aksara.parts.get(aksara.parts.size() - 2);
                    if (this.postjoins(part.usv) && (part.shape == Shape.dead || part.shape == Shape.live) && prevPart.shape == Shape.dead) {
                        if (part.shape == Shape.live) {
                            prevPart.shape = Shape.live;
                        }
                        part.shape = Shape.postjoins;
                    }
                }
                start = this.processAksaraMarks(run, start, limit, aksara);
            } else if (this.isIndependentVowel(run.elementAt(start))) {
                AksaraPart part = new AksaraPart();
                part.shape = Shape.live;
                part.usv = run.elementAt(start);
                part.nuktaCount = 0;
                aksara.parts.add(part);
                ++start;
                while (start + 1 < limit && this.virama() == run.elementAt(start) && this.postjoinsIndependentVowels(run.elementAt(start + 1))) {
                    part = new AksaraPart();
                    part.shape = Shape.postjoins;
                    part.usv = run.elementAt(start + 1);
                    part.nuktaCount = 0;
                    aksara.parts.add(part);
                    start += 2;
                }
                start = this.processAksaraMarks(run, start, limit, aksara);
            } else {
                AksaraPart part = new AksaraPart();
                part.shape = Shape.live;
                part.usv = run.elementAt(start);
                part.nuktaCount = 0;
                aksara.parts.add(part);
                aksara.firstMark = ++start;
                aksara.limit = start;
            }
            int newStart = aksara.toGlyphs(run, font);
            limit += newStart - start;
            start = newStart;
        }
        return limit;
    }

    protected int canRenderWithFont(OpenTypeFont font, AttributedRun run, int start, int limit, boolean notdefOk) throws InvalidFontException, UnsupportedFontException {
        int graphemeLimit;
        int usvc;
        int gid;
        int usv = run.elementAt(start);
        if (CharUtil.isControl(usv)) {
            return 1;
        }
        if (!CharUtil.isBase(usv) || start + 1 == limit || run.getElementStyle(start + 1, ElementAttribute.isGlyph) == Boolean.TRUE || !CharUtil.isCombining(run.elementAt(start + 1))) {
            int gid2 = font.getGlyphForChar(usv);
            return gid2 == 0 ? 0 : start + 1;
        }
        for (graphemeLimit = start + 1; graphemeLimit < limit && run.getElementStyle(graphemeLimit, ElementAttribute.isGlyph) == Boolean.FALSE && CharUtil.isCombining(run.elementAt(graphemeLimit)); ++graphemeLimit) {
        }
        boolean directMappingHasNotdef = false;
        int[] usvs = new int[graphemeLimit - start];
        int[] gids = new int[graphemeLimit - start];
        for (int i = start; i < graphemeLimit; ++i) {
            usvs[i - start] = run.elementAt(i);
            gid = font.getGlyphForChar(usvs[i - start]);
            if (gid == 0) {
                directMappingHasNotdef = true;
            }
            gids[i - start] = gid;
        }
        if (directMappingHasNotdef && (usvc = CharUtil.compose(usvs, 0, graphemeLimit - start)) != -1 && (gid = font.getGlyphForChar(usvc)) != 0) {
            return graphemeLimit - start;
        }
        if (notdefOk) {
            return graphemeLimit - start;
        }
        return 0;
    }

    protected boolean canFormatOT() {
        return true;
    }

    protected int formatOT(OpenTypeFont otFont, AttributedRun run, int start, int limit, boolean shouldKern) throws InvalidFontException, UnsupportedFontException {
        int scriptTag = IndicFormatter.getOTScriptTag((Integer)run.getElementStyle(start, InFontFormatter.scriptAttribute));
        int langTag = IndicFormatter.getOTLanguageTag((ULocale)run.getElementStyle(start, ElementAttribute.locale));
        limit = this.splitVowelsAndNormalize(run, start, limit);
        limit = this.processAksaras(run, start, limit, otFont);
        if (otFont.gsub != null) {
            int[][] gsubLookups = LookupsCache.resolveFeatureTag(otFont.gsub, scriptTag, langTag, gsubFeatures);
            limit = otFont.gsub.applyLookups(gsubLookups[0], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[1], run, start, limit, akhnSelector, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[2], run, start, limit, rphfSelector, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[3], run, start, limit, blwfSelector, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[11], run, start, limit, pstfSelector, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[4], run, start, limit, halfSelector, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[5], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[6], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[7], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[8], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[9], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gsub.applyLookups(gsubLookups[10], run, start, limit, everywhere, otFont.gdef);
        }
        this.posFromAdvanceWidth(run, otFont, start, limit);
        if (otFont.gpos != null) {
            int[][] gposLookups = LookupsCache.resolveFeatureTag(otFont.gpos, scriptTag, langTag, gposFeatures);
            limit = otFont.gpos.applyLookups(gposLookups[0], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gpos.applyLookups(gposLookups[1], run, start, limit, everywhere, otFont.gdef);
            limit = otFont.gpos.applyLookups(gposLookups[2], run, start, limit, everywhere, otFont.gdef);
        }
        return limit;
    }

    final class Aksara {
        public int start;
        public int firstMark;
        public int limit;
        public ArrayList parts;

        public Aksara() {
            this.reset();
        }

        public void reset() {
            this.parts = new ArrayList();
        }

        public int nbGlyphs() {
            int nbGlyphs = this.limit - this.firstMark;
            for (int i = 0; i < this.parts.size(); ++i) {
                nbGlyphs += ((AksaraPart)this.parts.get(i)).nbGlyphs();
            }
            return nbGlyphs;
        }

        public int addGlyph(int[] glyphIds, Shape[] glyphForms, int glyphCount, int ch, Shape shape, OpenTypeFont font) throws InvalidFontException, UnsupportedFontException {
            glyphIds[glyphCount] = font.getGlyphForChar(ch);
            glyphForms[glyphCount] = shape;
            return glyphCount + 1;
        }

        public int toGlyphs(AttributedRun run, OpenTypeFont font) throws InvalidFontException, UnsupportedFontException {
            int i;
            int i2;
            int[] glyphIds = new int[this.nbGlyphs()];
            Shape[] glyphForms = new Shape[glyphIds.length];
            int glyphCount = 0;
            for (int i3 = this.firstMark; i3 < this.limit; ++i3) {
                int usv = run.elementAt(i3);
                if (IndicFormatter.this.getPosition(usv) != Position.left) continue;
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, usv, Shape.any, font);
            }
            AksaraPart firstPart = null;
            AksaraPart lastPart = null;
            for (i2 = 0; i2 < this.parts.size(); ++i2) {
                AksaraPart part = (AksaraPart)this.parts.get(i2);
                if (firstPart == null) {
                    firstPart = part;
                }
                lastPart = part;
                if (part.shape == Shape.rephCons || part.shape == Shape.rephVowel || part.shape == Shape.postjoins) continue;
                glyphCount = part.toGlyphs(glyphIds, glyphForms, glyphCount, font);
            }
            for (i2 = this.firstMark; i2 < this.limit; ++i2) {
                int usv = run.elementAt(i2);
                if (IndicFormatter.this.getPosition(usv) != Position.bottom) continue;
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, usv, Shape.any, font);
            }
            for (i2 = this.firstMark; i2 < this.limit; ++i2) {
                int usv = run.elementAt(i2);
                if (IndicFormatter.this.getPosition(usv) != Position.topMatra) continue;
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, usv, Shape.any, font);
            }
            if (firstPart.shape == Shape.rephCons) {
                glyphCount = firstPart.toGlyphs(glyphIds, glyphForms, glyphCount, font);
            }
            if (lastPart.shape == Shape.postjoins) {
                glyphCount = lastPart.toGlyphs(glyphIds, glyphForms, glyphCount, font);
            }
            for (i2 = this.firstMark; i2 < this.limit; ++i2) {
                int usv = run.elementAt(i2);
                if (IndicFormatter.this.getPosition(usv) != Position.rightMatra) continue;
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, usv, Shape.any, font);
            }
            if (firstPart.shape == Shape.rephVowel) {
                glyphCount = firstPart.toGlyphs(glyphIds, glyphForms, glyphCount, font);
            }
            for (i2 = this.firstMark; i2 < this.limit; ++i2) {
                int usv = run.elementAt(i2);
                if (IndicFormatter.this.getPosition(usv) != Position.topOther) continue;
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, usv, Shape.any, font);
            }
            for (i2 = this.firstMark; i2 < this.limit; ++i2) {
                int usv = run.elementAt(i2);
                if (IndicFormatter.this.getPosition(usv) != Position.rightOther) continue;
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, usv, Shape.any, font);
            }
            int[] positions = new int[this.limit - this.start];
            for (i = 0; i < positions.length; ++i) {
                positions[i] = this.start + i;
            }
            run.replace(positions, glyphIds);
            run.setElementStyle(this.start, this.start + glyphIds.length, ElementAttribute.isGlyph, Boolean.TRUE);
            for (i = 0; i < glyphIds.length; ++i) {
                run.setElementStyle(this.start + i, indicShape, glyphForms[i]);
            }
            return this.start + glyphIds.length;
        }
    }

    final class AksaraPart {
        public int usv;
        public int nuktaCount;
        public Shape shape;

        AksaraPart() {
        }

        public int nbGlyphs() {
            return 1 + this.nuktaCount + (this.shape != Shape.live ? 1 : 0);
        }

        public int addGlyph(int[] glyphIds, Shape[] glyphForms, int glyphCount, int ch, Shape shape, OpenTypeFont font) throws InvalidFontException, UnsupportedFontException {
            glyphIds[glyphCount] = font.getGlyphForChar(ch);
            glyphForms[glyphCount] = shape;
            return glyphCount + 1;
        }

        public int toGlyphs(int[] glyphIds, Shape[] glyphForms, int glyphCount, OpenTypeFont font) throws InvalidFontException, UnsupportedFontException {
            glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, this.usv, this.shape, font);
            for (int i = 0; i < this.nuktaCount; ++i) {
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, IndicFormatter.this.nukta(), Shape.any, font);
            }
            if (this.shape != Shape.live) {
                glyphCount = this.addGlyph(glyphIds, glyphForms, glyphCount, IndicFormatter.this.virama(), this.shape, font);
            }
            return glyphCount;
        }
    }

    public static final class Position {
        private String name;
        public static final Position any = new Position("any");
        public static final Position left = new Position("left");
        public static final Position topMatra = new Position("topMatra");
        public static final Position topOther = new Position("topOther");
        public static final Position bottom = new Position("bottom");
        public static final Position rightMatra = new Position("rightMatra");
        public static final Position rightOther = new Position("rightOther");

        private Position(String name) {
            this.name = name;
        }

        public String toString() {
            return this.name;
        }
    }

    public static final class Shape {
        private String name;
        public static final Shape any = new Shape("any");
        public static final Shape live = new Shape("live");
        public static final Shape dead = new Shape("dead");
        public static final Shape half = new Shape("half");
        public static final Shape explicitVirama = new Shape("explicitVirama");
        public static final Shape rephCons = new Shape("rephCons");
        public static final Shape rephVowel = new Shape("rephVowel");
        public static final Shape vattu = new Shape("vattu");
        public static final Shape subjoins = new Shape("subjoins");
        public static final Shape postjoins = new Shape("postjoins");

        private Shape(String name) {
            this.name = name;
        }

        public String toString() {
            return this.name;
        }
    }

}