You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GPOSTestCase.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.complexscripts.fonts;
  19. import java.io.File;
  20. import org.junit.Test;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertNotNull;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.junit.Assert.fail;
  25. import org.apache.fop.complexscripts.fonts.GlyphTable.LookupTable;
  26. import org.apache.fop.complexscripts.fonts.ttx.TTXFile;
  27. import org.apache.fop.complexscripts.util.GlyphContextTester;
  28. import org.apache.fop.complexscripts.util.GlyphSequence;
  29. import org.apache.fop.complexscripts.util.ScriptContextTester;
  30. // CSOFF: LineLength
  31. public class GPOSTestCase implements ScriptContextTester, GlyphContextTester {
  32. private static String ttxFilesRoot = "test/resources/complexscripts";
  33. private static String[][] ttxFonts = {
  34. { "f0", "arab/ttx/arab-001.ttx" }, // simplified arabic
  35. { "f1", "arab/ttx/arab-002.ttx" }, // traditional arabic
  36. { "f2", "arab/ttx/arab-003.ttx" }, // lateef
  37. { "f3", "arab/ttx/arab-004.ttx" }, // scheherazade
  38. };
  39. private static Object[][] ltSingle = {
  40. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_SINGLE },
  41. // arab-001.ttx
  42. { "f0", "lu1", "arab", "dflt", "mark",
  43. new Object[][] {
  44. {
  45. new String[] { "fathatan" },
  46. new int[][] {
  47. { 0, 0, -412, 0 }
  48. }
  49. },
  50. {
  51. new String[] { "fatha" },
  52. new int[][] {
  53. { 0, 0, -410, 0 }
  54. }
  55. },
  56. },
  57. },
  58. { "f0", "lu9", "arab", "*", "*",
  59. new Object[][] {
  60. {
  61. new String[] { "fathatan" },
  62. new int[][] {
  63. { 50, 0, 0, 0 }
  64. }
  65. },
  66. {
  67. new String[] { "fatha" },
  68. new int[][] {
  69. { 50, 0, 0, 0 }
  70. }
  71. },
  72. },
  73. },
  74. { "f0", "lu10", "arab", "*", "*",
  75. new Object[][] {
  76. {
  77. new String[] { "kasratan" },
  78. new int[][] {
  79. { 0, -200, 0, 0 }
  80. }
  81. },
  82. {
  83. new String[] { "kasra" },
  84. new int[][] {
  85. { 0, -200, 0, 0 }
  86. }
  87. },
  88. },
  89. },
  90. { "f0", "lu11", "arab", "*", "*",
  91. new Object[][] {
  92. {
  93. new String[] { "kasratan" },
  94. new int[][] {
  95. { 0, -300, 0, 0 }
  96. }
  97. },
  98. {
  99. new String[] { "kasra" },
  100. new int[][] {
  101. { 0, -300, 0, 0 }
  102. }
  103. },
  104. {
  105. new String[] { "uni0655" },
  106. new int[][] {
  107. { 0, -250, 0, 0 }
  108. }
  109. },
  110. },
  111. },
  112. // arab-002.ttx - maybe add tests
  113. // arab-003.ttx - maybe add tests
  114. // arab-004.ttx - maybe add tests
  115. };
  116. private static Object[][] ltPair = {
  117. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_PAIR },
  118. // arab-001.ttx
  119. { "f0", "lu0", "arab", "dflt", "kern",
  120. new Object[][] {
  121. {
  122. new String[] { "wawwithhamzaabove", "hamza" },
  123. new int[][] {
  124. { -300, 0, -300, 0 }, { 0, 0, 0, 0 }
  125. }
  126. },
  127. {
  128. new String[] { "reh", "alefwithmaddaabove" },
  129. new int[][] {
  130. { -500, 0, -500, 0 }, { 0, 0, 0, 0 }
  131. }
  132. },
  133. {
  134. new String[] { "zain", "zain" },
  135. new int[][] {
  136. { -190, 0, -190, 0 }, { 0, 0, 0, 0 }
  137. }
  138. },
  139. {
  140. new String[] { "waw", "uni0649.init" },
  141. new int[][] {
  142. { -145, 0, -145, 0 }, { 0, 0, 0, 0 }
  143. }
  144. },
  145. {
  146. new String[] { "jeh", "uni06A5.init" },
  147. new int[][] {
  148. { -345, 0, -345, 0 }, { 0, 0, 0, 0 }
  149. }
  150. },
  151. },
  152. },
  153. // arab-002.ttx - maybe add tests
  154. // arab-003.ttx - maybe add tests
  155. // arab-004.ttx - maybe add tests
  156. };
  157. private static Object[][] ltCursive = {
  158. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_CURSIVE },
  159. // arab-001.ttx - none used
  160. // arab-002.ttx - none used
  161. // arab-003.ttx - maybe add tests
  162. { "f2", "lu0", "arab", "dflt", "curs",
  163. new Object[][] {
  164. {
  165. new String[] { "uni0644.init.preAlef", "uni0622.fina.postLamIni" },
  166. new int[][] {
  167. // { 576, 0, 0, 0 }, { 0, 0, 0, 0 } - with zero widths
  168. { 295, 0, 0, 0 }, { 0, 0, 0, 0 }
  169. }
  170. },
  171. {
  172. new String[] { "uni0644.medi.preAlef", "uni0622.fina.postLamMed" },
  173. new int[][] {
  174. // { 550, 0, 0, 0 }, { 0, 0, 0, 0 } - with zero widths
  175. { 282, 0, 0, 0 }, { 0, 0, 0, 0 }
  176. }
  177. },
  178. },
  179. },
  180. // arab-004.ttx - none used
  181. };
  182. private static Object[][] ltMarkToBase = {
  183. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_BASE },
  184. // arab-001.ttx - maybe add tests
  185. // arab-002.ttx
  186. { "f1", "lu4", "arab", "dflt", "mark",
  187. new Object[][] {
  188. {
  189. new String[] { "beh", "fatha" },
  190. new int[][] {
  191. // { 0, 0, 0, 0 }, { 266, -672, 0, 0 } - with zero widths
  192. { 0, 0, 0, 0 }, { 266, -672, -199, 0 }
  193. }
  194. },
  195. {
  196. new String[] { "alefwithhamzabelow", "kasra" },
  197. new int[][] {
  198. // { 0, 0, 0, 0 }, { -48, 344, 0, 0 } - with zero widths
  199. { 0, 0, 0, 0 }, { -48, 344, -199, 0 }
  200. }
  201. },
  202. },
  203. },
  204. // arab-003.ttx - maybe add tests
  205. // arab-004.ttx - maybe add tests
  206. };
  207. private static Object[][] ltMarkToLigature = {
  208. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_LIGATURE },
  209. // arab-001.ttx
  210. { "f0", "lu4", "arab", "dflt", "mark",
  211. new Object[][] {
  212. {
  213. new String[] { "rayaleflam", "fatha", "fatha", "fatha", "fatha" },
  214. new int[][] {
  215. { 0, 0, 0, 0 }, { 1260, -1150, 0, 0 }, { 910, -1020, 0, 0 }, { 590, -630, 0, 0 }, { 110, -720, 0, 0 }
  216. }
  217. },
  218. {
  219. new String[] { "rayaleflam", "kasra", "kasra", "kasra", "kasra" },
  220. new int[][] {
  221. { 0, 0, 0, 0 }, { 1110 , 225, 0, 0 }, { 760, 275, 0, 0 }, { 520, 475, 0, 0 }, { 110, 425, 0, 0 }
  222. }
  223. },
  224. },
  225. },
  226. // arab-002.ttx - maybe add tests
  227. // arab-003.ttx - maybe add tests
  228. // arab-004.ttx - maybe add tests
  229. };
  230. private static Object[][] ltMarkToMark = {
  231. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_MARK_TO_MARK },
  232. // arab-001.ttx - maybe add tests
  233. // arab-002.ttx - maybe add tests
  234. // arab-003.ttx - maybe add tests
  235. // arab-004.ttx
  236. { "f3", "lu3", "arab", "dflt", "mkmk",
  237. new Object[][] {
  238. {
  239. new String[] { "uni064F", "uni064E" },
  240. new int[][] {
  241. { 0, 0, 0, 0 }, { -15, 495, 0, 0 }
  242. }
  243. },
  244. {
  245. new String[] { "uni0651", "uni0670" },
  246. new int[][] {
  247. { 0, 0, 0, 0 }, { -30, 705, 0, 0 }
  248. }
  249. },
  250. },
  251. },
  252. };
  253. private static Object[][] ltContextual = {
  254. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_CONTEXTUAL },
  255. // arab-001.ttx - none used
  256. // arab-002.ttx - none used
  257. // arab-003.ttx - none used
  258. // arab-004.ttx - none used
  259. };
  260. private static Object[][] ltChainedContextual = {
  261. { GlyphPositioningTable.GPOS_LOOKUP_TYPE_CHAINED_CONTEXTUAL },
  262. // arab-001.ttx
  263. { "f0", "lu3", "arab", "dflt", "mark",
  264. new Object[][] {
  265. {
  266. new String[] { "behmedial", "fatha", "lam" },
  267. new int[][] {
  268. { 0, 0, 0, 0 }, { 50, 0, 0, 0 }, { 0, 0, 0, 0 }
  269. }
  270. },
  271. },
  272. },
  273. // arab-002.ttx
  274. { "f1", "lu6", "arab", "dflt", "mark",
  275. new Object[][] {
  276. {
  277. new String[] { "zain", "fatha", "kafinitial" },
  278. new int[][] {
  279. { 0, 250, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
  280. }
  281. },
  282. },
  283. },
  284. // arab-003.ttx - none used
  285. // arab-004.ttx
  286. { "f3", "lu5", "arab", "dflt", "mark",
  287. new Object[][] {
  288. {
  289. new String[] { "uni064D", "uni0622.fina.postLamIni", "uni0650" },
  290. new int[][] {
  291. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 55, 424, 0, 0 }
  292. }
  293. },
  294. },
  295. },
  296. };
  297. @Test
  298. public void testGPOSSingle() throws Exception {
  299. performPositioning(ltSingle);
  300. }
  301. @Test
  302. public void testGPOSPair() throws Exception {
  303. performPositioning(ltPair);
  304. }
  305. @Test
  306. public void testGPOSCursive() throws Exception {
  307. performPositioning(ltCursive);
  308. }
  309. @Test
  310. public void testGPOSMarkToBase() throws Exception {
  311. performPositioning(ltMarkToBase);
  312. }
  313. @Test
  314. public void testGPOSMarkToLigature() throws Exception {
  315. performPositioning(ltMarkToLigature);
  316. }
  317. @Test
  318. public void testGPOSMarkToMark() throws Exception {
  319. performPositioning(ltMarkToMark);
  320. }
  321. @Test
  322. public void testGPOSContextual() throws Exception {
  323. performPositioning(ltContextual);
  324. }
  325. @Test
  326. public void testGPOSChainedContextual() throws Exception {
  327. performPositioning(ltChainedContextual);
  328. }
  329. /**
  330. * Perform positioning on all test data in test specification TS.
  331. * @param ts test specification
  332. */
  333. private void performPositioning(Object[][] ts) {
  334. assert ts.length > 0;
  335. Object[] tp = ts[0];
  336. for (int i = 1; i < ts.length; i++) {
  337. performPositioning(tp, ts[i]);
  338. }
  339. }
  340. /**
  341. * Perform positioning on all test data TD using test parameters TP.
  342. * @param tp test parameters
  343. * @param td test data
  344. */
  345. private void performPositioning(Object[] tp, Object[] td) {
  346. assert tp.length > 0;
  347. if (td.length > 5) {
  348. String fid = (String) td[0];
  349. String lid = (String) td[1];
  350. String script = (String) td[2];
  351. String language = (String) td[3];
  352. String feature = (String) td[4];
  353. TTXFile tf = findTTX(fid);
  354. assertTrue(tf != null);
  355. GlyphPositioningTable gpos = tf.getGPOS();
  356. assertTrue(gpos != null);
  357. GlyphPositioningSubtable[] sta = findGPOSSubtables(gpos, script, language, feature, lid);
  358. assertTrue(sta != null);
  359. assertTrue(sta.length > 0);
  360. ScriptContextTester sct = findScriptContextTester(script, language, feature);
  361. Object[][] tia = (Object[][]) td[5]; // test instance array
  362. for (Object[] ti : tia) { // test instance
  363. if (ti != null) {
  364. if (ti.length > 0) { // must have at least input glyphs
  365. String[] igia = (String[]) ti[0]; // input glyph id array
  366. int[][] ogpa = (int[][]) ti[1]; // output glyph positioning array
  367. GlyphSequence igs = tf.getGlyphSequence(igia);
  368. int[] widths = tf.getWidths();
  369. int[][] tgpa = new int [ igia.length ] [ 4 ];
  370. boolean adjusted = GlyphPositioningSubtable.position(igs, script, language, feature, 1000, sta, widths, tgpa, sct);
  371. assertTrue(adjusted);
  372. assertSamePositions(ogpa, tgpa);
  373. }
  374. }
  375. }
  376. }
  377. }
  378. private String findTTXPath(String fid) {
  379. for (String[] fs : ttxFonts) {
  380. if ((fs != null) && (fs.length > 1)) {
  381. if (fs[0].equals(fid)) {
  382. return ttxFilesRoot + File.separator + fs[1];
  383. }
  384. }
  385. }
  386. return null;
  387. }
  388. private TTXFile findTTX(String fid) {
  389. String pn = findTTXPath(fid);
  390. assertTrue(pn != null);
  391. try {
  392. TTXFile tf = TTXFile.getFromCache(pn);
  393. return tf;
  394. } catch (Exception e) {
  395. fail(e.getMessage());
  396. return null;
  397. }
  398. }
  399. private GlyphPositioningSubtable[] findGPOSSubtables(GlyphPositioningTable gpos, String script, String language, String feature, String lid) {
  400. LookupTable lt = gpos.getLookupTable(lid);
  401. if (lt != null) {
  402. return (GlyphPositioningSubtable[]) lt.getSubtables();
  403. } else {
  404. return null;
  405. }
  406. }
  407. private ScriptContextTester findScriptContextTester(String script, String language, String feature) {
  408. return this;
  409. }
  410. public GlyphContextTester getTester(String feature) {
  411. return this;
  412. }
  413. public boolean test(String script, String language, String feature, GlyphSequence gs, int index, int flags) {
  414. return true;
  415. }
  416. private void assertSamePositions(int[][] pa1, int[][] pa2) {
  417. assertNotNull(pa1);
  418. assertNotNull(pa2);
  419. assertEquals("unequal adjustment count", pa1.length, pa2.length);
  420. for (int i = 0; i < pa1.length; i++) {
  421. int[] a1 = pa1 [ i ];
  422. int[] a2 = pa2 [ i ];
  423. assertNotNull(a1);
  424. assertNotNull(a2);
  425. assertEquals("bad adjustment array length", 4, a1.length);
  426. assertEquals("bad adjustment array length", 4, a2.length);
  427. for (int k = 0; k < a1.length; k++) {
  428. int p1 = a1[k];
  429. int p2 = a2[k];
  430. assertEquals("bad adjustment", p1, p2);
  431. }
  432. }
  433. }
  434. }