Resiter custom fonts for IOS

This commit is contained in:
vakrilov
2015-06-26 11:11:46 +03:00
parent d7da00fc17
commit 536791dee7
3 changed files with 47 additions and 13 deletions

View File

@@ -7,6 +7,7 @@ import fs = require("file-system");
var typefaceCache = new Map<string, android.graphics.Typeface>();
var appAssets: android.content.res.AssetManager;
var FONTS_BASE_PATH = "app/fonts/";
export class Font extends common.Font {
public static default = new Font(undefined, undefined, enums.FontStyle.normal, enums.FontWeight.normal);
@@ -99,10 +100,10 @@ export class Font extends common.Font {
var fontAssetPath: string;
var basePath = fs.path.join(fs.knownFolders.currentApp().path, "fonts", fontFamily);
if (fs.File.exists(basePath + ".ttf")) {
fontAssetPath = "app/fonts/" + fontFamily + ".ttf";
fontAssetPath = FONTS_BASE_PATH + fontFamily + ".ttf";
}
else if (fs.File.exists(basePath + ".otf")) {
fontAssetPath = "app/fonts/" + fontFamily + ".otf";
fontAssetPath = FONTS_BASE_PATH + fontFamily + ".otf";
}
else {
trace.write("Could not find font file for " + fontFamily, trace.categories.Error, trace.messageType.error);
@@ -120,4 +121,4 @@ export class Font extends common.Font {
return result;
}
}
}

View File

@@ -23,4 +23,8 @@
public static equals(value1: Font, value2: Font): boolean;
public static parse(cssValue: string): Font;
}
export module ios {
export function registerFont(fontFile: string);
}
}

View File

@@ -1,27 +1,31 @@
import enums = require("ui/enums");
import common = require("ui/styling/font-common");
import fs = require("file-system");
var DEFAULT_SERIF = "Times New Roman";
var DEFAULT_SANS_SERIF = "Helvetica";
var DEFAULT_MONOSPACE = "Courier New";
var areSystemFontSetsValid: boolean = false;
var systemFontFamilies = new Set();
var systemFonts = new Set();
function initSystemFotns() {
var nsFontFamilies = UIFont.familyNames();
for (var i = 0; i < nsFontFamilies.count; i++) {
var family = nsFontFamilies.objectAtIndex(i);
systemFontFamilies.add(family);
function assureSystemFotnSets() {
if (!areSystemFontSetsValid) {
var nsFontFamilies = UIFont.familyNames();
for (var i = 0; i < nsFontFamilies.count; i++) {
var family = nsFontFamilies.objectAtIndex(i);
systemFontFamilies.add(family);
var nsFonts = UIFont.fontNamesForFamilyName(family);
for (var j = 0; j < nsFonts.count; j++) {
var font = nsFonts.objectAtIndex(j);
systemFonts.add(font);
var nsFonts = UIFont.fontNamesForFamilyName(family);
for (var j = 0; j < nsFonts.count; j++) {
var font = nsFonts.objectAtIndex(j);
systemFonts.add(font);
}
}
areSystemFontSetsValid = true;
}
}
initSystemFotns();
export class Font extends common.Font {
public static default = new Font(undefined, undefined, enums.FontStyle.normal, enums.FontWeight.normal);
@@ -78,6 +82,8 @@ function resolveFontDescriptor(fontFamilyValue: string, symbolicTraits: number):
return null;
}
assureSystemFotnSets();
for (var i = 0; i < fonts.length; i++) {
var fontFamily = getFontFamilyRespectingGenericFonts(fonts[i]);
if (systemFontFamilies.has(fontFamily)) {
@@ -121,4 +127,27 @@ function getFontFamilyRespectingGenericFonts(fontFamily: string): string {
default:
return fontFamily;
}
}
export module ios {
export function registerFont(fontFile: string) {
var filePath = fs.path.join(fs.knownFolders.currentApp().path, "fonts", fontFile);
var fontData = NSFileManager.defaultManager().contentsAtPath(filePath);
if (!fontData) {
throw new Error("Could not load font from: " + fontFile);
}
var provider = CGDataProviderCreateWithCFData(fontData);
var font = CGFontCreateWithDataProvider(provider);
if (!font) {
throw new Error("Could not load font from: " + fontFile);
}
var error = NSError.alloc().init();
if (!CTFontManagerRegisterGraphicsFont(font, error)) {
throw new Error(error.localizedDescription);
}
areSystemFontSetsValid = false;
}
}