This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
Add fallback support to local ideograph font families #15255
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/FontUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.mapbox.mapboxsdk.utils; | ||
|
|
||
| import android.graphics.Typeface; | ||
|
|
||
| import com.mapbox.mapboxsdk.MapStrictMode; | ||
| import com.mapbox.mapboxsdk.log.Logger; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static com.mapbox.mapboxsdk.constants.MapboxConstants.DEFAULT_FONT; | ||
|
|
||
| /** | ||
| * Utility class to select a font from a range of font names based on the availability of fonts on the device. | ||
| */ | ||
| public class FontUtils { | ||
|
|
||
| private static final String TAG = "Mbgl-FontUtils"; | ||
|
|
||
| private FontUtils() { | ||
| // no instance | ||
| } | ||
|
|
||
| /** | ||
| * Select a font from a range of font names to match the availability of fonts on the device. | ||
| * | ||
| * @param fontNames the range of font names to select from | ||
| * @return the selected fon | ||
| */ | ||
| public static String extractValidFont(String... fontNames) { | ||
| if (fontNames == null) { | ||
| return null; | ||
| } | ||
|
|
||
| List<String> validFonts = getAvailableFonts(); | ||
| for (String fontName : fontNames) { | ||
| if (validFonts.contains(fontName)) { | ||
| return fontName; | ||
| } | ||
| } | ||
|
|
||
| Logger.i(TAG, String.format( | ||
| "Couldn't map font family for local ideograph, using %s instead", DEFAULT_FONT) | ||
| ); | ||
| return DEFAULT_FONT; | ||
| } | ||
|
|
||
| private static List<String> getAvailableFonts() { | ||
| List<String> fonts = new ArrayList<>(); | ||
| try { | ||
| Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL); | ||
| Field f = Typeface.class.getDeclaredField("sSystemFontMap"); | ||
| f.setAccessible(true); | ||
| Map<String, Typeface> fontMap = (Map<String, Typeface>) f.get(typeface); | ||
| fonts.addAll(fontMap.keySet()); | ||
| } catch (Exception exception) { | ||
| Logger.e(TAG,"Couldn't load fonts from Typeface", exception); | ||
| MapStrictMode.strictModeViolation("Couldn't load fonts from Typeface", exception); | ||
| } | ||
| return fonts; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...boxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/utils/FontUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.mapbox.mapboxsdk.utils; | ||
|
|
||
| import android.support.test.runner.AndroidJUnit4; | ||
|
|
||
| import com.mapbox.mapboxsdk.constants.MapboxConstants; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
|
|
||
| @RunWith(AndroidJUnit4.class) | ||
| public class FontUtilsTest { | ||
|
|
||
| @Test | ||
| public void testExtractedFontShouldMatchDefault() { | ||
| String[] fonts = new String[] {"foo", "bar"}; | ||
| String actual = FontUtils.extractValidFont(fonts); | ||
| assertEquals("Selected font should match", MapboxConstants.DEFAULT_FONT, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExtractedFontShouldMatchMonospace() { | ||
| String expected = "monospace"; | ||
| String[] fonts = new String[] {"foo", expected}; | ||
| String actual = FontUtils.extractValidFont(fonts); | ||
| assertEquals("Selected font should match", expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExtractedFontArrayShouldBeNull() { | ||
| String[] fonts = null; | ||
| String actual = FontUtils.extractValidFont(fonts); | ||
| assertNull(actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExtractedFontShouldBeNull() { | ||
| String actual = FontUtils.extractValidFont(null); | ||
| assertNull(actual); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.