Preload JS fonts, compile fix

This commit is contained in:
Joshua Granick
2014-08-13 14:19:39 -07:00
parent f409b5bc85
commit 580540aa99
3 changed files with 83 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import lime.Assets;
#if js
import js.html.Image;
import js.Browser;
import lime.net.URLLoader;
import lime.net.URLRequest;
#elseif flash
@@ -93,6 +94,11 @@ class Preloader #if flash extends Sprite #end {
loaders.set (url, loader);
total++;
case FONT:
total++;
loadFont (url);
default:
}
@@ -118,6 +124,78 @@ class Preloader #if flash extends Sprite #end {
}
#if js
private function loadFont (font:String):Void {
var node = Browser.document.createElement ("span");
node.innerHTML = "giItT1WQy@!-/#";
var style = node.style;
style.position = "absolute";
style.left = "-10000px";
style.top = "-10000px";
style.fontSize = "300px";
style.fontFamily = "sans-serif";
style.fontVariant = "normal";
style.fontStyle = "normal";
style.fontWeight = "normal";
style.letterSpacing = "0";
Browser.document.body.appendChild (node);
var width = node.offsetWidth;
style.fontFamily = "'" + font + "'";
var interval:Null<Int> = null;
var found = false;
var checkFont = function () {
if (node.offsetWidth != width) {
// Test font was still not available yet, try waiting one more interval?
if (!found) {
found = true;
return false;
}
loaded ++;
if (interval != null) {
Browser.window.clearInterval (interval);
}
node.parentNode.removeChild (node);
node = null;
update (loaded, total);
if (loaded == total) {
start ();
}
return true;
}
return false;
}
if (!checkFont ()) {
interval = Browser.window.setInterval (checkFont, 50);
}
}
#end
private function start ():Void {
#if flash

View File

@@ -30,8 +30,10 @@ import format.png.Reader;
import format.png.Tools;
import format.png.Writer;
import format.tools.Deflate;
#if sys
import sys.io.File;
#end
#end
@:allow(lime.graphics.util.ImageCanvasUtil)
@:allow(lime.graphics.util.ImageDataUtil)
@@ -851,7 +853,7 @@ class Image {
var buffer = null;
#if (!disable_cffi || !format)
#if (sys && (!disable_cffi || !format))
var data = lime_image_load (path);
if (data != null) buffer = new ImageBuffer (new UInt8Array (data.data), data.width, data.height, data.bpp);

View File

@@ -250,6 +250,8 @@ class DefaultAssetLibrary extends AssetLibrary {
#end
#end
return null;
}