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