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