LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/vcl/source/fontsubset - sft.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 381 1345 28.3 %
Date: 2013-07-09 Functions: 23 64 35.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10