Introduce set/getFontVariationSettings.
This CL enables developers to specify axis values to the underlying font collection. The specification of the font variation settings is the same as the CSS font-variation-settings attribute in CSS working draft as of 2016-11-30. Code example: Here is an example to set width 100 and weight 1.5. TextView tv = (TextView) findViewById(R.id.textView); tv.setFontVariationSettings("'wdth' 100, 'wght' 1.5"); Bug: 33062398 Test: Manually done. Ran FrameworksGraphicsTests, CtsGraphicsTestCases and CtsWidgetTestCases Change-Id: I249d464f8cdaa56017a987588b94ed685aadeb58
This commit is contained in:
@ -21,6 +21,7 @@ import android.util.Xml;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import android.annotation.Nullable;
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -104,9 +105,12 @@ public class FontListParser {
|
||||
// Note that a well-formed variation contains a four-character tag and a float as styleValue,
|
||||
// with spacers in between. The tag is enclosd either by double quotes or single quotes.
|
||||
@VisibleForTesting
|
||||
public static Axis[] parseFontVariationSettings(String settings) {
|
||||
String[] settingList = settings.split(",");
|
||||
public static ArrayList<Axis> parseFontVariationSettings(@Nullable String settings) {
|
||||
ArrayList<Axis> axisList = new ArrayList<>();
|
||||
if (settings == null) {
|
||||
return axisList;
|
||||
}
|
||||
String[] settingList = settings.split(",");
|
||||
settingLoop:
|
||||
for (String setting : settingList) {
|
||||
int pos = 0;
|
||||
@ -150,7 +154,7 @@ public class FontListParser {
|
||||
tagString.charAt(3));
|
||||
axisList.add(new Axis(tag, styleValue));
|
||||
}
|
||||
return axisList.toArray(new Axis[axisList.size()]);
|
||||
return axisList;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
@ -71,6 +71,7 @@ public class Paint {
|
||||
|
||||
private LocaleList mLocales;
|
||||
private String mFontFeatureSettings;
|
||||
private String mFontVariationSettings;
|
||||
|
||||
private static final Object sCacheLock = new Object();
|
||||
|
||||
@ -1493,6 +1494,37 @@ public class Paint {
|
||||
nSetFontFeatureSettings(mNativePaint, settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the font variation settings.
|
||||
*
|
||||
* @return the paint's currently set font variation settings. Default is null.
|
||||
*
|
||||
* @see #setFontVariationSettings(String)
|
||||
*/
|
||||
public String getFontVariationSettings() {
|
||||
return mFontVariationSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set font variation settings.
|
||||
*
|
||||
* @param settings font variation settings, e.g. "'wdth' 300, 'wght' 1.8"
|
||||
*
|
||||
* @see #getFontVariationSettings()
|
||||
*
|
||||
* @param settings the font variation settings. You can pass null or empty string as no
|
||||
* variation settings.
|
||||
*/
|
||||
public void setFontVariationSettings(String settings) {
|
||||
settings = TextUtils.nullIfEmpty(settings);
|
||||
if (settings == mFontVariationSettings
|
||||
|| (settings != null && settings.equals(mFontVariationSettings))) {
|
||||
return;
|
||||
}
|
||||
mFontVariationSettings = settings;
|
||||
setTypeface(Typeface.createFromTypefaceWithVariation(mTypeface, settings));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current value of hyphen edit.
|
||||
*
|
||||
|
@ -21,6 +21,7 @@ import android.util.Log;
|
||||
import android.util.LongSparseArray;
|
||||
import android.util.LruCache;
|
||||
import android.util.SparseArray;
|
||||
import android.graphics.FontListParser;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
@ -171,6 +172,15 @@ public class Typeface {
|
||||
return typeface;
|
||||
}
|
||||
|
||||
/** @hide */
|
||||
public static Typeface createFromTypefaceWithVariation(Typeface family,
|
||||
String fontVariationSettings) {
|
||||
final long ni = family == null ? 0 : family.native_instance;
|
||||
ArrayList<FontListParser.Axis> axes =
|
||||
FontListParser.parseFontVariationSettings(fontVariationSettings);
|
||||
return new Typeface(nativeCreateFromTypefaceWithVariation(ni, axes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one of the default typeface objects, based on the specified style
|
||||
*
|
||||
@ -443,6 +453,8 @@ public class Typeface {
|
||||
}
|
||||
|
||||
private static native long nativeCreateFromTypeface(long native_instance, int style);
|
||||
private static native long nativeCreateFromTypefaceWithVariation(
|
||||
long native_instance, List<FontListParser.Axis> axes);
|
||||
private static native long nativeCreateWeightAlias(long native_instance, int weight);
|
||||
private static native void nativeUnref(long native_instance);
|
||||
private static native int nativeGetStyle(long native_instance);
|
||||
|
Reference in New Issue
Block a user