MappingManager.java 23.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
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xfa.text;

import com.adobe.xfa.text.AFEElement;
import com.adobe.xfa.text.AFERun;
import com.adobe.xfa.text.DispLine;
import com.adobe.xfa.text.DispLineWrapped;
import com.adobe.xfa.text.DispMap;
import com.adobe.xfa.text.DispMapItem;
import com.adobe.xfa.text.DispRun;
import com.adobe.xfa.text.DispTab;
import com.adobe.xfa.text.GlyphLoc;
import com.adobe.xfa.text.TextAttr;
import com.adobe.xfa.text.TextCharProp;

class MappingManager {
    private final ExplicitRunProcessor mExplicitRunProcessor = new ExplicitRunProcessor();
    private DispLine mLine;
    private int mCharCount;
    private byte[] mLevels;
    private int[] mDirections;

    MappingManager() {
    }

    void analyze(DispLine line) {
        this.mLine = line;
        this.mCharCount = line.getCharCount();
        if (this.mCharCount == 0) {
            return;
        }
        if (!this.mLine.hasBIDI()) {
            return;
        }
        if (this.mDirections == null || this.mDirections.length < this.mCharCount) {
            this.mDirections = new int[this.mCharCount];
            this.mLevels = new byte[this.mCharCount];
        }
        this.mExplicitRunProcessor.setup(this.mLine, this.mDirections, this.mLevels);
        this.mExplicitRunProcessor.run();
        WeakTypeResolver weakResolver = new WeakTypeResolver();
        weakResolver.execute();
        NeutralTypeResolver neutralResolver = new NeutralTypeResolver();
        neutralResolver.execute();
        for (int i = 0; i < this.mCharCount; ++i) {
            byte level = this.mLevels[i];
            int direction = this.mDirections[i];
            int defaultDirection = MappingManager.getDirectionFromLevel(level);
            int add = 0;
            switch (direction) {
                case 262144: 
                case 1310720: {
                    if (defaultDirection == 3670016) {
                        add = 1;
                        break;
                    }
                    add = 2;
                    break;
                }
                case 2097152: {
                    if (defaultDirection != 3670016) break;
                    add = 1;
                    break;
                }
                case 3670016: {
                    if (defaultDirection != 2097152) break;
                    add = 1;
                }
            }
            this.mLevels[i] = (byte)(level + add);
        }
    }

    void applyToWrappedLine(DispLineWrapped wrappedLine, AFERun afeRun, int start) {
        int i;
        int length = wrappedLine.getCharCount();
        if (length <= 0) {
            return;
        }
        int limit = start + length;
        DispMap glyphLocMap = this.mLine.getGlyphLocMap();
        int glyphLocCount = glyphLocMap.size();
        if (glyphLocCount == 0) {
            glyphLocMap = null;
        }
        int[] indexes = null;
        if (this.mLine.hasBIDI()) {
            int maxLevel = this.resetTrailingSpaces(start, limit, afeRun);
            indexes = this.reorderRuns(start, limit, maxLevel);
        }
        boolean[] visited = new boolean[length];
        for (i = 0; i < length; ++i) {
            visited[i] = false;
        }
        int glyphIndex = 0;
        GlyphLoc glyphLoc = new GlyphLoc();
        for (i = 0; i < length; ++i) {
            int rawIndex = indexes == null ? i + start : indexes[i];
            int visitedIndex = rawIndex - start;
            assert (visitedIndex >= 0 && visitedIndex < length);
            if (visited[visitedIndex]) continue;
            int glyphLocIndex = rawIndex;
            int glyphLocLimit = rawIndex + 1;
            if (glyphLocMap != null) {
                int foundIndex = glyphLocMap.findItem(rawIndex);
                boolean charPresent = false;
                if (glyphLocMap.isValidMapIndex(foundIndex)) {
                    DispMapItem testGlyphLoc = glyphLocMap.getItem(foundIndex);
                    int charStart = testGlyphLoc.getMapIndex();
                    int charLimit = charStart + testGlyphLoc.getMapLength();
                    if (charStart <= rawIndex && rawIndex < charLimit) {
                        charPresent = true;
                    }
                }
                if (charPresent) {
                    glyphLocIndex = foundIndex;
                    int referenceIndex = this.mLine.getGlyphLoc(foundIndex).getMapIndex();
                    int testIndex = foundIndex;
                    while (testIndex > 0 && glyphLocMap.getItem(--testIndex).getMapIndex() == referenceIndex) {
                        glyphLocIndex = testIndex;
                    }
                    testIndex = glyphLocLimit = foundIndex + 1;
                    while (testIndex < glyphLocCount && glyphLocMap.getItem(testIndex).getMapIndex() == referenceIndex) {
                        glyphLocLimit = testIndex++;
                    }
                } else {
                    glyphLocLimit = glyphLocIndex;
                }
            }
            while (glyphLocIndex < glyphLocLimit) {
                glyphLoc.copyFrom(this.mLine.getGlyphLoc(glyphLocIndex));
                int wrappedIndex = glyphLoc.getMapIndex() - start;
                assert (wrappedIndex >= 0);
                for (int j = 0; j < glyphLoc.getMapLength(); ++j) {
                    int index = wrappedIndex + j;
                    assert (index < length);
                    visited[index] = true;
                }
                glyphLoc.setGlyphIndex(glyphIndex);
                wrappedLine.add(glyphLoc, wrappedIndex, glyphLoc.getMapLength());
                ++glyphIndex;
                ++glyphLocIndex;
            }
        }
    }

