UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

1 lines 2.96 kB
var ilib=require("./ilib.js");var Utils=require("./Utils.js");var JSUtils=require("./JSUtils.js");var IString=require("./IString.js");var CType=require("./CType.js");var GlyphString=function(t,i){if(i&&i.noinstance){return}IString.call(this,t);var n=true;var r={};if(i){if(typeof i.sync==="boolean"){n=i.sync}if(i.loadParams){r=i.loadParams}}CType._load("ctype_m",n,r,function(){if(!ilib.data.norm||JSUtils.isEmpty(ilib.data.norm.ccc)){Utils.loadData({object:GlyphString,locale:"-",name:"normdata.json",nonlocale:true,sync:n,loadParams:r,callback:ilib.bind(this,function(t){ilib.extend(ilib.data.norm,t);if(i&&typeof i.onLoad==="function"){i.onLoad(this)}})})}else{if(i&&typeof i.onLoad==="function"){i.onLoad(this)}}})};GlyphString.prototype=new IString(undefined);GlyphString.prototype.parent=IString;GlyphString.prototype.constructor=GlyphString;GlyphString._isJamoL=function(t){return t>=4352&&t<=4370};GlyphString._isJamoV=function(t){return t>=4449&&t<=4469};GlyphString._isJamoT=function(t){return t>=4520&&t<=4546};GlyphString._isHangul=function(t){return t>=44032&&t<=55203};GlyphString._composeJamoLV=function(t,i){var n=t-4352;var r=i-4449;return IString.fromCodePoint(44032+(n*21+r)*28)};GlyphString._composeJamoLVT=function(t,i){return IString.fromCodePoint(t+(i-4519))};GlyphString._compose=function(t,i){var n=t.charCodeAt(0);var r=i.charCodeAt(0);if(GlyphString._isHangul(n)&&GlyphString._isJamoT(r)){return GlyphString._composeJamoLVT(n,r)}else if(GlyphString._isJamoL(n)&&GlyphString._isJamoV(r)){return GlyphString._composeJamoLV(n,r)}var e=t+i;return ilib.data.norm.nfc&&ilib.data.norm.nfc[e]};GlyphString.prototype.charIterator=function(){var t=IString.prototype.charIterator.call(this);function i(i){this.index=0;this.spacingCombining=false;this.hasNext=function(){return!!this.nextChar||t.hasNext()};this.next=function(){var i=this.nextChar||t.next(),n=ilib.data.norm.ccc[i],r,e=i;this.nextChar=undefined;this.spacingCombining=false;if(ilib.data.norm.ccc&&(typeof ilib.data.norm.ccc[i]==="undefined"||ilib.data.norm.ccc[i]===0)){var a=true;while(t.hasNext()&&a){this.nextChar=t.next();r=ilib.data.norm.ccc[this.nextChar];var o=IString.toCodePoint(this.nextChar,0);var s=CType._inRange(o,"Mn",ilib.data.ctype_m);var c=CType._inRange(o,"Mc",ilib.data.ctype_m);if(s||c||typeof r!=="undefined"&&r!==0){if(c){this.spacingCombining=true}i+=this.nextChar;this.nextChar=undefined}else{var l=GlyphString._compose(e,this.nextChar);if(n===0&&typeof l!=="undefined"){e=l;i+=this.nextChar;this.nextChar=undefined}else{a=false}}n=r}}return i};this.wasSpacingCombining=function(){return this.spacingCombining}}return new i(this)};GlyphString.prototype.truncate=function(t){var i=this.charIterator();var n="";for(var r=0;r<t-1&&i.hasNext();r++){n+=i.next()}if(r<t&&i.hasNext()){var e=i.next();if(!i.wasSpacingCombining()){n+=e}}return n};GlyphString.prototype.ellipsize=function(t){return this.truncate(t>0?t-1:0)+"…"};module.exports=GlyphString;