15c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes/*
25c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * Copyright (C) 2017 The Android Open Source Project
35c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes *
45c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * Licensed under the Apache License, Version 2.0 (the "License");
55c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * you may not use this file except in compliance with the License.
65c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * You may obtain a copy of the License at
75c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes *
85c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes *      http://www.apache.org/licenses/LICENSE-2.0
95c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes *
105c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * Unless required by applicable law or agreed to in writing, software
115c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * distributed under the License is distributed on an "AS IS" BASIS,
125c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * See the License for the specific language governing permissions and
145c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes * limitations under the License.
155c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes */
165c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.core.graphics;
185c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes
195c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banesimport android.graphics.Paint;
2077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikasimport android.graphics.Rect;
215c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banesimport android.os.Build;
229dede51868bbbe16aadcd65e04860bea8ea50e05Aurimas Liutikas
23ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.annotation.NonNull;
24ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.core.util.Pair;
255c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes
265c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes/**
27645e5c8aa6b31961c5f73f3d30bb5261d5e04aebKirill Grouchnikov * Helper for accessing features in {@link Paint}.
285c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes */
295c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banespublic final class PaintCompat {
3077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas    // U+DFFFD which is very end of unassigned plane.
3177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas    private static final String TOFU_STRING = "\uDB3F\uDFFD";
3277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas    private static final String EM_STRING = "m";
3377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
3477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas    private static final ThreadLocal<Pair<Rect, Rect>> sRectThreadLocal = new ThreadLocal<>();
355c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes
365c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes    /**
375c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes     * Determine whether the typeface set on the paint has a glyph supporting the
385c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes     * string in a backwards compatible way.
395c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes     *
405c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes     * @param paint the paint instance to check
415c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes     * @param string the string to test whether there is glyph support
425c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes     * @return true if the typeface set on the given paint has a glyph for the string
435c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes     */
445c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes    public static boolean hasGlyph(@NonNull Paint paint, @NonNull String string) {
455c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes        if (Build.VERSION.SDK_INT >= 23) {
467347bef5c2512cbd809773a0cb5f247c23b3392dAurimas Liutikas            return paint.hasGlyph(string);
475c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes        }
4877baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        final int length = string.length();
4977baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
5077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        if (length == 1 && Character.isWhitespace(string.charAt(0))) {
5177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // measureText + getTextBounds skips whitespace so we need to special case it here
5277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            return true;
5377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        }
5477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
5577baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        final float missingGlyphWidth = paint.measureText(TOFU_STRING);
5677baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        final float emGlyphWidth = paint.measureText(EM_STRING);
5777baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
5877baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        final float width = paint.measureText(string);
5977baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
6077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        if (width == 0f) {
6177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // If the string width is 0, it can't be rendered
6277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            return false;
6377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        }
6477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
6577baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        if (string.codePointCount(0, string.length()) > 1) {
6677baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // Heuristic to detect fallback glyphs for ligatures like flags and ZWJ sequences
6777baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // Return false if string is rendered too widely
6877baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            if (width > 2 * emGlyphWidth) {
6977baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas                return false;
7077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            }
7177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
7277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // Heuristic to detect fallback glyphs for ligatures like flags and ZWJ sequences (2).
7377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // If width is greater than or equal to the sum of width of each code point, it is very
7477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // likely that the system is using fallback fonts to draw {@code string} in two or more
7577baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // glyphs instead of a single ligature glyph. (hasGlyph returns false in this case.)
7677baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // False detections are possible (the ligature glyph may happen to have the same width
7777baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // as the sum width), but there are no good way to avoid them.
7877baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // NOTE: This heuristic does not work with proportional glyphs.
7977baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // NOTE: This heuristic does not work when a ZWJ sequence is partially combined.
8077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // E.g. If system has a glyph for "A ZWJ B" and not for "A ZWJ B ZWJ C", this heuristic
8177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // returns true for "A ZWJ B ZWJ C".
8277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            float sumWidth = 0;
8377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            int i = 0;
8477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            while (i < length) {
8577baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas                int charCount = Character.charCount(string.codePointAt(i));
8677baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas                sumWidth += paint.measureText(string, i, i + charCount);
8777baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas                i += charCount;
8877baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            }
8977baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            if (width >= sumWidth) {
9077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas                return false;
9177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            }
9277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        }
9377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
9477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        if (width != missingGlyphWidth) {
9577baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            // If the widths are different then its not tofu
9677baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            return true;
9777baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        }
9877baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
9977baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        // If the widths are the same, lets check the bounds. The chance of them being
10077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        // different chars with the same bounds is extremely small
10177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        final Pair<Rect, Rect> rects = obtainEmptyRects();
10277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        paint.getTextBounds(TOFU_STRING, 0, TOFU_STRING.length(), rects.first);
10377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        paint.getTextBounds(string, 0, length, rects.second);
10477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        return !rects.first.equals(rects.second);
10577baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas    }
10677baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas
10777baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas    private static Pair<Rect, Rect> obtainEmptyRects() {
10877baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        Pair<Rect, Rect> rects = sRectThreadLocal.get();
10977baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        if (rects == null) {
11077baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            rects = new Pair<>(new Rect(), new Rect());
11177baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            sRectThreadLocal.set(rects);
11277baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        } else {
11377baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            rects.first.setEmpty();
11477baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas            rects.second.setEmpty();
11577baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        }
11677baef49563f9910641a797d9a9b8dd01c807564Aurimas Liutikas        return rects;
1175c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes    }
1185c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes
1195c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes    private PaintCompat() {}
1205c4dd610eace399c47eb0418d617aa05701f9ab9Chris Banes}
121