    int getLevel(int index) {
        return this.mLevels[index];
    }

    private int resetTrailingSpaces(int start, int limit, AFERun afeRun) {
        byte maxLevel = 0;
        byte defaultLevel = (byte)this.getDefaultLevel();
        boolean defaultWhitespaceLevels = true;
        int i = limit;
        while (i > 0) {
            int type;
            if ((type = TextCharProp.getBIDIClass(this.mLine.getBreakData(--i))) == 4718592 && this.mLine.tabAt(i) != null) {
                type = 4456448;
            }
            switch (type) {
                case 524288: 
                case 4456448: {
                    this.overrideLevel(i, defaultLevel, afeRun);
                    defaultWhitespaceLevels = true;
                    break;
                }
                case 4718592: {
                    if (!defaultWhitespaceLevels) break;
                    this.overrideLevel(i, defaultLevel, afeRun);
                    break;
                }
                default: {
                    if (MappingManager.isExplicitControl(type)) break;
                    defaultWhitespaceLevels = false;
                }
            }
            if (this.mLevels[i] <= maxLevel) continue;
            maxLevel = this.mLevels[i];
        }
        return maxLevel;
    }

    private int[] reorderRuns(int start, int limit, int maxLevel) {
        int i;
        int length = limit - start;
        if (length <= 0) {
            return null;
        }
        int[] indexes = new int[length];
        for (i = 0; i < length; ++i) {
            indexes[i] = i + start;
        }
        while (maxLevel > 0) {
            int runStart = -1;
            for (i = 0; i < length; ++i) {
                if (this.mLevels[i + start] >= maxLevel) {
                    if (runStart >= 0) continue;
                    runStart = i;
                    continue;
                }
                if (runStart < 0) continue;
                MappingManager.reverseRun(indexes, runStart, i);
                runStart = -1;
            }
            if (runStart >= 0) {
                MappingManager.reverseRun(indexes, runStart, length);
            }
            --maxLevel;
        }
        int accentRunStart = -1;
        for (i = 0; i < length; ++i) {
            int index = indexes[i];
            int breakType = this.mLine.getBreakClass(index);
            if (breakType == 8) {
                if (accentRunStart >= 0) continue;
                accentRunStart = i;
                continue;
            }
            if (accentRunStart >= 0 && MappingManager.isRTL(this.mLevels[index])) {
                MappingManager.reverseRun(indexes, accentRunStart, i + 1);
            }
            accentRunStart = -1;
        }
        return indexes;
    }

    private void overrideLevel(int index, byte newLevel, AFERun afeRun) {
        this.mLevels[index] = newLevel;
        afeRun.getElement(index).setBIDILevel(newLevel);
    }

