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

Generated by: LCOV version 1.10