Code coverage report for fontkit/src/layout/Script.coffee

Statements: 59.38% (19 / 32)      Branches: 50% (5 / 10)      Functions: 50% (2 / 4)      Lines: 65.52% (19 / 29)      Ignored: none     

All files » fontkit/src/layout/ » Script.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 2021         1                                                                                                                                                                                                                                                                 1     1 23 23 23 34     34               34 34 22   1   1                 1                                                         1 55 12   43  
unicode = require 'unicode-properties'
 
# This maps the Unicode Script property to an OpenType script tag
# Data from http://www.microsoft.com/typography/otspec/scripttags.htm
# and http://www.unicode.org/Public/UNIDATA/PropertyValueAliases.txt.
UNICODE_SCRIPTS = 
  Caucasian_Albanian: 'aghb'
  Arabic: 'arab'
  Imperial_Aramaic: 'armi'
  Armenian: 'armn'
  Avestan: 'avst'
  Balinese: 'bali'
  Bamum: 'bamu'
  Bassa_Vah: 'bass'
  Batak: 'batk'
  Bengali: ['bng2', 'beng']
  Bopomofo: 'bopo'
  Brahmi: 'brah'
  Braille: 'brai'
  Buginese: 'bugi'
  Buhid: 'buhd'
  Chakma: 'cakm'
  Canadian_Aboriginal: 'cans'
  Carian: 'cari'
  Cham: 'cham'
  Cherokee: 'cher'
  Coptic: 'copt'
  Cypriot: 'cprt'
  Cyrillic: 'cyrl'
  Devanagari: ['dev2', 'deva']
  Deseret: 'dsrt'
  Duployan: 'dupl'
  Egyptian_Hieroglyphs: 'egyp'
  Elbasan: 'elba'
  Ethiopic: 'ethi'
  Georgian: 'geor'
  Glagolitic: 'glag'
  Gothic: 'goth'
  Grantha: 'gran'
  Greek: 'grek'
  Gujarati: ['gjr2', 'gujr']
  Gurmukhi: ['gur2', 'guru']
  Hangul: 'hang'
  Han: 'hani'
  Hanunoo: 'hano'
  Hebrew: 'hebr'
  Hiragana: 'hira'
  Pahawh_Hmong: 'hmng'
  Katakana_Or_Hiragana: 'hrkt'
  Old_Italic: 'ital'
  Javanese: 'java'
  Kayah_Li: 'kali'
  Katakana: 'kana'
  Kharoshthi: 'khar'
  Khmer: 'khmr'
  Khojki: 'khoj'
  Kannada: ['knd2', 'knda']
  Kaithi: 'kthi'
  Tai_Tham: 'lana'
  Lao: 'lao '
  Latin: 'latn'
  Lepcha: 'lepc'
  Limbu: 'limb'
  Linear_A: 'lina'
  Linear_B: 'linb'
  Lisu: 'lisu'
  Lycian: 'lyci'
  Lydian: 'lydi'
  Mahajani: 'mahj'
  Mandaic: 'mand'
  Manichaean: 'mani'
  Mende_Kikakui: 'mend'
  Meroitic_Cursive: 'merc'
  Meroitic_Hieroglyphs: 'mero'
  Malayalam: ['mlm2', 'mlym']
  Modi: 'modi'
  Mongolian: 'mong'
  Mro: 'mroo'
  Meetei_Mayek: 'mtei'
  Myanmar: ['mym2', 'mymr']
  Old_North_Arabian: 'narb'
  Nabataean: 'nbat'
  Nko: 'nko '
  Ogham: 'ogam'
  Ol_Chiki: 'olck'
  Old_Turkic: 'orkh'
  Oriya: 'orya'
  Osmanya: 'osma'
  Palmyrene: 'palm'
  Pau_Cin_Hau: 'pauc'
  Old_Permic: 'perm'
  Phags_Pa: 'phag'
  Inscriptional_Pahlavi: 'phli'
  Psalter_Pahlavi: 'phlp'
  Phoenician: 'phnx'
  Miao: 'plrd'
  Inscriptional_Parthian: 'prti'
  Rejang: 'rjng'
  Runic: 'runr'
  Samaritan: 'samr'
  Old_South_Arabian: 'sarb'
  Saurashtra: 'saur'
  Shavian: 'shaw'
  Sharada: 'shrd'
  Siddham: 'sidd'
  Khudawadi: 'sind'
  Sinhala: 'sinh'
  Sora_Sompeng: 'sora'
  Sundanese: 'sund'
  Syloti_Nagri: 'sylo'
  Syriac: 'syrc'
  Tagbanwa: 'tagb'
  Syloti_Nagri: 'takr'
  Tai_Le: 'tale'
  New_Tai_Lue: 'talu'
  Tamil: 'taml'
  Tai_Viet: 'tavt'
  Telugu: ['tel2', 'telu']
  Tifinagh: 'tfng'
  Tagalog: 'tglg'
  Thaana: 'thaa'
  Thai: 'thai'
  Tibetan: 'tibt'
  Tirhuta: 'tirh'
  Ugaritic: 'ugar'
  Vai: 'vai '
  Warang_Citi: 'wara'
  Old_Persian: 'xpeo'
  Cuneiform: 'xsux'
  Yi: 'yi  '
  Inherited: 'zinh'
  Common: 'zyyy'
  Unknown: 'zzzz'
  
exports.fromUnicode = (script) ->
  return UNICODE_SCRIPTS[script]
  
exports.forString = (string) ->
  len = string.length
  idx = 0
  while idx < len
    code = string.charCodeAt idx++
    
    # Check if this is a high surrogate
    Iif 0xd800 <= code <= 0xdbff and idx < len
      next = string.charCodeAt idx
      
      # Check if this is a low surrogate
      if 0xdc00 <= next <= 0xdfff
        idx++
        code = ((code & 0x3FF) << 10) + (next & 0x3FF) + 0x10000
        
    script = unicode.getScript code
    unless script in ['Common', 'Inherited', 'Unknown']
      return UNICODE_SCRIPTS[script]
      
  return UNICODE_SCRIPTS.Unknown
  
exports.forCodePoints = (codePoints) ->
  for codePoint in codePoints
    script = unicode.getScript codePoint
    unless script in ['Common', 'Inherited', 'Unknown']
      return UNICODE_SCRIPTS[script]
    
  return UNICODE_SCRIPTS.Unknown
  
# The scripts in this map are written from right to left
RTL =
  arab: true # Arabic
  hebr: true # Hebrew
  syrc: true # Syriac
  thaa: true # Thaana
  cprt: true # Cypriot Syllabary
  khar: true # Kharosthi
  phnx: true # Phoenician
  'nko ': true # N'Ko
  lydi: true # Lydian
  avst: true # Avestan
  armi: true # Imperial Aramaic
  phli: true # Inscriptional Pahlavi
  prti: true # Inscriptional Parthian
  sarb: true # Old South Arabian
  orkh: true # Old Turkic, Orkhon Runic
  samr: true # Samaritan
  mand: true # Mandaic, Mandaean
  merc: true # Meroitic Cursive
  mero: true # Meroitic Hieroglyphs
 
  # Unicode 7.0 (not listed on http://www.microsoft.com/typography/otspec/scripttags.htm)
  mani: true # Manichaean
  mend: true # Mende Kikakui
  nbat: true # Nabataean
  narb: true # Old North Arabian
  palm: true # Palmyrene
  phlp: true # Psalter Pahlavi
 
exports.direction = (script) ->
  if RTL[script]
    return 'rtl'
    
  return 'ltr'