xref: /trunk/main/vcl/source/fontsubset/fontsubset.cxx (revision 88c764bff9c8901fbd5e0d89dee3bedcded6f1b3)
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
10cdf0e10cSrcweir  *
119f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
209f62ea84SAndrew Rist  *************************************************************/
219f62ea84SAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_vcl.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <tools/debug.hxx>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <fontsubset.hxx>
28cdf0e10cSrcweir #include <sft.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir // ====================================================================
31cdf0e10cSrcweir 
FontSubsetInfo()32cdf0e10cSrcweir FontSubsetInfo::FontSubsetInfo()
33cdf0e10cSrcweir :   m_nAscent( 0)
34cdf0e10cSrcweir ,   m_nDescent( 0)
35cdf0e10cSrcweir ,   m_nCapHeight( 0)
36cdf0e10cSrcweir ,   m_nFontType( FontSubsetInfo::NO_FONT)
37cdf0e10cSrcweir ,   mpInFontBytes( NULL)
38cdf0e10cSrcweir ,   mnInByteLength( 0)
39cdf0e10cSrcweir ,   meInFontType( FontSubsetInfo::NO_FONT)
40cdf0e10cSrcweir ,   mpSftTTFont( NULL)
41cdf0e10cSrcweir {}
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // --------------------------------------------------------------------
44cdf0e10cSrcweir 
~FontSubsetInfo()45cdf0e10cSrcweir FontSubsetInfo::~FontSubsetInfo()
46cdf0e10cSrcweir {}
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // --------------------------------------------------------------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // prepare subsetting for fonts where the input font file is mapped
LoadFont(FontSubsetInfo::FontType eInFontType,const unsigned char * pInFontBytes,int nInByteLength)51cdf0e10cSrcweir bool FontSubsetInfo::LoadFont(
52cdf0e10cSrcweir     FontSubsetInfo::FontType eInFontType,
53cdf0e10cSrcweir     const unsigned char* pInFontBytes, int nInByteLength)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     DBG_ASSERT( (mpSftTTFont == NULL), "Subset from SFT and from mapped font-file requested");
56cdf0e10cSrcweir     meInFontType = eInFontType;
57cdf0e10cSrcweir     mpInFontBytes = pInFontBytes;
58cdf0e10cSrcweir     mnInByteLength = nInByteLength;
59cdf0e10cSrcweir     return (mnInByteLength > 0);
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir // --------------------------------------------------------------------
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // prepare subsetting for fonts that are known to the SFT-parser
LoadFont(vcl::_TrueTypeFont * pSftTTFont)65cdf0e10cSrcweir bool FontSubsetInfo::LoadFont( vcl::_TrueTypeFont* pSftTTFont )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     DBG_ASSERT( (mpInFontBytes == NULL), "Subset from SFT and from mapped font-file requested");
68cdf0e10cSrcweir     mpSftTTFont = pSftTTFont;
69cdf0e10cSrcweir     meInFontType = ANY_SFNT;
70cdf0e10cSrcweir     return (mpSftTTFont == NULL);
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir // --------------------------------------------------------------------
74cdf0e10cSrcweir 
CreateFontSubset(int nReqFontTypeMask,FILE * pOutFile,const char * pReqFontName,const sal_GlyphId * pReqGlyphIds,const sal_uInt8 * pReqEncodedIds,int nReqGlyphCount,sal_Int32 * pOutGlyphWidths)75cdf0e10cSrcweir bool FontSubsetInfo::CreateFontSubset(
76cdf0e10cSrcweir     int nReqFontTypeMask,
77cdf0e10cSrcweir     FILE* pOutFile, const char* pReqFontName,
78248a599fSHerbert Dürr     const sal_GlyphId* pReqGlyphIds, const sal_uInt8* pReqEncodedIds, int nReqGlyphCount,
79cdf0e10cSrcweir     sal_Int32* pOutGlyphWidths)
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     // prepare request details needed by all underlying subsetters
82cdf0e10cSrcweir     mnReqFontTypeMask = nReqFontTypeMask;
83cdf0e10cSrcweir     mpOutFile       = pOutFile;
84cdf0e10cSrcweir     mpReqFontName   = pReqFontName;
85cdf0e10cSrcweir     mpReqGlyphIds   = pReqGlyphIds;
86cdf0e10cSrcweir     mpReqEncodedIds = pReqEncodedIds;
87cdf0e10cSrcweir     mnReqGlyphCount = nReqGlyphCount;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     // TODO: move the glyphid/encid/notdef reshuffling from the callers to here
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     // dispatch to underlying subsetters
92cdf0e10cSrcweir     bool bOK = false;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     // TODO: better match available input-type to possible subset-types
95cdf0e10cSrcweir     switch( meInFontType) {
96cdf0e10cSrcweir     case SFNT_TTF:
97cdf0e10cSrcweir     case SFNT_CFF:
98cdf0e10cSrcweir     case ANY_SFNT:
99cdf0e10cSrcweir         bOK = CreateFontSubsetFromSfnt( pOutGlyphWidths);
100cdf0e10cSrcweir         break;
101cdf0e10cSrcweir     case CFF_FONT:
102cdf0e10cSrcweir         bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
103cdf0e10cSrcweir         break;
104cdf0e10cSrcweir     case TYPE1_PFA:
105cdf0e10cSrcweir     case TYPE1_PFB:
106cdf0e10cSrcweir     case ANY_TYPE1:
107cdf0e10cSrcweir         bOK = CreateFontSubsetFromType1( pOutGlyphWidths);
108cdf0e10cSrcweir         break;
109*88c764bfSmseidel         // fall through
110cdf0e10cSrcweir     case NO_FONT:
111*88c764bfSmseidel         // fall through
112cdf0e10cSrcweir     default:
113cdf0e10cSrcweir         DBG_ERROR( "unhandled type in CreateFontSubset()");
114cdf0e10cSrcweir         break;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     return bOK;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir // --------------------------------------------------------------------
121cdf0e10cSrcweir 
122cdf0e10cSrcweir // TODO: move function to sft.cxx to replace dummy implementation
CreateFontSubsetFromSfnt(sal_Int32 * pOutGlyphWidths)123cdf0e10cSrcweir bool FontSubsetInfo::CreateFontSubsetFromSfnt( sal_Int32* pOutGlyphWidths )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     // handle SFNT_CFF fonts
126cdf0e10cSrcweir     int nCffLength = 0;
127cdf0e10cSrcweir     const sal_uInt8* pCffBytes = NULL;
128cdf0e10cSrcweir     if( GetSfntTable( mpSftTTFont, O_CFF, &pCffBytes, &nCffLength))
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         LoadFont( CFF_FONT, pCffBytes, nCffLength);
131cdf0e10cSrcweir         const bool bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
132cdf0e10cSrcweir         return bOK;
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     // handle SFNT_TTF fonts
136cdf0e10cSrcweir     // by forwarding the subset request to AG's sft subsetter
137cdf0e10cSrcweir #if 1 // TODO: remove conversion tp 16bit glyphids when sft-subsetter has been updated
138cdf0e10cSrcweir     sal_uInt16 aShortGlyphIds[256];
139cdf0e10cSrcweir     for( int i = 0; i < mnReqGlyphCount; ++i)
140cdf0e10cSrcweir         aShortGlyphIds[i] = (sal_uInt16)mpReqGlyphIds[i];
141cdf0e10cSrcweir     // remove const_cast when sft-subsetter is const-correct
142cdf0e10cSrcweir     sal_uInt8* pEncArray = const_cast<sal_uInt8*>( mpReqEncodedIds );
143cdf0e10cSrcweir #endif
144cdf0e10cSrcweir     int nSFTErr = vcl::SF_BADARG;
145cdf0e10cSrcweir     if( (mnReqFontTypeMask & TYPE42_FONT) != 0 )
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         nSFTErr = CreateT42FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
148cdf0e10cSrcweir             aShortGlyphIds, pEncArray, mnReqGlyphCount );
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir     else if( (mnReqFontTypeMask & TYPE3_FONT) != 0 )
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         nSFTErr = CreateT3FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
153cdf0e10cSrcweir             aShortGlyphIds, pEncArray, mnReqGlyphCount,
154cdf0e10cSrcweir                     0 /* 0 = horizontal, 1 = vertical */ );
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir     else if( (mnReqFontTypeMask & SFNT_TTF) != 0 )
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         // TODO: use CreateTTFromTTGlyphs()
159cdf0e10cSrcweir         // TODO: move functionality from callers here
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     return (nSFTErr != vcl::SF_OK);
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir // --------------------------------------------------------------------
166cdf0e10cSrcweir 
167cdf0e10cSrcweir // TODO: replace dummy implementation
CreateFontSubsetFromType1(sal_Int32 * pOutGlyphWidths)168cdf0e10cSrcweir bool FontSubsetInfo::CreateFontSubsetFromType1( sal_Int32* pOutGlyphWidths)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir #if 0
171cdf0e10cSrcweir     // TODO: replace dummy implementation when someone needs this
172cdf0e10cSrcweir #else
173cdf0e10cSrcweir     (void)pOutGlyphWidths;
174cdf0e10cSrcweir     fprintf(stderr,"CreateFontSubsetFromType1: replace dummy implementation\n");
175cdf0e10cSrcweir #endif
176cdf0e10cSrcweir     return false;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179*88c764bfSmseidel /* vim: set noet sw=4 ts=4: */
180