PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/vcl/source/fontsubset/sft.cxx

https://bitbucket.org/markjenkins/libreoffice_ubuntu-debian-fixes
C++ | 2769 lines | 2159 code | 411 blank | 199 comment | 544 complexity | 68340c6e646cf8d3efec2a9319656034 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause-No-Nuclear-License-2014
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * This file is part of the LibreOffice project.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * This file incorporates work covered by the following license notice:
  10. *
  11. * Licensed to the Apache Software Foundation (ASF) under one or more
  12. * contributor license agreements. See the NOTICE file distributed
  13. * with this work for additional information regarding copyright
  14. * ownership. The ASF licenses this file to you under the Apache
  15. * License, Version 2.0 (the "License"); you may not use this file
  16. * except in compliance with the License. You may obtain a copy of
  17. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  18. */
  19. /*
  20. * Sun Font Tools
  21. *
  22. * Author: Alexander Gelfenbain
  23. *
  24. */
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #ifdef UNX
  30. #include <sys/mman.h>
  31. #include <sys/stat.h>
  32. #endif
  33. #include "sft.hxx"
  34. #include "gsub.h"
  35. #if ! (defined(NO_TTCR) && defined(NO_TYPE42))
  36. #include "ttcr.hxx"
  37. #endif
  38. #ifndef NO_MAPPERS /* include MapChar() and MapString() */
  39. #include "xlat.hxx"
  40. #endif
  41. #ifndef NO_TYPE3 /* include CreateT3FromTTGlyphs() */
  42. #include <rtl/crc.h>
  43. #endif
  44. #include <osl/endian.h>
  45. #include <algorithm>
  46. namespace vcl
  47. {
  48. /*- module identification */
  49. static const char *modname = "SunTypeTools-TT";
  50. static const char *modver = "1.0";
  51. static const char *modextra = "gelf";
  52. /*- private functions, constants and data types */ /*FOLD00*/
  53. enum PathSegmentType {
  54. PS_NOOP = 0,
  55. PS_MOVETO = 1,
  56. PS_LINETO = 2,
  57. PS_CURVETO = 3,
  58. PS_CLOSEPATH = 4
  59. };
  60. struct PSPathElement
  61. {
  62. PathSegmentType type;
  63. int x1, y1;
  64. int x2, y2;
  65. int x3, y3;
  66. PSPathElement( PathSegmentType i_eType ) : type( i_eType ),
  67. x1( 0 ), y1( 0 ),
  68. x2( 0 ), y2( 0 ),
  69. x3( 0 ), y3( 0 )
  70. {
  71. }
  72. };
  73. /*- In horizontal writing mode right sidebearing is calculated using this formula
  74. *- rsb = aw - (lsb + xMax - xMin) -*/
  75. typedef struct {
  76. sal_Int16 xMin;
  77. sal_Int16 yMin;
  78. sal_Int16 xMax;
  79. sal_Int16 yMax;
  80. sal_uInt16 aw; /*- Advance Width (horizontal writing mode) */
  81. sal_Int16 lsb; /*- Left sidebearing (horizontal writing mode) */
  82. sal_uInt16 ah; /*- advance height (vertical writing mode) */
  83. sal_Int16 tsb; /*- top sidebearing (vertical writing mode) */
  84. } TTGlyphMetrics;
  85. #define HFORMAT_LINELEN 64
  86. typedef struct {
  87. FILE *o;
  88. char buffer[HFORMAT_LINELEN];
  89. size_t bufpos;
  90. int total;
  91. } HexFmt;
  92. typedef struct {
  93. sal_uInt32 nGlyphs; /* number of glyphs in the font + 1 */
  94. sal_uInt32 *offs; /* array of nGlyphs offsets */
  95. } GlyphOffsets;
  96. /* private tags */
  97. static const sal_uInt32 TTFontClassTag = 0x74746663; /* 'ttfc' */
  98. static const sal_uInt32 T_true = 0x74727565; /* 'true' */
  99. static const sal_uInt32 T_ttcf = 0x74746366; /* 'ttcf' */
  100. static const sal_uInt32 T_otto = 0x4f54544f; /* 'OTTO' */
  101. /* standard TrueType table tags */
  102. #define T_maxp 0x6D617870
  103. #define T_glyf 0x676C7966
  104. #define T_head 0x68656164
  105. #define T_loca 0x6C6F6361
  106. #define T_name 0x6E616D65
  107. #define T_hhea 0x68686561
  108. #define T_hmtx 0x686D7478
  109. #define T_cmap 0x636D6170
  110. #define T_vhea 0x76686561
  111. #define T_vmtx 0x766D7478
  112. #define T_OS2 0x4F532F32
  113. #define T_post 0x706F7374
  114. #define T_kern 0x6B65726E
  115. #define T_cvt 0x63767420
  116. #define T_prep 0x70726570
  117. #define T_fpgm 0x6670676D
  118. #define T_gsub 0x47535542
  119. #define T_CFF 0x43464620
  120. #define LAST_URANGE_BIT 69
  121. const char *ulcodes[LAST_URANGE_BIT+2] = {
  122. /* 0 */ "Basic Latin",
  123. /* 1 */ "Latin-1 Supplement",
  124. /* 2 */ "Latin Extended-A",
  125. /* 3 */ "Latin Extended-B",
  126. /* 4 */ "IPA Extensions",
  127. /* 5 */ "Spacing Modifier Letters",
  128. /* 6 */ "Combining Diacritical Marks",
  129. /* 7 */ "Basic Greek",
  130. /* 8 */ "Greek Symbols And Coptic",
  131. /* 9 */ "Cyrillic",
  132. /* 10 */ "Armenian",
  133. /* 11 */ "Basic Hebrew",
  134. /* 12 */ "Hebrew Extended (A and B blocks combined)",
  135. /* 13 */ "Basic Arabic",
  136. /* 14 */ "Arabic Extended",
  137. /* 15 */ "Devanagari",
  138. /* 16 */ "Bengali",
  139. /* 17 */ "Gurmukhi",
  140. /* 18 */ "Gujarati",
  141. /* 19 */ "Oriya",
  142. /* 20 */ "Tamil",
  143. /* 21 */ "Telugu",
  144. /* 22 */ "Kannada",
  145. /* 23 */ "Malayalam",
  146. /* 24 */ "Thai",
  147. /* 25 */ "Lao",
  148. /* 26 */ "Basic Georgian",
  149. /* 27 */ "Georgian Extended",
  150. /* 28 */ "Hangul Jamo",
  151. /* 29 */ "Latin Extended Additional",
  152. /* 30 */ "Greek Extended",
  153. /* 31 */ "General Punctuation",
  154. /* 32 */ "Superscripts And Subscripts",
  155. /* 33 */ "Currency Symbols",
  156. /* 34 */ "Combining Diacritical Marks For Symbols",
  157. /* 35 */ "Letterlike Symbols",
  158. /* 36 */ "Number Forms",
  159. /* 37 */ "Arrows",
  160. /* 38 */ "Mathematical Operators",
  161. /* 39 */ "Miscellaneous Technical",
  162. /* 40 */ "Control Pictures",
  163. /* 41 */ "Optical Character Recognition",
  164. /* 42 */ "Enclosed Alphanumerics",
  165. /* 43 */ "Box Drawing",
  166. /* 44 */ "Block Elements",
  167. /* 45 */ "Geometric Shapes",
  168. /* 46 */ "Miscellaneous Symbols",
  169. /* 47 */ "Dingbats",
  170. /* 48 */ "CJK Symbols And Punctuation",
  171. /* 49 */ "Hiragana",
  172. /* 50 */ "Katakana",
  173. /* 51 */ "Bopomofo",
  174. /* 52 */ "Hangul Compatibility Jamo",
  175. /* 53 */ "CJK Miscellaneous",
  176. /* 54 */ "Enclosed CJK Letters And Months",
  177. /* 55 */ "CJK Compatibility",
  178. /* 56 */ "Hangul",
  179. /* 57 */ "Reserved for Unicode SubRanges",
  180. /* 58 */ "Reserved for Unicode SubRanges",
  181. /* 59 */ "CJK Unified Ideographs",
  182. /* 60 */ "Private Use Area",
  183. /* 61 */ "CJK Compatibility Ideographs",
  184. /* 62 */ "Alphabetic Presentation Forms",
  185. /* 63 */ "Arabic Presentation Forms-A",
  186. /* 64 */ "Combining Half Marks",
  187. /* 65 */ "CJK Compatibility Forms",
  188. /* 66 */ "Small Form Variants",
  189. /* 67 */ "Arabic Presentation Forms-B",
  190. /* 68 */ "Halfwidth And Fullwidth Forms",
  191. /* 69 */ "Specials",
  192. /*70-127*/ "Reserved for Unicode SubRanges"
  193. };
  194. /*- inline functions */ /*FOLD01*/
  195. #ifdef __GNUC__
  196. #define _inline static __inline__
  197. #else
  198. #define _inline static
  199. #endif
  200. _inline void *smalloc(size_t size)
  201. {
  202. void *res = malloc(size);
  203. assert(res != 0);
  204. return res;
  205. }
  206. _inline void *scalloc(size_t n, size_t size)
  207. {
  208. void *res = calloc(n, size);
  209. assert(res != 0);
  210. return res;
  211. }
  212. _inline sal_uInt32 mkTag(sal_uInt8 a, sal_uInt8 b, sal_uInt8 c, sal_uInt8 d) {
  213. return (a << 24) | (b << 16) | (c << 8) | d;
  214. }
  215. /*- Data access macros for data stored in big-endian or little-endian format */
  216. _inline sal_Int16 GetInt16(const sal_uInt8 *ptr, size_t offset, int bigendian)
  217. {
  218. sal_Int16 t;
  219. assert(ptr != 0);
  220. if (bigendian) {
  221. t = (ptr+offset)[0] << 8 | (ptr+offset)[1];
  222. } else {
  223. t = (ptr+offset)[1] << 8 | (ptr+offset)[0];
  224. }
  225. return t;
  226. }
  227. _inline sal_uInt16 GetUInt16(const sal_uInt8 *ptr, size_t offset, int bigendian)
  228. {
  229. sal_uInt16 t;
  230. assert(ptr != 0);
  231. if (bigendian) {
  232. t = (ptr+offset)[0] << 8 | (ptr+offset)[1];
  233. } else {
  234. t = (ptr+offset)[1] << 8 | (ptr+offset)[0];
  235. }
  236. return t;
  237. }
  238. _inline sal_Int32 GetInt32(const sal_uInt8 *ptr, size_t offset, int bigendian)
  239. {
  240. sal_Int32 t;
  241. assert(ptr != 0);
  242. if (bigendian) {
  243. t = (ptr+offset)[0] << 24 | (ptr+offset)[1] << 16 |
  244. (ptr+offset)[2] << 8 | (ptr+offset)[3];
  245. } else {
  246. t = (ptr+offset)[3] << 24 | (ptr+offset)[2] << 16 |
  247. (ptr+offset)[1] << 8 | (ptr+offset)[0];
  248. }
  249. return t;
  250. }
  251. _inline sal_uInt32 GetUInt32(const sal_uInt8 *ptr, size_t offset, int bigendian)
  252. {
  253. sal_uInt32 t;
  254. assert(ptr != 0);
  255. if (bigendian) {
  256. t = (ptr+offset)[0] << 24 | (ptr+offset)[1] << 16 |
  257. (ptr+offset)[2] << 8 | (ptr+offset)[3];
  258. } else {
  259. t = (ptr+offset)[3] << 24 | (ptr+offset)[2] << 16 |
  260. (ptr+offset)[1] << 8 | (ptr+offset)[0];
  261. }
  262. return t;
  263. }
  264. _inline void PutInt16(sal_Int16 val, sal_uInt8 *ptr, size_t offset, int bigendian)
  265. {
  266. assert(ptr != 0);
  267. if (bigendian) {
  268. ptr[offset] = (sal_uInt8)((val >> 8) & 0xFF);
  269. ptr[offset+1] = (sal_uInt8)(val & 0xFF);
  270. } else {
  271. ptr[offset+1] = (sal_uInt8)((val >> 8) & 0xFF);
  272. ptr[offset] = (sal_uInt8)(val & 0xFF);
  273. }
  274. }
  275. #if defined(OSL_BIGENDIAN)
  276. #define Int16FromMOTA(a) (a)
  277. #define Int32FromMOTA(a) (a)
  278. #else
  279. static sal_uInt16 Int16FromMOTA(sal_uInt16 a) {
  280. return (sal_uInt16) (((sal_uInt8)((a) >> 8)) | ((sal_uInt8)(a) << 8));
  281. }
  282. static sal_uInt32 Int32FromMOTA(sal_uInt32 a) {
  283. return ((a>>24)&0xFF) | (((a>>8)&0xFF00) | ((a&0xFF00)<<8) | ((a&0xFF)<<24));
  284. }
  285. #endif
  286. _inline F16Dot16 fixedMul(F16Dot16 a, F16Dot16 b)
  287. {
  288. unsigned int a1, b1;
  289. unsigned int a2, b2;
  290. F16Dot16 res;
  291. int sign;
  292. sign = (a & 0x80000000) ^ (b & 0x80000000);
  293. if (a < 0) a = -a;
  294. if (b < 0) b = -b;
  295. a1 = a >> 16;
  296. b1 = a & 0xFFFF;
  297. a2 = b >> 16;
  298. b2 = b & 0xFFFF;
  299. res = a1 * a2;
  300. /* if (res > 0x7FFF) assert(!"fixedMul: F16Dot16 overflow"); */
  301. res <<= 16;
  302. res += a1 * b2 + b1 * a2 + ((b1 * b2) >> 16);
  303. return sign ? -res : res;
  304. }
  305. _inline F16Dot16 fixedDiv(F16Dot16 a, F16Dot16 b)
  306. {
  307. unsigned int f, r;
  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. f = a / b;
  314. r = a % b;
  315. /* if (f > 0x7FFFF) assert(!"fixedDiv: F16Dot16 overflow"); */
  316. while (r > 0xFFFF) {
  317. r >>= 1;
  318. b >>= 1;
  319. }
  320. res = (f << 16) + (r << 16) / b;
  321. return sign ? -res : res;
  322. }
  323. /*- returns a * b / c -*/
  324. /* XXX provide a real implementation that preserves accuracy */
  325. _inline F16Dot16 fixedMulDiv(F16Dot16 a, F16Dot16 b, F16Dot16 c)
  326. {
  327. F16Dot16 res;
  328. res = fixedMul(a, b);
  329. return fixedDiv(res, c);
  330. }
  331. /*- Translate units from TT to PS (standard 1/1000) -*/
  332. _inline int XUnits(int unitsPerEm, int n)
  333. {
  334. return (n * 1000) / unitsPerEm;
  335. }
  336. _inline const char *UnicodeRangeName(sal_uInt16 bit)
  337. {
  338. if (bit > LAST_URANGE_BIT) bit = LAST_URANGE_BIT+1;
  339. return ulcodes[bit];
  340. }
  341. _inline const sal_uInt8* getTable( TrueTypeFont *ttf, sal_uInt32 ord)
  342. {
  343. return (sal_uInt8*)ttf->tables[ord];
  344. }
  345. _inline sal_uInt32 getTableSize(TrueTypeFont *ttf, sal_uInt32 ord)
  346. {
  347. return ttf->tlens[ord];
  348. }
  349. #ifndef NO_TYPE42
  350. /* Hex Formatter functions */
  351. static char HexChars[] = "0123456789ABCDEF";
  352. static HexFmt *HexFmtNew(FILE *outf)
  353. {
  354. HexFmt* res = (HexFmt*)smalloc(sizeof(HexFmt));
  355. res->bufpos = res->total = 0;
  356. res->o = outf;
  357. return res;
  358. }
  359. static bool HexFmtFlush(HexFmt *_this)
  360. {
  361. bool bRet = true;
  362. if (_this->bufpos) {
  363. size_t nWritten = fwrite(_this->buffer, 1, _this->bufpos, _this->o);
  364. bRet = nWritten == _this->bufpos;
  365. _this->bufpos = 0;
  366. }
  367. return bRet;
  368. }
  369. _inline void HexFmtOpenString(HexFmt *_this)
  370. {
  371. fputs("<\n", _this->o);
  372. }
  373. _inline void HexFmtCloseString(HexFmt *_this)
  374. {
  375. HexFmtFlush(_this);
  376. fputs("00\n>\n", _this->o);
  377. }
  378. _inline void HexFmtDispose(HexFmt *_this)
  379. {
  380. HexFmtFlush(_this);
  381. free(_this);
  382. }
  383. static void HexFmtBlockWrite(HexFmt *_this, const void *ptr, sal_uInt32 size)
  384. {
  385. sal_uInt8 Ch;
  386. sal_uInt32 i;
  387. if (_this->total + size > 65534) {
  388. HexFmtFlush(_this);
  389. HexFmtCloseString(_this);
  390. _this->total = 0;
  391. HexFmtOpenString(_this);
  392. }
  393. for (i=0; i<size; i++) {
  394. Ch = ((sal_uInt8 *) ptr)[i];
  395. _this->buffer[_this->bufpos++] = HexChars[Ch >> 4];
  396. _this->buffer[_this->bufpos++] = HexChars[Ch & 0xF];
  397. if (_this->bufpos == HFORMAT_LINELEN) {
  398. HexFmtFlush(_this);
  399. fputc('\n', _this->o);
  400. }
  401. }
  402. _this->total += size;
  403. }
  404. #endif
  405. /* Outline Extraction functions */ /*FOLD01*/
  406. /* fills the aw and lsb entries of the TTGlyphMetrics structure from hmtx table -*/
  407. static void GetMetrics(TrueTypeFont *ttf, sal_uInt32 glyphID, TTGlyphMetrics *metrics)
  408. {
  409. const sal_uInt8* table = getTable( ttf, O_hmtx );
  410. metrics->aw = metrics->lsb = metrics->ah = metrics->tsb = 0;
  411. if (!table || !ttf->numberOfHMetrics) return;
  412. if (glyphID < ttf->numberOfHMetrics) {
  413. metrics->aw = GetUInt16(table, 4 * glyphID, 1);
  414. metrics->lsb = GetInt16(table, 4 * glyphID + 2, 1);
  415. } else {
  416. metrics->aw = GetUInt16(table, 4 * (ttf->numberOfHMetrics - 1), 1);
  417. metrics->lsb = GetInt16(table + ttf->numberOfHMetrics * 4, (glyphID - ttf->numberOfHMetrics) * 2, 1);
  418. }
  419. table = getTable(ttf, O_vmtx);
  420. if( !table || !ttf->numOfLongVerMetrics )
  421. return;
  422. if (glyphID < ttf->numOfLongVerMetrics) {
  423. metrics->ah = GetUInt16(table, 4 * glyphID, 1);
  424. metrics->tsb = GetInt16(table, 4 * glyphID + 2, 1);
  425. } else {
  426. metrics->ah = GetUInt16(table, 4 * (ttf->numOfLongVerMetrics - 1), 1);
  427. metrics->tsb = GetInt16(table + ttf->numOfLongVerMetrics * 4, (glyphID - ttf->numOfLongVerMetrics) * 2, 1);
  428. }
  429. }
  430. static int GetTTGlyphOutline(TrueTypeFont *, sal_uInt32 , ControlPoint **, TTGlyphMetrics *, std::vector< sal_uInt32 >* );
  431. /* returns the number of control points, allocates the pointArray */
  432. static int GetSimpleTTOutline(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray, TTGlyphMetrics *metrics) /*FOLD02*/
  433. {
  434. const sal_uInt8* table = getTable( ttf, O_glyf );
  435. sal_uInt8 flag, n;
  436. sal_uInt16 t, lastPoint=0;
  437. int i, j, z;
  438. *pointArray = 0;
  439. /* printf("GetSimpleTTOutline(%d)\n", glyphID); */
  440. if( glyphID >= ttf->nglyphs ) /*- glyph is not present in the font */
  441. return 0;
  442. const sal_uInt8* ptr = table + ttf->goffsets[glyphID];
  443. const sal_Int16 numberOfContours = GetInt16(ptr, 0, 1);
  444. if( numberOfContours <= 0 ) /*- glyph is not simple */
  445. return 0;
  446. if (metrics) { /*- GetCompoundTTOutline() calls this function with NULL metrics -*/
  447. metrics->xMin = GetInt16(ptr, 2, 1);
  448. metrics->yMin = GetInt16(ptr, 4, 1);
  449. metrics->xMax = GetInt16(ptr, 6, 1);
  450. metrics->yMax = GetInt16(ptr, 8, 1);
  451. GetMetrics(ttf, glyphID, metrics);
  452. }
  453. /* determine the last point and be extra safe about it. But probably this code is not needed */
  454. for (i=0; i<numberOfContours; i++) {
  455. if ((t = GetUInt16(ptr, 10+i*2, 1)) > lastPoint) lastPoint = t;
  456. }
  457. sal_uInt16 instLen = GetUInt16(ptr, 10 + numberOfContours*2, 1);
  458. const sal_uInt8* p = ptr + 10 + 2 * numberOfContours + 2 + instLen;
  459. ControlPoint* pa = (ControlPoint*)calloc(lastPoint+1, sizeof(ControlPoint));
  460. i = 0;
  461. while (i <= lastPoint) {
  462. pa[i++].flags = (sal_uInt32) (flag = *p++);
  463. if (flag & 8) { /*- repeat flag */
  464. n = *p++;
  465. for (j=0; j<n; j++) {
  466. if (i > lastPoint) { /*- if the font is really broken */
  467. free(pa);
  468. return 0;
  469. }
  470. pa[i++].flags = flag;
  471. }
  472. }
  473. }
  474. /*- Process the X coordinate */
  475. z = 0;
  476. for (i = 0; i <= lastPoint; i++) {
  477. if (pa[i].flags & 0x02) {
  478. if (pa[i].flags & 0x10) {
  479. z += (int) (*p++);
  480. } else {
  481. z -= (int) (*p++);
  482. }
  483. } else if ( !(pa[i].flags & 0x10)) {
  484. z += GetInt16(p, 0, 1);
  485. p += 2;
  486. }
  487. pa[i].x = (sal_Int16)z;
  488. }
  489. /*- Process the Y coordinate */
  490. z = 0;
  491. for (i = 0; i <= lastPoint; i++) {
  492. if (pa[i].flags & 0x04) {
  493. if (pa[i].flags & 0x20) {
  494. z += *p++;
  495. } else {
  496. z -= *p++;
  497. }
  498. } else if ( !(pa[i].flags & 0x20)) {
  499. z += GetInt16(p, 0, 1);
  500. p += 2;
  501. }
  502. pa[i].y = (sal_Int16)z;
  503. }
  504. for (i=0; i<numberOfContours; i++) {
  505. pa[GetUInt16(ptr, 10 + i * 2, 1)].flags |= 0x00008000; /*- set the end contour flag */
  506. }
  507. *pointArray = pa;
  508. return lastPoint + 1;
  509. }
  510. static int GetCompoundTTOutline(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray, TTGlyphMetrics *metrics, std::vector< sal_uInt32 >& glyphlist) /*FOLD02*/
  511. {
  512. sal_uInt16 flags, index;
  513. sal_Int16 e, f, numberOfContours;
  514. const sal_uInt8* table = getTable( ttf, O_glyf );
  515. std::vector<ControlPoint> myPoints;
  516. ControlPoint *nextComponent, *pa;
  517. int i, np;
  518. F16Dot16 a = 0x10000, b = 0, c = 0, d = 0x10000, m, n, abs1, abs2, abs3;
  519. *pointArray = 0;
  520. /* printf("GetCompoundTTOutline(%d)\n", glyphID); */
  521. if (glyphID >= ttf->nglyphs) /*- incorrect glyphID */
  522. return 0;
  523. const sal_uInt8* ptr = table + ttf->goffsets[glyphID];
  524. if ((numberOfContours = GetInt16(ptr, 0, 1)) != -1) /*- glyph is not compound */
  525. return 0;
  526. if (metrics) {
  527. metrics->xMin = GetInt16(ptr, 2, 1);
  528. metrics->yMin = GetInt16(ptr, 4, 1);
  529. metrics->xMax = GetInt16(ptr, 6, 1);
  530. metrics->yMax = GetInt16(ptr, 8, 1);
  531. GetMetrics(ttf, glyphID, metrics);
  532. }
  533. ptr += 10;
  534. do {
  535. flags = GetUInt16(ptr, 0, 1);
  536. /* printf("flags: 0x%X\n", flags); */
  537. index = GetUInt16(ptr, 2, 1);
  538. ptr += 4;
  539. if( std::find( glyphlist.begin(), glyphlist.end(), index ) != glyphlist.end() )
  540. {
  541. #if OSL_DEBUG_LEVEL > 1
  542. fprintf(stderr, "Endless loop found in a compound glyph.\n");
  543. fprintf(stderr, "%d -> ", index);
  544. fprintf(stderr," [");
  545. for( std::vector< sal_uInt32 >::const_iterator it = glyphlist.begin();
  546. it != glyphlist.end(); ++it )
  547. {
  548. fprintf( stderr,"%d ", (int) *it );
  549. }
  550. fprintf(stderr,"]\n");
  551. /**/
  552. #endif
  553. }
  554. glyphlist.push_back( index );
  555. if ((np = GetTTGlyphOutline(ttf, index, &nextComponent, 0, &glyphlist)) == 0)
  556. {
  557. /* XXX that probably indicates a corrupted font */
  558. #if OSL_DEBUG_LEVEL > 1
  559. fprintf(stderr, "An empty compound!\n");
  560. /* assert(!"An empty compound"); */
  561. #endif
  562. }
  563. if( ! glyphlist.empty() )
  564. glyphlist.pop_back();
  565. if (flags & USE_MY_METRICS) {
  566. if (metrics) GetMetrics(ttf, index, metrics);
  567. }
  568. if (flags & ARG_1_AND_2_ARE_WORDS) {
  569. e = GetInt16(ptr, 0, 1);
  570. f = GetInt16(ptr, 2, 1);
  571. /* printf("ARG_1_AND_2_ARE_WORDS: %d %d\n", e & 0xFFFF, f & 0xFFFF); */
  572. ptr += 4;
  573. } else {
  574. if (flags & ARGS_ARE_XY_VALUES) { /* args are signed */
  575. e = (sal_Int8) *ptr++;
  576. f = (sal_Int8) *ptr++;
  577. /* printf("ARGS_ARE_XY_VALUES: %d %d\n", e & 0xFF, f & 0xFF); */
  578. } else { /* args are unsigned */
  579. /* printf("!ARGS_ARE_XY_VALUES\n"); */
  580. e = *ptr++;
  581. f = *ptr++;
  582. }
  583. }
  584. a = d = 0x10000;
  585. b = c = 0;
  586. if (flags & WE_HAVE_A_SCALE) {
  587. a = GetInt16(ptr, 0, 1) << 2;
  588. d = a;
  589. ptr += 2;
  590. } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
  591. a = GetInt16(ptr, 0, 1) << 2;
  592. d = GetInt16(ptr, 2, 1) << 2;
  593. ptr += 4;
  594. } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
  595. a = GetInt16(ptr, 0, 1) << 2;
  596. b = GetInt16(ptr, 2, 1) << 2;
  597. c = GetInt16(ptr, 4, 1) << 2;
  598. d = GetInt16(ptr, 6, 1) << 2;
  599. ptr += 8;
  600. }
  601. abs1 = (a < 0) ? -a : a;
  602. abs2 = (b < 0) ? -b : b;
  603. m = (abs1 > abs2) ? abs1 : abs2;
  604. abs3 = abs1 - abs2;
  605. if (abs3 < 0) abs3 = -abs3;
  606. if (abs3 <= 33) m *= 2;
  607. abs1 = (c < 0) ? -c : c;
  608. abs2 = (d < 0) ? -d : d;
  609. n = (abs1 > abs2) ? abs1 : abs2;
  610. abs3 = abs1 - abs2;
  611. if (abs3 < 0) abs3 = -abs3;
  612. if (abs3 <= 33) n *= 2;
  613. if (!ARGS_ARE_XY_VALUES) { /* match the points */
  614. assert(!"ARGS_ARE_XY_VALUES is not implemented!!!\n");
  615. }
  616. for (i=0; i<np; i++) {
  617. F16Dot16 t;
  618. ControlPoint cp;
  619. cp.flags = nextComponent[i].flags;
  620. t = fixedMulDiv(a, nextComponent[i].x << 16, m) + fixedMulDiv(c, nextComponent[i].y << 16, m) + (e << 16);
  621. cp.x = (sal_Int16)(fixedMul(t, m) >> 16);
  622. t = fixedMulDiv(b, nextComponent[i].x << 16, n) + fixedMulDiv(d, nextComponent[i].y << 16, n) + (f << 16);
  623. cp.y = (sal_Int16)(fixedMul(t, n) >> 16);
  624. myPoints.push_back( cp );
  625. }
  626. free(nextComponent);
  627. } while (flags & MORE_COMPONENTS);
  628. np = myPoints.size();
  629. pa = (ControlPoint*)calloc(np, sizeof(ControlPoint));
  630. assert(pa != 0);
  631. memcpy( pa, &myPoints[0], np*sizeof(ControlPoint) );
  632. *pointArray = pa;
  633. return np;
  634. }
  635. /* NOTE: GetTTGlyphOutline() returns -1 if the glyphID is incorrect,
  636. * but Get{Simple|Compound}GlyphOutline returns 0 in such a case.
  637. *
  638. * NOTE: glyphlist is the stack of glyphs traversed while constructing
  639. * a composite glyph. This is a safequard against endless recursion
  640. * in corrupted fonts.
  641. */
  642. static int GetTTGlyphOutline(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray, TTGlyphMetrics *metrics, std::vector< sal_uInt32 >* glyphlist)
  643. {
  644. const sal_uInt8 *table = getTable( ttf, O_glyf );
  645. sal_Int16 numberOfContours;
  646. int res;
  647. *pointArray = 0;
  648. if (metrics) {
  649. memset(metrics, 0, sizeof(TTGlyphMetrics)); /*- metrics is initialized to all zeroes */
  650. }
  651. if (glyphID >= ttf->nglyphs) return -1; /**/
  652. const sal_uInt8* ptr = table + ttf->goffsets[glyphID];
  653. int length = ttf->goffsets[glyphID+1] - ttf->goffsets[glyphID];
  654. if (length == 0) { /*- empty glyphs still have hmtx and vmtx metrics values */
  655. if (metrics) GetMetrics(ttf, glyphID, metrics);
  656. return 0;
  657. }
  658. numberOfContours = GetInt16(ptr, 0, 1);
  659. if (numberOfContours >= 0)
  660. {
  661. res=GetSimpleTTOutline(ttf, glyphID, pointArray, metrics);
  662. }
  663. else
  664. {
  665. std::vector< sal_uInt32 > aPrivList;
  666. aPrivList.push_back( glyphID );
  667. res = GetCompoundTTOutline(ttf, glyphID, pointArray, metrics, glyphlist ? *glyphlist : aPrivList );
  668. }
  669. return res;
  670. }
  671. #ifndef NO_TYPE3
  672. /*- returns the number of items in the path -*/
  673. static int BSplineToPSPath(ControlPoint *srcA, int srcCount, PSPathElement **path)
  674. {
  675. std::vector< PSPathElement > aPathList;
  676. int nPathCount = 0;
  677. PSPathElement p( PS_NOOP );
  678. int x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2, y2, curx, cury;
  679. int lastOff = 0; /*- last point was off-contour */
  680. int scflag = 1; /*- start contour flag */
  681. int ecflag = 0; /*- end contour flag */
  682. int cp = 0; /*- current point */
  683. int StartContour = 0, EndContour = 1;
  684. *path = 0;
  685. /* if (srcCount > 0) for(;;) */
  686. while (srcCount > 0) { /*- srcCount does not get changed inside the loop. */
  687. if (scflag) {
  688. int l = cp;
  689. StartContour = cp;
  690. while (!(srcA[l].flags & 0x8000)) l++;
  691. EndContour = l;
  692. if (StartContour == EndContour) {
  693. if (cp + 1 < srcCount) {
  694. cp++;
  695. continue;
  696. } else {
  697. break;
  698. }
  699. }
  700. p = PSPathElement(PS_MOVETO);
  701. if (!(srcA[cp].flags & 1)) {
  702. if (!(srcA[EndContour].flags & 1)) {
  703. p.x1 = x0 = (srcA[cp].x + srcA[EndContour].x + 1) / 2;
  704. p.y1 = y0 = (srcA[cp].y + srcA[EndContour].y + 1) / 2;
  705. } else {
  706. p.x1 = x0 = srcA[EndContour].x;
  707. p.y1 = y0 = srcA[EndContour].y;
  708. }
  709. } else {
  710. p.x1 = x0 = srcA[cp].x;
  711. p.y1 = y0 = srcA[cp].y;
  712. cp++;
  713. }
  714. aPathList.push_back( p );
  715. lastOff = 0;
  716. scflag = 0;
  717. }
  718. curx = srcA[cp].x;
  719. cury = srcA[cp].y;
  720. if (srcA[cp].flags & 1)
  721. {
  722. if (lastOff)
  723. {
  724. p = PSPathElement(PS_CURVETO);
  725. p.x1 = x0 + (2 * (x1 - x0) + 1) / 3;
  726. p.y1 = y0 + (2 * (y1 - y0) + 1) / 3;
  727. p.x2 = x1 + (curx - x1 + 1) / 3;
  728. p.y2 = y1 + (cury - y1 + 1) / 3;
  729. p.x3 = curx;
  730. p.y3 = cury;
  731. aPathList.push_back( p );
  732. }
  733. else
  734. {
  735. if (!(x0 == curx && y0 == cury))
  736. { /* eliminate empty lines */
  737. p = PSPathElement(PS_LINETO);
  738. p.x1 = curx;
  739. p.y1 = cury;
  740. aPathList.push_back( p );
  741. }
  742. }
  743. x0 = curx; y0 = cury; lastOff = 0;
  744. }
  745. else
  746. {
  747. if (lastOff)
  748. {
  749. x2 = (x1 + curx + 1) / 2;
  750. y2 = (y1 + cury + 1) / 2;
  751. p = PSPathElement(PS_CURVETO);
  752. p.x1 = x0 + (2 * (x1 - x0) + 1) / 3;
  753. p.y1 = y0 + (2 * (y1 - y0) + 1) / 3;
  754. p.x2 = x1 + (x2 - x1 + 1) / 3;
  755. p.y2 = y1 + (y2 - y1 + 1) / 3;
  756. p.x3 = x2;
  757. p.y3 = y2;
  758. aPathList.push_back( p );
  759. x0 = x2; y0 = y2;
  760. x1 = curx; y1 = cury;
  761. } else {
  762. x1 = curx; y1 = cury;
  763. }
  764. lastOff = true;
  765. }
  766. if (ecflag) {
  767. aPathList.push_back( PSPathElement(PS_CLOSEPATH) );
  768. scflag = 1;
  769. ecflag = 0;
  770. cp = EndContour + 1;
  771. if (cp >= srcCount) break;
  772. continue;
  773. }
  774. if (cp == EndContour) {
  775. cp = StartContour;
  776. ecflag = true;
  777. } else {
  778. cp++;
  779. }
  780. }
  781. if( (nPathCount = (int)aPathList.size()) > 0)
  782. {
  783. *path = (PSPathElement*)calloc(nPathCount, sizeof(PSPathElement));
  784. assert(*path != 0);
  785. memcpy( *path, &aPathList[0], nPathCount * sizeof(PSPathElement) );
  786. }
  787. return nPathCount;
  788. }
  789. #endif
  790. /*- Extracts a string from the name table and allocates memory for it -*/
  791. static char *nameExtract( const sal_uInt8* name, int nTableSize, int n, int dbFlag, sal_uInt16** ucs2result )
  792. {
  793. char *res;
  794. const sal_uInt8* ptr = name + GetUInt16(name, 4, 1) + GetUInt16(name + 6, 12 * n + 10, 1);
  795. int len = GetUInt16(name+6, 12 * n + 8, 1);
  796. // sanity check
  797. if( (len <= 0) || ((ptr+len) > (name+nTableSize)) )
  798. {
  799. if( ucs2result )
  800. *ucs2result = NULL;
  801. return NULL;
  802. }
  803. if( ucs2result )
  804. *ucs2result = NULL;
  805. if (dbFlag) {
  806. res = (char*)malloc(1 + len/2);
  807. assert(res != 0);
  808. for (int i = 0; i < len/2; i++)
  809. res[i] = *(ptr + i * 2 + 1);
  810. res[len/2] = 0;
  811. if( ucs2result )
  812. {
  813. *ucs2result = (sal_uInt16*)malloc( len+2 );
  814. for (int i = 0; i < len/2; i++ )
  815. (*ucs2result)[i] = GetUInt16( ptr, 2*i, 1 );
  816. (*ucs2result)[len/2] = 0;
  817. }
  818. } else {
  819. res = (char*)malloc(1 + len);
  820. assert(res != 0);
  821. memcpy(res, ptr, len);
  822. res[len] = 0;
  823. }
  824. return res;
  825. }
  826. static int findname( const sal_uInt8 *name, sal_uInt16 n, sal_uInt16 platformID,
  827. sal_uInt16 encodingID, sal_uInt16 languageID, sal_uInt16 nameID )
  828. {
  829. int l = 0, r = n-1, i;
  830. sal_uInt32 t1, t2;
  831. sal_uInt32 m1, m2;
  832. if (n == 0) return -1;
  833. m1 = (platformID << 16) | encodingID;
  834. m2 = (languageID << 16) | nameID;
  835. do {
  836. i = (l + r) >> 1;
  837. t1 = GetUInt32(name + 6, i * 12 + 0, 1);
  838. t2 = GetUInt32(name + 6, i * 12 + 4, 1);
  839. if (! ((m1 < t1) || ((m1 == t1) && (m2 < t2)))) l = i + 1;
  840. if (! ((m1 > t1) || ((m1 == t1) && (m2 > t2)))) r = i - 1;
  841. } while (l <= r);
  842. if (l - r == 2) {
  843. return l - 1;
  844. }
  845. return -1;
  846. }
  847. /* XXX marlett.ttf uses (3, 0, 1033) instead of (3, 1, 1033) and does not have any Apple tables.
  848. * Fix: if (3, 1, 1033) is not found - need to check for (3, 0, 1033)
  849. *
  850. * /d/fonts/ttzh_tw/Big5/Hanyi/ma6b5p uses (1, 0, 19) for English strings, instead of (1, 0, 0)
  851. * and does not have (3, 1, 1033)
  852. * Fix: if (1, 0, 0) and (3, 1, 1033) are not found need to look for (1, 0, *) - that will
  853. * require a change in algorithm
  854. *
  855. * /d/fonts/fdltest/Korean/h2drrm has unsorted names and a an unknown (to me) Mac LanguageID,
  856. * but (1, 0, 1042) strings usable
  857. * Fix: change algorithm, and use (1, 0, *) if both standard Mac and MS strings are not found
  858. */
  859. static void GetNames(TrueTypeFont *t)
  860. {
  861. const sal_uInt8* table = getTable( t, O_name );
  862. int nTableSize = getTableSize(t, O_name);
  863. if (nTableSize < 4)
  864. {
  865. #if OSL_DEBUG_LEVEL > 1
  866. fprintf(stderr, "O_name table too small\n");
  867. #endif
  868. return;
  869. }
  870. sal_uInt16 n = GetUInt16(table, 2, 1);
  871. int i, r;
  872. sal_Bool bPSNameOK = sal_True;
  873. /* #129743# simple sanity check for name table entry count */
  874. if( nTableSize <= n * 12 + 6 )
  875. n = 0;
  876. /* PostScript name: preferred Microsoft */
  877. t->psname = NULL;
  878. if ((r = findname(table, n, 3, 1, 0x0409, 6)) != -1)
  879. t->psname = nameExtract(table, nTableSize, r, 1, NULL);
  880. if ( ! t->psname && (r = findname(table, n, 1, 0, 0, 6)) != -1)
  881. t->psname = nameExtract(table, nTableSize, r, 0, NULL);
  882. if ( ! t->psname && (r = findname(table, n, 3, 0, 0x0409, 6)) != -1)
  883. {
  884. // some symbol fonts like Marlett have a 3,0 name!
  885. t->psname = nameExtract(table, nTableSize, r, 1, NULL);
  886. }
  887. // for embedded font in Ghostscript PDFs
  888. if ( ! t->psname && (r = findname(table, n, 2, 2, 0, 6)) != -1)
  889. {
  890. t->psname = nameExtract(table, nTableSize, r, 0, NULL);
  891. }
  892. if ( ! t->psname )
  893. {
  894. if ( t->fname )
  895. {
  896. char* pReverse = t->fname + strlen(t->fname);
  897. /* take only last token of filename */
  898. while(pReverse != t->fname && *pReverse != '/') pReverse--;
  899. if(*pReverse == '/') pReverse++;
  900. t->psname = strdup(pReverse);
  901. assert(t->psname != 0);
  902. for (i=strlen(t->psname) - 1; i > 0; i--)
  903. {
  904. /*- Remove the suffix -*/
  905. if (t->psname[i] == '.' ) {
  906. t->psname[i] = 0;
  907. break;
  908. }
  909. }
  910. }
  911. else
  912. t->psname = strdup( "Unknown" );
  913. }
  914. /* Font family and subfamily names: preferred Apple */
  915. t->family = NULL;
  916. if ((r = findname(table, n, 0, 0, 0, 1)) != -1)
  917. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  918. if ( ! t->family && (r = findname(table, n, 3, 1, 0x0409, 1)) != -1)
  919. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  920. if ( ! t->family && (r = findname(table, n, 1, 0, 0, 1)) != -1)
  921. t->family = nameExtract(table, nTableSize, r, 0, NULL);
  922. if ( ! t->family && (r = findname(table, n, 3, 1, 0x0411, 1)) != -1)
  923. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  924. if ( ! t->family && (r = findname(table, n, 3, 0, 0x0409, 1)) != -1)
  925. t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
  926. if ( ! t->family )
  927. {
  928. t->family = strdup(t->psname);
  929. assert(t->family != 0);
  930. }
  931. t->subfamily = NULL;
  932. t->usubfamily = NULL;
  933. if ((r = findname(table, n, 1, 0, 0, 2)) != -1)
  934. t->subfamily = nameExtract(table, nTableSize, r, 0, &t->usubfamily);
  935. if ( ! t->subfamily && (r = findname(table, n, 3, 1, 0x0409, 2)) != -1)
  936. t->subfamily = nameExtract(table, nTableSize, r, 1, &t->usubfamily);
  937. if ( ! t->subfamily )
  938. {
  939. t->subfamily = strdup("");
  940. }
  941. /* #i60349# sanity check psname
  942. * psname parctically has to be 7bit ascii and should not contains spaces
  943. * there is a class of broken fonts which do not fullfill that at all, so let's try
  944. * if the family name is 7bit ascii and take it instead if so
  945. */
  946. /* check psname */
  947. for( i = 0; t->psname[i] != 0 && bPSNameOK; i++ )
  948. if( t->psname[ i ] < 33 || (t->psname[ i ] & 0x80) )
  949. bPSNameOK = sal_False;
  950. if( bPSNameOK == sal_False )
  951. {
  952. sal_Bool bReplace = sal_True;
  953. /* check if family is a suitable replacement */
  954. if( t->ufamily && t->family )
  955. {
  956. for( i = 0; t->ufamily[ i ] != 0 && bReplace; i++ )
  957. if( t->ufamily[ i ] < 33 || t->ufamily[ i ] > 127 )
  958. bReplace = sal_False;
  959. if( bReplace )
  960. {
  961. free( t->psname );
  962. t->psname = strdup( t->family );
  963. }
  964. }
  965. }
  966. }
  967. enum cmapType {
  968. CMAP_NOT_USABLE = -1,
  969. CMAP_MS_Symbol = 10,
  970. CMAP_MS_Unicode = 11,
  971. CMAP_MS_ShiftJIS = 12,
  972. CMAP_MS_Big5 = 13,
  973. CMAP_MS_PRC = 14,
  974. CMAP_MS_Wansung = 15,
  975. CMAP_MS_Johab = 16
  976. };
  977. #define MISSING_GLYPH_INDEX 0
  978. /*
  979. * getGlyph[0246]() functions and freinds are implemented by:
  980. * @author Manpreet Singh
  981. * getGlyph12() function and friends by:
  982. * @author HDU
  983. */
  984. static sal_uInt32 getGlyph0(const sal_uInt8* cmap, sal_uInt32 c) {
  985. if (c <= 255) {
  986. return *(cmap + 6 + c);
  987. } else {
  988. return MISSING_GLYPH_INDEX;
  989. }
  990. }
  991. typedef struct _subHeader2 {
  992. sal_uInt16 firstCode;
  993. sal_uInt16 entryCount;
  994. sal_uInt16 idDelta;
  995. sal_uInt16 idRangeOffset;
  996. } subHeader2;
  997. static sal_uInt32 getGlyph2(const sal_uInt8 *cmap, sal_uInt32 c) {
  998. sal_uInt16 *CMAP2 = (sal_uInt16 *) cmap;
  999. sal_uInt8 theHighByte;
  1000. sal_uInt8 theLowByte;
  1001. subHeader2* subHeader2s;
  1002. sal_uInt16* subHeader2Keys;
  1003. sal_uInt16 firstCode;
  1004. int k;
  1005. sal_uInt32 ToReturn;
  1006. theHighByte = (sal_uInt8)((c >> 8) & 0x00ff);
  1007. theLowByte = (sal_uInt8)(c & 0x00ff);
  1008. subHeader2Keys = CMAP2 + 3;
  1009. subHeader2s = (subHeader2 *)(subHeader2Keys + 256);
  1010. k = Int16FromMOTA(subHeader2Keys[theHighByte]) / 8;
  1011. if(k == 0) {
  1012. firstCode = Int16FromMOTA(subHeader2s[k].firstCode);
  1013. if(theLowByte >= firstCode && theLowByte < (firstCode + Int16FromMOTA(subHeader2s[k].entryCount))) {
  1014. return *((&(subHeader2s[0].idRangeOffset))
  1015. + (Int16FromMOTA(subHeader2s[0].idRangeOffset)/2) /* + offset */
  1016. + theLowByte /* + to_look */
  1017. - Int16FromMOTA(subHeader2s[0].firstCode)
  1018. );
  1019. } else {
  1020. return MISSING_GLYPH_INDEX;
  1021. }
  1022. } else if (k > 0) {
  1023. firstCode = Int16FromMOTA(subHeader2s[k].firstCode);
  1024. if(theLowByte >= firstCode && theLowByte < (firstCode + Int16FromMOTA(subHeader2s[k].entryCount))) {
  1025. ToReturn = *((&(subHeader2s[k].idRangeOffset))
  1026. + (Int16FromMOTA(subHeader2s[k].idRangeOffset)/2)
  1027. + theLowByte - firstCode);
  1028. if(ToReturn == 0) {
  1029. return MISSING_GLYPH_INDEX;
  1030. } else {
  1031. ToReturn += Int16FromMOTA(subHeader2s[k].idDelta);
  1032. return (ToReturn & 0xFFFF);
  1033. }
  1034. } else {
  1035. return MISSING_GLYPH_INDEX;
  1036. }
  1037. } else {
  1038. return MISSING_GLYPH_INDEX;
  1039. }
  1040. }
  1041. static sal_uInt32 getGlyph6(const sal_uInt8 *cmap, sal_uInt32 c) {
  1042. sal_uInt16 firstCode, lastCode, count;
  1043. sal_uInt16 *CMAP6 = (sal_uInt16 *) cmap;
  1044. firstCode = Int16FromMOTA(*(CMAP6 + 3));
  1045. count = Int16FromMOTA(*(CMAP6 + 4));
  1046. lastCode = firstCode + count - 1;
  1047. if (c < firstCode || c > lastCode) {
  1048. return MISSING_GLYPH_INDEX;
  1049. } else {
  1050. return *((CMAP6 + 5)/*glyphIdArray*/ + (c - firstCode));
  1051. }
  1052. }
  1053. static sal_uInt16 GEbinsearch(sal_uInt16 *ar, sal_uInt16 length, sal_uInt16 toSearch) {
  1054. signed int low, mid, high, lastfound = 0xffff;
  1055. sal_uInt16 res;
  1056. if(length == (sal_uInt16)0 || length == (sal_uInt16)0xFFFF) {
  1057. return (sal_uInt16)0xFFFF;
  1058. }
  1059. low = 0;
  1060. high = length - 1;
  1061. while(high >= low) {
  1062. mid = (high + low)/2;
  1063. res = Int16FromMOTA(*(ar+mid));
  1064. if(res >= toSearch) {
  1065. lastfound = mid;
  1066. high = --mid;
  1067. } else {
  1068. low = ++mid;
  1069. }
  1070. }
  1071. return (sal_uInt16)lastfound;
  1072. }
  1073. static sal_uInt32 getGlyph4(const sal_uInt8 *cmap, sal_uInt32 c) {
  1074. sal_uInt16 i;
  1075. int ToReturn;
  1076. sal_uInt16 segCount;
  1077. sal_uInt16 * startCode;
  1078. sal_uInt16 * endCode;
  1079. sal_uInt16 * idDelta;
  1080. /* sal_uInt16 * glyphIdArray; */
  1081. sal_uInt16 * idRangeOffset;
  1082. /*sal_uInt16 * glyphIndexArray;*/
  1083. sal_uInt16 *CMAP4 = (sal_uInt16 *) cmap;
  1084. /* sal_uInt16 GEbinsearch(sal_uInt16 *ar, sal_uInt16 length, sal_uInt16 toSearch); */
  1085. segCount = Int16FromMOTA(*(CMAP4 + 3))/2;
  1086. endCode = CMAP4 + 7;
  1087. i = GEbinsearch(endCode, segCount, (sal_uInt16)c);
  1088. if (i == (sal_uInt16) 0xFFFF) {
  1089. return MISSING_GLYPH_INDEX;
  1090. }
  1091. startCode = endCode + segCount + 1;
  1092. if(Int16FromMOTA(startCode[i]) > c) {
  1093. return MISSING_GLYPH_INDEX;
  1094. }
  1095. idDelta = startCode + segCount;
  1096. idRangeOffset = idDelta + segCount;
  1097. /*glyphIndexArray = idRangeOffset + segCount;*/
  1098. if(Int16FromMOTA(idRangeOffset[i]) != 0) {
  1099. c = Int16FromMOTA(*(&(idRangeOffset[i]) + (Int16FromMOTA(idRangeOffset[i])/2 + (c - Int16FromMOTA(startCode[i])))));
  1100. }
  1101. ToReturn = (Int16FromMOTA(idDelta[i]) + c) & 0xFFFF;
  1102. return ToReturn;
  1103. }
  1104. static sal_uInt32 getGlyph12(const sal_uInt8 *pCmap, sal_uInt32 cChar) {
  1105. const sal_uInt32* pCMAP12 = (const sal_uInt32*)pCmap;
  1106. int nLength = Int32FromMOTA( pCMAP12[1] );
  1107. int nGroups = Int32FromMOTA( pCMAP12[3] );
  1108. int nLower = 0;
  1109. int nUpper = nGroups;
  1110. if( nUpper > (nLength-16)/12 )
  1111. nUpper = (nLength-16)/12;
  1112. /* binary search in "segmented coverage" subtable */
  1113. while( nLower < nUpper ) {
  1114. int nIndex = (nLower + nUpper) / 2;
  1115. const sal_uInt32* pEntry = &pCMAP12[ 4 + 3*nIndex ];
  1116. sal_uInt32 cStart = Int32FromMOTA( pEntry[0] );
  1117. sal_uInt32 cLast = Int32FromMOTA( pEntry[1] );
  1118. if( cChar < cStart )
  1119. nUpper = nIndex;
  1120. else if( cChar > cLast )
  1121. nLower = nIndex + 1;
  1122. else { /* found matching entry! */
  1123. sal_uInt32 nGlyph = Int32FromMOTA( pEntry[2] );
  1124. nGlyph += cChar - cStart;
  1125. return nGlyph;
  1126. }
  1127. }
  1128. return MISSING_GLYPH_INDEX;
  1129. }
  1130. static void FindCmap(TrueTypeFont *ttf)
  1131. {
  1132. const sal_uInt8* table = getTable(ttf, O_cmap);
  1133. sal_uInt32 table_size = getTableSize(ttf, O_cmap);
  1134. sal_uInt16 ncmaps = GetUInt16(table, 2, 1);
  1135. unsigned int i;
  1136. sal_uInt32 AppleUni = 0; // Apple Unicode
  1137. sal_uInt32 ThreeZero = 0; /* MS Symbol */
  1138. sal_uInt32 ThreeOne = 0; /* MS UCS-2 */
  1139. sal_uInt32 ThreeTwo = 0; /* MS ShiftJIS */
  1140. sal_uInt32 ThreeThree = 0; /* MS Big5 */
  1141. sal_uInt32 ThreeFour = 0; /* MS PRC */
  1142. sal_uInt32 ThreeFive = 0; /* MS Wansung */
  1143. sal_uInt32 ThreeSix = 0; /* MS Johab */
  1144. for (i = 0; i < ncmaps; i++) {
  1145. sal_uInt32 offset;
  1146. sal_uInt16 pID, eID;
  1147. /* sanity check, cmap entry must lie within table */
  1148. if( i*8+4 > table_size )
  1149. break;
  1150. pID = GetUInt16(table, 4 + i * 8, 1);
  1151. eID = GetUInt16(table, 6 + i * 8, 1);
  1152. offset = GetUInt32(table, 8 + i * 8, 1);
  1153. /* sanity check, cmap must lie within file */
  1154. if( (table - ttf->ptr) + offset > (sal_uInt32)ttf->fsize )
  1155. continue;
  1156. /* Unicode tables in Apple fonts */
  1157. if (pID == 0) {
  1158. AppleUni = offset;
  1159. }
  1160. if (pID == 3) {
  1161. switch (eID) {
  1162. case 0: ThreeZero = offset; break;
  1163. case 10: // UCS-4
  1164. case 1: ThreeOne = offset; break;
  1165. case 2: ThreeTwo = offset; break;
  1166. case 3: ThreeThree = offset; break;
  1167. case 4: ThreeFour = offset; break;
  1168. case 5: ThreeFive = offset; break;
  1169. case 6: ThreeSix = offset; break;
  1170. }
  1171. }
  1172. }
  1173. // fall back to AppleUnicode if there are no ThreeOne/Threezero tables
  1174. if( AppleUni && !ThreeZero && !ThreeOne)
  1175. ThreeOne = AppleUni;
  1176. if (ThreeOne) {
  1177. ttf->cmapType = CMAP_MS_Unicode;
  1178. ttf->cmap = table + ThreeOne;
  1179. } else if (ThreeTwo) {
  1180. ttf->cmapType = CMAP_MS_ShiftJIS;
  1181. ttf->cmap = table + ThreeTwo;
  1182. } else if (ThreeThree) {
  1183. ttf->cmapType = CMAP_MS_Big5;
  1184. ttf->cmap = table + ThreeThree;
  1185. } else if (ThreeFour) {
  1186. ttf->cmapType = CMAP_MS_PRC;
  1187. ttf->cmap = table + ThreeFour;
  1188. } else if (ThreeFive) {
  1189. ttf->cmapType = CMAP_MS_Wansung;
  1190. ttf->cmap = table + ThreeFive;
  1191. } else if (ThreeSix) {
  1192. ttf->cmapType = CMAP_MS_Johab;
  1193. ttf->cmap = table + ThreeSix;
  1194. } else if (ThreeZero) {
  1195. ttf->cmapType = CMAP_MS_Symbol;
  1196. ttf->cmap = table + ThreeZero;
  1197. } else {
  1198. ttf->cmapType = CMAP_NOT_USABLE;
  1199. ttf->cmap = 0;
  1200. }
  1201. if (ttf->cmapType != CMAP_NOT_USABLE) {
  1202. switch (GetUInt16(ttf->cmap, 0, 1)) {
  1203. case 0: ttf->mapper = getGlyph0; break;
  1204. case 2: ttf->mapper = getGlyph2; break;
  1205. case 4: ttf->mapper = getGlyph4; break;
  1206. case 6: ttf->mapper = getGlyph6; break;
  1207. case 12: ttf->mapper= getGlyph12; break;
  1208. default:
  1209. #if OSL_DEBUG_LEVEL > 1
  1210. /*- if the cmap table is really broken */
  1211. printf("%s: %d is not a recognized cmap format.\n", ttf->fname, GetUInt16(ttf->cmap, 0, 1));
  1212. #endif
  1213. ttf->cmapType = CMAP_NOT_USABLE;
  1214. ttf->cmap = 0;
  1215. ttf->mapper = 0;
  1216. }
  1217. }
  1218. }
  1219. static void GetKern(TrueTypeFont *ttf)
  1220. {
  1221. const sal_uInt8* table = getTable(ttf, O_kern);
  1222. const sal_uInt8 *ptr;
  1223. if( !table )
  1224. goto badtable;
  1225. if (GetUInt16(table, 0, 1) == 0) { /* Traditional Microsoft style table with sal_uInt16 version and nTables fields */
  1226. ttf->nkern = GetUInt16(table, 2, 1);
  1227. ttf->kerntables = (const sal_uInt8**)calloc(ttf->nkern, sizeof(sal_uInt8 *));
  1228. assert(ttf->kerntables != 0);
  1229. ttf->kerntype = KT_MICROSOFT;
  1230. ptr = table + 4;
  1231. for( unsigned i = 0; i < ttf->nkern; ++i) {
  1232. ttf->kerntables[i] = ptr;
  1233. ptr += GetUInt16(ptr, 2, 1);
  1234. /* sanity check */
  1235. if( ptr > ttf->ptr+ttf->fsize )
  1236. {
  1237. free( ttf->kerntables );
  1238. goto badtable;
  1239. }
  1240. }
  1241. return;
  1242. }
  1243. if (GetUInt32(table, 0, 1) == 0x00010000) { /* MacOS style kern tables: fixed32 version and sal_uInt32 nTables fields */
  1244. ttf->nkern = GetUInt32(table, 4, 1);
  1245. ttf->kerntables = (const sal_uInt8**)calloc(ttf->nkern, sizeof(sal_uInt8 *));
  1246. assert(ttf->kerntables != 0);
  1247. ttf->kerntype = KT_APPLE_NEW;
  1248. ptr = table + 8;
  1249. for( unsigned i = 0; i < ttf->nkern; ++i) {
  1250. ttf->kerntables[i] = ptr;
  1251. ptr += GetUInt32(ptr, 0, 1);
  1252. /* sanity check; there are some fonts that are broken in this regard */
  1253. if( ptr > ttf->ptr+ttf->fsize )
  1254. {
  1255. free( ttf->kerntables );
  1256. goto badtable;
  1257. }
  1258. }
  1259. return;
  1260. }
  1261. badtable:
  1262. ttf->kerntype = KT_NONE;
  1263. ttf->kerntables = 0;
  1264. return;
  1265. }
  1266. /*- Public functions */ /*FOLD00*/
  1267. int CountTTCFonts(const char* fname)
  1268. {
  1269. int nFonts = 0;
  1270. sal_uInt8 buffer[12];
  1271. FILE* fd = fopen(fname, "rb");
  1272. if( fd ) {
  1273. if (fread(buffer, 1, 12, fd) == 12) {
  1274. if(GetUInt32(buffer, 0, 1) == T_ttcf )
  1275. nFonts = GetUInt32(buffer, 8, 1);
  1276. }
  1277. fclose(fd);
  1278. }
  1279. return nFonts;
  1280. }
  1281. static void allocTrueTypeFont( TrueTypeFont** ttf )
  1282. {
  1283. *ttf = (TrueTypeFont*)calloc(1,sizeof(TrueTypeFont));
  1284. if( *ttf != NULL )
  1285. {
  1286. (*ttf)->tag = 0;
  1287. (*ttf)->fname = 0;
  1288. (*ttf)->fsize = -1;
  1289. (*ttf)->ptr = 0;
  1290. (*ttf)->nglyphs = 0xFFFFFFFF;
  1291. (*ttf)->pGSubstitution = 0;
  1292. }
  1293. }
  1294. /* forward declariotn for the two entry points to use*/
  1295. static int doOpenTTFont( sal_uInt32 facenum, TrueTypeFont* t );
  1296. #if !defined(WIN32)
  1297. int OpenTTFontFile( const char* fname, sal_uInt32 facenum, TrueTypeFont** ttf )
  1298. {
  1299. int ret, fd = -1;
  1300. struct stat st;
  1301. if (!fname || !*fname) return SF_BADFILE;
  1302. allocTrueTypeFont( ttf );
  1303. if( ! *ttf )
  1304. return SF_MEMORY;
  1305. (*ttf)->fname = strdup(fname);
  1306. if( ! (*ttf)->fname )
  1307. {
  1308. ret = SF_MEMORY;
  1309. goto cleanup;
  1310. }
  1311. fd = open(fname, O_RDONLY);
  1312. if (fd == -1) {
  1313. ret = SF_BADFILE;
  1314. goto cleanup;
  1315. }
  1316. if (fstat(fd, &st) == -1) {
  1317. ret = SF_FILEIO;
  1318. goto cleanup;
  1319. }
  1320. (*ttf)->fsize = st.st_size;
  1321. /* On Mac OS, most likely will happen if a Mac user renames a font file
  1322. * to be .ttf when its really a Mac resource-based font.
  1323. * Size will be 0, but fonts smaller than 4 bytes would be broken anyway.
  1324. */
  1325. if ((*ttf)->fsize == 0) {
  1326. ret = SF_BADFILE;
  1327. goto cleanup;
  1328. }
  1329. if (((*ttf)->ptr = (sal_uInt8 *) mmap(0, (*ttf)->fsize, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
  1330. ret = SF_MEMORY;
  1331. goto cleanup;
  1332. }
  1333. close(fd);
  1334. return doOpenTTFont( facenum, *ttf );
  1335. cleanup:
  1336. if (fd != -1) close(fd);
  1337. /*- t and t->fname have been allocated! */
  1338. free((*ttf)->fname);
  1339. free(*ttf);
  1340. *ttf = NULL;
  1341. return ret;
  1342. }
  1343. #endif
  1344. int OpenTTFontBuffer(void* pBuffer, sal_uInt32 nLen, sal_uInt32 facenum, TrueTypeFont** ttf)
  1345. {
  1346. allocTrueTypeFont( ttf );
  1347. if( *ttf == NULL )
  1348. return SF_MEMORY;
  1349. (*ttf)->fname = NULL;
  1350. (*ttf)->fsize = nLen;
  1351. (*ttf)->ptr = (sal_uInt8*)pBuffer;
  1352. return doOpenTTFont( facenum, *ttf );
  1353. }
  1354. static int doOpenTTFont( sal_uInt32 facenum, TrueTypeFont* t )
  1355. {
  1356. int i;
  1357. sal_uInt32 length, tag;
  1358. sal_uInt32 tdoffset = 0; /* offset to TableDirectory in a TTC file. For TTF files is 0 */
  1359. int indexfmt;
  1360. sal_uInt32 version = GetInt32(t->ptr, 0, 1);
  1361. if ((version == 0x00010000) || (version == T_true)) {
  1362. tdoffset = 0;
  1363. } else if (version == T_otto) { /* PS-OpenType font */
  1364. tdoffset = 0;
  1365. } else if (version == T_ttcf) { /* TrueType collection */
  1366. if (GetUInt32(t->ptr, 4, 1) != 0x00010000) {
  1367. CloseTTFont(t);
  1368. return SF_TTFORMAT;
  1369. }
  1370. if (facenum >= GetUInt32(t->ptr, 8, 1)) {
  1371. CloseTTFont(t);
  1372. return SF_FONTNO;
  1373. }
  1374. tdoffset = GetUInt32(t->ptr, 12 + 4 * facenum, 1);
  1375. } else {
  1376. CloseTTFont(t);
  1377. return SF_TTFORMAT;
  1378. }
  1379. /* magic number */
  1380. t->tag = TTFontClassTag;
  1381. t->ntables = GetUInt16(t->ptr + tdoffset, 4, 1);
  1382. if( t->ntables >= 128 )
  1383. return SF_TTFORMAT;
  1384. t->tables = (const sal_uInt8**)calloc(NUM_TAGS, sizeof(sal_uInt8 *));
  1385. assert(t->tables != 0);
  1386. t->tlens = (sal_uInt32*)calloc(NUM_TAGS, sizeof(sal_uInt32));
  1387. assert(t->tlens != 0);
  1388. /* parse the tables */
  1389. for (i=0; i<(int)t->ntables; i++) {
  1390. int nIndex;
  1391. tag = GetUInt32(t->ptr + tdoffset + 12, 16 * i, 1);
  1392. switch( tag ) {
  1393. case T_maxp: nIndex = O_maxp; break;
  1394. case T_glyf: nIndex = O_glyf; break;
  1395. case T_head: nIndex = O_head; break;
  1396. case T_loca: nIndex = O_loca; break;
  1397. case T_name: nIndex = O_name; break;
  1398. case T_hhea: nIndex = O_hhea; break;
  1399. case T_hmtx: nIndex = O_hmtx; break;
  1400. case T_cmap: nIndex = O_cmap; break;
  1401. case T_vhea: nIndex = O_vhea; break;
  1402. case T_vmtx: nIndex = O_vmtx; break;
  1403. case T_OS2 : nIndex = O_OS2; break;
  1404. case T_post: nIndex = O_post; break;
  1405. case T_kern: nIndex = O_kern; break;
  1406. case T_cvt : nIndex = O_cvt; break;
  1407. case T_prep: nIndex = O_prep; break;
  1408. case T_fpgm: nIndex = O_fpgm; break;
  1409. case T_gsub: nIndex = O_gsub; break;
  1410. case T_CFF: nIndex = O_CFF; break;
  1411. default: nIndex = -1; break;
  1412. }
  1413. if( nIndex >= 0 ) {
  1414. sal_uInt32 nTableOffset = GetUInt32(t->ptr + tdoffset + 12, 16 * i + 8, 1);
  1415. length = GetUInt32(t->ptr + tdoffset + 12, 16 * i + 12, 1);
  1416. t->tables[nIndex] = t->ptr + nTableOffset;
  1417. t->tlens[nIndex] = length;
  1418. }
  1419. }
  1420. /* Fixup offsets when only a TTC extract was provided */
  1421. if( facenum == (sal_uInt32)~0 ) {
  1422. sal_uInt8* pHead = (sal_uInt8*)t->tables[O_head];
  1423. if( !pHead )
  1424. return SF_TTFORMAT;
  1425. /* limit Head candidate to TTC extract's limits */
  1426. if( pHead > t->ptr + (t->fsize - 54) )
  1427. pHead = t->ptr + (t->fsize - 54);
  1428. /* TODO: find better method than searching head table's magic */
  1429. sal_uInt8* p = NULL;
  1430. for( p = pHead + 12; p > t->ptr; --p ) {
  1431. if( p[0]==0x5F && p[1]==0x0F && p[2]==0x3C && p[3]==0xF5 ) {
  1432. int nDelta = (pHead + 12) - p;
  1433. if( nDelta )
  1434. for( int j = 0; j < NUM_TAGS; ++j )
  1435. if( t->tables[j] )
  1436. *(char**)&t->tables[j] -= nDelta;
  1437. break;
  1438. }
  1439. }
  1440. if( p <= t->ptr )
  1441. return SF_TTFORMAT;
  1442. }
  1443. /* Check the table offsets after TTC correction */
  1444. for (i=0; i<NUM_TAGS; i++) {
  1445. /* sanity check: table must lay completely within the file
  1446. * at this point one could check the checksum of all contained
  1447. * tables, but this would be quite time intensive.
  1448. * Try to fix tables, so we can cope with minor problems.
  1449. */
  1450. if( (sal_uInt8*)t->tables[i] < t->ptr )
  1451. {
  1452. #if OSL_DEBUG_LEVEL > 1
  1453. if( t->tables[i] )
  1454. fprintf( stderr, "font file %s has bad table offset %" SAL_PRI_PTRDIFFT "d (tagnum=%d)\n", t->fname, (sal_uInt8*)t->tables[i]-t->ptr, i );
  1455. #endif
  1456. t->tlens[i] = 0;
  1457. t->tables[i] = NULL;
  1458. }
  1459. else if( (sal_uInt8*)t->tables[i] + t->tlens[i] > t->ptr + t->fsize )
  1460. {
  1461. int nMaxLen = (t->ptr + t->fsize) - (sal_uInt8*)t->tables[i];
  1462. if( nMaxLen < 0 )
  1463. nMaxLen = 0;
  1464. t->tlens[i] = nMaxLen;
  1465. #if OSL_DEBUG_LEVEL > 1
  1466. fprintf( stderr, "font file %s has too big table (tagnum=%d)\n", t->fname, i );
  1467. #endif
  1468. }
  1469. }
  1470. /* At this point TrueTypeFont is constructed, now need to verify the font format
  1471. and read the basic font properties */
  1472. /* The following tables are absolutely required:
  1473. * maxp, head, name, cmap
  1474. */
  1475. if( !(getTable(t, O_maxp) && getTable(t, O_head) && getTable(t, O_name) && getTable(t, O_cmap)) ) {
  1476. CloseTTFont(t);
  1477. return SF_TTFORMAT;
  1478. }
  1479. const sal_uInt8* table = getTable(t, O_maxp);
  1480. t->nglyphs = GetUInt16(table, 4, 1);
  1481. table = getTable(t, O_head);
  1482. t->unitsPerEm = GetUInt16(table, 18, 1);
  1483. indexfmt = GetInt16(table, 50, 1);
  1484. if( ((indexfmt != 0) && (indexfmt != 1)) || (t->unitsPerEm <= 0) ) {
  1485. CloseTTFont(t);
  1486. return SF_TTFORMAT;
  1487. }
  1488. if( getTable(t, O_glyf) && getTable(t, O_loca) ) /* TTF or TTF-OpenType */
  1489. {
  1490. int k = (getTableSize(t, O_loca) / (indexfmt ? 4 : 2)) - 1;
  1491. if( k < (int)t->nglyphs ) /* Hack for broken Chinese fonts */
  1492. t->nglyphs = k;
  1493. table = getTable(t, O_loca);
  1494. t->goffsets = (sal_uInt32 *) calloc(1+t->nglyphs, sizeof(sal_uInt32));
  1495. assert(t->goffsets != 0);
  1496. for( i = 0; i <= (int)t->nglyphs; ++i )
  1497. t->goffsets[i] = indexfmt ? GetUInt32(table, i << 2, 1) : (sal_uInt32)GetUInt16(table, i << 1, 1) << 1;
  1498. } else if( getTable(t, O_CFF) ) { /* PS-OpenType */
  1499. t->goffsets = (sal_uInt32 *) calloc(1+t->nglyphs, sizeof(sal_uInt32));
  1500. /* TODO: implement to get subsetting */
  1501. assert(t->goffsets != 0);
  1502. } else {
  1503. CloseTTFont(t);
  1504. return SF_TTFORMAT;
  1505. }
  1506. table = getTable(t, O_hhea);
  1507. t->numberOfHMetrics = (table != 0) ? GetUInt16(table, 34, 1) : 0;
  1508. table = getTable(t, O_vhea);
  1509. t->numOfLongVerMetrics = (table != 0) ? GetUInt16(table, 34, 1) : 0;
  1510. GetNames(t);
  1511. FindCmap(t);
  1512. GetKern(t);
  1513. ReadGSUB( t, 0, 0 );
  1514. return SF_OK;
  1515. }
  1516. void CloseTTFont(TrueTypeFont *ttf) /*FOLD01*/
  1517. {
  1518. #if !defined(WIN32)
  1519. if( ttf->fname )
  1520. munmap((char *) ttf->ptr, ttf->fsize);
  1521. #endif
  1522. free(ttf->fname);
  1523. free(ttf->goffsets);
  1524. free(ttf->psname);
  1525. free(ttf->family);
  1526. if( ttf->ufamily )
  1527. free( ttf->ufamily );
  1528. free(ttf->subfamily);
  1529. if( ttf->usubfamily )
  1530. free( ttf->usubfamily );
  1531. free(ttf->tables);
  1532. free(ttf->tlens);
  1533. free(ttf->kerntables);
  1534. ReleaseGSUB(ttf);
  1535. free(ttf);
  1536. return;
  1537. }
  1538. int GetTTGlyphPoints(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray)
  1539. {
  1540. return GetTTGlyphOutline(ttf, glyphID, pointArray, 0, 0);
  1541. }
  1542. int GetTTGlyphComponents(TrueTypeFont *ttf, sal_uInt32 glyphID, std::vector< sal_uInt32 >& glyphlist)
  1543. {
  1544. int n = 1;
  1545. if( glyphID >= ttf->nglyphs )
  1546. return 0;
  1547. const sal_uInt8* glyf = getTable(ttf, O_glyf);
  1548. const sal_uInt8* ptr = glyf + ttf->goffsets[glyphID];
  1549. glyphlist.push_back( glyphID );
  1550. if (GetInt16(ptr, 0, 1) == -1) {
  1551. sal_uInt16 flags, index;
  1552. ptr += 10;
  1553. do {
  1554. flags = GetUInt16(ptr, 0, 1);
  1555. index = GetUInt16(ptr, 2, 1);
  1556. ptr += 4;
  1557. n += GetTTGlyphComponents(ttf, index, glyphlist);
  1558. if (flags & ARG_1_AND_2_ARE_WORDS) {
  1559. ptr += 4;
  1560. } else {
  1561. ptr += 2;
  1562. }
  1563. if (flags & WE_HAVE_A_SCALE) {
  1564. ptr += 2;
  1565. } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
  1566. ptr += 4;
  1567. } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
  1568. ptr += 8;
  1569. }
  1570. } while (flags & MORE_COMPONENTS);
  1571. }
  1572. return n;
  1573. }
  1574. #ifndef NO_TYPE3
  1575. int CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname, /*FOLD00*/
  1576. sal_uInt16 *glyphArray, sal_uInt8 *encoding, int nGlyphs,
  1577. int wmode)
  1578. {
  1579. ControlPoint *pa;
  1580. PSPathElement *path;
  1581. int i, j, r, n;
  1582. const sal_uInt8* table = getTable(ttf, O_head);
  1583. TTGlyphMetrics metrics;
  1584. int UPEm = ttf->unitsPerEm;
  1585. const char *h01 = "%%!PS-AdobeFont-%d.%d-%d.%d\n";
  1586. const char *h02 = "%% Creator: %s %s %s\n";
  1587. const char *h09 = "%% Original font name: %s\n";
  1588. const char *h10 =
  1589. "30 dict begin\n"
  1590. "/PaintType 0 def\n"
  1591. "/FontType 3 def\n"
  1592. "/StrokeWidth 0 def\n";
  1593. const char *h11 = "/FontName (%s) cvn def\n";
  1594. /*
  1595. const char *h12 = "%/UniqueID %d def\n";
  1596. */
  1597. const char *h13 = "/FontMatrix [.001 0 0 .001 0 0] def\n";
  1598. const char *h14 = "/FontBBox [%d %d %d %d] def\n";
  1599. const char *h15=
  1600. "/Encoding 256 array def\n"
  1601. " 0 1 255 {Encoding exch /.notdef put} for\n";
  1602. const char *h16 = " Encoding %d /glyph%d put\n";
  1603. const char *h17 = "/XUID [103 0 0 16#%08X %d 16#%08X 16#%08X] def\n";
  1604. const char *h30 = "/CharProcs %d dict def\n";
  1605. const char *h31 = " CharProcs begin\n";
  1606. const char *h32 = " /.notdef {} def\n";
  1607. const char *h33 = " /glyph%d {\n";
  1608. const char *h34 = " } bind def\n";
  1609. const char *h35 = " end\n";
  1610. const char *h40 =
  1611. "/BuildGlyph {\n"
  1612. " exch /CharProcs get exch\n"
  1613. " 2 copy known not\n"
  1614. " {pop /.notdef} if\n"
  1615. " get exec\n"
  1616. "} bind def\n"
  1617. "/BuildChar {\n"
  1618. " 1 index /Encoding get exch get\n"
  1619. " 1 index /BuildGlyph get exec\n"
  1620. "} bind def\n"
  1621. "currentdict end\n";
  1622. const char *h41 = "(%s) cvn exch definefont pop\n";
  1623. if (!((nGlyphs > 0) && (nGlyphs <= 256))) return SF_GLYPHNUM;
  1624. if (!glyphArray) return SF_BADARG;
  1625. if (!fname) fname = ttf->psname;
  1626. fprintf(outf, h01, GetInt16(table, 0, 1), GetUInt16(table, 2, 1), GetInt16(table, 4, 1), GetUInt16(table, 6, 1));
  1627. fprintf(outf, h02, modname, modver, modextra);
  1628. fprintf(outf, h09, ttf->psname);
  1629. fprintf(outf, "%s", h10);
  1630. fprintf(outf, h11, fname);
  1631. /* fprintf(outf, h12, 4000000); */
  1632. /* XUID generation:
  1633. * 103 0 0 C1 C2 C3 C4
  1634. * C1 - CRC-32 of the entire source TrueType font
  1635. * C2 - number of glyphs in the subset
  1636. * C3 - CRC-32 of the glyph array
  1637. * C4 - CRC-32 of the encoding array
  1638. *
  1639. * All CRC-32 numbers are presented as hexadecimal numbers
  1640. */
  1641. fprintf(outf, h17, rtl_crc32(0, ttf->ptr, ttf->fsize), nGlyphs, rtl_crc32(0, glyphArray, nGlyphs * 2), rtl_crc32(0, encoding, nGlyphs));
  1642. fprintf(outf, "%s", h13);
  1643. fprintf(outf, h14, XUnits(UPEm, GetInt16(table, 36, 1)), XUnits(UPEm, GetInt16(table, 38, 1)), XUnits(UPEm, GetInt16(table, 40, 1)), XUnits(UPEm, GetInt16(table, 42, 1)));
  1644. fprintf(outf, "%s", h15);
  1645. for (i = 0; i < nGlyphs; i++) {
  1646. fprintf(outf, h16, encoding[i], i);
  1647. }
  1648. fprintf(outf, h30, nGlyphs+1);
  1649. fprintf(outf, "%s", h31);
  1650. fprintf(outf, "%s", h32);
  1651. for (i = 0; i < nGlyphs; i++) {
  1652. fprintf(outf, h33, i);
  1653. r = GetTTGlyphOutline(ttf, glyphArray[i] < ttf->nglyphs ? glyphArray[i] : 0, &pa, &metrics, 0);
  1654. if (r > 0) {
  1655. n = BSplineToPSPath(pa, r, &path);
  1656. } else {
  1657. n = 0; /* glyph might have zero contours but valid metrics ??? */
  1658. path = 0;
  1659. if (r < 0) { /* glyph is not present in the font - pa array was not allocated, so no need to free it */
  1660. continue;
  1661. }
  1662. }
  1663. fprintf(outf, "\t%d %d %d %d %d %d setcachedevice\n",
  1664. wmode == 0 ? XUnits(UPEm, metrics.aw) : 0,
  1665. wmode == 0 ? 0 : -XUnits(UPEm, metrics.ah),
  1666. XUnits(UPEm, metrics.xMin),
  1667. XUnits(UPEm, metrics.yMin),
  1668. XUnits(UPEm, metrics.xMax),
  1669. XUnits(UPEm, metrics.yMax));
  1670. for (j = 0; j < n; j++)
  1671. {
  1672. switch (path[j].type)
  1673. {
  1674. case PS_MOVETO:
  1675. fprintf(outf, "\t%d %d moveto\n", XUnits(UPEm, path[j].x1), XUnits(UPEm, path[j].y1));
  1676. break;
  1677. case PS_LINETO:
  1678. fprintf(outf, "\t%d %d lineto\n", XUnits(UPEm, path[j].x1), XUnits(UPEm, path[j].y1));
  1679. break;
  1680. case PS_CURVETO:
  1681. fprintf(outf, "\t%d %d %d %d %d %d curveto\n", XUnits(UPEm, path[j].x1), XUnits(UPEm, path[j].y1), XUnits(UPEm, path[j].x2), XUnits(UPEm, path[j].y2), XUnits(UPEm, path[j].x3), XUnits(UPEm, path[j].y3));
  1682. break;
  1683. case PS_CLOSEPATH:
  1684. fprintf(outf, "\tclosepath\n");
  1685. break;
  1686. case PS_NOOP:
  1687. break;
  1688. }
  1689. }
  1690. if (n > 0) fprintf(outf, "\tfill\n"); /* if glyph is not a whitespace character */
  1691. fprintf(outf, "%s", h34);
  1692. free(pa);
  1693. free(path);
  1694. }
  1695. fprintf(outf, "%s", h35);
  1696. fprintf(outf, "%s", h40);
  1697. fprintf(outf, h41, fname);
  1698. return SF_OK;
  1699. }
  1700. #endif
  1701. #ifndef NO_TTCR
  1702. int CreateTTFromTTGlyphs(TrueTypeFont *ttf,
  1703. const char *fname,
  1704. sal_uInt16 *glyphArray,
  1705. sal_uInt8 *encoding,
  1706. int nGlyphs,
  1707. int nNameRecs,
  1708. NameRecord *nr,
  1709. sal_uInt32 flags)
  1710. {
  1711. TrueTypeCreator *ttcr;
  1712. TrueTypeTable *head=0, *hhea=0, *maxp=0, *cvt=0, *prep=0, *glyf=0, *fpgm=0, *cmap=0, *name=0, *post = 0, *os2 = 0;
  1713. int i;
  1714. int res;
  1715. TrueTypeCreatorNewEmpty(T_true, &ttcr);
  1716. /** name **/
  1717. if (flags & TTCF_AutoName) {
  1718. /* not implemented yet
  1719. NameRecord *names;
  1720. NameRecord newname;
  1721. int n = GetTTNameRecords(ttf, &names);
  1722. int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0, n6 = 0;
  1723. sal_uInt8 *cp1;
  1724. sal_uInt8 suffix[32];
  1725. sal_uInt32 c1 = crc32(glyphArray, nGlyphs * 2);
  1726. sal_uInt32 c2 = crc32(encoding, nGlyphs);
  1727. int len;
  1728. snprintf(suffix, 31, "S%08X%08X-%d", c1, c2, nGlyphs);
  1729. name = TrueTypeTableNew_name(0, 0);
  1730. for (i = 0; i < n; i++) {
  1731. if (names[i].platformID == 1 && names[i].encodingID == 0 && names[i].languageID == 0 && names[i].nameID == 1) {
  1732. memcpy(newname, names+i, sizeof(NameRecord));
  1733. newname.slen = name[i].slen + strlen(suffix);
  1734. */
  1735. const sal_uInt8 ptr[] = {0,'T',0,'r',0,'u',0,'e',0,'T',0,'y',0,'p',0,'e',0,'S',0,'u',0,'b',0,'s',0,'e',0,'t'};
  1736. NameRecord n1 = {1, 0, 0, 6, 14, (sal_uInt8*)"TrueTypeSubset"};
  1737. NameRecord n2 = {3, 1, 1033, 6, 28, 0};
  1738. n2.sptr = (sal_uInt8 *) ptr;
  1739. name = TrueTypeTableNew_name(0, 0);
  1740. nameAdd(name, &n1);
  1741. nameAdd(name, &n2);
  1742. } else {
  1743. if (nNameRecs == 0) {
  1744. NameRecord *names;
  1745. int n = GetTTNameRecords(ttf, &names);
  1746. name = TrueTypeTableNew_name(n, names);
  1747. DisposeNameRecords(names, n);
  1748. } else {
  1749. name = TrueTypeTableNew_name(nNameRecs, nr);
  1750. }
  1751. }
  1752. /** maxp **/
  1753. maxp = TrueTypeTableNew_maxp(getTable(ttf, O_maxp), getTableSize(ttf, O_maxp));
  1754. /** hhea **/
  1755. const sal_uInt8* p = getTable(ttf, O_hhea);
  1756. if (p) {
  1757. hhea = TrueTypeTableNew_hhea(GetUInt16(p, 4, 1), GetUInt16(p, 6, 1), GetUInt16(p, 8, 1), GetUInt16(p, 18, 1), GetUInt16(p, 20, 1));
  1758. } else {
  1759. hhea = TrueTypeTableNew_hhea(0, 0, 0, 0, 0);
  1760. }
  1761. /** head **/
  1762. p = getTable(ttf, O_head);
  1763. assert(p != 0);
  1764. head = TrueTypeTableNew_head(GetUInt32(p, 4, 1),
  1765. GetUInt16(p, 16, 1),
  1766. GetUInt16(p, 18, 1),
  1767. p+20,
  1768. GetUInt16(p, 44, 1),
  1769. GetUInt16(p, 46, 1),
  1770. GetInt16(p, 48, 1));
  1771. /** glyf **/
  1772. glyf = TrueTypeTableNew_glyf();
  1773. sal_uInt32* gID = (sal_uInt32*)scalloc(nGlyphs, sizeof(sal_uInt32));
  1774. for (i = 0; i < nGlyphs; i++) {
  1775. gID[i] = glyfAdd(glyf, GetTTRawGlyphData(ttf, glyphArray[i]), ttf);
  1776. }
  1777. /** cmap **/
  1778. cmap = TrueTypeTableNew_cmap();
  1779. for (i=0; i < nGlyphs; i++) {
  1780. cmapAdd(cmap, 0x010000, encoding[i], gID[i]);
  1781. }
  1782. /** cvt **/
  1783. if ((p = getTable(ttf, O_cvt)) != 0) {
  1784. cvt = TrueTypeTableNew(T_cvt, getTableSize(ttf, O_cvt), p);
  1785. }
  1786. /** prep **/
  1787. if ((p = getTable(ttf, O_prep)) != 0) {
  1788. prep = TrueTypeTableNew(T_prep, getTableSize(ttf, O_prep), p);
  1789. }
  1790. /** fpgm **/
  1791. if ((p = getTable(ttf, O_fpgm)) != 0) {
  1792. fpgm = TrueTypeTableNew(T_fpgm, getTableSize(ttf, O_fpgm), p);
  1793. }
  1794. /** post **/
  1795. if ((p = getTable(ttf, O_post)) != 0) {
  1796. post = TrueTypeTableNew_post(0x00030000,
  1797. GetUInt32(p, 4, 1),
  1798. GetUInt16(p, 8, 1),
  1799. GetUInt16(p, 10, 1),
  1800. GetUInt16(p, 12, 1));
  1801. } else {
  1802. post = TrueTypeTableNew_post(0x00030000, 0, 0, 0, 0);
  1803. }
  1804. if (flags & TTCF_IncludeOS2) {
  1805. if ((p = getTable(ttf, O_OS2)) != 0) {
  1806. os2 = TrueTypeTableNew(T_OS2, getTableSize(ttf, O_OS2), p);
  1807. }
  1808. }
  1809. AddTable(ttcr, name); AddTable(ttcr, maxp); AddTable(ttcr, hhea);
  1810. AddTable(ttcr, head); AddTable(ttcr, glyf); AddTable(ttcr, cmap);
  1811. AddTable(ttcr, cvt ); AddTable(ttcr, prep); AddTable(ttcr, fpgm);
  1812. AddTable(ttcr, post); AddTable(ttcr, os2);
  1813. if ((res = StreamToFile(ttcr, fname)) != SF_OK) {
  1814. #if OSL_DEBUG_LEVEL > 1
  1815. fprintf(stderr, "StreamToFile: error code: %d.\n", res);
  1816. #endif
  1817. }
  1818. TrueTypeCreatorDispose(ttcr);
  1819. free(gID);
  1820. return res;
  1821. }
  1822. #endif
  1823. #ifndef NO_TYPE42
  1824. static GlyphOffsets *GlyphOffsetsNew(sal_uInt8 *sfntP)
  1825. {
  1826. GlyphOffsets* res = (GlyphOffsets*)smalloc(sizeof(GlyphOffsets));
  1827. sal_uInt8 *loca = NULL;
  1828. sal_uInt16 i, numTables = GetUInt16(sfntP, 4, 1);
  1829. sal_uInt32 locaLen = 0;
  1830. sal_Int16 indexToLocFormat = 0;
  1831. for (i = 0; i < numTables; i++) {
  1832. sal_uInt32 tag = GetUInt32(sfntP + 12, 16 * i, 1);
  1833. sal_uInt32 off = GetUInt32(sfntP + 12, 16 * i + 8, 1);
  1834. sal_uInt32 len = GetUInt32(sfntP + 12, 16 * i + 12, 1);
  1835. if (tag == T_loca) {
  1836. loca = sfntP + off;
  1837. locaLen = len;
  1838. } else if (tag == T_head) {
  1839. indexToLocFormat = GetInt16(sfntP + off, 50, 1);
  1840. }
  1841. }
  1842. res->nGlyphs = locaLen / ((indexToLocFormat == 1) ? 4 : 2);
  1843. assert(res->nGlyphs != 0);
  1844. res->offs = (sal_uInt32*)scalloc(res->nGlyphs, sizeof(sal_uInt32));
  1845. for (i = 0; i < res->nGlyphs; i++) {
  1846. if (indexToLocFormat == 1) {
  1847. res->offs[i] = GetUInt32(loca, i * 4, 1);
  1848. } else {
  1849. res->offs[i] = GetUInt16(loca, i * 2, 1) << 1;
  1850. }
  1851. }
  1852. return res;
  1853. }
  1854. static void GlyphOffsetsDispose(GlyphOffsets *_this)
  1855. {
  1856. if (_this) {
  1857. free(_this->offs);
  1858. free(_this);
  1859. }
  1860. }
  1861. static void DumpSfnts(FILE *outf, sal_uInt8 *sfntP)
  1862. {
  1863. HexFmt *h = HexFmtNew(outf);
  1864. sal_uInt16 i, numTables = GetUInt16(sfntP, 4, 1);
  1865. GlyphOffsets *go = GlyphOffsetsNew(sfntP);
  1866. sal_uInt8 pad[] = {0,0,0,0}; /* zeroes */
  1867. assert(numTables <= 9); /* Type42 has 9 required tables */
  1868. sal_uInt32* offs = (sal_uInt32*)scalloc(numTables, sizeof(sal_uInt32));
  1869. // sal_uInt32* lens = (sal_uInt32*)scalloc(numTables, sizeof(sal_uInt32));
  1870. fputs("/sfnts [", outf);
  1871. HexFmtOpenString(h);
  1872. HexFmtBlockWrite(h, sfntP, 12); /* stream out the Offset Table */
  1873. HexFmtBlockWrite(h, sfntP+12, 16 * numTables); /* stream out the Table Directory */
  1874. for (i=0; i<numTables; i++) {
  1875. sal_uInt32 tag = GetUInt32(sfntP + 12, 16 * i, 1);
  1876. sal_uInt32 off = GetUInt32(sfntP + 12, 16 * i + 8, 1);
  1877. sal_uInt32 len = GetUInt32(sfntP + 12, 16 * i + 12, 1);
  1878. if (tag != T_glyf) {
  1879. HexFmtBlockWrite(h, sfntP + off, len);
  1880. } else {
  1881. sal_uInt8 *glyf = sfntP + off;
  1882. sal_uInt32 o, l, j;
  1883. for (j = 0; j < go->nGlyphs - 1; j++) {
  1884. o = go->offs[j];
  1885. l = go->offs[j + 1] - o;
  1886. HexFmtBlockWrite(h, glyf + o, l);
  1887. }
  1888. }
  1889. HexFmtBlockWrite(h, pad, (4 - (len & 3)) & 3);
  1890. }
  1891. HexFmtCloseString(h);
  1892. fputs("] def\n", outf);
  1893. GlyphOffsetsDispose(go);
  1894. HexFmtDispose(h);
  1895. free(offs);
  1896. // free(lens);
  1897. }
  1898. int CreateT42FromTTGlyphs(TrueTypeFont *ttf,
  1899. FILE *outf,
  1900. const char *psname,
  1901. sal_uInt16 *glyphArray,
  1902. sal_uInt8 *encoding,
  1903. int nGlyphs)
  1904. {
  1905. TrueTypeCreator *ttcr;
  1906. TrueTypeTable *head=0, *hhea=0, *maxp=0, *cvt=0, *prep=0, *glyf=0, *fpgm=0;
  1907. int i;
  1908. int res;
  1909. sal_uInt32 ver, rev;
  1910. sal_uInt8 *sfntP;
  1911. sal_uInt32 sfntLen;
  1912. int UPEm = ttf->unitsPerEm;
  1913. if (nGlyphs >= 256) return SF_GLYPHNUM;
  1914. assert(psname != 0);
  1915. TrueTypeCreatorNewEmpty(T_true, &ttcr);
  1916. /* head */
  1917. const sal_uInt8* p = getTable(ttf, O_head);
  1918. const sal_uInt8* headP = p;
  1919. assert(p != 0);
  1920. head = TrueTypeTableNew_head(GetUInt32(p, 4, 1), GetUInt16(p, 16, 1), GetUInt16(p, 18, 1), p+20, GetUInt16(p, 44, 1), GetUInt16(p, 46, 1), GetInt16(p, 48, 1));
  1921. ver = GetUInt32(p, 0, 1);
  1922. rev = GetUInt32(p, 4, 1);
  1923. /** hhea **/
  1924. p = getTable(ttf, O_hhea);
  1925. if (p) {
  1926. hhea = TrueTypeTableNew_hhea(GetUInt16(p, 4, 1), GetUInt16(p, 6, 1), GetUInt16(p, 8, 1), GetUInt16(p, 18, 1), GetUInt16(p, 20, 1));
  1927. } else {
  1928. hhea = TrueTypeTableNew_hhea(0, 0, 0, 0, 0);
  1929. }
  1930. /** maxp **/
  1931. maxp = TrueTypeTableNew_maxp(getTable(ttf, O_maxp), getTableSize(ttf, O_maxp));
  1932. /** cvt **/
  1933. if ((p = getTable(ttf, O_cvt)) != 0) {
  1934. cvt = TrueTypeTableNew(T_cvt, getTableSize(ttf, O_cvt), p);
  1935. }
  1936. /** prep **/
  1937. if ((p = getTable(ttf, O_prep)) != 0) {
  1938. prep = TrueTypeTableNew(T_prep, getTableSize(ttf, O_prep), p);
  1939. }
  1940. /** fpgm **/
  1941. if ((p = getTable(ttf, O_fpgm)) != 0) {
  1942. fpgm = TrueTypeTableNew(T_fpgm, getTableSize(ttf, O_fpgm), p);
  1943. }
  1944. /** glyf **/
  1945. glyf = TrueTypeTableNew_glyf();
  1946. sal_uInt16* gID = (sal_uInt16*)scalloc(nGlyphs, sizeof(sal_uInt32));
  1947. for (i = 0; i < nGlyphs; i++) {
  1948. gID[i] = (sal_uInt16)glyfAdd(glyf, GetTTRawGlyphData(ttf, glyphArray[i]), ttf);
  1949. }
  1950. AddTable(ttcr, head); AddTable(ttcr, hhea); AddTable(ttcr, maxp); AddTable(ttcr, cvt);
  1951. AddTable(ttcr, prep); AddTable(ttcr, glyf); AddTable(ttcr, fpgm);
  1952. if ((res = StreamToMemory(ttcr, &sfntP, &sfntLen)) != SF_OK) {
  1953. TrueTypeCreatorDispose(ttcr);
  1954. free(gID);
  1955. return res;
  1956. }
  1957. fprintf(outf, "%%!PS-TrueTypeFont-%d.%d-%d.%d\n", (int)(ver>>16), (int)(ver & 0xFFFF), (int)(rev>>16), (int)(rev & 0xFFFF));
  1958. fprintf(outf, "%%%%Creator: %s %s %s\n", modname, modver, modextra);
  1959. fprintf(outf, "%%- Font subset generated from a source font file: '%s'\n", ttf->fname);
  1960. fprintf(outf, "%%- Original font name: %s\n", ttf->psname);
  1961. fprintf(outf, "%%- Original font family: %s\n", ttf->family);
  1962. fprintf(outf, "%%- Original font sub-family: %s\n", ttf->subfamily);
  1963. fprintf(outf, "11 dict begin\n");
  1964. fprintf(outf, "/FontName (%s) cvn def\n", psname);
  1965. fprintf(outf, "/PaintType 0 def\n");
  1966. fprintf(outf, "/FontMatrix [1 0 0 1 0 0] def\n");
  1967. fprintf(outf, "/FontBBox [%d %d %d %d] def\n", XUnits(UPEm, GetInt16(headP, 36, 1)), XUnits(UPEm, GetInt16(headP, 38, 1)), XUnits(UPEm, GetInt16(headP, 40, 1)), XUnits(UPEm, GetInt16(headP, 42, 1)));
  1968. fprintf(outf, "/FontType 42 def\n");
  1969. fprintf(outf, "/Encoding 256 array def\n");
  1970. fprintf(outf, " 0 1 255 {Encoding exch /.notdef put} for\n");
  1971. for (i = 1; i<nGlyphs; i++) {
  1972. fprintf(outf, "Encoding %d /glyph%d put\n", encoding[i], gID[i]);
  1973. }
  1974. fprintf(outf, "/XUID [103 0 1 16#%08X %d 16#%08X 16#%08X] def\n", (unsigned int)rtl_crc32(0, ttf->ptr, ttf->fsize), (unsigned int)nGlyphs, (unsigned int)rtl_crc32(0, glyphArray, nGlyphs * 2), (unsigned int)rtl_crc32(0, encoding, nGlyphs));
  1975. DumpSfnts(outf, sfntP);
  1976. /* dump charstrings */
  1977. fprintf(outf, "/CharStrings %d dict dup begin\n", nGlyphs);
  1978. fprintf(outf, "/.notdef 0 def\n");
  1979. for (i = 1; i < (int)glyfCount(glyf); i++) {
  1980. fprintf(outf,"/glyph%d %d def\n", i, i);
  1981. }
  1982. fprintf(outf, "end readonly def\n");
  1983. fprintf(outf, "FontName currentdict end definefont pop\n");
  1984. TrueTypeCreatorDispose(ttcr);
  1985. free(gID);
  1986. free(sfntP);
  1987. return SF_OK;
  1988. }
  1989. #endif
  1990. #ifndef NO_MAPPERS
  1991. int MapString(TrueTypeFont *ttf, sal_uInt16 *str, int nchars, sal_uInt16 *glyphArray, int bvertical)
  1992. {
  1993. int i;
  1994. sal_uInt16 *cp;
  1995. if (ttf->cmapType == CMAP_NOT_USABLE ) return -1;
  1996. if (!nchars) return 0;
  1997. if (glyphArray == 0) {
  1998. cp = str;
  1999. } else {
  2000. cp = glyphArray;
  2001. }
  2002. switch (ttf->cmapType) {
  2003. case CMAP_MS_Symbol:
  2004. if( ttf->mapper == getGlyph0 ) {
  2005. sal_uInt16 aChar;
  2006. for( i = 0; i < nchars; i++ ) {
  2007. aChar = str[i];
  2008. if( ( aChar & 0xf000 ) == 0xf000 )
  2009. aChar &= 0x00ff;
  2010. cp[i] = aChar;
  2011. }
  2012. }
  2013. else if( glyphArray )
  2014. memcpy(glyphArray, str, nchars * 2);
  2015. break;
  2016. case CMAP_MS_Unicode:
  2017. if (glyphArray != 0) {
  2018. memcpy(glyphArray, str, nchars * 2);
  2019. }
  2020. break;
  2021. case CMAP_MS_ShiftJIS: TranslateString12(str, cp, nchars); break;
  2022. case CMAP_MS_Big5: TranslateString13(str, cp, nchars); break;
  2023. case CMAP_MS_PRC: TranslateString14(str, cp, nchars); break;
  2024. case CMAP_MS_Wansung: TranslateString15(str, cp, nchars); break;
  2025. case CMAP_MS_Johab: TranslateString16(str, cp, nchars); break;
  2026. }
  2027. for (i = 0; i < nchars; i++) {
  2028. cp[i] = (sal_uInt16)ttf->mapper(ttf->cmap, cp[i]);
  2029. if (cp[i]!=0 && bvertical!=0)
  2030. cp[i] = (sal_uInt16)UseGSUB(ttf,cp[i],bvertical);
  2031. }
  2032. return nchars;
  2033. }
  2034. sal_uInt16 MapChar(TrueTypeFont *ttf, sal_uInt16 ch, int bvertical)
  2035. {
  2036. switch (ttf->cmapType) {
  2037. case CMAP_MS_Symbol:
  2038. if( ttf->mapper == getGlyph0 && ( ch & 0xf000 ) == 0xf000 )
  2039. ch &= 0x00ff;
  2040. return (sal_uInt16)ttf->mapper(ttf->cmap, ch );
  2041. case CMAP_MS_Unicode: break;
  2042. case CMAP_MS_ShiftJIS: ch = TranslateChar12(ch); break;
  2043. case CMAP_MS_Big5: ch = TranslateChar13(ch); break;
  2044. case CMAP_MS_PRC: ch = TranslateChar14(ch); break;
  2045. case CMAP_MS_Wansung: ch = TranslateChar15(ch); break;
  2046. case CMAP_MS_Johab: ch = TranslateChar16(ch); break;
  2047. default: return 0;
  2048. }
  2049. ch = (sal_uInt16)ttf->mapper(ttf->cmap, ch);
  2050. if (ch!=0 && bvertical!=0)
  2051. ch = (sal_uInt16)UseGSUB(ttf,ch,bvertical);
  2052. return ch;
  2053. }
  2054. int DoesVerticalSubstitution( TrueTypeFont *ttf, int bvertical)
  2055. {
  2056. int nRet = 0;
  2057. if( bvertical)
  2058. nRet = HasVerticalGSUB( ttf);
  2059. return nRet;
  2060. }
  2061. #endif
  2062. int GetTTGlyphCount( TrueTypeFont* ttf )
  2063. {
  2064. return ttf->nglyphs;
  2065. }
  2066. bool GetSfntTable( TrueTypeFont* ttf, int nSubtableIndex,
  2067. const sal_uInt8** ppRawBytes, int* pRawLength )
  2068. {
  2069. if( (nSubtableIndex < 0) || (nSubtableIndex >= NUM_TAGS) )
  2070. return false;
  2071. *pRawLength = ttf->tlens[ nSubtableIndex ];
  2072. *ppRawBytes = ttf->tables[ nSubtableIndex ];
  2073. bool bOk = (*pRawLength > 0) && (ppRawBytes != NULL);
  2074. return bOk;
  2075. }
  2076. TTSimpleGlyphMetrics *GetTTSimpleGlyphMetrics(TrueTypeFont *ttf, sal_uInt16 *glyphArray, int nGlyphs, int mode)
  2077. {
  2078. const sal_uInt8* pTable;
  2079. sal_uInt32 n;
  2080. int nTableSize;
  2081. if (mode == 0) {
  2082. n = ttf->numberOfHMetrics;
  2083. pTable = getTable( ttf, O_hmtx );
  2084. nTableSize = getTableSize( ttf, O_hmtx );
  2085. } else {
  2086. n = ttf->numOfLongVerMetrics;
  2087. pTable = getTable( ttf, O_vmtx );
  2088. nTableSize = getTableSize( ttf, O_vmtx );
  2089. }
  2090. if (!nGlyphs || !glyphArray) return 0; /* invalid parameters */
  2091. if (!n || !pTable) return 0; /* the font does not contain the requested metrics */
  2092. TTSimpleGlyphMetrics* res = (TTSimpleGlyphMetrics*)calloc(nGlyphs, sizeof(TTSimpleGlyphMetrics));
  2093. assert(res != 0);
  2094. const int UPEm = ttf->unitsPerEm;
  2095. for( int i = 0; i < nGlyphs; ++i) {
  2096. int nAdvOffset, nLsbOffset;
  2097. sal_uInt16 glyphID = glyphArray[i];
  2098. if (glyphID < n) {
  2099. nAdvOffset = 4 * glyphID;
  2100. nLsbOffset = nAdvOffset + 2;
  2101. } else {
  2102. nAdvOffset = 4 * (n - 1);
  2103. if( glyphID < ttf->nglyphs )
  2104. nLsbOffset = 4 * n + 2 * (glyphID - n);
  2105. else /* font is broken -> use lsb of last hmetrics */
  2106. nLsbOffset = nAdvOffset + 2;
  2107. }
  2108. if( nAdvOffset >= nTableSize)
  2109. res[i].adv = 0; /* better than a crash for buggy fonts */
  2110. else
  2111. res[i].adv = static_cast<sal_uInt16>(
  2112. XUnits( UPEm, GetUInt16( pTable, nAdvOffset, 1) ) );
  2113. if( nLsbOffset >= nTableSize)
  2114. res[i].sb = 0; /* better than a crash for buggy fonts */
  2115. else
  2116. res[i].sb = static_cast<sal_Int16>(
  2117. XUnits( UPEm, GetInt16( pTable, nLsbOffset, 1) ) );
  2118. }
  2119. return res;
  2120. }
  2121. #ifndef NO_MAPPERS
  2122. TTSimpleGlyphMetrics *GetTTSimpleCharMetrics(TrueTypeFont * ttf, sal_uInt16 firstChar, int nChars, int mode)
  2123. {
  2124. TTSimpleGlyphMetrics *res = 0;
  2125. int i, n;
  2126. sal_uInt16* str = (sal_uInt16*)malloc(nChars * 2);
  2127. assert(str != 0);
  2128. for (i=0; i<nChars; i++) str[i] = (sal_uInt16)(firstChar + i);
  2129. if ((n = MapString(ttf, str, nChars, 0, mode)) != -1) {
  2130. res = GetTTSimpleGlyphMetrics(ttf, str, n, mode);
  2131. }
  2132. free(str);
  2133. return res;
  2134. }
  2135. #endif
  2136. void GetTTGlobalFontInfo(TrueTypeFont *ttf, TTGlobalFontInfo *info)
  2137. {
  2138. int UPEm = ttf->unitsPerEm;
  2139. memset(info, 0, sizeof(TTGlobalFontInfo));
  2140. info->family = ttf->family;
  2141. info->ufamily = ttf->ufamily;
  2142. info->subfamily = ttf->subfamily;
  2143. info->usubfamily = ttf->usubfamily;
  2144. info->psname = ttf->psname;
  2145. info->symbolEncoded = (ttf->cmapType == CMAP_MS_Symbol);
  2146. const sal_uInt8* table = getTable(ttf, O_OS2);
  2147. if (table) {
  2148. info->weight = GetUInt16(table, 4, 1);
  2149. info->width = GetUInt16(table, 6, 1);
  2150. /* There are 3 different versions of OS/2 table: original (68 bytes long),
  2151. * Microsoft old (78 bytes long) and Microsoft new (86 bytes long,)
  2152. * Apple's documentation recommends looking at the table length.
  2153. */
  2154. if (getTableSize(ttf, O_OS2) > 68) {
  2155. info->typoAscender = XUnits(UPEm,GetInt16(table, 68, 1));
  2156. info->typoDescender = XUnits(UPEm, GetInt16(table, 70, 1));
  2157. info->typoLineGap = XUnits(UPEm, GetInt16(table, 72, 1));
  2158. info->winAscent = XUnits(UPEm, GetUInt16(table, 74, 1));
  2159. info->winDescent = XUnits(UPEm, GetUInt16(table, 76, 1));
  2160. /* sanity check; some fonts treat winDescent as signed
  2161. * violating the standard */
  2162. if( info->winDescent > 5*UPEm )
  2163. info->winDescent = XUnits(UPEm, GetInt16(table, 76,1));
  2164. }
  2165. if (ttf->cmapType == CMAP_MS_Unicode) {
  2166. info->rangeFlag = 1;
  2167. info->ur1 = GetUInt32(table, 42, 1);
  2168. info->ur2 = GetUInt32(table, 46, 1);
  2169. info->ur3 = GetUInt32(table, 50, 1);
  2170. info->ur4 = GetUInt32(table, 54, 1);
  2171. }
  2172. memcpy(info->panose, table + 32, 10);
  2173. info->typeFlags = GetUInt16( table, 8, 1 );
  2174. if( getTable(ttf, O_CFF) )
  2175. info->typeFlags |= TYPEFLAG_PS_OPENTYPE;
  2176. }
  2177. table = getTable(ttf, O_post);
  2178. if (table && getTableSize(ttf, O_post) >= 12+sizeof(sal_uInt32)) {
  2179. info->pitch = GetUInt32(table, 12, 1);
  2180. info->italicAngle = GetInt32(table, 4, 1);
  2181. }
  2182. table = getTable(ttf, O_head); /* 'head' tables is always there */
  2183. info->xMin = XUnits(UPEm, GetInt16(table, 36, 1));
  2184. info->yMin = XUnits(UPEm, GetInt16(table, 38, 1));
  2185. info->xMax = XUnits(UPEm, GetInt16(table, 40, 1));
  2186. info->yMax = XUnits(UPEm, GetInt16(table, 42, 1));
  2187. info->macStyle = GetInt16(table, 44, 1);
  2188. table = getTable(ttf, O_hhea);
  2189. if (table) {
  2190. info->ascender = XUnits(UPEm, GetInt16(table, 4, 1));
  2191. info->descender = XUnits(UPEm, GetInt16(table, 6, 1));
  2192. info->linegap = XUnits(UPEm, GetInt16(table, 8, 1));
  2193. }
  2194. table = getTable(ttf, O_vhea);
  2195. if (table) {
  2196. info->vascent = XUnits(UPEm, GetInt16(table, 4, 1));
  2197. info->vdescent = XUnits(UPEm, GetInt16(table, 6, 1));
  2198. }
  2199. }
  2200. GlyphData *GetTTRawGlyphData(TrueTypeFont *ttf, sal_uInt32 glyphID)
  2201. {
  2202. const sal_uInt8* glyf = getTable(ttf, O_glyf);
  2203. const sal_uInt8* hmtx = getTable(ttf, O_hmtx);
  2204. int n;
  2205. if( glyphID >= ttf->nglyphs )
  2206. return 0;
  2207. /* #127161# check the glyph offsets */
  2208. sal_uInt32 length = getTableSize( ttf, O_glyf );
  2209. if( length < ttf->goffsets[ glyphID+1 ] )
  2210. return 0;
  2211. length = ttf->goffsets[glyphID+1] - ttf->goffsets[glyphID];
  2212. GlyphData* d = (GlyphData*)malloc(sizeof(GlyphData)); assert(d != 0);
  2213. if (length > 0) {
  2214. const sal_uInt8* srcptr = glyf + ttf->goffsets[glyphID];
  2215. d->ptr = (sal_uInt8*)malloc((length + 1) & ~1); assert(d->ptr != 0);
  2216. memcpy( d->ptr, srcptr, length );
  2217. d->compflag = (GetInt16( srcptr, 0, 1 ) < 0);
  2218. } else {
  2219. d->ptr = 0;
  2220. d->compflag = 0;
  2221. }
  2222. d->glyphID = glyphID;
  2223. d->nbytes = (sal_uInt16)((length + 1) & ~1);
  2224. /* now calculate npoints and ncontours */
  2225. ControlPoint *cp;
  2226. n = GetTTGlyphPoints(ttf, glyphID, &cp);
  2227. if (n != -1)
  2228. {
  2229. int m = 0;
  2230. for (int i = 0; i < n; i++)
  2231. {
  2232. if (cp[i].flags & 0x8000)
  2233. m++;
  2234. }
  2235. d->npoints = (sal_uInt16)n;
  2236. d->ncontours = (sal_uInt16)m;
  2237. free(cp);
  2238. } else {
  2239. d->npoints = 0;
  2240. d->ncontours = 0;
  2241. }
  2242. /* get advance width and left sidebearing */
  2243. if (glyphID < ttf->numberOfHMetrics) {
  2244. d->aw = GetUInt16(hmtx, 4 * glyphID, 1);
  2245. d->lsb = GetInt16(hmtx, 4 * glyphID + 2, 1);
  2246. } else {
  2247. d->aw = GetUInt16(hmtx, 4 * (ttf->numberOfHMetrics - 1), 1);
  2248. d->lsb = GetInt16(hmtx + ttf->numberOfHMetrics * 4, (glyphID - ttf->numberOfHMetrics) * 2, 1);
  2249. }
  2250. return d;
  2251. }
  2252. int GetTTNameRecords(TrueTypeFont *ttf, NameRecord **nr)
  2253. {
  2254. const sal_uInt8* table = getTable(ttf, O_name);
  2255. int nTableSize = getTableSize(ttf, O_name );
  2256. if (nTableSize < 6)
  2257. {
  2258. #if OSL_DEBUG_LEVEL > 1
  2259. fprintf(stderr, "O_name table too small\n");
  2260. #endif
  2261. return 0;
  2262. }
  2263. sal_uInt16 n = GetUInt16(table, 2, 1);
  2264. int nStrBase = GetUInt16(table, 4, 1);
  2265. int i;
  2266. *nr = 0;
  2267. if (n == 0) return 0;
  2268. NameRecord* rec = (NameRecord*)calloc(n, sizeof(NameRecord));
  2269. for (i = 0; i < n; i++) {
  2270. int nStrOffset = GetUInt16(table + 6, 10 + 12 * i, 1);
  2271. rec[i].platformID = GetUInt16(table + 6, 12 * i, 1);
  2272. rec[i].encodingID = GetUInt16(table + 6, 2 + 12 * i, 1);
  2273. rec[i].languageID = GetUInt16(table + 6, 4 + 12 * i, 1);
  2274. rec[i].nameID = GetUInt16(table + 6, 6 + 12 * i, 1);
  2275. rec[i].slen = GetUInt16(table + 6, 8 + 12 * i, 1);
  2276. if (rec[i].slen) {
  2277. if( nStrBase+nStrOffset+rec[i].slen >= nTableSize ) {
  2278. rec[i].sptr = 0;
  2279. rec[i].slen = 0;
  2280. continue;
  2281. }
  2282. const sal_uInt8* rec_string = table + nStrBase + nStrOffset;
  2283. // sanity check
  2284. if( rec_string > (sal_uInt8*)ttf->ptr && rec_string < ((sal_uInt8*)ttf->ptr + ttf->fsize - rec[i].slen ) )
  2285. {
  2286. rec[i].sptr = (sal_uInt8 *) malloc(rec[i].slen); assert(rec[i].sptr != 0);
  2287. memcpy(rec[i].sptr, rec_string, rec[i].slen);
  2288. }
  2289. else
  2290. {
  2291. rec[i].sptr = 0;
  2292. rec[i].slen = 0;
  2293. }
  2294. } else {
  2295. rec[i].sptr = 0;
  2296. }
  2297. // some fonts have 3.0 names => fix them to 3.1
  2298. if( (rec[i].platformID == 3) && (rec[i].encodingID == 0) )
  2299. rec[i].encodingID = 1;
  2300. }
  2301. *nr = rec;
  2302. return n;
  2303. }
  2304. void DisposeNameRecords(NameRecord* nr, int n)
  2305. {
  2306. int i;
  2307. for (i = 0; i < n; i++) {
  2308. if (nr[i].sptr) free(nr[i].sptr);
  2309. }
  2310. free(nr);
  2311. }
  2312. bool getTTCoverage(
  2313. boost::dynamic_bitset<sal_uInt32> &rUnicodeRange,
  2314. boost::dynamic_bitset<sal_uInt32> &rCodePageRange,
  2315. const unsigned char* pTable, size_t nLength)
  2316. {
  2317. bool bRet = false;
  2318. sal_uInt16 nVersion = GetUInt16(pTable, 0, 1);
  2319. // parse OS/2 header
  2320. if ( nVersion >= 0x0001 && nLength >= 58 )
  2321. {
  2322. rUnicodeRange.append(GetUInt32(pTable, 42, 1));
  2323. rUnicodeRange.append(GetUInt32(pTable, 46, 1));
  2324. rUnicodeRange.append(GetUInt32(pTable, 50, 1));
  2325. rUnicodeRange.append(GetUInt32(pTable, 54, 1));
  2326. bRet = true;
  2327. if (nLength >= 86)
  2328. {
  2329. rCodePageRange.append(GetUInt32(pTable, 78, 1));
  2330. rCodePageRange.append(GetUInt32(pTable, 82, 1));
  2331. }
  2332. }
  2333. return bRet;
  2334. }
  2335. void getTTScripts(std::vector< sal_uInt32 > &rScriptTags, const unsigned char* pTable, size_t nLength)
  2336. {
  2337. if (nLength < 6)
  2338. return;
  2339. // parse GSUB/GPOS header
  2340. const sal_uInt16 nOfsScriptList = GetUInt16(pTable, 4, 1);
  2341. // parse Script Table
  2342. const sal_uInt16 nCntScript = GetUInt16(pTable, nOfsScriptList, 1);
  2343. sal_uInt32 nCurrentPos = nOfsScriptList+2;
  2344. for( sal_uInt16 nScriptIndex = 0;
  2345. nScriptIndex < nCntScript && nLength >= 6; ++nScriptIndex,
  2346. nLength-=6 )
  2347. {
  2348. sal_uInt32 nTag = GetUInt32(pTable, nCurrentPos, 1);
  2349. nCurrentPos+=6;
  2350. rScriptTags.push_back(nTag); // e.g. hani/arab/kana/hang
  2351. }
  2352. std::sort(rScriptTags.begin(), rScriptTags.end());
  2353. rScriptTags.erase(std::unique(rScriptTags.begin(), rScriptTags.end()), rScriptTags.end());
  2354. }
  2355. } // namespace vcl
  2356. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */