1# -*- coding: utf-8 -*- 2# ==================================================================== 3# Copyright (c) 2011-2011 Open Source Applications Foundation. 4# 5# Permission is hereby granted, free of charge, to any person obtaining a 6# copy of this software and associated documentation files (the "Software"), 7# to deal in the Software without restriction, including without limitation 8# the rights to use, copy, modify, merge, publish, distribute, sublicense, 9# and/or sell copies of the Software, and to permit persons to whom the 10# Software is furnished to do so, subject to the following conditions: 11# 12# The above copyright notice and this permission notice shall be included 13# in all copies or substantial portions of the Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21# DEALINGS IN THE SOFTWARE. 22# ==================================================================== 23# 24 25import sys, os, six 26 27from unittest import TestCase, main 28from icu import * 29 30try: 31 if ICU_VERSION >= '58': 32 raise NotImplementedError 33 from fontTools.ttLib import TTFont 34except ImportError as e: 35 sys.stderr.write("\nfontTools package not found, skipping LayoutEngine tests\n") 36except NotImplementedError: 37 sys.stderr.write("\nLayoutEngine not available in ICU %s" %(ICU_VERSION)) 38else: 39 class TTXLEFont(LEFontInstance): 40 41 def __init__(self, fname, size=12): 42 super(TTXLEFont, self).__init__() 43 44 self.ttx = TTFont(fname) 45 self.size = size 46 self.upem = self.ttx['head'].unitsPerEm 47 self.cmap = self.ttx['cmap'].getcmap(3, 1).cmap 48 49 def getFontTable(self, table): 50 return self.ttx.getTableData(table) 51 52 def getAscent(self): 53 self.ttx['hhea'].ascent * self.size * 1. / self.upem 54 55 def getDescent(self): 56 self.ttx['hhea'].descent * self.size * 1. / self.upem 57 58 def getLeading(self): 59 self.ttx['hhea'].lineGap * self.size * 1. / self.upem 60 61 def getUnitsPerEm(self): 62 return self.upem 63 64 def mapCharToGlyph(self, code): 65 return self.ttx.getGlyphID(self.cmap[code]) 66 67 def getGlyphAdvance(self, glyph): 68 69 if glyph >= self.ttx['maxp'].numGlyphs: 70 return (0., 0.) 71 72 name = self.ttx.getGlyphName(glyph) 73 x = self.ttx['hmtx'][name][0] * self.size * 1. / self.upem 74 75 if 'vmtx' in self.ttx: 76 y = self.ttx['vmtx'][name][0] * self.size * 1. / self.upem 77 else: 78 y = 0. 79 80 return (x, y) 81 82 def getGlyphPoint(self, glyph, point): 83 return (0., 0.) 84 85 def getXPixelsPerEm(self): 86 return self.size 87 88 def getYPixelsPerEm(self): 89 return self.size 90 91 def getScaleFactorX(self): 92 return 1. 93 94 def getScaleFactorY(self): 95 return 1. 96 97 98try: 99 if ICU_VERSION >= '58': 100 raise NotImplementedError 101 import fontTools 102except ImportError: 103 pass 104except NotImplementedError: 105 pass 106else: 107 class TestLayoutEngine(TestCase): 108 109 def filePath(self, name): 110 111 module = sys.modules[TestLayoutEngine.__module__].__file__ 112 return os.path.join(os.path.dirname(module), name) 113 114 def setUp(self): 115 116 self.font = TTXLEFont(self.filePath("lohit_hi.ttf")) 117 self.layout = LayoutEngine.layoutEngineFactory(self.font, 118 ScriptCode.deva, 119 LanguageCode.nul) 120 121 def testHello(self): 122 123 self.layout.layoutChars(u"नमस्ते दुनिया") 124 self.assertEqual(self.layout.getGlyphCount(), 13) 125 126 127if __name__ == "__main__": 128 try: 129 if ICU_VERSION >= '58': 130 raise NotImplementedError 131 import fontTools 132 except ImportError: 133 pass 134 except NotImplementedError: 135 pass 136 else: 137 main() 138