93 lines
3.4 KiB
Java
93 lines
3.4 KiB
Java
package androidx.core.graphics;
|
|
|
|
import android.graphics.BlendMode;
|
|
import android.graphics.Paint;
|
|
import android.graphics.PorterDuff;
|
|
import android.graphics.PorterDuffXfermode;
|
|
import android.graphics.Rect;
|
|
import android.os.Build;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.core.util.Pair;
|
|
public final class PaintCompat {
|
|
private static final String EM_STRING = "m";
|
|
private static final String TOFU_STRING = "";
|
|
private static final ThreadLocal<Pair<Rect, Rect>> sRectThreadLocal = new ThreadLocal<>();
|
|
|
|
private PaintCompat() {
|
|
}
|
|
|
|
public static boolean hasGlyph(@NonNull Paint paint, @NonNull String str) {
|
|
if (Build.VERSION.SDK_INT >= 23) {
|
|
return paint.hasGlyph(str);
|
|
}
|
|
int length = str.length();
|
|
if (length == 1 && Character.isWhitespace(str.charAt(0))) {
|
|
return true;
|
|
}
|
|
float measureText = paint.measureText(TOFU_STRING);
|
|
float measureText2 = paint.measureText(EM_STRING);
|
|
float measureText3 = paint.measureText(str);
|
|
float f = 0.0f;
|
|
if (measureText3 == 0.0f) {
|
|
return false;
|
|
}
|
|
if (str.codePointCount(0, str.length()) > 1) {
|
|
if (measureText3 > measureText2 * 2.0f) {
|
|
return false;
|
|
}
|
|
int i = 0;
|
|
while (i < length) {
|
|
int charCount = Character.charCount(str.codePointAt(i)) + i;
|
|
f += paint.measureText(str, i, charCount);
|
|
i = charCount;
|
|
}
|
|
if (measureText3 >= f) {
|
|
return false;
|
|
}
|
|
}
|
|
if (measureText3 != measureText) {
|
|
return true;
|
|
}
|
|
Pair<Rect, Rect> obtainEmptyRects = obtainEmptyRects();
|
|
paint.getTextBounds(TOFU_STRING, 0, 2, obtainEmptyRects.first);
|
|
paint.getTextBounds(str, 0, length, obtainEmptyRects.second);
|
|
return !obtainEmptyRects.first.equals(obtainEmptyRects.second);
|
|
}
|
|
|
|
private static Pair<Rect, Rect> obtainEmptyRects() {
|
|
ThreadLocal<Pair<Rect, Rect>> threadLocal = sRectThreadLocal;
|
|
Pair<Rect, Rect> pair = threadLocal.get();
|
|
if (pair == null) {
|
|
Pair<Rect, Rect> pair2 = new Pair<>(new Rect(), new Rect());
|
|
threadLocal.set(pair2);
|
|
return pair2;
|
|
}
|
|
pair.first.setEmpty();
|
|
pair.second.setEmpty();
|
|
return pair;
|
|
}
|
|
|
|
public static boolean setBlendMode(@NonNull Paint paint, @Nullable BlendModeCompat blendModeCompat) {
|
|
PorterDuffXfermode porterDuffXfermode = null;
|
|
BlendMode blendMode = null;
|
|
if (Build.VERSION.SDK_INT >= 29) {
|
|
if (blendModeCompat != null) {
|
|
blendMode = BlendModeUtils.obtainBlendModeFromCompat(blendModeCompat);
|
|
}
|
|
paint.setBlendMode(blendMode);
|
|
return true;
|
|
} else if (blendModeCompat != null) {
|
|
PorterDuff.Mode obtainPorterDuffFromCompat = BlendModeUtils.obtainPorterDuffFromCompat(blendModeCompat);
|
|
if (obtainPorterDuffFromCompat != null) {
|
|
porterDuffXfermode = new PorterDuffXfermode(obtainPorterDuffFromCompat);
|
|
}
|
|
paint.setXfermode(porterDuffXfermode);
|
|
return obtainPorterDuffFromCompat != null;
|
|
} else {
|
|
paint.setXfermode(null);
|
|
return true;
|
|
}
|
|
}
|
|
}
|