19f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59f62ea84SAndrew Rist  * distributed with this work for additional information
69f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
99f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
109f62ea84SAndrew Rist  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129f62ea84SAndrew Rist  *
139f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149f62ea84SAndrew Rist  * software distributed under the License is distributed on an
159f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
179f62ea84SAndrew Rist  * specific language governing permissions and limitations
189f62ea84SAndrew Rist  * under the License.
199f62ea84SAndrew Rist  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
229f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <fontsubset.hxx>
30cdf0e10cSrcweir #include <sft.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ====================================================================
33cdf0e10cSrcweir 
FontSubsetInfo()34cdf0e10cSrcweir FontSubsetInfo::FontSubsetInfo()
35cdf0e10cSrcweir :	m_nAscent( 0)
36cdf0e10cSrcweir ,	m_nDescent( 0)
37cdf0e10cSrcweir ,	m_nCapHeight( 0)
38cdf0e10cSrcweir ,	m_nFontType( FontSubsetInfo::NO_FONT)
39cdf0e10cSrcweir ,	mpInFontBytes( NULL)
40cdf0e10cSrcweir ,	mnInByteLength( 0)
41cdf0e10cSrcweir ,	meInFontType( FontSubsetInfo::NO_FONT)
42cdf0e10cSrcweir ,	mpSftTTFont( NULL)
43cdf0e10cSrcweir {}
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // --------------------------------------------------------------------
46cdf0e10cSrcweir 
~FontSubsetInfo()47cdf0e10cSrcweir FontSubsetInfo::~FontSubsetInfo()
48cdf0e10cSrcweir {}
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // --------------------------------------------------------------------
51cdf0e10cSrcweir 
52cdf0e10cSrcweir // prepare subsetting for fonts where the input font file is mapped
LoadFont(FontSubsetInfo::FontType eInFontType,const unsigned char * pInFontBytes,int nInByteLength)53cdf0e10cSrcweir bool FontSubsetInfo::LoadFont(
54cdf0e10cSrcweir 	FontSubsetInfo::FontType eInFontType,
55cdf0e10cSrcweir 	const unsigned char* pInFontBytes, int nInByteLength)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	DBG_ASSERT( (mpSftTTFont == NULL), "Subset from SFT and from mapped font-file requested");
58cdf0e10cSrcweir 	meInFontType = eInFontType;
59cdf0e10cSrcweir 	mpInFontBytes = pInFontBytes;
60cdf0e10cSrcweir 	mnInByteLength = nInByteLength;
61cdf0e10cSrcweir 	return (mnInByteLength > 0);
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // --------------------------------------------------------------------
65cdf0e10cSrcweir 
66cdf0e10cSrcweir // prepare subsetting for fonts that are known to the SFT-parser
LoadFont(vcl::_TrueTypeFont * pSftTTFont)67cdf0e10cSrcweir bool FontSubsetInfo::LoadFont( vcl::_TrueTypeFont* pSftTTFont )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	DBG_ASSERT( (mpInFontBytes == NULL), "Subset from SFT and from mapped font-file requested");
70cdf0e10cSrcweir 	mpSftTTFont = pSftTTFont;
71cdf0e10cSrcweir 	meInFontType = ANY_SFNT;
72cdf0e10cSrcweir 	return (mpSftTTFont == NULL);
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // --------------------------------------------------------------------
76cdf0e10cSrcweir 
CreateFontSubset(int nReqFontTypeMask,FILE * pOutFile,const char * pReqFontName,const sal_GlyphId * pReqGlyphIds,const sal_uInt8 * pReqEncodedIds,int nReqGlyphCount,sal_Int32 * pOutGlyphWidths)77cdf0e10cSrcweir bool FontSubsetInfo::CreateFontSubset(
78cdf0e10cSrcweir 	int nReqFontTypeMask,
79cdf0e10cSrcweir 	FILE* pOutFile, const char* pReqFontName,
80*248a599fSHerbert Dürr 	const sal_GlyphId* pReqGlyphIds, const sal_uInt8* pReqEncodedIds, int nReqGlyphCount,
81cdf0e10cSrcweir 	sal_Int32* pOutGlyphWidths)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir 	// prepare request details needed by all underlying subsetters
84cdf0e10cSrcweir 	mnReqFontTypeMask = nReqFontTypeMask;
85cdf0e10cSrcweir 	mpOutFile		= pOutFile;
86cdf0e10cSrcweir 	mpReqFontName	= pReqFontName;
87cdf0e10cSrcweir 	mpReqGlyphIds	= pReqGlyphIds;
88cdf0e10cSrcweir 	mpReqEncodedIds	= pReqEncodedIds;
89cdf0e10cSrcweir 	mnReqGlyphCount	= nReqGlyphCount;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	// TODO: move the glyphid/encid/notdef reshuffling from the callers to here
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	// dispatch to underlying subsetters
94cdf0e10cSrcweir 	bool bOK = false;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	// TODO: better match available input-type to possible subset-types
97cdf0e10cSrcweir 	switch( meInFontType) {
98cdf0e10cSrcweir 	case SFNT_TTF:
99cdf0e10cSrcweir 	case SFNT_CFF:
100cdf0e10cSrcweir 	case ANY_SFNT:
101cdf0e10cSrcweir 		bOK = CreateFontSubsetFromSfnt( pOutGlyphWidths);
102cdf0e10cSrcweir 		break;
103cdf0e10cSrcweir 	case CFF_FONT:
104cdf0e10cSrcweir 		bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
105cdf0e10cSrcweir 		break;
106cdf0e10cSrcweir 	case TYPE1_PFA:
107cdf0e10cSrcweir 	case TYPE1_PFB:
108cdf0e10cSrcweir 	case ANY_TYPE1:
109cdf0e10cSrcweir 		bOK = CreateFontSubsetFromType1( pOutGlyphWidths);
110cdf0e10cSrcweir 		break;
111cdf0e10cSrcweir 		// fall trough
112cdf0e10cSrcweir 	case NO_FONT:
113cdf0e10cSrcweir 		// fall trough
114cdf0e10cSrcweir 	default:
115cdf0e10cSrcweir 		DBG_ERROR( "unhandled type in CreateFontSubset()");
116cdf0e10cSrcweir 		break;
117cdf0e10cSrcweir 	}
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	return bOK;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir // --------------------------------------------------------------------
123cdf0e10cSrcweir 
124cdf0e10cSrcweir // TODO: move function to sft.cxx to replace dummy implementation
CreateFontSubsetFromSfnt(sal_Int32 * pOutGlyphWidths)125cdf0e10cSrcweir bool FontSubsetInfo::CreateFontSubsetFromSfnt( sal_Int32* pOutGlyphWidths )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	// handle SFNT_CFF fonts
128cdf0e10cSrcweir 	int nCffLength = 0;
129cdf0e10cSrcweir 	const sal_uInt8* pCffBytes = NULL;
130cdf0e10cSrcweir 	if( GetSfntTable( mpSftTTFont, O_CFF, &pCffBytes, &nCffLength))
131cdf0e10cSrcweir 	{
132cdf0e10cSrcweir 		LoadFont( CFF_FONT, pCffBytes, nCffLength);
133cdf0e10cSrcweir 		const bool bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
134cdf0e10cSrcweir 		return bOK;
135cdf0e10cSrcweir 	}
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 	// handle SFNT_TTF fonts
138cdf0e10cSrcweir 	// by forwarding the subset request to AG's sft subsetter
139cdf0e10cSrcweir #if 1 // TODO: remove conversion tp 16bit glyphids when sft-subsetter has been updated
140cdf0e10cSrcweir 	sal_uInt16 aShortGlyphIds[256];
141cdf0e10cSrcweir 	for( int i = 0; i < mnReqGlyphCount; ++i)
142cdf0e10cSrcweir 		aShortGlyphIds[i] = (sal_uInt16)mpReqGlyphIds[i];
143cdf0e10cSrcweir 	// remove const_cast when sft-subsetter is const-correct
144cdf0e10cSrcweir 	sal_uInt8* pEncArray = const_cast<sal_uInt8*>( mpReqEncodedIds );
145cdf0e10cSrcweir #endif
146cdf0e10cSrcweir 	int nSFTErr = vcl::SF_BADARG;
147cdf0e10cSrcweir 	if( (mnReqFontTypeMask & TYPE42_FONT) != 0 )
148cdf0e10cSrcweir 	{
149cdf0e10cSrcweir 		nSFTErr = CreateT42FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
150cdf0e10cSrcweir 			aShortGlyphIds, pEncArray, mnReqGlyphCount );
151cdf0e10cSrcweir 	}
152cdf0e10cSrcweir 	else if( (mnReqFontTypeMask & TYPE3_FONT) != 0 )
153cdf0e10cSrcweir 	{
154cdf0e10cSrcweir 		nSFTErr = CreateT3FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
155cdf0e10cSrcweir 			aShortGlyphIds, pEncArray, mnReqGlyphCount,
156cdf0e10cSrcweir             		0 /* 0 = horizontal, 1 = vertical */ );
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir 	else if( (mnReqFontTypeMask & SFNT_TTF) != 0 )
159cdf0e10cSrcweir 	{
160cdf0e10cSrcweir 		// TODO: use CreateTTFromTTGlyphs()
161cdf0e10cSrcweir 		// TODO: move functionality from callers here
162cdf0e10cSrcweir 	}
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	return (nSFTErr != vcl::SF_OK);
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir // --------------------------------------------------------------------
168cdf0e10cSrcweir 
169cdf0e10cSrcweir // TODO: replace dummy implementation
CreateFontSubsetFromType1(sal_Int32 * pOutGlyphWidths)170cdf0e10cSrcweir bool FontSubsetInfo::CreateFontSubsetFromType1( sal_Int32* pOutGlyphWidths)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir #if 0
173cdf0e10cSrcweir 	// TODO: replace dummy implementation when someone needs this
174cdf0e10cSrcweir #else
175cdf0e10cSrcweir 	(void)pOutGlyphWidths;
176cdf0e10cSrcweir 	fprintf(stderr,"CreateFontSubsetFromType1: replace dummy implementation\n");
177cdf0e10cSrcweir #endif
178cdf0e10cSrcweir 	return false;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir // ====================================================================
182cdf0e10cSrcweir 
183