Skip to content

Commit

Permalink
New method, getDefaultFont
Browse files Browse the repository at this point in the history
  • Loading branch information
eb1 committed Sep 30, 2016
1 parent 4cb29c1 commit 606ae24
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 47 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
|:-:|:-:|
| [![npm](https://img.shields.io/npm/dm/cordova-plugin-fonts.svg)](https://www.npmjs.com/package/cordova-plugin-fonts) | [![Build Status](https://travis-ci.org/adapt-it/cordova-fonts.svg?branch=master)](https://travis-ci.org/adapt-it/cordova-fonts) |

A Cordova plugin that enumerates the fonts installed on the local device.
A Cordova plugin that enumerates the fonts installed on the local device, and also provides the name of the default font.

This plugin defines a global `Fonts` object, which provides access to the fonts installed on the device. The `Fonts` object is available from the `navigator` object after the `deviceready` event fires.

Expand Down Expand Up @@ -58,7 +58,7 @@ The `Fonts` object provides a way to enumerate through the list of fonts install

## Methods

Currently this plugin only provides a single method, **getFontList**.
Currently this plugin provides two methods, **getFontList** and **getDefaultFont**.

### GetFontList

Expand Down Expand Up @@ -91,6 +91,35 @@ Firefox OS does not provide an API to access the fonts on the device. The Fonts
console.log("Plugin error: Fonts plugin not found (is it installed?)");
}

### GetDefaultFont

**Parameters:**

- **successCallback**: Callback that returns the string name of the default font on the device.
- **errorCallback:** Callback that executes if an error occurs during the call.

**Firefox OS quirks**

Firefox OS does not provide an API to access the fonts on the device. The Fonts plugin currently returns a hard-coded string for the default font "Fira Sans Regular". See https://www.mozilla.org/en-US/styleguide/products/firefox-os/typeface/ for more information.

### Example

if (navigator.Fonts) {
console.log("Fonts object in navigator");
navigator.Fonts.getDefaultFont(
function (defaultFont) {
if (defaultFont) {
console.log("Default Font: " + defaultFont);
}
},
function (error) {
console.log("DefaultFont error: " + error);
}
);
} else {
console.log("Plugin error: Fonts plugin not found (is it installed?)");
}


## Internal Development / Unit Testing

Expand Down
36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
{
"name": "cordova-plugin-fonts",
"version": "0.5.0",
"version": "0.6.0",
"description": "Cordova Fonts plugin",
"keywords": [
"cordova",
"ecosystem:cordova",
"cordova-android",
"cordova-ios",
"cordova-firefox",
"cordova-windows",
"plugin",
"fonts"
],
"homepage": "https://github.com/adapt-it/cordova-fonts",
"bugs": {
"url": "https://github.com/adapt-it/cordova-fonts/issues"
},
"license": "Apache-2.0",
"cordova": {
"id": "cordova-plugin-fonts",
"platforms": [
Expand All @@ -10,7 +26,6 @@
"windows"
]
},
"description": "Cordova Fonts plugin",
"main": "fonts.js",
"directories": {
"doc": "doc",
Expand All @@ -25,17 +40,6 @@
"url": "https://github.com/adapt-it/cordova-fonts.git"
},
"author": "Erik Brommers",
"license": "Apache-2.0",
"keywords": [
"cordova",
"ecosystem:cordova",
"cordova-android",
"cordova-ios",
"cordova-firefox",
"cordova-windows",
"plugin",
"fonts"
],
"engines": {
"cordovaDependencies": {
"2.0.0": {
Expand All @@ -45,9 +49,5 @@
},
"devDependencies": {
"jshint": "^2.6.0"
},
"bugs": {
"url": "https://github.com/adapt-it/cordova-fonts/issues"
},
"homepage": "https://github.com/adapt-it/cordova-fonts"
}
}
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-fonts"
version="0.5.0"
version="0.6.0"
xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android">
Expand Down
62 changes: 60 additions & 2 deletions src/android/Fonts.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;


public class Fonts extends CordovaPlugin {
public static final String GETFONTLIST = "getFontList";
public static final String GETDEFAULTFONT = "getDefaultFont";

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
// JSONArray results;

try {
if (action.equals(GETFONTLIST)) {
Expand All @@ -56,7 +57,17 @@ public void run() {
}
);
return true;

} else if (action.equals(GETDEFAULTFONT)) {
cordova.getThreadPool().execute(
new Runnable() {
public void run() {
final String results = getDefaultFont();
System.out.println("results: " + results.toString());
callbackContext.success(results);
}
}
);
return true;
} else {
return false;
}
Expand All @@ -67,6 +78,53 @@ public void run() {
return true;
}

private String getDefaultFont() {
return "Roboto Regular";
/*
// EDB - work around a google / android quirk. The following is how it _should_ work.
// Unfortunately, even when Typeface.DEFAULT
// is compared against _every_ font in the font directories, no matches are found. Even
// the Roboto Regular font shows up as a mismatch, when it is the standard font
// according to the Google materials doc (https://material.google.com/style/typography.html#)
String[] fontdirs = { "/system/fonts", "/system/font", "/data/fonts" };
TTFAnalyzer analyzer = new TTFAnalyzer();
Typeface tfDefault = Typeface.DEFAULT;
Typeface tfTemp = null;
String defaultFontName = "";
System.out.println("getDefaultFont(): entry");
System.out.println("tfDefault: " + tfDefault.toString());
for ( String fontdir : fontdirs )
{
File dir = new File( fontdir );
if ( !dir.exists() )
continue;
File[] files = dir.listFiles();
if ( files == null )
continue;
for ( File file : files )
{
String fontname = analyzer.getTtfFontName( file.getAbsolutePath() );
if ( fontname != null ) {
System.out.println("found font: " + fontname);
tfTemp = Typeface.createFromFile(file);
System.out.println("tfTemp: " + tfTemp.toString());
if (tfTemp.equals(tfDefault)) {
System.out.println("Found default font: " + fontname);
defaultFontName = fontname;
}
}
}
}
return defaultFontName;
*/
}

private JSONArray getFontList() {
System.out.println("getFontList(): entry");
String[] fontdirs = { "/system/fonts", "/system/font", "/data/fonts" };
Expand Down
8 changes: 7 additions & 1 deletion src/firefoxos/FontsProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ function getFontList(successCB, errorCB) {
successCB({value: fonts});
}

function getDefaultFont(successCB, errorCB) {
'use strict';
successCB({value: "Fira Sans Regular"});
}

var Fonts = {
getFontList: getFontList
getFontList: getFontList,
getDefaultFont: getDefaultFont
};

require("cordova/exec/proxy").add("Fonts", Fonts);
1 change: 1 addition & 0 deletions src/ios/CDVFonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
@interface CDVFonts : CDVPlugin

- (void)getFontList:(CDVInvokedUrlCommand*)command;
- (void)getDefaultFont:(CDVInvokedUrlCommand*)command;

@end
18 changes: 17 additions & 1 deletion src/ios/CDVFonts.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,23 @@ - (void)getFontList:(CDVInvokedUrlCommand*)command
}

if (fonts != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:fonts];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:fonts];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)getDefaultFont:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult = nil;

UIFont *systemFont = [UIFont systemFontOfSize:12];

if (systemFont != nil) {
NSString *fontName = systemFont.familyName;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:fontName];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
Expand Down
75 changes: 53 additions & 22 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,58 @@ exports.defineAutoTests = function () {
expect(typeof navigator.Fonts.getFontList).toBeDefined();
expect(typeof navigator.Fonts.getFontList).toBe("function");
});