    private static boolean isRTL(int level) {
        return (level & 1) != 0;
    }

    private static int getDirectionFromLevel(int level) {
        return MappingManager.isRTL(level) ? 3670016 : 2097152;
    }

    private static int nextEvenLevel(int level) {
        return MappingManager.isRTL(level) ? level + 1 : level + 2;
    }

    private static int nextOddLevel(int level) {
        return MappingManager.isRTL(level) ? level + 2 : level + 1;
    }

    private int getDefaultLevel() {
        return this.mLine.isRTL() ? 1 : 0;
    }

    private static int resolveDirection(int override, int direction) {
        return override == 3145728 ? direction : override;
    }

    private static boolean isStrong(int direction) {
        switch (direction) {
            case 0: 
            case 2097152: 
            case 2359296: 
            case 2621440: 
            case 3670016: 
            case 3932160: 
            case 4194304: {
                return true;
            }
        }
        return false;
    }

    private static boolean isExplicitControl(int direction) {
        switch (direction) {
            case 2359296: 
            case 2621440: 
            case 3407872: 
            case 3932160: 
            case 4194304: {
                return true;
            }
        }
        return false;
    }

    private final int findNonExplicit(int index, int limit) {
        while (index < limit && MappingManager.isExplicitControl(this.mDirections[index])) {
            ++index;
        }
        return index;
    }

    private static void reverseRun(int[] indexes, int start, int limit) {
        int mid = (start + limit) / 2;
        int end = limit - 1;
        while (start < mid) {
            int swap = indexes[start];
            indexes[start] = indexes[end];
            indexes[end] = swap;
            ++start;
            --end;
        }
    }

    private class NeutralTypeResolver
    extends LevelRunProcessor {
        private int mRunDirection;
        private int mPrevStrongDirection;
        private int mNextStrongDirection;
        private int mFirstNeutralIndex;
        private int mNextIndex;
        private boolean mFirstTime;

        NeutralTypeResolver() {
            super();
            this.mFirstNeutralIndex = -1;
            this.mFirstTime = true;
        }

        @Override
        int processChar(int index, int prevDirection, int direction, int nextDirection) {
            if (this.mFirstTime) {
                this.mRunDirection = MappingManager.getDirectionFromLevel(this.getDirection(index));
                this.mPrevStrongDirection = prevDirection;
                this.mFirstTime = false;
            }
            int strongDir = 3145728;
            switch (direction) {
                case 0: 
                case 262144: 
                case 1310720: 
                case 3670016: {
                    strongDir = 3670016;
                    break;
                }
                case 2097152: {
                    strongDir = 2097152;
                    break;
                }
                case 524288: 
                case 3145728: 
                case 4456448: 
                case 4718592: {
                    if (this.mFirstNeutralIndex >= 0) break;
                    this.mFirstNeutralIndex = index;
                }
            }
            if (strongDir != 3145728) {
                this.flushRun(index, strongDir);
                this.mPrevStrongDirection = strongDir;
            }
            this.mNextStrongDirection = nextDirection;
            this.mNextIndex = index + 1;
            return -1;
        }

        @Override
        void finishRun() {
            this.flushRun(this.mNextIndex, this.mNextStrongDirection);
        }

        private final void flushRun(int limit, int direction) {
            if (this.mFirstNeutralIndex >= 0) {
                int neutralDir = direction == this.mPrevStrongDirection ? direction : this.mRunDirection;
                this.setDirection(this.mFirstNeutralIndex, limit, neutralDir);
                this.mFirstNeutralIndex = -1;
            }
        }
    }

