2021-07-24 02:37:17 +00:00
|
|
|
package androidx.core.graphics;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.graphics.fonts.Font;
|
|
|
|
import android.graphics.fonts.FontFamily;
|
|
|
|
import android.graphics.fonts.FontStyle;
|
|
|
|
import android.os.CancellationSignal;
|
|
|
|
import android.os.ParcelFileDescriptor;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
import androidx.annotation.RestrictTo;
|
|
|
|
import androidx.core.content.res.FontResourcesParserCompat;
|
|
|
|
import androidx.core.provider.FontsContractCompat;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
@RequiresApi(29)
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
|
|
public class TypefaceCompatApi29Impl extends TypefaceCompatBaseImpl {
|
|
|
|
@Override // androidx.core.graphics.TypefaceCompatBaseImpl
|
|
|
|
@Nullable
|
|
|
|
public Typeface createFromFontFamilyFilesResourceEntry(Context context, FontResourcesParserCompat.FontFamilyFilesResourceEntry fontFamilyFilesResourceEntry, Resources resources, int i) {
|
2021-09-21 07:33:49 +00:00
|
|
|
try {
|
|
|
|
FontResourcesParserCompat.FontFileResourceEntry[] entries = fontFamilyFilesResourceEntry.getEntries();
|
|
|
|
int length = entries.length;
|
|
|
|
int i2 = 0;
|
|
|
|
FontFamily.Builder builder = null;
|
|
|
|
int i3 = 0;
|
|
|
|
while (true) {
|
|
|
|
int i4 = 1;
|
|
|
|
if (i3 >= length) {
|
|
|
|
break;
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
FontResourcesParserCompat.FontFileResourceEntry fontFileResourceEntry = entries[i3];
|
|
|
|
try {
|
|
|
|
Font.Builder weight = new Font.Builder(resources, fontFileResourceEntry.getResourceId()).setWeight(fontFileResourceEntry.getWeight());
|
|
|
|
if (!fontFileResourceEntry.isItalic()) {
|
|
|
|
i4 = 0;
|
|
|
|
}
|
|
|
|
Font build = weight.setSlant(i4).setTtcIndex(fontFileResourceEntry.getTtcIndex()).setFontVariationSettings(fontFileResourceEntry.getVariationSettings()).build();
|
|
|
|
if (builder == null) {
|
|
|
|
builder = new FontFamily.Builder(build);
|
|
|
|
} else {
|
|
|
|
builder.addFont(build);
|
|
|
|
}
|
|
|
|
} catch (IOException unused) {
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
i3++;
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
if (builder == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
int i5 = (i & 1) != 0 ? 700 : 400;
|
|
|
|
if ((i & 2) != 0) {
|
|
|
|
i2 = 1;
|
|
|
|
}
|
|
|
|
return new Typeface.CustomFallbackBuilder(builder.build()).setStyle(new FontStyle(i5, i2)).build();
|
|
|
|
} catch (Exception unused2) {
|
2021-07-24 02:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.core.graphics.TypefaceCompatBaseImpl
|
|
|
|
@Nullable
|
|
|
|
public Typeface createFromFontInfo(Context context, @Nullable CancellationSignal cancellationSignal, @NonNull FontsContractCompat.FontInfo[] fontInfoArr, int i) {
|
|
|
|
ContentResolver contentResolver = context.getContentResolver();
|
2021-09-21 07:33:49 +00:00
|
|
|
try {
|
|
|
|
int length = fontInfoArr.length;
|
|
|
|
int i2 = 0;
|
|
|
|
FontFamily.Builder builder = null;
|
|
|
|
int i3 = 0;
|
|
|
|
while (true) {
|
|
|
|
int i4 = 1;
|
|
|
|
if (i3 >= length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FontsContractCompat.FontInfo fontInfo = fontInfoArr[i3];
|
|
|
|
try {
|
|
|
|
ParcelFileDescriptor openFileDescriptor = contentResolver.openFileDescriptor(fontInfo.getUri(), "r", cancellationSignal);
|
|
|
|
if (openFileDescriptor != null) {
|
|
|
|
try {
|
|
|
|
Font.Builder weight = new Font.Builder(openFileDescriptor).setWeight(fontInfo.getWeight());
|
|
|
|
if (!fontInfo.isItalic()) {
|
|
|
|
i4 = 0;
|
|
|
|
}
|
|
|
|
Font build = weight.setSlant(i4).setTtcIndex(fontInfo.getTtcIndex()).build();
|
|
|
|
if (builder == null) {
|
|
|
|
builder = new FontFamily.Builder(build);
|
|
|
|
} else {
|
|
|
|
builder.addFont(build);
|
|
|
|
}
|
|
|
|
} catch (Throwable th) {
|
|
|
|
th.addSuppressed(th);
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
} else if (openFileDescriptor == null) {
|
|
|
|
i3++;
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
openFileDescriptor.close();
|
|
|
|
} catch (IOException unused) {
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
i3++;
|
2021-07-24 02:37:17 +00:00
|
|
|
}
|
2021-09-21 07:33:49 +00:00
|
|
|
if (builder == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
int i5 = (i & 1) != 0 ? 700 : 400;
|
|
|
|
if ((i & 2) != 0) {
|
|
|
|
i2 = 1;
|
|
|
|
}
|
|
|
|
return new Typeface.CustomFallbackBuilder(builder.build()).setStyle(new FontStyle(i5, i2)).build();
|
|
|
|
throw th;
|
|
|
|
} catch (Exception unused2) {
|
2021-07-24 02:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.core.graphics.TypefaceCompatBaseImpl
|
|
|
|
public Typeface createFromInputStream(Context context, InputStream inputStream) {
|
|
|
|
throw new RuntimeException("Do not use this function in API 29 or later.");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.core.graphics.TypefaceCompatBaseImpl
|
|
|
|
@Nullable
|
|
|
|
public Typeface createFromResourcesFontFile(Context context, Resources resources, int i, String str, int i2) {
|
|
|
|
try {
|
|
|
|
Font build = new Font.Builder(resources, i).build();
|
|
|
|
return new Typeface.CustomFallbackBuilder(new FontFamily.Builder(build).build()).setStyle(build.getStyle()).build();
|
2021-09-21 07:33:49 +00:00
|
|
|
} catch (Exception unused) {
|
2021-07-24 02:37:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override // androidx.core.graphics.TypefaceCompatBaseImpl
|
|
|
|
public FontsContractCompat.FontInfo findBestInfo(FontsContractCompat.FontInfo[] fontInfoArr, int i) {
|
|
|
|
throw new RuntimeException("Do not use this function in API 29 or later.");
|
|
|
|
}
|
|
|
|
}
|