mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-22 01:34:21 +00:00
Added support for in-memory fonts on Android.
This commit is contained in:
parent
a0af60a179
commit
c87463cad1
3 changed files with 127 additions and 20 deletions
|
|
@ -42,10 +42,8 @@ import android.opengl.*;
|
|||
import android.text.ClipboardManager;
|
||||
import android.text.InputType;
|
||||
import android.util.DisplayMetrics;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import android.util.Log;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.net.HttpURLConnection;
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
|
|
@ -79,6 +77,8 @@ public class JuceAppActivity extends Activity
|
|||
{
|
||||
quitApp();
|
||||
super.onDestroy();
|
||||
|
||||
clearDataCache();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -816,4 +816,85 @@ public class JuceAppActivity extends Activity
|
|||
{
|
||||
new SingleMediaScanner (this, filename);
|
||||
}
|
||||
|
||||
public final Typeface getTypeFaceFromAsset (String assetName)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Typeface.createFromAsset (this.getResources().getAssets(), assetName);
|
||||
}
|
||||
catch (Throwable e) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
public static String bytesToHex (byte[] bytes)
|
||||
{
|
||||
char[] hexChars = new char[bytes.length * 2];
|
||||
|
||||
for (int j = 0; j < bytes.length; ++j)
|
||||
{
|
||||
int v = bytes[j] & 0xff;
|
||||
hexChars[j * 2] = hexArray[v >>> 4];
|
||||
hexChars[j * 2 + 1] = hexArray[v & 0x0f];
|
||||
}
|
||||
|
||||
return new String (hexChars);
|
||||
}
|
||||
|
||||
final private java.util.Map dataCache = new java.util.HashMap();
|
||||
|
||||
synchronized private final File getDataCacheFile (byte[] data)
|
||||
{
|
||||
try
|
||||
{
|
||||
java.security.MessageDigest digest = java.security.MessageDigest.getInstance ("MD5");
|
||||
digest.update (data);
|
||||
|
||||
String key = bytesToHex (digest.digest());
|
||||
|
||||
if (dataCache.containsKey (key))
|
||||
return (File) dataCache.get (key);
|
||||
|
||||
File f = new File (this.getCacheDir(), "bindata_" + key);
|
||||
f.delete();
|
||||
FileOutputStream os = new FileOutputStream (f);
|
||||
os.write (data, 0, data.length);
|
||||
dataCache.put (key, f);
|
||||
return f;
|
||||
}
|
||||
catch (Throwable e) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private final void clearDataCache()
|
||||
{
|
||||
java.util.Iterator it = dataCache.values().iterator();
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
File f = (File) it.next();
|
||||
f.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public final Typeface getTypeFaceFromByteArray (byte[] data)
|
||||
{
|
||||
try
|
||||
{
|
||||
File f = getDataCacheFile (data);
|
||||
|
||||
if (f != null)
|
||||
return Typeface.createFromFile (f);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.e ("JUCE", e.toString());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue