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