/*
* ScriptInfo.js - information about scripts
*
* Copyright © 2012-2018, JEDLSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// !data scripts
var ilib = require("../index.js");
var Utils = require("./Utils.js");
/**
* @class
* Create a new script info instance. This class encodes information about
* scripts, which are sets of characters used in a writing system.<p>
*
* The options object may contain any of the following properties:
*
* <ul>
* <li><i>onLoad</i> - a callback function to call when the script info object is fully
* loaded. When the onLoad option is given, the script info object will attempt to
* load any missing locale data using the ilib loader callback.
* When the constructor is done (even if the data is already preassembled), the
* onLoad function is called with the current instance as a parameter, so this
* callback can be used with preassembled or dynamic loading or a mix of the two.
*
* <li><i>sync</i> - tell whether to load any missing locale data synchronously or
* asynchronously. If this option is given as "false", then the "onLoad"
* callback must be given, as the instance returned from this constructor will
* not be usable for a while.
*
* <li><i>loadParams</i> - an object containing parameters to pass to the
* loader callback function when locale data is missing. The parameters are not
* interpretted or modified in any way. They are simply passed along. The object
* may contain any property/value pairs as long as the calling code is in
* agreement with the loader callback function as to what those parameters mean.
* </ul>
*
*
* @constructor
* @param {string} script The ISO 15924 4-letter identifier for the script
* @param {Object=} options parameters to initialize this instance
*/
var ScriptInfo = function(script, options) {
var sync = true,
loadParams = undefined;
this.script = script;
if (options) {
if (typeof(options.sync) !== 'undefined') {
sync = !!options.sync;
}
if (typeof(options.loadParams) !== 'undefined') {
loadParams = options.loadParams;
}
}
if (!ilib.data.scripts) {
Utils.loadData({
object: "ScriptInfo",
nonlocale: true,
name: "scripts.json",
sync: sync,
loadParams: loadParams,
callback: ilib.bind(this, function (info) {
if (!info) {
info = {"Latn":{"nb":215,"nm":"Latin","lid":"Latin","rtl":false,"ime":false,"casing":true}};
}
ilib.data.scripts = info;
this.info = script && ilib.data.scripts[script];
if (options && typeof(options.onLoad) === 'function') {
options.onLoad(this);
}
})
});
} else {
this.info = ilib.data.scripts[script];
if (options && typeof(options.onLoad) === 'function') {
options.onLoad(this);
}
}
};
/**
* @private
*/
ScriptInfo._getScriptsArray = function() {
var ret = [],
script = undefined,
scripts = ilib.data.scripts;
for (script in scripts) {
if (script && scripts[script]) {
ret.push(script);
}
}
return ret;
};
/**
* Return an array of all ISO 15924 4-letter identifier script identifiers that
* this copy of ilib knows about.
* @static
* @param {boolean} sync whether to find the available ids synchronously (true) or asynchronously (false)
* @param {Object} loadParams arbitrary object full of properties to pass to the loader
* @param {function(Array.<string>)} onLoad callback function to call when the data is finished loading
* @return {Array.<string>} an array of all script identifiers that this copy of
* ilib knows about
*/
ScriptInfo.getAllScripts = function(sync, loadParams, onLoad) {
if (!ilib.data.scripts) {
Utils.loadData({
object: "ScriptInfo",
locale: "-",
name: "scripts.json",
sync: sync,
loadParams: loadParams,
callback: ilib.bind(this, function (info) {
ilib.data.scripts = info;
if (typeof(onLoad) === 'function') {
onLoad(ScriptInfo._getScriptsArray());
}
})
});
} else {
if (typeof(onLoad) === 'function') {
onLoad(ScriptInfo._getScriptsArray());
}
}
return ScriptInfo._getScriptsArray();
};
ScriptInfo.prototype = {
/**
* Return the 4-letter ISO 15924 identifier associated
* with this script.
* @return {string} the 4-letter ISO code for this script
*/
getCode: function () {
return this.info && this.script;
},
/**
* Get the ISO 15924 code number associated with this
* script.
*
* @return {number} the ISO 15924 code number
*/
getCodeNumber: function () {
return this.info && this.info.nb || 0;
},
/**
* Get the name of this script in English.
*
* @return {string} the name of this script in English
*/
getName: function () {
return this.info && this.info.nm;
},
/**
* Get the long identifier assciated with this script.
*
* @return {string} the long identifier of this script
*/
getLongCode: function () {
return this.info && this.info.lid;
},
/**
* Return the usual direction that text in this script is written
* in. Possible return values are "rtl" for right-to-left,
* "ltr" for left-to-right, and "ttb" for top-to-bottom.
*
* @return {string} the usual direction that text in this script is
* written in
*/
getScriptDirection: function() {
return (this.info && typeof(this.info.rtl) !== 'undefined' && this.info.rtl) ? "rtl" : "ltr";
},
/**
* Return true if this script typically requires an input method engine
* to enter its characters.
*
* @return {boolean} true if this script typically requires an IME
*/
getNeedsIME: function () {
return this.info && this.info.ime ? true : false; // converts undefined to false
},
/**
* Return true if this script uses lower- and upper-case characters.
*
* @return {boolean} true if this script uses letter case
*/
getCasing: function () {
return this.info && this.info.casing ? true : false; // converts undefined to false
}
};
module.exports = ScriptInfo;
Source