1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "glyphset.hxx"
32*cdf0e10cSrcweir #include "psputil.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "sft.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "printergfx.hxx"
37*cdf0e10cSrcweir #include "fontsubset.hxx"
38*cdf0e10cSrcweir #include "vcl/fontmanager.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "osl/thread.h"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include "sal/alloca.h"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include "rtl/ustring.hxx"
45*cdf0e10cSrcweir #include "rtl/strbuf.hxx"
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <set>
48*cdf0e10cSrcweir #include <map>
49*cdf0e10cSrcweir #include <algorithm>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir using namespace vcl;
52*cdf0e10cSrcweir using namespace psp;
53*cdf0e10cSrcweir using namespace rtl;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir GlyphSet::GlyphSet ()
56*cdf0e10cSrcweir         : mnFontID   (-1),
57*cdf0e10cSrcweir           mbVertical (0),
58*cdf0e10cSrcweir           mbUseFontEncoding (false)
59*cdf0e10cSrcweir {}
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir GlyphSet::GlyphSet (sal_Int32 nFontID, sal_Bool bVertical)
62*cdf0e10cSrcweir         : mnFontID (nFontID),
63*cdf0e10cSrcweir           mbVertical (bVertical)
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir     PrintFontManager &rMgr = PrintFontManager::get();
66*cdf0e10cSrcweir     meBaseType      	= rMgr.getFontType (mnFontID);
67*cdf0e10cSrcweir     maBaseName      	= OUStringToOString (rMgr.getPSName(mnFontID),
68*cdf0e10cSrcweir                                            RTL_TEXTENCODING_ASCII_US);
69*cdf0e10cSrcweir     mnBaseEncoding  	= rMgr.getFontEncoding(mnFontID);
70*cdf0e10cSrcweir     mbUseFontEncoding	= rMgr.getUseOnlyFontEncoding(mnFontID);
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir GlyphSet::~GlyphSet ()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir     /* FIXME delete the glyphlist ??? */
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir sal_Int32
79*cdf0e10cSrcweir GlyphSet::GetFontID ()
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     return mnFontID;
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir fonttype::type
85*cdf0e10cSrcweir GlyphSet::GetFontType ()
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     return meBaseType;
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir sal_Bool
91*cdf0e10cSrcweir GlyphSet::IsVertical ()
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir     return mbVertical;
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir sal_Bool
97*cdf0e10cSrcweir GlyphSet::SetFont (sal_Int32 nFontID, sal_Bool bVertical)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     if (mnFontID != -1)
100*cdf0e10cSrcweir         return sal_False;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     mnFontID   = nFontID;
103*cdf0e10cSrcweir     mbVertical = bVertical;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     PrintFontManager &rMgr = PrintFontManager::get();
106*cdf0e10cSrcweir     meBaseType      = rMgr.getFontType (mnFontID);
107*cdf0e10cSrcweir     maBaseName      = OUStringToOString (rMgr.getPSName(mnFontID),
108*cdf0e10cSrcweir                                            RTL_TEXTENCODING_ASCII_US);
109*cdf0e10cSrcweir     mnBaseEncoding  = rMgr.getFontEncoding(mnFontID);
110*cdf0e10cSrcweir     mbUseFontEncoding	= rMgr.getUseOnlyFontEncoding(mnFontID);
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     return sal_True;
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir sal_Bool
116*cdf0e10cSrcweir GlyphSet::GetCharID (
117*cdf0e10cSrcweir                      sal_Unicode nChar,
118*cdf0e10cSrcweir                      sal_uChar* nOutGlyphID,
119*cdf0e10cSrcweir                      sal_Int32* nOutGlyphSetID
120*cdf0e10cSrcweir                      )
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     return    LookupCharID (nChar, nOutGlyphID, nOutGlyphSetID)
123*cdf0e10cSrcweir            || AddCharID    (nChar, nOutGlyphID, nOutGlyphSetID);
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir sal_Bool
127*cdf0e10cSrcweir GlyphSet::GetGlyphID (
128*cdf0e10cSrcweir                       sal_uInt32 nGlyph,
129*cdf0e10cSrcweir                       sal_Unicode nUnicode,
130*cdf0e10cSrcweir                       sal_uChar* nOutGlyphID,
131*cdf0e10cSrcweir                       sal_Int32* nOutGlyphSetID
132*cdf0e10cSrcweir                      )
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     return    LookupGlyphID (nGlyph, nOutGlyphID, nOutGlyphSetID)
135*cdf0e10cSrcweir            || AddGlyphID    (nGlyph, nUnicode, nOutGlyphID, nOutGlyphSetID);
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir sal_Bool
139*cdf0e10cSrcweir GlyphSet::LookupCharID (
140*cdf0e10cSrcweir                         sal_Unicode nChar,
141*cdf0e10cSrcweir                         sal_uChar* nOutGlyphID,
142*cdf0e10cSrcweir                         sal_Int32* nOutGlyphSetID
143*cdf0e10cSrcweir                         )
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir     char_list_t::iterator aGlyphSet;
146*cdf0e10cSrcweir     sal_Int32             nGlyphSetID;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     // loop thru all the font subsets
149*cdf0e10cSrcweir     for (aGlyphSet  = maCharList.begin(), nGlyphSetID = 1;
150*cdf0e10cSrcweir          aGlyphSet != maCharList.end();
151*cdf0e10cSrcweir          ++aGlyphSet, nGlyphSetID++)
152*cdf0e10cSrcweir     {
153*cdf0e10cSrcweir         // check every subset if it contains the queried unicode char
154*cdf0e10cSrcweir         char_map_t::const_iterator aGlyph = (*aGlyphSet).find (nChar);
155*cdf0e10cSrcweir         if (aGlyph != (*aGlyphSet).end())
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             // success: found the unicode char, return the glyphid and the glyphsetid
158*cdf0e10cSrcweir             *nOutGlyphSetID = nGlyphSetID;
159*cdf0e10cSrcweir             *nOutGlyphID    = (*aGlyph).second;
160*cdf0e10cSrcweir             return sal_True;
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     *nOutGlyphSetID = -1;
165*cdf0e10cSrcweir     *nOutGlyphID    =  0;
166*cdf0e10cSrcweir     return sal_False;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir sal_Bool
170*cdf0e10cSrcweir GlyphSet::LookupGlyphID (
171*cdf0e10cSrcweir                         sal_uInt32 nGlyph,
172*cdf0e10cSrcweir                         sal_uChar* nOutGlyphID,
173*cdf0e10cSrcweir                         sal_Int32* nOutGlyphSetID
174*cdf0e10cSrcweir                         )
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir     glyph_list_t::iterator aGlyphSet;
177*cdf0e10cSrcweir     sal_Int32             nGlyphSetID;
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     // loop thru all the font subsets
180*cdf0e10cSrcweir     for (aGlyphSet  = maGlyphList.begin(), nGlyphSetID = 1;
181*cdf0e10cSrcweir          aGlyphSet != maGlyphList.end();
182*cdf0e10cSrcweir          ++aGlyphSet, nGlyphSetID++)
183*cdf0e10cSrcweir     {
184*cdf0e10cSrcweir         // check every subset if it contains the queried unicode char
185*cdf0e10cSrcweir         glyph_map_t::const_iterator aGlyph = (*aGlyphSet).find (nGlyph);
186*cdf0e10cSrcweir         if (aGlyph != (*aGlyphSet).end())
187*cdf0e10cSrcweir         {
188*cdf0e10cSrcweir             // success: found the glyph id, return the mapped glyphid and the glyphsetid
189*cdf0e10cSrcweir             *nOutGlyphSetID = nGlyphSetID;
190*cdf0e10cSrcweir             *nOutGlyphID    = (*aGlyph).second;
191*cdf0e10cSrcweir             return sal_True;
192*cdf0e10cSrcweir         }
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     *nOutGlyphSetID = -1;
196*cdf0e10cSrcweir     *nOutGlyphID    =  0;
197*cdf0e10cSrcweir     return sal_False;
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir sal_uChar
201*cdf0e10cSrcweir GlyphSet::GetAnsiMapping (sal_Unicode nUnicodeChar)
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     static rtl_UnicodeToTextConverter aConverter =
204*cdf0e10cSrcweir                 rtl_createUnicodeToTextConverter(RTL_TEXTENCODING_MS_1252);
205*cdf0e10cSrcweir 	static rtl_UnicodeToTextContext aContext =
206*cdf0e10cSrcweir 		 	rtl_createUnicodeToTextContext( aConverter );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     sal_Char            nAnsiChar;
209*cdf0e10cSrcweir 	sal_uInt32          nCvtInfo;
210*cdf0e10cSrcweir 	sal_Size            nCvtChars;
211*cdf0e10cSrcweir    	const sal_uInt32    nCvtFlags =  RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
212*cdf0e10cSrcweir                                    | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR;
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 	sal_Size nSize = rtl_convertUnicodeToText( aConverter, aContext,
215*cdf0e10cSrcweir 				&nUnicodeChar, 1, &nAnsiChar, 1,
216*cdf0e10cSrcweir 				nCvtFlags, &nCvtInfo, &nCvtChars );
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     return nSize == 1 ? (sal_uChar)nAnsiChar : (sal_uChar)0;
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir sal_uChar
222*cdf0e10cSrcweir GlyphSet::GetSymbolMapping (sal_Unicode nUnicodeChar)
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir     if (0x0000 < nUnicodeChar && nUnicodeChar < 0x0100)
225*cdf0e10cSrcweir         return (sal_uChar)nUnicodeChar;
226*cdf0e10cSrcweir     if (0xf000 < nUnicodeChar && nUnicodeChar < 0xf100)
227*cdf0e10cSrcweir         return (sal_uChar)nUnicodeChar;
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     return 0;
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir void
233*cdf0e10cSrcweir GlyphSet::AddNotdef (char_map_t &rCharMap)
234*cdf0e10cSrcweir {
235*cdf0e10cSrcweir     if (rCharMap.size() == 0)
236*cdf0e10cSrcweir         rCharMap[0] = 0;
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir void
240*cdf0e10cSrcweir GlyphSet::AddNotdef (glyph_map_t &rGlyphMap)
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir     if (rGlyphMap.size() == 0)
243*cdf0e10cSrcweir         rGlyphMap[0] = 0;
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir sal_Bool
246*cdf0e10cSrcweir GlyphSet::AddCharID (
247*cdf0e10cSrcweir                      sal_Unicode nChar,
248*cdf0e10cSrcweir                      sal_uChar* nOutGlyphID,
249*cdf0e10cSrcweir                      sal_Int32* nOutGlyphSetID
250*cdf0e10cSrcweir                      )
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir     sal_uChar nMappedChar;
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     // XXX important: avoid to reencode type1 symbol fonts
255*cdf0e10cSrcweir     if (mnBaseEncoding == RTL_TEXTENCODING_SYMBOL)
256*cdf0e10cSrcweir         nMappedChar = GetSymbolMapping (nChar);
257*cdf0e10cSrcweir     else
258*cdf0e10cSrcweir         nMappedChar = GetAnsiMapping (nChar);
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     // create an empty glyphmap that is reserved for iso1252 encoded glyphs
261*cdf0e10cSrcweir     // (or -- unencoded -- symbol glyphs) and a second map that takes any other
262*cdf0e10cSrcweir     if (maCharList.empty())
263*cdf0e10cSrcweir     {
264*cdf0e10cSrcweir         char_map_t aMap, aMapp;
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir         maCharList.push_back (aMap);
267*cdf0e10cSrcweir         maCharList.push_back (aMapp);
268*cdf0e10cSrcweir     }
269*cdf0e10cSrcweir     // if the last map is full, create a new one
270*cdf0e10cSrcweir     if ((!nMappedChar) && (maCharList.back().size() == 255))
271*cdf0e10cSrcweir     {
272*cdf0e10cSrcweir         char_map_t aMap;
273*cdf0e10cSrcweir         maCharList.push_back (aMap);
274*cdf0e10cSrcweir     }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir     // insert a new glyph in the font subset
277*cdf0e10cSrcweir     if (nMappedChar)
278*cdf0e10cSrcweir     {
279*cdf0e10cSrcweir         // always put iso1252 chars into the first map, map them on itself
280*cdf0e10cSrcweir         char_map_t& aGlyphSet = maCharList.front();
281*cdf0e10cSrcweir         AddNotdef (aGlyphSet);
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir         aGlyphSet [nChar] = nMappedChar;
284*cdf0e10cSrcweir         *nOutGlyphSetID   = 1;
285*cdf0e10cSrcweir         *nOutGlyphID      = nMappedChar;
286*cdf0e10cSrcweir     }
287*cdf0e10cSrcweir     else
288*cdf0e10cSrcweir     {
289*cdf0e10cSrcweir         // other chars are just appended to the list
290*cdf0e10cSrcweir         char_map_t& aGlyphSet = maCharList.back();
291*cdf0e10cSrcweir         AddNotdef (aGlyphSet);
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir         int nSize         = aGlyphSet.size();
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir         aGlyphSet [nChar] = nSize;
296*cdf0e10cSrcweir         *nOutGlyphSetID   = maCharList.size();
297*cdf0e10cSrcweir         *nOutGlyphID      = aGlyphSet [nChar];
298*cdf0e10cSrcweir     }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     return sal_True;
301*cdf0e10cSrcweir }
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir sal_Bool
304*cdf0e10cSrcweir GlyphSet::AddGlyphID (
305*cdf0e10cSrcweir                      sal_uInt32 nGlyph,
306*cdf0e10cSrcweir                      sal_Unicode nUnicode,
307*cdf0e10cSrcweir                      sal_uChar* nOutGlyphID,
308*cdf0e10cSrcweir                      sal_Int32* nOutGlyphSetID
309*cdf0e10cSrcweir                      )
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir     sal_uChar nMappedChar;
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir     // XXX important: avoid to reencode type1 symbol fonts
314*cdf0e10cSrcweir     if (mnBaseEncoding == RTL_TEXTENCODING_SYMBOL)
315*cdf0e10cSrcweir         nMappedChar = GetSymbolMapping (nUnicode);
316*cdf0e10cSrcweir     else
317*cdf0e10cSrcweir         nMappedChar = GetAnsiMapping (nUnicode);
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir     // create an empty glyphmap that is reserved for iso1252 encoded glyphs
320*cdf0e10cSrcweir     // (or -- unencoded -- symbol glyphs) and a second map that takes any other
321*cdf0e10cSrcweir     if (maGlyphList.empty())
322*cdf0e10cSrcweir     {
323*cdf0e10cSrcweir         glyph_map_t aMap, aMapp;
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir         maGlyphList.push_back (aMap);
326*cdf0e10cSrcweir         maGlyphList.push_back (aMapp);
327*cdf0e10cSrcweir     }
328*cdf0e10cSrcweir     // if the last map is full, create a new one
329*cdf0e10cSrcweir     if ((!nMappedChar) && (maGlyphList.back().size() == 255))
330*cdf0e10cSrcweir     {
331*cdf0e10cSrcweir         glyph_map_t aMap;
332*cdf0e10cSrcweir         maGlyphList.push_back (aMap);
333*cdf0e10cSrcweir     }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir     // insert a new glyph in the font subset
336*cdf0e10cSrcweir     if (nMappedChar)
337*cdf0e10cSrcweir     {
338*cdf0e10cSrcweir         // always put iso1252 chars into the first map, map them on itself
339*cdf0e10cSrcweir         glyph_map_t& aGlyphSet = maGlyphList.front();
340*cdf0e10cSrcweir         AddNotdef (aGlyphSet);
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir         aGlyphSet [nGlyph] = nMappedChar;
343*cdf0e10cSrcweir         *nOutGlyphSetID    = 1;
344*cdf0e10cSrcweir         *nOutGlyphID       = nMappedChar;
345*cdf0e10cSrcweir     }
346*cdf0e10cSrcweir     else
347*cdf0e10cSrcweir     {
348*cdf0e10cSrcweir         // other chars are just appended to the list
349*cdf0e10cSrcweir         glyph_map_t& aGlyphSet = maGlyphList.back();
350*cdf0e10cSrcweir         AddNotdef (aGlyphSet);
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir         int nSize         = aGlyphSet.size();
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir         aGlyphSet [nGlyph] = nSize;
355*cdf0e10cSrcweir         *nOutGlyphSetID   = maGlyphList.size();
356*cdf0e10cSrcweir         *nOutGlyphID      = aGlyphSet [nGlyph];
357*cdf0e10cSrcweir     }
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir     return sal_True;
360*cdf0e10cSrcweir }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir OString
363*cdf0e10cSrcweir GlyphSet::GetCharSetName (sal_Int32 nGlyphSetID)
364*cdf0e10cSrcweir {
365*cdf0e10cSrcweir     if (meBaseType == fonttype::TrueType)
366*cdf0e10cSrcweir     {
367*cdf0e10cSrcweir         OStringBuffer aSetName( maBaseName.getLength() + 32 );
368*cdf0e10cSrcweir         aSetName.append( maBaseName );
369*cdf0e10cSrcweir         aSetName.append( "FID" );
370*cdf0e10cSrcweir         aSetName.append( mnFontID );
371*cdf0e10cSrcweir         aSetName.append( mbVertical ? "VCSet" : "HCSet" );
372*cdf0e10cSrcweir         aSetName.append( nGlyphSetID );
373*cdf0e10cSrcweir         return aSetName.makeStringAndClear();
374*cdf0e10cSrcweir     }
375*cdf0e10cSrcweir     else
376*cdf0e10cSrcweir     /* (meBaseType == fonttype::Type1 || meBaseType == fonttype::Builtin) */
377*cdf0e10cSrcweir     {
378*cdf0e10cSrcweir         return maBaseName;
379*cdf0e10cSrcweir     }
380*cdf0e10cSrcweir }
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir OString
383*cdf0e10cSrcweir GlyphSet::GetGlyphSetName (sal_Int32 nGlyphSetID)
384*cdf0e10cSrcweir {
385*cdf0e10cSrcweir     if (meBaseType == fonttype::TrueType)
386*cdf0e10cSrcweir     {
387*cdf0e10cSrcweir         OStringBuffer aSetName( maBaseName.getLength() + 32 );
388*cdf0e10cSrcweir         aSetName.append( maBaseName );
389*cdf0e10cSrcweir         aSetName.append( "FID" );
390*cdf0e10cSrcweir         aSetName.append( mnFontID );
391*cdf0e10cSrcweir         aSetName.append( mbVertical ? "VGSet" : "HGSet" );
392*cdf0e10cSrcweir         aSetName.append( nGlyphSetID );
393*cdf0e10cSrcweir         return aSetName.makeStringAndClear();
394*cdf0e10cSrcweir     }
395*cdf0e10cSrcweir     else
396*cdf0e10cSrcweir     /* (meBaseType == fonttype::Type1 || meBaseType == fonttype::Builtin) */
397*cdf0e10cSrcweir     {
398*cdf0e10cSrcweir         return maBaseName;
399*cdf0e10cSrcweir     }
400*cdf0e10cSrcweir }
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir sal_Int32
403*cdf0e10cSrcweir GlyphSet::GetGlyphSetEncoding (sal_Int32 nGlyphSetID)
404*cdf0e10cSrcweir {
405*cdf0e10cSrcweir     if (meBaseType == fonttype::TrueType)
406*cdf0e10cSrcweir         return RTL_TEXTENCODING_DONTKNOW;
407*cdf0e10cSrcweir     else
408*cdf0e10cSrcweir     {
409*cdf0e10cSrcweir     /* (meBaseType == fonttype::Type1 || meBaseType == fonttype::Builtin) */
410*cdf0e10cSrcweir         if (mnBaseEncoding == RTL_TEXTENCODING_SYMBOL)
411*cdf0e10cSrcweir             return RTL_TEXTENCODING_SYMBOL;
412*cdf0e10cSrcweir         else
413*cdf0e10cSrcweir             return nGlyphSetID == 1 ? RTL_TEXTENCODING_MS_1252
414*cdf0e10cSrcweir                                     : RTL_TEXTENCODING_USER_START + nGlyphSetID;
415*cdf0e10cSrcweir     }
416*cdf0e10cSrcweir }
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir OString
419*cdf0e10cSrcweir GlyphSet::GetGlyphSetEncodingName (rtl_TextEncoding nEnc, const OString &rFontName)
420*cdf0e10cSrcweir {
421*cdf0e10cSrcweir     if (   nEnc == RTL_TEXTENCODING_MS_1252
422*cdf0e10cSrcweir         || nEnc == RTL_TEXTENCODING_ISO_8859_1)
423*cdf0e10cSrcweir     {
424*cdf0e10cSrcweir         return OString("ISO1252Encoding");
425*cdf0e10cSrcweir     }
426*cdf0e10cSrcweir     else
427*cdf0e10cSrcweir     if (nEnc >= RTL_TEXTENCODING_USER_START && nEnc <= RTL_TEXTENCODING_USER_END)
428*cdf0e10cSrcweir     {
429*cdf0e10cSrcweir         return  rFontName
430*cdf0e10cSrcweir                 + OString("Enc")
431*cdf0e10cSrcweir                 + OString::valueOf ((sal_Int32)(nEnc - RTL_TEXTENCODING_USER_START));
432*cdf0e10cSrcweir     }
433*cdf0e10cSrcweir     else
434*cdf0e10cSrcweir     {
435*cdf0e10cSrcweir         return OString();
436*cdf0e10cSrcweir     }
437*cdf0e10cSrcweir }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir OString
440*cdf0e10cSrcweir GlyphSet::GetGlyphSetEncodingName (sal_Int32 nGlyphSetID)
441*cdf0e10cSrcweir {
442*cdf0e10cSrcweir     return GetGlyphSetEncodingName (GetGlyphSetEncoding(nGlyphSetID), maBaseName);
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir void
446*cdf0e10cSrcweir GlyphSet::PSDefineReencodedFont (osl::File* pOutFile, sal_Int32 nGlyphSetID)
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir     // only for ps fonts
449*cdf0e10cSrcweir     if ((meBaseType != fonttype::Builtin) && (meBaseType != fonttype::Type1))
450*cdf0e10cSrcweir         return;
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir     sal_Char  pEncodingVector [256];
453*cdf0e10cSrcweir     sal_Int32 nSize = 0;
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir     nSize += psp::appendStr ("(", pEncodingVector + nSize);
456*cdf0e10cSrcweir     nSize += psp::appendStr (GetReencodedFontName(nGlyphSetID),
457*cdf0e10cSrcweir                                   pEncodingVector + nSize);
458*cdf0e10cSrcweir     nSize += psp::appendStr (") cvn (", pEncodingVector + nSize);
459*cdf0e10cSrcweir     nSize += psp::appendStr (maBaseName.getStr(),
460*cdf0e10cSrcweir                                   pEncodingVector + nSize);
461*cdf0e10cSrcweir     nSize += psp::appendStr (") cvn ", pEncodingVector + nSize);
462*cdf0e10cSrcweir     nSize += psp::appendStr (GetGlyphSetEncodingName(nGlyphSetID),
463*cdf0e10cSrcweir                                   pEncodingVector + nSize);
464*cdf0e10cSrcweir     nSize += psp::appendStr (" psp_definefont\n",
465*cdf0e10cSrcweir                                   pEncodingVector + nSize);
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir     psp::WritePS (pOutFile, pEncodingVector);
468*cdf0e10cSrcweir }
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir OString
471*cdf0e10cSrcweir GlyphSet::GetReencodedFontName (rtl_TextEncoding nEnc, const OString &rFontName)
472*cdf0e10cSrcweir {
473*cdf0e10cSrcweir     if (   nEnc == RTL_TEXTENCODING_MS_1252
474*cdf0e10cSrcweir         || nEnc == RTL_TEXTENCODING_ISO_8859_1)
475*cdf0e10cSrcweir     {
476*cdf0e10cSrcweir         return rFontName
477*cdf0e10cSrcweir                + OString("-iso1252");
478*cdf0e10cSrcweir     }
479*cdf0e10cSrcweir     else
480*cdf0e10cSrcweir     if (nEnc >= RTL_TEXTENCODING_USER_START && nEnc <= RTL_TEXTENCODING_USER_END)
481*cdf0e10cSrcweir     {
482*cdf0e10cSrcweir         return rFontName
483*cdf0e10cSrcweir                + OString("-enc")
484*cdf0e10cSrcweir                + OString::valueOf ((sal_Int32)(nEnc - RTL_TEXTENCODING_USER_START));
485*cdf0e10cSrcweir     }
486*cdf0e10cSrcweir     else
487*cdf0e10cSrcweir     {
488*cdf0e10cSrcweir         return OString();
489*cdf0e10cSrcweir     }
490*cdf0e10cSrcweir }
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir OString
493*cdf0e10cSrcweir GlyphSet::GetReencodedFontName (sal_Int32 nGlyphSetID)
494*cdf0e10cSrcweir {
495*cdf0e10cSrcweir     return GetReencodedFontName (GetGlyphSetEncoding(nGlyphSetID), maBaseName);
496*cdf0e10cSrcweir }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir void GlyphSet::DrawGlyphs(
499*cdf0e10cSrcweir                           PrinterGfx& rGfx,
500*cdf0e10cSrcweir                           const Point& rPoint,
501*cdf0e10cSrcweir                           const sal_uInt32* pGlyphIds,
502*cdf0e10cSrcweir                           const sal_Unicode* pUnicodes,
503*cdf0e10cSrcweir                           sal_Int16 nLen,
504*cdf0e10cSrcweir                           const sal_Int32* pDeltaArray )
505*cdf0e10cSrcweir {
506*cdf0e10cSrcweir     sal_uChar *pGlyphID    = (sal_uChar*)alloca (nLen * sizeof(sal_uChar));
507*cdf0e10cSrcweir     sal_Int32 *pGlyphSetID = (sal_Int32*)alloca (nLen * sizeof(sal_Int32));
508*cdf0e10cSrcweir     std::set< sal_Int32 > aGlyphSet;
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir     // convert unicode to font glyph id and font subset
511*cdf0e10cSrcweir     for (int nChar = 0; nChar < nLen; nChar++)
512*cdf0e10cSrcweir     {
513*cdf0e10cSrcweir         GetGlyphID (pGlyphIds[nChar], pUnicodes[nChar], pGlyphID + nChar, pGlyphSetID + nChar);
514*cdf0e10cSrcweir         aGlyphSet.insert (pGlyphSetID[nChar]);
515*cdf0e10cSrcweir     }
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir     // loop over all glyph sets to detect substrings that can be xshown together
518*cdf0e10cSrcweir     // without changing the postscript font
519*cdf0e10cSrcweir     sal_Int32 *pDeltaSubset = (sal_Int32*)alloca (nLen * sizeof(sal_Int32));
520*cdf0e10cSrcweir     sal_uChar *pGlyphSubset = (sal_uChar*)alloca (nLen * sizeof(sal_uChar));
521*cdf0e10cSrcweir 
522*cdf0e10cSrcweir     std::set< sal_Int32 >::iterator aSet;
523*cdf0e10cSrcweir     for (aSet = aGlyphSet.begin(); aSet != aGlyphSet.end(); ++aSet)
524*cdf0e10cSrcweir     {
525*cdf0e10cSrcweir         Point     aPoint  = rPoint;
526*cdf0e10cSrcweir         sal_Int32 nOffset = 0;
527*cdf0e10cSrcweir         sal_Int32 nGlyphs = 0;
528*cdf0e10cSrcweir         sal_Int32 nChar;
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir         // get offset to first glyph
531*cdf0e10cSrcweir         for (nChar = 0; (nChar < nLen) && (pGlyphSetID[nChar] != *aSet); nChar++)
532*cdf0e10cSrcweir         {
533*cdf0e10cSrcweir             nOffset = pDeltaArray [nChar];
534*cdf0e10cSrcweir         }
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir         // loop over all chars to extract those that share the current glyph set
537*cdf0e10cSrcweir         for (nChar = 0; nChar < nLen; nChar++)
538*cdf0e10cSrcweir         {
539*cdf0e10cSrcweir             if (pGlyphSetID[nChar] == *aSet)
540*cdf0e10cSrcweir             {
541*cdf0e10cSrcweir                 pGlyphSubset [nGlyphs] = pGlyphID [nChar];
542*cdf0e10cSrcweir                 // the offset to the next glyph is determined by the glyph in
543*cdf0e10cSrcweir                 // front of the next glyph with the same glyphset id
544*cdf0e10cSrcweir                 // most often, this will be the current glyph
545*cdf0e10cSrcweir                 while ((nChar + 1) < nLen)
546*cdf0e10cSrcweir                 {
547*cdf0e10cSrcweir                     if (pGlyphSetID[nChar + 1] == *aSet)
548*cdf0e10cSrcweir                         break;
549*cdf0e10cSrcweir                     else
550*cdf0e10cSrcweir                         nChar += 1;
551*cdf0e10cSrcweir                 }
552*cdf0e10cSrcweir                 pDeltaSubset [nGlyphs] = pDeltaArray[nChar] - nOffset;
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir                 nGlyphs += 1;
555*cdf0e10cSrcweir             }
556*cdf0e10cSrcweir         }
557*cdf0e10cSrcweir 
558*cdf0e10cSrcweir         // show the text using the PrinterGfx text api
559*cdf0e10cSrcweir         aPoint.Move (nOffset, 0);
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir         OString aGlyphSetName(GetGlyphSetName(*aSet));
562*cdf0e10cSrcweir         rGfx.PSSetFont  (aGlyphSetName, GetGlyphSetEncoding(*aSet));
563*cdf0e10cSrcweir         rGfx.PSMoveTo   (aPoint);
564*cdf0e10cSrcweir         rGfx.PSShowText (pGlyphSubset, nGlyphs, nGlyphs, nGlyphs > 1 ? pDeltaSubset : NULL);
565*cdf0e10cSrcweir     }
566*cdf0e10cSrcweir }
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir void
569*cdf0e10cSrcweir GlyphSet::DrawText (PrinterGfx &rGfx, const Point& rPoint,
570*cdf0e10cSrcweir                     const sal_Unicode* pStr, sal_Int16 nLen, const sal_Int32* pDeltaArray)
571*cdf0e10cSrcweir {
572*cdf0e10cSrcweir     // dispatch to the impl method
573*cdf0e10cSrcweir     if (pDeltaArray == NULL)
574*cdf0e10cSrcweir         ImplDrawText (rGfx, rPoint, pStr, nLen);
575*cdf0e10cSrcweir     else
576*cdf0e10cSrcweir         ImplDrawText (rGfx, rPoint, pStr, nLen, pDeltaArray);
577*cdf0e10cSrcweir }
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir void
580*cdf0e10cSrcweir GlyphSet::ImplDrawText (PrinterGfx &rGfx, const Point& rPoint,
581*cdf0e10cSrcweir                         const sal_Unicode* pStr, sal_Int16 nLen)
582*cdf0e10cSrcweir {
583*cdf0e10cSrcweir     rGfx.PSMoveTo (rPoint);
584*cdf0e10cSrcweir 
585*cdf0e10cSrcweir     if( mbUseFontEncoding )
586*cdf0e10cSrcweir     {
587*cdf0e10cSrcweir         OString aPSName( OUStringToOString( rGfx.GetFontMgr().getPSName( mnFontID ), RTL_TEXTENCODING_ISO_8859_1 ) );
588*cdf0e10cSrcweir         OString aBytes( OUStringToOString( OUString( pStr, nLen ), mnBaseEncoding ) );
589*cdf0e10cSrcweir         rGfx.PSSetFont( aPSName, mnBaseEncoding );
590*cdf0e10cSrcweir         rGfx.PSShowText( (const unsigned char*)aBytes.getStr(), nLen, aBytes.getLength() );
591*cdf0e10cSrcweir         return;
592*cdf0e10cSrcweir     }
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir     int nChar;
595*cdf0e10cSrcweir     sal_uChar *pGlyphID    = (sal_uChar*)alloca (nLen * sizeof(sal_uChar));
596*cdf0e10cSrcweir     sal_Int32 *pGlyphSetID = (sal_Int32*)alloca (nLen * sizeof(sal_Int32));
597*cdf0e10cSrcweir 
598*cdf0e10cSrcweir     // convert unicode to glyph id and char set (font subset)
599*cdf0e10cSrcweir     for (nChar = 0; nChar < nLen; nChar++)
600*cdf0e10cSrcweir         GetCharID (pStr[nChar], pGlyphID + nChar, pGlyphSetID + nChar);
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir     // loop over the string to draw subsequent pieces of chars
603*cdf0e10cSrcweir     // with the same postscript font
604*cdf0e10cSrcweir     for (nChar = 0; nChar < nLen; /* atend */)
605*cdf0e10cSrcweir     {
606*cdf0e10cSrcweir         sal_Int32 nGlyphSetID = pGlyphSetID [nChar];
607*cdf0e10cSrcweir         sal_Int32 nGlyphs     = 1;
608*cdf0e10cSrcweir         for (int nNextChar = nChar + 1; nNextChar < nLen; nNextChar++)
609*cdf0e10cSrcweir         {
610*cdf0e10cSrcweir             if (pGlyphSetID[nNextChar] == nGlyphSetID)
611*cdf0e10cSrcweir                 nGlyphs++;
612*cdf0e10cSrcweir             else
613*cdf0e10cSrcweir                 break;
614*cdf0e10cSrcweir         }
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir         // show the text using the PrinterGfx text api
617*cdf0e10cSrcweir         OString aGlyphSetName(GetCharSetName(nGlyphSetID));
618*cdf0e10cSrcweir         rGfx.PSSetFont (aGlyphSetName, GetGlyphSetEncoding(nGlyphSetID));
619*cdf0e10cSrcweir         rGfx.PSShowText (pGlyphID + nChar, nGlyphs, nGlyphs);
620*cdf0e10cSrcweir 
621*cdf0e10cSrcweir         nChar += nGlyphs;
622*cdf0e10cSrcweir     }
623*cdf0e10cSrcweir }
624*cdf0e10cSrcweir 
625*cdf0e10cSrcweir void
626*cdf0e10cSrcweir GlyphSet::ImplDrawText (PrinterGfx &rGfx, const Point& rPoint,
627*cdf0e10cSrcweir                         const sal_Unicode* pStr, sal_Int16 nLen, const sal_Int32* pDeltaArray)
628*cdf0e10cSrcweir {
629*cdf0e10cSrcweir     if( mbUseFontEncoding )
630*cdf0e10cSrcweir     {
631*cdf0e10cSrcweir         OString aPSName( OUStringToOString( rGfx.GetFontMgr().getPSName( mnFontID ), RTL_TEXTENCODING_ISO_8859_1 ) );
632*cdf0e10cSrcweir         OString aBytes( OUStringToOString( OUString( pStr, nLen ), mnBaseEncoding ) );
633*cdf0e10cSrcweir         rGfx.PSMoveTo( rPoint );
634*cdf0e10cSrcweir         rGfx.PSSetFont( aPSName, mnBaseEncoding );
635*cdf0e10cSrcweir         rGfx.PSShowText( (const unsigned char*)aBytes.getStr(), nLen, aBytes.getLength(), pDeltaArray );
636*cdf0e10cSrcweir         return;
637*cdf0e10cSrcweir     }
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir     sal_uChar *pGlyphID    = (sal_uChar*)alloca (nLen * sizeof(sal_uChar));
640*cdf0e10cSrcweir     sal_Int32 *pGlyphSetID = (sal_Int32*)alloca (nLen * sizeof(sal_Int32));
641*cdf0e10cSrcweir     std::set< sal_Int32 > aGlyphSet;
642*cdf0e10cSrcweir 
643*cdf0e10cSrcweir     // convert unicode to font glyph id and font subset
644*cdf0e10cSrcweir     for (int nChar = 0; nChar < nLen; nChar++)
645*cdf0e10cSrcweir     {
646*cdf0e10cSrcweir         GetCharID (pStr[nChar], pGlyphID + nChar, pGlyphSetID + nChar);
647*cdf0e10cSrcweir         aGlyphSet.insert (pGlyphSetID[nChar]);
648*cdf0e10cSrcweir     }
649*cdf0e10cSrcweir 
650*cdf0e10cSrcweir     // loop over all glyph sets to detect substrings that can be xshown together
651*cdf0e10cSrcweir     // without changing the postscript font
652*cdf0e10cSrcweir     sal_Int32 *pDeltaSubset = (sal_Int32*)alloca (nLen * sizeof(sal_Int32));
653*cdf0e10cSrcweir     sal_uChar *pGlyphSubset = (sal_uChar*)alloca (nLen * sizeof(sal_uChar));
654*cdf0e10cSrcweir 
655*cdf0e10cSrcweir     std::set< sal_Int32 >::iterator aSet;
656*cdf0e10cSrcweir     for (aSet = aGlyphSet.begin(); aSet != aGlyphSet.end(); ++aSet)
657*cdf0e10cSrcweir     {
658*cdf0e10cSrcweir         Point     aPoint  = rPoint;
659*cdf0e10cSrcweir         sal_Int32 nOffset = 0;
660*cdf0e10cSrcweir         sal_Int32 nGlyphs = 0;
661*cdf0e10cSrcweir         sal_Int32 nChar;
662*cdf0e10cSrcweir 
663*cdf0e10cSrcweir         // get offset to first glyph
664*cdf0e10cSrcweir         for (nChar = 0; (nChar < nLen) && (pGlyphSetID[nChar] != *aSet); nChar++)
665*cdf0e10cSrcweir         {
666*cdf0e10cSrcweir             nOffset = pDeltaArray [nChar];
667*cdf0e10cSrcweir         }
668*cdf0e10cSrcweir 
669*cdf0e10cSrcweir         // loop over all chars to extract those that share the current glyph set
670*cdf0e10cSrcweir         for (nChar = 0; nChar < nLen; nChar++)
671*cdf0e10cSrcweir         {
672*cdf0e10cSrcweir             if (pGlyphSetID[nChar] == *aSet)
673*cdf0e10cSrcweir             {
674*cdf0e10cSrcweir                 pGlyphSubset [nGlyphs] = pGlyphID [nChar];
675*cdf0e10cSrcweir                 // the offset to the next glyph is determined by the glyph in
676*cdf0e10cSrcweir                 // front of the next glyph with the same glyphset id
677*cdf0e10cSrcweir                 // most often, this will be the current glyph
678*cdf0e10cSrcweir                 while ((nChar + 1) < nLen)
679*cdf0e10cSrcweir                 {
680*cdf0e10cSrcweir                     if (pGlyphSetID[nChar + 1] == *aSet)
681*cdf0e10cSrcweir                         break;
682*cdf0e10cSrcweir                     else
683*cdf0e10cSrcweir                         nChar += 1;
684*cdf0e10cSrcweir                 }
685*cdf0e10cSrcweir                 pDeltaSubset [nGlyphs] = pDeltaArray[nChar] - nOffset;
686*cdf0e10cSrcweir 
687*cdf0e10cSrcweir                 nGlyphs += 1;
688*cdf0e10cSrcweir             }
689*cdf0e10cSrcweir         }
690*cdf0e10cSrcweir 
691*cdf0e10cSrcweir         // show the text using the PrinterGfx text api
692*cdf0e10cSrcweir         aPoint.Move (nOffset, 0);
693*cdf0e10cSrcweir 
694*cdf0e10cSrcweir         OString aGlyphSetName(GetCharSetName(*aSet));
695*cdf0e10cSrcweir         rGfx.PSSetFont  (aGlyphSetName, GetGlyphSetEncoding(*aSet));
696*cdf0e10cSrcweir         rGfx.PSMoveTo   (aPoint);
697*cdf0e10cSrcweir         rGfx.PSShowText (pGlyphSubset, nGlyphs, nGlyphs, nGlyphs > 1 ? pDeltaSubset : NULL);
698*cdf0e10cSrcweir     }
699*cdf0e10cSrcweir }
700*cdf0e10cSrcweir 
701*cdf0e10cSrcweir sal_Bool
702*cdf0e10cSrcweir GlyphSet::PSUploadEncoding(osl::File* pOutFile, PrinterGfx &rGfx)
703*cdf0e10cSrcweir {
704*cdf0e10cSrcweir     // only for ps fonts
705*cdf0e10cSrcweir     if ((meBaseType != fonttype::Builtin) && (meBaseType != fonttype::Type1))
706*cdf0e10cSrcweir         return sal_False;
707*cdf0e10cSrcweir     if (mnBaseEncoding == RTL_TEXTENCODING_SYMBOL)
708*cdf0e10cSrcweir         return sal_False;
709*cdf0e10cSrcweir 
710*cdf0e10cSrcweir     PrintFontManager &rMgr = rGfx.GetFontMgr();
711*cdf0e10cSrcweir 
712*cdf0e10cSrcweir     // loop thru all the font subsets
713*cdf0e10cSrcweir     sal_Int32               nGlyphSetID = 0;
714*cdf0e10cSrcweir     char_list_t::iterator   aGlyphSet;
715*cdf0e10cSrcweir     for (aGlyphSet = maCharList.begin(); aGlyphSet != maCharList.end(); aGlyphSet++)
716*cdf0e10cSrcweir     {
717*cdf0e10cSrcweir         ++nGlyphSetID;
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir         if (nGlyphSetID == 1) // latin1 page uses global reencoding table
720*cdf0e10cSrcweir         {
721*cdf0e10cSrcweir             PSDefineReencodedFont (pOutFile, nGlyphSetID);
722*cdf0e10cSrcweir             continue;
723*cdf0e10cSrcweir         }
724*cdf0e10cSrcweir         if ((*aGlyphSet).size() == 0) // empty set, doesn't need reencoding
725*cdf0e10cSrcweir         {
726*cdf0e10cSrcweir             continue;
727*cdf0e10cSrcweir         }
728*cdf0e10cSrcweir 
729*cdf0e10cSrcweir         // create reencoding table
730*cdf0e10cSrcweir 
731*cdf0e10cSrcweir         sal_Char  pEncodingVector [256];
732*cdf0e10cSrcweir         sal_Int32 nSize = 0;
733*cdf0e10cSrcweir 
734*cdf0e10cSrcweir         nSize += psp::appendStr ("/",
735*cdf0e10cSrcweir                                  pEncodingVector + nSize);
736*cdf0e10cSrcweir         nSize += psp::appendStr (GetGlyphSetEncodingName(nGlyphSetID),
737*cdf0e10cSrcweir                                  pEncodingVector + nSize);
738*cdf0e10cSrcweir         nSize += psp::appendStr (" [ ",
739*cdf0e10cSrcweir                                  pEncodingVector + nSize);
740*cdf0e10cSrcweir 
741*cdf0e10cSrcweir         // need a list of glyphs, sorted by glyphid
742*cdf0e10cSrcweir         typedef std::map< sal_uInt8, sal_Unicode > ps_mapping_t;
743*cdf0e10cSrcweir         typedef ps_mapping_t::value_type ps_value_t;
744*cdf0e10cSrcweir         ps_mapping_t aSortedGlyphSet;
745*cdf0e10cSrcweir 
746*cdf0e10cSrcweir         char_map_t::const_iterator aUnsortedGlyph;
747*cdf0e10cSrcweir         for (aUnsortedGlyph  = (*aGlyphSet).begin();
748*cdf0e10cSrcweir              aUnsortedGlyph != (*aGlyphSet).end();
749*cdf0e10cSrcweir              ++aUnsortedGlyph)
750*cdf0e10cSrcweir         {
751*cdf0e10cSrcweir             aSortedGlyphSet.insert(ps_value_t((*aUnsortedGlyph).second,
752*cdf0e10cSrcweir                                              (*aUnsortedGlyph).first));
753*cdf0e10cSrcweir         }
754*cdf0e10cSrcweir 
755*cdf0e10cSrcweir         ps_mapping_t::const_iterator aSortedGlyph;
756*cdf0e10cSrcweir         // loop thru all the glyphs in the subset
757*cdf0e10cSrcweir         for (aSortedGlyph  = (aSortedGlyphSet).begin();
758*cdf0e10cSrcweir              aSortedGlyph != (aSortedGlyphSet).end();
759*cdf0e10cSrcweir              ++aSortedGlyph)
760*cdf0e10cSrcweir         {
761*cdf0e10cSrcweir             nSize += psp::appendStr ("/",
762*cdf0e10cSrcweir                                      pEncodingVector + nSize);
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir             std::list< OString > aName( rMgr.getAdobeNameFromUnicode((*aSortedGlyph).second) );
765*cdf0e10cSrcweir 
766*cdf0e10cSrcweir             if( aName.begin() != aName.end() )
767*cdf0e10cSrcweir                 nSize += psp::appendStr ( aName.front(), pEncodingVector + nSize);
768*cdf0e10cSrcweir             else
769*cdf0e10cSrcweir                 nSize += psp::appendStr (".notdef", pEncodingVector + nSize );
770*cdf0e10cSrcweir             nSize += psp::appendStr (" ",  pEncodingVector + nSize);
771*cdf0e10cSrcweir             // flush line
772*cdf0e10cSrcweir             if (nSize >= 70)
773*cdf0e10cSrcweir             {
774*cdf0e10cSrcweir                 nSize += psp::appendStr ("\n", pEncodingVector + nSize);
775*cdf0e10cSrcweir                 psp::WritePS (pOutFile, pEncodingVector);
776*cdf0e10cSrcweir                 nSize = 0;
777*cdf0e10cSrcweir             }
778*cdf0e10cSrcweir         }
779*cdf0e10cSrcweir 
780*cdf0e10cSrcweir         nSize += psp::appendStr ("] def\n", pEncodingVector + nSize);
781*cdf0e10cSrcweir         psp::WritePS (pOutFile, pEncodingVector);
782*cdf0e10cSrcweir 
783*cdf0e10cSrcweir         PSDefineReencodedFont (pOutFile, nGlyphSetID);
784*cdf0e10cSrcweir     }
785*cdf0e10cSrcweir 
786*cdf0e10cSrcweir     return sal_True;
787*cdf0e10cSrcweir }
788*cdf0e10cSrcweir 
789*cdf0e10cSrcweir struct EncEntry
790*cdf0e10cSrcweir {
791*cdf0e10cSrcweir     sal_uChar  aEnc;
792*cdf0e10cSrcweir     long       aGID;
793*cdf0e10cSrcweir 
794*cdf0e10cSrcweir     EncEntry() : aEnc( 0 ), aGID( 0 ) {}
795*cdf0e10cSrcweir 
796*cdf0e10cSrcweir     bool operator<( const EncEntry& rRight ) const
797*cdf0e10cSrcweir     { return aEnc < rRight.aEnc; }
798*cdf0e10cSrcweir };
799*cdf0e10cSrcweir 
800*cdf0e10cSrcweir static void CreatePSUploadableFont( TrueTypeFont* pSrcFont, FILE* pTmpFile,
801*cdf0e10cSrcweir 	const char* pGlyphSetName, int nGlyphCount,
802*cdf0e10cSrcweir 	/*const*/ sal_uInt16* pRequestedGlyphs, /*const*/ sal_uChar* pEncoding,
803*cdf0e10cSrcweir 	bool bAllowType42, bool /*bAllowCID*/ )
804*cdf0e10cSrcweir {
805*cdf0e10cSrcweir 	// match the font-subset to the printer capabilities
806*cdf0e10cSrcweir  	// TODO: allow CFF for capable printers
807*cdf0e10cSrcweir 	int nTargetMask = FontSubsetInfo::TYPE1_PFA | FontSubsetInfo::TYPE3_FONT;
808*cdf0e10cSrcweir 	if( bAllowType42 )
809*cdf0e10cSrcweir 		nTargetMask |= FontSubsetInfo::TYPE42_FONT;
810*cdf0e10cSrcweir 
811*cdf0e10cSrcweir     std::vector< EncEntry > aSorted( nGlyphCount, EncEntry() );
812*cdf0e10cSrcweir     for( int i = 0; i < nGlyphCount; i++ )
813*cdf0e10cSrcweir     {
814*cdf0e10cSrcweir         aSorted[i].aEnc = pEncoding[i];
815*cdf0e10cSrcweir         aSorted[i].aGID = pRequestedGlyphs[i];
816*cdf0e10cSrcweir     }
817*cdf0e10cSrcweir 
818*cdf0e10cSrcweir     std::stable_sort( aSorted.begin(), aSorted.end() );
819*cdf0e10cSrcweir 
820*cdf0e10cSrcweir     std::vector< sal_uChar > aEncoding( nGlyphCount );
821*cdf0e10cSrcweir     std::vector< long > aRequestedGlyphs( nGlyphCount );
822*cdf0e10cSrcweir 
823*cdf0e10cSrcweir     for( int i = 0; i < nGlyphCount; i++ )
824*cdf0e10cSrcweir     {
825*cdf0e10cSrcweir         aEncoding[i]        = aSorted[i].aEnc;
826*cdf0e10cSrcweir         aRequestedGlyphs[i] = aSorted[i].aGID;
827*cdf0e10cSrcweir     }
828*cdf0e10cSrcweir 
829*cdf0e10cSrcweir 	FontSubsetInfo aInfo;
830*cdf0e10cSrcweir 	aInfo.LoadFont( pSrcFont );
831*cdf0e10cSrcweir 
832*cdf0e10cSrcweir 	aInfo.CreateFontSubset( nTargetMask, pTmpFile, pGlyphSetName,
833*cdf0e10cSrcweir 		&aRequestedGlyphs[0], &aEncoding[0], nGlyphCount, NULL );
834*cdf0e10cSrcweir }
835*cdf0e10cSrcweir 
836*cdf0e10cSrcweir sal_Bool
837*cdf0e10cSrcweir GlyphSet::PSUploadFont (osl::File& rOutFile, PrinterGfx &rGfx, bool bAllowType42, std::list< OString >& rSuppliedFonts )
838*cdf0e10cSrcweir {
839*cdf0e10cSrcweir     // only for truetype fonts
840*cdf0e10cSrcweir     if (meBaseType != fonttype::TrueType)
841*cdf0e10cSrcweir         return sal_False;
842*cdf0e10cSrcweir 
843*cdf0e10cSrcweir     TrueTypeFont *pTTFont;
844*cdf0e10cSrcweir     OString aTTFileName (rGfx.GetFontMgr().getFontFileSysPath(mnFontID));
845*cdf0e10cSrcweir     int nFace = rGfx.GetFontMgr().getFontFaceNumber(mnFontID);
846*cdf0e10cSrcweir     sal_Int32 nSuccess = OpenTTFontFile(aTTFileName.getStr(), nFace < 0 ? 0 : nFace, &pTTFont);
847*cdf0e10cSrcweir     if (nSuccess != SF_OK)
848*cdf0e10cSrcweir         return sal_False;
849*cdf0e10cSrcweir     FILE* pTmpFile = tmpfile();
850*cdf0e10cSrcweir     if (pTmpFile == NULL)
851*cdf0e10cSrcweir         return sal_False;
852*cdf0e10cSrcweir 
853*cdf0e10cSrcweir     // array of unicode source characters
854*cdf0e10cSrcweir     sal_Unicode pUChars[256];
855*cdf0e10cSrcweir 
856*cdf0e10cSrcweir     // encoding vector maps character encoding to the ordinal number
857*cdf0e10cSrcweir     // of the glyph in the output file
858*cdf0e10cSrcweir     sal_uChar  pEncoding[256];
859*cdf0e10cSrcweir     sal_uInt16 pTTGlyphMapping[256];
860*cdf0e10cSrcweir 	const bool bAllowCID = false; // TODO: nPSLanguageLevel>=3
861*cdf0e10cSrcweir 
862*cdf0e10cSrcweir     // loop thru all the font subsets
863*cdf0e10cSrcweir     sal_Int32 nCharSetID;
864*cdf0e10cSrcweir     char_list_t::iterator aCharSet;
865*cdf0e10cSrcweir     for (aCharSet = maCharList.begin(), nCharSetID = 1;
866*cdf0e10cSrcweir          aCharSet != maCharList.end();
867*cdf0e10cSrcweir          ++aCharSet, nCharSetID++)
868*cdf0e10cSrcweir     {
869*cdf0e10cSrcweir         if ((*aCharSet).size() == 0)
870*cdf0e10cSrcweir             continue;
871*cdf0e10cSrcweir 
872*cdf0e10cSrcweir         // loop thru all the chars in the subset
873*cdf0e10cSrcweir         char_map_t::const_iterator aChar;
874*cdf0e10cSrcweir         sal_Int32 n = 0;
875*cdf0e10cSrcweir         for (aChar = (*aCharSet).begin(); aChar != (*aCharSet).end(); aChar++)
876*cdf0e10cSrcweir         {
877*cdf0e10cSrcweir             pUChars [n]   = (*aChar).first;
878*cdf0e10cSrcweir             pEncoding [n] = (*aChar).second;
879*cdf0e10cSrcweir             n++;
880*cdf0e10cSrcweir         }
881*cdf0e10cSrcweir         // create a mapping from the unicode chars to the char encoding in
882*cdf0e10cSrcweir         // source TrueType font
883*cdf0e10cSrcweir         MapString (pTTFont, pUChars, (*aCharSet).size(), pTTGlyphMapping, mbVertical);
884*cdf0e10cSrcweir 
885*cdf0e10cSrcweir         // create the current subset
886*cdf0e10cSrcweir         OString aCharSetName = GetCharSetName(nCharSetID);
887*cdf0e10cSrcweir         fprintf( pTmpFile, "%%%%BeginResource: font %s\n", aCharSetName.getStr() );
888*cdf0e10cSrcweir         CreatePSUploadableFont( pTTFont, pTmpFile, aCharSetName.getStr(), (*aCharSet).size(),
889*cdf0e10cSrcweir 								pTTGlyphMapping, pEncoding, bAllowType42, bAllowCID );
890*cdf0e10cSrcweir         fprintf( pTmpFile, "%%%%EndResource\n" );
891*cdf0e10cSrcweir         rSuppliedFonts.push_back( aCharSetName );
892*cdf0e10cSrcweir     }
893*cdf0e10cSrcweir 
894*cdf0e10cSrcweir     // loop thru all the font glyph subsets
895*cdf0e10cSrcweir     sal_Int32 nGlyphSetID;
896*cdf0e10cSrcweir     glyph_list_t::iterator aGlyphSet;
897*cdf0e10cSrcweir     for (aGlyphSet = maGlyphList.begin(), nGlyphSetID = 1;
898*cdf0e10cSrcweir          aGlyphSet != maGlyphList.end();
899*cdf0e10cSrcweir          ++aGlyphSet, nGlyphSetID++)
900*cdf0e10cSrcweir     {
901*cdf0e10cSrcweir         if ((*aGlyphSet).size() == 0)
902*cdf0e10cSrcweir             continue;
903*cdf0e10cSrcweir 
904*cdf0e10cSrcweir         // loop thru all the glyphs in the subset
905*cdf0e10cSrcweir         glyph_map_t::const_iterator aGlyph;
906*cdf0e10cSrcweir         sal_Int32 n = 0;
907*cdf0e10cSrcweir         for (aGlyph = (*aGlyphSet).begin(); aGlyph != (*aGlyphSet).end(); aGlyph++)
908*cdf0e10cSrcweir         {
909*cdf0e10cSrcweir             pTTGlyphMapping [n] = (*aGlyph).first;
910*cdf0e10cSrcweir             pEncoding 		[n] = (*aGlyph).second;
911*cdf0e10cSrcweir             n++;
912*cdf0e10cSrcweir         }
913*cdf0e10cSrcweir 
914*cdf0e10cSrcweir         // create the current subset
915*cdf0e10cSrcweir         OString aGlyphSetName = GetGlyphSetName(nGlyphSetID);
916*cdf0e10cSrcweir         fprintf( pTmpFile, "%%%%BeginResource: font %s\n", aGlyphSetName.getStr() );
917*cdf0e10cSrcweir         CreatePSUploadableFont( pTTFont, pTmpFile, aGlyphSetName.getStr(), (*aGlyphSet).size(),
918*cdf0e10cSrcweir 								pTTGlyphMapping, pEncoding, bAllowType42, bAllowCID );
919*cdf0e10cSrcweir         fprintf( pTmpFile, "%%%%EndResource\n" );
920*cdf0e10cSrcweir         rSuppliedFonts.push_back( aGlyphSetName );
921*cdf0e10cSrcweir     }
922*cdf0e10cSrcweir 
923*cdf0e10cSrcweir     // copy the file into the page header
924*cdf0e10cSrcweir     rewind(pTmpFile);
925*cdf0e10cSrcweir     fflush(pTmpFile);
926*cdf0e10cSrcweir 
927*cdf0e10cSrcweir     sal_uChar  pBuffer[0x2000];
928*cdf0e10cSrcweir     sal_uInt64 nIn;
929*cdf0e10cSrcweir     sal_uInt64 nOut;
930*cdf0e10cSrcweir     do
931*cdf0e10cSrcweir     {
932*cdf0e10cSrcweir         nIn = fread(pBuffer, 1, sizeof(pBuffer), pTmpFile);
933*cdf0e10cSrcweir         rOutFile.write (pBuffer, nIn, nOut);
934*cdf0e10cSrcweir     }
935*cdf0e10cSrcweir     while ((nIn == nOut) && !feof(pTmpFile));
936*cdf0e10cSrcweir 
937*cdf0e10cSrcweir     // cleanup
938*cdf0e10cSrcweir     CloseTTFont (pTTFont);
939*cdf0e10cSrcweir     fclose (pTmpFile);
940*cdf0e10cSrcweir 
941*cdf0e10cSrcweir     return sal_True;
942*cdf0e10cSrcweir }
943