diff --git a/apps/tests/pages/fonts-test.xml b/apps/tests/pages/fonts-test.xml
index d9fd65d20..65a34edc6 100644
--- a/apps/tests/pages/fonts-test.xml
+++ b/apps/tests/pages/fonts-test.xml
@@ -9,6 +9,9 @@
+
+
+
@@ -17,6 +20,9 @@
+
+
+
@@ -25,6 +31,9 @@
+
+
+
@@ -33,6 +42,9 @@
+
+
+
diff --git a/ui/styling/font-common.ts b/ui/styling/font-common.ts
index 87d5bf867..fc5ed9977 100644
--- a/ui/styling/font-common.ts
+++ b/ui/styling/font-common.ts
@@ -69,3 +69,9 @@ export class Font implements definitios.Font {
throw new Error("This should be called on the derived class");
}
}
+
+export module genericFontFamilies {
+ export var serif = "serif";
+ export var sansSerif = "sans-serif";
+ export var monospace = "monospace";
+}
\ No newline at end of file
diff --git a/ui/styling/font.android.ts b/ui/styling/font.android.ts
index 4ed2a2db0..e92e87cfe 100644
--- a/ui/styling/font.android.ts
+++ b/ui/styling/font.android.ts
@@ -1,8 +1,11 @@
import enums = require("ui/enums");
import common = require("ui/styling/font-common");
+import application = require("application");
+import trace = require("trace");
+import types = require("utils/types");
-//declare var exports;
-//require("utils/module-merge").merge(common, exports);
+var typefaceCache = new Map();
+var appAssets: android.content.res.AssetManager;
export class Font extends common.Font {
public static default = new Font(undefined, enums.FontStyle.normal, enums.FontWeight.normal);
@@ -26,7 +29,8 @@ export class Font extends common.Font {
style = android.graphics.Typeface.NORMAL;
}
- this._android = android.graphics.Typeface.create(this.fontFamily, style);
+ var typeFace = this.getTypeFace(this.fontFamily);
+ this._android = android.graphics.Typeface.create(typeFace, style);
}
return this._android;
}
@@ -46,5 +50,46 @@ export class Font extends common.Font {
public withFontWeight(weight: string): Font {
return new Font(this.fontFamily, this.fontStyle, weight);
}
-}
+ private getTypeFace(fontFamily: string): android.graphics.Typeface {
+ if (!fontFamily) {
+ return null;
+ }
+
+ switch (fontFamily.toLowerCase()) {
+ case common.genericFontFamilies.serif:
+ return android.graphics.Typeface.SERIF;
+
+ case common.genericFontFamilies.sansSerif:
+ return android.graphics.Typeface.SANS_SERIF;
+
+ case common.genericFontFamilies.monospace:
+ return android.graphics.Typeface.MONOSPACE;
+
+ default:
+ return this.loadFontFromAsset(fontFamily);
+ }
+ }
+
+ private loadFontFromAsset(fontFamily: string): android.graphics.Typeface {
+ appAssets = appAssets || application.android.context.getAssets();
+ if (!appAssets) {
+ return null;
+ }
+
+ var result = typefaceCache.get(fontFamily);
+ // Check for undefined explicitly as null mean we tried to load the font, but failed.
+ if (types.isUndefined(result)) {
+ result = null;
+ var fontAsset = "app/fonts/" + fontFamily + ".ttf";
+ try {
+ result = android.graphics.Typeface.createFromAsset(appAssets, fontAsset);
+ } catch (e) {
+ trace.write("Cannot find font asset: " + fontAsset, trace.categories.Error, trace.messageType.error);
+ }
+ typefaceCache.set(fontFamily, result);
+ }
+
+ return result;
+ }
+}
diff --git a/ui/styling/font.ios.ts b/ui/styling/font.ios.ts
index 562deaf34..7149748ce 100644
--- a/ui/styling/font.ios.ts
+++ b/ui/styling/font.ios.ts
@@ -1,8 +1,9 @@
import enums = require("ui/enums");
import common = require("ui/styling/font-common");
-//declare var exports;
-//require("utils/module-merge").merge(common, exports);
+var DEFAULT_SERIF = "Times New Roman";
+var DEFAULT_SANS_SERIF = "Helvetica";
+var DEFAULT_MONOSPACE = "Courier New";
export class Font extends common.Font {
public static default = new Font(undefined, enums.FontStyle.normal, enums.FontWeight.normal);
@@ -10,7 +11,8 @@ export class Font extends common.Font {
private _ios: UIFontDescriptor;
get ios(): UIFontDescriptor {
if (!this._ios) {
- this._ios = UIFontDescriptor.fontDescriptorWithNameSize(this.fontFamily, 0);
+ var fontFamily = this.getFontFamilyRespectingGenericFonts(this.fontFamily);
+ this._ios = UIFontDescriptor.fontDescriptorWithNameSize(fontFamily, 0);
if (this.isBold) {
if (this.isItalic) {
this._ios = this._ios.fontDescriptorWithSymbolicTraits(
@@ -45,5 +47,25 @@ export class Font extends common.Font {
public withFontWeight(weight: string): Font {
return new Font(this.fontFamily, this.fontStyle, weight);
}
+
+ private getFontFamilyRespectingGenericFonts(fontFamily: string): string {
+ if (!fontFamily) {
+ return fontFamily;
+ }
+
+ switch (fontFamily.toLowerCase()) {
+ case common.genericFontFamilies.serif:
+ return DEFAULT_SERIF;
+
+ case common.genericFontFamilies.sansSerif:
+ return DEFAULT_SANS_SERIF;
+
+ case common.genericFontFamilies.monospace:
+ return DEFAULT_MONOSPACE;
+
+ default:
+ return fontFamily;
+ }
+ }
}
diff --git a/ui/styling/styling.d.ts b/ui/styling/styling.d.ts
index 210b62f40..643f06502 100644
--- a/ui/styling/styling.d.ts
+++ b/ui/styling/styling.d.ts
@@ -68,7 +68,6 @@
*/
fontWeight: string;
-
/**
* Gets or sets text-alignment style property.
*/