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