From 606ae240d70272202e21eb0e711afe2b8ffa260f Mon Sep 17 00:00:00 2001 From: eb1 Date: Fri, 30 Sep 2016 16:28:40 -0500 Subject: [PATCH] New method, getDefaultFont --- README.md | 33 +++++++++++++++- package.json | 36 +++++++++--------- plugin.xml | 2 +- src/android/Fonts.java | 62 +++++++++++++++++++++++++++++- src/firefoxos/FontsProxy.js | 8 +++- src/ios/CDVFonts.h | 1 + src/ios/CDVFonts.m | 18 ++++++++- tests/tests.js | 75 ++++++++++++++++++++++++++----------- www/fonts.js | 16 ++++++++ 9 files changed, 204 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index d329d38..facc8e6 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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 diff --git a/package.json b/package.json index d1514a1..a99d6d1 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -10,7 +26,6 @@ "windows" ] }, - "description": "Cordova Fonts plugin", "main": "fonts.js", "directories": { "doc": "doc", @@ -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": { @@ -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" + } } diff --git a/plugin.xml b/plugin.xml index 4cc61db..8342193 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,6 +1,6 @@ diff --git a/src/android/Fonts.java b/src/android/Fonts.java index 0ce6c23..91f1cfb 100644 --- a/src/android/Fonts.java +++ b/src/android/Fonts.java @@ -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)) { @@ -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; } @@ -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" }; diff --git a/src/firefoxos/FontsProxy.js b/src/firefoxos/FontsProxy.js index 2e07e18..092af51 100644 --- a/src/firefoxos/FontsProxy.js +++ b/src/firefoxos/FontsProxy.js @@ -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); \ No newline at end of file diff --git a/src/ios/CDVFonts.h b/src/ios/CDVFonts.h index 08691a0..e5616c0 100644 --- a/src/ios/CDVFonts.h +++ b/src/ios/CDVFonts.h @@ -3,5 +3,6 @@ @interface CDVFonts : CDVPlugin - (void)getFontList:(CDVInvokedUrlCommand*)command; +- (void)getDefaultFont:(CDVInvokedUrlCommand*)command; @end \ No newline at end of file diff --git a/src/ios/CDVFonts.m b/src/ios/CDVFonts.m index 6b3496c..508365c 100644 --- a/src/ios/CDVFonts.m +++ b/src/ios/CDVFonts.m @@ -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]; } diff --git a/tests/tests.js b/tests/tests.js index f87f47e..afd45d6 100755 --- a/tests/tests.js +++ b/tests/tests.js @@ -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 = '

Press Fonts button to get the list of defined fonts

' + - '
' + - '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 = '
' + 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]); } @@ -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 = '

' + + '

'; + + contentEl.innerHTML = '
' + device_tests; + + createActionButton('Default Font', function () { + defaultFontTest(); + }, "cdv_default"); + + createActionButton('Display Fonts', function () { + fontListTest(); }, "cdv_fonts"); }; diff --git a/www/fonts.js b/www/fonts.js index 80e1acb..d1c14f0 100644 --- a/www/fonts.js +++ b/www/fonts.js @@ -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", []); } };