PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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");
  1407. if( fd ) {
  1408. if (fread(buffer, 1, 12, fd) == 12) {
  1409. if(GetUInt32(buffer, 0, 1) == T_ttcf )
  1410. nFonts = GetUInt32(buffer, 8, 1);
  1411. }
  1412. fclose(fd);
  1413. }
  1414. return nFonts;
  1415. }
  1416. static void allocTrueTypeFont( TrueTypeFont** ttf )
  1417. {
  1418. *ttf = (TrueTypeFont*)calloc(1,sizeof(TrueTypeFont));
  1419. if( *ttf != NULL )
  1420. {
  1421. (*ttf)->tag = 0;
  1422. (*ttf)->fname = 0;
  1423. (*ttf)->fsize = -1;
  1424. (*ttf)->ptr = 0;
  1425. (*ttf)->nglyphs = 0xFFFFFFFF;
  1426. (*ttf)->pGSubstitution = 0;
  1427. }
  1428. }
  1429. /* forward declariotn for the two entry points to use*/
  1430. static int doOpenTTFont( sal_uInt32 facenum, TrueTypeFont* t );
  1431. #if !defined(WIN32) && !defined(OS2)
  1432. int OpenTTFontFile( const char* fname, sal_uInt32 facenum, TrueTypeFont** ttf )
  1433. {
  1434. int ret, fd = -1;
  1435. struct stat st;
  1436. if (!fname || !*fname) return SF_BADFILE;
  1437. allocTrueTypeFont( ttf );
  1438. if( ! *ttf )
  1439. return SF_MEMORY;
  1440. (*ttf)->fname = strdup(fname);
  1441. if( ! (*ttf)->fname )
  1442. {
  1443. ret = SF_MEMORY;
  1444. goto cleanup;
  1445. }
  1446. fd = open(fname, O_RDONLY);
  1447. if (fd == -1) {
  1448. ret = SF_BADFILE;
  1449. goto cleanup;
  1450. }
  1451. if (fstat(fd, &st) == -1) {
  1452. ret = SF_FILEIO;
  1453. goto cleanup;
  1454. }
  1455. (*ttf)->fsize = st.st_size;
  1456. /* On Mac OS, most likely will happen if a Mac user renames a font file
  1457. * to be .ttf when its really a Mac resource-based font.
  1458. * Size will be 0, but fonts smaller than 4 bytes would be broken anyway.
  1459. */
  1460. if ((*ttf)->fsize == 0) {
  1461. ret = SF_BADFILE;
  1462. goto cleanup;
  1463. }
  1464. if (((*ttf)->ptr = (sal_uInt8 *) mmap(0, (*ttf)->fsize, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
  1465. ret = SF_MEMORY;
  1466. goto cleanup;
  1467. }
  1468. close(fd);
  1469. return doOpenTTFont( facenum, *ttf );
  1470. cleanup:
  1471. if (fd != -1) close(fd);
  1472. /*- t and t->fname have been allocated! */
  1473. free((*ttf)->fname);
  1474. free(*ttf);
  1475. *ttf = NULL;
  1476. return ret;
  1477. }
  1478. #endif
  1479. int OpenTTFontBuffer(void* pBuffer, sal_uInt32 nLen, sal_uInt32 facenum, TrueTypeFont** ttf)
  1480. {
  1481. allocTrueTypeFont( ttf );
  1482. if( *ttf == NULL )
  1483. return SF_MEMORY;
  1484. (*ttf)->fname = NULL;
  1485. (*ttf)->fsize = nLen;
  1486. (*ttf)->ptr = (sal_uInt8*)pBuffer;
  1487. return doOpenTTFont( facenum, *ttf );
  1488. }
  1489. static int doOpenTTFont( sal_uInt32 facenum, TrueTypeFont* t )
  1490. {
  1491. int i;
  1492. sal_uInt32 length, tag;
  1493. sal_uInt32 tdoffset = 0; /* offset to TableDirectory in a TTC file. For TTF files is 0 */
  1494. int indexfmt, k;
  1495. sal_uInt32 version = GetInt32(t->ptr, 0, 1);
  1496. if ((version == 0x00010000) || (version == T_true)) {
  1497. tdoffset = 0;
  1498. } else if (version == T_otto) { /* PS-OpenType font */
  1499. tdoffset = 0;
  1500. } else if (version == T_ttcf) { /* TrueType collection */
  1501. if (GetUInt32(t->ptr, 4, 1) != 0x00010000) {
  1502. CloseTTFont(t);
  1503. return SF_TTFORMAT;
  1504. }
  1505. if (facenum >= GetUInt32(t->ptr, 8, 1)) {
  1506. CloseTTFont(t);
  1507. return SF_FONTNO;
  1508. }
  1509. tdoffset = GetUInt32(t->ptr, 12 + 4 * facenum, 1);
  1510. } else {
  1511. CloseTTFont(t);
  1512. return SF_TTFORMAT;
  1513. }
  1514. #ifdef DEBUG2
  1515. fprintf(stderr, "tdoffset: %d\n", tdoffset);
  1516. #endif
  1517. /* magic number */
  1518. t->tag = TTFontClassTag;
  1519. t->ntables = GetUInt16(t->ptr + tdoffset, 4, 1);
  1520. if( t->ntables >= 128 )
  1521. return SF_TTFORMAT;
  1522. t->tables = (const sal_uInt8**)calloc(NUM_TAGS, sizeof(sal_uInt8*));
  1523. assert(t->tables != 0);
  1524. t->tlens = (sal_uInt32*)calloc(NUM_TAGS, sizeof(sal_uInt32));
  1525. assert(t->tlens != 0);
  1526. memset(t->tables, 0, NUM_TAGS * sizeof(void *));
  1527. memset(t->tlens, 0, NUM_TAGS * sizeof(sal_uInt32));
  1528. /* parse the tables */
  1529. for (i=0; i<(int)t->ntables; i++) {
  1530. int nIndex;
  1531. tag = GetUInt32(t->ptr + tdoffset + 12, 16 * i, 1);
  1532. switch( tag ) {
  1533. case T_maxp: nIndex = O_maxp; break;
  1534. case T_glyf: nIndex = O_glyf; break;
  1535. case T_head: nIndex = O_head; break;
  1536. case T_loca: nIndex = O_loca; break;
  1537. case T_name: nIndex = O_name; break;
  1538. case T_hhea: nIndex = O_hhea; break;
  1539. case T_hmtx: nIndex = O_hmtx; break;
  1540. case T_cmap: nIndex = O_cmap; break;
  1541. case T_vhea: nIndex = O_vhea; break;
  1542. case T_vmtx: nIndex = O_vmtx; break;
  1543. case T_OS2 : nIndex = O_OS2; break;
  1544. case T_post: nIndex = O_post; break;
  1545. case T_kern: nIndex = O_kern; break;
  1546. case T_cvt : nIndex = O_cvt; break;
  1547. case T_prep: nIndex = O_prep; break;
  1548. case T_fpgm: nIndex = O_fpgm; break;
  1549. case T_gsub: nIndex = O_gsub; break;
  1550. case T_CFF: nIndex = O_CFF; break;
  1551. default: nIndex = -1; break;
  1552. }
  1553. if( nIndex >= 0 ) {
  1554. sal_uInt32 nTableOffset = GetUInt32(t->ptr + tdoffset + 12, 16 * i + 8, 1);
  1555. length = GetUInt32(t->ptr + tdoffset + 12, 16 * i + 12, 1);
  1556. t->tables[nIndex] = t->ptr + nTableOffset;
  1557. t->tlens[nIndex] = length;
  1558. }
  1559. }
  1560. /* Fixup offsets when only a TTC extract was provided */
  1561. if( facenum == (sal_uInt32)~0 ) {
  1562. sal_uInt8* pHead = (sal_uInt8*)t->tables[O_head];
  1563. if( !pHead )
  1564. return SF_TTFORMAT;
  1565. /* limit Head candidate to TTC extract's limits */
  1566. if( pHead > t->ptr + (t->fsize - 54) )
  1567. pHead = t->ptr + (t->fsize - 54);
  1568. /* TODO: find better method than searching head table's magic */
  1569. sal_uInt8* p = NULL;
  1570. for( p = pHead + 12; p > t->ptr; --p ) {
  1571. if( p[0]==0x5F && p[1]==0x0F && p[2]==0x3C && p[3]==0xF5 ) {
  1572. int nDelta = (pHead + 12) - p, j;
  1573. if( nDelta )
  1574. for( j=0; j<NUM_TAGS; ++j )
  1575. if( t->tables[j] )
  1576. *(char**)&t->tables[j] -= nDelta;
  1577. break;
  1578. }
  1579. }
  1580. if( p <= t->ptr )
  1581. return SF_TTFORMAT;
  1582. }
  1583. /* Check the table offsets after TTC correction */
  1584. for (i=0; i<NUM_TAGS; i++) {
  1585. /* sanity check: table must lay completely within the file
  1586. * at this point one could check the checksum of all contained
  1587. * tables, but this would be quite time intensive.
  1588. * Try to fix tables, so we can cope with minor problems.
  1589. */
  1590. if( (sal_uInt8*)t->tables[i] < t->ptr )
  1591. {
  1592. #if OSL_DEBUG_LEVEL > 1
  1593. if( t->tables[i] )
  1594. fprintf( stderr, "font file %s has bad table offset %d (tagnum=%d)\n", t->fname, (sal_uInt8*)t->tables[i]-t->ptr, i );
  1595. #endif
  1596. t->tlens[i] = 0;
  1597. t->tables[i] = NULL;
  1598. }
  1599. else if( (sal_uInt8*)t->tables[i] + t->tlens[i] > t->ptr + t->fsize )
  1600. {
  1601. int nMaxLen = (t->ptr + t->fsize) - (sal_uInt8*)t->tables[i];
  1602. if( nMaxLen < 0 )
  1603. nMaxLen = 0;
  1604. t->tlens[i] = nMaxLen;
  1605. #if OSL_DEBUG_LEVEL > 1
  1606. fprintf( stderr, "font file %s has too big table (tagnum=%d)\n", t->fname, i );
  1607. #endif
  1608. }
  1609. }
  1610. /* At this point TrueTypeFont is constructed, now need to verify the font format
  1611. and read the basic font properties */
  1612. /* The following tables are absolutely required:
  1613. * maxp, head, name, cmap
  1614. */
  1615. if( !(getTable(t, O_maxp) && getTable(t, O_head) && getTable(t, O_name) && getTable(t, O_cmap)) ) {
  1616. CloseTTFont(t);
  1617. return SF_TTFORMAT;
  1618. }
  1619. const sal_uInt8* table = getTable(t, O_maxp);
  1620. t->nglyphs = GetUInt16(table, 4, 1);
  1621. table = getTable(t, O_head);
  1622. t->unitsPerEm = GetUInt16(table, 18, 1);
  1623. indexfmt = GetInt16(table, 50, 1);
  1624. if( ((indexfmt != 0) && (indexfmt != 1)) || (t->unitsPerEm <= 0) ) {
  1625. CloseTTFont(t);
  1626. return SF_TTFORMAT;
  1627. }
  1628. if( getTable(t, O_glyf) && getTable(t, O_loca) ) { /* TTF or TTF-OpenType */
  1629. k = (getTableSize(t, O_loca) / (indexfmt ? 4 : 2)) - 1;
  1630. if( k < (int)t->nglyphs ) /* Hack for broken Chinese fonts */
  1631. t->nglyphs = k;
  1632. table = getTable(t, O_loca);
  1633. t->goffsets = (sal_uInt32 *) calloc(1+t->nglyphs, sizeof(sal_uInt32));
  1634. assert(t->goffsets != 0);
  1635. for( i = 0; i <= (int)t->nglyphs; ++i )
  1636. t->goffsets[i] = indexfmt ? GetUInt32(table, i << 2, 1) : (sal_uInt32)GetUInt16(table, i << 1, 1) << 1;
  1637. } else if( getTable(t, O_CFF) ) { /* PS-OpenType */
  1638. t->goffsets = (sal_uInt32 *) calloc(1+t->nglyphs, sizeof(sal_uInt32));
  1639. /* TODO: implement to get subsetting */
  1640. assert(t->goffsets != 0);
  1641. } else {
  1642. CloseTTFont(t);
  1643. return SF_TTFORMAT;
  1644. }
  1645. table = getTable(t, O_hhea);
  1646. t->numberOfHMetrics = (table != 0) ? GetUInt16(table, 34, 1) : 0;
  1647. table = getTable(t, O_vhea);
  1648. t->numOfLongVerMetrics = (table != 0) ? GetUInt16(table, 34, 1) : 0;
  1649. GetNames(t);
  1650. FindCmap(t);
  1651. GetKern(t);
  1652. ReadGSUB( t, 0, 0 );
  1653. return SF_OK;
  1654. }
  1655. void CloseTTFont(TrueTypeFont *ttf) /*FOLD01*/
  1656. {
  1657. if (ttf->tag != TTFontClassTag) return;
  1658. #if !defined(WIN32) && !defined(OS2)
  1659. if( ttf->fname )
  1660. munmap((char *) ttf->ptr, ttf->fsize);
  1661. #endif
  1662. free(ttf->fname);
  1663. free(ttf->goffsets);
  1664. free(ttf->psname);
  1665. free(ttf->family);
  1666. if( ttf->ufamily )
  1667. free( ttf->ufamily );
  1668. free(ttf->subfamily);
  1669. if( ttf->usubfamily )
  1670. free( ttf->usubfamily );
  1671. free(ttf->tables);
  1672. free(ttf->tlens);
  1673. free(ttf->kerntables);
  1674. ReleaseGSUB(ttf);
  1675. free(ttf);
  1676. return;
  1677. }
  1678. int GetTTGlyphPoints(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray)
  1679. {
  1680. return GetTTGlyphOutline(ttf, glyphID, pointArray, 0, 0);
  1681. }
  1682. int GetTTGlyphComponents(TrueTypeFont *ttf, sal_uInt32 glyphID, std::vector< sal_uInt32 >& glyphlist)
  1683. {
  1684. int n = 1;
  1685. if( glyphID >= ttf->nglyphs )
  1686. return 0;
  1687. const sal_uInt8* glyf = getTable(ttf, O_glyf);
  1688. const sal_uInt8* ptr = glyf + ttf->goffsets[glyphID];
  1689. glyphlist.push_back( glyphID );
  1690. if (GetInt16(ptr, 0, 1) == -1) {
  1691. sal_uInt16 flags, index;
  1692. ptr += 10;
  1693. do {
  1694. flags = GetUInt16(ptr, 0, 1);
  1695. index = GetUInt16(ptr, 2, 1);
  1696. ptr += 4;
  1697. n += GetTTGlyphComponents(ttf, index, glyphlist);
  1698. if (flags & ARG_1_AND_2_ARE_WORDS) {
  1699. ptr += 4;
  1700. } else {
  1701. ptr += 2;
  1702. }
  1703. if (flags & WE_HAVE_A_SCALE) {
  1704. ptr += 2;
  1705. } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
  1706. ptr += 4;
  1707. } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
  1708. ptr += 8;
  1709. }
  1710. } while (flags & MORE_COMPONENTS);
  1711. }
  1712. return n;
  1713. }
  1714. #ifndef NO_TYPE3
  1715. int CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname, /*FOLD00*/
  1716. sal_uInt16 *glyphArray, sal_uInt8 *encoding, int nGlyphs,
  1717. int wmode)
  1718. {
  1719. ControlPoint *pa;
  1720. PSPathElement *path;
  1721. int i, j, r, n;
  1722. const sal_uInt8* table = getTable(ttf, O_head);
  1723. TTGlyphMetrics metrics;
  1724. int UPEm = ttf->unitsPerEm;
  1725. const char *h01 = "%%!PS-AdobeFont-%d.%d-%d.%d\n";
  1726. const char *h02 = "%% Creator: %s %s %s\n";
  1727. const char *h09 = "%% Original font name: %s\n";
  1728. const char *h10 =
  1729. "30 dict begin\n"
  1730. "/PaintType 0 def\n"
  1731. "/FontType 3 def\n"
  1732. "/StrokeWidth 0 def\n";
  1733. const char *h11 = "/FontName (%s) cvn def\n";
  1734. /*
  1735. const char *h12 = "%/UniqueID %d def\n";
  1736. */
  1737. const char *h13 = "/FontMatrix [.001 0 0 .001 0 0] def\n";
  1738. const char *h14 = "/FontBBox [%d %d %d %d] def\n";
  1739. const char *h15=
  1740. "/Encoding 256 array def\n"
  1741. " 0 1 255 {Encoding exch /.notdef put} for\n";
  1742. const char *h16 = " Encoding %d /glyph%d put\n";
  1743. const char *h17 = "/XUID [103 0 0 16#%08X %d 16#%08X 16#%08X] def\n";
  1744. const char *h30 = "/CharProcs %d dict def\n";
  1745. const char *h31 = " CharProcs begin\n";
  1746. const char *h32 = " /.notdef {} def\n";
  1747. const char *h33 = " /glyph%d {\n";
  1748. const char *h34 = " } bind def\n";
  1749. const char *h35 = " end\n";
  1750. const char *h40 =
  1751. "/BuildGlyph {\n"
  1752. " exch /CharProcs get exch\n"
  1753. " 2 copy known not\n"
  1754. " {pop /.notdef} if\n"
  1755. " get exec\n"
  1756. "} bind def\n"
  1757. "/BuildChar {\n"
  1758. " 1 index /Encoding get exch get\n"
  1759. " 1 index /BuildGlyph get exec\n"
  1760. "} bind def\n"
  1761. "currentdict end\n";
  1762. const char *h41 = "(%s) cvn exch definefont pop\n";
  1763. if (!((nGlyphs > 0) && (nGlyphs <= 256))) return SF_GLYPHNUM;
  1764. if (!glyphArray) return SF_BADARG;
  1765. if (!fname) fname = ttf->psname;
  1766. fprintf(outf, h01, GetInt16(table, 0, 1), GetUInt16(table, 2, 1), GetInt16(table, 4, 1), GetUInt16(table, 6, 1));
  1767. fprintf(outf, h02, modname, modver, modextra);
  1768. fprintf(outf, h09, ttf->psname);
  1769. fprintf(outf, h10);
  1770. fprintf(outf, h11, fname);
  1771. /* fprintf(outf, h12, 4000000); */
  1772. /* XUID generation:
  1773. * 103 0 0 C1 C2 C3 C4
  1774. * C1 - CRC-32 of the entire source TrueType font
  1775. * C2 - number of glyphs in the subset
  1776. * C3 - CRC-32 of the glyph array
  1777. * C4 - CRC-32 of the encoding array
  1778. *
  1779. * All CRC-32 numbers are presented as hexadecimal numbers
  1780. */
  1781. fprintf(outf, h17, rtl_crc32(0, ttf->ptr, ttf->fsize), nGlyphs, rtl_crc32(0, glyphArray, nGlyphs * 2), rtl_crc32(0, encoding, nGlyphs));
  1782. fprintf(outf, h13);
  1783. 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)));
  1784. fprintf(outf, h15);
  1785. for (i = 0; i < nGlyphs; i++) {
  1786. fprintf(outf, h16, encoding[i], i);
  1787. }
  1788. fprintf(outf, h30, nGlyphs+1);
  1789. fprintf(outf, h31);
  1790. fprintf(outf, h32);
  1791. for (i = 0; i < nGlyphs; i++) {
  1792. fprintf(outf, h33, i);
  1793. r = GetTTGlyphOutline(ttf, glyphArray[i] < ttf->nglyphs ? glyphArray[i] : 0, &pa, &metrics, 0);
  1794. if (r > 0) {
  1795. n = BSplineToPSPath(pa, r, &path);
  1796. } else {
  1797. n = 0; /* glyph might have zero contours but valid metrics ??? */
  1798. path = 0;
  1799. if (r < 0) { /* glyph is not present in the font - pa array was not allocated, so no need to free it */
  1800. continue;
  1801. }
  1802. }
  1803. fprintf(outf, "\t%d %d %d %d %d %d setcachedevice\n",
  1804. wmode == 0 ? XUnits(UPEm, metrics.aw) : 0,
  1805. wmode == 0 ? 0 : -XUnits(UPEm, metrics.ah),
  1806. XUnits(UPEm, metrics.xMin),
  1807. XUnits(UPEm, metrics.yMin),
  1808. XUnits(UPEm, metrics.xMax),
  1809. XUnits(UPEm, metrics.yMax));
  1810. for (j = 0; j < n; j++)
  1811. {
  1812. switch (path[j].type)
  1813. {
  1814. case PS_MOVETO:
  1815. fprintf(outf, "\t%d %d moveto\n", XUnits(UPEm, path[j].x1), XUnits(UPEm, path[j].y1));
  1816. break;
  1817. case PS_LINETO:
  1818. fprintf(outf, "\t%d %d lineto\n", XUnits(UPEm, path[j].x1), XUnits(UPEm, path[j].y1));
  1819. break;
  1820. case PS_CURVETO:
  1821. 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));
  1822. break;
  1823. case PS_CLOSEPATH:
  1824. fprintf(outf, "\tclosepath\n");
  1825. break;
  1826. case PS_NOOP:
  1827. break;
  1828. }
  1829. }
  1830. if (n > 0) fprintf(outf, "\tfill\n"); /* if glyph is not a whitespace character */
  1831. fprintf(outf, h34);
  1832. free(pa);
  1833. free(path);
  1834. }
  1835. fprintf(outf, h35);
  1836. fprintf(outf, h40);
  1837. fprintf(outf, h41, fname);
  1838. return SF_OK;
  1839. }
  1840. #endif
  1841. #ifndef NO_TTCR
  1842. int CreateTTFromTTGlyphs(TrueTypeFont *ttf,
  1843. const char *fname,
  1844. sal_uInt16 *glyphArray,
  1845. sal_uInt8 *encoding,
  1846. int nGlyphs,
  1847. int nNameRecs,
  1848. NameRecord *nr,
  1849. sal_uInt32 flags)
  1850. {
  1851. TrueTypeCreator *ttcr;
  1852. TrueTypeTable *head=0, *hhea=0, *maxp=0, *cvt=0, *prep=0, *glyf=0, *fpgm=0, *cmap=0, *name=0, *post = 0, *os2 = 0;
  1853. int i;
  1854. int res;
  1855. TrueTypeCreatorNewEmpty(T_true, &ttcr);
  1856. /** name **/
  1857. if (flags & TTCF_AutoName) {
  1858. /* not implemented yet
  1859. NameRecord *names;
  1860. NameRecord newname;
  1861. int n = GetTTNameRecords(ttf, &names);
  1862. int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0, n6 = 0;
  1863. sal_uInt8 *cp1;
  1864. sal_uInt8 suffix[32];
  1865. sal_uInt32 c1 = crc32(glyphArray, nGlyphs * 2);
  1866. sal_uInt32 c2 = crc32(encoding, nGlyphs);
  1867. int len;
  1868. snprintf(suffix, 31, "S%08X%08X-%d", c1, c2, nGlyphs);
  1869. name = TrueTypeTableNew_name(0, 0);
  1870. for (i = 0; i < n; i++) {
  1871. if (names[i].platformID == 1 && names[i].encodingID == 0 && names[i].languageID == 0 && names[i].nameID == 1) {
  1872. memcpy(newname, names+i, sizeof(NameRecord));
  1873. newname.slen = name[i].slen + strlen(suffix);
  1874. */
  1875. 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'};
  1876. NameRecord n1 = {1, 0, 0, 6, 14, (sal_uInt8*)"TrueTypeSubset"};
  1877. NameRecord n2 = {3, 1, 1033, 6, 28, 0};
  1878. n2.sptr = (sal_uInt8 *) ptr;
  1879. name = TrueTypeTableNew_name(0, 0);
  1880. nameAdd(name, &n1);
  1881. nameAdd(name, &n2);
  1882. } else {
  1883. if (nNameRecs == 0) {
  1884. NameRecord *names;
  1885. int n = GetTTNameRecords(ttf, &names);
  1886. name = TrueTypeTableNew_name(n, names);
  1887. DisposeNameRecords(names, n);
  1888. } else {
  1889. name = TrueTypeTableNew_name(nNameRecs, nr);
  1890. }
  1891. }
  1892. /** maxp **/
  1893. maxp = TrueTypeTableNew_maxp(getTable(ttf, O_maxp), getTableSize(ttf, O_maxp));
  1894. /** hhea **/
  1895. const sal_uInt8* p = getTable(ttf, O_hhea);
  1896. if (p) {
  1897. hhea = TrueTypeTableNew_hhea(GetUInt16(p, 4, 1), GetUInt16(p, 6, 1), GetUInt16(p, 8, 1), GetUInt16(p, 18, 1), GetUInt16(p, 20, 1));
  1898. } else {
  1899. hhea = TrueTypeTableNew_hhea(0, 0, 0, 0, 0);
  1900. }
  1901. /** head **/
  1902. p = getTable(ttf, O_head);
  1903. assert(p != 0);
  1904. head = TrueTypeTableNew_head(GetUInt32(p, 4, 1),
  1905. GetUInt16(p, 16, 1),
  1906. GetUInt16(p, 18, 1),
  1907. p+20,
  1908. GetUInt16(p, 44, 1),
  1909. GetUInt16(p, 46, 1),
  1910. GetInt16(p, 48, 1));
  1911. /** glyf **/
  1912. glyf = TrueTypeTableNew_glyf();
  1913. sal_uInt32* gID = (sal_uInt32*)scalloc(nGlyphs, sizeof(sal_uInt32));
  1914. for (i = 0; i < nGlyphs; i++) {
  1915. gID[i] = glyfAdd(glyf, GetTTRawGlyphData(ttf, glyphArray[i]), ttf);
  1916. }
  1917. /** cmap **/
  1918. cmap = TrueTypeTableNew_cmap();
  1919. for (i=0; i < nGlyphs; i++) {
  1920. cmapAdd(cmap, 0x010000, encoding[i], gID[i]);
  1921. }
  1922. /** cvt **/
  1923. if ((p = getTable(ttf, O_cvt)) != 0) {
  1924. cvt = TrueTypeTableNew(T_cvt, getTableSize(ttf, O_cvt), p);
  1925. }
  1926. /** prep **/
  1927. if ((p = getTable(ttf, O_prep)) != 0) {
  1928. prep = TrueTypeTableNew(T_prep, getTableSize(ttf, O_prep), p);
  1929. }
  1930. /** fpgm **/
  1931. if ((p = getTable(ttf, O_fpgm)) != 0) {
  1932. fpgm = TrueTypeTableNew(T_fpgm, getTableSize(ttf, O_fpgm), p);
  1933. }
  1934. /** post **/
  1935. if ((p = getTable(ttf, O_post)) != 0) {
  1936. post = TrueTypeTableNew_post(0x00030000,
  1937. GetUInt32(p, 4, 1),
  1938. GetUInt16(p, 8, 1),
  1939. GetUInt16(p, 10, 1),
  1940. GetUInt16(p, 12, 1));
  1941. } else {
  1942. post = TrueTypeTableNew_post(0x00030000, 0, 0, 0, 0);
  1943. }
  1944. if (flags & TTCF_IncludeOS2) {
  1945. if ((p = getTable(ttf, O_OS2)) != 0) {
  1946. os2 = TrueTypeTableNew(T_OS2, getTableSize(ttf, O_OS2), p);
  1947. }
  1948. }
  1949. AddTable(ttcr, name); AddTable(ttcr, maxp); AddTable(ttcr, hhea);
  1950. AddTable(ttcr, head); AddTable(ttcr, glyf); AddTable(ttcr, cmap);
  1951. AddTable(ttcr, cvt ); AddTable(ttcr, prep); AddTable(ttcr, fpgm);
  1952. AddTable(ttcr, post); AddTable(ttcr, os2);
  1953. if ((res = StreamToFile(ttcr, fname)) != SF_OK) {
  1954. #if OSL_DEBUG_LEVEL > 1
  1955. fprintf(stderr, "StreamToFile: error code: %d.\n", res);
  1956. #endif
  1957. }
  1958. TrueTypeCreatorDispose(ttcr);
  1959. free(gID);
  1960. return res;
  1961. }
  1962. #endif
  1963. #ifndef NO_TYPE42
  1964. static GlyphOffsets *GlyphOffsetsNew(sal_uInt8 *sfntP)
  1965. {
  1966. GlyphOffsets* res = (GlyphOffsets*)smalloc(sizeof(GlyphOffsets));
  1967. sal_uInt8 *loca = NULL;
  1968. sal_uInt16 i, numTables = GetUInt16(sfntP, 4, 1);
  1969. sal_uInt32 locaLen = 0;
  1970. sal_Int16 indexToLocFormat = 0;
  1971. for (i = 0; i < numTables; i++) {
  1972. sal_uInt32 tag = GetUInt32(sfntP + 12, 16 * i, 1);
  1973. sal_uInt32 off = GetUInt32(sfntP + 12, 16 * i + 8, 1);
  1974. sal_uInt32 len = GetUInt32(sfntP + 12, 16 * i + 12, 1);
  1975. if (tag == T_loca) {
  1976. loca = sfntP + off;
  1977. locaLen = len;
  1978. } else if (tag == T_head) {
  1979. indexToLocFormat = GetInt16(sfntP + off, 50, 1);
  1980. }
  1981. }
  1982. res->nGlyphs = locaLen / ((indexToLocFormat == 1) ? 4 : 2);
  1983. assert(res->nGlyphs != 0);
  1984. res->offs = (sal_uInt32*)scalloc(res->nGlyphs, sizeof(sal_uInt32));
  1985. for (i = 0; i < res->nGlyphs; i++) {
  1986. if (indexToLocFormat == 1) {
  1987. res->offs[i] = GetUInt32(loca, i * 4, 1);
  1988. } else {
  1989. res->offs[i] = GetUInt16(loca, i * 2, 1) << 1;
  1990. }
  1991. }
  1992. return res;
  1993. }
  1994. static void GlyphOffsetsDispose(GlyphOffsets *_this)
  1995. {
  1996. if (_this) {
  1997. free(_this->offs);
  1998. free(_this);
  1999. }
  2000. }
  2001. static void DumpSfnts(FILE *outf, sal_uInt8 *sfntP)
  2002. {
  2003. HexFmt *h = HexFmtNew(outf);
  2004. sal_uInt16 i, numTables = GetUInt16(sfntP, 4, 1);
  2005. GlyphOffsets *go = GlyphOffsetsNew(sfntP);
  2006. sal_uInt8 pad[] = {0,0,0,0}; /* zeroes */
  2007. assert(numTables <= 9); /* Type42 has 9 required tables */
  2008. sal_uInt32* offs = (sal_uInt32*)scalloc(numTables, sizeof(sal_uInt32));
  2009. // sal_uInt32* lens = (sal_uInt32*)scalloc(numTables, sizeof(sal_uInt32));
  2010. fputs("/sfnts [", outf);
  2011. HexFmtOpenString(h);
  2012. HexFmtBlockWrite(h, sfntP, 12); /* stream out the Offset Table */
  2013. HexFmtBlockWrite(h, sfntP+12, 16 * numTables); /* stream out the Table Directory */
  2014. for (i=0; i<numTables; i++) {
  2015. sal_uInt32 tag = GetUInt32(sfntP + 12, 16 * i, 1);
  2016. sal_uInt32 off = GetUInt32(sfntP + 12, 16 * i + 8, 1);
  2017. sal_uInt32 len = GetUInt32(sfntP + 12, 16 * i + 12, 1);
  2018. if (tag != T_glyf) {
  2019. HexFmtBlockWrite(h, sfntP + off, len);
  2020. } else {
  2021. sal_uInt8 *glyf = sfntP + off;
  2022. sal_uInt32 o, l, j;
  2023. for (j = 0; j < go->nGlyphs - 1; j++) {
  2024. o = go->offs[j];
  2025. l = go->offs[j + 1] - o;
  2026. HexFmtBlockWrite(h, glyf + o, l);
  2027. }
  2028. }
  2029. HexFmtBlockWrite(h, pad, (4 - (len & 3)) & 3);
  2030. }
  2031. HexFmtCloseString(h);
  2032. fputs("] def\n", outf);
  2033. GlyphOffsetsDispose(go);
  2034. HexFmtDispose(h);
  2035. free(offs);
  2036. // free(lens);
  2037. }
  2038. int CreateT42FromTTGlyphs(TrueTypeFont *ttf,
  2039. FILE *outf,
  2040. const char *psname,
  2041. sal_uInt16 *glyphArray,
  2042. sal_uInt8 *encoding,
  2043. int nGlyphs)
  2044. {
  2045. TrueTypeCreator *ttcr;
  2046. TrueTypeTable *head=0, *hhea=0, *maxp=0, *cvt=0, *prep=0, *glyf=0, *fpgm=0;
  2047. int i;
  2048. int res;
  2049. sal_uInt32 ver, rev;
  2050. sal_uInt8 *sfntP;
  2051. sal_uInt32 sfntLen;
  2052. int UPEm = ttf->unitsPerEm;
  2053. if (nGlyphs >= 256) return SF_GLYPHNUM;
  2054. assert(psname != 0);
  2055. TrueTypeCreatorNewEmpty(T_true, &ttcr);
  2056. /* head */
  2057. const sal_uInt8* p = getTable(ttf, O_head);
  2058. const sal_uInt8* headP = p;
  2059. assert(p != 0);
  2060. 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));
  2061. ver = GetUInt32(p, 0, 1);
  2062. rev = GetUInt32(p, 4, 1);
  2063. /** hhea **/
  2064. p = getTable(ttf, O_hhea);
  2065. if (p) {
  2066. hhea = TrueTypeTableNew_hhea(GetUInt16(p, 4, 1), GetUInt16(p, 6, 1), GetUInt16(p, 8, 1), GetUInt16(p, 18, 1), GetUInt16(p, 20, 1));
  2067. } else {
  2068. hhea = TrueTypeTableNew_hhea(0, 0, 0, 0, 0);
  2069. }
  2070. /** maxp **/
  2071. maxp = TrueTypeTableNew_maxp(getTable(ttf, O_maxp), getTableSize(ttf, O_maxp));
  2072. /** cvt **/
  2073. if ((p = getTable(ttf, O_cvt)) != 0) {
  2074. cvt = TrueTypeTableNew(T_cvt, getTableSize(ttf, O_cvt), p);
  2075. }
  2076. /** prep **/
  2077. if ((p = getTable(ttf, O_prep)) != 0) {
  2078. prep = TrueTypeTableNew(T_prep, getTableSize(ttf, O_prep), p);
  2079. }
  2080. /** fpgm **/
  2081. if ((p = getTable(ttf, O_fpgm)) != 0) {
  2082. fpgm = TrueTypeTableNew(T_fpgm, getTableSize(ttf, O_fpgm), p);
  2083. }
  2084. /** glyf **/
  2085. glyf = TrueTypeTableNew_glyf();
  2086. sal_uInt16* gID = (sal_uInt16*)scalloc(nGlyphs, sizeof(sal_uInt32));
  2087. for (i = 0; i < nGlyphs; i++) {
  2088. gID[i] = (sal_uInt16)glyfAdd(glyf, GetTTRawGlyphData(ttf, glyphArray[i]), ttf);
  2089. }
  2090. AddTable(ttcr, head); AddTable(ttcr, hhea); AddTable(ttcr, maxp); AddTable(ttcr, cvt);
  2091. AddTable(ttcr, prep); AddTable(ttcr, glyf); AddTable(ttcr, fpgm);
  2092. if ((res = StreamToMemory(ttcr, &sfntP, &sfntLen)) != SF_OK) {
  2093. TrueTypeCreatorDispose(ttcr);
  2094. free(gID);
  2095. return res;
  2096. }
  2097. fprintf(outf, "%%!PS-TrueTypeFont-%d.%d-%d.%d\n", (int)(ver>>16), (int)(ver & 0xFFFF), (int)(rev>>16), (int)(rev & 0xFFFF));
  2098. fprintf(outf, "%%%%Creator: %s %s %s\n", modname, modver, modextra);
  2099. fprintf(outf, "%%- Font subset generated from a source font file: '%s'\n", ttf->fname);
  2100. fprintf(outf, "%%- Original font name: %s\n", ttf->psname);
  2101. fprintf(outf, "%%- Original font family: %s\n", ttf->family);
  2102. fprintf(outf, "%%- Original font sub-family: %s\n", ttf->subfamily);
  2103. fprintf(outf, "11 dict begin\n");
  2104. fprintf(outf, "/FontName (%s) cvn def\n", psname);
  2105. fprintf(outf, "/PaintType 0 def\n");
  2106. fprintf(outf, "/FontMatrix [1 0 0 1 0 0] def\n");
  2107. 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)));
  2108. fprintf(outf, "/FontType 42 def\n");
  2109. fprintf(outf, "/Encoding 256 array def\n");
  2110. fprintf(outf, " 0 1 255 {Encoding exch /.notdef put} for\n");
  2111. for (i = 1; i<nGlyphs; i++) {
  2112. fprintf(outf, "Encoding %d /glyph%d put\n", encoding[i], gID[i]);
  2113. }
  2114. 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));
  2115. DumpSfnts(outf, sfntP);
  2116. /* dump charstrings */
  2117. fprintf(outf, "/CharStrings %d dict dup begin\n", nGlyphs);
  2118. fprintf(outf, "/.notdef 0 def\n");
  2119. for (i = 1; i < (int)glyfCount(glyf); i++) {
  2120. fprintf(outf,"/glyph%d %d def\n", i, i);
  2121. }
  2122. fprintf(outf, "end readonly def\n");
  2123. fprintf(outf, "FontName currentdict end definefont pop\n");
  2124. TrueTypeCreatorDispose(ttcr);
  2125. free(gID);
  2126. free(sfntP);
  2127. return SF_OK;
  2128. }
  2129. #endif
  2130. #ifndef NO_MAPPERS
  2131. int MapString(TrueTypeFont *ttf, sal_uInt16 *str, int nchars, sal_uInt16 *glyphArray, int bvertical)
  2132. {
  2133. int i;
  2134. sal_uInt16 *cp;
  2135. if (ttf->cmapType == CMAP_NOT_USABLE ) return -1;
  2136. if (!nchars) return 0;
  2137. if (glyphArray == 0) {
  2138. cp = str;
  2139. } else {
  2140. cp = glyphArray;
  2141. }
  2142. switch (ttf->cmapType) {
  2143. case CMAP_MS_Symbol:
  2144. if( ttf->mapper == getGlyph0 ) {
  2145. sal_uInt16 aChar;
  2146. for( i = 0; i < nchars; i++ ) {
  2147. aChar = str[i];
  2148. if( ( aChar & 0xf000 ) == 0xf000 )
  2149. aChar &= 0x00ff;
  2150. cp[i] = aChar;
  2151. }
  2152. }
  2153. else if( glyphArray )
  2154. memcpy(glyphArray, str, nchars * 2);
  2155. break;
  2156. case CMAP_MS_Unicode:
  2157. if (glyphArray != 0) {
  2158. memcpy(glyphArray, str, nchars * 2);
  2159. }
  2160. break;
  2161. case CMAP_MS_ShiftJIS: TranslateString12(str, cp, nchars); break;
  2162. case CMAP_MS_Big5: TranslateString13(str, cp, nchars); break;
  2163. case CMAP_MS_PRC: TranslateString14(str, cp, nchars); break;
  2164. case CMAP_MS_Wansung: TranslateString15(str, cp, nchars); break;
  2165. case CMAP_MS_Johab: TranslateString16(str, cp, nchars); break;
  2166. }
  2167. for (i = 0; i < nchars; i++) {
  2168. cp[i] = (sal_uInt16)ttf->mapper(ttf->cmap, cp[i]);
  2169. if (cp[i]!=0 && bvertical!=0)
  2170. cp[i] = (sal_uInt16)UseGSUB(ttf,cp[i],bvertical);
  2171. }
  2172. return nchars;
  2173. }
  2174. sal_uInt16 MapChar(TrueTypeFont *ttf, sal_uInt16 ch, int bvertical)
  2175. {
  2176. switch (ttf->cmapType) {
  2177. case CMAP_MS_Symbol:
  2178. if( ttf->mapper == getGlyph0 && ( ch & 0xf000 ) == 0xf000 )
  2179. ch &= 0x00ff;
  2180. return (sal_uInt16)ttf->mapper(ttf->cmap, ch );
  2181. case CMAP_MS_Unicode: break;
  2182. case CMAP_MS_ShiftJIS: ch = TranslateChar12(ch); break;
  2183. case CMAP_MS_Big5: ch = TranslateChar13(ch); break;
  2184. case CMAP_MS_PRC: ch = TranslateChar14(ch); break;
  2185. case CMAP_MS_Wansung: ch = TranslateChar15(ch); break;
  2186. case CMAP_MS_Johab: ch = TranslateChar16(ch); break;
  2187. default: return 0;
  2188. }
  2189. ch = (sal_uInt16)ttf->mapper(ttf->cmap, ch);
  2190. if (ch!=0 && bvertical!=0)
  2191. ch = (sal_uInt16)UseGSUB(ttf,ch,bvertical);
  2192. return ch;
  2193. }
  2194. int DoesVerticalSubstitution( TrueTypeFont *ttf, int bvertical)
  2195. {
  2196. int nRet = 0;
  2197. if( bvertical)
  2198. nRet = HasVerticalGSUB( ttf);
  2199. return nRet;
  2200. }
  2201. #endif
  2202. int GetTTGlyphCount( TrueTypeFont* ttf )
  2203. {
  2204. return ttf->nglyphs;
  2205. }
  2206. bool GetSfntTable( TrueTypeFont* ttf, int nSubtableIndex,
  2207. const sal_uInt8** ppRawBytes, int* pRawLength )
  2208. {
  2209. if( (nSubtableIndex < 0) || (nSubtableIndex >= NUM_TAGS) )
  2210. return false;
  2211. *pRawLength = ttf->tlens[ nSubtableIndex ];
  2212. *ppRawBytes = ttf->tables[ nSubtableIndex ];
  2213. bool bOk = (*pRawLength > 0) && (ppRawBytes != NULL);
  2214. return bOk;
  2215. }
  2216. TTSimpleGlyphMetrics *GetTTSimpleGlyphMetrics(TrueTypeFont *ttf, sal_uInt16 *glyphArray, int nGlyphs, int mode)
  2217. {
  2218. const sal_uInt8* pTable;
  2219. sal_uInt32 n;
  2220. int nTableSize;
  2221. if (mode == 0) {
  2222. n = ttf->numberOfHMetrics;
  2223. pTable = getTable( ttf, O_hmtx );
  2224. nTableSize = getTableSize( ttf, O_hmtx );
  2225. } else {
  2226. n = ttf->numOfLongVerMetrics;
  2227. pTable = getTable( ttf, O_vmtx );
  2228. nTableSize = getTableSize( ttf, O_vmtx );
  2229. }
  2230. if (!nGlyphs || !glyphArray) return 0; /* invalid parameters */
  2231. if (!n || !pTable) return 0; /* the font does not contain the requested metrics */
  2232. TTSimpleGlyphMetrics* res = (TTSimpleGlyphMetrics*)calloc(nGlyphs, sizeof(TTSimpleGlyphMetrics));
  2233. assert(res != 0);
  2234. const int UPEm = ttf->unitsPerEm;
  2235. for( int i = 0; i < nGlyphs; ++i) {
  2236. int nAdvOffset, nLsbOffset;
  2237. sal_uInt16 glyphID = glyphArray[i];
  2238. if (glyphID < n) {
  2239. nAdvOffset = 4 * glyphID;
  2240. nLsbOffset = nAdvOffset + 2;
  2241. } else {
  2242. nAdvOffset = 4 * (n - 1);
  2243. if( glyphID < ttf->nglyphs )
  2244. nLsbOffset = 4 * n + 2 * (glyphID - n);
  2245. else /* font is broken -> use lsb of last hmetrics */
  2246. nLsbOffset = nAdvOffset + 2;
  2247. }
  2248. if( nAdvOffset >= nTableSize)
  2249. res[i].adv = 0; /* better than a crash for buggy fonts */
  2250. else
  2251. res[i].adv = static_cast<sal_uInt16>(
  2252. XUnits( UPEm, GetUInt16( pTable, nAdvOffset, 1) ) );
  2253. if( nLsbOffset >= nTableSize)
  2254. res[i].sb = 0; /* better than a crash for buggy fonts */
  2255. else
  2256. res[i].sb = static_cast<sal_Int16>(
  2257. XUnits( UPEm, GetInt16( pTable, nLsbOffset, 1) ) );
  2258. }
  2259. return res;
  2260. }
  2261. #ifndef NO_MAPPERS
  2262. TTSimpleGlyphMetrics *GetTTSimpleCharMetrics(TrueTypeFont * ttf, sal_uInt16 firstChar, int nChars, int mode)
  2263. {
  2264. TTSimpleGlyphMetrics *res = 0;
  2265. int i, n;
  2266. sal_uInt16* str = (sal_uInt16*)malloc(nChars * 2);
  2267. assert(str != 0);
  2268. for (i=0; i<nChars; i++) str[i] = (sal_uInt16)(firstChar + i);
  2269. if ((n = MapString(ttf, str, nChars, 0, mode)) != -1) {
  2270. res = GetTTSimpleGlyphMetrics(ttf, str, n, mode);
  2271. }
  2272. free(str);
  2273. return res;
  2274. }
  2275. #endif
  2276. void GetTTGlobalFontInfo(TrueTypeFont *ttf, TTGlobalFontInfo *info)
  2277. {
  2278. int UPEm = ttf->unitsPerEm;
  2279. memset(info, 0, sizeof(TTGlobalFontInfo));
  2280. info->family = ttf->family;
  2281. info->ufamily = ttf->ufamily;
  2282. info->subfamily = ttf->subfamily;
  2283. info->usubfamily = ttf->usubfamily;
  2284. info->psname = ttf->psname;
  2285. info->symbolEncoded = (ttf->cmapType == CMAP_MS_Symbol);
  2286. const sal_uInt8* table = getTable(ttf, O_OS2);
  2287. if (table) {
  2288. info->weight = GetUInt16(table, 4, 1);
  2289. info->width = GetUInt16(table, 6, 1);
  2290. /* There are 3 different versions of OS/2 table: original (68 bytes long),
  2291. * Microsoft old (78 bytes long) and Microsoft new (86 bytes long,)
  2292. * Apple's documentation recommends looking at the table length.
  2293. */
  2294. if (getTableSize(ttf, O_OS2) > 68) {
  2295. info->typoAscender = XUnits(UPEm,GetInt16(table, 68, 1));
  2296. info->typoDescender = XUnits(UPEm, GetInt16(table, 70, 1));
  2297. info->typoLineGap = XUnits(UPEm, GetInt16(table, 72, 1));
  2298. info->winAscent = XUnits(UPEm, GetUInt16(table, 74, 1));
  2299. info->winDescent = XUnits(UPEm, GetUInt16(table, 76, 1));
  2300. /* sanity check; some fonts treat winDescent as signed
  2301. * violating the standard */
  2302. if( info->winDescent > 5*UPEm )
  2303. info->winDescent = XUnits(UPEm, GetInt16(table, 76,1));
  2304. }
  2305. if (ttf->cmapType == CMAP_MS_Unicode) {
  2306. info->rangeFlag = 1;
  2307. info->ur1 = GetUInt32(table, 42, 1);
  2308. info->ur2 = GetUInt32(table, 46, 1);
  2309. info->ur3 = GetUInt32(table, 50, 1);
  2310. info->ur4 = GetUInt32(table, 54, 1);
  2311. }
  2312. memcpy(info->panose, table + 32, 10);
  2313. info->typeFlags = GetUInt16( table, 8, 1 );
  2314. if( getTable(ttf, O_CFF) )
  2315. info->typeFlags |= TYPEFLAG_PS_OPENTYPE;
  2316. }
  2317. table = getTable(ttf, O_post);
  2318. if (table && getTableSize(ttf, O_post) >= 12+sizeof(sal_uInt32)) {
  2319. info->pitch = GetUInt32(table, 12, 1);
  2320. info->italicAngle = GetInt32(table, 4, 1);
  2321. }
  2322. table = getTable(ttf, O_head); /* 'head' tables is always there */
  2323. info->xMin = XUnits(UPEm, GetInt16(table, 36, 1));
  2324. info->yMin = XUnits(UPEm, GetInt16(table, 38, 1));
  2325. info->xMax = XUnits(UPEm, GetInt16(table, 40, 1));
  2326. info->yMax = XUnits(UPEm, GetInt16(table, 42, 1));
  2327. info->macStyle = GetInt16(table, 44, 1);
  2328. table = getTable(ttf, O_hhea);
  2329. if (table) {
  2330. info->ascender = XUnits(UPEm, GetInt16(table, 4, 1));
  2331. info->descender = XUnits(UPEm, GetInt16(table, 6, 1));
  2332. info->linegap = XUnits(UPEm, GetInt16(table, 8, 1));
  2333. }
  2334. table = getTable(ttf, O_vhea);
  2335. if (table) {
  2336. info->vascent = XUnits(UPEm, GetInt16(table, 4, 1));
  2337. info->vdescent = XUnits(UPEm, GetInt16(table, 6, 1));
  2338. }
  2339. }
  2340. #ifdef TEST5
  2341. void KernGlyphs(TrueTypeFont *ttf, sal_uInt16 *glyphs, int nglyphs, int wmode, KernData *kern)
  2342. {
  2343. int i;
  2344. if (!nglyphs || !glyphs || !kern) return;
  2345. for (i = 0; i < nglyphs-1; i++) kern[i].x = kern[i].y = 0;
  2346. switch (ttf->kerntype) {
  2347. case KT_APPLE_NEW: KernGlyphsPrim1(ttf, glyphs, nglyphs, wmode, kern); return;
  2348. case KT_MICROSOFT: KernGlyphsPrim2(ttf, glyphs, nglyphs, wmode, kern); return;
  2349. default: return;
  2350. }
  2351. }
  2352. #endif
  2353. GlyphData *GetTTRawGlyphData(TrueTypeFont *ttf, sal_uInt32 glyphID)
  2354. {
  2355. const sal_uInt8* glyf = getTable(ttf, O_glyf);
  2356. const sal_uInt8* hmtx = getTable(ttf, O_hmtx);
  2357. int i, n, m;
  2358. if( glyphID >= ttf->nglyphs )
  2359. return 0;
  2360. /* #127161# check the glyph offsets */
  2361. sal_uInt32 length = getTableSize( ttf, O_glyf );
  2362. if( length < ttf->goffsets[ glyphID+1 ] )
  2363. return 0;
  2364. length = ttf->goffsets[glyphID+1] - ttf->goffsets[glyphID];
  2365. GlyphData* d = (GlyphData*)malloc(sizeof(GlyphData)); assert(d != 0);
  2366. if (length > 0) {
  2367. const sal_uInt8* srcptr = glyf + ttf->goffsets[glyphID];
  2368. d->ptr = (sal_uInt8*)malloc((length + 1) & ~1); assert(d->ptr != 0);
  2369. memcpy( d->ptr, srcptr, length );
  2370. d->compflag = (GetInt16( srcptr, 0, 1 ) < 0);
  2371. } else {
  2372. d->ptr = 0;
  2373. d->compflag = 0;
  2374. }
  2375. d->glyphID = glyphID;
  2376. d->nbytes = (sal_uInt16)((length + 1) & ~1);
  2377. /* now calculate npoints and ncontours */
  2378. ControlPoint *cp;
  2379. n = GetTTGlyphPoints(ttf, glyphID, &cp);
  2380. if (n != -1) {
  2381. m = 0;
  2382. for (i = 0; i < n; i++) {
  2383. if (cp[i].flags & 0x8000) m++;
  2384. }
  2385. d->npoints = (sal_uInt16)n;
  2386. d->ncontours = (sal_uInt16)m;
  2387. free(cp);
  2388. } else {
  2389. d->npoints = 0;
  2390. d->ncontours = 0;
  2391. }
  2392. /* get advance width and left sidebearing */
  2393. if (glyphID < ttf->numberOfHMetrics) {
  2394. d->aw = GetUInt16(hmtx, 4 * glyphID, 1);
  2395. d->lsb = GetInt16(hmtx, 4 * glyphID + 2, 1);
  2396. } else {
  2397. d->aw = GetUInt16(hmtx, 4 * (ttf->numberOfHMetrics - 1), 1);
  2398. d->lsb = GetInt16(hmtx + ttf->numberOfHMetrics * 4, (glyphID - ttf->numberOfHMetrics) * 2, 1);
  2399. }
  2400. return d;
  2401. }
  2402. int GetTTNameRecords(TrueTypeFont *ttf, NameRecord **nr)
  2403. {
  2404. const sal_uInt8* table = getTable(ttf, O_name);
  2405. int nTableSize = getTableSize(ttf, O_name );
  2406. if (nTableSize < 6)
  2407. {
  2408. #if OSL_DEBUG_LEVEL > 1
  2409. fprintf(stderr, "O_name table too small\n");
  2410. #endif
  2411. return 0;
  2412. }
  2413. sal_uInt16 n = GetUInt16(table, 2, 1);
  2414. int nStrBase = GetUInt16(table, 4, 1);
  2415. int i;
  2416. *nr = 0;
  2417. if (n == 0) return 0;
  2418. NameRecord* rec = (NameRecord*)calloc(n, sizeof(NameRecord));
  2419. for (i = 0; i < n; i++) {
  2420. int nStrOffset = GetUInt16(table + 6, 10 + 12 * i, 1);
  2421. rec[i].platformID = GetUInt16(table + 6, 12 * i, 1);
  2422. rec[i].encodingID = GetUInt16(table + 6, 2 + 12 * i, 1);
  2423. rec[i].languageID = GetUInt16(table + 6, 4 + 12 * i, 1);
  2424. rec[i].nameID = GetUInt16(table + 6, 6 + 12 * i, 1);
  2425. rec[i].slen = GetUInt16(table + 6, 8 + 12 * i, 1);
  2426. if (rec[i].slen) {
  2427. if( nStrBase+nStrOffset+rec[i].slen >= nTableSize ) {
  2428. rec[i].sptr = 0;
  2429. rec[i].slen = 0;
  2430. continue;
  2431. }
  2432. const sal_uInt8* rec_string = table + nStrBase + nStrOffset;
  2433. // sanity check
  2434. if( rec_string > (sal_uInt8*)ttf->ptr && rec_string < ((sal_uInt8*)ttf->ptr + ttf->fsize - rec[i].slen ) )
  2435. {
  2436. rec[i].sptr = (sal_uInt8 *) malloc(rec[i].slen); assert(rec[i].sptr != 0);
  2437. memcpy(rec[i].sptr, rec_string, rec[i].slen);
  2438. }
  2439. else
  2440. {
  2441. #ifdef DEBUG
  2442. fprintf( stderr, "found invalid name record %d with name id %d for file %s\n",
  2443. i, rec[i].nameID, ttf->fname );
  2444. #endif
  2445. rec[i].sptr = 0;
  2446. rec[i].slen = 0;
  2447. }
  2448. } else {
  2449. rec[i].sptr = 0;
  2450. }
  2451. // some fonts have 3.0 names => fix them to 3.1
  2452. if( (rec[i].platformID == 3) && (rec[i].encodingID == 0) )
  2453. rec[i].encodingID = 1;
  2454. }
  2455. *nr = rec;
  2456. return n;
  2457. }
  2458. void DisposeNameRecords(NameRecord* nr, int n)
  2459. {
  2460. int i;
  2461. for (i = 0; i < n; i++) {
  2462. if (nr[i].sptr) free(nr[i].sptr);
  2463. }
  2464. free(nr);
  2465. }
  2466. } // namespace vcl
  2467. #ifdef TEST1
  2468. /* This example creates a subset of a TrueType font with two encoded characters */
  2469. int main(int ac, char **av)
  2470. {
  2471. TrueTypeFont *fnt;
  2472. int r;
  2473. /* Array of Unicode source characters */
  2474. sal_uInt16 chars[2];
  2475. /* Encoding vector maps character encoding to the ordinal number
  2476. * of the glyph in the output file */
  2477. sal_uInt8 encoding[2];
  2478. /* This array is for glyph IDs that source characters map to */
  2479. sal_uInt16 g[2];
  2480. if (ac < 2) return 0;
  2481. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2482. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2483. return 0;
  2484. }
  2485. /* We want to create the output file that only contains two Unicode characters:
  2486. * L'a' and L'A' */
  2487. chars[0] = L'a';
  2488. chars[1] = L'A';
  2489. /* Figure out what glyphs do these characters map in our font */
  2490. MapString(fnt, chars, 2, g);
  2491. /* Encode the characters. Value of encoding[i] is the number 0..255 which maps to glyph i of the
  2492. * newly generated font */
  2493. encoding[0] = chars[0];
  2494. encoding[1] = chars[1];
  2495. /* Generate a subset */
  2496. CreateT3FromTTGlyphs(fnt, stdout, 0, g, encoding, 2, 0);
  2497. /* Now call the dtor for the font */
  2498. CloseTTFont(fnt);
  2499. return 0;
  2500. }
  2501. #endif
  2502. #ifdef TEST2
  2503. /* This example extracts first 224 glyphs from a TT fonts and encodes them starting at 32 */
  2504. int main(int ac, char **av)
  2505. {
  2506. TrueTypeFont *fnt;
  2507. int i, r;
  2508. /* Array of Unicode source characters */
  2509. sal_uInt16 glyphs[224];
  2510. /* Encoding vector maps character encoding to the ordinal number
  2511. * of the glyph in the output file */
  2512. sal_uInt8 encoding[224];
  2513. for (i=0; i<224; i++) {
  2514. glyphs[i] = i;
  2515. encoding[i] = 32 + i;
  2516. }
  2517. if (ac < 2) return 0;
  2518. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2519. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2520. return 0;
  2521. }
  2522. /* Encode the characters. Value of encoding[i] is the number 0..255 which maps to glyph i of the
  2523. * newly generated font */
  2524. /* Generate a subset */
  2525. CreateT3FromTTGlyphs(fnt, stdout, 0, glyphs, encoding, 224, 0);
  2526. /* Now call the dtor for the font */
  2527. CloseTTFont(fnt);
  2528. return 0;
  2529. }
  2530. #endif
  2531. #ifdef TEST3
  2532. /* Glyph metrics example */
  2533. int main(int ac, char **av)
  2534. {
  2535. TrueTypeFont *fnt;
  2536. int i, r;
  2537. sal_uInt16 glyphs[224];
  2538. TTSimpleGlyphMetrics *m;
  2539. for (i=0; i<224; i++) {
  2540. glyphs[i] = i;
  2541. }
  2542. if (ac < 2) return 0;
  2543. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2544. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2545. return 0;
  2546. }
  2547. if ((m = GetTTSimpleGlyphMetrics(fnt, glyphs, 224, 0)) == 0) {
  2548. printf("Requested metrics is not available\n");
  2549. } else {
  2550. for (i=0; i<224; i++) {
  2551. printf("%d. advWid: %5d, LSBear: %5d\n", i, m[i].adv, m[i].sb);
  2552. }
  2553. }
  2554. /* Now call the dtor for the font */
  2555. free(m);
  2556. CloseTTFont(fnt);
  2557. return 0;
  2558. }
  2559. #endif
  2560. #ifdef TEST4
  2561. int main(int ac, char **av)
  2562. {
  2563. TrueTypeFont *fnt;
  2564. TTGlobalFontInfo info;
  2565. int i, r;
  2566. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2567. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2568. return 0;
  2569. }
  2570. printf("Font file: %s\n", av[1]);
  2571. #ifdef PRINT_KERN
  2572. switch (fnt->kerntype) {
  2573. case KT_MICROSOFT:
  2574. printf("\tkern: MICROSOFT, ntables: %d.", fnt->nkern);
  2575. if (fnt->nkern) {
  2576. printf(" [");
  2577. for (i=0; i<fnt->nkern; i++) {
  2578. printf("%04X ", GetUInt16(fnt->kerntables[i], 4, 1));
  2579. }
  2580. printf("]");
  2581. }
  2582. printf("\n");
  2583. break;
  2584. case KT_APPLE_NEW:
  2585. printf("\tkern: APPLE_NEW, ntables: %d.", fnt->nkern);
  2586. if (fnt->nkern) {
  2587. printf(" [");
  2588. for (i=0; i<fnt->nkern; i++) {
  2589. printf("%04X ", GetUInt16(fnt->kerntables[i], 4, 1));
  2590. }
  2591. printf("]");
  2592. }
  2593. printf("\n");
  2594. break;
  2595. case KT_NONE:
  2596. printf("\tkern: none.\n");
  2597. break;
  2598. default:
  2599. printf("\tkern: unrecoginzed.\n");
  2600. break;
  2601. }
  2602. printf("\n");
  2603. #endif
  2604. GetTTGlobalFontInfo(fnt, &info);
  2605. printf("\tfamily name: `%s`\n", info.family);
  2606. printf("\tsubfamily name: `%s`\n", info.subfamily);
  2607. printf("\tpostscript name: `%s`\n", info.psname);
  2608. printf("\tweight: %d\n", info.weight);
  2609. printf("\twidth: %d\n", info.width);
  2610. printf("\tpitch: %d\n", info.pitch);
  2611. printf("\titalic angle: %d\n", info.italicAngle);
  2612. printf("\tbouding box: [%d %d %d %d]\n", info.xMin, info.yMin, info.xMax, info.yMax);
  2613. printf("\tascender: %d\n", info.ascender);
  2614. printf("\tdescender: %d\n", info.descender);
  2615. printf("\tlinegap: %d\n", info.linegap);
  2616. printf("\tvascent: %d\n", info.vascent);
  2617. printf("\tvdescent: %d\n", info.vdescent);
  2618. printf("\ttypoAscender: %d\n", info.typoAscender);
  2619. printf("\ttypoDescender: %d\n", info.typoDescender);
  2620. printf("\ttypoLineGap: %d\n", info.typoLineGap);
  2621. printf("\twinAscent: %d\n", info.winAscent);
  2622. printf("\twinDescent: %d\n", info.winDescent);
  2623. printf("\tUnicode ranges:\n");
  2624. for (i = 0; i < 32; i++) {
  2625. if ((info.ur1 >> i) & 1) {
  2626. printf("\t\t\t%s\n", UnicodeRangeName(i));
  2627. }
  2628. }
  2629. for (i = 0; i < 32; i++) {
  2630. if ((info.ur2 >> i) & 1) {
  2631. printf("\t\t\t%s\n", UnicodeRangeName(i+32));
  2632. }
  2633. }
  2634. for (i = 0; i < 32; i++) {
  2635. if ((info.ur3 >> i) & 1) {
  2636. printf("\t\t\t%s\n", UnicodeRangeName(i+64));
  2637. }
  2638. }
  2639. for (i = 0; i < 32; i++) {
  2640. if ((info.ur4 >> i) & 1) {
  2641. printf("\t\t\t%s\n", UnicodeRangeName(i+96));
  2642. }
  2643. }
  2644. CloseTTFont(fnt);
  2645. return 0;
  2646. }
  2647. #endif
  2648. #ifdef TEST5
  2649. /* Kerning example */
  2650. int main(int ac, char **av)
  2651. {
  2652. TrueTypeFont *fnt;
  2653. sal_uInt16 g[224];
  2654. KernData d[223];
  2655. int r, i, k = 0;
  2656. g[k++] = 11;
  2657. g[k++] = 36;
  2658. g[k++] = 11;
  2659. g[k++] = 98;
  2660. g[k++] = 11;
  2661. g[k++] = 144;
  2662. g[k++] = 41;
  2663. g[k++] = 171;
  2664. g[k++] = 51;
  2665. g[k++] = 15;
  2666. if (ac < 2) return 0;
  2667. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2668. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2669. return 0;
  2670. }
  2671. KernGlyphs(fnt, g, k, 0, d);
  2672. for (i = 0; i < k-1; i++) {
  2673. printf("%3d %3d: [%3d %3d]\n", g[i], g[i+1], d[i].x, d[i].y);
  2674. }
  2675. CloseTTFont(fnt);
  2676. return 0;
  2677. }
  2678. #endif
  2679. #ifdef TEST6
  2680. /* This example extracts a single glyph from a font */
  2681. int main(int ac, char **av)
  2682. {
  2683. TrueTypeFont *fnt;
  2684. int r, i;
  2685. sal_uInt16 glyphs[256];
  2686. sal_uInt8 encoding[256];
  2687. for (i=0; i<256; i++) {
  2688. glyphs[i] = 512 + i;
  2689. encoding[i] = i;
  2690. }
  2691. #if 0
  2692. i=0;
  2693. glyphs[i++] = 2001;
  2694. glyphs[i++] = 2002;
  2695. glyphs[i++] = 2003;
  2696. glyphs[i++] = 2004;
  2697. glyphs[i++] = 2005;
  2698. glyphs[i++] = 2006;
  2699. glyphs[i++] = 2007;
  2700. glyphs[i++] = 2008;
  2701. glyphs[i++] = 2009;
  2702. glyphs[i++] = 2010;
  2703. glyphs[i++] = 2011;
  2704. glyphs[i++] = 2012;
  2705. glyphs[i++] = 2013;
  2706. glyphs[i++] = 2014;
  2707. glyphs[i++] = 2015;
  2708. glyphs[i++] = 2016;
  2709. glyphs[i++] = 2017;
  2710. glyphs[i++] = 2018;
  2711. glyphs[i++] = 2019;
  2712. glyphs[i++] = 2020;
  2713. r = 97;
  2714. i = 0;
  2715. encoding[i++] = r++;
  2716. encoding[i++] = r++;
  2717. encoding[i++] = r++;
  2718. encoding[i++] = r++;
  2719. encoding[i++] = r++;
  2720. encoding[i++] = r++;
  2721. encoding[i++] = r++;
  2722. encoding[i++] = r++;
  2723. encoding[i++] = r++;
  2724. encoding[i++] = r++;
  2725. encoding[i++] = r++;
  2726. encoding[i++] = r++;
  2727. encoding[i++] = r++;
  2728. encoding[i++] = r++;
  2729. encoding[i++] = r++;
  2730. encoding[i++] = r++;
  2731. encoding[i++] = r++;
  2732. encoding[i++] = r++;
  2733. encoding[i++] = r++;
  2734. encoding[i++] = r++;
  2735. #endif
  2736. if (ac < 2) return 0;
  2737. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2738. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2739. return 0;
  2740. }
  2741. /* Generate a subset */
  2742. CreateT3FromTTGlyphs(fnt, stdout, 0, glyphs, encoding, 256, 0);
  2743. fprintf(stderr, "UnitsPerEm: %d.\n", fnt->unitsPerEm);
  2744. /* Now call the dtor for the font */
  2745. CloseTTFont(fnt);
  2746. return 0;
  2747. }
  2748. #endif
  2749. #ifdef TEST7
  2750. /* NameRecord extraction example */
  2751. int main(int ac, char **av)
  2752. {
  2753. TrueTypeFont *fnt;
  2754. int r, i, j, n;
  2755. NameRecord *nr;
  2756. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2757. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2758. return 0;
  2759. }
  2760. if ((n = GetTTNameRecords(fnt, &nr)) == 0) {
  2761. fprintf(stderr, "No name records in the font.\n");
  2762. return 0;
  2763. }
  2764. printf("Number of name records: %d.\n", n);
  2765. for (i = 0; i < n; i++) {
  2766. printf("%d %d %04X %d [", nr[i].platformID, nr[i].encodingID, nr[i].languageID, nr[i].nameID);
  2767. for (j=0; j<nr[i].slen; j++) {
  2768. printf("%c", isprint(nr[i].sptr[j]) ? nr[i].sptr[j] : '.');
  2769. }
  2770. printf("]\n");
  2771. }
  2772. DisposeNameRecords(nr, n);
  2773. CloseTTFont(fnt);
  2774. return 0;
  2775. }
  2776. #endif
  2777. #ifdef TEST8
  2778. /* TrueType -> TrueType subsetting */
  2779. int main(int ac, char **av)
  2780. {
  2781. TrueTypeFont *fnt;
  2782. sal_uInt16 glyphArray[] = { 0, 98, 99, 22, 24, 25, 26, 27, 28, 29, 30, 31, 1270, 1289, 34};
  2783. sal_uInt8 encoding[] = {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46};
  2784. int r;
  2785. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2786. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2787. return 0;
  2788. }
  2789. CreateTTFromTTGlyphs(fnt, "subfont.ttf", glyphArray, encoding, 15, 0, 0, TTCF_AutoName | TTCF_IncludeOS2);
  2790. CloseTTFont(fnt);
  2791. return 0;
  2792. }
  2793. #endif
  2794. #ifdef TEST9
  2795. /* TrueType -> Type42 subsetting */
  2796. int main(int ac, char **av)
  2797. {
  2798. TrueTypeFont *fnt;
  2799. /*
  2800. sal_uInt16 glyphArray[] = { 0, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34};
  2801. sal_uInt8 encoding[] = {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46};
  2802. */
  2803. sal_uInt16 glyphArray[] = { 0, 6711, 6724, 11133, 11144, 14360, 26, 27, 28, 29, 30, 31, 1270, 1289, 34};
  2804. sal_uInt8 encoding[] = {32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46};
  2805. int r;
  2806. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2807. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2808. return 0;
  2809. }
  2810. CreateT42FromTTGlyphs(fnt, stdout, "testfont", glyphArray, encoding, 15);
  2811. CloseTTFont(fnt);
  2812. return 0;
  2813. }
  2814. #endif
  2815. #ifdef TEST10
  2816. /* Component glyph test */
  2817. int main(int ac, char **av)
  2818. {
  2819. TrueTypeFont *fnt;
  2820. int r, i;
  2821. list glyphlist = listNewEmpty();
  2822. if ((r = OpenTTFont(av[1], 0, &fnt)) != SF_OK) {
  2823. fprintf(stderr, "Error %d opening font file: `%s`.\n", r, av[1]);
  2824. return 0;
  2825. }
  2826. for (i = 0; i < fnt->nglyphs; i++) {
  2827. r = GetTTGlyphComponents(fnt, i, glyphlist);
  2828. if (r > 1) {
  2829. printf("%d -> ", i);
  2830. listToFirst(glyphlist);
  2831. do {
  2832. printf("%d ", (int) listCurrent(glyphlist));
  2833. } while (listNext(glyphlist));
  2834. printf("\n");
  2835. } else {
  2836. printf("%d: single glyph.\n", i);
  2837. }
  2838. listClear(glyphlist);
  2839. }
  2840. CloseTTFont(fnt);
  2841. listDispose(glyphlist);
  2842. return 0;
  2843. }
  2844. #endif