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