    private class WeakTypeResolver
    extends LevelRunProcessor {
        private int mPrevStrong;
        private int mETRunStart;

        WeakTypeResolver() {
            super();
        }

        @Override
        int processChar(int index, int prevDirection, int direction, int nextDirection) {
            int prevOverride = -1;
            boolean preserveETRun = false;
            switch (direction) {
                case 2883584: {
                    this.setDirection(index, prevDirection);
                    break;
                }
                case 1310720: {
                    if (this.mPrevStrong == 0) {
                        this.setDirection(index, 262144);
                    } else {
                        int newDirection = this.resolveENDirection(this.mPrevStrong);
                        this.setDirection(index, newDirection);
                        this.setDirection(this.mETRunStart, index, newDirection);
                    }
                    prevOverride = 1310720;
                    break;
                }
                case 0: {
                    this.setDirection(index, 3670016);
                    break;
                }
                case 1572864: {
                    if (prevDirection == 1310720 && nextDirection == 1310720) {
                        this.setDirection(index, this.resolveENDirection(this.mPrevStrong));
                        break;
                    }
                    this.setDirection(index, 3145728);
                    break;
                }
                case 1048576: {
                    boolean usePrev = false;
                    if (prevDirection == nextDirection) {
                        switch (prevDirection) {
                            case 262144: 
                            case 1310720: {
                                usePrev = true;
                            }
                        }
                    }
                    if (usePrev) {
                        this.setDirection(index, prevDirection);
                        break;
                    }
                    this.setDirection(index, 3145728);
                    break;
                }
                case 1835008: {
                    if (prevDirection == 1310720) {
                        this.setDirection(index, this.resolveENDirection(this.mPrevStrong));
                        prevOverride = 1310720;
                        break;
                    }
                    preserveETRun = true;
                    if (this.mETRunStart >= 0) break;
                    this.mETRunStart = index;
                    break;
                }
                default: {
                    if (!MappingManager.isStrong(direction)) break;
                    this.mPrevStrong = direction;
                }
            }
            if (!preserveETRun) {
                this.mETRunStart = -1;
            }
            return prevOverride;
        }
    }

    private abstract class LevelRunProcessor {
        LevelRunProcessor() {
        }

        void execute() {
            int i = 0;
            while (i < MappingManager.this.mCharCount) {
                int runStart = i;
                byte sorLevel = MappingManager.this.mLevels[i];
                if (i > 0 && MappingManager.this.mLevels[i - 1] > sorLevel) {
                    sorLevel = MappingManager.this.mLevels[i - 1];
                }
                ++i;
                byte eorLevel = sorLevel;
                while (i < MappingManager.this.mCharCount) {
                    byte nextLevel = MappingManager.this.mLevels[i];
                    if (nextLevel != eorLevel) {
                        if (nextLevel <= eorLevel) break;
                        eorLevel = nextLevel;
                        break;
                    }
                    ++i;
                }
                int runLimit = i;
                this.processRun(runStart, runLimit, sorLevel, eorLevel);
            }
        }

        abstract int processChar(int var1, int var2, int var3, int var4);

        void finishRun() {
        }

        final int resolveENDirection(int prevStrong) {
            return prevStrong == 2097152 ? 2097152 : 1310720;
        }

        final int getDirection(int index) {
            return MappingManager.this.mDirections[index];
        }

        final void setDirection(int index, int direction) {
            MappingManager.access$500((MappingManager)MappingManager.this)[index] = direction;
        }

        final void setDirection(int start, int limit, int direction) {
            if (start >= 0) {
                int i = start;
                while (i < limit) {
                    this.setDirection(i, direction);
                    i = MappingManager.this.findNonExplicit(i + 1, limit);
                }
            }
        }

        private void processRun(int runStart, int runLimit, int sorLevel, int eorLevel) {
            int index = MappingManager.this.findNonExplicit(runStart, runLimit);
            if (index >= runLimit) {
                return;
            }
            int direction = MappingManager.this.mDirections[index];
            int prevDirection = MappingManager.getDirectionFromLevel(sorLevel);
            while (index < runLimit) {
                int nextIndex = MappingManager.this.findNonExplicit(index + 1, runLimit);
                int nextDirection = nextIndex < runLimit ? MappingManager.this.mDirections[nextIndex] : MappingManager.getDirectionFromLevel(eorLevel);
                int prevOverride = this.processChar(index, prevDirection, direction, nextDirection);
                prevDirection = prevOverride == -1 ? MappingManager.this.mDirections[index] : prevOverride;
                direction = nextDirection;
                index = nextIndex;
            }
            this.finishRun();
        }
    }