it("should contain a getDefaultFont function", function () {
expect(typeof navigator.Fonts.getDefaultFont).toBeDefined();
expect(typeof navigator.Fonts.getDefaultFont).toBe("function");
});
});
};

exports.defineManualTests = function (contentEl, createActionButton) {
var device_tests = '<h3>Press Fonts button to get the list of defined fonts</h3>' +
'<div id="cdv_fonts"></div>' +
'Expected result: Status box will get updated with installed fonts.',

logMessage = function (message, color) {
var log = document.getElementById('info'),
logLine = document.createElement('div');

if (color) {
logLine.style.color = color;
}
logLine.innerHTML = message;
log.appendChild(logLine);
},
var logMessage = function (message, color) {
var log = document.getElementById('info'),
logLine = document.createElement('div');

clearLog = function () {
var log = document.getElementById('info');
log.innerHTML = '';
};
if (color) {
logLine.style.color = color;
}
logLine.innerHTML = message;
log.appendChild(logLine);
};

var clearLog = function () {
var log = document.getElementById('info');
log.innerHTML = '';
};

contentEl.innerHTML = '<div id="info"></div>' + device_tests;
var defaultFontTest = function () {
clearLog();
console.log("defaultFontTest()");
if (navigator.Fonts) {
navigator.Fonts.getDefaultFont(
function (defaultFont) {
console.log("defaultFontTest() - value returned: " + defaultFont);
logMessage("Default Font: " + defaultFont);
logMessage("-----");
},
function (error) {
logMessage(error);
}
);
} else {
console.log("Plugin error: Fonts plugin not found (is it installed?)");
}
};

createActionButton('Display Fonts', function () {
var fontListTest = function () {
clearLog();
var i = 0;
console.log("fontListTest()");
if (navigator.Fonts) {
navigator.Fonts.getFontList(
function (fontlist) {
console.log(fontlist.length + " font(s) returned");
for (i = 0; i < fontlist.length; i++) {
logMessage("Font: " + fontlist[i]);
}
Expand All @@ -73,8 +92,20 @@ exports.defineManualTests = function (contentEl, createActionButton) {
}
);
} else {
logMessage("no Fonts object in navigator");
console.log("Plugin error: Fonts plugin not found (is it installed?)");
}

};

var device_tests = '<p/><div id="cdv_fonts"></div>' +
'<p/><div id="cdv_default"></div>';

contentEl.innerHTML = '<div id="info"></div>' + device_tests;

createActionButton('Default Font', function () {
defaultFontTest();
}, "cdv_default");

createActionButton('Display Fonts', function () {
fontListTest();
}, "cdv_fonts");
};
16 changes: 16 additions & 0 deletions www/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@ var Fonts = {
* @param {function} successCB
* @param {function} errorCB
*
* @return Object.value {Array{String}}: the list of fonts installed on this device.
* Example
* Fonts.getFontList(function(fontList) {console.log(fontList);},
* function(error) {console.log(error);});
*/
getFontList: function (successCB, errorCB) {
exec(successCB, errorCB, "Fonts", "getFontList", []);
},
/**
* Returns the string name of the default font on the device.
*
* @param {function} successCB
* @param {function} errorCB
*
* @return Object.value {String}: the default font name
*
* Example
* Fonts.getDefaultFont(function(fontName) {console.log(fontName);},
* function(error) {console.log(error);});
*/
getDefaultFont: function (successCB, errorCB) {
exec(successCB, errorCB, "Fonts", "getDefaultFont", []);
}

};
Expand Down

0 comments on commit 606ae24

Please sign in to comment.