LCOV - code coverage report
Current view: top level - vcl/source/fontsubset - sft.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 369 1370 26.9 %
Date: 2014-11-03 Functions: 23 64 35.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10