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