This means that the Java activity layer's user-defined default font through standard Framework/jni is not passed to the chromium kernel.
The basic idea is to obtain the current default font settings for the Java layer system through Typeface.default, get the native_instance handle through the Java dynamic Reflection call, and then try to get the sktypeface* pointer with JNI C + + code. Call Sktypeface::serialize to serialize to a temporary font file, then return the path to Java and re-upload it to the Chromium kernel code.
Section 2 Core Demo code:
Awsettings.java:
#if enable_user_overrides_fontfallback//hacker techniques to get the current system's default typeface and apply it to C Hromium/skia Kernel code: @CalledByNative private static Long Getdefaulttypefacenativepointer () {try{ Android.graphics.Typeface defaulttypeface = Android.graphics.Typeface.DEFAULT; Java.lang.reflect.Field Field = null; field = Android.graphics.Typeface.class.getDeclaredField ("native_instance"); LONG ret = 0; if (android.os.build.version.sdk_int>=21) {//5.0+ ret = Field.getlong (defaulttypeface); }else{//4.4-ret = Field.getint (defaulttypeface);//boost to Long} LOG.I (TAG, "android.graphics.typeface.default.native_instance=" +ret); return ret; }catch (Exception e) {log.e (TAG, E.getmessage (), E); return 0; }}//private Boolean Suseroverridesfontfallbackenabled = false; private static native String Nativegettempuseroverridesfontfallbackpath (); public void setuseroverridesfontfallbackenabled (Boolean enable) {if (enable) {String Path = awsettings . Nativegettempuseroverridesfontfallbackpath (); Setuseroverridesfontfallback (True, path); }else{Setuseroverridesfontfallback (False, ""); }}//#if
aw_settings.cc:
#if enable_user_overrides_fontfallbackstatic jstring Gettempuseroverridesfontfallbackpath (JNIEnv* env, Jclass clazz) {VLOG (0) << "Gettempuseroverridesfontfallbackpath enter"; Jlong Native_instance_pointer = Java_awsettings_getdefaulttypefacenativepointer (env); if (native_instance_pointer==0) {VLOG (0) << "Gettempuseroverridesfontfallbackpath Getdefaulttypefacenativepointer failed! "; return 0; }//sktypeface* typeface = (sktypeface*) (int) Native_instance_pointer; Alert:this C + + Sktypeface is got from the framework layer, it may not be having the same ABI compatibility with Chromioum ' s Internal Skia Library typefaceimpl* Typefaceimpl = (typefaceimpl*) native_instance_pointer; sktypeface* typeface = 0; Todo:cannot get sktypeface* from typefaceimpl*//i need a writable path! FilePath Cache_dir_filepath; /*bool ret = */base::android::getcachedirectory (&cache_dir_filepath); FilePath Temp_filepath = Cache_dir_filepath. Append (FilePath ("tEst.ttf.ser ")); <--todo:generate this std::string gen_temp_path = Temp_filepath. Asutf8unsafe (); Const char* Temp_path = Gen_temp_path.c_str (); "/data/local/test.ttf.ser"; VLOG (0) << "Gettempuseroverridesfontfallbackpath gen_temp_path=" <<temp_path; Skfilewstream Wstream (Temp_path); Typeface->serialize (&wstream);//may fail, but we don ' t know! scopedjavalocalref<jstring> Jpath = convertutf8tojavastring (env, Temp_path); Return Jpath. Release (); } #endif
Unfortunately, Native_instance actually corresponds to an opaque typefaceimpl*, which may also be inaccessible under JNI. Even if you can access it, you will not get sktypeface* from typefaceimpl*.
Android Chromium: Unsuccessful attempt to get C + + Sktypeface object from Typeface.java class