PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/OOO330_m20/vcl/source/fontsubset/sft.cxx

https://bitbucket.org/tora/ooo-enum-attribute-packed-experiment-ooo330_m20-vcl
C++ | 3359 lines | 2601 code | 513 blank | 245 comment | 620 complexity | 27d2a58efa1b623d0f7f56963a234b5b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * Copyright 2000, 2010 Oracle and/or its affiliates.
  6. *
  7. * OpenOffice.org - a multi-platform office productivity suite
  8. *
  9. * This file is part of OpenOffice.org.
  10. *
  11. * OpenOffice.org is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License version 3
  13. * only, as published by the Free Software Foundation.
  14. *
  15. * OpenOffice.org is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License version 3 for more details
  19. * (a copy is included in the LICENSE file that accompanied this code).
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * version 3 along with OpenOffice.org. If not, see
  23. * <http://www.openoffice.org/license.html>
  24. * for a copy of the LGPLv3 License.
  25. *
  26. ************************************************************************/
  27. // MARKER(update_precomp.py): autogen include statement, do not remove
  28. #include "precompiled_vcl.hxx"
  29. /*
  30. * Sun Font Tools
  31. *
  32. * Author: Alexander Gelfenbain
  33. *
  34. */
  35. #if OSL_DEBUG_LEVEL == 0
  36. # ifndef NDEBUG
  37. # define NDEBUG
  38. # endif
  39. #endif
  40. #include <assert.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <fcntl.h>
  44. #ifdef UNX
  45. #include <sys/mman.h>
  46. #include <sys/stat.h>
  47. #endif
  48. #include "sft.hxx"
  49. #include "gsub.h"
  50. #if ! (defined(NO_TTCR) && defined(NO_TYPE42))
  51. #include "ttcr.hxx"
  52. #endif
  53. #ifndef NO_MAPPERS /* include MapChar() and MapString() */
  54. #include "xlat.hxx"
  55. #endif
  56. #ifndef NO_TYPE3 /* include CreateT3FromTTGlyphs() */
  57. #include <rtl/crc.h>
  58. #endif
  59. #include <osl/endian.h>
  60. #include <algorithm>
  61. #ifdef TEST7
  62. #include <ctype.h>
  63. #endif
  64. namespace vcl
  65. {
  66. /*- module identification */
  67. static const char *modname = "SunTypeTools-TT";
  68. static const char *modver = "1.0";
  69. static const char *modextra = "gelf";
  70. /*- private functions, constants and data types */ /*FOLD00*/
  71. enum PathSegmentType {
  72. PS_NOOP = 0,
  73. PS_MOVETO = 1,
  74. PS_LINETO = 2,
  75. PS_CURVETO = 3,
  76. PS_CLOSEPATH = 4
  77. };
  78. struct PSPathElement
  79. {
  80. PathSegmentType type;
  81. int x1, y1;
  82. int x2, y2;
  83. int x3, y3;
  84. PSPathElement( PathSegmentType i_eType ) : type( i_eType ),
  85. x1( 0 ), y1( 0 ),
  86. x2( 0 ), y2( 0 ),
  87. x3( 0 ), y3( 0 )
  88. {
  89. }
  90. };
  91. /*- In horisontal writing mode right sidebearing is calculated using this formula
  92. *- rsb = aw - (lsb + xMax - xMin) -*/
  93. typedef struct {
  94. sal_Int16 xMin;
  95. sal_Int16 yMin;
  96. sal_Int16 xMax;
  97. sal_Int16 yMax;
  98. sal_uInt16 aw; /*- Advance Width (horisontal writing mode) */
  99. sal_Int16 lsb; /*- Left sidebearing (horisontal writing mode) */
  100. sal_uInt16 ah; /*- advance height (vertical writing mode) */
  101. sal_Int16 tsb; /*- top sidebearing (vertical writing mode) */
  102. } TTGlyphMetrics;
  103. #define HFORMAT_LINELEN 64
  104. typedef struct {
  105. FILE *o;
  106. char buffer[HFORMAT_LINELEN];
  107. int bufpos;
  108. int total;
  109. } HexFmt;
  110. typedef struct {
  111. sal_uInt32 nGlyphs; /* number of glyphs in the font + 1 */
  112. sal_uInt32 *offs; /* array of nGlyphs offsets */
  113. } GlyphOffsets;
  114. /* private tags */
  115. static const sal_uInt32 TTFontClassTag = 0x74746663; /* 'ttfc' */
  116. static const sal_uInt32 T_true = 0x74727565; /* 'true' */
  117. static const sal_uInt32 T_ttcf = 0x74746366; /* 'ttcf' */
  118. static const sal_uInt32 T_otto = 0x4f54544f; /* 'OTTO' */
  119. /* standard TrueType table tags */
  120. #define T_maxp 0x6D617870
  121. #define T_glyf 0x676C7966
  122. #define T_head 0x68656164
  123. #define T_loca 0x6C6F6361
  124. #define T_name 0x6E616D65
  125. #define T_hhea 0x68686561
  126. #define T_hmtx 0x686D7478
  127. #define T_cmap 0x636D6170
  128. #define T_vhea 0x76686561
  129. #define T_vmtx 0x766D7478
  130. #define T_OS2 0x4F532F32
  131. #define T_post 0x706F7374
  132. #define T_kern 0x6B65726E
  133. #define T_cvt 0x63767420
  134. #define T_prep 0x70726570
  135. #define T_fpgm 0x6670676D
  136. #define T_gsub 0x47535542
  137. #define T_CFF 0x43464620
  138. #define LAST_URANGE_BIT 69
  139. const char *ulcodes[LAST_URANGE_BIT+2] = {
  140. /* 0 */ "Basic Latin",
  141. /* 1 */ "Latin-1 Supplement",
  142. /* 2 */ "Latin Extended-A",
  143. /* 3 */ "Latin Extended-B",
  144. /* 4 */ "IPA Extensions",
  145. /* 5 */ "Spacing Modifier Letters",
  146. /* 6 */ "Combining Diacritical Marks",
  147. /* 7 */ "Basic Greek",
  148. /* 8 */ "Greek Symbols And Coptic",
  149. /* 9 */ "Cyrillic",
  150. /* 10 */ "Armenian",
  151. /* 11 */ "Basic Hebrew",
  152. /* 12 */ "Hebrew Extended (A and B blocks combined)",
  153. /* 13 */ "Basic Arabic",
  154. /* 14 */ "Arabic Extended",
  155. /* 15 */ "Devanagari",
  156. /* 16 */ "Bengali",
  157. /* 17 */ "Gurmukhi",
  158. /* 18 */ "Gujarati",
  159. /* 19 */ "Oriya",
  160. /* 20 */ "Tamil",
  161. /* 21 */ "Telugu",
  162. /* 22 */ "Kannada",
  163. /* 23 */ "Malayalam",
  164. /* 24 */ "Thai",
  165. /* 25 */ "Lao",
  166. /* 26 */ "Basic Georgian",
  167. /* 27 */ "Georgian Extended",
  168. /* 28 */ "Hangul Jamo",
  169. /* 29 */ "Latin Extended Additional",
  170. /* 30 */ "Greek Extended",
  171. /* 31 */ "General Punctuation",
  172. /* 32 */ "Superscripts And Subscripts",
  173. /* 33 */ "Currency Symbols",
  174. /* 34 */ "Combining Diacritical Marks For Symbols",
  175. /* 35 */ "Letterlike Symbols",
  176. /* 36 */ "Number Forms",
  177. /* 37 */ "Arrows",
  178. /* 38 */ "Mathematical Operators",
  179. /* 39 */ "Miscellaneous Technical",
  180. /* 40 */ "Control Pictures",
  181. /* 41 */ "Optical Character Recognition",
  182. /* 42 */ "Enclosed Alphanumerics",
  183. /* 43 */ "Box Drawing",
  184. /* 44 */ "Block Elements",
  185. /* 45 */ "Geometric Shapes",
  186. /* 46 */ "Miscellaneous Symbols",
  187. /* 47 */ "Dingbats",
  188. /* 48 */ "CJK Symbols And Punctuation",
  189. /* 49 */ "Hiragana",
  190. /* 50 */ "Katakana",
  191. /* 51 */ "Bopomofo",
  192. /* 52 */ "Hangul Compatibility Jamo",
  193. /* 53 */ "CJK Miscellaneous",
  194. /* 54 */ "Enclosed CJK Letters And Months",
  195. /* 55 */ "CJK Compatibility",
  196. /* 56 */ "Hangul",
  197. /* 57 */ "Reserved for Unicode SubRanges",
  198. /* 58 */ "Reserved for Unicode SubRanges",
  199. /* 59 */ "CJK Unified Ideographs",
  200. /* 60 */ "Private Use Area",
  201. /* 61 */ "CJK Compatibility Ideographs",
  202. /* 62 */ "Alphabetic Presentation Forms",
  203. /* 63 */ "Arabic Presentation Forms-A",
  204. /* 64 */ "Combining Half Marks",
  205. /* 65 */ "CJK Compatibility Forms",
  206. /* 66 */ "Small Form Variants",
  207. /* 67 */ "Arabic Presentation Forms-B",
  208. /* 68 */ "Halfwidth And Fullwidth Forms",
  209. /* 69 */ "Specials",
  210. /*70-127*/ "Reserved for Unicode SubRanges"
  211. };
  212. /*- inline functions */ /*FOLD01*/
  213. #ifdef __GNUC__
  214. #define _inline static __inline__
  215. #else
  216. #define _inline static
  217. #endif
  218. _inline void *smalloc(size_t size)
  219. {
  220. void *res = malloc(size);
  221. assert(res != 0);
  222. return res;
  223. }
  224. _inline void *scalloc(size_t n, size_t size)
  225. {
  226. void *res = calloc(n, size);
  227. assert(res != 0);
  228. return res;
  229. }
  230. _inline sal_uInt32 mkTag(sal_uInt8 a, sal_uInt8 b, sal_uInt8 c, sal_uInt8 d) {
  231. return (a << 24) | (b << 16) | (c << 8) | d;
  232. }
  233. /*- Data access macros for data stored in big-endian or little-endian format */
  234. _inline sal_Int16 GetInt16(const sal_uInt8 *ptr, size_t offset, int bigendian)
  235. {
  236. sal_Int16 t;
  237. assert(ptr != 0);
  238. if (bigendian) {
  239. t = (ptr+offset)[0] << 8 | (ptr+offset)[1];
  240. } else {
  241. t = (ptr+offset)[1] << 8 | (ptr+offset)[0];
  242. }
  243. return t;
  244. }
  245. _inline sal_uInt16 GetUInt16(const sal_uInt8 *ptr, size_t offset, int bigendian)
  246. {
  247. sal_uInt16 t;
  248. assert(ptr != 0);
  249. if (bigendian) {
  250. t = (ptr+offset)[0] << 8 | (ptr+offset)[1];
  251. } else {
  252. t = (ptr+offset)[1] << 8 | (ptr+offset)[0];
  253. }
  254. return t;
  255. }
  256. _inline sal_Int32 GetInt32(const sal_uInt8 *ptr, size_t offset, int bigendian)
  257. {
  258. sal_Int32 t;
  259. assert(ptr != 0);
  260. if (bigendian) {
  261. t = (ptr+offset)[0] << 24 | (ptr+offset)[1] << 16 |
  262. (ptr+offset)[2] << 8 | (ptr+offset)[3];
  263. } else {
  264. t = (ptr+offset)[3] << 24 | (ptr+offset)[2] << 16 |
  265. (ptr+offset)[1] << 8 | (ptr+offset)[0];
  266. }
  267. return t;
  268. }
  269. _inline sal_uInt32 GetUInt32(const sal_uInt8 *ptr, size_t offset, int bigendian)
  270. {
  271. sal_uInt32 t;
  272. assert(ptr != 0);
  273. if (bigendian) {
  274. t = (ptr+offset)[0] << 24 | (ptr+offset)[1] << 16 |
  275. (ptr+offset)[2] << 8 | (ptr+offset)[3];
  276. } else {
  277. t = (ptr+offset)[3] << 24 | (ptr+offset)[2] << 16 |
  278. (ptr+offset)[1] << 8 | (ptr+offset)[0];
  279. }
  280. return t;
  281. }
  282. _inline void PutInt16(sal_Int16 val, sal_uInt8 *ptr, size_t offset, int bigendian)
  283. {
  284. assert(ptr != 0);
  285. if (bigendian) {
  286. ptr[offset] = (sal_uInt8)((val >> 8) & 0xFF);
  287. ptr[offset+1] = (sal_uInt8)(val & 0xFF);
  288. } else {
  289. ptr[offset+1] = (sal_uInt8)((val >> 8) & 0xFF);
  290. ptr[offset] = (sal_uInt8)(val & 0xFF);
  291. }
  292. }
  293. #if defined(OSL_BIGENDIAN)
  294. #define Int16FromMOTA(a) (a)
  295. #define Int32FromMOTA(a) (a)
  296. #else
  297. static sal_uInt16 Int16FromMOTA(sal_uInt16 a) {
  298. return (sal_uInt16) (((sal_uInt8)((a) >> 8)) | ((sal_uInt8)(a) << 8));
  299. }
  300. static sal_uInt32 Int32FromMOTA(sal_uInt32 a) {
  301. return ((a>>24)&0xFF) | (((a>>8)&0xFF00) | ((a&0xFF00)<<8) | ((a&0xFF)<<24));
  302. }
  303. #endif
  304. _inline F16Dot16 fixedMul(F16Dot16 a, F16Dot16 b)
  305. {
  306. unsigned int a1, b1;
  307. unsigned int a2, b2;
  308. F16Dot16 res;
  309. int sign;
  310. sign = (a & 0x80000000) ^ (b & 0x80000000);
  311. if (a < 0) a = -a;
  312. if (b < 0) b = -b;
  313. a1 = a >> 16;
  314. b1 = a & 0xFFFF;
  315. a2 = b >> 16;
  316. b2 = b & 0xFFFF;
  317. res = a1 * a2;
  318. /* if (res > 0x7FFF) assert(!"fixedMul: F16Dot16 overflow"); */
  319. res <<= 16;
  320. res += a1 * b2 + b1 * a2 + ((b1 * b2) >> 16);
  321. return sign ? -res : res;
  322. }
  323. _inline F16Dot16 fixedDiv(F16Dot16 a, F16Dot16 b)
  324. {
  325. unsigned int f, r;
  326. F16Dot16 res;
  327. int sign;
  328. sign = (a & 0x80000000) ^ (b & 0x80000000);
  329. if (a < 0) a = -a;
  330. if (b < 0) b = -b;
  331. f = a / b;
  332. r = a % b;
  333. /* if (f > 0x7FFFF) assert(!"fixedDiv: F16Dot16 overflow"); */
  334. while (r > 0xFFFF) {
  335. r >>= 1;
  336. b >>= 1;
  337. }
  338. res = (f << 16) + (r << 16) / b;
  339. return sign ? -res : res;
  340. }
  341. /*- returns a * b / c -*/
  342. /* XXX provide a real implementation that preserves accuracy */
  343. _inline F16Dot16 fixedMulDiv(F16Dot16 a, F16Dot16 b, F16Dot16 c)
  344. {
  345. F16Dot16 res;
  346. res = fixedMul(a, b);
  347. return fixedDiv(res, c);
  348. }
  349. /*- Translate units from TT to PS (standard 1/1000) -*/
  350. _inline int XUnits(int unitsPerEm, int n)
  351. {
  352. return (n * 1000) / unitsPerEm;
  353. }
  354. _inline const char *UnicodeRangeName(sal_uInt16 bit)
  355. {
  356. if (bit > LAST_URANGE_BIT) bit = LAST_URANGE_BIT+1;
  357. return ulcodes[bit];
  358. }
  359. _inline const sal_uInt8* getTable( TrueTypeFont *ttf, sal_uInt32 ord)
  360. {
  361. return (sal_uInt8*)ttf->tables[ord];
  362. }
  363. _inline sal_uInt32 getTableSize(TrueTypeFont *ttf, sal_uInt32 ord)
  364. {
  365. return ttf->tlens[ord];
  366. }
  367. #ifndef NO_TYPE42
  368. /* Hex Formatter functions */
  369. static char HexChars[] = "0123456789ABCDEF";
  370. static HexFmt *HexFmtNew(FILE *outf)
  371. {
  372. HexFmt* res = (HexFmt*)smalloc(sizeof(HexFmt));
  373. res->bufpos = res->total = 0;
  374. res->o = outf;
  375. return res;
  376. }
  377. static void HexFmtFlush(HexFmt *_this)
  378. {
  379. if (_this->bufpos) {
  380. fwrite(_this->buffer, 1, _this->bufpos, _this->o);
  381. _this->bufpos = 0;
  382. }
  383. }
  384. _inline void HexFmtOpenString(HexFmt *_this)
  385. {
  386. fputs("<\n", _this->o);
  387. }
  388. _inline void HexFmtCloseString(HexFmt *_this)
  389. {
  390. HexFmtFlush(_this);
  391. fputs("00\n>\n", _this->o);
  392. }
  393. _inline void HexFmtDispose(HexFmt *_this)
  394. {
  395. HexFmtFlush(_this);
  396. free(_this);
  397. }
  398. static void HexFmtBlockWrite(HexFmt *_this, const void *ptr, sal_uInt32 size)
  399. {
  400. sal_uInt8 Ch;
  401. sal_uInt32 i;
  402. if (_this->total + size > 65534) {
  403. HexFmtFlush(_this);
  404. HexFmtCloseString(_this);
  405. _this->total = 0;
  406. HexFmtOpenString(_this);
  407. }
  408. for (i=0; i<size; i++) {
  409. Ch = ((sal_uInt8 *) ptr)[i];
  410. _this->buffer[_this->bufpos++] = HexChars[Ch >> 4];
  411. _this->buffer[_this->bufpos++] = HexChars[Ch & 0xF];
  412. if (_this->bufpos == HFORMAT_LINELEN) {
  413. HexFmtFlush(_this);
  414. fputc('\n', _this->o);
  415. }
  416. }
  417. _this->total += size;
  418. }
  419. #endif
  420. /* Outline Extraction functions */ /*FOLD01*/
  421. /* fills the aw and lsb entries of the TTGlyphMetrics structure from hmtx table -*/
  422. static void GetMetrics(TrueTypeFont *ttf, sal_uInt32 glyphID, TTGlyphMetrics *metrics)
  423. {
  424. const sal_uInt8* table = getTable( ttf, O_hmtx );
  425. metrics->aw = metrics->lsb = metrics->ah = metrics->tsb = 0;
  426. if (!table || !ttf->numberOfHMetrics) return;
  427. if (glyphID < ttf->numberOfHMetrics) {
  428. metrics->aw = GetUInt16(table, 4 * glyphID, 1);
  429. metrics->lsb = GetInt16(table, 4 * glyphID + 2, 1);
  430. } else {
  431. metrics->aw = GetUInt16(table, 4 * (ttf->numberOfHMetrics - 1), 1);
  432. metrics->lsb = GetInt16(table + ttf->numberOfHMetrics * 4, (glyphID - ttf->numberOfHMetrics) * 2, 1);
  433. }
  434. table = getTable(ttf, O_vmtx);
  435. if( !table || !ttf->numOfLongVerMetrics )
  436. return;
  437. if (glyphID < ttf->numOfLongVerMetrics) {
  438. metrics->ah = GetUInt16(table, 4 * glyphID, 1);
  439. metrics->tsb = GetInt16(table, 4 * glyphID + 2, 1);
  440. } else {
  441. metrics->ah = GetUInt16(table, 4 * (ttf->numOfLongVerMetrics - 1), 1);
  442. metrics->tsb = GetInt16(table + ttf->numOfLongVerMetrics * 4, (glyphID - ttf->numOfLongVerMetrics) * 2, 1);
  443. }
  444. }
  445. static int GetTTGlyphOutline(TrueTypeFont *, sal_uInt32 , ControlPoint **, TTGlyphMetrics *, std::vector< sal_uInt32 >* );
  446. /* returns the number of control points, allocates the pointArray */
  447. static int GetSimpleTTOutline(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray, TTGlyphMetrics *metrics) /*FOLD02*/
  448. {
  449. const sal_uInt8* table = getTable( ttf, O_glyf );
  450. sal_uInt8 flag, n;
  451. sal_uInt16 t, lastPoint=0;
  452. int i, j, z;
  453. *pointArray = 0;
  454. /* printf("GetSimpleTTOutline(%d)\n", glyphID); */
  455. if( glyphID >= ttf->nglyphs ) /*- glyph is not present in the font */
  456. return 0;
  457. const sal_uInt8* ptr = table + ttf->goffsets[glyphID];
  458. const sal_Int16 numberOfContours = GetInt16(ptr, 0, 1);
  459. if( numberOfContours <= 0 ) /*- glyph is not simple */
  460. return 0;
  461. if (metrics) { /*- GetCompoundTTOutline() calls this function with NULL metrics -*/
  462. metrics->xMin = GetInt16(ptr, 2, 1);
  463. metrics->yMin = GetInt16(ptr, 4, 1);
  464. metrics->xMax = GetInt16(ptr, 6, 1);
  465. metrics->yMax = GetInt16(ptr, 8, 1);
  466. GetMetrics(ttf, glyphID, metrics);
  467. }
  468. /* determine the last point and be extra safe about it. But probably this code is not needed */
  469. for (i=0; i<numberOfContours; i++) {
  470. if ((t = GetUInt16(ptr, 10+i*2, 1)) > lastPoint) lastPoint = t;
  471. }
  472. sal_uInt16 instLen = GetUInt16(ptr, 10 + numberOfContours*2, 1);
  473. const sal_uInt8* p = ptr + 10 + 2 * numberOfContours + 2 + instLen;
  474. ControlPoint* pa = (ControlPoint*)calloc(lastPoint+1, sizeof(ControlPoint));
  475. i = 0;
  476. while (i <= lastPoint) {
  477. pa[i++].flags = (sal_uInt32) (flag = *p++);
  478. if (flag & 8) { /*- repeat flag */
  479. n = *p++;
  480. for (j=0; j<n; j++) {
  481. if (i > lastPoint) { /*- if the font is really broken */
  482. free(pa);
  483. return 0;
  484. }
  485. pa[i++].flags = flag;
  486. }
  487. }
  488. }
  489. /*- Process the X coordinate */
  490. z = 0;
  491. for (i = 0; i <= lastPoint; i++) {
  492. if (pa[i].flags & 0x02) {
  493. if (pa[i].flags & 0x10) {
  494. z += (int) (*p++);
  495. } else {
  496. z -= (int) (*p++);
  497. }
  498. } else if ( !(pa[i].flags & 0x10)) {
  499. z += GetInt16(p, 0, 1);
  500. p += 2;
  501. }
  502. pa[i].x = (sal_Int16)z;
  503. }
  504. /*- Process the Y coordinate */
  505. z = 0;
  506. for (i = 0; i <= lastPoint; i++) {
  507. if (pa[i].flags & 0x04) {
  508. if (pa[i].flags & 0x20) {
  509. z += *p++;
  510. } else {
  511. z -= *p++;
  512. }
  513. } else if ( !(pa[i].flags & 0x20)) {
  514. z += GetInt16(p, 0, 1);
  515. p += 2;
  516. }
  517. pa[i].y = (sal_Int16)z;
  518. }
  519. for (i=0; i<numberOfContours; i++) {
  520. pa[GetUInt16(ptr, 10 + i * 2, 1)].flags |= 0x00008000; /*- set the end contour flag */
  521. }
  522. *pointArray = pa;
  523. return lastPoint + 1;
  524. }
  525. static int GetCompoundTTOutline(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray, TTGlyphMetrics *metrics, std::vector< sal_uInt32 >& glyphlist) /*FOLD02*/
  526. {
  527. sal_uInt16 flags, index;
  528. sal_Int16 e, f, numberOfContours;
  529. const sal_uInt8* table = getTable( ttf, O_glyf );
  530. std::vector<ControlPoint> myPoints;
  531. ControlPoint *nextComponent, *pa;
  532. int i, np;
  533. F16Dot16 a = 0x10000, b = 0, c = 0, d = 0x10000, m, n, abs1, abs2, abs3;
  534. *pointArray = 0;
  535. /* printf("GetCompoundTTOutline(%d)\n", glyphID); */
  536. if (glyphID >= ttf->nglyphs) /*- incorrect glyphID */
  537. return 0;
  538. const sal_uInt8* ptr = table + ttf->goffsets[glyphID];
  539. if ((numberOfContours = GetInt16(ptr, 0, 1)) != -1) /*- glyph is not compound */
  540. return 0;
  541. if (metrics) {
  542. metrics->xMin = GetInt16(ptr, 2, 1);
  543. metrics->yMin = GetInt16(ptr, 4, 1);
  544. metrics->xMax = GetInt16(ptr, 6, 1);
  545. metrics->yMax = GetInt16(ptr, 8, 1);
  546. GetMetrics(ttf, glyphID, metrics);
  547. }
  548. ptr += 10;
  549. do {
  550. flags = GetUInt16(ptr, 0, 1);
  551. /* printf("flags: 0x%X\n", flags); */
  552. index = GetUInt16(ptr, 2, 1);
  553. ptr += 4;
  554. if( std::find( glyphlist.begin(), glyphlist.end(), index ) != glyphlist.end() )
  555. {
  556. #if OSL_DEBUG_LEVEL > 1
  557. fprintf(stderr, "Endless loop found in a compound glyph.\n");
  558. fprintf(stderr, "%d -> ", index);
  559. fprintf(stderr," [");
  560. for( std::vector< sal_uInt32 >::const_iterator it = glyphlist.begin();
  561. it != glyphlist.end(); ++it )
  562. {
  563. fprintf( stderr,"%d ", (int) *it );
  564. }
  565. fprintf(stderr,"]\n");
  566. /**/
  567. #endif
  568. }
  569. glyphlist.push_back( index );
  570. #ifdef DEBUG2
  571. fprintf(stderr,"glyphlist: += %d\n", index);
  572. #endif
  573. if ((np = GetTTGlyphOutline(ttf, index, &nextComponent, 0, &glyphlist)) == 0)
  574. {
  575. /* XXX that probably indicates a corrupted font */
  576. #if OSL_DEBUG_LEVEL > 1
  577. fprintf(stderr, "An empty compound!\n");
  578. /* assert(!"An empty compound"); */
  579. #endif
  580. }
  581. #ifdef DEBUG2
  582. fprintf(stderr,"%d [", (int)glyphlist.size() );
  583. for( std::vector< sal_uInt32 >::const_iterator it = glyphlist.begin();
  584. it != glyphlist.end(); ++it )
  585. {
  586. fprintf( stderr,"%d ", (int) *it );
  587. }
  588. fprintf(stderr, "]\n");
  589. if( ! glyphlist.empty() )
  590. fprintf(stderr, "glyphlist: -= %d\n", (int) glyphlist.back());
  591. #endif
  592. if( ! glyphlist.empty() )
  593. glyphlist.pop_back();
  594. if (flags & USE_MY_METRICS) {
  595. if (metrics) GetMetrics(ttf, index, metrics);
  596. }
  597. if (flags & ARG_1_AND_2_ARE_WORDS) {
  598. e = GetInt16(ptr, 0, 1);
  599. f = GetInt16(ptr, 2, 1);
  600. /* printf("ARG_1_AND_2_ARE_WORDS: %d %d\n", e & 0xFFFF, f & 0xFFFF); */
  601. ptr += 4;
  602. } else {
  603. if (flags & ARGS_ARE_XY_VALUES) { /* args are signed */
  604. e = (sal_Int8) *ptr++;
  605. f = (sal_Int8) *ptr++;
  606. /* printf("ARGS_ARE_XY_VALUES: %d %d\n", e & 0xFF, f & 0xFF); */
  607. } else { /* args are unsigned */
  608. /* printf("!ARGS_ARE_XY_VALUES\n"); */
  609. e = *ptr++;
  610. f = *ptr++;
  611. }
  612. }
  613. a = d = 0x10000;
  614. b = c = 0;
  615. if (flags & WE_HAVE_A_SCALE) {
  616. #ifdef DEBUG2
  617. fprintf(stderr, "WE_HAVE_A_SCALE\n");
  618. #endif
  619. a = GetInt16(ptr, 0, 1) << 2;
  620. d = a;
  621. ptr += 2;
  622. } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
  623. #ifdef DEBUG2
  624. fprintf(stderr, "WE_HAVE_AN_X_AND_Y_SCALE\n");
  625. #endif
  626. a = GetInt16(ptr, 0, 1) << 2;
  627. d = GetInt16(ptr, 2, 1) << 2;
  628. ptr += 4;
  629. } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
  630. #ifdef DEBUG2
  631. fprintf(stderr, "WE_HAVE_A_TWO_BY_TWO\n");
  632. #endif
  633. a = GetInt16(ptr, 0, 1) << 2;
  634. b = GetInt16(ptr, 2, 1) << 2;
  635. c = GetInt16(ptr, 4, 1) << 2;
  636. d = GetInt16(ptr, 6, 1) << 2;
  637. ptr += 8;
  638. }
  639. abs1 = (a < 0) ? -a : a;
  640. abs2 = (b < 0) ? -b : b;
  641. m = (abs1 > abs2) ? abs1 : abs2;
  642. abs3 = abs1 - abs2;
  643. if (abs3 < 0) abs3 = -abs3;
  644. if (abs3 <= 33) m *= 2;
  645. abs1 = (c < 0) ? -c : c;
  646. abs2 = (d < 0) ? -d : d;
  647. n = (abs1 > abs2) ? abs1 : abs2;
  648. abs3 = abs1 - abs2;
  649. if (abs3 < 0) abs3 = -abs3;
  650. if (abs3 <= 33) n *= 2;
  651. if (!ARGS_ARE_XY_VALUES) { /* match the points */
  652. assert(!"ARGS_ARE_XY_VALUES is not implemented!!!\n");
  653. }
  654. #ifdef DEBUG2
  655. fprintf(stderr, "a: %f, b: %f, c: %f, d: %f, e: %f, f: %f, m: %f, n: %f\n",
  656. ((double) a) / 65536,
  657. ((double) b) / 65536,
  658. ((double) c) / 65536,
  659. ((double) d) / 65536,
  660. ((double) e) / 65536,
  661. ((double) f) / 65536,
  662. ((double) m) / 65536,
  663. ((double) n) / 65536);
  664. #endif
  665. for (i=0; i<np; i++) {
  666. F16Dot16 t;
  667. ControlPoint cp;
  668. cp.flags = nextComponent[i].flags;
  669. t = fixedMulDiv(a, nextComponent[i].x << 16, m) + fixedMulDiv(c, nextComponent[i].y << 16, m) + (e << 16);
  670. cp.x = (sal_Int16)(fixedMul(t, m) >> 16);
  671. t = fixedMulDiv(b, nextComponent[i].x << 16, n) + fixedMulDiv(d, nextComponent[i].y << 16, n) + (f << 16);
  672. cp.y = (sal_Int16)(fixedMul(t, n) >> 16);
  673. #ifdef DEBUG2
  674. fprintf(stderr, "( %d %d ) -> ( %d %d )\n", nextComponent[i].x, nextComponent[i].y, cp.x, cp.y);
  675. #endif
  676. myPoints.push_back( cp );
  677. }
  678. free(nextComponent);
  679. } while (flags & MORE_COMPONENTS);
  680. np = myPoints.size();
  681. pa = (ControlPoint*)calloc(np, sizeof(ControlPoint));
  682. assert(pa != 0);
  683. memcpy( pa, &myPoints[0], np*sizeof(ControlPoint) );
  684. *pointArray = pa;
  685. return np;
  686. }
  687. /* NOTE: GetTTGlyphOutline() returns -1 if the glyphID is incorrect,
  688. * but Get{Simple|Compound}GlyphOutline returns 0 in such a case.
  689. *
  690. * NOTE: glyphlist is the stack of glyphs traversed while constructing
  691. * a composite glyph. This is a safequard against endless recursion
  692. * in corrupted fonts.
  693. */
  694. static int GetTTGlyphOutline(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray, TTGlyphMetrics *metrics, std::vector< sal_uInt32 >* glyphlist)
  695. {
  696. const sal_uInt8 *table = getTable( ttf, O_glyf );
  697. sal_Int16 numberOfContours;
  698. int res;
  699. *pointArray = 0;
  700. if (metrics) {
  701. memset(metrics, 0, sizeof(TTGlyphMetrics)); /*- metrics is initialized to all zeroes */
  702. }
  703. if (glyphID >= ttf->nglyphs) return -1; /**/
  704. const sal_uInt8* ptr = table + ttf->goffsets[glyphID];
  705. int length = ttf->goffsets[glyphID+1] - ttf->goffsets[glyphID];
  706. if (length == 0) { /*- empty glyphs still have hmtx and vmtx metrics values */
  707. if (metrics) GetMetrics(ttf, glyphID, metrics);
  708. return 0;
  709. }
  710. numberOfContours = GetInt16(ptr, 0, 1);
  711. if (numberOfContours >= 0)
  712. {
  713. res=GetSimpleTTOutline(ttf, glyphID, pointArray, metrics);
  714. }
  715. else
  716. {
  717. std::vector< sal_uInt32 > aPrivList;
  718. aPrivList.push_back( glyphID );
  719. res = GetCompoundTTOutline(ttf, glyphID, pointArray, metrics, glyphlist ? *glyphlist : aPrivList );
  720. }
  721. #ifdef DEBUG3
  722. {
  723. int i;
  724. FILE *out = fopen("points.dat", "a");
  725. assert(out != 0);
  726. fprintf(out, "Glyph: %d\nPoints: %d\n", glyphID, res);
  727. for (i=0; i<res; i++) {
  728. fprintf(out, "%c ", ((*pointArray)[i].flags & 0x8000) ? 'X' : '.');
  729. fprintf(out, "%c ", ((*pointArray)[i].flags & 1) ? '+' : '-');
  730. fprintf(out, "%d %d\n", (*pointArray)[i].x, (*pointArray)[i].y);
  731. }
  732. fclose(out);
  733. }
  734. #endif
  735. return res;
  736. }
  737. #ifndef NO_TYPE3
  738. /*- returns the number of items in the path -*/
  739. static int BSplineToPSPath(ControlPoint *srcA, int srcCount, PSPathElement **path)
  740. {
  741. std::vector< PSPathElement > aPathList;
  742. int nPathCount = 0;
  743. PSPathElement p( PS_NOOP );
  744. int x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2, y2, curx, cury;
  745. int lastOff = 0; /*- last point was off-contour */
  746. int scflag = 1; /*- start contour flag */
  747. int ecflag = 0; /*- end contour flag */
  748. int cp = 0; /*- current point */
  749. int StartContour = 0, EndContour = 1;
  750. *path = 0;
  751. /* if (srcCount > 0) for(;;) */
  752. while (srcCount > 0) { /*- srcCount does not get changed inside the loop. */
  753. if (scflag) {
  754. int l = cp;
  755. StartContour = cp;
  756. while (!(srcA[l].flags & 0x8000)) l++;
  757. EndContour = l;
  758. if (StartContour == EndContour) {
  759. if (cp + 1 < srcCount) {
  760. cp++;
  761. continue;
  762. } else {
  763. break;
  764. }
  765. }
  766. p = PSPathElement(PS_MOVETO);
  767. if (!(srcA[cp].flags & 1)) {
  768. if (!(srcA[EndContour].flags & 1)) {
  769. p.x1 = x0 = (srcA[cp].x + srcA[EndContour].x + 1) / 2;
  770. p.y1 = y0 = (srcA[cp].y + srcA[EndContour].y + 1) / 2;
  771. } else {
  772. p.x1 = x0 = srcA[EndContour].x;
  773. p.y1 = y0 = srcA[EndContour].y;
  774. }
  775. } else {
  776. p.x1 = x0 = srcA[cp].x;
  777. p.y1 = y0 = srcA[cp].y;
  778. cp++;
  779. }
  780. aPathList.push_back( p );
  781. lastOff = 0;
  782. scflag = 0;
  783. }
  784. curx = srcA[cp].x;
  785. cury = srcA[cp].y;
  786. if (srcA[cp].flags & 1)
  787. {
  788. if (lastOff)
  789. {
  790. p = PSPathElement(PS_CURVETO);
  791. p.x1 = x0 + (2 * (x1 - x0) + 1) / 3;
  792. p.y1 = y0 + (2 * (y1 - y0) + 1) / 3;
  793. p.x2 = x1 + (curx - x1 + 1) / 3;
  794. p.y2 = y1 + (cury - y1 + 1) / 3;
  795. p.x3 = curx;
  796. p.y3 = cury;
  797. aPathList.push_back( p );
  798. }
  799. else
  800. {
  801. if (!(x0 == curx && y0 == cury))
  802. { /* eliminate empty lines */
  803. p = PSPathElement(PS_LINETO);
  804. p.x1 = curx;
  805. p.y1 = cury;
  806. aPathList.push_back( p );
  807. }
  808. }
  809. x0 = curx; y0 = cury; lastOff = 0;
  810. }
  811. else
  812. {
  813. if (lastOff)
  814. {
  815. x2 = (x1 + curx + 1) / 2;
  816. y2 = (y1 + cury + 1) / 2;
  817. p = PSPathElement(PS_CURVETO);
  818. p.x1 = x0 + (2 * (x1 - x0) + 1) / 3;
  819. p.y1 = y0 + (2 * (y1 - y0) + 1) / 3;
  820. p.x2 = x1 + (x2 - x1 + 1) / 3;
  821. p.y2 = y1 + (y2 - y1 + 1) / 3;
  822. p.x3 = x2;
  823. p.y3 = y2;
  824. aPathList.push_back( p );
  825. x0 = x2; y0 = y2;
  826. x1 = curx; y1 = cury;
  827. } else {
  828. x1 = curx; y1 = cury;
  829. }
  830. lastOff = true;
  831. }
  832. if (ecflag) {
  833. aPathList.push_back( PSPathElement(PS_CLOSEPATH) );
  834. scflag = 1;
  835. ecflag = 0;
  836. cp = EndContour + 1;
  837. if (cp >= srcCount) break;
  838. continue;
  839. }
  840. if (cp == EndContour) {
  841. cp = StartContour;
  842. ecflag = true;
  843. } else {
  844. cp++;
  845. }
  846. }
  847. if( (nPathCount = (int)aPathList.size()) > 0)
  848. {
  849. *path = (PSPathElement*)calloc(nPathCount, sizeof(PSPathElement));
  850. assert(*path != 0);
  851. memcpy( *path, &aPathList[0], nPathCount * sizeof(PSPathElement) );
  852. }
  853. return nPathCount;
  854. }
  855. #endif
  856. /*- Extracts a string from the name table and allocates memory for it -*/
  857. static char *nameExtract( const sal_uInt8* name, int nTableSize, int n, int dbFlag, sal_uInt16** ucs2result )
  858. {
  859. int i;
  860. char *res;
  861. const sal_uInt8* ptr = name + GetUInt16(name, 4, 1) + GetUInt16(name + 6, 12 * n + 10, 1);
  862. int len = GetUInt16(name+6, 12 * n + 8, 1);
  863. // sanity check
  864. if( (len <= 0) || ((ptr+len) > (name+nTableSize)) )
  865. {
  866. if( ucs2result )
  867. *ucs2result = NULL;
  868. return NULL;
  869. }
  870. if( ucs2result )
  871. *ucs2result = NULL;
  872. if (dbFlag) {
  873. res = (char*)malloc(1 + len/2);
  874. assert(res != 0);
  875. for (i = 0; i < len/2; i++) res[i] = *(ptr + i * 2 + 1);
  876. res[len/2] = 0;
  877. if( ucs2result )
  878. {
  879. *ucs2result = (sal_uInt16*)malloc( len+2 );
  880. for (i = 0; i < len/2; i++ ) (*ucs2result)[i] = GetUInt16( ptr, 2*i, 1 );
  881. (*ucs2result)[len/2] = 0;
  882. }
  883. } else {
  884. res = (char*)malloc(1 + len);
  885. assert(res != 0);
  886. memcpy(res, ptr, len);
  887. res[len] = 0;
  888. }
  889. return res;
  890. }
  891. static int findname( const sal_uInt8 *name, sal_uInt16 n, sal_uInt16 platformID,
  892. sal_uInt16 encodingID, sal_uInt16 languageID, sal_uInt16 nameID )
  893. {
  894. int l = 0, r = n-1, i;
  895. sal_uInt32 t1, t2;
  896. sal_uInt32 m1, m2;
  897. if (n == 0) return -1;
  898. m1 = (platformID << 16) | encodingID;
  899. m2 = (languageID << 16) | nameID;
  900. do {
  901. i = (l + r) >> 1;
  902. t1 = GetUInt32(name + 6, i * 12 + 0, 1);
  903. t2 = GetUInt32(name + 6, i * 12 + 4, 1);
  904. if (! ((m1 < t1) || ((m1 == t1) && (m2 < t2)))) l = i + 1;
  905. if (! ((m1 > t1) || ((m1 == t1) && (m2 > t2)))) r = i - 1;
  906. } while (l <= r);
  907. if (l - r == 2) {
  908. return l - 1;
  909. }
  910. return -1;
  911. }
  912. /* XXX marlett.ttf uses (3, 0, 1033) instead of (3, 1, 1033) and does not have any Apple tables.
  913. * Fix: if (3, 1, 1033) is not found - need to check for (3, 0, 1033)
  914. *
  915. * /d/fonts/ttzh_tw/Big5/Hanyi/ma6b5p uses (1, 0, 19) for English strings, instead of (1, 0, 0)
  916. * and does not have (3, 1, 1033)
  917. * Fix: if (1, 0, 0) and (3, 1, 1033) are not found need to look for (1, 0, *) - that will
  918. * require a change in algorithm
  919. *
  920. * /d/fonts/fdltest/Korean/h2drrm has unsorted names and a an unknown (to me) Mac LanguageID,
  921. * but (1, 0, 1042) strings usable
  922. * Fix: change algorithm, and use (1, 0, *) if both standard Mac and MS strings are not found
  923. */
  924. static void GetNames(TrueTypeFont *t)
  925. {
  926. const sal_uInt8* table = getTable( t, O_name );
  927. int nTableSize = getTableSize(t, O_name);
  928. if (nTableSize < 4)
  929. {
  930. #if OSL_DEBUG_LEVEL > 1
  931. fprintf(stderr, "O_name table too small\n");
  932. #endif
  933. return;
  934. }
  935. sal_uInt16 n = GetUInt16(table, 2, 1);
  936. int i, r;
  937. sal_Bool bPSNameOK = sal_True;
  938. /* #129743# simple sanity check for name table entry count */
  939. if( nTableSize <= n * 12 + 6 )
  940. n = 0;
  941. /* PostScript name: preferred Microsoft */
  942. t->psname = NULL;
  943. if ((r = findname(table, n, 3, 1, 0x0409, 6)) != -1)
  944. t->psname = nameExtract(table, nTableSize, r, 1, NULL);
  945. if ( ! t->psname && (r = findname(table, n, 1, 0, 0, 6)) != -1)
  946. t->psname = nameExtract(table, nTableSize, r, 0, NULL);
  947. if ( ! t->psname && (r = findname(table, n, 3, 0, 0x0409, 6)) != -1)
  948. {
  949. // some symbol fonts like Marlett have a 3,0 name!
  950. t->psname = nameExtract(table, nTableSize, r, 1, NULL);
  951. }
  952. // for embedded font in Ghostscript PDFs
  953. if ( ! t->psname && (r = findname(table, n, 2, 2, 0, 6)) != -1)
  954. {
  955. t->psname = nameExtract(table, nTableSize, r, 0, NULL);
  956. }
  957. if ( ! t->psname )
  958. {
  959. if ( t->fname )
  960. {
  961. char* pReverse = t->fname + strlen(t->fname);
  962. /* take only last token of filename */
  963. while(pReverse != t->fname && *pReverse != '/') pReverse--;
  964. if(*pReverse == '/') pReverse++;
  965. t->psname = strdup(pReverse);
  966. assert(t->psname != 0);
  967. for (i=strlen(t->psname) - 1; i > 0; i--)
  968. {
  969. /*- Remove the suffix -*/
  970. if (t->psname[i] == '.' ) {
  971. t->psname[i] = 0;
  972. break;
  973. }
  974. }
  975. }
  976. else
  977. t->psname = strdup( "Unknown" );
  978. }
  979. /* Font family and subfamily names: preferred Apple */
  980. t->family = NULL;
  981. if ((r = findname(table, n, 0, 0, 0, 1)) != -1)
  982. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  983. if ( ! t->family && (r = findname(table, n, 3, 1, 0x0409, 1)) != -1)
  984. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  985. if ( ! t->family && (r = findname(table, n, 1, 0, 0, 1)) != -1)
  986. t->family = nameExtract(table, nTableSize, r, 0, NULL);
  987. if ( ! t->family && (r = findname(table, n, 3, 1, 0x0411, 1)) != -1)
  988. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  989. if ( ! t->family && (r = findname(table, n, 3, 0, 0x0409, 1)) != -1)
  990. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  991. if ( ! t->family )
  992. {
  993. t->family = strdup(t->psname);
  994. assert(t->family != 0);
  995. }
  996. t->subfamily = NULL;
  997. t->usubfamily = NULL;
  998. if ((r = findname(table, n, 1, 0, 0, 2)) != -1)
  999. t->subfamily = nameExtract(table, nTableSize, r, 0, &t->usubfamily);
  1000. if ( ! t->subfamily && (r = findname(table, n, 3, 1, 0x0409, 2)) != -1)
  1001. t->subfamily = nameExtract(table, nTableSize, r, 1, &t->usubfamily);
  1002. if ( ! t->subfamily )
  1003. {
  1004. t->subfamily = strdup("");
  1005. }
  1006. /* #i60349# sanity check psname
  1007. * psname parctically has to be 7bit ascii and should not contains spaces
  1008. * there is a class of broken fonts which do not fullfill that at all, so let's try
  1009. * if the family name is 7bit ascii and take it instead if so
  1010. */
  1011. /* check psname */
  1012. for( i = 0; t->psname[i] != 0 && bPSNameOK; i++ )
  1013. if( t->psname[ i ] < 33 || (t->psname[ i ] & 0x80) )
  1014. bPSNameOK = sal_False;
  1015. if( bPSNameOK == sal_False )
  1016. {
  1017. sal_Bool bReplace = sal_True;
  1018. /* check if family is a suitable replacement */
  1019. if( t->ufamily && t->family )
  1020. {
  1021. for( i = 0; t->ufamily[ i ] != 0 && bReplace; i++ )
  1022. if( t->ufamily[ i ] < 33 || t->ufamily[ i ] > 127 )
  1023. bReplace = sal_False;
  1024. if( bReplace )
  1025. {
  1026. free( t->psname );
  1027. t->psname = strdup( t->family );
  1028. }
  1029. }
  1030. }
  1031. }
  1032. enum cmapType {
  1033. CMAP_NOT_USABLE = -1,
  1034. CMAP_MS_Symbol = 10,
  1035. CMAP_MS_Unicode = 11,
  1036. CMAP_MS_ShiftJIS = 12,
  1037. CMAP_MS_Big5 = 13,
  1038. CMAP_MS_PRC = 14,
  1039. CMAP_MS_Wansung = 15,
  1040. CMAP_MS_Johab = 16
  1041. };
  1042. #define MISSING_GLYPH_INDEX 0
  1043. /*
  1044. * getGlyph[0246]() functions and freinds are implemented by:
  1045. * @author Manpreet Singh
  1046. * getGlyph12() function and friends by:
  1047. * @author HDU
  1048. */
  1049. static sal_uInt32 getGlyph0(const sal_uInt8* cmap, sal_uInt32 c) {
  1050. if (c <= 255) {
  1051. return *(cmap + 6 + c);
  1052. } else {
  1053. return MISSING_GLYPH_INDEX;
  1054. }
  1055. }
  1056. typedef struct _subHeader2 {
  1057. sal_uInt16 firstCode;
  1058. sal_uInt16 entryCount;
  1059. sal_uInt16 idDelta;
  1060. sal_uInt16 idRangeOffset;
  1061. } subHeader2;
  1062. static sal_uInt32 getGlyph2(const sal_uInt8 *cmap, sal_uInt32 c) {
  1063. sal_uInt16 *CMAP2 = (sal_uInt16 *) cmap;
  1064. sal_uInt8 theHighByte;
  1065. sal_uInt8 theLowByte;
  1066. subHeader2* subHeader2s;
  1067. sal_uInt16* subHeader2Keys;
  1068. sal_uInt16 firstCode;
  1069. int k;
  1070. sal_uInt32 ToReturn;
  1071. theHighByte = (sal_uInt8)((c >> 8) & 0x00ff);
  1072. theLowByte = (sal_uInt8)(c & 0x00ff);
  1073. subHeader2Keys = CMAP2 + 3;
  1074. subHeader2s = (subHeader2 *)(subHeader2Keys + 256);
  1075. k = Int16FromMOTA(subHeader2Keys[theHighByte]) / 8;
  1076. if(k == 0) {
  1077. firstCode = Int16FromMOTA(subHeader2s[k].firstCode);
  1078. if(theLowByte >= firstCode && theLowByte < (firstCode + Int16FromMOTA(subHeader2s[k].entryCount))) {
  1079. return *((&(subHeader2s[0].idRangeOffset))
  1080. + (Int16FromMOTA(subHeader2s[0].idRangeOffset)/2) /* + offset */
  1081. + theLowByte /* + to_look */
  1082. - Int16FromMOTA(subHeader2s[0].firstCode)
  1083. );
  1084. } else {
  1085. return MISSING_GLYPH_INDEX;
  1086. }
  1087. } else if (k > 0) {
  1088. firstCode = Int16FromMOTA(subHeader2s[k].firstCode);
  1089. if(theLowByte >= firstCode && theLowByte < (firstCode + Int16FromMOTA(subHeader2s[k].entryCount))) {
  1090. ToReturn = *((&(subHeader2s[k].idRangeOffset))
  1091. + (Int16FromMOTA(subHeader2s[k].idRangeOffset)/2)
  1092. + theLowByte - firstCode);
  1093. if(ToReturn == 0) {
  1094. return MISSING_GLYPH_INDEX;
  1095. } else {
  1096. ToReturn += Int16FromMOTA(subHeader2s[k].idDelta);
  1097. return (ToReturn & 0xFFFF);
  1098. }
  1099. } else {
  1100. return MISSING_GLYPH_INDEX;
  1101. }
  1102. } else {
  1103. return MISSING_GLYPH_INDEX;
  1104. }
  1105. }
  1106. static sal_uInt32 getGlyph6(const sal_uInt8 *cmap, sal_uInt32 c) {
  1107. sal_uInt16 firstCode, lastCode, count;
  1108. sal_uInt16 *CMAP6 = (sal_uInt16 *) cmap;
  1109. firstCode = Int16FromMOTA(*(CMAP6 + 3));
  1110. count = Int16FromMOTA(*(CMAP6 + 4));
  1111. lastCode = firstCode + count - 1;
  1112. if (c < firstCode || c > lastCode) {
  1113. return MISSING_GLYPH_INDEX;
  1114. } else {
  1115. return *((CMAP6 + 5)/*glyphIdArray*/ + (c - firstCode));
  1116. }
  1117. }
  1118. static sal_uInt16 GEbinsearch(sal_uInt16 *ar, sal_uInt16 length, sal_uInt16 toSearch) {
  1119. signed int low, mid, high, lastfound = 0xffff;
  1120. sal_uInt16 res;
  1121. if(length == (sal_uInt16)0 || length == (sal_uInt16)0xFFFF) {
  1122. return (sal_uInt16)0xFFFF;
  1123. }
  1124. low = 0;
  1125. high = length - 1;
  1126. while(high >= low) {
  1127. mid = (high + low)/2;
  1128. res = Int16FromMOTA(*(ar+mid));
  1129. if(res >= toSearch) {
  1130. lastfound = mid;
  1131. high = --mid;
  1132. } else {
  1133. low = ++mid;
  1134. }
  1135. }
  1136. return (sal_uInt16)lastfound;
  1137. }
  1138. static sal_uInt32 getGlyph4(const sal_uInt8 *cmap, sal_uInt32 c) {
  1139. sal_uInt16 i;
  1140. int ToReturn;
  1141. sal_uInt16 segCount;
  1142. sal_uInt16 * startCode;
  1143. sal_uInt16 * endCode;
  1144. sal_uInt16 * idDelta;
  1145. /* sal_uInt16 * glyphIdArray; */
  1146. sal_uInt16 * idRangeOffset;
  1147. sal_uInt16 * glyphIndexArray;
  1148. sal_uInt16 *CMAP4 = (sal_uInt16 *) cmap;
  1149. /* sal_uInt16 GEbinsearch(sal_uInt16 *ar, sal_uInt16 length, sal_uInt16 toSearch); */
  1150. segCount = Int16FromMOTA(*(CMAP4 + 3))/2;
  1151. endCode = CMAP4 + 7;
  1152. i = GEbinsearch(endCode, segCount, (sal_uInt16)c);
  1153. if (i == (sal_uInt16) 0xFFFF) {
  1154. return MISSING_GLYPH_INDEX;
  1155. }
  1156. startCode = endCode + segCount + 1;
  1157. if(Int16FromMOTA(startCode[i]) > c) {
  1158. return MISSING_GLYPH_INDEX;
  1159. }
  1160. idDelta = startCode + segCount;
  1161. idRangeOffset = idDelta + segCount;
  1162. glyphIndexArray = idRangeOffset + segCount;
  1163. if(Int16FromMOTA(idRangeOffset[i]) != 0) {
  1164. c = Int16FromMOTA(*(&(idRangeOffset[i]) + (Int16FromMOTA(idRangeOffset[i])/2 + (c - Int16FromMOTA(startCode[i])))));
  1165. }
  1166. ToReturn = (Int16FromMOTA(idDelta[i]) + c) & 0xFFFF;
  1167. return ToReturn;
  1168. }
  1169. static sal_uInt32 getGlyph12(const sal_uInt8 *pCmap, sal_uInt32 cChar) {
  1170. const sal_uInt32* pCMAP12 = (const sal_uInt32*)pCmap;
  1171. int nLength = Int32FromMOTA( pCMAP12[1] );
  1172. int nGroups = Int32FromMOTA( pCMAP12[3] );
  1173. int nLower = 0;
  1174. int nUpper = nGroups;
  1175. if( nUpper > (nLength-16)/12 )
  1176. nUpper = (nLength-16)/12;
  1177. /* binary search in "segmented coverage" subtable */
  1178. while( nLower < nUpper ) {
  1179. int nIndex = (nLower + nUpper) / 2;
  1180. const sal_uInt32* pEntry = &pCMAP12[ 4 + 3*nIndex ];
  1181. sal_uInt32 cStart = Int32FromMOTA( pEntry[0] );
  1182. sal_uInt32 cLast = Int32FromMOTA( pEntry[1] );
  1183. if( cChar < cStart )
  1184. nUpper = nIndex;
  1185. else if( cChar > cLast )
  1186. nLower = nIndex + 1;
  1187. else { /* found matching entry! */
  1188. sal_uInt32 nGlyph = Int32FromMOTA( pEntry[2] );
  1189. nGlyph += cChar - cStart;
  1190. return nGlyph;
  1191. }
  1192. }
  1193. return MISSING_GLYPH_INDEX;
  1194. }
  1195. static void FindCmap(TrueTypeFont *ttf)
  1196. {
  1197. const sal_uInt8* table = getTable(ttf, O_cmap);
  1198. sal_uInt32 table_size = getTableSize(ttf, O_cmap);
  1199. sal_uInt16 ncmaps = GetUInt16(table, 2, 1);
  1200. unsigned int i;
  1201. sal_uInt32 AppleUni = 0; // Apple Unicode
  1202. sal_uInt32 ThreeZero = 0; /* MS Symbol */
  1203. sal_uInt32 ThreeOne = 0; /* MS UCS-2 */
  1204. sal_uInt32 ThreeTwo = 0; /* MS ShiftJIS */
  1205. sal_uInt32 ThreeThree = 0; /* MS Big5 */
  1206. sal_uInt32 ThreeFour = 0; /* MS PRC */
  1207. sal_uInt32 ThreeFive = 0; /* MS Wansung */
  1208. sal_uInt32 ThreeSix = 0; /* MS Johab */
  1209. for (i = 0; i < ncmaps; i++) {
  1210. sal_uInt32 offset;
  1211. sal_uInt16 pID, eID;
  1212. /* sanity check, cmap entry must lie within table */
  1213. if( i*8+4 > table_size )
  1214. break;
  1215. pID = GetUInt16(table, 4 + i * 8, 1);
  1216. eID = GetUInt16(table, 6 + i * 8, 1);
  1217. offset = GetUInt32(table, 8 + i * 8, 1);
  1218. /* sanity check, cmap must lie within file */
  1219. if( (table - ttf->ptr) + offset > (sal_uInt32)ttf->fsize )
  1220. continue;
  1221. /* Unicode tables in Apple fonts */
  1222. if (pID == 0) {
  1223. AppleUni = offset;
  1224. }
  1225. if (pID == 3) {
  1226. switch (eID) {
  1227. case 0: ThreeZero = offset; break;
  1228. case 10: // UCS-4
  1229. case 1: ThreeOne = offset; break;
  1230. case 2: ThreeTwo = offset; break;
  1231. case 3: ThreeThree = offset; break;
  1232. case 4: ThreeFour = offset; break;
  1233. case 5: ThreeFive = offset; break;
  1234. case 6: ThreeSix = offset; break;
  1235. }
  1236. }
  1237. }
  1238. // fall back to AppleUnicode if there are no ThreeOne/Threezero tables
  1239. if( AppleUni && !ThreeZero && !ThreeOne)
  1240. ThreeOne = AppleUni;
  1241. if (ThreeOne) {
  1242. ttf->cmapType = CMAP_MS_Unicode;
  1243. ttf->cmap = table + ThreeOne;
  1244. } else if (ThreeTwo) {
  1245. ttf->cmapType = CMAP_MS_ShiftJIS;
  1246. ttf->cmap = table + ThreeTwo;
  1247. } else if (ThreeThree) {
  1248. ttf->cmapType = CMAP_MS_Big5;
  1249. ttf->cmap = table + ThreeThree;
  1250. } else if (ThreeFour) {
  1251. ttf->cmapType = CMAP_MS_PRC;
  1252. ttf->cmap = table + ThreeFour;
  1253. } else if (ThreeFive) {
  1254. ttf->cmapType = CMAP_MS_Wansung;
  1255. ttf->cmap = table + ThreeFive;
  1256. } else if (ThreeSix) {
  1257. ttf->cmapType = CMAP_MS_Johab;
  1258. ttf->cmap = table + ThreeSix;
  1259. } else if (ThreeZero) {
  1260. ttf->cmapType = CMAP_MS_Symbol;
  1261. ttf->cmap = table + ThreeZero;
  1262. } else {
  1263. ttf->cmapType = CMAP_NOT_USABLE;
  1264. ttf->cmap = 0;
  1265. }
  1266. if (ttf->cmapType != CMAP_NOT_USABLE) {
  1267. switch (GetUInt16(ttf->cmap, 0, 1)) {
  1268. case 0: ttf->mapper = getGlyph0; break;
  1269. case 2: ttf->mapper = getGlyph2; break;
  1270. case 4: ttf->mapper = getGlyph4; break;
  1271. case 6: ttf->mapper = getGlyph6; break;
  1272. case 12: ttf->mapper= getGlyph12; break;
  1273. default:
  1274. #if OSL_DEBUG_LEVEL > 1
  1275. /*- if the cmap table is really broken */
  1276. printf("%s: %d is not a recognized cmap format.\n", ttf->fname, GetUInt16(ttf->cmap, 0, 1));
  1277. #endif
  1278. ttf->cmapType = CMAP_NOT_USABLE;
  1279. ttf->cmap = 0;
  1280. ttf->mapper = 0;
  1281. }
  1282. }
  1283. }
  1284. static void GetKern(TrueTypeFont *ttf)
  1285. {
  1286. const sal_uInt8* table = getTable(ttf, O_kern);
  1287. const sal_uInt8 *ptr;
  1288. if( !table )
  1289. goto badtable;
  1290. if (GetUInt16(table, 0, 1) == 0) { /* Traditional Microsoft style table with USHORT version and nTables fields */
  1291. ttf->nkern = GetUInt16(table, 2, 1);
  1292. ttf->kerntables = (const sal_uInt8**)calloc(ttf->nkern, sizeof(sal_uInt8 *));
  1293. assert(ttf->kerntables != 0);
  1294. memset(ttf->kerntables, 0, ttf->nkern * sizeof(sal_uInt8 *));
  1295. ttf->kerntype = KT_MICROSOFT;
  1296. ptr = table + 4;
  1297. for( unsigned i = 0; i < ttf->nkern; ++i) {
  1298. ttf->kerntables[i] = ptr;
  1299. ptr += GetUInt16(ptr, 2, 1);
  1300. /* sanity check */
  1301. if( ptr > ttf->ptr+ttf->fsize )
  1302. {
  1303. free( ttf->kerntables );
  1304. goto badtable;
  1305. }
  1306. }
  1307. return;
  1308. }
  1309. if (GetUInt32(table, 0, 1) == 0x00010000) { /* MacOS style kern tables: fixed32 version and sal_uInt32 nTables fields */
  1310. ttf->nkern = GetUInt32(table, 4, 1);
  1311. ttf->kerntables = (const sal_uInt8**)calloc(ttf->nkern, sizeof(sal_uInt8*));
  1312. assert(ttf->kerntables != 0);
  1313. memset(ttf->kerntables, 0, ttf->nkern * sizeof(sal_uInt8 *));
  1314. ttf->kerntype = KT_APPLE_NEW;
  1315. ptr = table + 8;
  1316. for( unsigned i = 0; i < ttf->nkern; ++i) {
  1317. ttf->kerntables[i] = ptr;
  1318. ptr += GetUInt32(ptr, 0, 1);
  1319. /* sanity check; there are some fonts that are broken in this regard */
  1320. if( ptr > ttf->ptr+ttf->fsize )
  1321. {
  1322. free( ttf->kerntables );
  1323. goto badtable;
  1324. }
  1325. }
  1326. return;
  1327. }
  1328. badtable:
  1329. ttf->kerntype = KT_NONE;
  1330. ttf->kerntables = 0;
  1331. return;
  1332. }
  1333. #ifdef TEST5
  1334. /* KernGlyphsPrim?() functions expect the caller to ensure the validity of their arguments and
  1335. * that x and y elements of the kern array are initialized to zeroes
  1336. */
  1337. static void KernGlyphsPrim1(TrueTypeFont *ttf, sal_uInt16 *glyphs, int nglyphs, int wmode, KernData *kern)
  1338. {
  1339. (void)ttf; /* avoid warning */
  1340. (void)glyphs; /* avoid warning */
  1341. (void)nglyphs; /* avoid warning */
  1342. (void)wmode; /* avoid warning */
  1343. (void)nglyphs; /* avoid warning */
  1344. (void)kern; /* avoid warning */
  1345. fprintf(stderr, "MacOS kerning tables have not been implemented yet!\n");
  1346. }
  1347. static void KernGlyphsPrim2(TrueTypeFont *ttf, sal_uInt16 *glyphs, int nglyphs, int wmode, KernData *kern)
  1348. {
  1349. sal_uInt32 i, j;
  1350. sal_uInt32 gpair;
  1351. if( ! nglyphs )
  1352. return;
  1353. for (i = 0; i < (sal_uInt32)nglyphs - 1; i++) {
  1354. gpair = (glyphs[i] << 16) | glyphs[i+1];
  1355. #ifdef DEBUG2
  1356. /* All fonts with MS kern table that I've seen so far contain just one kern subtable.
  1357. * MS kern documentation is very poor and I doubt that font developers will be using
  1358. * several subtables. I expect them to be using OpenType tables instead.
  1359. * According to MS documention, format 2 subtables are not supported by Windows and OS/2.
  1360. */
  1361. if (ttf->nkern > 1) {
  1362. fprintf(stderr, "KernGlyphsPrim2: %d kern tables found.\n", ttf->nkern);
  1363. }
  1364. #endif
  1365. for (j = 0; j < ttf->nkern; j++) {
  1366. sal_uInt16 coverage = GetUInt16(ttf->kerntables[j], 4, 1);
  1367. sal_uInt8 *ptr;
  1368. int npairs;
  1369. sal_uInt32 t;
  1370. int l, r, k;
  1371. if (! ((coverage & 1) ^ wmode)) continue;
  1372. if ((coverage & 0xFFFE) != 0) {
  1373. #ifdef DEBUG2
  1374. fprintf(stderr, "KernGlyphsPrim2: coverage flags are not supported: %04X.\n", coverage);
  1375. #endif
  1376. continue;
  1377. }
  1378. ptr = ttf->kerntables[j];
  1379. npairs = GetUInt16(ptr, 6, 1);
  1380. ptr += 14;
  1381. l = 0;
  1382. r = npairs;
  1383. do {
  1384. k = (l + r) >> 1;
  1385. t = GetUInt32(ptr, k * 6, 1);
  1386. if (gpair >= t) l = k + 1;
  1387. if (gpair <= t) r = k - 1;
  1388. } while (l <= r);
  1389. if (l - r == 2) {
  1390. if (!wmode) {
  1391. kern[i].x = XUnits(ttf->unitsPerEm, GetInt16(ptr, 4 + (l-1) * 6, 1));
  1392. } else {
  1393. kern[i].y = XUnits(ttf->unitsPerEm, GetInt16(ptr, 4 + (l-1) * 6, 1));
  1394. }
  1395. /* !wmode ? kern[i].x : kern[i].y = GetInt16(ptr, 4 + (l-1) * 6, 1); */
  1396. }
  1397. }
  1398. }
  1399. }
  1400. #endif
  1401. /*- Public functions */ /*FOLD00*/
  1402. int CountTTCFonts(const char* fname)
  1403. {
  1404. int nFonts = 0;
  1405. sal_uInt8 buffer[12];
  1406. FILE* fd = fopen(fname, "rb");

Large files files are truncated, but you can click here to view the full file