| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.content.res; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.graphics.Typeface; |
| 21 |
import android.util.AttributeSet; |
| 22 |
import android.util.Log; |
| 23 |
import android.util.Xml; |
| 24 |
|
| 25 |
import com.android.internal.R; |
| 26 |
|
| 27 |
import org.xmlpull.v1.XmlPullParser; |
| 28 |
import org.xmlpull.v1.XmlPullParserException; |
| 29 |
|
| 30 |
import java.io.IOException; |
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.Arrays; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class FontResourcesParser { |
| 40 |
private static final String TAG = "FontResourcesParser"; |
| 41 |
|
| 42 |
|
| 43 |
public interface FamilyResourceEntry {} |
| 44 |
|
| 45 |
|
| 46 |
public static final class ProviderResourceEntry implements FamilyResourceEntry { |
| 47 |
private final @NonNull String mProviderAuthority; |
| 48 |
private final @NonNull String mProviderPackage; |
| 49 |
private final @NonNull String mQuery; |
| 50 |
private final @Nullable String mSystemFontFamilyName; |
| 51 |
private final @Nullable List<List<String>> mCerts; |
| 52 |
|
| 53 |
public ProviderResourceEntry(@NonNull String authority, @NonNull String pkg, |
| 54 |
@NonNull String query, @Nullable List<List<String>> certs, |
| 55 |
@Nullable String systemFontFamilyName) { |
| 56 |
mProviderAuthority = authority; |
| 57 |
mProviderPackage = pkg; |
| 58 |
mQuery = query; |
| 59 |
mCerts = certs; |
| 60 |
mSystemFontFamilyName = systemFontFamilyName; |
| 61 |
} |
| 62 |
|
| 63 |
public @NonNull String getAuthority() { |
| 64 |
return mProviderAuthority; |
| 65 |
} |
| 66 |
|
| 67 |
public @NonNull String getPackage() { |
| 68 |
return mProviderPackage; |
| 69 |
} |
| 70 |
|
| 71 |
public @NonNull String getQuery() { |
| 72 |
return mQuery; |
| 73 |
} |
| 74 |
|
| 75 |
public @NonNull String getSystemFontFamilyName() { |
| 76 |
return mSystemFontFamilyName; |
| 77 |
} |
| 78 |
|
| 79 |
public @Nullable List<List<String>> getCerts() { |
| 80 |
return mCerts; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
public static final class FontFileResourceEntry { |
| 86 |
public static final int RESOLVE_BY_FONT_TABLE = Typeface.RESOLVE_BY_FONT_TABLE; |
| 87 |
public static final int UPRIGHT = 0; |
| 88 |
public static final int ITALIC = 1; |
| 89 |
|
| 90 |
private final @NonNull String mFileName; |
| 91 |
private int mWeight; |
| 92 |
private int mItalic; |
| 93 |
private int mTtcIndex; |
| 94 |
private String mVariationSettings; |
| 95 |
private int mResourceId; |
| 96 |
|
| 97 |
public FontFileResourceEntry(@NonNull String fileName, int weight, int italic, |
| 98 |
@Nullable String variationSettings, int ttcIndex) { |
| 99 |
mFileName = fileName; |
| 100 |
mWeight = weight; |
| 101 |
mItalic = italic; |
| 102 |
mVariationSettings = variationSettings; |
| 103 |
mTtcIndex = ttcIndex; |
| 104 |
} |
| 105 |
|
| 106 |
public @NonNull String getFileName() { |
| 107 |
return mFileName; |
| 108 |
} |
| 109 |
|
| 110 |
public int getWeight() { |
| 111 |
return mWeight; |
| 112 |
} |
| 113 |
|
| 114 |
public int getItalic() { |
| 115 |
return mItalic; |
| 116 |
} |
| 117 |
|
| 118 |
public @Nullable String getVariationSettings() { |
| 119 |
return mVariationSettings; |
| 120 |
} |
| 121 |
|
| 122 |
public int getTtcIndex() { |
| 123 |
return mTtcIndex; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
public static final class FontFamilyFilesResourceEntry implements FamilyResourceEntry { |
| 129 |
private final @NonNull FontFileResourceEntry[] mEntries; |
| 130 |
|
| 131 |
public FontFamilyFilesResourceEntry(@NonNull FontFileResourceEntry[] entries) { |
| 132 |
mEntries = entries; |
| 133 |
} |
| 134 |
|
| 135 |
public @NonNull FontFileResourceEntry[] getEntries() { |
| 136 |
return mEntries; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public static @Nullable FamilyResourceEntry parse(XmlPullParser parser, Resources resources) |
| 141 |
throws XmlPullParserException, IOException { |
| 142 |
int type; |
| 143 |
while ((type=parser.next()) != XmlPullParser.START_TAG |
| 144 |
&& type != XmlPullParser.END_DOCUMENT) { |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
if (type != XmlPullParser.START_TAG) { |
| 149 |
throw new XmlPullParserException("No start tag found"); |
| 150 |
} |
| 151 |
return readFamilies(parser, resources); |
| 152 |
} |
| 153 |
|
| 154 |
private static @Nullable FamilyResourceEntry readFamilies(XmlPullParser parser, |
| 155 |
Resources resources) throws XmlPullParserException, IOException { |
| 156 |
parser.require(XmlPullParser.START_TAG, null, "font-family"); |
| 157 |
String tag = parser.getName(); |
| 158 |
FamilyResourceEntry result = null; |
| 159 |
if (tag.equals("font-family")) { |
| 160 |
return readFamily(parser, resources); |
| 161 |
} else { |
| 162 |
skip(parser); |
| 163 |
Log.e(TAG, "Failed to find font-family tag"); |
| 164 |
return null; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
private static @Nullable FamilyResourceEntry readFamily(XmlPullParser parser, |
| 169 |
Resources resources) throws XmlPullParserException, IOException { |
| 170 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 171 |
TypedArray array = resources.obtainAttributes(attrs, R.styleable.FontFamily); |
| 172 |
String authority = array.getString(R.styleable.FontFamily_fontProviderAuthority); |
| 173 |
String providerPackage = array.getString(R.styleable.FontFamily_fontProviderPackage); |
| 174 |
String query = array.getString(R.styleable.FontFamily_fontProviderQuery); |
| 175 |
int certsId = array.getResourceId(R.styleable.FontFamily_fontProviderCerts, 0); |
| 176 |
String systemFontFamilyName = array.getString( |
| 177 |
R.styleable.FontFamily_fontProviderSystemFontFamily); |
| 178 |
array.recycle(); |
| 179 |
if (authority != null && providerPackage != null && query != null) { |
| 180 |
while (parser.next() != XmlPullParser.END_TAG) { |
| 181 |
skip(parser); |
| 182 |
} |
| 183 |
List<List<String>> certs = null; |
| 184 |
if (certsId != 0) { |
| 185 |
TypedArray typedArray = resources.obtainTypedArray(certsId); |
| 186 |
if (typedArray.length() > 0) { |
| 187 |
certs = new ArrayList<>(); |
| 188 |
boolean isArrayOfArrays = typedArray.getResourceId(0, 0) != 0; |
| 189 |
if (isArrayOfArrays) { |
| 190 |
for (int i = 0; i < typedArray.length(); i++) { |
| 191 |
int certId = typedArray.getResourceId(i, 0); |
| 192 |
String[] certsArray = resources.getStringArray(certId); |
| 193 |
List<String> certsList = Arrays.asList(certsArray); |
| 194 |
certs.add(certsList); |
| 195 |
} |
| 196 |
} else { |
| 197 |
String[] certsArray = resources.getStringArray(certsId); |
| 198 |
List<String> certsList = Arrays.asList(certsArray); |
| 199 |
certs.add(certsList); |
| 200 |
} |
| 201 |
} |
| 202 |
typedArray.recycle(); |
| 203 |
} |
| 204 |
return new ProviderResourceEntry( |
| 205 |
authority, |
| 206 |
providerPackage, |
| 207 |
query, |
| 208 |
certs, |
| 209 |
systemFontFamilyName |
| 210 |
); |
| 211 |
} |
| 212 |
List<FontFileResourceEntry> fonts = new ArrayList<>(); |
| 213 |
while (parser.next() != XmlPullParser.END_TAG) { |
| 214 |
if (parser.getEventType() != XmlPullParser.START_TAG) continue; |
| 215 |
String tag = parser.getName(); |
| 216 |
if (tag.equals("font")) { |
| 217 |
final FontFileResourceEntry entry = readFont(parser, resources); |
| 218 |
if (entry != null) { |
| 219 |
fonts.add(entry); |
| 220 |
} |
| 221 |
} else { |
| 222 |
skip(parser); |
| 223 |
} |
| 224 |
} |
| 225 |
if (fonts.isEmpty()) { |
| 226 |
return null; |
| 227 |
} |
| 228 |
return new FontFamilyFilesResourceEntry(fonts.toArray( |
| 229 |
new FontFileResourceEntry[fonts.size()])); |
| 230 |
} |
| 231 |
|
| 232 |
private static FontFileResourceEntry readFont(XmlPullParser parser, Resources resources) |
| 233 |
throws XmlPullParserException, IOException { |
| 234 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 235 |
TypedArray array = resources.obtainAttributes(attrs, R.styleable.FontFamilyFont); |
| 236 |
int weight = array.getInt(R.styleable.FontFamilyFont_fontWeight, |
| 237 |
Typeface.RESOLVE_BY_FONT_TABLE); |
| 238 |
int italic = array.getInt(R.styleable.FontFamilyFont_fontStyle, |
| 239 |
FontFileResourceEntry.RESOLVE_BY_FONT_TABLE); |
| 240 |
String variationSettings = array.getString( |
| 241 |
R.styleable.FontFamilyFont_fontVariationSettings); |
| 242 |
int ttcIndex = array.getInt(R.styleable.FontFamilyFont_ttcIndex, 0); |
| 243 |
String filename = array.getString(R.styleable.FontFamilyFont_font); |
| 244 |
array.recycle(); |
| 245 |
while (parser.next() != XmlPullParser.END_TAG) { |
| 246 |
skip(parser); |
| 247 |
} |
| 248 |
if (filename == null) { |
| 249 |
return null; |
| 250 |
} |
| 251 |
return new FontFileResourceEntry(filename, weight, italic, variationSettings, ttcIndex); |
| 252 |
} |
| 253 |
|
| 254 |
private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException { |
| 255 |
int depth = 1; |
| 256 |
while (depth > 0) { |
| 257 |
switch (parser.next()) { |
| 258 |
case XmlPullParser.START_TAG: |
| 259 |
depth++; |
| 260 |
break; |
| 261 |
case XmlPullParser.END_TAG: |
| 262 |
depth--; |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |