build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / fontsubset / fontsubset.cxx
blob4eca8a4be76f52b33d0420cb4e1821d20b7c736c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <tools/debug.hxx>
22 #include <osl/diagnose.h>
24 #include <fontsubset.hxx>
25 #include <sft.hxx>
27 FontSubsetInfo::FontSubsetInfo()
28 : m_nAscent( 0)
29 , m_nDescent( 0)
30 , m_nCapHeight( 0)
31 , m_nFontType( FontSubsetInfo::NO_FONT)
32 , mpInFontBytes( nullptr)
33 , mnInByteLength( 0)
34 , meInFontType( FontSubsetInfo::NO_FONT)
35 , mpSftTTFont( nullptr)
36 , mnReqFontTypeMask(0)
37 , mpOutFile(nullptr)
38 , mpReqFontName(nullptr)
39 , mpReqGlyphIds(nullptr)
40 , mpReqEncodedIds(nullptr)
41 , mnReqGlyphCount(0)
45 FontSubsetInfo::~FontSubsetInfo()
49 // prepare subsetting for fonts where the input font file is mapped
50 bool FontSubsetInfo::LoadFont(
51 FontSubsetInfo::FontType eInFontType,
52 const unsigned char* pInFontBytes, int nInByteLength)
54 SAL_WARN_IF( (mpSftTTFont != nullptr), "vcl", "Subset from SFT and from mapped font-file requested");
55 meInFontType = eInFontType;
56 mpInFontBytes = pInFontBytes;
57 mnInByteLength = nInByteLength;
58 return (mnInByteLength > 0);
61 // prepare subsetting for fonts that are known to the SFT-parser
62 bool FontSubsetInfo::LoadFont( vcl::TrueTypeFont* pSftTTFont )
64 SAL_WARN_IF( (mpInFontBytes != nullptr), "vcl", "Subset from SFT and from mapped font-file requested");
65 mpSftTTFont = pSftTTFont;
66 meInFontType = ANY_SFNT;
67 return (mpSftTTFont == nullptr);
70 bool FontSubsetInfo::CreateFontSubset(
71 int nReqFontTypeMask,
72 FILE* pOutFile, const char* pReqFontName,
73 const sal_GlyphId* pReqGlyphIds, const sal_uInt8* pReqEncodedIds, int nReqGlyphCount,
74 sal_Int32* pOutGlyphWidths)
76 // prepare request details needed by all underlying subsetters
77 mnReqFontTypeMask = nReqFontTypeMask;
78 mpOutFile = pOutFile;
79 mpReqFontName = pReqFontName;
80 mpReqGlyphIds = pReqGlyphIds;
81 mpReqEncodedIds = pReqEncodedIds;
82 mnReqGlyphCount = nReqGlyphCount;
84 // TODO: move the glyphid/encid/notdef reshuffling from the callers to here
86 // dispatch to underlying subsetters
87 bool bOK = false;
89 // TODO: better match available input-type to possible subset-types
90 switch( meInFontType) {
91 case SFNT_TTF:
92 case SFNT_CFF:
93 case ANY_SFNT:
94 bOK = CreateFontSubsetFromSfnt( pOutGlyphWidths);
95 break;
96 case CFF_FONT:
97 bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
98 break;
99 case TYPE1_PFA:
100 case TYPE1_PFB:
101 case ANY_TYPE1:
102 bOK = CreateFontSubsetFromType1( pOutGlyphWidths);
103 break;
104 // fall through
105 case NO_FONT:
106 // fall through
107 default:
108 OSL_FAIL( "unhandled type in CreateFontSubset()");
109 break;
112 return bOK;
115 // TODO: move function to sft.cxx to replace dummy implementation
116 bool FontSubsetInfo::CreateFontSubsetFromSfnt( sal_Int32* pOutGlyphWidths )
118 // handle SFNT_CFF fonts
119 int nCffLength = 0;
120 const sal_uInt8* pCffBytes = nullptr;
121 if( GetSfntTable( mpSftTTFont, O_CFF, &pCffBytes, &nCffLength))
123 LoadFont( CFF_FONT, pCffBytes, nCffLength);
124 const bool bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
125 return bOK;
128 // handle SFNT_TTF fonts
129 // by forwarding the subset request to AG's sft subsetter
130 #if 1 // TODO: remove conversion tp 16bit glyphids when sft-subsetter has been updated
131 sal_uInt16 aShortGlyphIds[256];
132 for( int i = 0; i < mnReqGlyphCount; ++i)
133 aShortGlyphIds[i] = (sal_uInt16)mpReqGlyphIds[i];
134 // remove const_cast when sft-subsetter is const-correct
135 sal_uInt8* pEncArray = const_cast<sal_uInt8*>( mpReqEncodedIds );
136 #endif
137 int nSFTErr = vcl::SF_BADARG;
138 if( (mnReqFontTypeMask & TYPE42_FONT) != 0 )
140 nSFTErr = CreateT42FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
141 aShortGlyphIds, pEncArray, mnReqGlyphCount );
143 else if( (mnReqFontTypeMask & TYPE3_FONT) != 0 )
145 nSFTErr = CreateT3FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
146 aShortGlyphIds, pEncArray, mnReqGlyphCount,
147 0 /* 0 = horizontal, 1 = vertical */ );
149 else if( (mnReqFontTypeMask & SFNT_TTF) != 0 )
151 // TODO: use CreateTTFromTTGlyphs()
152 // TODO: move functionality from callers here
155 return (nSFTErr != vcl::SF_OK);
158 // TODO: replace dummy implementation
159 bool FontSubsetInfo::CreateFontSubsetFromType1( sal_Int32* pOutGlyphWidths)
161 (void)pOutGlyphWidths;
162 fprintf(stderr,"CreateFontSubsetFromType1: replace dummy implementation\n");
163 return false;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */