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 const char * const modname = "SunTypeTools-TT";
53 const char * const modver = "1.0";
54 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  SAL_INFO("vcl.fonts", "Endless loop found in a compound glyph.");
524 
525  std::ostringstream oss;
526  oss << index << " -> [";
527  for( const auto& rGlyph : glyphlist )
528  {
529  oss << (int) rGlyph << " ";
530  }
531  oss << "]";
532  SAL_INFO("vcl.fonts", oss.str());
533 
534 #endif
535  }
536 
537  glyphlist.push_back( index );
538 
539  if ((np = GetTTGlyphOutline(ttf, index, &nextComponent, nullptr, &glyphlist)) == 0)
540  {
541  /* XXX that probably indicates a corrupted font */
542 #if OSL_DEBUG_LEVEL > 1
543  SAL_WARN("vcl.fonts", "An empty compound!");
544  /* assert(!"An empty compound"); */
545 #endif
546  }
547 
548  if( ! glyphlist.empty() )
549  glyphlist.pop_back();
550 
551  if ((flags & USE_MY_METRICS) && metrics)
552  GetMetrics(ttf, index, metrics);
553 
554  if (flags & ARG_1_AND_2_ARE_WORDS) {
555  e = GetInt16(ptr, 0);
556  f = GetInt16(ptr, 2);
557  /* printf("ARG_1_AND_2_ARE_WORDS: %d %d\n", e & 0xFFFF, f & 0xFFFF); */
558  ptr += 4;
559  } else {
560  if (flags & ARGS_ARE_XY_VALUES) { /* args are signed */
561  e = static_cast<sal_Int8>(*ptr++);
562  f = static_cast<sal_Int8>(*ptr++);
563  /* printf("ARGS_ARE_XY_VALUES: %d %d\n", e & 0xFF, f & 0xFF); */
564  } else { /* args are unsigned */
565  /* printf("!ARGS_ARE_XY_VALUES\n"); */
566  e = *ptr++;
567  f = *ptr++;
568  }
569 
570  }
571 
572  a = d = 0x10000;
573  b = c = 0;
574 
575  if (flags & WE_HAVE_A_SCALE) {
576  a = fromF2Dot14(GetInt16(ptr, 0));
577  d = a;
578  ptr += 2;
579  } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
580  a = fromF2Dot14(GetInt16(ptr, 0));
581  d = fromF2Dot14(GetInt16(ptr, 2));
582  ptr += 4;
583  } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
584  a = fromF2Dot14(GetInt16(ptr, 0));
585  b = fromF2Dot14(GetInt16(ptr, 2));
586  c = fromF2Dot14(GetInt16(ptr, 4));
587  d = fromF2Dot14(GetInt16(ptr, 6));
588  ptr += 8;
589  }
590 
591  abs1 = (a < 0) ? -a : a;
592  abs2 = (b < 0) ? -b : b;
593  m = std::max(abs1, abs2);
594  abs3 = abs1 - abs2;
595  if (abs3 < 0) abs3 = -abs3;
596  if (abs3 <= 33) m *= 2;
597 
598  abs1 = (c < 0) ? -c : c;
599  abs2 = (d < 0) ? -d : d;
600  n = std::max(abs1, abs2);
601  abs3 = abs1 - abs2;
602  if (abs3 < 0) abs3 = -abs3;
603  if (abs3 <= 33) n *= 2;
604 
605  SAL_WARN_IF(np && !m, "vcl.fonts", "Parsing error in " << OUString::createFromAscii(ttf->fname) <<
606  ": divide by zero");
607 
608  if (m != 0) {
609  for (i=0; i<np; i++) {
610  F16Dot16 t;
611  ControlPoint cp;
612  cp.flags = nextComponent[i].flags;
613  const sal_uInt16 x = nextComponent[i].x;
614  const sal_uInt16 y = nextComponent[i].y;
615  t = fixedMulDiv(a, x << 16, m) + fixedMulDiv(c, y << 16, m) + sal_Int32(sal_uInt16(e) << 16);
616  cp.x = static_cast<sal_Int16>(fixedMul(t, m) >> 16);
617  t = fixedMulDiv(b, x << 16, n) + fixedMulDiv(d, y << 16, n) + sal_Int32(sal_uInt16(f) << 16);
618  cp.y = static_cast<sal_Int16>(fixedMul(t, n) >> 16);
619 
620  myPoints.push_back( cp );
621  }
622  }
623 
624  free(nextComponent);
625 
626  } while (flags & MORE_COMPONENTS);
627 
628  // #i123417# some fonts like IFAOGrec have no outline points in some compound glyphs
629  // so this unlikely but possible scenario should be handled gracefully
630  if( myPoints.empty() )
631  return 0;
632 
633  np = myPoints.size();
634  if (np > 0)
635  {
636  pa = static_cast<ControlPoint*>(calloc(np, sizeof(ControlPoint)));
637  assert(pa != nullptr);
638 
639  memcpy(pa, myPoints.data(), np * sizeof(ControlPoint));
640 
641  *pointArray = pa;
642  }
643  return np;
644 }
645 
646 /* NOTE: GetTTGlyphOutline() returns -1 if the glyphID is incorrect,
647  * but Get{Simple|Compound}GlyphOutline returns 0 in such a case.
648  *
649  * NOTE: glyphlist is the stack of glyphs traversed while constructing
650  * a composite glyph. This is a safeguard against endless recursion
651  * in corrupted fonts.
652  */
653 static int GetTTGlyphOutline(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray, TTGlyphMetrics *metrics, std::vector< sal_uInt32 >* glyphlist)
654 {
655  const sal_uInt8 *table = getTable( ttf, O_glyf );
656  sal_Int16 numberOfContours;
657  int res;
658  *pointArray = nullptr;
659 
660  if (metrics) {
661  memset(metrics, 0, sizeof(TTGlyphMetrics)); /*- metrics is initialized to all zeroes */
662  }
663 
664  if (glyphID >= ttf->nglyphs) return -1;
665 
666  const sal_uInt8* ptr = table + ttf->goffsets[glyphID];
667  int length = ttf->goffsets[glyphID+1] - ttf->goffsets[glyphID];
668 
669  if (length == 0) { /*- empty glyphs still have hmtx and vmtx metrics values */
670  if (metrics) GetMetrics(ttf, glyphID, metrics);
671  return 0;
672  }
673 
674  numberOfContours = GetInt16(ptr, 0);
675 
676  if (numberOfContours >= 0)
677  {
678  res=GetSimpleTTOutline(ttf, glyphID, pointArray, metrics);
679  }
680  else
681  {
682  std::vector< sal_uInt32 > aPrivList;
683  aPrivList.push_back( glyphID );
684  res = GetCompoundTTOutline(ttf, glyphID, pointArray, metrics, glyphlist ? *glyphlist : aPrivList );
685  }
686 
687  return res;
688 }
689 
690 /*- returns the number of items in the path -*/
691 
692 static int BSplineToPSPath(ControlPoint const *srcA, int srcCount, PSPathElement **path)
693 {
694  std::vector< PSPathElement > aPathList;
695  int nPathCount = 0;
696  PSPathElement p( PS_NOOP );
697 
698  int x0 = 0, y0 = 0, x1 = 0, y1 = 0, x2, y2, curx, cury;
699  bool lastOff = false; /*- last point was off-contour */
700  int scflag = 1; /*- start contour flag */
701  bool ecflag = false; /*- end contour flag */
702  int cp = 0; /*- current point */
703  int StartContour = 0, EndContour = 1;
704 
705  *path = nullptr;
706 
707  /* if (srcCount > 0) for(;;) */
708  while (srcCount > 0) { /*- srcCount does not get changed inside the loop. */
709  if (scflag) {
710  int l = cp;
711  StartContour = cp;
712  while (!(srcA[l].flags & 0x8000)) l++;
713  EndContour = l;
714  if (StartContour == EndContour) {
715  if (cp + 1 < srcCount) {
716  cp++;
717  continue;
718  } else {
719  break;
720  }
721  }
722  p = PSPathElement(PS_MOVETO);
723  if (!(srcA[cp].flags & 1)) {
724  if (!(srcA[EndContour].flags & 1)) {
725  p.x1 = x0 = (srcA[cp].x + srcA[EndContour].x + 1) / 2;
726  p.y1 = y0 = (srcA[cp].y + srcA[EndContour].y + 1) / 2;
727  } else {
728  p.x1 = x0 = srcA[EndContour].x;
729  p.y1 = y0 = srcA[EndContour].y;
730  }
731  } else {
732  p.x1 = x0 = srcA[cp].x;
733  p.y1 = y0 = srcA[cp].y;
734  cp++;
735  }
736  aPathList.push_back( p );
737  lastOff = false;
738  scflag = 0;
739  }
740 
741  curx = srcA[cp].x;
742  cury = srcA[cp].y;
743 
744  if (srcA[cp].flags & 1)
745  {
746  if (lastOff)
747  {
748  p = PSPathElement(PS_CURVETO);
749  p.x1 = x0 + (2 * (x1 - x0) + 1) / 3;
750  p.y1 = y0 + (2 * (y1 - y0) + 1) / 3;
751  p.x2 = x1 + (curx - x1 + 1) / 3;
752  p.y2 = y1 + (cury - y1 + 1) / 3;
753  p.x3 = curx;
754  p.y3 = cury;
755  aPathList.push_back( p );
756  }
757  else
758  {
759  if (x0 != curx || y0 != cury)
760  { /* eliminate empty lines */
761  p = PSPathElement(PS_LINETO);
762  p.x1 = curx;
763  p.y1 = cury;
764  aPathList.push_back( p );
765  }
766  }
767  x0 = curx; y0 = cury; lastOff = false;
768  }
769  else
770  {
771  if (lastOff)
772  {
773  x2 = (x1 + curx + 1) / 2;
774  y2 = (y1 + cury + 1) / 2;
775  p = PSPathElement(PS_CURVETO);
776  p.x1 = x0 + (2 * (x1 - x0) + 1) / 3;
777  p.y1 = y0 + (2 * (y1 - y0) + 1) / 3;
778  p.x2 = x1 + (x2 - x1 + 1) / 3;
779  p.y2 = y1 + (y2 - y1 + 1) / 3;
780  p.x3 = x2;
781  p.y3 = y2;
782  aPathList.push_back( p );
783  x0 = x2; y0 = y2;
784  x1 = curx; y1 = cury;
785  } else {
786  x1 = curx; y1 = cury;
787  }
788  lastOff = true;
789  }
790 
791  if (ecflag) {
792  aPathList.emplace_back(PS_CLOSEPATH );
793  scflag = 1;
794  ecflag = false;
795  cp = EndContour + 1;
796  if (cp >= srcCount) break;
797  continue;
798  }
799 
800  if (cp == EndContour) {
801  cp = StartContour;
802  ecflag = true;
803  } else {
804  cp++;
805  }
806  }
807 
808  if( (nPathCount = static_cast<int>(aPathList.size())) > 0)
809  {
810  *path = static_cast<PSPathElement*>(calloc(nPathCount, sizeof(PSPathElement)));
811  assert(*path != nullptr);
812  memcpy( *path, aPathList.data(), nPathCount * sizeof(PSPathElement) );
813  }
814 
815  return nPathCount;
816 }
817 
818 /*- Extracts a string from the name table and allocates memory for it -*/
819 
820 static char *nameExtract( const sal_uInt8* name, int nTableSize, int n, int dbFlag, sal_Unicode** ucs2result )
821 {
822  char *res;
823  const sal_uInt8* ptr = name + GetUInt16(name, 4) + GetUInt16(name + 6, 12 * n + 10);
824  int len = GetUInt16(name+6, 12 * n + 8);
825 
826  // sanity check
827  const sal_uInt8* end_table = name+nTableSize;
828  const int available_space = ptr > end_table ? 0 : (end_table - ptr);
829  if( (len <= 0) || len > available_space)
830  {
831  if( ucs2result )
832  *ucs2result = nullptr;
833  return nullptr;
834  }
835 
836  if( ucs2result )
837  *ucs2result = nullptr;
838  if (dbFlag) {
839  res = static_cast<char*>(malloc(1 + len/2));
840  assert(res != nullptr);
841  for (int i = 0; i < len/2; i++)
842  res[i] = *(ptr + i * 2 + 1);
843  res[len/2] = 0;
844  if( ucs2result )
845  {
846  *ucs2result = static_cast<sal_Unicode*>(malloc( len+2 ));
847  for (int i = 0; i < len/2; i++ )
848  (*ucs2result)[i] = GetUInt16( ptr, 2*i );
849  (*ucs2result)[len/2] = 0;
850  }
851  } else {
852  res = static_cast<char*>(malloc(1 + len));
853  assert(res != nullptr);
854  memcpy(res, ptr, len);
855  res[len] = 0;
856  }
857 
858  return res;
859 }
860 
861 static int findname( const sal_uInt8 *name, sal_uInt16 n, sal_uInt16 platformID,
862  sal_uInt16 encodingID, sal_uInt16 languageID, sal_uInt16 nameID )
863 {
864  if (n == 0) return -1;
865 
866  int l = 0, r = n-1;
867  sal_uInt32 t1, t2;
868  sal_uInt32 m1, m2;
869 
870  m1 = (platformID << 16) | encodingID;
871  m2 = (languageID << 16) | nameID;
872 
873  do {
874  const int i = (l + r) >> 1;
875  t1 = GetUInt32(name + 6, i * 12 + 0);
876  t2 = GetUInt32(name + 6, i * 12 + 4);
877 
878  if (! ((m1 < t1) || ((m1 == t1) && (m2 < t2)))) l = i + 1;
879  if (! ((m1 > t1) || ((m1 == t1) && (m2 > t2)))) r = i - 1;
880  } while (l <= r);
881 
882  if (l - r == 2) {
883  return l - 1;
884  }
885 
886  return -1;
887 }
888 
889 /* XXX marlett.ttf uses (3, 0, 1033) instead of (3, 1, 1033) and does not have any Apple tables.
890  * Fix: if (3, 1, 1033) is not found - need to check for (3, 0, 1033)
891  *
892  * /d/fonts/ttzh_tw/Big5/Hanyi/ma6b5p uses (1, 0, 19) for English strings, instead of (1, 0, 0)
893  * and does not have (3, 1, 1033)
894  * Fix: if (1, 0, 0) and (3, 1, 1033) are not found need to look for (1, 0, *) - that will
895  * require a change in algorithm
896  *
897  * /d/fonts/fdltest/Korean/h2drrm has unsorted names and an unknown (to me) Mac LanguageID,
898  * but (1, 0, 1042) strings usable
899  * Fix: change algorithm, and use (1, 0, *) if both standard Mac and MS strings are not found
900  */
901 
902 static void GetNames(TrueTypeFont *t)
903 {
904  const sal_uInt8* table = getTable( t, O_name );
905  const sal_uInt32 nTableSize = getTableSize(t, O_name);
906 
907  if (nTableSize < 6)
908  {
909 #if OSL_DEBUG_LEVEL > 1
910  SAL_WARN("vcl.fonts", "O_name table too small.");
911 #endif
912  return;
913  }
914 
915  sal_uInt16 n = GetUInt16(table, 2);
916 
917  /* simple sanity check for name table entry count */
918  const size_t nMinRecordSize = 12;
919  const size_t nSpaceAvailable = nTableSize - 6;
920  const size_t nMaxRecords = nSpaceAvailable/nMinRecordSize;
921  if (n >= nMaxRecords)
922  n = 0;
923 
924  int i, r;
925  bool bPSNameOK = true;
926 
927  /* PostScript name: preferred Microsoft */
928  t->psname = nullptr;
929  if ((r = findname(table, n, 3, 1, 0x0409, 6)) != -1)
930  t->psname = nameExtract(table, nTableSize, r, 1, nullptr);
931  if ( ! t->psname && (r = findname(table, n, 1, 0, 0, 6)) != -1)
932  t->psname = nameExtract(table, nTableSize, r, 0, nullptr);
933  if ( ! t->psname && (r = findname(table, n, 3, 0, 0x0409, 6)) != -1)
934  {
935  // some symbol fonts like Marlett have a 3,0 name!
936  t->psname = nameExtract(table, nTableSize, r, 1, nullptr);
937  }
938  // for embedded font in Ghostscript PDFs
939  if ( ! t->psname && (r = findname(table, n, 2, 2, 0, 6)) != -1)
940  {
941  t->psname = nameExtract(table, nTableSize, r, 0, nullptr);
942  }
943  if ( ! t->psname )
944  {
945  if ( t->fname )
946  {
947  char* pReverse = t->fname + strlen(t->fname);
948  /* take only last token of filename */
949  while(pReverse != t->fname && *pReverse != '/') pReverse--;
950  if(*pReverse == '/') pReverse++;
951  t->psname = strdup(pReverse);
952  assert(t->psname != nullptr);
953  for (i=strlen(t->psname) - 1; i > 0; i--)
954  {
955  /*- Remove the suffix -*/
956  if (t->psname[i] == '.' ) {
957  t->psname[i] = 0;
958  break;
959  }
960  }
961  }
962  else
963  t->psname = strdup( "Unknown" );
964  }
965 
966  /* Font family and subfamily names: preferred Apple */
967  t->family = nullptr;
968  if ((r = findname(table, n, 0, 0, 0, 1)) != -1)
969  t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
970  if ( ! t->family && (r = findname(table, n, 3, 1, 0x0409, 1)) != -1)
971  t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
972  if ( ! t->family && (r = findname(table, n, 1, 0, 0, 1)) != -1)
973  t->family = nameExtract(table, nTableSize, r, 0, nullptr);
974  if ( ! t->family && (r = findname(table, n, 3, 1, 0x0411, 1)) != -1)
975  t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
976  if ( ! t->family && (r = findname(table, n, 3, 0, 0x0409, 1)) != -1)
977  t->family = nameExtract(table, nTableSize, r, 1, &t->ufamily);
978  if ( ! t->family )
979  {
980  t->family = strdup(t->psname);
981  assert(t->family != nullptr);
982  }
983 
984  t->subfamily = nullptr;
985  t->usubfamily = nullptr;
986  if ((r = findname(table, n, 1, 0, 0, 2)) != -1)
987  t->subfamily = nameExtract(table, nTableSize, r, 0, &t->usubfamily);
988  if ( ! t->subfamily && (r = findname(table, n, 3, 1, 0x0409, 2)) != -1)
989  t->subfamily = nameExtract(table, nTableSize, r, 1, &t->usubfamily);
990  if ( ! t->subfamily )
991  {
992  t->subfamily = strdup("");
993  }
994 
995  /* #i60349# sanity check psname
996  * psname practically has to be 7bit ASCII and should not contain spaces
997  * there is a class of broken fonts which do not fulfill that at all, so let's try
998  * if the family name is 7bit ASCII and take it instead if so
999  */
1000  /* check psname */
1001  for( i = 0; t->psname[i] != 0 && bPSNameOK; i++ )
1002  if( t->psname[ i ] < 33 || (t->psname[ i ] & 0x80) )
1003  bPSNameOK = false;
1004  if( !bPSNameOK )
1005  {
1006  /* check if family is a suitable replacement */
1007  if( t->ufamily && t->family )
1008  {
1009  bool bReplace = true;
1010 
1011  for( i = 0; t->ufamily[ i ] != 0 && bReplace; i++ )
1012  if( t->ufamily[ i ] < 33 || t->ufamily[ i ] > 127 )
1013  bReplace = false;
1014  if( bReplace )
1015  {
1016  free( t->psname );
1017  t->psname = strdup( t->family );
1018  }
1019  }
1020  }
1021 }
1022 
1023 namespace {
1024 
1025 enum cmapType {
1026  CMAP_NOT_USABLE = -1,
1027  CMAP_MS_Symbol = 10,
1028  CMAP_MS_Unicode = 11,
1029  CMAP_MS_ShiftJIS = 12,
1030  CMAP_MS_PRC = 13,
1031  CMAP_MS_Big5 = 14,
1032  CMAP_MS_Wansung = 15,
1033  CMAP_MS_Johab = 16
1034 };
1035 
1036 }
1037 
1038 #define MISSING_GLYPH_INDEX 0
1039 
1040 static sal_uInt32 getGlyph0(const sal_uInt8* cmap, sal_uInt32, sal_uInt32 c) {
1041  if (c <= 255) {
1042  return *(cmap + 6 + c);
1043  } else {
1044  return MISSING_GLYPH_INDEX;
1045  }
1046 }
1047 
1048 namespace {
1049 
1050 struct subHeader2 {
1051  sal_uInt16 firstCode;
1052  sal_uInt16 entryCount;
1053  sal_uInt16 idDelta;
1054  sal_uInt16 idRangeOffset;
1055 };
1056 
1057 }
1058 
1059 static sal_uInt32 getGlyph2(const sal_uInt8 *cmap, const sal_uInt32 nMaxCmapSize, sal_uInt32 c) {
1060  sal_uInt16 const *CMAP2 = reinterpret_cast<sal_uInt16 const *>(cmap);
1061  sal_uInt8 theHighByte;
1062 
1063  sal_uInt8 theLowByte;
1064  subHeader2 const * subHeader2s;
1065  sal_uInt16 const * subHeader2Keys;
1066  sal_uInt16 firstCode;
1067  int k = -1;
1068  sal_uInt32 ToReturn;
1069 
1070  theHighByte = static_cast<sal_uInt8>((c >> 8) & 0x00ff);
1071  theLowByte = static_cast<sal_uInt8>(c & 0x00ff);
1072  subHeader2Keys = CMAP2 + 3;
1073  subHeader2s = reinterpret_cast<subHeader2 const *>(subHeader2Keys + 256);
1074  if(reinterpret_cast<sal_uInt8 const *>(&subHeader2Keys[theHighByte]) - cmap < int(nMaxCmapSize - 2))
1075  {
1076  k = Int16FromMOTA(subHeader2Keys[theHighByte]) / 8;
1077  // check if the subheader record fits into available space
1078  if(reinterpret_cast<sal_uInt8 const *>(&subHeader2s[k]) - cmap >= int(nMaxCmapSize - sizeof(subHeader2)))
1079  k = -1;
1080  }
1081 
1082  if(k == 0) {
1083  firstCode = Int16FromMOTA(subHeader2s[0].firstCode);
1084  if(theLowByte >= firstCode && theLowByte < (firstCode + Int16FromMOTA(subHeader2s[k].entryCount))) {
1085  sal_uInt16 const * pGlyph = (&(subHeader2s[0].idRangeOffset))
1086  + (Int16FromMOTA(subHeader2s[0].idRangeOffset)/2) /* + offset */
1087  + theLowByte /* + to_look */
1088  - firstCode
1089  ;
1090  if (reinterpret_cast<sal_uInt8 const *>(pGlyph) - cmap < int(nMaxCmapSize) - 4)
1091  return *pGlyph;
1092  else
1093  return MISSING_GLYPH_INDEX;
1094  } else {
1095  return MISSING_GLYPH_INDEX;
1096  }
1097  } else if (k > 0) {
1098  firstCode = Int16FromMOTA(subHeader2s[k].firstCode);
1099  if(theLowByte >= firstCode && theLowByte < (firstCode + Int16FromMOTA(subHeader2s[k].entryCount))) {
1100  ToReturn = *((&(subHeader2s[k].idRangeOffset))
1101  + (Int16FromMOTA(subHeader2s[k].idRangeOffset)/2)
1102  + theLowByte - firstCode);
1103  if(ToReturn == 0) {
1104  return MISSING_GLYPH_INDEX;
1105  } else {
1106  ToReturn += Int16FromMOTA(subHeader2s[k].idDelta);
1107  return (ToReturn & 0xFFFF);
1108  }
1109  } else {
1110  return MISSING_GLYPH_INDEX;
1111  }
1112  } else {
1113  return MISSING_GLYPH_INDEX;
1114  }
1115 }
1116 
1117 static sal_uInt32 getGlyph6(const sal_uInt8 *cmap, sal_uInt32, sal_uInt32 c) {
1118  sal_uInt16 firstCode, lastCode, count;
1119  sal_uInt16 const *CMAP6 = reinterpret_cast<sal_uInt16 const *>(cmap);
1120 
1121  firstCode = Int16FromMOTA(*(CMAP6 + 3));
1122  count = Int16FromMOTA(*(CMAP6 + 4));
1123  lastCode = firstCode + count - 1;
1124  if (c < firstCode || c > lastCode) {
1125  return MISSING_GLYPH_INDEX;
1126  } else {
1127  return *((CMAP6 + 5)/*glyphIdArray*/ + (c - firstCode));
1128  }
1129 }
1130 
1131 static sal_uInt16 GEbinsearch(sal_uInt16 const *ar, sal_uInt16 length, sal_uInt16 toSearch) {
1132  signed int low, high, lastfound = 0xffff;
1133  sal_uInt16 res;
1134  if(length == sal_uInt16(0) || length == sal_uInt16(0xFFFF)) {
1135  return sal_uInt16(0xFFFF);
1136  }
1137  low = 0;
1138  high = length - 1;
1139  while(high >= low) {
1140  int mid = (high + low)/2;
1141  res = Int16FromMOTA(*(ar+mid));
1142  if(res >= toSearch) {
1143  lastfound = mid;
1144  high = --mid;
1145  } else {
1146  low = ++mid;
1147  }
1148  }
1149  return static_cast<sal_uInt16>(lastfound);
1150 }
1151 
1152 static sal_uInt32 getGlyph4(const sal_uInt8 *cmap, const sal_uInt32 nMaxCmapSize, sal_uInt32 c) {
1153  sal_uInt16 i;
1154  int ToReturn;
1155  sal_uInt16 segCount;
1156  sal_uInt16 const * startCode;
1157  sal_uInt16 const * endCode;
1158  sal_uInt16 const * idDelta;
1159  /* sal_uInt16 * glyphIdArray; */
1160  sal_uInt16 const * idRangeOffset;
1161  /*sal_uInt16 * glyphIndexArray;*/
1162  sal_uInt16 const *CMAP4 = reinterpret_cast<sal_uInt16 const *>(cmap);
1163  /* sal_uInt16 GEbinsearch(sal_uInt16 *ar, sal_uInt16 length, sal_uInt16 toSearch); */
1164 
1165  segCount = Int16FromMOTA(*(CMAP4 + 3))/2;
1166  endCode = CMAP4 + 7;
1167  i = GEbinsearch(endCode, segCount, static_cast<sal_uInt16>(c));
1168 
1169  if (i == sal_uInt16(0xFFFF)) {
1170  return MISSING_GLYPH_INDEX;
1171  }
1172  startCode = endCode + segCount + 1;
1173 
1174  if((reinterpret_cast<sal_uInt8 const *>(&startCode[i]) - cmap >= int(nMaxCmapSize - 2)) || Int16FromMOTA(startCode[i]) > c) {
1175  return MISSING_GLYPH_INDEX;
1176  }
1177  idDelta = startCode + segCount;
1178  idRangeOffset = idDelta + segCount;
1179  /*glyphIndexArray = idRangeOffset + segCount;*/
1180 
1181  if((reinterpret_cast<sal_uInt8 const *>(&idRangeOffset[i]) - cmap < int(nMaxCmapSize - 2)) && Int16FromMOTA(idRangeOffset[i]) != 0) {
1182  sal_uInt16 const * pGlyphOffset = &(idRangeOffset[i]) + (Int16FromMOTA(idRangeOffset[i])/2 + (c - Int16FromMOTA(startCode[i])));
1183  if(reinterpret_cast<sal_uInt8 const *>(pGlyphOffset) - cmap >= int(nMaxCmapSize - 2))
1184  return MISSING_GLYPH_INDEX;
1185  c = Int16FromMOTA(*pGlyphOffset);
1186  }
1187 
1188  ToReturn = (Int16FromMOTA(idDelta[i]) + c) & 0xFFFF;
1189  return ToReturn;
1190 }
1191 
1192 static sal_uInt32 getGlyph12(const sal_uInt8 *pCmap, sal_uInt32, sal_uInt32 cChar) {
1193  const sal_uInt32* pCMAP12 = reinterpret_cast<const sal_uInt32*>(pCmap);
1194  int nLength = Int32FromMOTA( pCMAP12[1] );
1195  int nGroups = Int32FromMOTA( pCMAP12[3] );
1196  int nLower = 0;
1197  int nUpper = nGroups;
1198 
1199  if( nUpper > (nLength-16)/12 )
1200  nUpper = (nLength-16)/12;
1201 
1202  /* binary search in "segmented coverage" subtable */
1203  while( nLower < nUpper ) {
1204  int nIndex = (nLower + nUpper) / 2;
1205  const sal_uInt32* pEntry = &pCMAP12[ 4 + 3*nIndex ];
1206  sal_uInt32 cStart = Int32FromMOTA( pEntry[0] );
1207  sal_uInt32 cLast = Int32FromMOTA( pEntry[1] );
1208  if( cChar < cStart )
1209  nUpper = nIndex;
1210  else if( cChar > cLast )
1211  nLower = nIndex + 1;
1212  else { /* found matching entry! */
1213  sal_uInt32 nGlyph = Int32FromMOTA( pEntry[2] );
1214  nGlyph += cChar - cStart;
1215  return nGlyph;
1216  }
1217  }
1218 
1219  return MISSING_GLYPH_INDEX;
1220 }
1221 
1222 static void FindCmap(TrueTypeFont *ttf)
1223 {
1224  const sal_uInt8* table = getTable(ttf, O_cmap);
1225  sal_uInt32 table_size = getTableSize(ttf, O_cmap);
1226  if (table_size < 4)
1227  {
1228  SAL_WARN("vcl.fonts", "Parsing error in " << OUString::createFromAscii(ttf->fname) <<
1229  "cmap table size too short");
1230  return;
1231  }
1232  sal_uInt16 ncmaps = GetUInt16(table, 2);
1233  sal_uInt32 AppleUni = 0; // Apple Unicode
1234  sal_uInt32 ThreeZero = 0; /* MS Symbol */
1235  sal_uInt32 ThreeOne = 0; /* MS UCS-2 */
1236  sal_uInt32 ThreeTwo = 0; /* MS ShiftJIS */
1237  sal_uInt32 ThreeThree = 0; /* MS PRC */
1238  sal_uInt32 ThreeFour = 0; /* MS Big5 */
1239  sal_uInt32 ThreeFive = 0; /* MS Wansung */
1240  sal_uInt32 ThreeSix = 0; /* MS Johab */
1241 
1242  const sal_uInt32 remaining_table_size = table_size-4;
1243  const sal_uInt32 nMinRecordSize = 8;
1244  const sal_uInt32 nMaxRecords = remaining_table_size / nMinRecordSize;
1245  if (ncmaps > nMaxRecords)
1246  {
1247  SAL_WARN("vcl.fonts", "Parsing error in " << OUString::createFromAscii(ttf->fname) <<
1248  ": " << nMaxRecords << " max possible entries, but " <<
1249  ncmaps << " claimed, truncating");
1250  ncmaps = nMaxRecords;
1251  }
1252 
1253  for (unsigned int i = 0; i < ncmaps; i++) {
1254  /* sanity check, cmap entry must lie within table */
1255  sal_uInt32 nLargestFixedOffsetPos = 8 + i * 8;
1256  sal_uInt32 nMinSize = nLargestFixedOffsetPos + sizeof(sal_uInt32);
1257  if (nMinSize > table_size)
1258  {
1259  SAL_WARN( "vcl.fonts", "Font " << OUString::createFromAscii(ttf->fname) << " claimed to have "
1260  << ncmaps << " cmaps, but only space for " << i);
1261  break;
1262  }
1263 
1264  sal_uInt16 pID = GetUInt16(table, 4 + i * 8);
1265  sal_uInt16 eID = GetUInt16(table, 6 + i * 8);
1266  sal_uInt32 offset = GetUInt32(table, nLargestFixedOffsetPos);
1267 
1268  /* sanity check, cmap must lie within file */
1269  if( (table - ttf->ptr) + offset > static_cast<sal_uInt32>(ttf->fsize) )
1270  continue;
1271 
1272  /* Unicode tables in Apple fonts */
1273  if (pID == 0) {
1274  AppleUni = offset;
1275  }
1276 
1277  if (pID == 3) {
1278  switch (eID) {
1279  case 0: ThreeZero = offset; break;
1280  case 10: // UCS-4
1281  case 1: ThreeOne = offset; break;
1282  case 2: ThreeTwo = offset; break;
1283  case 3: ThreeThree = offset; break;
1284  case 4: ThreeFour = offset; break;
1285  case 5: ThreeFive = offset; break;
1286  case 6: ThreeSix = offset; break;
1287  }
1288  }
1289  }
1290 
1291  // fall back to AppleUnicode if there are no ThreeOne/Threezero tables
1292  if( AppleUni && !ThreeZero && !ThreeOne)
1293  ThreeOne = AppleUni;
1294 
1295  if (ThreeOne) {
1296  ttf->cmapType = CMAP_MS_Unicode;
1297  ttf->cmap = table + ThreeOne;
1298  } else if (ThreeTwo) {
1299  ttf->cmapType = CMAP_MS_ShiftJIS;
1300  ttf->cmap = table + ThreeTwo;
1301  } else if (ThreeThree) {
1302  ttf->cmapType = CMAP_MS_PRC;
1303  ttf->cmap = table + ThreeThree;
1304  } else if (ThreeFour) {
1305  ttf->cmapType = CMAP_MS_Big5;
1306  ttf->cmap = table + ThreeFour;
1307  } else if (ThreeFive) {
1308  ttf->cmapType = CMAP_MS_Wansung;
1309  ttf->cmap = table + ThreeFive;
1310  } else if (ThreeSix) {
1311  ttf->cmapType = CMAP_MS_Johab;
1312  ttf->cmap = table + ThreeSix;
1313  } else if (ThreeZero) {
1314  ttf->cmapType = CMAP_MS_Symbol;
1315  ttf->cmap = table + ThreeZero;
1316  } else {
1317  ttf->cmapType = CMAP_NOT_USABLE;
1318  ttf->cmap = nullptr;
1319  }
1320 
1321  if (ttf->cmapType != CMAP_NOT_USABLE) {
1322  if( (ttf->cmap - ttf->ptr + 2U) > static_cast<sal_uInt32>(ttf->fsize) ) {
1323  ttf->cmapType = CMAP_NOT_USABLE;
1324  ttf->cmap = nullptr;
1325  }
1326  }
1327 
1328  if (ttf->cmapType != CMAP_NOT_USABLE) {
1329  switch (GetUInt16(ttf->cmap, 0)) {
1330  case 0: ttf->mapper = getGlyph0; break;
1331  case 2: ttf->mapper = getGlyph2; break;
1332  case 4: ttf->mapper = getGlyph4; break;
1333  case 6: ttf->mapper = getGlyph6; break;
1334  case 12: ttf->mapper= getGlyph12; break;
1335  default:
1336 #if OSL_DEBUG_LEVEL > 1
1337  /*- if the cmap table is really broken */
1338  SAL_WARN("vcl.fonts", ttf->fname << ": "
1339  << GetUInt16(ttf->cmap, 0)
1340  << " is not a recognized cmap format..");
1341 #endif
1342  ttf->cmapType = CMAP_NOT_USABLE;
1343  ttf->cmap = nullptr;
1344  ttf->mapper = nullptr;
1345  }
1346  }
1347 }
1348 
1349 /*- Public functions */
1350 
1351 int CountTTCFonts(const char* fname)
1352 {
1353  int nFonts = 0;
1354  sal_uInt8 buffer[12];
1355  FILE* fd = fopen(fname, "rb");
1356  if( fd ) {
1357  if (fread(buffer, 1, 12, fd) == 12) {
1358  if(GetUInt32(buffer, 0) == T_ttcf )
1359  nFonts = GetUInt32(buffer, 8);
1360  }
1361  fclose(fd);
1362  }
1363  return nFonts;
1364 }
1365 
1366 static void allocTrueTypeFont( TrueTypeFont** ttf )
1367 {
1368  *ttf = static_cast<TrueTypeFont*>(calloc(1,sizeof(TrueTypeFont)));
1369  if( *ttf != nullptr )
1370  {
1371  (*ttf)->fname = nullptr;
1372  (*ttf)->fsize = -1;
1373  (*ttf)->ptr = nullptr;
1374  (*ttf)->nglyphs = 0xFFFFFFFF;
1375  }
1376 }
1377 
1378 /* forward declaration for the two entry points to use*/
1379 static SFErrCodes doOpenTTFont( sal_uInt32 facenum, TrueTypeFont* t );
1380 
1381 #if !defined(_WIN32)
1382 SFErrCodes OpenTTFontFile( const char* fname, sal_uInt32 facenum, TrueTypeFont** ttf )
1383 {
1384  SFErrCodes ret;
1385  int fd = -1;
1386  struct stat st;
1387 
1388  if (!fname || !*fname) return SFErrCodes::BadFile;
1389 
1390  allocTrueTypeFont( ttf );
1391  if( ! *ttf )
1392  return SFErrCodes::Memory;
1393 
1394  (*ttf)->fname = strdup(fname);
1395  if( ! (*ttf)->fname )
1396  {
1397  ret = SFErrCodes::Memory;
1398  goto cleanup;
1399  }
1400 
1401  fd = open(fname, O_RDONLY);
1402 
1403  if (fd == -1) {
1404  ret = SFErrCodes::BadFile;
1405  goto cleanup;
1406  }
1407 
1408  if (fstat(fd, &st) == -1) {
1409  ret = SFErrCodes::FileIo;
1410  goto cleanup;
1411  }
1412 
1413  (*ttf)->fsize = st.st_size;
1414 
1415  /* On Mac OS, most likely will happen if a Mac user renames a font file
1416  * to be .ttf when it's really a Mac resource-based font.
1417  * Size will be 0, but fonts smaller than 4 bytes would be broken anyway.
1418  */
1419  if ((*ttf)->fsize == 0) {
1420  ret = SFErrCodes::BadFile;
1421  goto cleanup;
1422  }
1423 
1424  if (((*ttf)->ptr = static_cast<sal_uInt8 *>(mmap(nullptr, (*ttf)->fsize, PROT_READ, MAP_SHARED, fd, 0))) == MAP_FAILED) {
1425  ret = SFErrCodes::Memory;
1426  goto cleanup;
1427  }
1428  close(fd);
1429 
1430  return doOpenTTFont( facenum, *ttf );
1431 
1432 cleanup:
1433  if (fd != -1) close(fd);
1434  /*- t and t->fname have been allocated! */
1435  free((*ttf)->fname);
1436  free(*ttf);
1437  *ttf = nullptr;
1438  return ret;
1439 }
1440 #endif
1441 
1442 SFErrCodes OpenTTFontBuffer(const void* pBuffer, sal_uInt32 nLen, sal_uInt32 facenum, TrueTypeFont** ttf)
1443 {
1444  allocTrueTypeFont( ttf );
1445  if( *ttf == nullptr )
1446  return SFErrCodes::Memory;
1447 
1448  (*ttf)->fname = nullptr;
1449  (*ttf)->fsize = nLen;
1450  (*ttf)->ptr = const_cast<sal_uInt8 *>(static_cast<sal_uInt8 const *>(pBuffer));
1451 
1452  return doOpenTTFont( facenum, *ttf );
1453 }
1454 
1455 namespace {
1456 
1457 bool withinBounds(sal_uInt32 tdoffset, sal_uInt32 moreoffset, sal_uInt32 len, sal_uInt32 available)
1458 {
1459  sal_uInt32 result;
1460  if (o3tl::checked_add(tdoffset, moreoffset, result))
1461  return false;
1462  if (o3tl::checked_add(result, len, result))
1463  return false;
1464  return result <= available;
1465 }
1466 
1467 class TTFontCloser
1468 {
1469  TrueTypeFont* m_font;
1470 public:
1471  TTFontCloser(TrueTypeFont* t)
1472  : m_font(t)
1473  {
1474  }
1475  void clear() { m_font = nullptr; }
1476  ~TTFontCloser()
1477  {
1478  if (m_font)
1479  CloseTTFont(m_font);
1480  }
1481 };
1482 
1483 }
1484 
1485 static SFErrCodes doOpenTTFont( sal_uInt32 facenum, TrueTypeFont* t )
1486 {
1487  TTFontCloser aCloseGuard(t);
1488 
1489  if (t->fsize < 4) {
1490  return SFErrCodes::TtFormat;
1491  }
1492  int i;
1493  sal_uInt32 length, tag;
1494  sal_uInt32 tdoffset = 0; /* offset to TableDirectory in a TTC file. For TTF files is 0 */
1495 
1496  sal_uInt32 TTCTag = GetInt32(t->ptr, 0);
1497 
1498  if ((TTCTag == 0x00010000) || (TTCTag == T_true)) {
1499  tdoffset = 0;
1500  } else if (TTCTag == T_otto) { /* PS-OpenType font */
1501  tdoffset = 0;
1502  } else if (TTCTag == T_ttcf) { /* TrueType collection */
1503  if (!withinBounds(12, 4 * facenum, sizeof(sal_uInt32), t->fsize)) {
1504  return SFErrCodes::FontNo;
1505  }
1506  sal_uInt32 Version = GetUInt32(t->ptr, 4);
1507  if (Version != 0x00010000 && Version != 0x00020000) {
1508  return SFErrCodes::TtFormat;
1509  }
1510  if (facenum >= GetUInt32(t->ptr, 8)) {
1511  return SFErrCodes::FontNo;
1512  }
1513  tdoffset = GetUInt32(t->ptr, 12 + 4 * facenum);
1514  } else {
1515  return SFErrCodes::TtFormat;
1516  }
1517 
1518  if (withinBounds(tdoffset, 0, 4 + sizeof(sal_uInt16), t->fsize)) {
1519  t->ntables = GetUInt16(t->ptr + tdoffset, 4);
1520  }
1521 
1522  if (t->ntables >= 128 || t->ntables == 0) {
1523  return SFErrCodes::TtFormat;
1524  }
1525 
1526  /* parse the tables */
1527  for (i=0; i<static_cast<int>(t->ntables); i++) {
1528  int nIndex;
1529  const sal_uInt32 nStart = tdoffset + 12;
1530  const sal_uInt32 nOffset = 16 * i;
1531  if (withinBounds(nStart, nOffset, sizeof(sal_uInt32), t->fsize))
1532  tag = GetUInt32(t->ptr + nStart, nOffset);
1533  else
1534  tag = static_cast<sal_uInt32>(-1);
1535  switch( tag ) {
1536  case T_maxp: nIndex = O_maxp; break;
1537  case T_glyf: nIndex = O_glyf; break;
1538  case T_head: nIndex = O_head; break;
1539  case T_loca: nIndex = O_loca; break;
1540  case T_name: nIndex = O_name; break;
1541  case T_hhea: nIndex = O_hhea; break;
1542  case T_hmtx: nIndex = O_hmtx; break;
1543  case T_cmap: nIndex = O_cmap; break;
1544  case T_vhea: nIndex = O_vhea; break;
1545  case T_vmtx: nIndex = O_vmtx; break;
1546  case T_OS2 : nIndex = O_OS2; break;
1547  case T_post: nIndex = O_post; break;
1548  case T_cvt : nIndex = O_cvt; break;
1549  case T_prep: nIndex = O_prep; break;
1550  case T_fpgm: nIndex = O_fpgm; break;
1551  case T_gsub: nIndex = O_gsub; break;
1552  case T_CFF: nIndex = O_CFF; break;
1553  default: nIndex = -1; break;
1554  }
1555 
1556  if ((nIndex >= 0) && withinBounds(nStart, nOffset, 12 + sizeof(sal_uInt32), t->fsize)) {
1557  sal_uInt32 nTableOffset = GetUInt32(t->ptr + nStart, nOffset + 8);
1558  length = GetUInt32(t->ptr + nStart, nOffset + 12);
1559  t->tables[nIndex] = t->ptr + nTableOffset;
1560  t->tlens[nIndex] = length;
1561  }
1562  }
1563 
1564  /* Fixup offsets when only a TTC extract was provided */
1565  if( facenum == sal_uInt32(~0) ) {
1566  sal_uInt8* pHead = const_cast<sal_uInt8*>(t->tables[O_head]);
1567  if (!pHead) {
1568  return SFErrCodes::TtFormat;
1569  }
1570  /* limit Head candidate to TTC extract's limits */
1571  if( pHead > t->ptr + (t->fsize - 54) )
1572  pHead = t->ptr + (t->fsize - 54);
1573  /* TODO: find better method than searching head table's magic */
1574  sal_uInt8* p = nullptr;
1575  for( p = pHead + 12; p > t->ptr; --p ) {
1576  if( p[0]==0x5F && p[1]==0x0F && p[2]==0x3C && p[3]==0xF5 ) {
1577  int nDelta = (pHead + 12) - p;
1578  if( nDelta )
1579  for( int j = 0; j < NUM_TAGS; ++j )
1580  if( t->tables[j] )
1581  *reinterpret_cast<char const **>(&t->tables[j]) -= nDelta;
1582  break;
1583  }
1584  }
1585  if (p <= t->ptr) {
1586  return SFErrCodes::TtFormat;
1587  }
1588  }
1589 
1590  /* Check the table offsets after TTC correction */
1591  for (i=0; i<NUM_TAGS; i++) {
1592  /* sanity check: table must lay completely within the file
1593  * at this point one could check the checksum of all contained
1594  * tables, but this would be quite time intensive.
1595  * Try to fix tables, so we can cope with minor problems.
1596  */
1597 
1598  if( t->tables[i] < t->ptr )
1599  {
1600 #if OSL_DEBUG_LEVEL > 1
1601  SAL_WARN_IF(t->tables[i], "vcl.fonts", "font file " << t->fname
1602  << " has bad table offset "
1603  << (sal_uInt8*)t->tables[i]-t->ptr
1604  << "d (tagnum=" << i << ").");
1605 #endif
1606  t->tlens[i] = 0;
1607  t->tables[i] = nullptr;
1608  }
1609  else if( const_cast<sal_uInt8*>(t->tables[i]) + t->tlens[i] > t->ptr + t->fsize )
1610  {
1611  sal_PtrDiff nMaxLen = (t->ptr + t->fsize) - t->tables[i];
1612  if( nMaxLen < 0 )
1613  nMaxLen = 0;
1614  t->tlens[i] = nMaxLen;
1615 #if OSL_DEBUG_LEVEL > 1
1616  SAL_WARN("vcl.fonts", "font file " << t->fname
1617  << " has too big table (tagnum=" << i << ").");
1618 #endif
1619  }
1620  }
1621 
1622  /* At this point TrueTypeFont is constructed, now need to verify the font format
1623  and read the basic font properties */
1624 
1625  /* The following tables are absolutely required:
1626  * maxp, head, name, cmap
1627  */
1628 
1629  if( !(getTable(t, O_maxp) && getTable(t, O_head) && getTable(t, O_name) && getTable(t, O_cmap)) ) {
1630  return SFErrCodes::TtFormat;
1631  }
1632 
1633  const sal_uInt8* table = getTable(t, O_maxp);
1634  sal_uInt32 table_size = getTableSize(t, O_maxp);
1635  t->nglyphs = table_size >= 6 ? GetUInt16(table, 4) : 0;
1636 
1637  table = getTable(t, O_head);
1638  table_size = getTableSize(t, O_head);
1639  if (table_size < HEAD_Length) {
1640  return SFErrCodes::TtFormat;
1641  }
1643  int indexfmt = GetInt16(table, HEAD_indexToLocFormat_offset);
1644 
1645  if( ((indexfmt != 0) && (indexfmt != 1)) || (t->unitsPerEm <= 0) ) {
1646  return SFErrCodes::TtFormat;
1647  }
1648 
1649  if( getTable(t, O_glyf) && getTable(t, O_loca) ) /* TTF or TTF-OpenType */
1650  {
1651  int k = (getTableSize(t, O_loca) / (indexfmt ? 4 : 2)) - 1;
1652  if( k < static_cast<int>(t->nglyphs) ) /* Hack for broken Chinese fonts */
1653  t->nglyphs = k;
1654 
1655  table = getTable(t, O_loca);
1656  t->goffsets = static_cast<sal_uInt32 *>(calloc(1+t->nglyphs, sizeof(sal_uInt32)));
1657  assert(t->goffsets != nullptr);
1658 
1659  for( i = 0; i <= static_cast<int>(t->nglyphs); ++i )
1660  t->goffsets[i] = indexfmt ? GetUInt32(table, i << 2) : static_cast<sal_uInt32>(GetUInt16(table, i << 1)) << 1;
1661  } else if( getTable(t, O_CFF) ) { /* PS-OpenType */
1662  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 */
1663  if( k < static_cast<int>(t->nglyphs) )
1664  t->nglyphs = k;
1665  t->goffsets = static_cast<sal_uInt32 *>(calloc(1+t->nglyphs, sizeof(sal_uInt32)));
1666  /* TODO: implement to get subsetting */
1667  assert(t->goffsets != nullptr);
1668  } else {
1669  // Bitmap font, accept for now.
1670  t->goffsets = static_cast<sal_uInt32 *>(calloc(1+t->nglyphs, sizeof(sal_uInt32)));
1671  /* TODO: implement to get subsetting */
1672  assert(t->goffsets != nullptr);
1673  }
1674 
1675  table = getTable(t, O_hhea);
1676  table_size = getTableSize(t, O_hhea);
1677  t->numberOfHMetrics = (table && table_size >= 36) ? GetUInt16(table, 34) : 0;
1678 
1679  table = getTable(t, O_vhea);
1680  table_size = getTableSize(t, O_vhea);
1681  t->numOfLongVerMetrics = (table && table_size >= 36) ? GetUInt16(table, 34) : 0;
1682 
1683  GetNames(t);
1684  FindCmap(t);
1685 
1686  aCloseGuard.clear();
1687 
1688  return SFErrCodes::Ok;
1689 }
1690 
1692 {
1693 #if !defined(_WIN32)
1694  if( ttf->fname )
1695  munmap(ttf->ptr, ttf->fsize);
1696 #endif
1697  free(ttf->fname);
1698  free(ttf->goffsets);
1699  free(ttf->psname);
1700  free(ttf->family);
1701  if( ttf->ufamily )
1702  free( ttf->ufamily );
1703  free(ttf->subfamily);
1704  if( ttf->usubfamily )
1705  free( ttf->usubfamily );
1706 
1707  free(ttf);
1708 }
1709 
1710 int GetTTGlyphPoints(TrueTypeFont *ttf, sal_uInt32 glyphID, ControlPoint **pointArray)
1711 {
1712  return GetTTGlyphOutline(ttf, glyphID, pointArray, nullptr, nullptr);
1713 }
1714 
1715 int GetTTGlyphComponents(TrueTypeFont *ttf, sal_uInt32 glyphID, std::vector< sal_uInt32 >& glyphlist)
1716 {
1717  int n = 1;
1718 
1719  if( glyphID >= ttf->nglyphs )
1720  return 0;
1721 
1722  const sal_uInt8* glyf = getTable(ttf, O_glyf);
1723  const sal_uInt8* ptr = glyf + ttf->goffsets[glyphID];
1724  const sal_uInt8* nptr = glyf + ttf->goffsets[glyphID+1];
1725  if (nptr <= ptr)
1726  return 0;
1727 
1728  glyphlist.push_back( glyphID );
1729 
1730  if (GetInt16(ptr, 0) == -1) {
1731  sal_uInt16 flags, index;
1732  ptr += 10;
1733  do {
1734  flags = GetUInt16(ptr, 0);
1735  index = GetUInt16(ptr, 2);
1736 
1737  ptr += 4;
1738  n += GetTTGlyphComponents(ttf, index, glyphlist);
1739 
1740  if (flags & ARG_1_AND_2_ARE_WORDS) {
1741  ptr += 4;
1742  } else {
1743  ptr += 2;
1744  }
1745 
1746  if (flags & WE_HAVE_A_SCALE) {
1747  ptr += 2;
1748  } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
1749  ptr += 4;
1750  } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
1751  ptr += 8;
1752  }
1753  } while (flags & MORE_COMPONENTS);
1754  }
1755 
1756  return n;
1757 }
1758 
1759 SFErrCodes CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname,
1760  sal_uInt16 const *glyphArray, sal_uInt8 *encoding, int nGlyphs,
1761  int wmode)
1762 {
1763  ControlPoint *pa;
1764  PSPathElement *path;
1765  int i, j, n;
1766  const sal_uInt8* table = getTable(ttf, O_head);
1767  TTGlyphMetrics metrics;
1768  int UPEm = ttf->unitsPerEm;
1769 
1770  const char * const h01 = "%%!PS-AdobeFont-%d.%d-%d.%d\n";
1771  const char * const h02 = "%% Creator: %s %s %s\n";
1772  const char * const h09 = "%% Original font name: %s\n";
1773 
1774  const char * const h10 =
1775  "30 dict begin\n"
1776  "/PaintType 0 def\n"
1777  "/FontType 3 def\n"
1778  "/StrokeWidth 0 def\n";
1779 
1780  const char * const h11 = "/FontName (%s) cvn def\n";
1781 
1782  /*
1783  const char *h12 = "%/UniqueID %d def\n";
1784  */
1785  const char * const h13 = "/FontMatrix [.001 0 0 .001 0 0] def\n";
1786  const char * const h14 = "/FontBBox [%d %d %d %d] def\n";
1787 
1788  const char * const h15=
1789  "/Encoding 256 array def\n"
1790  " 0 1 255 {Encoding exch /.notdef put} for\n";
1791 
1792  const char * const h16 = " Encoding %d /glyph%d put\n";
1793  const char * const h17 = "/XUID [103 0 0 16#%08" SAL_PRIXUINT32 " %d 16#%08" SAL_PRIXUINT32 " 16#%08" SAL_PRIXUINT32 "] def\n";
1794 
1795  const char * const h30 = "/CharProcs %d dict def\n";
1796  const char * const h31 = " CharProcs begin\n";
1797  const char * const h32 = " /.notdef {} def\n";
1798  const char * const h33 = " /glyph%d {\n";
1799  const char * const h34 = " } bind def\n";
1800  const char * const h35 = " end\n";
1801 
1802  const char * const h40 =
1803  "/BuildGlyph {\n"
1804  " exch /CharProcs get exch\n"
1805  " 2 copy known not\n"
1806  " {pop /.notdef} if\n"
1807  " get exec\n"
1808  "} bind def\n"
1809  "/BuildChar {\n"
1810  " 1 index /Encoding get exch get\n"
1811  " 1 index /BuildGlyph get exec\n"
1812  "} bind def\n"
1813  "currentdict end\n";
1814 
1815  const char * const h41 = "(%s) cvn exch definefont pop\n";
1816 
1817  if ((nGlyphs <= 0) || (nGlyphs > 256)) return SFErrCodes::GlyphNum;
1818  if (!glyphArray) return SFErrCodes::BadArg;
1819  if (!fname) fname = ttf->psname;
1820 
1821  fprintf(outf, h01, GetInt16(table, 0), GetUInt16(table, 2), GetInt16(table, 4), GetUInt16(table, 6));
1822  fprintf(outf, h02, modname, modver, modextra);
1823  fprintf(outf, h09, ttf->psname);
1824 
1825  fprintf(outf, "%s", h10);
1826  fprintf(outf, h11, fname);
1827 /* fprintf(outf, h12, 4000000); */
1828 
1829  /* XUID generation:
1830  * 103 0 0 C1 C2 C3 C4
1831  * C1 - CRC-32 of the entire source TrueType font
1832  * C2 - number of glyphs in the subset
1833  * C3 - CRC-32 of the glyph array
1834  * C4 - CRC-32 of the encoding array
1835  *
1836  * All CRC-32 numbers are presented as hexadecimal numbers
1837  */
1838 
1839  fprintf(outf, h17, rtl_crc32(0, ttf->ptr, ttf->fsize), nGlyphs, rtl_crc32(0, glyphArray, nGlyphs * 2), rtl_crc32(0, encoding, nGlyphs));
1840  fprintf(outf, "%s", h13);
1841  fprintf(outf, h14, XUnits(UPEm, GetInt16(table, 36)), XUnits(UPEm, GetInt16(table, 38)), XUnits(UPEm, GetInt16(table, 40)), XUnits(UPEm, GetInt16(table, 42)));
1842  fprintf(outf, "%s", h15);
1843 
1844  for (i = 0; i < nGlyphs; i++) {
1845  fprintf(outf, h16, encoding[i], i);
1846  }
1847 
1848  fprintf(outf, h30, nGlyphs+1);
1849  fprintf(outf, "%s", h31);
1850  fprintf(outf, "%s", h32);
1851 
1852  for (i = 0; i < nGlyphs; i++) {
1853  fprintf(outf, h33, i);
1854  int r = GetTTGlyphOutline(ttf, glyphArray[i] < ttf->nglyphs ? glyphArray[i] : 0, &pa, &metrics, nullptr);
1855 
1856  if (r > 0) {
1857  n = BSplineToPSPath(pa, r, &path);
1858  } else {
1859  n = 0; /* glyph might have zero contours but valid metrics ??? */
1860  path = nullptr;
1861  if (r < 0) { /* glyph is not present in the font - pa array was not allocated, so no need to free it */
1862  continue;
1863  }
1864  }
1865  fprintf(outf, "\t%d %d %d %d %d %d setcachedevice\n",
1866  wmode == 0 ? XUnits(UPEm, metrics.aw) : 0,
1867  wmode == 0 ? 0 : -XUnits(UPEm, metrics.ah),
1868  XUnits(UPEm, metrics.xMin),
1869  XUnits(UPEm, metrics.yMin),
1870  XUnits(UPEm, metrics.xMax),
1871  XUnits(UPEm, metrics.yMax));
1872 
1873  for (j = 0; j < n; j++)
1874  {
1875  switch (path[j].type)
1876  {
1877  case PS_MOVETO:
1878  fprintf(outf, "\t%d %d moveto\n", XUnits(UPEm, path[j].x1), XUnits(UPEm, path[j].y1));
1879  break;
1880 
1881  case PS_LINETO:
1882  fprintf(outf, "\t%d %d lineto\n", XUnits(UPEm, path[j].x1), XUnits(UPEm, path[j].y1));
1883  break;
1884 
1885  case PS_CURVETO:
1886  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));
1887  break;
1888 
1889  case PS_CLOSEPATH:
1890  fprintf(outf, "\tclosepath\n");
1891  break;
1892  case PS_NOOP:
1893  break;
1894  }
1895  }
1896  if (n > 0) fprintf(outf, "\tfill\n"); /* if glyph is not a whitespace character */
1897 
1898  fprintf(outf, "%s", h34);
1899 
1900  free(pa);
1901  free(path);
1902  }
1903  fprintf(outf, "%s", h35);
1904 
1905  fprintf(outf, "%s", h40);
1906  fprintf(outf, h41, fname);
1907 
1908  return SFErrCodes::Ok;
1909 }
1910 
1912  const char *fname,
1913  sal_uInt16 const *glyphArray,
1914  sal_uInt8 const *encoding,
1915  int nGlyphs)
1916 {
1917  TrueTypeCreator *ttcr;
1918  TrueTypeTable *head=nullptr, *hhea=nullptr, *maxp=nullptr, *cvt=nullptr, *prep=nullptr, *glyf=nullptr, *fpgm=nullptr, *cmap=nullptr, *name=nullptr, *post = nullptr, *os2 = nullptr;
1919  int i;
1920  SFErrCodes res;
1921 
1923 
1926  NameRecord *names;
1927  int n = GetTTNameRecords(ttf, &names);
1928  name = TrueTypeTableNew_name(n, names);
1929  DisposeNameRecords(names, n);
1930 
1933 
1935  const sal_uInt8* p = getTable(ttf, O_hhea);
1936  if (p) {
1938  } else {
1939  hhea = TrueTypeTableNew_hhea(0, 0, 0, 0, 0);
1940  }
1941 
1944  p = getTable(ttf, O_head);
1945  assert(p != nullptr);
1953 
1956  glyf = TrueTypeTableNew_glyf();
1957  sal_uInt32* gID = static_cast<sal_uInt32*>(scalloc(nGlyphs, sizeof(sal_uInt32)));
1958 
1959  for (i = 0; i < nGlyphs; i++) {
1960  gID[i] = glyfAdd(glyf, GetTTRawGlyphData(ttf, glyphArray[i]), ttf);
1961  }
1962 
1964  cmap = TrueTypeTableNew_cmap();
1965 
1966  for (i=0; i < nGlyphs; i++) {
1967  cmapAdd(cmap, 0x010000, encoding[i], gID[i]);
1968  }
1969 
1971  if ((p = getTable(ttf, O_cvt)) != nullptr) {
1972  cvt = TrueTypeTableNew(T_cvt, getTableSize(ttf, O_cvt), p);
1973  }
1974 
1976  if ((p = getTable(ttf, O_prep)) != nullptr) {
1977  prep = TrueTypeTableNew(T_prep, getTableSize(ttf, O_prep), p);
1978  }
1979 
1981  if ((p = getTable(ttf, O_fpgm)) != nullptr) {
1982  fpgm = TrueTypeTableNew(T_fpgm, getTableSize(ttf, O_fpgm), p);
1983  }
1984 
1986  if ((p = getTable(ttf, O_post)) != nullptr) {
1987  post = TrueTypeTableNew_post(0x00030000,
1992  } else {
1993  post = TrueTypeTableNew_post(0x00030000, 0, 0, 0, 0);
1994  }
1995 
1996  AddTable(ttcr, name); AddTable(ttcr, maxp); AddTable(ttcr, hhea);
1997  AddTable(ttcr, head); AddTable(ttcr, glyf); AddTable(ttcr, cmap);
1998  AddTable(ttcr, cvt ); AddTable(ttcr, prep); AddTable(ttcr, fpgm);
1999  AddTable(ttcr, post); AddTable(ttcr, os2);
2000 
2001  res = StreamToFile(ttcr, fname);
2002 #if OSL_DEBUG_LEVEL > 1
2003  SAL_WARN_IF(res != SFErrCodes::Ok, "vcl.fonts", "StreamToFile: error code: "
2004  << (int) res << ".");
2005 #endif
2006 
2007  TrueTypeCreatorDispose(ttcr);
2008  free(gID);
2009 
2010  return res;
2011 }
2012 
2013 static GlyphOffsets *GlyphOffsetsNew(sal_uInt8 *sfntP, sal_uInt32 sfntLen)
2014 {
2015  GlyphOffsets* res = static_cast<GlyphOffsets*>(smalloc(sizeof(GlyphOffsets)));
2016  sal_uInt8 *loca = nullptr;
2017  sal_uInt16 numTables = GetUInt16(sfntP, 4);
2018  sal_uInt32 locaLen = 0;
2019  sal_Int16 indexToLocFormat = 0;
2020 
2021  sal_uInt32 nMaxPossibleTables = sfntLen / (3*sizeof(sal_uInt32)); /*the three GetUInt32 calls*/
2022  if (numTables > nMaxPossibleTables)
2023  {
2024  SAL_WARN( "vcl.fonts", "GlyphOffsetsNew claimed to have "
2025  << numTables << " tables, but that's impossibly large");
2026  numTables = nMaxPossibleTables;
2027  }
2028 
2029  for (sal_uInt16 i = 0; i < numTables; i++) {
2030  sal_uInt32 nLargestFixedOffsetPos = 12 + 16 * i + 12;
2031  sal_uInt32 nMinSize = nLargestFixedOffsetPos + sizeof(sal_uInt32);
2032  if (nMinSize > sfntLen)
2033  {
2034  SAL_WARN( "vcl.fonts", "GlyphOffsetsNew claimed to have "
2035  << numTables << " tables, but only space for " << i);
2036  break;
2037  }
2038 
2039  sal_uInt32 tag = GetUInt32(sfntP, 12 + 16 * i);
2040  sal_uInt32 off = GetUInt32(sfntP, 12 + 16 * i + 8);
2041  sal_uInt32 len = GetUInt32(sfntP, nLargestFixedOffsetPos);
2042 
2043  if (tag == T_loca) {
2044  loca = sfntP + off;
2045  locaLen = len;
2046  } else if (tag == T_head) {
2047  indexToLocFormat = GetInt16(sfntP + off, 50);
2048  }
2049  }
2050 
2051  res->nGlyphs = locaLen / ((indexToLocFormat == 1) ? 4 : 2);
2052  assert(res->nGlyphs != 0);
2053  res->offs = static_cast<sal_uInt32*>(scalloc(res->nGlyphs, sizeof(sal_uInt32)));
2054 
2055  for (sal_uInt32 i = 0; i < res->nGlyphs; i++) {
2056  if (indexToLocFormat == 1) {
2057  res->offs[i] = GetUInt32(loca, i * 4);
2058  } else {
2059  res->offs[i] = GetUInt16(loca, i * 2) << 1;
2060  }
2061  }
2062  return res;
2063 }
2064 
2065 static void GlyphOffsetsDispose(GlyphOffsets *_this)
2066 {
2067  if (_this) {
2068  free(_this->offs);
2069  free(_this);
2070  }
2071 }
2072 
2073 static void DumpSfnts(FILE *outf, sal_uInt8 *sfntP, sal_uInt32 sfntLen)
2074 {
2075  if (sfntLen < 12)
2076  {
2077  SAL_WARN( "vcl.fonts", "DumpSfnts sfntLen is too short: "
2078  << sfntLen << " legal min is: " << 12);
2079  return;
2080  }
2081 
2082  const sal_uInt32 nSpaceForTables = sfntLen - 12;
2083  const sal_uInt32 nTableSize = 16;
2084  const sal_uInt32 nMaxPossibleTables = nSpaceForTables/nTableSize;
2085 
2086  HexFmt *h = HexFmtNew(outf);
2087  sal_uInt16 i, numTables = GetUInt16(sfntP, 4);
2088  GlyphOffsets *go = GlyphOffsetsNew(sfntP, sfntLen);
2089  sal_uInt8 const pad[] = {0,0,0,0}; /* zeroes */
2090 
2091  if (numTables > nMaxPossibleTables)
2092  {
2093  SAL_WARN( "vcl.fonts", "DumpSfnts claimed to have "
2094  << numTables << " tables, but only space for " << nMaxPossibleTables);
2095  numTables = nMaxPossibleTables;
2096  }
2097 
2098  assert(numTables <= 9); /* Type42 has 9 required tables */
2099 
2100  sal_uInt32* offs = static_cast<sal_uInt32*>(scalloc(numTables, sizeof(sal_uInt32)));
2101 
2102  fputs("/sfnts [", outf);
2103  HexFmtOpenString(h);
2104  HexFmtBlockWrite(h, sfntP, 12); /* stream out the Offset Table */
2105  HexFmtBlockWrite(h, sfntP+12, 16 * numTables); /* stream out the Table Directory */
2106 
2107  for (i=0; i<numTables; i++)
2108  {
2109  sal_uInt32 nLargestFixedOffsetPos = 12 + 16 * i + 12;
2110  sal_uInt32 nMinSize = nLargestFixedOffsetPos + sizeof(sal_uInt32);
2111  if (nMinSize > sfntLen)
2112  {
2113  SAL_WARN( "vcl.fonts", "DumpSfnts claimed to have "
2114  << numTables << " tables, but only space for " << i);
2115  break;
2116  }
2117 
2118  sal_uInt32 tag = GetUInt32(sfntP, 12 + 16 * i);
2119  sal_uInt32 off = GetUInt32(sfntP, 12 + 16 * i + 8);
2120  if (off > sfntLen)
2121  {
2122  SAL_WARN( "vcl.fonts", "DumpSfnts claims offset of "
2123  << off << " but max possible is " << sfntLen);
2124  break;
2125  }
2126  sal_uInt8 *pRecordStart = sfntP + off;
2127  sal_uInt32 len = GetUInt32(sfntP, nLargestFixedOffsetPos);
2128  sal_uInt32 nMaxLenPossible = sfntLen - off;
2129  if (len > nMaxLenPossible)
2130  {
2131  SAL_WARN( "vcl.fonts", "DumpSfnts claims len of "
2132  << len << " but only space for " << nMaxLenPossible);
2133  break;
2134  }
2135 
2136  if (tag != T_glyf)
2137  {
2138  HexFmtBlockWrite(h, pRecordStart, len);
2139  }
2140  else
2141  {
2142  sal_uInt8 *glyf = pRecordStart;
2143  for (sal_uInt32 j = 0; j < go->nGlyphs - 1; j++)
2144  {
2145  sal_uInt32 o = go->offs[j];
2146  sal_uInt32 l = go->offs[j + 1] - o;
2147  HexFmtBlockWrite(h, glyf + o, l);
2148  }
2149  }
2150  HexFmtBlockWrite(h, pad, (4 - (len & 3)) & 3);
2151  }
2152  HexFmtCloseString(h);
2153  fputs("] def\n", outf);
2154  GlyphOffsetsDispose(go);
2155  HexFmtDispose(h);
2156  free(offs);
2157 }
2158 
2160  FILE *outf,
2161  const char *psname,
2162  sal_uInt16 const *glyphArray,
2163  sal_uInt8 *encoding,
2164  int nGlyphs)
2165 {
2166  TrueTypeCreator *ttcr;
2167  TrueTypeTable *head=nullptr, *hhea=nullptr, *maxp=nullptr, *cvt=nullptr, *prep=nullptr, *glyf=nullptr, *fpgm=nullptr;
2168  int i;
2169  SFErrCodes res;
2170 
2171  sal_uInt16 ver;
2172  sal_Int32 rev;
2173 
2174  sal_uInt8 *sfntP;
2175  sal_uInt32 sfntLen;
2176  int UPEm = ttf->unitsPerEm;
2177 
2178  if (nGlyphs >= 256) return SFErrCodes::GlyphNum;
2179 
2180  assert(psname != nullptr);
2181 
2183 
2184  /* head */
2185  const sal_uInt8* p = getTable(ttf, O_head);
2186  const sal_uInt8* headP = p;
2187  assert(p != nullptr);
2191 
2193  p = getTable(ttf, O_hhea);
2194  if (p) {
2196  } else {
2197  hhea = TrueTypeTableNew_hhea(0, 0, 0, 0, 0);
2198  }
2199 
2202 
2204  if ((p = getTable(ttf, O_cvt)) != nullptr) {
2205  cvt = TrueTypeTableNew(T_cvt, getTableSize(ttf, O_cvt), p);
2206  }
2207 
2209  if ((p = getTable(ttf, O_prep)) != nullptr) {
2210  prep = TrueTypeTableNew(T_prep, getTableSize(ttf, O_prep), p);
2211  }
2212 
2214  if ((p = getTable(ttf, O_fpgm)) != nullptr) {
2215  fpgm = TrueTypeTableNew(T_fpgm, getTableSize(ttf, O_fpgm), p);
2216  }
2217 
2219  glyf = TrueTypeTableNew_glyf();
2220  sal_uInt16* gID = static_cast<sal_uInt16*>(scalloc(nGlyphs, sizeof(sal_uInt32)));
2221 
2222  for (i = 0; i < nGlyphs; i++) {
2223  gID[i] = static_cast<sal_uInt16>(glyfAdd(glyf, GetTTRawGlyphData(ttf, glyphArray[i]), ttf));
2224  }
2225 
2226  AddTable(ttcr, head); AddTable(ttcr, hhea); AddTable(ttcr, maxp); AddTable(ttcr, cvt);
2227  AddTable(ttcr, prep); AddTable(ttcr, glyf); AddTable(ttcr, fpgm);
2228 
2229  if ((res = StreamToMemory(ttcr, &sfntP, &sfntLen)) != SFErrCodes::Ok) {
2230  TrueTypeCreatorDispose(ttcr);
2231  free(gID);
2232  return res;
2233  }
2234 
2235  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));
2236  fprintf(outf, "%%%%Creator: %s %s %s\n", modname, modver, modextra);
2237  fprintf(outf, "%%- Font subset generated from a source font file: '%s'\n", ttf->fname);
2238  fprintf(outf, "%%- Original font name: %s\n", ttf->psname);
2239  fprintf(outf, "%%- Original font family: %s\n", ttf->family);
2240  fprintf(outf, "%%- Original font sub-family: %s\n", ttf->subfamily);
2241  fprintf(outf, "11 dict begin\n");
2242  fprintf(outf, "/FontName (%s) cvn def\n", psname);
2243  fprintf(outf, "/PaintType 0 def\n");
2244  fprintf(outf, "/FontMatrix [1 0 0 1 0 0] def\n");
2245  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)));
2246  fprintf(outf, "/FontType 42 def\n");
2247  fprintf(outf, "/Encoding 256 array def\n");
2248  fprintf(outf, " 0 1 255 {Encoding exch /.notdef put} for\n");
2249 
2250  for (i = 1; i<nGlyphs; i++) {
2251  fprintf(outf, "Encoding %d /glyph%u put\n", encoding[i], gID[i]);
2252  }
2253  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)));
2254 
2255  DumpSfnts(outf, sfntP, sfntLen);
2256 
2257  /* dump charstrings */
2258  fprintf(outf, "/CharStrings %d dict dup begin\n", nGlyphs);
2259  fprintf(outf, "/.notdef 0 def\n");
2260  for (i = 1; i < static_cast<int>(glyfCount(glyf)); i++) {
2261  fprintf(outf,"/glyph%d %d def\n", i, i);
2262  }
2263  fprintf(outf, "end readonly def\n");
2264 
2265  fprintf(outf, "FontName currentdict end definefont pop\n");
2266  TrueTypeCreatorDispose(ttcr);
2267  free(gID);
2268  free(sfntP);
2269  return SFErrCodes::Ok;
2270 }
2271 
2272 #if defined(_WIN32) || defined(MACOSX) || defined(IOS)
2273 sal_uInt16 MapChar(TrueTypeFont const *ttf, sal_uInt16 ch)
2274 {
2275  switch (ttf->cmapType) {
2276  case CMAP_MS_Symbol:
2277  {
2278  const sal_uInt32 nMaxCmapSize = ttf->ptr + ttf->fsize - ttf->cmap;
2279  if( ttf->mapper == getGlyph0 && ( ch & 0xf000 ) == 0xf000 )
2280  ch &= 0x00ff;
2281  return static_cast<sal_uInt16>(ttf->mapper(ttf->cmap, nMaxCmapSize, ch ));
2282  }
2283 
2284  case CMAP_MS_Unicode: break;
2285  case CMAP_MS_ShiftJIS: ch = TranslateChar12(ch); break;
2286  case CMAP_MS_PRC: ch = TranslateChar13(ch); break;
2287  case CMAP_MS_Big5: ch = TranslateChar14(ch); break;
2288  case CMAP_MS_Wansung: ch = TranslateChar15(ch); break;
2289  case CMAP_MS_Johab: ch = TranslateChar16(ch); break;
2290  default: return 0;
2291  }
2292  const sal_uInt32 nMaxCmapSize = ttf->ptr + ttf->fsize - ttf->cmap;
2293  ch = static_cast<sal_uInt16>(ttf->mapper(ttf->cmap, nMaxCmapSize, ch));
2294  return ch;
2295 }
2296 #endif
2297 
2298 
2299 int GetTTGlyphCount( TrueTypeFont const * ttf )
2300 {
2301  return ttf->nglyphs;
2302 }
2303 
2304 bool GetSfntTable( TrueTypeFont const * ttf, int nSubtableIndex,
2305  const sal_uInt8** ppRawBytes, int* pRawLength )
2306 {
2307  if( (nSubtableIndex < 0) || (nSubtableIndex >= NUM_TAGS) )
2308  return false;
2309  *pRawLength = ttf->tlens[ nSubtableIndex ];
2310  *ppRawBytes = ttf->tables[ nSubtableIndex ];
2311  bool bOk = (*pRawLength > 0) && (*ppRawBytes != nullptr);
2312  return bOk;
2313 }
2314 
2315 std::unique_ptr<sal_uInt16[]> GetTTSimpleGlyphMetrics(TrueTypeFont const *ttf, const sal_uInt16 *glyphArray, int nGlyphs, bool vertical)
2316 {
2317  const sal_uInt8* pTable;
2318  sal_uInt32 n;
2319  int nTableSize;
2320 
2321  if (!vertical) {
2322  n = ttf->numberOfHMetrics;
2323  pTable = getTable( ttf, O_hmtx );
2324  nTableSize = getTableSize( ttf, O_hmtx );
2325  } else {
2326  n = ttf->numOfLongVerMetrics;
2327  pTable = getTable( ttf, O_vmtx );
2328  nTableSize = getTableSize( ttf, O_vmtx );
2329  }
2330 
2331  if (!nGlyphs || !glyphArray) return nullptr; /* invalid parameters */
2332  if (!n || !pTable) return nullptr; /* the font does not contain the requested metrics */
2333 
2334  std::unique_ptr<sal_uInt16[]> res(new sal_uInt16[nGlyphs]);
2335 
2336  const int UPEm = ttf->unitsPerEm;
2337  for( int i = 0; i < nGlyphs; ++i) {
2338  int nAdvOffset;
2339  sal_uInt16 glyphID = glyphArray[i];
2340 
2341  if (glyphID < n) {
2342  nAdvOffset = 4 * glyphID;
2343  } else {
2344  nAdvOffset = 4 * (n - 1);
2345  }
2346 
2347  if( nAdvOffset >= nTableSize)
2348  res[i] = 0; /* better than a crash for buggy fonts */
2349  else
2350  res[i] = static_cast<sal_uInt16>(
2351  XUnits( UPEm, GetUInt16( pTable, nAdvOffset) ) );
2352  }
2353 
2354  return res;
2355 }
2356 
2357 // TODO, clean up table parsing and re-use it elsewhere in this file.
2358 void GetTTFontMetrics(const uint8_t *pHhea, size_t nHhea,
2359  const uint8_t *pOs2, size_t nOs2,
2360  TTGlobalFontInfo *info)
2361 {
2362  /* There are 3 different versions of OS/2 table: original (68 bytes long),
2363  * Microsoft old (78 bytes long) and Microsoft new (86 bytes long,)
2364  * Apple's documentation recommends looking at the table length.
2365  */
2366  if (nOs2 >= OS2_V0_length)
2367  {
2372  info->winAscent = GetUInt16(pOs2, OS2_winAscent_offset);
2374  }
2375 
2376  if (nHhea >= HHEA_lineGap_offset + 2) {
2377  info->ascender = GetInt16(pHhea, HHEA_ascender_offset);
2378  info->descender = GetInt16(pHhea, HHEA_descender_offset);
2379  info->linegap = GetInt16(pHhea, HHEA_lineGap_offset);
2380  }
2381 }
2382 
2384 {
2385  int UPEm = ttf->unitsPerEm;
2386 
2387  memset(info, 0, sizeof(TTGlobalFontInfo));
2388 
2389  info->family = ttf->family;
2390  info->ufamily = ttf->ufamily;
2391  info->subfamily = ttf->subfamily;
2392  info->usubfamily = ttf->usubfamily;
2393  info->psname = ttf->psname;
2394  info->symbolEncoded = (ttf->cmapType == CMAP_MS_Symbol);
2395 
2396  const sal_uInt8* table = getTable(ttf, O_OS2);
2397  sal_uInt32 table_size = getTableSize(ttf, O_OS2);
2398  if (table && table_size >= 42) {
2399  info->weight = GetUInt16(table, OS2_usWeightClass_offset);
2400  info->width = GetUInt16(table, OS2_usWidthClass_offset);
2401 
2402  if (table_size >= OS2_V0_length) {
2403  info->typoAscender = XUnits(UPEm,GetInt16(table, OS2_typoAscender_offset));
2404  info->typoDescender = XUnits(UPEm, GetInt16(table, OS2_typoDescender_offset));
2405  info->typoLineGap = XUnits(UPEm, GetInt16(table, OS2_typoLineGap_offset));
2406  info->winAscent = XUnits(UPEm, GetUInt16(table, OS2_winAscent_offset));
2407  info->winDescent = XUnits(UPEm, GetUInt16(table, OS2_winDescent_offset));
2408  /* sanity check; some fonts treat winDescent as signed
2409  * violating the standard */
2410  if( info->winDescent > 5*UPEm )
2411  info->winDescent = XUnits(UPEm, GetInt16(table, OS2_winDescent_offset));
2412  }
2413  memcpy(info->panose, table + OS2_panose_offset, OS2_panoseNbBytes_offset);
2414  info->typeFlags = GetUInt16( table, OS2_fsType_offset );
2415  }
2416 
2417  table = getTable(ttf, O_post);
2418  if (table && getTableSize(ttf, O_post) >= 12+sizeof(sal_uInt32)) {
2419  info->pitch = GetUInt32(table, POST_isFixedPitch_offset);
2421  }
2422 
2423  table = getTable(ttf, O_head); /* 'head' tables is always there */
2424  table_size = getTableSize(ttf, O_head);
2425  if (table_size >= 46) {
2426  info->xMin = XUnits(UPEm, GetInt16(table, HEAD_xMin_offset));
2427  info->yMin = XUnits(UPEm, GetInt16(table, HEAD_yMin_offset));
2428  info->xMax = XUnits(UPEm, GetInt16(table, HEAD_xMax_offset));
2429  info->yMax = XUnits(UPEm, GetInt16(table, HEAD_yMax_offset));
2430  info->macStyle = GetUInt16(table, HEAD_macStyle_offset);
2431  }
2432 
2433  table = getTable(ttf, O_hhea);
2434  table_size = getTableSize(ttf, O_hhea);
2435  if (table && table_size >= 10) {
2436  info->ascender = XUnits(UPEm, GetInt16(table, HHEA_ascender_offset));
2437  info->descender = XUnits(UPEm, GetInt16(table, HHEA_descender_offset));
2438  info->linegap = XUnits(UPEm, GetInt16(table, HHEA_lineGap_offset));
2439  }
2440 }
2441 
2442 GlyphData *GetTTRawGlyphData(TrueTypeFont *ttf, sal_uInt32 glyphID)
2443 {
2444  const sal_uInt8* glyf = getTable(ttf, O_glyf);
2445  const sal_uInt8* hmtx = getTable(ttf, O_hmtx);
2446  int n;
2447 
2448  if( glyphID >= ttf->nglyphs )
2449  return nullptr;
2450 
2451  /* #127161# check the glyph offsets */
2452  sal_uInt32 length = getTableSize( ttf, O_glyf );
2453  if( length < ttf->goffsets[ glyphID+1 ] )
2454  return nullptr;
2455 
2456  length = ttf->goffsets[glyphID+1] - ttf->goffsets[glyphID];
2457 
2458  GlyphData* d = static_cast<GlyphData*>(malloc(sizeof(GlyphData))); assert(d != nullptr);
2459 
2460  if (length > 0) {
2461  const sal_uInt8* srcptr = glyf + ttf->goffsets[glyphID];
2462  const size_t nChunkLen = ((length + 1) & ~1);
2463  d->ptr = static_cast<sal_uInt8*>(malloc(nChunkLen)); assert(d->ptr != nullptr);
2464  memcpy(d->ptr, srcptr, length);
2465  memset(d->ptr + length, 0, nChunkLen - length);
2466  d->compflag = (GetInt16( srcptr, 0 ) < 0);
2467  } else {
2468  d->ptr = nullptr;
2469  d->compflag = false;
2470  }
2471 
2472  d->glyphID = glyphID;
2473  d->nbytes = static_cast<sal_uInt16>((length + 1) & ~1);
2474 
2475  /* now calculate npoints and ncontours */
2476  ControlPoint *cp;
2477  n = GetTTGlyphPoints(ttf, glyphID, &cp);
2478  if (n > 0)
2479  {
2480  int m = 0;
2481  for (int i = 0; i < n; i++)
2482  {
2483  if (cp[i].flags & 0x8000)
2484  m++;
2485  }
2486  d->npoints = static_cast<sal_uInt16>(n);
2487  d->ncontours = static_cast<sal_uInt16>(m);
2488  free(cp);
2489  } else {
2490  d->npoints = 0;
2491  d->ncontours = 0;
2492  }
2493 
2494  /* get advance width and left sidebearing */
2495  if (glyphID < ttf->numberOfHMetrics) {
2496  d->aw = GetUInt16(hmtx, 4 * glyphID);
2497  d->lsb = GetInt16(hmtx, 4 * glyphID + 2);
2498  } else {
2499  d->aw = GetUInt16(hmtx, 4 * (ttf->numberOfHMetrics - 1));
2500  d->lsb = GetInt16(hmtx + ttf->numberOfHMetrics * 4, (glyphID - ttf->numberOfHMetrics) * 2);
2501  }
2502 
2503  return d;
2504 }
2505 
2507 {
2508  const sal_uInt8* table = getTable(ttf, O_name);
2509  int nTableSize = getTableSize(ttf, O_name );
2510 
2511  if (nTableSize < 6)
2512  {
2513 #if OSL_DEBUG_LEVEL > 1
2514  SAL_WARN("vcl.fonts", "O_name table too small.");
2515 #endif
2516  return 0;
2517  }
2518 
2519  sal_uInt16 n = GetUInt16(table, 2);
2520  int nStrBase = GetUInt16(table, 4);
2521  int i;
2522 
2523  *nr = nullptr;
2524  if (n == 0) return 0;
2525 
2526  const sal_uInt32 remaining_table_size = nTableSize-6;
2527  const sal_uInt32 nMinRecordSize = 12;
2528  const sal_uInt32 nMaxRecords = remaining_table_size / nMinRecordSize;
2529  if (n > nMaxRecords)
2530  {
2531  SAL_WARN("vcl.fonts", "Parsing error in " << OUString::createFromAscii(ttf->fname) <<
2532  ": " << nMaxRecords << " max possible entries, but " <<
2533  n << " claimed, truncating");
2534  n = nMaxRecords;
2535  }
2536 
2537  NameRecord* rec = static_cast<NameRecord*>(calloc(n, sizeof(NameRecord)));
2538  assert(rec);
2539 
2540  for (i = 0; i < n; i++) {
2541  int nLargestFixedOffsetPos = 6 + 10 + 12 * i;
2542  int nMinSize = nLargestFixedOffsetPos + sizeof(sal_uInt16);
2543  if (nMinSize > nTableSize)
2544  {
2545  SAL_WARN( "vcl.fonts", "Font " << OUString::createFromAscii(ttf->fname) << " claimed to have "
2546  << n << " name records, but only space for " << i);
2547  n = i;
2548  break;
2549  }
2550 
2551  rec[i].platformID = GetUInt16(table, 6 + 0 + 12 * i);
2552  rec[i].encodingID = GetUInt16(table, 6 + 2 + 12 * i);
2553  rec[i].languageID = LanguageType(GetUInt16(table, 6 + 4 + 12 * i));
2554  rec[i].nameID = GetUInt16(table, 6 + 6 + 12 * i);
2555  rec[i].slen = GetUInt16(table, 6 + 8 + 12 * i);
2556  int nStrOffset = GetUInt16(table, nLargestFixedOffsetPos);
2557  if (rec[i].slen) {
2558  if( nStrBase+nStrOffset+rec[i].slen >= nTableSize ) {
2559  rec[i].sptr = nullptr;
2560  rec[i].slen = 0;
2561  continue;
2562  }
2563 
2564  const sal_uInt8* rec_string = table + nStrBase + nStrOffset;
2565  // sanity check
2566  const sal_uInt8* end_table = ttf->ptr + ttf->fsize;
2567  const size_t available_space = rec_string > end_table ? 0 : (end_table - rec_string);
2568  if (rec[i].slen <= available_space)
2569  {
2570  rec[i].sptr = static_cast<sal_uInt8 *>(malloc(rec[i].slen)); assert(rec[i].sptr != nullptr);
2571  memcpy(rec[i].sptr, rec_string, rec[i].slen);
2572  }
2573  else
2574  {
2575  rec[i].sptr = nullptr;
2576  rec[i].slen = 0;
2577  }
2578  } else {
2579  rec[i].sptr = nullptr;
2580  }
2581  // some fonts have 3.0 names => fix them to 3.1
2582  if( (rec[i].platformID == 3) && (rec[i].encodingID == 0) )
2583  rec[i].encodingID = 1;
2584  }
2585 
2586  *nr = rec;
2587  return n;
2588 }
2589 
2591 {
2592  int i;
2593  for (i = 0; i < n; i++) {
2594  if (nr[i].sptr) free(nr[i].sptr);
2595  }
2596  free(nr);
2597 }
2598 
2599 template<size_t N> static void
2600 append(std::bitset<N> & rSet, size_t const nOffset, sal_uInt32 const nValue)
2601 {
2602  for (size_t i = 0; i < 32; ++i)
2603  {
2604  rSet.set(nOffset + i, (nValue & (1 << i)) != 0);
2605  }
2606 }
2607 
2609  std::optional<std::bitset<UnicodeCoverage::MAX_UC_ENUM>> &rUnicodeRange,
2610  std::optional<std::bitset<CodePageCoverage::MAX_CP_ENUM>> &rCodePageRange,
2611  const unsigned char* pTable, size_t nLength)
2612 {
2613  bool bRet = false;
2614  // parse OS/2 header
2615  if (nLength >= OS2_Legacy_length)
2616  {
2617  rUnicodeRange = std::bitset<UnicodeCoverage::MAX_UC_ENUM>();
2618  append(*rUnicodeRange, 0, GetUInt32(pTable, OS2_ulUnicodeRange1_offset));
2619  append(*rUnicodeRange, 32, GetUInt32(pTable, OS2_ulUnicodeRange2_offset));
2620  append(*rUnicodeRange, 64, GetUInt32(pTable, OS2_ulUnicodeRange3_offset));
2621  append(*rUnicodeRange, 96, GetUInt32(pTable, OS2_ulUnicodeRange4_offset));
2622  bRet = true;
2623  if (nLength >= OS2_V1_length)
2624  {
2625  rCodePageRange = std::bitset<CodePageCoverage::MAX_CP_ENUM>();
2626  append(*rCodePageRange, 0, GetUInt32(pTable, OS2_ulCodePageRange1_offset));
2627  append(*rCodePageRange, 32, GetUInt32(pTable, OS2_ulCodePageRange2_offset));
2628  }
2629  }
2630  return bRet;
2631 }
2632 
2633 } // namespace vcl
2634 
2635 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static sal_uInt32 getGlyph12(const sal_uInt8 *pCmap, sal_uInt32, sal_uInt32 cChar)
Definition: sft.cxx:1192
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:1366
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
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:1442
int CountTTCFonts(const char *fname)
Get the number of fonts contained in a TrueType collection.
Definition: sft.cxx:1351
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:2590
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:2304
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:1145
TrueTypeTable * TrueTypeTableNew_cmap()
Creates a new empty 'cmap' table.
Definition: ttcr.cxx:967
char * fname
Definition: sft.hxx:724
void CloseTTFont(TrueTypeFont *ttf)
TrueTypeFont destructor.
Definition: sft.cxx:1691
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:2442
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:1040
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
exports com.sun.star. table
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:951
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:839
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:936
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:1485
static void * smalloc(size_t size)
Definition: sft.cxx:111
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
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:861
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:2159
void TrueTypeCreatorDispose(vcl::TrueTypeCreator *_this)
TrueTypeCreator destructor.
Definition: ttcr.cxx:1438
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:2013
SFErrCodes StreamToFile(TrueTypeCreator *_this, const char *fname)
Writes a TrueType font generated by the TrueTypeCreator to a file.
Definition: ttcr.cxx:270
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:893
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:2383
void GetTTFontMetrics(const uint8_t *pHhea, size_t nHhea, const uint8_t *pOs2, size_t nOs2, TTGlobalFontInfo *info)
Returns fonts metrics.
Definition: sft.cxx:2358
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:1030
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:861
float y
#define SAL_N_ELEMENTS(arr)
constexpr sal_uInt32 T_maxp
Definition: sft.hxx:433
sal_uInt32 tag
Definition: ttcr.cxx:482
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
int i
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:692
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:2073
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
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:1117
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:1059
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:820
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:1911
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:2600
void TrueTypeCreatorNewEmpty(sal_uInt32 tag, TrueTypeCreator **_this)
TrueTypeCreator constructor.
Definition: ttcr.cxx:148
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:1710
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:1382
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:2506
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:1038
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:191
int typoDescender
OS/2 portable typographic descender.
Definition: sft.hxx:166
sal_Int16 lsb
left sidebearing
Definition: sft.hxx:126
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:1715
static sal_uInt32 getGlyph4(const sal_uInt8 *cmap, const sal_uInt32 nMaxCmapSize, sal_uInt32 c)
Definition: sft.cxx:1152
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:2065
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
#define SAL_INFO(area, stream)
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:1222
constexpr sal_uInt32 T_otto
Definition: sft.hxx:430
constexpr sal_uInt32 T_post
Definition: sft.hxx:444
bool getTTCoverage(std::optional< std::bitset< UnicodeCoverage::MAX_UC_ENUM >> &rUnicodeRange, std::optional< std::bitset< CodePageCoverage::MAX_CP_ENUM >> &rCodePageRange, const unsigned char *pTable, size_t nLength)
Definition: sft.cxx:2608
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:1079
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
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:2299
void AddTable(TrueTypeCreator *_this, TrueTypeTable *table)
Adds a TrueType table to the TrueType creator.
Definition: ttcr.cxx:160
sal_uInt16 aw
advance width
Definition: sft.hxx:125
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:1007
static int GetTTGlyphOutline(TrueTypeFont *, sal_uInt32, ControlPoint **, TTGlyphMetrics *, std::vector< sal_uInt32 > *)
Definition: sft.cxx:653
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:1199
#define SAL_WARN(area, stream)
char * subfamily
subfamily name
Definition: sft.hxx:149
void(* f)(TrueTypeTable *)
Definition: ttcr.cxx:483
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:1759
int italicAngle
in counter-clockwise degrees * 65536
Definition: sft.hxx:156
sal_uInt16 macStyle
macstyle bits from 'HEAD' table
Definition: sft.hxx:152
sal_Int32 nLength
tuple m
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
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:2315
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:902
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:1131
sal_uInt32 nglyphs
Definition: sft.hxx:736