    private static class ExplicitRunProcessor {
        private DispLine mLine;
        private int mCharCount;
        private int[] mDirections;
        private byte[] mLevels;
        private boolean mDefaultRTL;
        private int mDefaultEmbedding;
        private int mRunIndex;
        private int mRunLimit;

        private ExplicitRunProcessor() {
        }

        void setup(DispLine line, int[] directions, byte[] levels) {
            this.mLine = line;
            this.mCharCount = this.mLine.getCharCount();
            this.mDirections = directions;
            this.mLevels = levels;
            this.mDefaultRTL = this.mLine.isRTL();
            this.mDefaultEmbedding = this.mDefaultRTL ? 3932160 : 2359296;
            this.mRunIndex = 0;
            this.mRunLimit = 0;
            byte defaultLevel = this.mDefaultRTL ? 1 : 0;
            for (int i = 0; i < this.mCharCount; ++i) {
                this.mDirections[i] = line.getBreakData(i);
                this.mLevels[i] = defaultLevel;
            }
        }

        void run() {
            this.processExplicitRun(0, this.mDefaultEmbedding, this.mDefaultRTL ? 1 : 0, 3145728);
        }

        private int processExplicitRun(int index, int dispRunDir, int level, int override) {
            boolean atInitialDepth;
            boolean bl = atInitialDepth = index == 0;
            while (index < this.mLine.getCharCount()) {
                if (index >= this.mRunLimit) {
                    ++this.mRunIndex;
                    if (this.mRunIndex >= this.mLine.getRunCount()) {
                        this.mRunLimit = this.mLine.getCharCount();
                    } else {
                        DispRun run = this.mLine.getRun(this.mRunIndex);
                        this.mRunLimit = run.getMapIndex() + run.getMapLength();
                        TextAttr textAttr = run.getAttr();
                        if (textAttr != null) {
                            int newDispRunDir;
                            switch (textAttr.direction()) {
                                case 1: {
                                    newDispRunDir = 2359296;
                                    break;
                                }
                                case 2: {
                                    newDispRunDir = 3932160;
                                    break;
                                }
                                default: {
                                    newDispRunDir = this.mDefaultEmbedding;
                                }
                            }
                            if (newDispRunDir != dispRunDir) {
                                if (newDispRunDir == this.mDefaultEmbedding) {
                                    return index;
                                }
                                int newLevel = newDispRunDir == 2359296 ? MappingManager.nextEvenLevel(level) : MappingManager.nextOddLevel(level);
                                if ((index = this.processExplicitRun(index, newDispRunDir, newLevel, override)) >= this.mCharCount) {
                                    return index;
                                }
                            }
                        }
                    }
                }
                int charData = this.mLine.getBreakData(index);
                int direction = TextCharProp.getBIDIClass(charData);
                int newLevel = level;
                int newOverride = override;
                switch (direction) {
                    case 2359296: {
                        newLevel = MappingManager.nextEvenLevel(level);
                        newOverride = 3145728;
                        break;
                    }
                    case 2621440: {
                        newLevel = MappingManager.nextEvenLevel(level);
                        newOverride = 2097152;
                        break;
                    }
                    case 3932160: {
                        newLevel = MappingManager.nextOddLevel(level);
                        newOverride = 3145728;
                        break;
                    }
                    case 4194304: {
                        newLevel = MappingManager.nextOddLevel(level);
                        newOverride = 3670016;
                        break;
                    }
                    case 3407872: {
                        if (atInitialDepth) break;
                        this.mLevels[index] = (byte)level;
                        this.mDirections[index] = MappingManager.resolveDirection(override, direction);
                        return index + 1;
                    }
                }
                if (newLevel != level && newLevel > 61) {
                    newLevel = level;
                    newOverride = override;
                }
                this.mLevels[index] = (byte)newLevel;
                this.mDirections[index] = MappingManager.resolveDirection(newOverride, direction);
                if (newLevel == level) {
                    ++index;
                    continue;
                }
                if ((index = this.processExplicitRun(index + 1, dispRunDir, newLevel, newOverride)) < this.mCharCount) continue;
                return index;
            }
            return index;
        }
    }

}