PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vcl/source/fontsubset/sft.cxx

https://bitbucket.org/markjenkins/libreoffice_ubuntu-debian-fixes
C++ | 2769 lines | 2159 code | 411 blank | 199 comment | 544 complexity | 68340c6e646cf8d3efec2a9319656034 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause-No-Nuclear-License-2014

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

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

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