这是indexloc提供的服务,不要输入任何密码
Skip to content

Find best matched font in TypefaceCompatApi29Impl #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@
@RestrictTo(LIBRARY_GROUP)
@RequiresApi(29)
public class TypefaceCompatApi29Impl extends TypefaceCompatBaseImpl {

private static int getMatchScore(@NonNull FontStyle o1, @NonNull FontStyle o2) {
return Math.abs((o1.getWeight() - o2.getWeight())) / 100 + (o1.getSlant() == o2.getSlant() ? 0 : 2);
}

private Font findBaseFont(@NonNull FontFamily family) {
final FontStyle normal = new FontStyle(FontStyle.FONT_WEIGHT_NORMAL,
FontStyle.FONT_SLANT_UPRIGHT);
Font bestFont = family.getFont(0);
int bestScore = getMatchScore(normal, bestFont.getStyle());
for (int i = 1; i < family.getSize(); ++i) {
final Font candidate = family.getFont(i);
final int score = getMatchScore(normal, candidate.getStyle());
if (score < bestScore) {
bestFont = candidate;
bestScore = score;
}
}
return bestFont;
}

@Override
protected FontsContractCompat.FontInfo findBestInfo(FontsContractCompat.FontInfo[] fonts,
int style) {
Expand Down Expand Up @@ -86,14 +107,9 @@ public Typeface createFromFontInfo(Context context,
if (familyBuilder == null) {
return null; // No font is added. Give up.
}
final FontStyle defaultStyle = new FontStyle(
(style & Typeface.BOLD) != 0 ? FontStyle.FONT_WEIGHT_BOLD
: FontStyle.FONT_WEIGHT_NORMAL,
(style & Typeface.ITALIC) != 0 ? FontStyle.FONT_SLANT_ITALIC
: FontStyle.FONT_SLANT_UPRIGHT
);
return new Typeface.CustomFallbackBuilder(familyBuilder.build())
.setStyle(defaultStyle)
final FontFamily family = familyBuilder.build();
return new Typeface.CustomFallbackBuilder(family)
.setStyle(findBaseFont(family).getStyle())
.build();
} catch (Exception e) {
return null;
Expand Down Expand Up @@ -128,14 +144,9 @@ public Typeface createFromFontFamilyFilesResourceEntry(Context context,
if (familyBuilder == null) {
return null; // No font is added. Give up.
}
final FontStyle defaultStyle = new FontStyle(
(style & Typeface.BOLD) != 0 ? FontStyle.FONT_WEIGHT_BOLD
: FontStyle.FONT_WEIGHT_NORMAL,
(style & Typeface.ITALIC) != 0 ? FontStyle.FONT_SLANT_ITALIC
: FontStyle.FONT_SLANT_UPRIGHT
);
return new Typeface.CustomFallbackBuilder(familyBuilder.build())
.setStyle(defaultStyle)
final FontFamily family = familyBuilder.build();
return new Typeface.CustomFallbackBuilder(family)
.setStyle(findBaseFont(family).getStyle())
.build();
} catch (Exception e) {
return null;
Expand Down