xref: /aoo41x/main/vcl/win/source/gdi/salgdi3.cxx (revision 59213536)
1cdf0e10cSrcweir /*************************************************************************
2cdf0e10cSrcweir  *
3cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4cdf0e10cSrcweir  *
5cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6cdf0e10cSrcweir  *
7cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8cdf0e10cSrcweir  *
9cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10cdf0e10cSrcweir  *
11cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14cdf0e10cSrcweir  *
15cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20cdf0e10cSrcweir  *
21cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  ************************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <string.h>
32cdf0e10cSrcweir #include <malloc.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "rtl/logfile.hxx"
35cdf0e10cSrcweir #include "rtl/tencinfo.h"
36cdf0e10cSrcweir #include "rtl/textcvt.h"
37cdf0e10cSrcweir #include "rtl/bootstrap.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "i18npool/mslangid.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include "osl/module.h"
42cdf0e10cSrcweir #include "osl/file.hxx"
43cdf0e10cSrcweir #include "osl/thread.hxx"
44cdf0e10cSrcweir #include "osl/process.h"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "basegfx/polygon/b2dpolygon.hxx"
47cdf0e10cSrcweir #include "basegfx/polygon/b2dpolypolygon.hxx"
48cdf0e10cSrcweir #include "basegfx/matrix/b2dhommatrix.hxx"
49cdf0e10cSrcweir #include "basegfx/matrix/b2dhommatrixtools.hxx"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include "unotools/fontcfg.hxx"	// for IMPL_FONT_ATTR_SYMBOL
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include "vcl/font.hxx"
54cdf0e10cSrcweir #include "vcl/svapp.hxx"
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #include "tools/poly.hxx"
57cdf0e10cSrcweir #include "tools/debug.hxx"
58cdf0e10cSrcweir #include "tools/stream.hxx"
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #include <tools/prewin.h>
61cdf0e10cSrcweir #include <windows.h>
62cdf0e10cSrcweir #include <tools/postwin.h>
63cdf0e10cSrcweir 
64cdf0e10cSrcweir #include <vcl/sysdata.hxx>
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #include "win/wincomp.hxx"
67cdf0e10cSrcweir #include "win/saldata.hxx"
68cdf0e10cSrcweir #include "win/salgdi.h"
69cdf0e10cSrcweir 
70cdf0e10cSrcweir #include "outfont.hxx"
71cdf0e10cSrcweir #include "fontsubset.hxx"
72cdf0e10cSrcweir #include "sallayout.hxx"
73cdf0e10cSrcweir #include "outdev.h"			// for ImplGlyphFallbackFontSubstitution
74cdf0e10cSrcweir #include "sft.hxx"
75cdf0e10cSrcweir 
76cdf0e10cSrcweir #ifdef GCP_KERN_HACK
77cdf0e10cSrcweir #include <algorithm>
78cdf0e10cSrcweir #endif
79cdf0e10cSrcweir 
80cdf0e10cSrcweir #ifdef ENABLE_GRAPHITE
81cdf0e10cSrcweir #include <graphite/GrClient.h>
82cdf0e10cSrcweir #include <graphite/WinFont.h>
83cdf0e10cSrcweir #endif
84cdf0e10cSrcweir 
85cdf0e10cSrcweir #include <vector>
86cdf0e10cSrcweir #include <set>
87cdf0e10cSrcweir #include <map>
88cdf0e10cSrcweir 
89cdf0e10cSrcweir using namespace vcl;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir static const int MAXFONTHEIGHT = 2048;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir // -----------
94cdf0e10cSrcweir // - Inlines -
95cdf0e10cSrcweir // -----------
96cdf0e10cSrcweir 
97cdf0e10cSrcweir inline FIXED FixedFromDouble( double d )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir     const long l = (long) ( d * 65536. );
100cdf0e10cSrcweir     return *(FIXED*) &l;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir // -----------------------------------------------------------------------
104cdf0e10cSrcweir 
105cdf0e10cSrcweir inline int IntTimes256FromFixed(FIXED f)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     int nFixedTimes256 = (f.value << 8) + ((f.fract+0x80) >> 8);
108cdf0e10cSrcweir     return nFixedTimes256;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir // =======================================================================
112cdf0e10cSrcweir 
113cdf0e10cSrcweir // these variables can be static because they store system wide settings
114cdf0e10cSrcweir static bool bImplSalCourierScalable = false;
115cdf0e10cSrcweir static bool bImplSalCourierNew = false;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 
118cdf0e10cSrcweir // =======================================================================
119cdf0e10cSrcweir 
120cdf0e10cSrcweir // -----------------------------------------------------------------------
121cdf0e10cSrcweir 
122cdf0e10cSrcweir // TODO: also support temporary TTC font files
123cdf0e10cSrcweir typedef std::map< String, ImplDevFontAttributes > FontAttrMap;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir class ImplFontAttrCache
126cdf0e10cSrcweir {
127cdf0e10cSrcweir private:
128cdf0e10cSrcweir 	FontAttrMap		aFontAttributes;
129cdf0e10cSrcweir 	rtl::OUString	aCacheFileName;
130cdf0e10cSrcweir 	String			aBaseURL;
131cdf0e10cSrcweir 	sal_Bool			bModified;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir protected:
134cdf0e10cSrcweir 	String	OptimizeURL( const String& rURL ) const;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     enum{ MAGIC = 0x12349876 }; // change if fontattrcache format changes
137cdf0e10cSrcweir 
138cdf0e10cSrcweir public:
139cdf0e10cSrcweir             ImplFontAttrCache( const String& rCacheFileName, const String& rBaseURL );
140cdf0e10cSrcweir             ~ImplFontAttrCache();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     ImplDevFontAttributes  GetFontAttr( const String& rFontFileName ) const;
143cdf0e10cSrcweir     void                   AddFontAttr( const String& rFontFileName, const ImplDevFontAttributes& );
144cdf0e10cSrcweir };
145cdf0e10cSrcweir 
146cdf0e10cSrcweir ImplFontAttrCache::ImplFontAttrCache( const String& rFileNameURL, const String& rBaseURL ) : aBaseURL( rBaseURL )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir 	bModified = FALSE;
149cdf0e10cSrcweir 	aBaseURL.ToLowerAscii();	// Windows only, no problem...
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     // open the cache file
152cdf0e10cSrcweir     osl::FileBase::getSystemPathFromFileURL( rFileNameURL, aCacheFileName );
153cdf0e10cSrcweir     SvFileStream aCacheFile( aCacheFileName, STREAM_READ );
154cdf0e10cSrcweir     if( !aCacheFile.IsOpen() )
155cdf0e10cSrcweir         return;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     // check the cache version
158cdf0e10cSrcweir     sal_uInt32 nCacheMagic;
159cdf0e10cSrcweir     aCacheFile >> nCacheMagic;
160cdf0e10cSrcweir     if( nCacheMagic != ImplFontAttrCache::MAGIC )
161cdf0e10cSrcweir         return;  // ignore cache and rewrite if no match
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     // read the cache entries from the file
164cdf0e10cSrcweir     String aFontFileURL, aFontName;
165cdf0e10cSrcweir     ImplDevFontAttributes aDFA;
166cdf0e10cSrcweir     for(;;)
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         aCacheFile.ReadByteString( aFontFileURL, RTL_TEXTENCODING_UTF8 );
169cdf0e10cSrcweir         if( !aFontFileURL.Len() )
170cdf0e10cSrcweir             break;
171cdf0e10cSrcweir         aCacheFile.ReadByteString( aDFA.maName, RTL_TEXTENCODING_UTF8 );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         short n;
174cdf0e10cSrcweir         aCacheFile >> n; aDFA.meWeight     = static_cast<FontWeight>(n);
175cdf0e10cSrcweir         aCacheFile >> n; aDFA.meItalic     = static_cast<FontItalic>(n);
176cdf0e10cSrcweir         aCacheFile >> n; aDFA.mePitch      = static_cast<FontPitch>(n);
177cdf0e10cSrcweir         aCacheFile >> n; aDFA.meWidthType  = static_cast<FontWidth>(n);
178cdf0e10cSrcweir         aCacheFile >> n; aDFA.meFamily     = static_cast<FontFamily>(n);
179cdf0e10cSrcweir         aCacheFile >> n; aDFA.mbSymbolFlag = (n != 0);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir         aCacheFile.ReadByteStringLine( aDFA.maStyleName, RTL_TEXTENCODING_UTF8 );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         aFontAttributes[ aFontFileURL ] = aDFA;
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir ImplFontAttrCache::~ImplFontAttrCache()
188cdf0e10cSrcweir {
189cdf0e10cSrcweir 	if ( bModified )
190cdf0e10cSrcweir 	{
191cdf0e10cSrcweir 		SvFileStream aCacheFile( aCacheFileName, STREAM_WRITE|STREAM_TRUNC );
192cdf0e10cSrcweir 		if ( aCacheFile.IsWritable() )
193cdf0e10cSrcweir 		{
194cdf0e10cSrcweir             sal_uInt32 nCacheMagic = ImplFontAttrCache::MAGIC;
195cdf0e10cSrcweir             aCacheFile << nCacheMagic;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 			// write the cache entries to the file
198cdf0e10cSrcweir 			FontAttrMap::const_iterator aIter = aFontAttributes.begin();
199cdf0e10cSrcweir 			while ( aIter != aFontAttributes.end() )
200cdf0e10cSrcweir 			{
201cdf0e10cSrcweir 				const String rFontFileURL( (*aIter).first );
202cdf0e10cSrcweir 				const ImplDevFontAttributes& rDFA( (*aIter).second );
203cdf0e10cSrcweir 				aCacheFile.WriteByteString( rFontFileURL, RTL_TEXTENCODING_UTF8 );
204cdf0e10cSrcweir 				aCacheFile.WriteByteString( rDFA.maName, RTL_TEXTENCODING_UTF8 );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                 aCacheFile << static_cast<short>(rDFA.meWeight);
207cdf0e10cSrcweir                 aCacheFile << static_cast<short>(rDFA.meItalic);
208cdf0e10cSrcweir                 aCacheFile << static_cast<short>(rDFA.mePitch);
209cdf0e10cSrcweir                 aCacheFile << static_cast<short>(rDFA.meWidthType);
210cdf0e10cSrcweir                 aCacheFile << static_cast<short>(rDFA.meFamily);
211cdf0e10cSrcweir                 aCacheFile << static_cast<short>(rDFA.mbSymbolFlag != false);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir                 aCacheFile.WriteByteStringLine( rDFA.maStyleName, RTL_TEXTENCODING_UTF8 );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 				aIter++;
216cdf0e10cSrcweir 			}
217cdf0e10cSrcweir 			// EOF Marker
218cdf0e10cSrcweir 			String aEmptyStr;
219cdf0e10cSrcweir 			aCacheFile.WriteByteString( aEmptyStr, RTL_TEXTENCODING_UTF8 );
220cdf0e10cSrcweir 		}
221cdf0e10cSrcweir 	}
222cdf0e10cSrcweir }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir String ImplFontAttrCache::OptimizeURL( const String& rURL ) const
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	String aOptimizedFontFileURL( rURL );
227cdf0e10cSrcweir 	aOptimizedFontFileURL.ToLowerAscii();	// Windows only, no problem...
228cdf0e10cSrcweir 	if ( aOptimizedFontFileURL.CompareTo( aBaseURL, aBaseURL.Len() ) == COMPARE_EQUAL )
229cdf0e10cSrcweir 		aOptimizedFontFileURL = aOptimizedFontFileURL.Copy( aBaseURL.Len() );
230cdf0e10cSrcweir 	return aOptimizedFontFileURL;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir ImplDevFontAttributes ImplFontAttrCache::GetFontAttr( const String& rFontFileName ) const
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	ImplDevFontAttributes aDFA;
236cdf0e10cSrcweir 	FontAttrMap::const_iterator it = aFontAttributes.find( OptimizeURL( rFontFileName ) );
237cdf0e10cSrcweir 	if( it != aFontAttributes.end() )
238cdf0e10cSrcweir 	{
239cdf0e10cSrcweir 		aDFA = it->second;
240cdf0e10cSrcweir 	}
241cdf0e10cSrcweir 	return aDFA;
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir void ImplFontAttrCache::AddFontAttr( const String& rFontFileName, const ImplDevFontAttributes& rDFA )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	DBG_ASSERT( rFontFileName.Len() && rDFA.maName.Len(), "ImplFontNameCache::AddFontName - invalid data!" );
247cdf0e10cSrcweir 	if ( rFontFileName.Len() && rDFA.maName.Len() )
248cdf0e10cSrcweir 	{
249cdf0e10cSrcweir 		aFontAttributes.insert( FontAttrMap::value_type( OptimizeURL( rFontFileName ), rDFA ) );
250cdf0e10cSrcweir 		bModified = TRUE;
251cdf0e10cSrcweir 	}
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir // =======================================================================
255cdf0e10cSrcweir 
256cdf0e10cSrcweir // raw font data with a scoped lifetime
257cdf0e10cSrcweir class RawFontData
258cdf0e10cSrcweir {
259cdf0e10cSrcweir public:
260cdf0e10cSrcweir     explicit	RawFontData( HDC, DWORD nTableTag=0 );
261cdf0e10cSrcweir     			~RawFontData() { delete[] mpRawBytes; }
262cdf0e10cSrcweir     const unsigned char*	get() const { return mpRawBytes; }
263cdf0e10cSrcweir     const unsigned char*	steal() { unsigned char* p = mpRawBytes; mpRawBytes = NULL; return p; }
264cdf0e10cSrcweir     const int				size() const { return mnByteCount; }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir private:
267cdf0e10cSrcweir     unsigned char*	mpRawBytes;
268cdf0e10cSrcweir     int				mnByteCount;
269cdf0e10cSrcweir };
270cdf0e10cSrcweir 
271cdf0e10cSrcweir RawFontData::RawFontData( HDC hDC, DWORD nTableTag )
272cdf0e10cSrcweir :	mpRawBytes( NULL )
273cdf0e10cSrcweir ,	mnByteCount( 0 )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir 	// get required size in bytes
276cdf0e10cSrcweir     mnByteCount = ::GetFontData( hDC, nTableTag, 0, NULL, 0 );
277cdf0e10cSrcweir     if( mnByteCount == GDI_ERROR )
278cdf0e10cSrcweir         return;
279cdf0e10cSrcweir     else if( !mnByteCount )
280cdf0e10cSrcweir         return;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 	// allocate the array
283cdf0e10cSrcweir     mpRawBytes = new unsigned char[ mnByteCount ];
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 	// get raw data in chunks small enough for GetFontData()
286cdf0e10cSrcweir 	int nRawDataOfs = 0;
287cdf0e10cSrcweir 	DWORD nMaxChunkSize = 0x100000;
288cdf0e10cSrcweir 	for(;;)
289cdf0e10cSrcweir 	{
290cdf0e10cSrcweir 		// calculate remaining raw data to get
291cdf0e10cSrcweir 		DWORD nFDGet = mnByteCount - nRawDataOfs;
292cdf0e10cSrcweir 		if( nFDGet <= 0 )
293cdf0e10cSrcweir 			break;
294cdf0e10cSrcweir 		// #i56745# limit GetFontData requests
295cdf0e10cSrcweir 		if( nFDGet > nMaxChunkSize )
296cdf0e10cSrcweir 			nFDGet = nMaxChunkSize;
297cdf0e10cSrcweir 		const DWORD nFDGot = ::GetFontData( hDC, nTableTag, nRawDataOfs,
298cdf0e10cSrcweir 			(void*)(mpRawBytes + nRawDataOfs), nFDGet );
299cdf0e10cSrcweir 		if( !nFDGot )
300cdf0e10cSrcweir 			break;
301cdf0e10cSrcweir 		else if( nFDGot != GDI_ERROR )
302cdf0e10cSrcweir 			nRawDataOfs += nFDGot;
303cdf0e10cSrcweir 		else
304cdf0e10cSrcweir 		{
305cdf0e10cSrcweir 			// was the chunk too big? reduce it
306cdf0e10cSrcweir 			nMaxChunkSize /= 2;
307cdf0e10cSrcweir 			if( nMaxChunkSize < 0x10000 )
308cdf0e10cSrcweir 				break;
309cdf0e10cSrcweir 		}
310cdf0e10cSrcweir 	}
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	// cleanup if the raw data is incomplete
313cdf0e10cSrcweir 	if( nRawDataOfs != mnByteCount )
314cdf0e10cSrcweir 	{
315cdf0e10cSrcweir 		delete[] mpRawBytes;
316cdf0e10cSrcweir 		mpRawBytes = NULL;
317cdf0e10cSrcweir 	}
318cdf0e10cSrcweir }
319cdf0e10cSrcweir 
320cdf0e10cSrcweir // ===========================================================================
321cdf0e10cSrcweir // platform specific font substitution hooks for glyph fallback enhancement
322cdf0e10cSrcweir // TODO: move into i18n module (maybe merge with svx/ucsubset.*
323cdf0e10cSrcweir //       or merge with i18nutil/source/utility/unicode_data.h)
324cdf0e10cSrcweir struct Unicode2LangType
325cdf0e10cSrcweir {
326cdf0e10cSrcweir 	sal_UCS4 mnMinCode;
327cdf0e10cSrcweir 	sal_UCS4 mnMaxCode;
328cdf0e10cSrcweir 	LanguageType mnLangID;
329cdf0e10cSrcweir };
330cdf0e10cSrcweir 
331cdf0e10cSrcweir // entries marked with default-CJK get replaced with the default-CJK language
332cdf0e10cSrcweir #define LANGUAGE_DEFAULT_CJK 0xFFF0
333cdf0e10cSrcweir 
334cdf0e10cSrcweir // map unicode ranges to languages supported by OOo
335cdf0e10cSrcweir // NOTE: due to the binary search used this list must be sorted by mnMinCode
336cdf0e10cSrcweir static Unicode2LangType aLangFromCodeChart[]= {
337cdf0e10cSrcweir 	{0x0000, 0x007F, LANGUAGE_ENGLISH},				// Basic Latin
338cdf0e10cSrcweir 	{0x0080, 0x024F, LANGUAGE_ENGLISH},				// Latin Extended-A and Latin Extended-B
339cdf0e10cSrcweir 	{0x0250, 0x02AF, LANGUAGE_SYSTEM},				// IPA Extensions
340cdf0e10cSrcweir 	{0x0370, 0x03FF, LANGUAGE_GREEK},				// Greek
341cdf0e10cSrcweir 	{0x0590, 0x05FF, LANGUAGE_HEBREW},				// Hebrew
342cdf0e10cSrcweir 	{0x0600, 0x06FF, LANGUAGE_ARABIC_PRIMARY_ONLY},	// Arabic
343cdf0e10cSrcweir 	{0x0900, 0x097F, LANGUAGE_HINDI},				// Devanagari
344cdf0e10cSrcweir 	{0x0980, 0x09FF, LANGUAGE_BENGALI},				// Bengali
345cdf0e10cSrcweir 	{0x0A80, 0x0AFF, LANGUAGE_GUJARATI},			// Gujarati
346cdf0e10cSrcweir 	{0x0B00, 0x0B7F, LANGUAGE_ORIYA},				// Oriya
347cdf0e10cSrcweir 	{0x0B80, 0x0BFF, LANGUAGE_TAMIL},				// Tamil
348cdf0e10cSrcweir 	{0x0C00, 0x0C7F, LANGUAGE_TELUGU},				// Telugu
349cdf0e10cSrcweir 	{0x0C80, 0x0CFF, LANGUAGE_KANNADA},				// Kannada
350cdf0e10cSrcweir 	{0x0D00, 0x0D7F, LANGUAGE_MALAYALAM},			// Malayalam
351cdf0e10cSrcweir 	{0x0D80, 0x0D7F, LANGUAGE_SINHALESE_SRI_LANKA},	// Sinhala
352cdf0e10cSrcweir 	{0x0E00, 0x0E7F, LANGUAGE_THAI},				// Thai
353cdf0e10cSrcweir 	{0x0E80, 0x0EFF, LANGUAGE_LAO},					// Lao
354cdf0e10cSrcweir 	{0x0F00, 0x0FFF, LANGUAGE_TIBETAN},				// Tibetan
355cdf0e10cSrcweir 	{0x1000, 0x109F, LANGUAGE_BURMESE},				// Burmese
356cdf0e10cSrcweir 	{0x10A0, 0x10FF, LANGUAGE_GEORGIAN},			// Georgian
357cdf0e10cSrcweir 	{0x1100, 0x11FF, LANGUAGE_KOREAN},				// Hangul Jamo, Korean-specific
358cdf0e10cSrcweir //	{0x1200, 0x139F, LANGUAGE_AMHARIC_ETHIOPIA},	// Ethiopic
359cdf0e10cSrcweir //	{0x1200, 0x139F, LANGUAGE_TIGRIGNA_ETHIOPIA},	// Ethiopic
360cdf0e10cSrcweir 	{0x13A0, 0x13FF, LANGUAGE_CHEROKEE_UNITED_STATES}, // Cherokee
361cdf0e10cSrcweir //	{0x1400, 0x167F, LANGUAGE_CANADIAN_ABORIGINAL},	// Canadian Aboriginial Syllabics
362cdf0e10cSrcweir //	{0x1680, 0x169F, LANGUAGE_OGHAM},				// Ogham
363cdf0e10cSrcweir //	{0x16A0, 0x16F0, LANGUAGE_RUNIC},				// Runic
364cdf0e10cSrcweir //	{0x1700, 0x171F, LANGUAGE_TAGALOG},				// Tagalog
365cdf0e10cSrcweir //	{0x1720, 0x173F, LANGUAGE_HANUNOO},				// Hanunoo
366cdf0e10cSrcweir //	{0x1740, 0x175F, LANGUAGE_BUHID},				// Buhid
367cdf0e10cSrcweir //	{0x1760, 0x177F, LANGUAGE_TAGBANWA},			// Tagbanwa
368cdf0e10cSrcweir 	{0x1780, 0x17FF, LANGUAGE_KHMER},				// Khmer
369cdf0e10cSrcweir 	{0x18A0, 0x18AF, LANGUAGE_MONGOLIAN},			// Mongolian
370cdf0e10cSrcweir //	{0x1900, 0x194F, LANGUAGE_LIMBU},				// Limbu
371cdf0e10cSrcweir //	{0x1950, 0x197F, LANGUAGE_TAILE},				// Tai Le
372cdf0e10cSrcweir //	{0x1980, 0x19DF, LANGUAGE_TAILUE},				// Tai Lue
373cdf0e10cSrcweir 	{0x19E0, 0x19FF, LANGUAGE_KHMER},				// Khmer Symbols
374cdf0e10cSrcweir //	{0x1A00, 0x1A1F, LANGUAGE_BUGINESE},			// Buginese/Lontara
375cdf0e10cSrcweir //	{0x1B00, 0x1B7F, LANGUAGE_BALINESE},			// Balinese
376cdf0e10cSrcweir //	{0x1D00, 0x1DFF, LANGUAGE_NONE},				// Phonetic Symbols
377cdf0e10cSrcweir 	{0x1E00, 0x1EFF, LANGUAGE_ENGLISH},				// Latin Extended Additional
378cdf0e10cSrcweir 	{0x1F00, 0x1FFF, LANGUAGE_GREEK},				// Greek Extended
379cdf0e10cSrcweir 	{0x2C60, 0x2C7F, LANGUAGE_ENGLISH},				// Latin Extended-C
380cdf0e10cSrcweir 	{0x2E80, 0x2FFf, LANGUAGE_CHINESE_SIMPLIFIED},	// CJK Radicals Supplement + Kangxi Radical + Ideographic Description Characters
381cdf0e10cSrcweir 	{0x3000, 0x303F, LANGUAGE_DEFAULT_CJK},			// CJK Symbols and punctuation
382cdf0e10cSrcweir 	{0x3040, 0x30FF, LANGUAGE_JAPANESE},			// Japanese Hiragana + Katakana
383cdf0e10cSrcweir 	{0x3100, 0x312F, LANGUAGE_CHINESE_TRADITIONAL},	// Bopomofo
384cdf0e10cSrcweir 	{0x3130, 0x318F, LANGUAGE_KOREAN},				// Hangul Compatibility Jamo, Kocrean-specific
385cdf0e10cSrcweir 	{0x3190, 0x319F, LANGUAGE_JAPANESE},			// Kanbun
386cdf0e10cSrcweir 	{0x31A0, 0x31BF, LANGUAGE_CHINESE_TRADITIONAL},	// Bopomofo Extended
387cdf0e10cSrcweir 	{0x31C0, 0x31EF, LANGUAGE_DEFAULT_CJK},			// CJK Ideographs
388cdf0e10cSrcweir 	{0x31F0, 0x31FF, LANGUAGE_JAPANESE},			// Japanese Katakana Phonetic Extensions
389cdf0e10cSrcweir 	{0x3200, 0x321F, LANGUAGE_KOREAN},				// Parenthesized Hangul
390cdf0e10cSrcweir 	{0x3220, 0x325F, LANGUAGE_DEFAULT_CJK},			// Parenthesized Ideographs
391cdf0e10cSrcweir 	{0x3260, 0x327F, LANGUAGE_KOREAN},				// Circled Hangul
392cdf0e10cSrcweir 	{0x3280, 0x32CF, LANGUAGE_DEFAULT_CJK},			// Circled Ideographs
393cdf0e10cSrcweir 	{0x32d0, 0x32FF, LANGUAGE_JAPANESE},			// Japanese Circled Katakana
394cdf0e10cSrcweir 	{0x3400, 0x4DBF, LANGUAGE_DEFAULT_CJK},			// CJK Unified Ideographs Extension A
395cdf0e10cSrcweir 	{0x4E00, 0x9FCF, LANGUAGE_DEFAULT_CJK},			// Unified CJK Ideographs
396cdf0e10cSrcweir 	{0xA720, 0xA7FF, LANGUAGE_ENGLISH},				// Latin Extended-D
397cdf0e10cSrcweir 	{0xAC00, 0xD7AF, LANGUAGE_KOREAN},				// Hangul Syllables, Korean-specific
398cdf0e10cSrcweir 	{0xF900, 0xFAFF, LANGUAGE_DEFAULT_CJK},			// CJK Compatibility Ideographs
399cdf0e10cSrcweir 	{0xFB00, 0xFB4F, LANGUAGE_HEBREW},				// Hebrew Presentation Forms
400cdf0e10cSrcweir 	{0xFB50, 0xFDFF, LANGUAGE_ARABIC_PRIMARY_ONLY},	// Arabic Presentation Forms-A
401cdf0e10cSrcweir 	{0xFE70, 0xFEFE, LANGUAGE_ARABIC_PRIMARY_ONLY},	// Arabic Presentation Forms-B
402cdf0e10cSrcweir 	{0xFF65, 0xFF9F, LANGUAGE_JAPANESE},			// Japanese Halfwidth Katakana variant
403cdf0e10cSrcweir 	{0xFFA0, 0xFFDC, LANGUAGE_KOREAN},				// Kocrean halfwidth hangual variant
404cdf0e10cSrcweir 	{0x10140, 0x1018F, LANGUAGE_GREEK},				// Ancient Greak numbers
405cdf0e10cSrcweir 	{0x1D200, 0x1D24F, LANGUAGE_GREEK},				// Ancient Greek Musical
406cdf0e10cSrcweir 	{0x20000, 0x2A6DF, LANGUAGE_DEFAULT_CJK},		// CJK Unified Ideographs Extension B
407cdf0e10cSrcweir 	{0x2F800, 0x2FA1F, LANGUAGE_DEFAULT_CJK}		// CJK Compatibility Ideographs Supplement
408cdf0e10cSrcweir };
409cdf0e10cSrcweir 
410cdf0e10cSrcweir // get language matching to the missing char
411cdf0e10cSrcweir LanguageType MapCharToLanguage( sal_UCS4 uChar )
412cdf0e10cSrcweir {
413cdf0e10cSrcweir 	// entries marked with default-CJK get replaced with the prefered CJK language
414cdf0e10cSrcweir 	static bool bFirst = true;
415cdf0e10cSrcweir 	if( bFirst )
416cdf0e10cSrcweir 	{
417cdf0e10cSrcweir 		bFirst = false;
418cdf0e10cSrcweir 
419cdf0e10cSrcweir 		// use method suggested in #i97086# to determnine the systems default language
420cdf0e10cSrcweir 		// TODO: move into i18npool or sal/osl/w32/nlsupport.c
421cdf0e10cSrcweir 		LanguageType nDefaultLang = 0;
422cdf0e10cSrcweir 		HKEY hKey = NULL;
423cdf0e10cSrcweir 		LONG lResult = ::RegOpenKeyExA( HKEY_LOCAL_MACHINE,
424cdf0e10cSrcweir 			"SYSTEM\\CurrentControlSet\\Control\\Nls\\Language",
425cdf0e10cSrcweir 			0, KEY_QUERY_VALUE, &hKey );
426cdf0e10cSrcweir 		char aKeyValBuf[16];
427cdf0e10cSrcweir 		DWORD nKeyValSize = sizeof(aKeyValBuf);
428cdf0e10cSrcweir 		if( ERROR_SUCCESS == lResult )
429cdf0e10cSrcweir 			lResult = RegQueryValueExA( hKey, "Default", NULL, NULL, (LPBYTE)aKeyValBuf, &nKeyValSize );
430cdf0e10cSrcweir 		aKeyValBuf[ sizeof(aKeyValBuf)-1 ] = '\0';
431cdf0e10cSrcweir 		if( ERROR_SUCCESS == lResult )
432cdf0e10cSrcweir 			nDefaultLang = (LanguageType)rtl_str_toInt32( aKeyValBuf, 16 );
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 		// TODO: use the default-CJK language selected in
435cdf0e10cSrcweir 		//	Tools->Options->LangSettings->Languages when it becomes available here
436cdf0e10cSrcweir 		if( !nDefaultLang )
437cdf0e10cSrcweir 			nDefaultLang = Application::GetSettings().GetUILanguage();
438cdf0e10cSrcweir 
439cdf0e10cSrcweir 		LanguageType nDefaultCJK = LANGUAGE_CHINESE;
440cdf0e10cSrcweir 		switch( nDefaultLang )
441cdf0e10cSrcweir 		{
442cdf0e10cSrcweir 			case LANGUAGE_JAPANESE:
443cdf0e10cSrcweir 			case LANGUAGE_KOREAN:
444cdf0e10cSrcweir 			case LANGUAGE_KOREAN_JOHAB:
445cdf0e10cSrcweir 			case LANGUAGE_CHINESE_SIMPLIFIED:
446cdf0e10cSrcweir 			case LANGUAGE_CHINESE_TRADITIONAL:
447cdf0e10cSrcweir 			case LANGUAGE_CHINESE_SINGAPORE:
448cdf0e10cSrcweir 			case LANGUAGE_CHINESE_HONGKONG:
449cdf0e10cSrcweir 			case LANGUAGE_CHINESE_MACAU:
450cdf0e10cSrcweir 				nDefaultCJK = nDefaultLang;
451cdf0e10cSrcweir 				break;
452cdf0e10cSrcweir 			default:
453cdf0e10cSrcweir 				nDefaultCJK = LANGUAGE_CHINESE;
454cdf0e10cSrcweir 				break;
455cdf0e10cSrcweir 		}
456cdf0e10cSrcweir 
457cdf0e10cSrcweir 		// change the marked entries to prefered language
458cdf0e10cSrcweir 		static const int nCount = (sizeof(aLangFromCodeChart) / sizeof(*aLangFromCodeChart));
459cdf0e10cSrcweir 		for( int i = 0; i < nCount; ++i )
460cdf0e10cSrcweir 		{
461cdf0e10cSrcweir 			if( aLangFromCodeChart[ i].mnLangID == LANGUAGE_DEFAULT_CJK )
462cdf0e10cSrcweir 				aLangFromCodeChart[ i].mnLangID = nDefaultCJK;
463cdf0e10cSrcweir 		}
464cdf0e10cSrcweir 	}
465cdf0e10cSrcweir 
466cdf0e10cSrcweir 	// binary search
467cdf0e10cSrcweir 	int nLow = 0;
468cdf0e10cSrcweir 	int nHigh = (sizeof(aLangFromCodeChart) / sizeof(*aLangFromCodeChart)) - 1;
469cdf0e10cSrcweir 	while( nLow <= nHigh )
470cdf0e10cSrcweir 	{
471cdf0e10cSrcweir 		int nMiddle = (nHigh + nLow) / 2;
472cdf0e10cSrcweir 		if( uChar < aLangFromCodeChart[ nMiddle].mnMinCode )
473cdf0e10cSrcweir 			nHigh = nMiddle - 1;
474cdf0e10cSrcweir 		else if( uChar > aLangFromCodeChart[ nMiddle].mnMaxCode )
475cdf0e10cSrcweir 			nLow = nMiddle + 1;
476cdf0e10cSrcweir 		else
477cdf0e10cSrcweir 			return aLangFromCodeChart[ nMiddle].mnLangID;
478cdf0e10cSrcweir 	}
479cdf0e10cSrcweir 
480cdf0e10cSrcweir 	return LANGUAGE_DONTKNOW;
481cdf0e10cSrcweir }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir class WinGlyphFallbackSubstititution
484cdf0e10cSrcweir :    public ImplGlyphFallbackFontSubstitution
485cdf0e10cSrcweir {
486cdf0e10cSrcweir public:
487cdf0e10cSrcweir 	explicit	WinGlyphFallbackSubstititution( HDC );
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 	bool FindFontSubstitute( ImplFontSelectData&, rtl::OUString& rMissingChars ) const;
490cdf0e10cSrcweir private:
491cdf0e10cSrcweir 	HDC mhDC;
492cdf0e10cSrcweir 	bool HasMissingChars( const ImplFontData*, const rtl::OUString& rMissingChars ) const;
493cdf0e10cSrcweir };
494cdf0e10cSrcweir 
495cdf0e10cSrcweir inline WinGlyphFallbackSubstititution::WinGlyphFallbackSubstititution( HDC hDC )
496cdf0e10cSrcweir :	mhDC( hDC )
497cdf0e10cSrcweir {}
498cdf0e10cSrcweir 
499cdf0e10cSrcweir void ImplGetLogFontFromFontSelect( HDC, const ImplFontSelectData*,
500cdf0e10cSrcweir 	LOGFONTW&, bool /*bTestVerticalAvail*/ );
501cdf0e10cSrcweir 
502cdf0e10cSrcweir // does a font face hold the given missing characters?
503cdf0e10cSrcweir bool WinGlyphFallbackSubstititution::HasMissingChars( const ImplFontData* pFace, const rtl::OUString& rMissingChars ) const
504cdf0e10cSrcweir {
505cdf0e10cSrcweir 	const ImplWinFontData* pWinFont = static_cast<const ImplWinFontData*>(pFace);
506cdf0e10cSrcweir 	const ImplFontCharMap* pCharMap = pWinFont->GetImplFontCharMap();
507cdf0e10cSrcweir 	if( !pCharMap )
508cdf0e10cSrcweir 	{
509cdf0e10cSrcweir 		// construct a Size structure as the parameter of constructor of class ImplFontSelectData
510cdf0e10cSrcweir 		const Size aSize( pFace->GetWidth(), pFace->GetHeight() );
511cdf0e10cSrcweir 		// create a ImplFontSelectData object for getting s LOGFONT
512cdf0e10cSrcweir 		const ImplFontSelectData aFSD( *pFace, aSize, (float)aSize.Height(), 0, false );
513cdf0e10cSrcweir 		// construct log font
514cdf0e10cSrcweir 		LOGFONTW aLogFont;
515cdf0e10cSrcweir 		ImplGetLogFontFromFontSelect( mhDC, &aFSD, aLogFont, true );
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 		// create HFONT from log font
518cdf0e10cSrcweir 		HFONT hNewFont = ::CreateFontIndirectW( &aLogFont );
519cdf0e10cSrcweir 		// select the new font into device
520cdf0e10cSrcweir 		HFONT hOldFont = ::SelectFont( mhDC, hNewFont );
521cdf0e10cSrcweir 
522cdf0e10cSrcweir 		// read CMAP table to update their pCharMap
523cdf0e10cSrcweir 		pWinFont->UpdateFromHDC( mhDC );;
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 		// cleanup temporary font
526cdf0e10cSrcweir 		::SelectFont( mhDC, hOldFont );
527cdf0e10cSrcweir 		::DeleteFont( hNewFont );
528cdf0e10cSrcweir 
529cdf0e10cSrcweir 		// get the new charmap
530cdf0e10cSrcweir 		pCharMap = pWinFont->GetImplFontCharMap();
531cdf0e10cSrcweir 	}
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 	// avoid fonts with unknown CMAP subtables for glyph fallback
534cdf0e10cSrcweir 	if( !pCharMap || pCharMap->IsDefaultMap() )
535cdf0e10cSrcweir 		return false;
536cdf0e10cSrcweir         pCharMap->AddReference();
537cdf0e10cSrcweir 
538cdf0e10cSrcweir 	int nMatchCount = 0;
539cdf0e10cSrcweir 	// static const int nMaxMatchCount = 1; // TODO: tolerate more missing characters?
540cdf0e10cSrcweir 	const sal_Int32 nStrLen = rMissingChars.getLength();
541cdf0e10cSrcweir 	for( sal_Int32 nStrIdx = 0; nStrIdx < nStrLen; ++nStrIdx )
542cdf0e10cSrcweir 	{
543cdf0e10cSrcweir 		const sal_UCS4 uChar = rMissingChars.iterateCodePoints( &nStrIdx );
544cdf0e10cSrcweir 		nMatchCount += pCharMap->HasChar( uChar );
545cdf0e10cSrcweir 		break; // for now
546cdf0e10cSrcweir 	}
547cdf0e10cSrcweir         pCharMap->DeReference();
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 	const bool bHasMatches = (nMatchCount > 0);
550cdf0e10cSrcweir 	return bHasMatches;
551cdf0e10cSrcweir }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir // find a fallback font for missing characters
554cdf0e10cSrcweir // TODO: should stylistic matches be searched and prefered?
555cdf0e10cSrcweir bool WinGlyphFallbackSubstititution::FindFontSubstitute( ImplFontSelectData& rFontSelData, rtl::OUString& rMissingChars ) const
556cdf0e10cSrcweir {
557cdf0e10cSrcweir 	// guess a locale matching to the missing chars
558cdf0e10cSrcweir 	com::sun::star::lang::Locale aLocale;
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 	sal_Int32 nStrIdx = 0;
561cdf0e10cSrcweir 	const sal_Int32 nStrLen = rMissingChars.getLength();
562cdf0e10cSrcweir 	while( nStrIdx < nStrLen )
563cdf0e10cSrcweir 	{
564cdf0e10cSrcweir 		const sal_UCS4 uChar = rMissingChars.iterateCodePoints( &nStrIdx );
565cdf0e10cSrcweir 		const LanguageType eLang = MapCharToLanguage( uChar );
566cdf0e10cSrcweir 		if( eLang == LANGUAGE_DONTKNOW )
567cdf0e10cSrcweir 			continue;
568cdf0e10cSrcweir 		MsLangId::convertLanguageToLocale( eLang, aLocale );
569cdf0e10cSrcweir 		break;
570cdf0e10cSrcweir 	}
571cdf0e10cSrcweir 
572cdf0e10cSrcweir 	// fall back to default UI locale if the missing characters are inconclusive
573cdf0e10cSrcweir 	if( nStrIdx >= nStrLen )
574cdf0e10cSrcweir 		aLocale = Application::GetSettings().GetUILocale();
575cdf0e10cSrcweir 
576cdf0e10cSrcweir 	// first level fallback:
577cdf0e10cSrcweir 	// try use the locale specific default fonts defined in VCL.xcu
578cdf0e10cSrcweir 	const ImplDevFontList* pDevFontList = ImplGetSVData()->maGDIData.mpScreenFontList;
579cdf0e10cSrcweir 	/*const*/ ImplDevFontListData* pDevFont = pDevFontList->ImplFindByLocale( aLocale );
580cdf0e10cSrcweir 	if( pDevFont )
581cdf0e10cSrcweir 	{
582cdf0e10cSrcweir 		const ImplFontData* pFace = pDevFont->FindBestFontFace( rFontSelData );
583cdf0e10cSrcweir 		if( HasMissingChars( pFace, rMissingChars ) )
584cdf0e10cSrcweir 		{
585cdf0e10cSrcweir 			rFontSelData.maSearchName = pDevFont->GetSearchName();
586cdf0e10cSrcweir 			return true;
587cdf0e10cSrcweir 		}
588cdf0e10cSrcweir 	}
589cdf0e10cSrcweir 
590cdf0e10cSrcweir 	// are the missing characters symbols?
591cdf0e10cSrcweir 	pDevFont = pDevFontList->ImplFindByAttributes( IMPL_FONT_ATTR_SYMBOL,
592cdf0e10cSrcweir 					rFontSelData.meWeight, rFontSelData.meWidthType,
593cdf0e10cSrcweir 					rFontSelData.meFamily, rFontSelData.meItalic, rFontSelData.maSearchName );
594cdf0e10cSrcweir 	if( pDevFont )
595cdf0e10cSrcweir 	{
596cdf0e10cSrcweir 		const ImplFontData* pFace = pDevFont->FindBestFontFace( rFontSelData );
597cdf0e10cSrcweir 		if( HasMissingChars( pFace, rMissingChars ) )
598cdf0e10cSrcweir 		{
599cdf0e10cSrcweir 			rFontSelData.maSearchName = pDevFont->GetSearchName();
600cdf0e10cSrcweir 			return true;
601cdf0e10cSrcweir 		}
602cdf0e10cSrcweir 	}
603cdf0e10cSrcweir 
604cdf0e10cSrcweir 	// last level fallback, check each font type face one by one
605cdf0e10cSrcweir 	const ImplGetDevFontList* pTestFontList = pDevFontList->GetDevFontList();
606cdf0e10cSrcweir 	// limit the count of fonts to be checked to prevent hangs
607cdf0e10cSrcweir 	static const int MAX_GFBFONT_COUNT = 600;
608cdf0e10cSrcweir 	int nTestFontCount = pTestFontList->Count();
609cdf0e10cSrcweir 	if( nTestFontCount > MAX_GFBFONT_COUNT )
610cdf0e10cSrcweir 		nTestFontCount = MAX_GFBFONT_COUNT;
611cdf0e10cSrcweir 
612cdf0e10cSrcweir 	for( int i = 0; i < nTestFontCount; ++i )
613cdf0e10cSrcweir 	{
614cdf0e10cSrcweir 		const ImplFontData* pFace = pTestFontList->Get( i );
615cdf0e10cSrcweir 		if( !HasMissingChars( pFace, rMissingChars ) )
616cdf0e10cSrcweir 			continue;
617cdf0e10cSrcweir 		rFontSelData.maSearchName = pFace->maName;
618cdf0e10cSrcweir 		return true;
619cdf0e10cSrcweir 	}
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 	return false;
622cdf0e10cSrcweir }
623cdf0e10cSrcweir 
624cdf0e10cSrcweir // =======================================================================
625cdf0e10cSrcweir 
626cdf0e10cSrcweir struct ImplEnumInfo
627cdf0e10cSrcweir {
628cdf0e10cSrcweir     HDC                 mhDC;
629cdf0e10cSrcweir     ImplDevFontList*    mpList;
630cdf0e10cSrcweir     String*             mpName;
631cdf0e10cSrcweir     LOGFONTA*           mpLogFontA;
632cdf0e10cSrcweir     LOGFONTW*           mpLogFontW;
633cdf0e10cSrcweir     UINT                mnPreferedCharSet;
634cdf0e10cSrcweir     bool                mbCourier;
635cdf0e10cSrcweir     bool                mbImplSalCourierScalable;
636cdf0e10cSrcweir     bool                mbImplSalCourierNew;
637cdf0e10cSrcweir     bool                mbPrinter;
638cdf0e10cSrcweir     int                 mnFontCount;
639cdf0e10cSrcweir };
640cdf0e10cSrcweir 
641cdf0e10cSrcweir // =======================================================================
642cdf0e10cSrcweir 
643cdf0e10cSrcweir static CharSet ImplCharSetToSal( BYTE nCharSet )
644cdf0e10cSrcweir {
645cdf0e10cSrcweir     rtl_TextEncoding eTextEncoding;
646cdf0e10cSrcweir 
647cdf0e10cSrcweir     if ( nCharSet == OEM_CHARSET )
648cdf0e10cSrcweir     {
649cdf0e10cSrcweir         UINT nCP = (sal_uInt16)GetOEMCP();
650cdf0e10cSrcweir         switch ( nCP )
651cdf0e10cSrcweir         {
652cdf0e10cSrcweir             // It is unclear why these two (undefined?) code page numbers are
653cdf0e10cSrcweir             // handled specially here:
654cdf0e10cSrcweir             case 1004:  eTextEncoding = RTL_TEXTENCODING_MS_1252; break;
655cdf0e10cSrcweir             case 65400: eTextEncoding = RTL_TEXTENCODING_SYMBOL; break;
656cdf0e10cSrcweir             default:
657cdf0e10cSrcweir                 eTextEncoding = rtl_getTextEncodingFromWindowsCodePage(nCP);
658cdf0e10cSrcweir                 break;
659cdf0e10cSrcweir         };
660cdf0e10cSrcweir     }
661cdf0e10cSrcweir     else
662cdf0e10cSrcweir     {
663cdf0e10cSrcweir         if( nCharSet )
664cdf0e10cSrcweir             eTextEncoding = rtl_getTextEncodingFromWindowsCharset( nCharSet );
665cdf0e10cSrcweir         else
666cdf0e10cSrcweir             eTextEncoding = RTL_TEXTENCODING_UNICODE;
667cdf0e10cSrcweir     }
668cdf0e10cSrcweir 
669cdf0e10cSrcweir     return eTextEncoding;
670cdf0e10cSrcweir }
671cdf0e10cSrcweir 
672cdf0e10cSrcweir // -----------------------------------------------------------------------
673cdf0e10cSrcweir 
674cdf0e10cSrcweir static FontFamily ImplFamilyToSal( BYTE nFamily )
675cdf0e10cSrcweir {
676cdf0e10cSrcweir     switch ( nFamily & 0xF0 )
677cdf0e10cSrcweir     {
678cdf0e10cSrcweir         case FF_DECORATIVE:
679cdf0e10cSrcweir             return FAMILY_DECORATIVE;
680cdf0e10cSrcweir 
681cdf0e10cSrcweir         case FF_MODERN:
682cdf0e10cSrcweir             return FAMILY_MODERN;
683cdf0e10cSrcweir 
684cdf0e10cSrcweir         case FF_ROMAN:
685cdf0e10cSrcweir             return FAMILY_ROMAN;
686cdf0e10cSrcweir 
687cdf0e10cSrcweir         case FF_SCRIPT:
688cdf0e10cSrcweir             return FAMILY_SCRIPT;
689cdf0e10cSrcweir 
690cdf0e10cSrcweir         case FF_SWISS:
691cdf0e10cSrcweir             return FAMILY_SWISS;
692cdf0e10cSrcweir 
693cdf0e10cSrcweir         default:
694cdf0e10cSrcweir             break;
695cdf0e10cSrcweir     }
696cdf0e10cSrcweir 
697cdf0e10cSrcweir     return FAMILY_DONTKNOW;
698cdf0e10cSrcweir }
699cdf0e10cSrcweir 
700cdf0e10cSrcweir // -----------------------------------------------------------------------
701cdf0e10cSrcweir 
702cdf0e10cSrcweir static BYTE ImplFamilyToWin( FontFamily eFamily )
703cdf0e10cSrcweir {
704cdf0e10cSrcweir     switch ( eFamily )
705cdf0e10cSrcweir     {
706cdf0e10cSrcweir         case FAMILY_DECORATIVE:
707cdf0e10cSrcweir             return FF_DECORATIVE;
708cdf0e10cSrcweir 
709cdf0e10cSrcweir         case FAMILY_MODERN:
710cdf0e10cSrcweir             return FF_MODERN;
711cdf0e10cSrcweir 
712cdf0e10cSrcweir         case FAMILY_ROMAN:
713cdf0e10cSrcweir             return FF_ROMAN;
714cdf0e10cSrcweir 
715cdf0e10cSrcweir         case FAMILY_SCRIPT:
716cdf0e10cSrcweir             return FF_SCRIPT;
717cdf0e10cSrcweir 
718cdf0e10cSrcweir         case FAMILY_SWISS:
719cdf0e10cSrcweir             return FF_SWISS;
720cdf0e10cSrcweir 
721cdf0e10cSrcweir         case FAMILY_SYSTEM:
722cdf0e10cSrcweir             return FF_SWISS;
723cdf0e10cSrcweir 
724cdf0e10cSrcweir         default:
725cdf0e10cSrcweir             break;
726cdf0e10cSrcweir     }
727cdf0e10cSrcweir 
728cdf0e10cSrcweir     return FF_DONTCARE;
729cdf0e10cSrcweir }
730cdf0e10cSrcweir 
731cdf0e10cSrcweir // -----------------------------------------------------------------------
732cdf0e10cSrcweir 
733cdf0e10cSrcweir static FontWeight ImplWeightToSal( int nWeight )
734cdf0e10cSrcweir {
735cdf0e10cSrcweir     if ( nWeight <= FW_THIN )
736cdf0e10cSrcweir         return WEIGHT_THIN;
737cdf0e10cSrcweir     else if ( nWeight <= FW_ULTRALIGHT )
738cdf0e10cSrcweir         return WEIGHT_ULTRALIGHT;
739cdf0e10cSrcweir     else if ( nWeight <= FW_LIGHT )
740cdf0e10cSrcweir         return WEIGHT_LIGHT;
741cdf0e10cSrcweir     else if ( nWeight < FW_MEDIUM )
742cdf0e10cSrcweir         return WEIGHT_NORMAL;
743cdf0e10cSrcweir     else if ( nWeight == FW_MEDIUM )
744cdf0e10cSrcweir         return WEIGHT_MEDIUM;
745cdf0e10cSrcweir     else if ( nWeight <= FW_SEMIBOLD )
746cdf0e10cSrcweir         return WEIGHT_SEMIBOLD;
747cdf0e10cSrcweir     else if ( nWeight <= FW_BOLD )
748cdf0e10cSrcweir         return WEIGHT_BOLD;
749cdf0e10cSrcweir     else if ( nWeight <= FW_ULTRABOLD )
750cdf0e10cSrcweir         return WEIGHT_ULTRABOLD;
751cdf0e10cSrcweir     else
752cdf0e10cSrcweir         return WEIGHT_BLACK;
753cdf0e10cSrcweir }
754cdf0e10cSrcweir 
755cdf0e10cSrcweir // -----------------------------------------------------------------------
756cdf0e10cSrcweir 
757cdf0e10cSrcweir static int ImplWeightToWin( FontWeight eWeight )
758cdf0e10cSrcweir {
759cdf0e10cSrcweir     switch ( eWeight )
760cdf0e10cSrcweir     {
761cdf0e10cSrcweir         case WEIGHT_THIN:
762cdf0e10cSrcweir             return FW_THIN;
763cdf0e10cSrcweir 
764cdf0e10cSrcweir         case WEIGHT_ULTRALIGHT:
765cdf0e10cSrcweir             return FW_ULTRALIGHT;
766cdf0e10cSrcweir 
767cdf0e10cSrcweir         case WEIGHT_LIGHT:
768cdf0e10cSrcweir             return FW_LIGHT;
769cdf0e10cSrcweir 
770cdf0e10cSrcweir         case WEIGHT_SEMILIGHT:
771cdf0e10cSrcweir         case WEIGHT_NORMAL:
772cdf0e10cSrcweir             return FW_NORMAL;
773cdf0e10cSrcweir 
774cdf0e10cSrcweir         case WEIGHT_MEDIUM:
775cdf0e10cSrcweir             return FW_MEDIUM;
776cdf0e10cSrcweir 
777cdf0e10cSrcweir         case WEIGHT_SEMIBOLD:
778cdf0e10cSrcweir             return FW_SEMIBOLD;
779cdf0e10cSrcweir 
780cdf0e10cSrcweir         case WEIGHT_BOLD:
781cdf0e10cSrcweir             return FW_BOLD;
782cdf0e10cSrcweir 
783cdf0e10cSrcweir         case WEIGHT_ULTRABOLD:
784cdf0e10cSrcweir             return FW_ULTRABOLD;
785cdf0e10cSrcweir 
786cdf0e10cSrcweir         case WEIGHT_BLACK:
787cdf0e10cSrcweir             return FW_BLACK;
788cdf0e10cSrcweir 
789cdf0e10cSrcweir         default:
790cdf0e10cSrcweir             break;
791cdf0e10cSrcweir     }
792cdf0e10cSrcweir 
793cdf0e10cSrcweir     return 0;
794cdf0e10cSrcweir }
795cdf0e10cSrcweir 
796cdf0e10cSrcweir // -----------------------------------------------------------------------
797cdf0e10cSrcweir 
798cdf0e10cSrcweir inline FontPitch ImplLogPitchToSal( BYTE nPitch )
799cdf0e10cSrcweir {
800cdf0e10cSrcweir     if ( nPitch & FIXED_PITCH )
801cdf0e10cSrcweir         return PITCH_FIXED;
802cdf0e10cSrcweir     else
803cdf0e10cSrcweir         return PITCH_VARIABLE;
804cdf0e10cSrcweir }
805cdf0e10cSrcweir 
806cdf0e10cSrcweir // -----------------------------------------------------------------------
807cdf0e10cSrcweir 
808cdf0e10cSrcweir inline FontPitch ImplMetricPitchToSal( BYTE nPitch )
809cdf0e10cSrcweir {
810cdf0e10cSrcweir     // Sausaecke bei MS !! siehe NT Hilfe
811cdf0e10cSrcweir     if ( !(nPitch & TMPF_FIXED_PITCH) )
812cdf0e10cSrcweir         return PITCH_FIXED;
813cdf0e10cSrcweir     else
814cdf0e10cSrcweir         return PITCH_VARIABLE;
815cdf0e10cSrcweir }
816cdf0e10cSrcweir 
817cdf0e10cSrcweir // -----------------------------------------------------------------------
818cdf0e10cSrcweir 
819cdf0e10cSrcweir inline BYTE ImplPitchToWin( FontPitch ePitch )
820cdf0e10cSrcweir {
821cdf0e10cSrcweir     if ( ePitch == PITCH_FIXED )
822cdf0e10cSrcweir         return FIXED_PITCH;
823cdf0e10cSrcweir     else if ( ePitch == PITCH_VARIABLE )
824cdf0e10cSrcweir         return VARIABLE_PITCH;
825cdf0e10cSrcweir     else
826cdf0e10cSrcweir         return DEFAULT_PITCH;
827cdf0e10cSrcweir }
828cdf0e10cSrcweir 
829cdf0e10cSrcweir // -----------------------------------------------------------------------
830cdf0e10cSrcweir 
831cdf0e10cSrcweir static ImplDevFontAttributes WinFont2DevFontAttributes( const ENUMLOGFONTEXA& rEnumFont,
832cdf0e10cSrcweir     const NEWTEXTMETRICA& rMetric, DWORD nFontType )
833cdf0e10cSrcweir {
834cdf0e10cSrcweir     ImplDevFontAttributes aDFA;
835cdf0e10cSrcweir 
836cdf0e10cSrcweir     const LOGFONTA rLogFont = rEnumFont.elfLogFont;
837cdf0e10cSrcweir 
838cdf0e10cSrcweir     // get font face attributes
839cdf0e10cSrcweir     aDFA.meFamily       = ImplFamilyToSal( rLogFont.lfPitchAndFamily );
840cdf0e10cSrcweir     aDFA.meWidthType    = WIDTH_DONTKNOW;
841cdf0e10cSrcweir     aDFA.meWeight       = ImplWeightToSal( rLogFont.lfWeight );
842cdf0e10cSrcweir     aDFA.meItalic       = (rLogFont.lfItalic) ? ITALIC_NORMAL : ITALIC_NONE;
843cdf0e10cSrcweir     aDFA.mePitch        = ImplLogPitchToSal( rLogFont.lfPitchAndFamily );
844cdf0e10cSrcweir     aDFA.mbSymbolFlag   = (rLogFont.lfCharSet == SYMBOL_CHARSET);
845cdf0e10cSrcweir 
846cdf0e10cSrcweir     // get the font face name
847cdf0e10cSrcweir     aDFA.maName = ImplSalGetUniString( rLogFont.lfFaceName );
848cdf0e10cSrcweir 
849cdf0e10cSrcweir     // use the face's style name only if it looks reasonable
850cdf0e10cSrcweir     const char* pStyleName = (const char*)rEnumFont.elfStyle;
851cdf0e10cSrcweir     const char* pEnd = pStyleName + sizeof( rEnumFont.elfStyle );
852cdf0e10cSrcweir     const char* p = pStyleName;
853cdf0e10cSrcweir     for(; *p && (p < pEnd); ++p )
854cdf0e10cSrcweir         if( (0x00 < *p) && (*p < 0x20) )
855cdf0e10cSrcweir             break;
856cdf0e10cSrcweir     if( p < pEnd )
857cdf0e10cSrcweir         aDFA.maStyleName = ImplSalGetUniString( pStyleName );
858cdf0e10cSrcweir 
859cdf0e10cSrcweir     // get device specific font attributes
860cdf0e10cSrcweir     aDFA.mbOrientation  = (nFontType & RASTER_FONTTYPE) == 0;
861cdf0e10cSrcweir     aDFA.mbDevice       = (rMetric.tmPitchAndFamily & TMPF_DEVICE) != 0;
862cdf0e10cSrcweir 
863cdf0e10cSrcweir     aDFA.mbEmbeddable   = false;
864cdf0e10cSrcweir     aDFA.mbSubsettable  = false;
865cdf0e10cSrcweir     if( 0 != (rMetric.ntmFlags & (NTM_TT_OPENTYPE | NTM_PS_OPENTYPE))
866cdf0e10cSrcweir      || 0 != (rMetric.tmPitchAndFamily & TMPF_TRUETYPE))
867cdf0e10cSrcweir         aDFA.mbSubsettable = true;
868cdf0e10cSrcweir     else if( 0 != (rMetric.ntmFlags & NTM_TYPE1) ) // TODO: implement subsetting for type1 too
869cdf0e10cSrcweir         aDFA.mbEmbeddable = true;
870cdf0e10cSrcweir 
871cdf0e10cSrcweir     // heuristics for font quality
872cdf0e10cSrcweir     // -   standard-type1 > opentypeTT > truetype > non-standard-type1 > raster
873cdf0e10cSrcweir     // -   subsetting > embedding > none
874cdf0e10cSrcweir     aDFA.mnQuality = 0;
875cdf0e10cSrcweir     if( rMetric.tmPitchAndFamily & TMPF_TRUETYPE )
876cdf0e10cSrcweir         aDFA.mnQuality += 50;
877cdf0e10cSrcweir     if( 0 != (rMetric.ntmFlags & (NTM_TT_OPENTYPE | NTM_PS_OPENTYPE)) )
878cdf0e10cSrcweir         aDFA.mnQuality += 10;
879cdf0e10cSrcweir     if( aDFA.mbSubsettable )
880cdf0e10cSrcweir         aDFA.mnQuality += 200;
881cdf0e10cSrcweir     else if( aDFA.mbEmbeddable )
882cdf0e10cSrcweir         aDFA.mnQuality += 100;
883cdf0e10cSrcweir 
884cdf0e10cSrcweir     // #i38665# prefer Type1 versions of the standard postscript fonts
885cdf0e10cSrcweir     if( aDFA.mbEmbeddable )
886cdf0e10cSrcweir     {
887cdf0e10cSrcweir         if( aDFA.maName.EqualsAscii( "AvantGarde" )
888cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Bookman" )
889cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Courier" )
890cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Helvetica" )
891cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "NewCenturySchlbk" )
892cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Palatino" )
893cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Symbol" )
894cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Times" )
895cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "ZapfChancery" )
896cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "ZapfDingbats" ) )
897cdf0e10cSrcweir             aDFA.mnQuality += 500;
898cdf0e10cSrcweir     }
899cdf0e10cSrcweir 
900cdf0e10cSrcweir     // TODO: add alias names
901cdf0e10cSrcweir     return aDFA;
902cdf0e10cSrcweir }
903cdf0e10cSrcweir 
904cdf0e10cSrcweir // -----------------------------------------------------------------------
905cdf0e10cSrcweir 
906cdf0e10cSrcweir static ImplDevFontAttributes WinFont2DevFontAttributes( const ENUMLOGFONTEXW& rEnumFont,
907cdf0e10cSrcweir     const NEWTEXTMETRICW& rMetric, DWORD nFontType )
908cdf0e10cSrcweir {
909cdf0e10cSrcweir     ImplDevFontAttributes aDFA;
910cdf0e10cSrcweir 
911cdf0e10cSrcweir     const LOGFONTW rLogFont = rEnumFont.elfLogFont;
912cdf0e10cSrcweir 
913cdf0e10cSrcweir     // get font face attributes
914cdf0e10cSrcweir     aDFA.meFamily       = ImplFamilyToSal( rLogFont.lfPitchAndFamily );
915cdf0e10cSrcweir     aDFA.meWidthType    = WIDTH_DONTKNOW;
916cdf0e10cSrcweir     aDFA.meWeight       = ImplWeightToSal( rLogFont.lfWeight );
917cdf0e10cSrcweir     aDFA.meItalic       = (rLogFont.lfItalic) ? ITALIC_NORMAL : ITALIC_NONE;
918cdf0e10cSrcweir     aDFA.mePitch        = ImplLogPitchToSal( rLogFont.lfPitchAndFamily );
919cdf0e10cSrcweir     aDFA.mbSymbolFlag   = (rLogFont.lfCharSet == SYMBOL_CHARSET);
920cdf0e10cSrcweir 
921cdf0e10cSrcweir     // get the font face name
922cdf0e10cSrcweir     aDFA.maName = reinterpret_cast<const sal_Unicode*>(rLogFont.lfFaceName);
923cdf0e10cSrcweir 
924cdf0e10cSrcweir     // use the face's style name only if it looks reasonable
925cdf0e10cSrcweir     const wchar_t* pStyleName = rEnumFont.elfStyle;
926cdf0e10cSrcweir     const wchar_t* pEnd = pStyleName + sizeof(rEnumFont.elfStyle)/sizeof(*rEnumFont.elfStyle);
927cdf0e10cSrcweir     const wchar_t* p = pStyleName;
928cdf0e10cSrcweir     for(; *p && (p < pEnd); ++p )
929cdf0e10cSrcweir         if( *p < 0x0020 )
930cdf0e10cSrcweir             break;
931cdf0e10cSrcweir     if( p < pEnd )
932cdf0e10cSrcweir         aDFA.maStyleName = reinterpret_cast<const sal_Unicode*>(pStyleName);
933cdf0e10cSrcweir 
934cdf0e10cSrcweir     // get device specific font attributes
935cdf0e10cSrcweir     aDFA.mbOrientation  = (nFontType & RASTER_FONTTYPE) == 0;
936cdf0e10cSrcweir     aDFA.mbDevice       = (rMetric.tmPitchAndFamily & TMPF_DEVICE) != 0;
937cdf0e10cSrcweir 
938cdf0e10cSrcweir     aDFA.mbEmbeddable   = false;
939cdf0e10cSrcweir     aDFA.mbSubsettable  = false;
940cdf0e10cSrcweir     if( 0 != (rMetric.ntmFlags & (NTM_TT_OPENTYPE | NTM_PS_OPENTYPE))
941cdf0e10cSrcweir      || 0 != (rMetric.tmPitchAndFamily & TMPF_TRUETYPE))
942cdf0e10cSrcweir         aDFA.mbSubsettable = true;
943cdf0e10cSrcweir     else if( 0 != (rMetric.ntmFlags & NTM_TYPE1) ) // TODO: implement subsetting for type1 too
944cdf0e10cSrcweir         aDFA.mbEmbeddable = true;
945cdf0e10cSrcweir 
946cdf0e10cSrcweir     // heuristics for font quality
947cdf0e10cSrcweir     // -   standard-type1 > opentypeTT > truetype > non-standard-type1 > raster
948cdf0e10cSrcweir     // -   subsetting > embedding > none
949cdf0e10cSrcweir     aDFA.mnQuality = 0;
950cdf0e10cSrcweir     if( rMetric.tmPitchAndFamily & TMPF_TRUETYPE )
951cdf0e10cSrcweir         aDFA.mnQuality += 50;
952cdf0e10cSrcweir     if( 0 != (rMetric.ntmFlags & (NTM_TT_OPENTYPE | NTM_PS_OPENTYPE)) )
953cdf0e10cSrcweir         aDFA.mnQuality += 10;
954cdf0e10cSrcweir     if( aDFA.mbSubsettable )
955cdf0e10cSrcweir         aDFA.mnQuality += 200;
956cdf0e10cSrcweir     else if( aDFA.mbEmbeddable )
957cdf0e10cSrcweir         aDFA.mnQuality += 100;
958cdf0e10cSrcweir 
959cdf0e10cSrcweir     // #i38665# prefer Type1 versions of the standard postscript fonts
960cdf0e10cSrcweir     if( aDFA.mbEmbeddable )
961cdf0e10cSrcweir     {
962cdf0e10cSrcweir         if( aDFA.maName.EqualsAscii( "AvantGarde" )
963cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Bookman" )
964cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Courier" )
965cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Helvetica" )
966cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "NewCenturySchlbk" )
967cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Palatino" )
968cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Symbol" )
969cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "Times" )
970cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "ZapfChancery" )
971cdf0e10cSrcweir         ||  aDFA.maName.EqualsAscii( "ZapfDingbats" ) )
972cdf0e10cSrcweir             aDFA.mnQuality += 500;
973cdf0e10cSrcweir     }
974cdf0e10cSrcweir 
975cdf0e10cSrcweir     // TODO: add alias names
976cdf0e10cSrcweir     return aDFA;
977cdf0e10cSrcweir }
978cdf0e10cSrcweir 
979cdf0e10cSrcweir // -----------------------------------------------------------------------
980cdf0e10cSrcweir 
981cdf0e10cSrcweir static ImplWinFontData* ImplLogMetricToDevFontDataA( const ENUMLOGFONTEXA* pLogFont,
982cdf0e10cSrcweir                                          const NEWTEXTMETRICA* pMetric,
983cdf0e10cSrcweir                                          DWORD nFontType )
984cdf0e10cSrcweir {
985cdf0e10cSrcweir     int nHeight = 0;
986cdf0e10cSrcweir     if ( nFontType & RASTER_FONTTYPE )
987cdf0e10cSrcweir         nHeight = pMetric->tmHeight - pMetric->tmInternalLeading;
988cdf0e10cSrcweir 
989cdf0e10cSrcweir     ImplWinFontData* pData = new ImplWinFontData(
990cdf0e10cSrcweir         WinFont2DevFontAttributes(*pLogFont, *pMetric, nFontType),
991cdf0e10cSrcweir         nHeight,
992cdf0e10cSrcweir         pLogFont->elfLogFont.lfCharSet,
993cdf0e10cSrcweir         pMetric->tmPitchAndFamily );
994cdf0e10cSrcweir 
995cdf0e10cSrcweir     return pData;
996cdf0e10cSrcweir }
997cdf0e10cSrcweir 
998cdf0e10cSrcweir // -----------------------------------------------------------------------
999cdf0e10cSrcweir 
1000cdf0e10cSrcweir static ImplWinFontData* ImplLogMetricToDevFontDataW( const ENUMLOGFONTEXW* pLogFont,
1001cdf0e10cSrcweir                                          const NEWTEXTMETRICW* pMetric,
1002cdf0e10cSrcweir                                          DWORD nFontType )
1003cdf0e10cSrcweir {
1004cdf0e10cSrcweir     int nHeight = 0;
1005cdf0e10cSrcweir     if ( nFontType & RASTER_FONTTYPE )
1006cdf0e10cSrcweir         nHeight = pMetric->tmHeight - pMetric->tmInternalLeading;
1007cdf0e10cSrcweir 
1008cdf0e10cSrcweir     ImplWinFontData* pData = new ImplWinFontData(
1009cdf0e10cSrcweir         WinFont2DevFontAttributes(*pLogFont, *pMetric, nFontType),
1010cdf0e10cSrcweir         nHeight,
1011cdf0e10cSrcweir         pLogFont->elfLogFont.lfCharSet,
1012cdf0e10cSrcweir         pMetric->tmPitchAndFamily );
1013cdf0e10cSrcweir 
1014cdf0e10cSrcweir     return pData;
1015cdf0e10cSrcweir }
1016cdf0e10cSrcweir 
1017cdf0e10cSrcweir // -----------------------------------------------------------------------
1018cdf0e10cSrcweir 
1019cdf0e10cSrcweir void ImplSalLogFontToFontA( HDC hDC, const LOGFONTA& rLogFont, Font& rFont )
1020cdf0e10cSrcweir {
1021cdf0e10cSrcweir     String aFontName( ImplSalGetUniString( rLogFont.lfFaceName ) );
1022cdf0e10cSrcweir     if ( aFontName.Len() )
1023cdf0e10cSrcweir     {
1024cdf0e10cSrcweir         rFont.SetName( aFontName );
1025cdf0e10cSrcweir         rFont.SetCharSet( ImplCharSetToSal( rLogFont.lfCharSet ) );
1026cdf0e10cSrcweir         rFont.SetFamily( ImplFamilyToSal( rLogFont.lfPitchAndFamily ) );
1027cdf0e10cSrcweir         rFont.SetPitch( ImplLogPitchToSal( rLogFont.lfPitchAndFamily ) );
1028cdf0e10cSrcweir         rFont.SetWeight( ImplWeightToSal( rLogFont.lfWeight ) );
1029cdf0e10cSrcweir 
1030cdf0e10cSrcweir         long nFontHeight = rLogFont.lfHeight;
1031cdf0e10cSrcweir         if ( nFontHeight < 0 )
1032cdf0e10cSrcweir             nFontHeight = -nFontHeight;
1033cdf0e10cSrcweir         long nDPIY = GetDeviceCaps( hDC, LOGPIXELSY );
1034cdf0e10cSrcweir         if( !nDPIY )
1035cdf0e10cSrcweir             nDPIY = 600;
1036cdf0e10cSrcweir         nFontHeight *= 72;
1037cdf0e10cSrcweir         nFontHeight += nDPIY/2;
1038cdf0e10cSrcweir         nFontHeight /= nDPIY;
1039cdf0e10cSrcweir         rFont.SetSize( Size( 0, nFontHeight ) );
1040cdf0e10cSrcweir         rFont.SetOrientation( (short)rLogFont.lfEscapement );
1041cdf0e10cSrcweir         if ( rLogFont.lfItalic )
1042cdf0e10cSrcweir             rFont.SetItalic( ITALIC_NORMAL );
1043cdf0e10cSrcweir         else
1044cdf0e10cSrcweir             rFont.SetItalic( ITALIC_NONE );
1045cdf0e10cSrcweir         if ( rLogFont.lfUnderline )
1046cdf0e10cSrcweir             rFont.SetUnderline( UNDERLINE_SINGLE );
1047cdf0e10cSrcweir         else
1048cdf0e10cSrcweir             rFont.SetUnderline( UNDERLINE_NONE );
1049cdf0e10cSrcweir         if ( rLogFont.lfStrikeOut )
1050cdf0e10cSrcweir             rFont.SetStrikeout( STRIKEOUT_SINGLE );
1051cdf0e10cSrcweir         else
1052cdf0e10cSrcweir             rFont.SetStrikeout( STRIKEOUT_NONE );
1053cdf0e10cSrcweir     }
1054cdf0e10cSrcweir }
1055cdf0e10cSrcweir 
1056cdf0e10cSrcweir // -----------------------------------------------------------------------
1057cdf0e10cSrcweir 
1058cdf0e10cSrcweir void ImplSalLogFontToFontW( HDC hDC, const LOGFONTW& rLogFont, Font& rFont )
1059cdf0e10cSrcweir {
1060cdf0e10cSrcweir     XubString aFontName( reinterpret_cast<const xub_Unicode*>(rLogFont.lfFaceName) );
1061cdf0e10cSrcweir     if ( aFontName.Len() )
1062cdf0e10cSrcweir     {
1063cdf0e10cSrcweir         rFont.SetName( aFontName );
1064cdf0e10cSrcweir         rFont.SetCharSet( ImplCharSetToSal( rLogFont.lfCharSet ) );
1065cdf0e10cSrcweir         rFont.SetFamily( ImplFamilyToSal( rLogFont.lfPitchAndFamily ) );
1066cdf0e10cSrcweir         rFont.SetPitch( ImplLogPitchToSal( rLogFont.lfPitchAndFamily ) );
1067cdf0e10cSrcweir         rFont.SetWeight( ImplWeightToSal( rLogFont.lfWeight ) );
1068cdf0e10cSrcweir 
1069cdf0e10cSrcweir         long nFontHeight = rLogFont.lfHeight;
1070cdf0e10cSrcweir         if ( nFontHeight < 0 )
1071cdf0e10cSrcweir             nFontHeight = -nFontHeight;
1072cdf0e10cSrcweir         long nDPIY = GetDeviceCaps( hDC, LOGPIXELSY );
1073cdf0e10cSrcweir         if( !nDPIY )
1074cdf0e10cSrcweir             nDPIY = 600;
1075cdf0e10cSrcweir         nFontHeight *= 72;
1076cdf0e10cSrcweir         nFontHeight += nDPIY/2;
1077cdf0e10cSrcweir         nFontHeight /= nDPIY;
1078cdf0e10cSrcweir         rFont.SetSize( Size( 0, nFontHeight ) );
1079cdf0e10cSrcweir         rFont.SetOrientation( (short)rLogFont.lfEscapement );
1080cdf0e10cSrcweir         if ( rLogFont.lfItalic )
1081cdf0e10cSrcweir             rFont.SetItalic( ITALIC_NORMAL );
1082cdf0e10cSrcweir         else
1083cdf0e10cSrcweir             rFont.SetItalic( ITALIC_NONE );
1084cdf0e10cSrcweir         if ( rLogFont.lfUnderline )
1085cdf0e10cSrcweir             rFont.SetUnderline( UNDERLINE_SINGLE );
1086cdf0e10cSrcweir         else
1087cdf0e10cSrcweir             rFont.SetUnderline( UNDERLINE_NONE );
1088cdf0e10cSrcweir         if ( rLogFont.lfStrikeOut )
1089cdf0e10cSrcweir             rFont.SetStrikeout( STRIKEOUT_SINGLE );
1090cdf0e10cSrcweir         else
1091cdf0e10cSrcweir             rFont.SetStrikeout( STRIKEOUT_NONE );
1092cdf0e10cSrcweir     }
1093cdf0e10cSrcweir }
1094cdf0e10cSrcweir 
1095cdf0e10cSrcweir // =======================================================================
1096cdf0e10cSrcweir 
1097cdf0e10cSrcweir ImplWinFontData::ImplWinFontData( const ImplDevFontAttributes& rDFS,
1098cdf0e10cSrcweir     int nHeight, BYTE eWinCharSet, BYTE nPitchAndFamily )
1099cdf0e10cSrcweir :   ImplFontData( rDFS, 0 ),
1100cdf0e10cSrcweir     meWinCharSet( eWinCharSet ),
1101cdf0e10cSrcweir     mnPitchAndFamily( nPitchAndFamily ),
1102cdf0e10cSrcweir     mpFontCharSets( NULL ),
1103cdf0e10cSrcweir     mpUnicodeMap( NULL ),
1104cdf0e10cSrcweir     mbGsubRead( false ),
1105cdf0e10cSrcweir     mbDisableGlyphApi( false ),
1106cdf0e10cSrcweir     mbHasKoreanRange( false ),
1107cdf0e10cSrcweir     mbHasCJKSupport( false ),
1108cdf0e10cSrcweir #ifdef ENABLE_GRAPHITE
1109cdf0e10cSrcweir     mbHasGraphiteSupport( false ),
1110cdf0e10cSrcweir #endif
1111cdf0e10cSrcweir     mbHasArabicSupport ( false ),
1112cdf0e10cSrcweir     mbAliasSymbolsLow( false ),
1113cdf0e10cSrcweir     mbAliasSymbolsHigh( false ),
1114cdf0e10cSrcweir     mnId( 0 ),
1115cdf0e10cSrcweir     mpEncodingVector( NULL )
1116cdf0e10cSrcweir {
1117cdf0e10cSrcweir     SetBitmapSize( 0, nHeight );
1118cdf0e10cSrcweir 
1119cdf0e10cSrcweir     if( eWinCharSet == SYMBOL_CHARSET )
1120cdf0e10cSrcweir     {
1121cdf0e10cSrcweir         if( (nPitchAndFamily & TMPF_TRUETYPE) != 0 )
1122cdf0e10cSrcweir         {
1123cdf0e10cSrcweir             // truetype fonts need their symbols as U+F0xx
1124cdf0e10cSrcweir             mbAliasSymbolsHigh = true;
1125cdf0e10cSrcweir         }
1126cdf0e10cSrcweir         else if( (nPitchAndFamily & (TMPF_VECTOR|TMPF_DEVICE))
1127cdf0e10cSrcweir                                  == (TMPF_VECTOR|TMPF_DEVICE) )
1128cdf0e10cSrcweir         {
1129cdf0e10cSrcweir             // scalable device fonts (e.g. builtin printer fonts)
1130cdf0e10cSrcweir             // need their symbols as U+00xx
1131cdf0e10cSrcweir             mbAliasSymbolsLow  = true;
1132cdf0e10cSrcweir         }
1133cdf0e10cSrcweir         else if( (nPitchAndFamily & (TMPF_VECTOR|TMPF_TRUETYPE)) == 0 )
1134cdf0e10cSrcweir         {
1135cdf0e10cSrcweir             // bitmap fonts need their symbols as U+F0xx
1136cdf0e10cSrcweir             mbAliasSymbolsHigh = true;
1137cdf0e10cSrcweir         }
1138cdf0e10cSrcweir     }
1139cdf0e10cSrcweir }
1140cdf0e10cSrcweir 
1141cdf0e10cSrcweir // -----------------------------------------------------------------------
1142cdf0e10cSrcweir 
1143cdf0e10cSrcweir ImplWinFontData::~ImplWinFontData()
1144cdf0e10cSrcweir {
1145cdf0e10cSrcweir     delete[] mpFontCharSets;
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir     if( mpUnicodeMap )
1148cdf0e10cSrcweir         mpUnicodeMap->DeReference();
1149cdf0e10cSrcweir     delete mpEncodingVector;
1150cdf0e10cSrcweir }
1151cdf0e10cSrcweir 
1152cdf0e10cSrcweir // -----------------------------------------------------------------------
1153cdf0e10cSrcweir 
1154cdf0e10cSrcweir sal_IntPtr ImplWinFontData::GetFontId() const
1155cdf0e10cSrcweir {
1156cdf0e10cSrcweir     return mnId;
1157cdf0e10cSrcweir }
1158cdf0e10cSrcweir 
1159cdf0e10cSrcweir // -----------------------------------------------------------------------
1160cdf0e10cSrcweir 
1161cdf0e10cSrcweir void ImplWinFontData::UpdateFromHDC( HDC hDC ) const
1162cdf0e10cSrcweir {
1163cdf0e10cSrcweir     // short circuit if already initialized
1164cdf0e10cSrcweir     if( mpUnicodeMap != NULL )
1165cdf0e10cSrcweir         return;
1166cdf0e10cSrcweir 
1167cdf0e10cSrcweir     ReadCmapTable( hDC );
1168cdf0e10cSrcweir     ReadOs2Table( hDC );
1169cdf0e10cSrcweir #ifdef ENABLE_GRAPHITE
1170cdf0e10cSrcweir 	static const char* pDisableGraphiteText = getenv( "SAL_DISABLE_GRAPHITE" );
1171cdf0e10cSrcweir     if( !pDisableGraphiteText || (pDisableGraphiteText[0] == '0') )
1172cdf0e10cSrcweir 	{
1173cdf0e10cSrcweir 		mbHasGraphiteSupport = gr::WinFont::FontHasGraphiteTables(hDC);
1174cdf0e10cSrcweir 	}
1175cdf0e10cSrcweir #endif
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir     // even if the font works some fonts have problems with the glyph API
1178cdf0e10cSrcweir     // => the heuristic below tries to figure out which fonts have the problem
1179cdf0e10cSrcweir     TEXTMETRICA aTextMetric;
1180cdf0e10cSrcweir     if( ::GetTextMetricsA( hDC, &aTextMetric ) )
1181cdf0e10cSrcweir         if( !(aTextMetric.tmPitchAndFamily & TMPF_TRUETYPE)
1182cdf0e10cSrcweir         ||   (aTextMetric.tmPitchAndFamily & TMPF_DEVICE) )
1183cdf0e10cSrcweir             mbDisableGlyphApi = true;
1184cdf0e10cSrcweir 
1185cdf0e10cSrcweir #if 0
1186cdf0e10cSrcweir     // #110548# more important than #107885# => TODO: better solution
1187cdf0e10cSrcweir     DWORD nFLI = GetFontLanguageInfo( hDC );
1188cdf0e10cSrcweir     if( 0 == (nFLI & GCP_GLYPHSHAPE) )
1189cdf0e10cSrcweir         mbDisableGlyphApi = true;
1190cdf0e10cSrcweir #endif
1191cdf0e10cSrcweir }
1192cdf0e10cSrcweir 
1193cdf0e10cSrcweir // -----------------------------------------------------------------------
1194cdf0e10cSrcweir 
1195cdf0e10cSrcweir bool ImplWinFontData::HasGSUBstitutions( HDC hDC ) const
1196cdf0e10cSrcweir {
1197cdf0e10cSrcweir     if( !mbGsubRead )
1198cdf0e10cSrcweir         ReadGsubTable( hDC );
1199cdf0e10cSrcweir     return !maGsubTable.empty();
1200cdf0e10cSrcweir }
1201cdf0e10cSrcweir 
1202cdf0e10cSrcweir // -----------------------------------------------------------------------
1203cdf0e10cSrcweir 
1204cdf0e10cSrcweir bool ImplWinFontData::IsGSUBstituted( sal_UCS4 cChar ) const
1205cdf0e10cSrcweir {
1206cdf0e10cSrcweir     return( maGsubTable.find( cChar ) != maGsubTable.end() );
1207cdf0e10cSrcweir }
1208cdf0e10cSrcweir 
1209cdf0e10cSrcweir // -----------------------------------------------------------------------
1210cdf0e10cSrcweir 
1211cdf0e10cSrcweir const ImplFontCharMap* ImplWinFontData::GetImplFontCharMap() const
1212cdf0e10cSrcweir {
1213cdf0e10cSrcweir     if( !mpUnicodeMap )
1214cdf0e10cSrcweir         return NULL;
1215cdf0e10cSrcweir     return mpUnicodeMap;
1216cdf0e10cSrcweir }
1217cdf0e10cSrcweir 
1218cdf0e10cSrcweir // -----------------------------------------------------------------------
1219cdf0e10cSrcweir 
1220cdf0e10cSrcweir static unsigned GetUInt( const unsigned char* p ) { return((p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3]);}
1221cdf0e10cSrcweir static unsigned GetUShort( const unsigned char* p ){ return((p[0]<<8)+p[1]);}
1222cdf0e10cSrcweir //static signed GetSShort( const unsigned char* p ){ return((short)((p[0]<<8)+p[1]));}
1223cdf0e10cSrcweir static inline DWORD CalcTag( const char p[4]) { return (p[0]+(p[1]<<8)+(p[2]<<16)+(p[3]<<24)); }
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir void ImplWinFontData::ReadOs2Table( HDC hDC ) const
1226cdf0e10cSrcweir {
1227cdf0e10cSrcweir     const DWORD Os2Tag = CalcTag( "OS/2" );
1228cdf0e10cSrcweir     DWORD nLength = ::GetFontData( hDC, Os2Tag, 0, NULL, 0 );
1229cdf0e10cSrcweir     if( (nLength == GDI_ERROR) || !nLength )
1230cdf0e10cSrcweir         return;
1231cdf0e10cSrcweir     std::vector<unsigned char> aOS2map( nLength );
1232cdf0e10cSrcweir     unsigned char* pOS2map = &aOS2map[0];
1233cdf0e10cSrcweir     ::GetFontData( hDC, Os2Tag, 0, pOS2map, nLength );
1234cdf0e10cSrcweir     sal_uInt32 nVersion = GetUShort( pOS2map );
1235cdf0e10cSrcweir     if ( nVersion >= 0x0001 && nLength >= 58 )
1236cdf0e10cSrcweir     {
1237cdf0e10cSrcweir         // We need at least version 0x0001 (TrueType rev 1.66)
1238cdf0e10cSrcweir         // to have access to the needed struct members.
1239cdf0e10cSrcweir         sal_uInt32 ulUnicodeRange1 = GetUInt( pOS2map + 42 );
1240cdf0e10cSrcweir         sal_uInt32 ulUnicodeRange2 = GetUInt( pOS2map + 46 );
1241cdf0e10cSrcweir #if 0
1242cdf0e10cSrcweir         sal_uInt32 ulUnicodeRange3 = GetUInt( pOS2map + 50 );
1243cdf0e10cSrcweir         sal_uInt32 ulUnicodeRange4 = GetUInt( pOS2map + 54 );
1244cdf0e10cSrcweir #endif
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir         // Check for CJK capabilities of the current font
1247cdf0e10cSrcweir         mbHasCJKSupport = (ulUnicodeRange2 & 0x2DF00000);
1248cdf0e10cSrcweir         mbHasKoreanRange= (ulUnicodeRange1 & 0x10000000)
1249cdf0e10cSrcweir                         | (ulUnicodeRange2 & 0x01100000);
1250cdf0e10cSrcweir         mbHasArabicSupport = (ulUnicodeRange1 & 0x00002000);
1251cdf0e10cSrcweir    }
1252cdf0e10cSrcweir }
1253cdf0e10cSrcweir 
1254cdf0e10cSrcweir // -----------------------------------------------------------------------
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir void ImplWinFontData::ReadGsubTable( HDC hDC ) const
1257cdf0e10cSrcweir {
1258cdf0e10cSrcweir     mbGsubRead = true;
1259cdf0e10cSrcweir 
1260cdf0e10cSrcweir     // check the existence of a GSUB table
1261cdf0e10cSrcweir     const DWORD GsubTag = CalcTag( "GSUB" );
1262cdf0e10cSrcweir     DWORD nRC = ::GetFontData( hDC, GsubTag, 0, NULL, 0 );
1263cdf0e10cSrcweir     if( (nRC == GDI_ERROR) || !nRC )
1264cdf0e10cSrcweir         return;
1265cdf0e10cSrcweir 
1266cdf0e10cSrcweir 	// parse the GSUB table through sft
1267cdf0e10cSrcweir     // TODO: parse it directly
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir     // sft needs the full font file data => get it
1270cdf0e10cSrcweir     const RawFontData aRawFontData( hDC );
1271cdf0e10cSrcweir     if( !aRawFontData.get() )
1272cdf0e10cSrcweir     	return;
1273cdf0e10cSrcweir 
1274cdf0e10cSrcweir     // open font file
1275cdf0e10cSrcweir     sal_uInt32 nFaceNum = 0;
1276cdf0e10cSrcweir     if( !*aRawFontData.get() )  // TTC candidate
1277cdf0e10cSrcweir         nFaceNum = ~0U;  // indicate "TTC font extracts only"
1278cdf0e10cSrcweir 
1279cdf0e10cSrcweir     TrueTypeFont* pTTFont = NULL;
1280cdf0e10cSrcweir     ::OpenTTFontBuffer( (void*)aRawFontData.get(), aRawFontData.size(), nFaceNum, &pTTFont );
1281cdf0e10cSrcweir     if( !pTTFont )
1282cdf0e10cSrcweir         return;
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir     // add vertically substituted characters to list
1285cdf0e10cSrcweir     static const sal_Unicode aGSUBCandidates[] = {
1286cdf0e10cSrcweir         0x0020, 0x0080, // ASCII
1287cdf0e10cSrcweir         0x2000, 0x2600, // misc
1288cdf0e10cSrcweir         0x3000, 0x3100, // CJK punctutation
1289cdf0e10cSrcweir         0x3300, 0x3400, // squared words
1290cdf0e10cSrcweir         0xFF00, 0xFFF0, // halfwidth|fullwidth forms
1291cdf0e10cSrcweir     0 };
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir     for( const sal_Unicode* pPair = aGSUBCandidates; *pPair; pPair += 2 )
1294cdf0e10cSrcweir         for( sal_Unicode cChar = pPair[0]; cChar < pPair[1]; ++cChar )
1295cdf0e10cSrcweir             if( ::MapChar( pTTFont, cChar, 0 ) != ::MapChar( pTTFont, cChar, 1 ) )
1296cdf0e10cSrcweir                 maGsubTable.insert( cChar ); // insert GSUBbed unicodes
1297cdf0e10cSrcweir 
1298cdf0e10cSrcweir     CloseTTFont( pTTFont );
1299cdf0e10cSrcweir }
1300cdf0e10cSrcweir 
1301cdf0e10cSrcweir // -----------------------------------------------------------------------
1302cdf0e10cSrcweir 
1303cdf0e10cSrcweir void ImplWinFontData::ReadCmapTable( HDC hDC ) const
1304cdf0e10cSrcweir {
1305cdf0e10cSrcweir     if( mpUnicodeMap != NULL )
1306cdf0e10cSrcweir         return;
1307cdf0e10cSrcweir 
1308cdf0e10cSrcweir     bool bIsSymbolFont = (meWinCharSet == SYMBOL_CHARSET);
1309cdf0e10cSrcweir     // get the CMAP table from the font which is selected into the DC
1310cdf0e10cSrcweir     const DWORD nCmapTag = CalcTag( "cmap" );
1311cdf0e10cSrcweir     const RawFontData aRawFontData( hDC, nCmapTag );
1312cdf0e10cSrcweir     // parse the CMAP table if available
1313cdf0e10cSrcweir     if( aRawFontData.get() ) {
1314cdf0e10cSrcweir         CmapResult aResult;
1315cdf0e10cSrcweir         ParseCMAP( aRawFontData.get(), aRawFontData.size(), aResult );
1316cdf0e10cSrcweir         mbDisableGlyphApi |= aResult.mbRecoded;
1317cdf0e10cSrcweir         aResult.mbSymbolic = bIsSymbolFont;
1318cdf0e10cSrcweir         if( aResult.mnRangeCount > 0 )
1319cdf0e10cSrcweir             mpUnicodeMap = new ImplFontCharMap( aResult );
1320cdf0e10cSrcweir     }
1321cdf0e10cSrcweir 
1322cdf0e10cSrcweir     if( !mpUnicodeMap )
1323cdf0e10cSrcweir         mpUnicodeMap = ImplFontCharMap::GetDefaultMap( bIsSymbolFont );
1324cdf0e10cSrcweir     mpUnicodeMap->AddReference();
1325cdf0e10cSrcweir }
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir // =======================================================================
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir void WinSalGraphics::SetTextColor( SalColor nSalColor )
1330cdf0e10cSrcweir {
1331cdf0e10cSrcweir     COLORREF aCol = PALETTERGB( SALCOLOR_RED( nSalColor ),
1332cdf0e10cSrcweir                                 SALCOLOR_GREEN( nSalColor ),
1333cdf0e10cSrcweir                                 SALCOLOR_BLUE( nSalColor ) );
1334cdf0e10cSrcweir 
1335cdf0e10cSrcweir     if( !mbPrinter &&
1336cdf0e10cSrcweir         GetSalData()->mhDitherPal &&
1337cdf0e10cSrcweir         ImplIsSysColorEntry( nSalColor ) )
1338cdf0e10cSrcweir     {
1339cdf0e10cSrcweir         aCol = PALRGB_TO_RGB( aCol );
1340cdf0e10cSrcweir     }
1341cdf0e10cSrcweir 
1342cdf0e10cSrcweir     ::SetTextColor( mhDC, aCol );
1343cdf0e10cSrcweir }
1344cdf0e10cSrcweir 
1345cdf0e10cSrcweir // -----------------------------------------------------------------------
1346cdf0e10cSrcweir 
1347cdf0e10cSrcweir int CALLBACK SalEnumQueryFontProcExW( const ENUMLOGFONTEXW*,
1348cdf0e10cSrcweir                                       const NEWTEXTMETRICEXW*,
1349cdf0e10cSrcweir                                       DWORD, LPARAM lParam )
1350cdf0e10cSrcweir {
1351cdf0e10cSrcweir     *((bool*)(void*)lParam) = true;
1352cdf0e10cSrcweir     return 0;
1353cdf0e10cSrcweir }
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir // -----------------------------------------------------------------------
1356cdf0e10cSrcweir 
1357cdf0e10cSrcweir int CALLBACK SalEnumQueryFontProcExA( const ENUMLOGFONTEXA*,
1358cdf0e10cSrcweir                                       const NEWTEXTMETRICEXA*,
1359cdf0e10cSrcweir                                       DWORD, LPARAM lParam )
1360cdf0e10cSrcweir {
1361cdf0e10cSrcweir     *((bool*)(void*)lParam) = true;
1362cdf0e10cSrcweir     return 0;
1363cdf0e10cSrcweir }
1364cdf0e10cSrcweir 
1365cdf0e10cSrcweir // -----------------------------------------------------------------------
1366cdf0e10cSrcweir 
1367cdf0e10cSrcweir bool ImplIsFontAvailable( HDC hDC, const UniString& rName )
1368cdf0e10cSrcweir {
1369cdf0e10cSrcweir         // Test, if Font available
1370cdf0e10cSrcweir         LOGFONTW aLogFont;
1371cdf0e10cSrcweir         memset( &aLogFont, 0, sizeof( aLogFont ) );
1372cdf0e10cSrcweir         aLogFont.lfCharSet = DEFAULT_CHARSET;
1373cdf0e10cSrcweir 
1374cdf0e10cSrcweir         UINT nNameLen = rName.Len();
1375cdf0e10cSrcweir         if ( nNameLen > (sizeof( aLogFont.lfFaceName )/sizeof( wchar_t ))-1 )
1376cdf0e10cSrcweir             nNameLen = (sizeof( aLogFont.lfFaceName )/sizeof( wchar_t ))-1;
1377cdf0e10cSrcweir         memcpy( aLogFont.lfFaceName, rName.GetBuffer(), nNameLen*sizeof( wchar_t ) );
1378cdf0e10cSrcweir         aLogFont.lfFaceName[nNameLen] = 0;
1379cdf0e10cSrcweir 
1380cdf0e10cSrcweir     bool bAvailable = false;
1381cdf0e10cSrcweir     EnumFontFamiliesExW( hDC, &aLogFont, (FONTENUMPROCW)SalEnumQueryFontProcExW,
1382cdf0e10cSrcweir                              (LPARAM)(void*)&bAvailable, 0 );
1383cdf0e10cSrcweir 
1384cdf0e10cSrcweir     return bAvailable;
1385cdf0e10cSrcweir }
1386cdf0e10cSrcweir 
1387cdf0e10cSrcweir // -----------------------------------------------------------------------
1388cdf0e10cSrcweir 
1389cdf0e10cSrcweir void ImplGetLogFontFromFontSelect( HDC hDC,
1390cdf0e10cSrcweir                                    const ImplFontSelectData* pFont,
1391cdf0e10cSrcweir                                    LOGFONTW& rLogFont,
1392cdf0e10cSrcweir                                    bool /*bTestVerticalAvail*/ )
1393cdf0e10cSrcweir {
1394cdf0e10cSrcweir     UniString   aName;
1395cdf0e10cSrcweir     if ( pFont->mpFontData )
1396cdf0e10cSrcweir         aName = pFont->mpFontData->maName;
1397cdf0e10cSrcweir     else
1398cdf0e10cSrcweir         aName = pFont->maName.GetToken( 0 );
1399cdf0e10cSrcweir 
1400cdf0e10cSrcweir     UINT nNameLen = aName.Len();
1401cdf0e10cSrcweir     if ( nNameLen > (sizeof( rLogFont.lfFaceName )/sizeof( wchar_t ))-1 )
1402cdf0e10cSrcweir         nNameLen = (sizeof( rLogFont.lfFaceName )/sizeof( wchar_t ))-1;
1403cdf0e10cSrcweir     memcpy( rLogFont.lfFaceName, aName.GetBuffer(), nNameLen*sizeof( wchar_t ) );
1404cdf0e10cSrcweir     rLogFont.lfFaceName[nNameLen] = 0;
1405cdf0e10cSrcweir 
1406cdf0e10cSrcweir     if( !pFont->mpFontData )
1407cdf0e10cSrcweir     {
1408cdf0e10cSrcweir         rLogFont.lfCharSet = pFont->IsSymbolFont() ? SYMBOL_CHARSET : DEFAULT_CHARSET;
1409cdf0e10cSrcweir         rLogFont.lfPitchAndFamily = ImplPitchToWin( pFont->mePitch )
1410cdf0e10cSrcweir                                   | ImplFamilyToWin( pFont->meFamily );
1411cdf0e10cSrcweir     }
1412cdf0e10cSrcweir     else
1413cdf0e10cSrcweir     {
1414cdf0e10cSrcweir         const ImplWinFontData* pWinFontData = static_cast<const ImplWinFontData*>( pFont->mpFontData );
1415cdf0e10cSrcweir         rLogFont.lfCharSet        = pWinFontData->GetCharSet();
1416cdf0e10cSrcweir         rLogFont.lfPitchAndFamily = pWinFontData->GetPitchAndFamily();
1417cdf0e10cSrcweir     }
1418cdf0e10cSrcweir 
1419cdf0e10cSrcweir     rLogFont.lfWeight          = ImplWeightToWin( pFont->meWeight );
1420cdf0e10cSrcweir     rLogFont.lfHeight          = (LONG)-pFont->mnHeight;
1421cdf0e10cSrcweir     rLogFont.lfWidth           = (LONG)pFont->mnWidth;
1422cdf0e10cSrcweir     rLogFont.lfUnderline       = 0;
1423cdf0e10cSrcweir     rLogFont.lfStrikeOut       = 0;
1424cdf0e10cSrcweir     rLogFont.lfItalic          = (pFont->meItalic) != ITALIC_NONE;
1425cdf0e10cSrcweir     rLogFont.lfEscapement      = pFont->mnOrientation;
1426cdf0e10cSrcweir     rLogFont.lfOrientation     = rLogFont.lfEscapement;
1427cdf0e10cSrcweir     rLogFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
1428cdf0e10cSrcweir     rLogFont.lfQuality         = DEFAULT_QUALITY;
1429cdf0e10cSrcweir     rLogFont.lfOutPrecision    = OUT_TT_PRECIS;
1430cdf0e10cSrcweir     if ( pFont->mnOrientation )
1431cdf0e10cSrcweir         rLogFont.lfClipPrecision |= CLIP_LH_ANGLES;
1432cdf0e10cSrcweir 
1433cdf0e10cSrcweir     // disable antialiasing if requested
1434cdf0e10cSrcweir     if ( pFont->mbNonAntialiased )
1435cdf0e10cSrcweir         rLogFont.lfQuality = NONANTIALIASED_QUALITY;
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir     // select vertical mode if requested and available
1438cdf0e10cSrcweir     if( pFont->mbVertical && nNameLen )
1439cdf0e10cSrcweir     {
1440cdf0e10cSrcweir         // vertical fonts start with an '@'
1441cdf0e10cSrcweir         memmove( &rLogFont.lfFaceName[1], &rLogFont.lfFaceName[0],
1442cdf0e10cSrcweir             sizeof(rLogFont.lfFaceName)-sizeof(rLogFont.lfFaceName[0]) );
1443cdf0e10cSrcweir         rLogFont.lfFaceName[0] = '@';
1444cdf0e10cSrcweir 
1445cdf0e10cSrcweir         // check availability of vertical mode for this font
1446cdf0e10cSrcweir         bool bAvailable = false;
1447cdf0e10cSrcweir         EnumFontFamiliesExW( hDC, &rLogFont, (FONTENUMPROCW)SalEnumQueryFontProcExW,
1448cdf0e10cSrcweir                          (LPARAM)&bAvailable, 0 );
1449cdf0e10cSrcweir 
1450cdf0e10cSrcweir         if( !bAvailable )
1451cdf0e10cSrcweir         {
1452cdf0e10cSrcweir             // restore non-vertical name if not vertical mode isn't available
1453cdf0e10cSrcweir             memcpy( &rLogFont.lfFaceName[0], aName.GetBuffer(), nNameLen*sizeof(wchar_t) );
1454cdf0e10cSrcweir             if( nNameLen < LF_FACESIZE )
1455cdf0e10cSrcweir                 rLogFont.lfFaceName[nNameLen] = '\0';
1456cdf0e10cSrcweir         }
1457cdf0e10cSrcweir     }
1458cdf0e10cSrcweir }
1459cdf0e10cSrcweir 
1460cdf0e10cSrcweir // -----------------------------------------------------------------------
1461cdf0e10cSrcweir 
1462cdf0e10cSrcweir static void ImplGetLogFontFromFontSelect( HDC hDC,
1463cdf0e10cSrcweir                                    const ImplFontSelectData* pFont,
1464cdf0e10cSrcweir                                    LOGFONTA& rLogFont,
1465cdf0e10cSrcweir                                    bool /*bTestVerticalAvail*/ )
1466cdf0e10cSrcweir {
1467cdf0e10cSrcweir     ByteString aName;
1468cdf0e10cSrcweir     if( pFont->mpFontData )
1469cdf0e10cSrcweir         aName = ImplSalGetWinAnsiString( pFont->mpFontData->maName );
1470cdf0e10cSrcweir     else
1471cdf0e10cSrcweir         aName = ImplSalGetWinAnsiString( pFont->maName.GetToken( 0 ) );
1472cdf0e10cSrcweir 
1473cdf0e10cSrcweir     int nNameLen = aName.Len();
1474cdf0e10cSrcweir     if( nNameLen > LF_FACESIZE )
1475cdf0e10cSrcweir         nNameLen = LF_FACESIZE;
1476cdf0e10cSrcweir     memcpy( rLogFont.lfFaceName, aName.GetBuffer(), nNameLen );
1477cdf0e10cSrcweir     if( nNameLen < LF_FACESIZE )
1478cdf0e10cSrcweir         rLogFont.lfFaceName[nNameLen] = '\0';
1479cdf0e10cSrcweir 
1480cdf0e10cSrcweir     if( !pFont->mpFontData )
1481cdf0e10cSrcweir     {
1482cdf0e10cSrcweir         rLogFont.lfCharSet = pFont->IsSymbolFont() ? SYMBOL_CHARSET : DEFAULT_CHARSET;
1483cdf0e10cSrcweir         rLogFont.lfPitchAndFamily = ImplPitchToWin( pFont->mePitch )
1484cdf0e10cSrcweir                                   | ImplFamilyToWin( pFont->meFamily );
1485cdf0e10cSrcweir     }
1486cdf0e10cSrcweir     else
1487cdf0e10cSrcweir     {
1488cdf0e10cSrcweir         const ImplWinFontData* pWinFontData = static_cast<const ImplWinFontData*>( pFont->mpFontData );
1489cdf0e10cSrcweir         rLogFont.lfCharSet        = pWinFontData->GetCharSet();
1490cdf0e10cSrcweir         rLogFont.lfPitchAndFamily = pWinFontData->GetPitchAndFamily();
1491cdf0e10cSrcweir     }
1492cdf0e10cSrcweir 
1493cdf0e10cSrcweir     rLogFont.lfWeight           = ImplWeightToWin( pFont->meWeight );
1494cdf0e10cSrcweir     rLogFont.lfHeight           = (LONG)-pFont->mnHeight;
1495cdf0e10cSrcweir     rLogFont.lfWidth            = (LONG)pFont->mnWidth;
1496cdf0e10cSrcweir     rLogFont.lfUnderline        = 0;
1497cdf0e10cSrcweir     rLogFont.lfStrikeOut        = 0;
1498cdf0e10cSrcweir     rLogFont.lfItalic           = (pFont->meItalic) != ITALIC_NONE;
1499cdf0e10cSrcweir     rLogFont.lfEscapement       = pFont->mnOrientation;
1500cdf0e10cSrcweir     rLogFont.lfOrientation      = rLogFont.lfEscapement; // ignored by W98
1501cdf0e10cSrcweir     rLogFont.lfClipPrecision    = CLIP_DEFAULT_PRECIS;
1502cdf0e10cSrcweir     rLogFont.lfQuality          = DEFAULT_QUALITY;
1503cdf0e10cSrcweir     rLogFont.lfOutPrecision     = OUT_TT_PRECIS;
1504cdf0e10cSrcweir     if( pFont->mnOrientation )
1505cdf0e10cSrcweir         rLogFont.lfClipPrecision |= CLIP_LH_ANGLES;
1506cdf0e10cSrcweir 
1507cdf0e10cSrcweir     // disable antialiasing if requested
1508cdf0e10cSrcweir     if( pFont->mbNonAntialiased )
1509cdf0e10cSrcweir         rLogFont.lfQuality = NONANTIALIASED_QUALITY;
1510cdf0e10cSrcweir 
1511cdf0e10cSrcweir     // select vertical mode if requested and available
1512cdf0e10cSrcweir     if( pFont->mbVertical && nNameLen )
1513cdf0e10cSrcweir     {
1514cdf0e10cSrcweir         // vertical fonts start with an '@'
1515cdf0e10cSrcweir         memmove( &rLogFont.lfFaceName[1], &rLogFont.lfFaceName[0],
1516cdf0e10cSrcweir                     sizeof(rLogFont.lfFaceName)-sizeof(rLogFont.lfFaceName[0]) );
1517cdf0e10cSrcweir         rLogFont.lfFaceName[0] = '@';
1518cdf0e10cSrcweir 
1519cdf0e10cSrcweir         // check availability of vertical mode for this font
1520cdf0e10cSrcweir         bool bAvailable = false;
1521cdf0e10cSrcweir         EnumFontFamiliesExA( hDC, &rLogFont, (FONTENUMPROCA)SalEnumQueryFontProcExA,
1522cdf0e10cSrcweir                          (LPARAM)&bAvailable, 0 );
1523cdf0e10cSrcweir 
1524cdf0e10cSrcweir         if( !bAvailable )
1525cdf0e10cSrcweir         {
1526cdf0e10cSrcweir             // restore non-vertical name if vertical mode is not supported
1527cdf0e10cSrcweir             memcpy( rLogFont.lfFaceName, aName.GetBuffer(), nNameLen );
1528cdf0e10cSrcweir             if( nNameLen < LF_FACESIZE )
1529cdf0e10cSrcweir                 rLogFont.lfFaceName[nNameLen] = '\0';
1530cdf0e10cSrcweir         }
1531cdf0e10cSrcweir     }
1532cdf0e10cSrcweir }
1533cdf0e10cSrcweir 
1534cdf0e10cSrcweir // -----------------------------------------------------------------------
1535cdf0e10cSrcweir 
1536cdf0e10cSrcweir HFONT WinSalGraphics::ImplDoSetFont( ImplFontSelectData* i_pFont, float& o_rFontScale, HFONT& o_rOldFont )
1537cdf0e10cSrcweir {
1538cdf0e10cSrcweir     HFONT hNewFont = 0;
1539cdf0e10cSrcweir 
1540cdf0e10cSrcweir     HDC hdcScreen = 0;
1541cdf0e10cSrcweir     if( mbVirDev )
1542cdf0e10cSrcweir         // only required for virtual devices, see below for details
1543cdf0e10cSrcweir         hdcScreen = GetDC(0);
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir     if( true/*aSalShlData.mbWNT*/ )
1546cdf0e10cSrcweir     {
1547cdf0e10cSrcweir         LOGFONTW aLogFont;
1548cdf0e10cSrcweir         ImplGetLogFontFromFontSelect( mhDC, i_pFont, aLogFont, true );
1549cdf0e10cSrcweir 
1550cdf0e10cSrcweir         // on the display we prefer Courier New when Courier is a
1551cdf0e10cSrcweir         // bitmap only font and we need to stretch or rotate it
1552cdf0e10cSrcweir         if( mbScreen
1553cdf0e10cSrcweir         &&  (i_pFont->mnWidth != 0
1554cdf0e10cSrcweir           || i_pFont->mnOrientation != 0
1555cdf0e10cSrcweir           || i_pFont->mpFontData == NULL
1556cdf0e10cSrcweir           || (i_pFont->mpFontData->GetHeight() != i_pFont->mnHeight))
1557cdf0e10cSrcweir         && !bImplSalCourierScalable
1558cdf0e10cSrcweir         && bImplSalCourierNew
1559cdf0e10cSrcweir         && (ImplSalWICompareAscii( aLogFont.lfFaceName, "Courier" ) == 0) )
1560cdf0e10cSrcweir             lstrcpynW( aLogFont.lfFaceName, L"Courier New", 11 );
1561cdf0e10cSrcweir 
1562cdf0e10cSrcweir         // #i47675# limit font requests to MAXFONTHEIGHT
1563cdf0e10cSrcweir         // TODO: share MAXFONTHEIGHT font instance
1564cdf0e10cSrcweir         if( (-aLogFont.lfHeight <= MAXFONTHEIGHT)
1565cdf0e10cSrcweir         &&  (+aLogFont.lfWidth <= MAXFONTHEIGHT) )
1566cdf0e10cSrcweir         {
1567cdf0e10cSrcweir             o_rFontScale = 1.0;
1568cdf0e10cSrcweir         }
1569cdf0e10cSrcweir         else if( -aLogFont.lfHeight >= +aLogFont.lfWidth )
1570cdf0e10cSrcweir         {
1571cdf0e10cSrcweir             o_rFontScale = -aLogFont.lfHeight / (float)MAXFONTHEIGHT;
1572cdf0e10cSrcweir             aLogFont.lfHeight = -MAXFONTHEIGHT;
1573cdf0e10cSrcweir             aLogFont.lfWidth = FRound( aLogFont.lfWidth / o_rFontScale );
1574cdf0e10cSrcweir         }
1575cdf0e10cSrcweir         else // #i95867# also limit font widths
1576cdf0e10cSrcweir         {
1577cdf0e10cSrcweir             o_rFontScale = +aLogFont.lfWidth / (float)MAXFONTHEIGHT;
1578cdf0e10cSrcweir             aLogFont.lfWidth = +MAXFONTHEIGHT;
1579cdf0e10cSrcweir             aLogFont.lfHeight = FRound( aLogFont.lfHeight / o_rFontScale );
1580cdf0e10cSrcweir         }
1581cdf0e10cSrcweir 
1582cdf0e10cSrcweir         hNewFont = ::CreateFontIndirectW( &aLogFont );
1583cdf0e10cSrcweir         if( hdcScreen )
1584cdf0e10cSrcweir         {
1585cdf0e10cSrcweir             // select font into screen hdc first to get an antialiased font
1586cdf0e10cSrcweir             // see knowledge base article 305290:
1587cdf0e10cSrcweir             // "PRB: Fonts Not Drawn Antialiased on Device Context for DirectDraw Surface"
1588cdf0e10cSrcweir             SelectFont( hdcScreen, SelectFont( hdcScreen , hNewFont ) );
1589cdf0e10cSrcweir         }
1590cdf0e10cSrcweir         o_rOldFont = ::SelectFont( mhDC, hNewFont );
1591cdf0e10cSrcweir 
1592cdf0e10cSrcweir         TEXTMETRICW aTextMetricW;
1593cdf0e10cSrcweir         if( !::GetTextMetricsW( mhDC, &aTextMetricW ) )
1594cdf0e10cSrcweir         {
1595cdf0e10cSrcweir             // the selected font doesn't work => try a replacement
1596cdf0e10cSrcweir             // TODO: use its font fallback instead
1597cdf0e10cSrcweir             lstrcpynW( aLogFont.lfFaceName, L"Courier New", 11 );
1598cdf0e10cSrcweir             aLogFont.lfPitchAndFamily = FIXED_PITCH;
1599cdf0e10cSrcweir             HFONT hNewFont2 = CreateFontIndirectW( &aLogFont );
1600cdf0e10cSrcweir             SelectFont( mhDC, hNewFont2 );
1601cdf0e10cSrcweir             DeleteFont( hNewFont );
1602cdf0e10cSrcweir             hNewFont = hNewFont2;
1603cdf0e10cSrcweir         }
1604cdf0e10cSrcweir     }
1605cdf0e10cSrcweir 
1606cdf0e10cSrcweir     if( hdcScreen )
1607cdf0e10cSrcweir         ::ReleaseDC( NULL, hdcScreen );
1608cdf0e10cSrcweir 
1609cdf0e10cSrcweir     return hNewFont;
1610cdf0e10cSrcweir }
1611cdf0e10cSrcweir 
1612cdf0e10cSrcweir sal_uInt16 WinSalGraphics::SetFont( ImplFontSelectData* pFont, int nFallbackLevel )
1613cdf0e10cSrcweir {
1614cdf0e10cSrcweir     // return early if there is no new font
1615cdf0e10cSrcweir     if( !pFont )
1616cdf0e10cSrcweir     {
1617cdf0e10cSrcweir         // deselect still active font
1618cdf0e10cSrcweir         if( mhDefFont )
1619cdf0e10cSrcweir             ::SelectFont( mhDC, mhDefFont );
1620cdf0e10cSrcweir         // release no longer referenced font handles
1621cdf0e10cSrcweir         for( int i = nFallbackLevel; i < MAX_FALLBACK; ++i )
1622cdf0e10cSrcweir         {
1623cdf0e10cSrcweir             if( mhFonts[i] )
1624cdf0e10cSrcweir                 ::DeleteFont( mhFonts[i] );
1625cdf0e10cSrcweir             mhFonts[ i ] = 0;
1626cdf0e10cSrcweir         }
1627cdf0e10cSrcweir         mhDefFont = 0;
1628cdf0e10cSrcweir         return 0;
1629cdf0e10cSrcweir     }
1630cdf0e10cSrcweir 
1631cdf0e10cSrcweir     DBG_ASSERT( pFont->mpFontData, "WinSalGraphics mpFontData==NULL");
1632cdf0e10cSrcweir     mpWinFontEntry[ nFallbackLevel ] = reinterpret_cast<ImplWinFontEntry*>( pFont->mpFontEntry );
1633cdf0e10cSrcweir     mpWinFontData[ nFallbackLevel ] = static_cast<const ImplWinFontData*>( pFont->mpFontData );
1634cdf0e10cSrcweir 
1635cdf0e10cSrcweir     HFONT hOldFont = 0;
1636cdf0e10cSrcweir     HFONT hNewFont = ImplDoSetFont( pFont, mfFontScale, hOldFont );
1637cdf0e10cSrcweir 
1638cdf0e10cSrcweir     if( !mhDefFont )
1639cdf0e10cSrcweir     {
1640cdf0e10cSrcweir         // keep default font
1641cdf0e10cSrcweir         mhDefFont = hOldFont;
1642cdf0e10cSrcweir     }
1643cdf0e10cSrcweir     else
1644cdf0e10cSrcweir     {
1645cdf0e10cSrcweir         // release no longer referenced font handles
1646cdf0e10cSrcweir         for( int i = nFallbackLevel; i < MAX_FALLBACK; ++i )
1647cdf0e10cSrcweir         {
1648cdf0e10cSrcweir             if( mhFonts[i] )
1649cdf0e10cSrcweir             {
1650cdf0e10cSrcweir                 ::DeleteFont( mhFonts[i] );
1651cdf0e10cSrcweir                 mhFonts[i] = 0;
1652cdf0e10cSrcweir             }
1653cdf0e10cSrcweir         }
1654cdf0e10cSrcweir     }
1655cdf0e10cSrcweir 
1656cdf0e10cSrcweir     // store new font in correct layer
1657cdf0e10cSrcweir     mhFonts[ nFallbackLevel ] = hNewFont;
1658cdf0e10cSrcweir     // now the font is live => update font face
1659cdf0e10cSrcweir     if( mpWinFontData[ nFallbackLevel ] )
1660cdf0e10cSrcweir         mpWinFontData[ nFallbackLevel ]->UpdateFromHDC( mhDC );
1661cdf0e10cSrcweir 
1662cdf0e10cSrcweir     if( !nFallbackLevel )
1663cdf0e10cSrcweir     {
1664cdf0e10cSrcweir         mbFontKernInit = TRUE;
1665cdf0e10cSrcweir         if ( mpFontKernPairs )
1666cdf0e10cSrcweir         {
1667cdf0e10cSrcweir             delete[] mpFontKernPairs;
1668cdf0e10cSrcweir             mpFontKernPairs = NULL;
1669cdf0e10cSrcweir         }
1670cdf0e10cSrcweir         mnFontKernPairCount = 0;
1671cdf0e10cSrcweir     }
1672cdf0e10cSrcweir 
1673cdf0e10cSrcweir     mnFontCharSetCount = 0;
1674cdf0e10cSrcweir 
1675cdf0e10cSrcweir     // some printers have higher internal resolution, so their
1676cdf0e10cSrcweir     // text output would be different from what we calculated
1677cdf0e10cSrcweir     // => suggest DrawTextArray to workaround this problem
1678cdf0e10cSrcweir     if ( mbPrinter )
1679cdf0e10cSrcweir         return SAL_SETFONT_USEDRAWTEXTARRAY;
1680cdf0e10cSrcweir     else
1681cdf0e10cSrcweir         return 0;
1682cdf0e10cSrcweir }
1683cdf0e10cSrcweir 
1684cdf0e10cSrcweir // -----------------------------------------------------------------------
1685cdf0e10cSrcweir 
1686cdf0e10cSrcweir void WinSalGraphics::GetFontMetric( ImplFontMetricData* pMetric, int nFallbackLevel )
1687cdf0e10cSrcweir {
1688cdf0e10cSrcweir     // temporarily change the HDC to the font in the fallback level
1689cdf0e10cSrcweir     HFONT hOldFont = SelectFont( mhDC, mhFonts[nFallbackLevel] );
1690cdf0e10cSrcweir 
1691cdf0e10cSrcweir         wchar_t aFaceName[LF_FACESIZE+60];
1692cdf0e10cSrcweir         if( ::GetTextFaceW( mhDC, sizeof(aFaceName)/sizeof(wchar_t), aFaceName ) )
1693cdf0e10cSrcweir             pMetric->maName = reinterpret_cast<const sal_Unicode*>(aFaceName);
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir     // get the font metric
1696cdf0e10cSrcweir     TEXTMETRICA aWinMetric;
1697cdf0e10cSrcweir     const bool bOK = GetTextMetricsA( mhDC, &aWinMetric );
1698cdf0e10cSrcweir     // restore the HDC to the font in the base level
1699cdf0e10cSrcweir     SelectFont( mhDC, hOldFont );
1700cdf0e10cSrcweir     if( !bOK )
1701cdf0e10cSrcweir         return;
1702cdf0e10cSrcweir 
1703cdf0e10cSrcweir     // device independent font attributes
1704cdf0e10cSrcweir     pMetric->meFamily       = ImplFamilyToSal( aWinMetric.tmPitchAndFamily );;
1705cdf0e10cSrcweir     pMetric->mbSymbolFlag   = (aWinMetric.tmCharSet == SYMBOL_CHARSET);
1706cdf0e10cSrcweir     pMetric->meWeight       = ImplWeightToSal( aWinMetric.tmWeight );
1707cdf0e10cSrcweir     pMetric->mePitch        = ImplMetricPitchToSal( aWinMetric.tmPitchAndFamily );
1708cdf0e10cSrcweir     pMetric->meItalic       = aWinMetric.tmItalic ? ITALIC_NORMAL : ITALIC_NONE;
1709cdf0e10cSrcweir     pMetric->mnSlant        = 0;
1710cdf0e10cSrcweir 
1711cdf0e10cSrcweir     // device dependend font attributes
1712cdf0e10cSrcweir     pMetric->mbDevice       = (aWinMetric.tmPitchAndFamily & TMPF_DEVICE) != 0;
1713cdf0e10cSrcweir     pMetric->mbScalableFont = (aWinMetric.tmPitchAndFamily & (TMPF_VECTOR|TMPF_TRUETYPE)) != 0;
1714cdf0e10cSrcweir     if( pMetric->mbScalableFont )
1715cdf0e10cSrcweir     {
1716cdf0e10cSrcweir         // check if there are kern pairs
1717cdf0e10cSrcweir         // TODO: does this work with GPOS kerning?
1718cdf0e10cSrcweir         DWORD nKernPairs = ::GetKerningPairsA( mhDC, 0, NULL );
1719cdf0e10cSrcweir         pMetric->mbKernableFont = (nKernPairs > 0);
1720cdf0e10cSrcweir     }
1721cdf0e10cSrcweir     else
1722cdf0e10cSrcweir     {
1723cdf0e10cSrcweir         // bitmap fonts cannot be rotated directly
1724cdf0e10cSrcweir         pMetric->mnOrientation  = 0;
1725cdf0e10cSrcweir         // bitmap fonts have no kerning
1726cdf0e10cSrcweir         pMetric->mbKernableFont = false;
1727cdf0e10cSrcweir     }
1728cdf0e10cSrcweir 
1729cdf0e10cSrcweir     // transformation dependend font metrics
1730cdf0e10cSrcweir     pMetric->mnWidth        = static_cast<int>( mfFontScale * aWinMetric.tmAveCharWidth );
1731cdf0e10cSrcweir     pMetric->mnIntLeading   = static_cast<int>( mfFontScale * aWinMetric.tmInternalLeading );
1732cdf0e10cSrcweir     pMetric->mnExtLeading   = static_cast<int>( mfFontScale * aWinMetric.tmExternalLeading );
1733cdf0e10cSrcweir     pMetric->mnAscent       = static_cast<int>( mfFontScale * aWinMetric.tmAscent );
1734cdf0e10cSrcweir     pMetric->mnDescent      = static_cast<int>( mfFontScale * aWinMetric.tmDescent );
1735cdf0e10cSrcweir 
1736cdf0e10cSrcweir     // #107888# improved metric compatibility for Asian fonts...
1737cdf0e10cSrcweir     // TODO: assess workaround below for CWS >= extleading
1738cdf0e10cSrcweir     // TODO: evaluate use of aWinMetric.sTypo* members for CJK
1739cdf0e10cSrcweir     if( mpWinFontData[nFallbackLevel] && mpWinFontData[nFallbackLevel]->SupportsCJK() )
1740cdf0e10cSrcweir     {
1741cdf0e10cSrcweir         pMetric->mnIntLeading += pMetric->mnExtLeading;
1742cdf0e10cSrcweir 
1743cdf0e10cSrcweir         // #109280# The line height for Asian fonts is too small.
1744cdf0e10cSrcweir         // Therefore we add half of the external leading to the
1745cdf0e10cSrcweir         // ascent, the other half is added to the descent.
1746cdf0e10cSrcweir         const long nHalfTmpExtLeading = pMetric->mnExtLeading / 2;
1747cdf0e10cSrcweir         const long nOtherHalfTmpExtLeading = pMetric->mnExtLeading - nHalfTmpExtLeading;
1748cdf0e10cSrcweir 
1749cdf0e10cSrcweir         // #110641# external leading for Asian fonts.
1750cdf0e10cSrcweir         // The factor 0.3 has been confirmed with experiments.
1751cdf0e10cSrcweir         long nCJKExtLeading = static_cast<long>(0.30 * (pMetric->mnAscent + pMetric->mnDescent));
1752cdf0e10cSrcweir         nCJKExtLeading -= pMetric->mnExtLeading;
1753cdf0e10cSrcweir         pMetric->mnExtLeading = (nCJKExtLeading > 0) ? nCJKExtLeading : 0;
1754cdf0e10cSrcweir 
1755cdf0e10cSrcweir         pMetric->mnAscent   += nHalfTmpExtLeading;
1756cdf0e10cSrcweir         pMetric->mnDescent  += nOtherHalfTmpExtLeading;
1757cdf0e10cSrcweir     }
1758cdf0e10cSrcweir 
1759cdf0e10cSrcweir     pMetric->mnMinKashida = GetMinKashidaWidth();
1760cdf0e10cSrcweir }
1761cdf0e10cSrcweir 
1762cdf0e10cSrcweir // -----------------------------------------------------------------------
1763cdf0e10cSrcweir 
1764cdf0e10cSrcweir int CALLBACK SalEnumCharSetsProcExA( const ENUMLOGFONTEXA* pLogFont,
1765cdf0e10cSrcweir                                      const NEWTEXTMETRICEXA* /*pMetric*/,
1766cdf0e10cSrcweir                                      DWORD /*nFontType*/, LPARAM lParam )
1767cdf0e10cSrcweir {
1768cdf0e10cSrcweir     WinSalGraphics* pData = (WinSalGraphics*)lParam;
1769cdf0e10cSrcweir     // Charset already in the list?
1770cdf0e10cSrcweir     for ( BYTE i = 0; i < pData->mnFontCharSetCount; i++ )
1771cdf0e10cSrcweir     {
1772cdf0e10cSrcweir         if ( pData->mpFontCharSets[i] == pLogFont->elfLogFont.lfCharSet )
1773cdf0e10cSrcweir             return 1;
1774cdf0e10cSrcweir     }
1775cdf0e10cSrcweir     pData->mpFontCharSets[pData->mnFontCharSetCount] = pLogFont->elfLogFont.lfCharSet;
1776cdf0e10cSrcweir     pData->mnFontCharSetCount++;
1777cdf0e10cSrcweir     return 1;
1778cdf0e10cSrcweir }
1779cdf0e10cSrcweir 
1780cdf0e10cSrcweir // -----------------------------------------------------------------------
1781cdf0e10cSrcweir 
1782cdf0e10cSrcweir static void ImplGetAllFontCharSets( WinSalGraphics* pData )
1783cdf0e10cSrcweir {
1784cdf0e10cSrcweir     if ( !pData->mpFontCharSets )
1785cdf0e10cSrcweir         pData->mpFontCharSets = new BYTE[256];
1786cdf0e10cSrcweir 
1787cdf0e10cSrcweir     LOGFONTA aLogFont;
1788cdf0e10cSrcweir     memset( &aLogFont, 0, sizeof( aLogFont ) );
1789cdf0e10cSrcweir     aLogFont.lfCharSet = DEFAULT_CHARSET;
1790cdf0e10cSrcweir     GetTextFaceA( pData->mhDC, sizeof( aLogFont.lfFaceName ), aLogFont.lfFaceName );
1791cdf0e10cSrcweir     EnumFontFamiliesExA( pData->mhDC, &aLogFont, (FONTENUMPROCA)SalEnumCharSetsProcExA,
1792cdf0e10cSrcweir                          (LPARAM)(void*)pData, 0 );
1793cdf0e10cSrcweir }
1794cdf0e10cSrcweir 
1795cdf0e10cSrcweir // -----------------------------------------------------------------------
1796cdf0e10cSrcweir 
1797cdf0e10cSrcweir static void ImplAddKerningPairs( WinSalGraphics* pData )
1798cdf0e10cSrcweir {
1799cdf0e10cSrcweir     sal_uLong nPairs = ::GetKerningPairsA( pData->mhDC, 0, NULL );
1800cdf0e10cSrcweir     if ( !nPairs )
1801cdf0e10cSrcweir         return;
1802cdf0e10cSrcweir 
1803cdf0e10cSrcweir     CHARSETINFO aInfo;
1804cdf0e10cSrcweir     if ( !TranslateCharsetInfo( (DWORD*)(sal_uLong)GetTextCharset( pData->mhDC ), &aInfo, TCI_SRCCHARSET ) )
1805cdf0e10cSrcweir         return;
1806cdf0e10cSrcweir 
1807cdf0e10cSrcweir     if ( !pData->mpFontKernPairs )
1808cdf0e10cSrcweir         pData->mpFontKernPairs = new KERNINGPAIR[nPairs];
1809cdf0e10cSrcweir     else
1810cdf0e10cSrcweir     {
1811cdf0e10cSrcweir         KERNINGPAIR* pOldPairs = pData->mpFontKernPairs;
1812cdf0e10cSrcweir         pData->mpFontKernPairs = new KERNINGPAIR[nPairs+pData->mnFontKernPairCount];
1813cdf0e10cSrcweir         memcpy( pData->mpFontKernPairs, pOldPairs,
1814cdf0e10cSrcweir                 pData->mnFontKernPairCount*sizeof( KERNINGPAIR ) );
1815cdf0e10cSrcweir         delete[] pOldPairs;
1816cdf0e10cSrcweir     }
1817cdf0e10cSrcweir 
1818cdf0e10cSrcweir     UINT            nCP = aInfo.ciACP;
1819cdf0e10cSrcweir     sal_uLong           nOldPairs = pData->mnFontKernPairCount;
1820cdf0e10cSrcweir     KERNINGPAIR*    pTempPair = pData->mpFontKernPairs+pData->mnFontKernPairCount;
1821cdf0e10cSrcweir     nPairs = ::GetKerningPairsA( pData->mhDC, nPairs, pTempPair );
1822cdf0e10cSrcweir     for ( sal_uLong i = 0; i < nPairs; i++ )
1823cdf0e10cSrcweir     {
1824cdf0e10cSrcweir         unsigned char   aBuf[2];
1825cdf0e10cSrcweir         wchar_t         nChar;
1826cdf0e10cSrcweir         int             nLen;
1827cdf0e10cSrcweir         sal_Bool            bAdd = TRUE;
1828cdf0e10cSrcweir 
1829cdf0e10cSrcweir         // None-ASCII?, then we must convert the char
1830cdf0e10cSrcweir         if ( (pTempPair->wFirst > 125) || (pTempPair->wFirst == 92) )
1831cdf0e10cSrcweir         {
1832cdf0e10cSrcweir             if ( pTempPair->wFirst < 256 )
1833cdf0e10cSrcweir             {
1834cdf0e10cSrcweir                 aBuf[0] = (unsigned char)pTempPair->wFirst;
1835cdf0e10cSrcweir                 nLen = 1;
1836cdf0e10cSrcweir             }
1837cdf0e10cSrcweir             else
1838cdf0e10cSrcweir             {
1839cdf0e10cSrcweir                 aBuf[0] = (unsigned char)(pTempPair->wFirst >> 8);
1840cdf0e10cSrcweir                 aBuf[1] = (unsigned char)(pTempPair->wFirst & 0xFF);
1841cdf0e10cSrcweir                 nLen = 2;
1842cdf0e10cSrcweir             }
1843cdf0e10cSrcweir             if ( MultiByteToWideChar( nCP, MB_PRECOMPOSED | MB_USEGLYPHCHARS,
1844cdf0e10cSrcweir                                       (const char*)aBuf, nLen, &nChar, 1 ) )
1845cdf0e10cSrcweir                 pTempPair->wFirst = nChar;
1846cdf0e10cSrcweir             else
1847cdf0e10cSrcweir                 bAdd = FALSE;
1848cdf0e10cSrcweir         }
1849cdf0e10cSrcweir         if ( (pTempPair->wSecond > 125) || (pTempPair->wSecond == 92) )
1850cdf0e10cSrcweir         {
1851cdf0e10cSrcweir             if ( pTempPair->wSecond < 256 )
1852cdf0e10cSrcweir             {
1853cdf0e10cSrcweir                 aBuf[0] = (unsigned char)pTempPair->wSecond;
1854cdf0e10cSrcweir                 nLen = 1;
1855cdf0e10cSrcweir             }
1856cdf0e10cSrcweir             else
1857cdf0e10cSrcweir             {
1858cdf0e10cSrcweir                 aBuf[0] = (unsigned char)(pTempPair->wSecond >> 8);
1859cdf0e10cSrcweir                 aBuf[1] = (unsigned char)(pTempPair->wSecond & 0xFF);
1860cdf0e10cSrcweir                 nLen = 2;
1861cdf0e10cSrcweir             }
1862cdf0e10cSrcweir             if ( MultiByteToWideChar( nCP, MB_PRECOMPOSED | MB_USEGLYPHCHARS,
1863cdf0e10cSrcweir                                       (const char*)aBuf, nLen, &nChar, 1 ) )
1864cdf0e10cSrcweir                 pTempPair->wSecond = nChar;
1865cdf0e10cSrcweir             else
1866cdf0e10cSrcweir                 bAdd = FALSE;
1867cdf0e10cSrcweir         }
1868cdf0e10cSrcweir 
1869cdf0e10cSrcweir         // TODO: get rid of linear search!
1870cdf0e10cSrcweir         KERNINGPAIR* pTempPair2 = pData->mpFontKernPairs;
1871cdf0e10cSrcweir         for ( sal_uLong j = 0; j < nOldPairs; j++ )
1872cdf0e10cSrcweir         {
1873cdf0e10cSrcweir             if ( (pTempPair2->wFirst == pTempPair->wFirst) &&
1874cdf0e10cSrcweir                  (pTempPair2->wSecond == pTempPair->wSecond) )
1875cdf0e10cSrcweir             {
1876cdf0e10cSrcweir                 bAdd = FALSE;
1877cdf0e10cSrcweir                 break;
1878cdf0e10cSrcweir             }
1879cdf0e10cSrcweir             pTempPair2++;
1880cdf0e10cSrcweir         }
1881cdf0e10cSrcweir 
1882cdf0e10cSrcweir         if ( bAdd )
1883cdf0e10cSrcweir         {
1884cdf0e10cSrcweir             KERNINGPAIR* pDestPair = pData->mpFontKernPairs+pData->mnFontKernPairCount;
1885cdf0e10cSrcweir             if ( pDestPair != pTempPair )
1886cdf0e10cSrcweir                 memcpy( pDestPair, pTempPair, sizeof( KERNINGPAIR ) );
1887cdf0e10cSrcweir             pData->mnFontKernPairCount++;
1888cdf0e10cSrcweir         }
1889cdf0e10cSrcweir 
1890cdf0e10cSrcweir         pTempPair++;
1891cdf0e10cSrcweir     }
1892cdf0e10cSrcweir }
1893cdf0e10cSrcweir 
1894cdf0e10cSrcweir // -----------------------------------------------------------------------
1895cdf0e10cSrcweir 
1896cdf0e10cSrcweir sal_uLong WinSalGraphics::GetKernPairs( sal_uLong nPairs, ImplKernPairData* pKernPairs )
1897cdf0e10cSrcweir {
1898cdf0e10cSrcweir     DBG_ASSERT( sizeof( KERNINGPAIR ) == sizeof( ImplKernPairData ),
1899cdf0e10cSrcweir                 "WinSalGraphics::GetKernPairs(): KERNINGPAIR != ImplKernPairData" );
1900cdf0e10cSrcweir 
1901cdf0e10cSrcweir     if ( mbFontKernInit )
1902cdf0e10cSrcweir     {
1903cdf0e10cSrcweir         if( mpFontKernPairs )
1904cdf0e10cSrcweir         {
1905cdf0e10cSrcweir             delete[] mpFontKernPairs;
1906cdf0e10cSrcweir             mpFontKernPairs = NULL;
1907cdf0e10cSrcweir         }
1908cdf0e10cSrcweir         mnFontKernPairCount = 0;
1909cdf0e10cSrcweir 
1910cdf0e10cSrcweir         KERNINGPAIR* pPairs = NULL;
1911cdf0e10cSrcweir         int nCount = ::GetKerningPairsW( mhDC, 0, NULL );
1912cdf0e10cSrcweir         if( nCount )
1913cdf0e10cSrcweir         {
1914cdf0e10cSrcweir             #ifdef GCP_KERN_HACK
1915cdf0e10cSrcweir             pPairs = new KERNINGPAIR[ nCount+1 ];
1916cdf0e10cSrcweir             mpFontKernPairs = pPairs;
1917cdf0e10cSrcweir             mnFontKernPairCount = nCount;
1918cdf0e10cSrcweir             ::GetKerningPairsW( mhDC, nCount, pPairs );
1919cdf0e10cSrcweir             #else // GCP_KERN_HACK
1920cdf0e10cSrcweir             pPairs = pKernPairs;
1921cdf0e10cSrcweir             nCount = (nCount < nPairs) : nCount : nPairs;
1922cdf0e10cSrcweir             ::GetKerningPairsW( mhDC, nCount, pPairs );
1923cdf0e10cSrcweir             return nCount;
1924cdf0e10cSrcweir             #endif // GCP_KERN_HACK
1925cdf0e10cSrcweir         }
1926cdf0e10cSrcweir 
1927cdf0e10cSrcweir         mbFontKernInit = FALSE;
1928cdf0e10cSrcweir 
1929cdf0e10cSrcweir         std::sort( mpFontKernPairs, mpFontKernPairs + mnFontKernPairCount, ImplCmpKernData );
1930cdf0e10cSrcweir     }
1931cdf0e10cSrcweir 
1932cdf0e10cSrcweir     if( !pKernPairs )
1933cdf0e10cSrcweir         return mnFontKernPairCount;
1934cdf0e10cSrcweir     else if( mpFontKernPairs )
1935cdf0e10cSrcweir     {
1936cdf0e10cSrcweir         if ( nPairs < mnFontKernPairCount )
1937cdf0e10cSrcweir             nPairs = mnFontKernPairCount;
1938cdf0e10cSrcweir         memcpy( pKernPairs, mpFontKernPairs,
1939cdf0e10cSrcweir                 nPairs*sizeof( ImplKernPairData ) );
1940cdf0e10cSrcweir         return nPairs;
1941cdf0e10cSrcweir     }
1942cdf0e10cSrcweir 
1943cdf0e10cSrcweir     return 0;
1944cdf0e10cSrcweir }
1945cdf0e10cSrcweir 
1946cdf0e10cSrcweir // -----------------------------------------------------------------------
1947cdf0e10cSrcweir 
1948cdf0e10cSrcweir const ImplFontCharMap* WinSalGraphics::GetImplFontCharMap() const
1949cdf0e10cSrcweir {
1950cdf0e10cSrcweir     if( !mpWinFontData[0] )
1951cdf0e10cSrcweir         return ImplFontCharMap::GetDefaultMap();
1952cdf0e10cSrcweir     return mpWinFontData[0]->GetImplFontCharMap();
1953cdf0e10cSrcweir }
1954cdf0e10cSrcweir 
1955cdf0e10cSrcweir // -----------------------------------------------------------------------
1956cdf0e10cSrcweir 
1957cdf0e10cSrcweir int CALLBACK SalEnumFontsProcExA( const ENUMLOGFONTEXA* pLogFont,
1958cdf0e10cSrcweir                                   const NEWTEXTMETRICEXA* pMetric,
1959cdf0e10cSrcweir                                   DWORD nFontType, LPARAM lParam )
1960cdf0e10cSrcweir {
1961cdf0e10cSrcweir     ImplEnumInfo* pInfo = (ImplEnumInfo*)(void*)lParam;
1962cdf0e10cSrcweir     if ( !pInfo->mpName )
1963cdf0e10cSrcweir     {
1964cdf0e10cSrcweir         // Ignore vertical fonts
1965cdf0e10cSrcweir         if ( pLogFont->elfLogFont.lfFaceName[0] != '@' )
1966cdf0e10cSrcweir         {
1967cdf0e10cSrcweir             if ( !pInfo->mbImplSalCourierNew )
1968cdf0e10cSrcweir                 pInfo->mbImplSalCourierNew = stricmp( pLogFont->elfLogFont.lfFaceName, "Courier New" ) == 0;
1969cdf0e10cSrcweir             if ( !pInfo->mbImplSalCourierScalable )
1970cdf0e10cSrcweir                 pInfo->mbCourier = stricmp( pLogFont->elfLogFont.lfFaceName, "Courier" ) == 0;
1971cdf0e10cSrcweir             else
1972cdf0e10cSrcweir                 pInfo->mbCourier = FALSE;
1973cdf0e10cSrcweir             String aName( ImplSalGetUniString( pLogFont->elfLogFont.lfFaceName ) );
1974cdf0e10cSrcweir             pInfo->mpName = &aName;
1975cdf0e10cSrcweir             strncpy( pInfo->mpLogFontA->lfFaceName, pLogFont->elfLogFont.lfFaceName, LF_FACESIZE );
1976cdf0e10cSrcweir             pInfo->mpLogFontA->lfCharSet = pLogFont->elfLogFont.lfCharSet;
1977cdf0e10cSrcweir             EnumFontFamiliesExA( pInfo->mhDC, pInfo->mpLogFontA, (FONTENUMPROCA)SalEnumFontsProcExA,
1978cdf0e10cSrcweir                                  (LPARAM)(void*)pInfo, 0 );
1979cdf0e10cSrcweir             pInfo->mpLogFontA->lfFaceName[0] = '\0';
1980cdf0e10cSrcweir             pInfo->mpLogFontA->lfCharSet = DEFAULT_CHARSET;
1981cdf0e10cSrcweir             pInfo->mpName = NULL;
1982cdf0e10cSrcweir             pInfo->mbCourier = FALSE;
1983cdf0e10cSrcweir         }
1984cdf0e10cSrcweir     }
1985cdf0e10cSrcweir     else
1986cdf0e10cSrcweir     {
1987cdf0e10cSrcweir         // ignore non-scalable non-device font on printer
1988cdf0e10cSrcweir         if( pInfo->mbPrinter )
1989cdf0e10cSrcweir             if( (nFontType & RASTER_FONTTYPE) && !(nFontType & DEVICE_FONTTYPE) )
1990cdf0e10cSrcweir                 return 1;
1991cdf0e10cSrcweir 
1992cdf0e10cSrcweir         ImplWinFontData* pData = ImplLogMetricToDevFontDataA( pLogFont, &(pMetric->ntmTm), nFontType );
1993cdf0e10cSrcweir         pData->SetFontId( sal_IntPtr( pInfo->mnFontCount++ ) );
1994cdf0e10cSrcweir 
1995cdf0e10cSrcweir         // prefer the system character set, so that we get as much as
1996cdf0e10cSrcweir         // possible important characters. In the other case we could only
1997cdf0e10cSrcweir         // display a limited set of characters (#87309#)
1998cdf0e10cSrcweir         if ( pInfo->mnPreferedCharSet == pLogFont->elfLogFont.lfCharSet )
1999cdf0e10cSrcweir             pData->mnQuality += 100;
2000cdf0e10cSrcweir 
2001cdf0e10cSrcweir         // knowing Courier to be scalable is nice
2002cdf0e10cSrcweir         if( pInfo->mbCourier )
2003cdf0e10cSrcweir             pInfo->mbImplSalCourierScalable |= pData->IsScalable();
2004cdf0e10cSrcweir 
2005cdf0e10cSrcweir         pInfo->mpList->Add( pData );
2006cdf0e10cSrcweir     }
2007cdf0e10cSrcweir 
2008cdf0e10cSrcweir     return 1;
2009cdf0e10cSrcweir }
2010cdf0e10cSrcweir 
2011cdf0e10cSrcweir // -----------------------------------------------------------------------
2012cdf0e10cSrcweir 
2013cdf0e10cSrcweir int CALLBACK SalEnumFontsProcExW( const ENUMLOGFONTEXW* pLogFont,
2014cdf0e10cSrcweir                                   const NEWTEXTMETRICEXW* pMetric,
2015cdf0e10cSrcweir                                   DWORD nFontType, LPARAM lParam )
2016cdf0e10cSrcweir {
2017cdf0e10cSrcweir     ImplEnumInfo* pInfo = (ImplEnumInfo*)(void*)lParam;
2018cdf0e10cSrcweir     if ( !pInfo->mpName )
2019cdf0e10cSrcweir     {
2020cdf0e10cSrcweir         // Ignore vertical fonts
2021cdf0e10cSrcweir         if ( pLogFont->elfLogFont.lfFaceName[0] != '@' )
2022cdf0e10cSrcweir         {
2023cdf0e10cSrcweir             if ( !pInfo->mbImplSalCourierNew )
2024cdf0e10cSrcweir                 pInfo->mbImplSalCourierNew = ImplSalWICompareAscii( pLogFont->elfLogFont.lfFaceName, "Courier New" ) == 0;
2025cdf0e10cSrcweir             if ( !pInfo->mbImplSalCourierScalable )
2026cdf0e10cSrcweir                 pInfo->mbCourier = ImplSalWICompareAscii( pLogFont->elfLogFont.lfFaceName, "Courier" ) == 0;
2027cdf0e10cSrcweir             else
2028cdf0e10cSrcweir                 pInfo->mbCourier = FALSE;
2029cdf0e10cSrcweir             String aName( reinterpret_cast<const sal_Unicode*>(pLogFont->elfLogFont.lfFaceName) );
2030cdf0e10cSrcweir             pInfo->mpName = &aName;
2031cdf0e10cSrcweir             memcpy( pInfo->mpLogFontW->lfFaceName, pLogFont->elfLogFont.lfFaceName, (aName.Len()+1)*sizeof( wchar_t ) );
2032cdf0e10cSrcweir             pInfo->mpLogFontW->lfCharSet = pLogFont->elfLogFont.lfCharSet;
2033cdf0e10cSrcweir             EnumFontFamiliesExW( pInfo->mhDC, pInfo->mpLogFontW, (FONTENUMPROCW)SalEnumFontsProcExW,
2034cdf0e10cSrcweir                                  (LPARAM)(void*)pInfo, 0 );
2035cdf0e10cSrcweir             pInfo->mpLogFontW->lfFaceName[0] = '\0';
2036cdf0e10cSrcweir             pInfo->mpLogFontW->lfCharSet = DEFAULT_CHARSET;
2037cdf0e10cSrcweir             pInfo->mpName = NULL;
2038cdf0e10cSrcweir             pInfo->mbCourier = FALSE;
2039cdf0e10cSrcweir         }
2040cdf0e10cSrcweir     }
2041cdf0e10cSrcweir     else
2042cdf0e10cSrcweir     {
2043cdf0e10cSrcweir         // ignore non-scalable non-device font on printer
2044cdf0e10cSrcweir         if( pInfo->mbPrinter )
2045cdf0e10cSrcweir             if( (nFontType & RASTER_FONTTYPE) && !(nFontType & DEVICE_FONTTYPE) )
2046cdf0e10cSrcweir                 return 1;
2047cdf0e10cSrcweir 
2048cdf0e10cSrcweir         ImplWinFontData* pData = ImplLogMetricToDevFontDataW( pLogFont, &(pMetric->ntmTm), nFontType );
2049cdf0e10cSrcweir         pData->SetFontId( sal_IntPtr( pInfo->mnFontCount++ ) );
2050cdf0e10cSrcweir 
2051cdf0e10cSrcweir         // knowing Courier to be scalable is nice
2052cdf0e10cSrcweir         if( pInfo->mbCourier )
2053cdf0e10cSrcweir             pInfo->mbImplSalCourierScalable |= pData->IsScalable();
2054cdf0e10cSrcweir 
2055cdf0e10cSrcweir         pInfo->mpList->Add( pData );
2056cdf0e10cSrcweir     }
2057cdf0e10cSrcweir 
2058cdf0e10cSrcweir     return 1;
2059cdf0e10cSrcweir }
2060cdf0e10cSrcweir 
2061cdf0e10cSrcweir // -----------------------------------------------------------------------
2062cdf0e10cSrcweir 
2063cdf0e10cSrcweir struct TempFontItem
2064cdf0e10cSrcweir {
2065cdf0e10cSrcweir     ::rtl::OUString maFontFilePath;
2066cdf0e10cSrcweir     ::rtl::OString maResourcePath;
2067cdf0e10cSrcweir     TempFontItem* mpNextItem;
2068cdf0e10cSrcweir };
2069cdf0e10cSrcweir 
2070cdf0e10cSrcweir #ifdef FR_PRIVATE
2071cdf0e10cSrcweir static int WINAPI __AddFontResourceExW( LPCWSTR lpszfileName, DWORD fl, PVOID pdv )
2072cdf0e10cSrcweir {
2073cdf0e10cSrcweir 	typedef int (WINAPI *AddFontResourceExW_FUNC)(LPCWSTR, DWORD, PVOID );
2074cdf0e10cSrcweir 
2075cdf0e10cSrcweir 	static AddFontResourceExW_FUNC	pFunc = NULL;
2076cdf0e10cSrcweir 	static HMODULE					hmGDI = NULL;
2077cdf0e10cSrcweir 
2078cdf0e10cSrcweir 	if ( !pFunc && !hmGDI )
2079cdf0e10cSrcweir 	{
2080cdf0e10cSrcweir 		hmGDI = GetModuleHandleA( "GDI32" );
2081cdf0e10cSrcweir 		if ( hmGDI )
2082cdf0e10cSrcweir 			pFunc = reinterpret_cast<AddFontResourceExW_FUNC>( GetProcAddress( hmGDI, "AddFontResourceExW" ) );
2083cdf0e10cSrcweir 	}
2084cdf0e10cSrcweir 
2085cdf0e10cSrcweir 	if ( pFunc )
2086cdf0e10cSrcweir 		return pFunc( lpszfileName, fl, pdv );
2087cdf0e10cSrcweir 	else
2088cdf0e10cSrcweir 	{
2089cdf0e10cSrcweir 		SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
2090cdf0e10cSrcweir 		return 0;
2091cdf0e10cSrcweir 	}
2092cdf0e10cSrcweir }
2093cdf0e10cSrcweir #endif
2094cdf0e10cSrcweir 
2095cdf0e10cSrcweir bool ImplAddTempFont( SalData& rSalData, const String& rFontFileURL )
2096cdf0e10cSrcweir {
2097cdf0e10cSrcweir     int nRet = 0;
2098cdf0e10cSrcweir     ::rtl::OUString aUSytemPath;
2099cdf0e10cSrcweir     OSL_VERIFY( !osl::FileBase::getSystemPathFromFileURL( rFontFileURL, aUSytemPath ) );
2100cdf0e10cSrcweir 
2101cdf0e10cSrcweir #ifdef FR_PRIVATE
2102cdf0e10cSrcweir     nRet = __AddFontResourceExW( reinterpret_cast<LPCWSTR>(aUSytemPath.getStr()), FR_PRIVATE, NULL );
2103cdf0e10cSrcweir #endif
2104cdf0e10cSrcweir 
2105cdf0e10cSrcweir 	if ( !nRet )
2106cdf0e10cSrcweir     {
2107cdf0e10cSrcweir         static int nCounter = 0;
2108cdf0e10cSrcweir         char aFileName[] = "soAA.fot";
2109cdf0e10cSrcweir         aFileName[2] = sal::static_int_cast<char>('A' + (15 & (nCounter>>4)));
2110cdf0e10cSrcweir         aFileName[3] = sal::static_int_cast<char>('A' + (15 & nCounter));
2111cdf0e10cSrcweir         char aResourceName[512];
2112cdf0e10cSrcweir         int nMaxLen = sizeof(aResourceName)/sizeof(*aResourceName) - 16;
2113cdf0e10cSrcweir         int nLen = ::GetTempPathA( nMaxLen, aResourceName );
2114cdf0e10cSrcweir         ::strncpy( aResourceName + nLen, aFileName, sizeof( aResourceName )- nLen );
2115cdf0e10cSrcweir         // security: end buffer in any case
2116cdf0e10cSrcweir         aResourceName[ (sizeof(aResourceName)/sizeof(*aResourceName))-1 ] = 0;
2117cdf0e10cSrcweir         ::DeleteFileA( aResourceName );
2118cdf0e10cSrcweir 
2119cdf0e10cSrcweir         rtl_TextEncoding theEncoding = osl_getThreadTextEncoding();
2120cdf0e10cSrcweir         ::rtl::OString aCFileName = rtl::OUStringToOString( aUSytemPath, theEncoding );
2121cdf0e10cSrcweir         // TODO: font should be private => need to investigate why it doesn't work then
2122cdf0e10cSrcweir         if( !::CreateScalableFontResourceA( 0, aResourceName, aCFileName.getStr(), NULL ) )
2123cdf0e10cSrcweir             return false;
2124cdf0e10cSrcweir         ++nCounter;
2125cdf0e10cSrcweir 
2126cdf0e10cSrcweir         nRet = ::AddFontResourceA( aResourceName );
2127cdf0e10cSrcweir         if( nRet > 0 )
2128cdf0e10cSrcweir         {
2129cdf0e10cSrcweir             TempFontItem* pNewItem = new TempFontItem;
2130cdf0e10cSrcweir             pNewItem->maResourcePath = rtl::OString( aResourceName );
2131cdf0e10cSrcweir             pNewItem->maFontFilePath = aUSytemPath.getStr();
2132cdf0e10cSrcweir             pNewItem->mpNextItem = rSalData.mpTempFontItem;
2133cdf0e10cSrcweir             rSalData.mpTempFontItem = pNewItem;
2134cdf0e10cSrcweir         }
2135cdf0e10cSrcweir     }
2136cdf0e10cSrcweir 
2137cdf0e10cSrcweir 	return (nRet > 0);
2138cdf0e10cSrcweir }
2139cdf0e10cSrcweir 
2140cdf0e10cSrcweir // -----------------------------------------------------------------------
2141cdf0e10cSrcweir 
2142cdf0e10cSrcweir void ImplReleaseTempFonts( SalData& rSalData )
2143cdf0e10cSrcweir {
2144cdf0e10cSrcweir     int nCount = 0;
2145cdf0e10cSrcweir     while( TempFontItem* p = rSalData.mpTempFontItem )
2146cdf0e10cSrcweir     {
2147cdf0e10cSrcweir         ++nCount;
2148cdf0e10cSrcweir         if( p->maResourcePath.getLength() )
2149cdf0e10cSrcweir         {
2150cdf0e10cSrcweir             const char* pResourcePath = p->maResourcePath.getStr();
2151cdf0e10cSrcweir             ::RemoveFontResourceA( pResourcePath );
2152cdf0e10cSrcweir             ::DeleteFileA( pResourcePath );
2153cdf0e10cSrcweir         }
2154cdf0e10cSrcweir         else
2155cdf0e10cSrcweir         {
2156cdf0e10cSrcweir             ::RemoveFontResourceW( reinterpret_cast<LPCWSTR>(p->maFontFilePath.getStr()) );
2157cdf0e10cSrcweir         }
2158cdf0e10cSrcweir 
2159cdf0e10cSrcweir         rSalData.mpTempFontItem = p->mpNextItem;
2160cdf0e10cSrcweir         delete p;
2161cdf0e10cSrcweir     }
2162cdf0e10cSrcweir 
2163cdf0e10cSrcweir #ifndef FR_PRIVATE
2164cdf0e10cSrcweir     // notify every other application
2165cdf0e10cSrcweir     // unless the temp fonts were installed as private fonts
2166cdf0e10cSrcweir     if( nCount > 0 )
2167cdf0e10cSrcweir         ::PostMessage( HWND_BROADCAST, WM_FONTCHANGE, 0, NULL );
2168cdf0e10cSrcweir #endif // FR_PRIVATE
2169cdf0e10cSrcweir }
2170cdf0e10cSrcweir 
2171cdf0e10cSrcweir // -----------------------------------------------------------------------
2172cdf0e10cSrcweir 
2173cdf0e10cSrcweir static bool ImplGetFontAttrFromFile( const String& rFontFileURL,
2174cdf0e10cSrcweir     ImplDevFontAttributes& rDFA )
2175cdf0e10cSrcweir {
2176cdf0e10cSrcweir     ::rtl::OUString aUSytemPath;
2177cdf0e10cSrcweir     OSL_VERIFY( !osl::FileBase::getSystemPathFromFileURL( rFontFileURL, aUSytemPath ) );
2178cdf0e10cSrcweir 
2179cdf0e10cSrcweir     // get FontAttributes from a *fot file
2180cdf0e10cSrcweir     // TODO: use GetTTGlobalFontInfo() to access the font directly
2181cdf0e10cSrcweir     rDFA.mnQuality    = 1000;
2182cdf0e10cSrcweir     rDFA.mbDevice     = true;
2183cdf0e10cSrcweir     rDFA.meFamily     = FAMILY_DONTKNOW;
2184cdf0e10cSrcweir     rDFA.meWidthType  = WIDTH_DONTKNOW;
2185cdf0e10cSrcweir     rDFA.meWeight     = WEIGHT_DONTKNOW;
2186cdf0e10cSrcweir     rDFA.meItalic     = ITALIC_DONTKNOW;
2187cdf0e10cSrcweir     rDFA.mePitch      = PITCH_DONTKNOW;;
2188cdf0e10cSrcweir     rDFA.mbSubsettable= true;
2189cdf0e10cSrcweir     rDFA.mbEmbeddable = false;
2190cdf0e10cSrcweir 
2191cdf0e10cSrcweir     // Create temporary file name
2192cdf0e10cSrcweir     char aFileName[] = "soAAT.fot";
2193cdf0e10cSrcweir     char aResourceName[512];
2194cdf0e10cSrcweir     int nMaxLen = sizeof(aResourceName)/sizeof(*aResourceName) - 16;
2195cdf0e10cSrcweir     int nLen = ::GetTempPathA( nMaxLen, aResourceName );
2196cdf0e10cSrcweir     ::strncpy( aResourceName + nLen, aFileName, Max( 0, nMaxLen - nLen ));
2197cdf0e10cSrcweir     ::DeleteFileA( aResourceName );
2198cdf0e10cSrcweir 
2199cdf0e10cSrcweir     // Create font resource file (typically with a .fot file name extension).
2200cdf0e10cSrcweir     rtl_TextEncoding theEncoding = osl_getThreadTextEncoding();
2201cdf0e10cSrcweir     ::rtl::OString aCFileName = rtl::OUStringToOString( aUSytemPath, theEncoding );
2202cdf0e10cSrcweir     ::CreateScalableFontResourceA( 0, aResourceName, aCFileName.getStr(), NULL );
2203cdf0e10cSrcweir 
2204cdf0e10cSrcweir     // Open and read the font resource file
2205cdf0e10cSrcweir     rtl::OUString aFotFileName = rtl::OStringToOUString( aResourceName, osl_getThreadTextEncoding() );
2206cdf0e10cSrcweir     osl::FileBase::getFileURLFromSystemPath( aFotFileName, aFotFileName );
2207cdf0e10cSrcweir     osl::File aFotFile( aFotFileName );
2208cdf0e10cSrcweir     osl::FileBase::RC aError = aFotFile.open( osl_File_OpenFlag_Read );
2209cdf0e10cSrcweir     if( aError != osl::FileBase::E_None )
2210cdf0e10cSrcweir         return false;
2211cdf0e10cSrcweir 
2212cdf0e10cSrcweir     sal_uInt64  nBytesRead = 0;
2213cdf0e10cSrcweir     char        aBuffer[4096];
2214cdf0e10cSrcweir     aFotFile.read( aBuffer, sizeof( aBuffer ), nBytesRead );
2215cdf0e10cSrcweir     // clean up temporary resource file
2216cdf0e10cSrcweir     aFotFile.close();
2217cdf0e10cSrcweir     ::DeleteFileA( aResourceName );
2218cdf0e10cSrcweir 
2219cdf0e10cSrcweir     // retrieve font family name from byte offset 0x4F6
2220cdf0e10cSrcweir     int i = 0x4F6;
2221cdf0e10cSrcweir     int nNameOfs = i;
2222cdf0e10cSrcweir     while( (i < nBytesRead) && (aBuffer[i++] != 0) );
2223cdf0e10cSrcweir     // skip full name
2224cdf0e10cSrcweir     while( (i < nBytesRead) && (aBuffer[i++] != 0) );
2225cdf0e10cSrcweir     // retrieve font style name
2226cdf0e10cSrcweir     int nStyleOfs = i;
2227cdf0e10cSrcweir     while( (i < nBytesRead) && (aBuffer[i++] != 0) );
2228cdf0e10cSrcweir     if( i >= nBytesRead )
2229cdf0e10cSrcweir         return false;
2230cdf0e10cSrcweir 
2231cdf0e10cSrcweir     // convert byte strings to unicode
2232cdf0e10cSrcweir     rDFA.maName      = String( aBuffer + nNameOfs, osl_getThreadTextEncoding() );
2233cdf0e10cSrcweir     rDFA.maStyleName = String( aBuffer + nStyleOfs, osl_getThreadTextEncoding() );
2234cdf0e10cSrcweir 
2235cdf0e10cSrcweir     // byte offset 0x4C7: OS2_fsSelection
2236cdf0e10cSrcweir     const char nFSS = aBuffer[ 0x4C7 ];
2237cdf0e10cSrcweir     if( nFSS & 0x01 )   // italic
2238cdf0e10cSrcweir         rDFA.meItalic = ITALIC_NORMAL;
2239cdf0e10cSrcweir     //if( nFSS & 0x20 )   // bold
2240cdf0e10cSrcweir     //   rDFA.meWeight = WEIGHT_BOLD;
2241cdf0e10cSrcweir     if( nFSS & 0x40 )   // regular
2242cdf0e10cSrcweir     {
2243cdf0e10cSrcweir         rDFA.meWeight = WEIGHT_NORMAL;
2244cdf0e10cSrcweir         rDFA.meItalic = ITALIC_NONE;
2245cdf0e10cSrcweir     }
2246cdf0e10cSrcweir 
2247cdf0e10cSrcweir     // byte offsets 0x4D7/0x4D8: wingdi's FW_WEIGHT
2248cdf0e10cSrcweir     int nWinWeight = (aBuffer[0x4D7] & 0xFF) + ((aBuffer[0x4D8] & 0xFF) << 8);
2249cdf0e10cSrcweir     rDFA.meWeight = ImplWeightToSal( nWinWeight );
2250cdf0e10cSrcweir 
2251cdf0e10cSrcweir     rDFA.mbSymbolFlag = false;          // TODO
2252cdf0e10cSrcweir     rDFA.mePitch      = PITCH_DONTKNOW; // TODO
2253cdf0e10cSrcweir 
2254cdf0e10cSrcweir     // byte offset 0x4DE: pitch&family
2255cdf0e10cSrcweir     rDFA.meFamily = ImplFamilyToSal( aBuffer[0x4DE] );
2256cdf0e10cSrcweir 
2257cdf0e10cSrcweir     // byte offsets 0x4C8/0x4C9: emunits
2258cdf0e10cSrcweir     // byte offsets 0x4CE/0x4CF: winascent
2259cdf0e10cSrcweir     // byte offsets 0x4D0/0x4D1: winascent+windescent-emunits
2260cdf0e10cSrcweir     // byte offsets 0x4DF/0x4E0: avgwidth
2261cdf0e10cSrcweir     //...
2262cdf0e10cSrcweir 
2263cdf0e10cSrcweir     return true;
2264cdf0e10cSrcweir }
2265cdf0e10cSrcweir 
2266cdf0e10cSrcweir // -----------------------------------------------------------------------
2267cdf0e10cSrcweir 
2268cdf0e10cSrcweir bool WinSalGraphics::AddTempDevFont( ImplDevFontList* pFontList,
2269cdf0e10cSrcweir     const String& rFontFileURL, const String& rFontName )
2270cdf0e10cSrcweir {
2271cdf0e10cSrcweir 	RTL_LOGFILE_TRACE1( "WinSalGraphics::AddTempDevFont(): %s", rtl::OUStringToOString( rFontFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
2272cdf0e10cSrcweir 
2273cdf0e10cSrcweir     ImplDevFontAttributes aDFA;
2274cdf0e10cSrcweir     aDFA.maName = rFontName;
2275cdf0e10cSrcweir     aDFA.mnQuality    = 1000;
2276cdf0e10cSrcweir     aDFA.mbDevice     = true;
2277cdf0e10cSrcweir 
2278cdf0e10cSrcweir     // Search Font Name in Cache
2279cdf0e10cSrcweir     if( !rFontName.Len() && mpFontAttrCache )
2280cdf0e10cSrcweir         aDFA = mpFontAttrCache->GetFontAttr( rFontFileURL );
2281cdf0e10cSrcweir 
2282cdf0e10cSrcweir     // Retrieve font name from font resource
2283cdf0e10cSrcweir     if( !aDFA.maName.Len() )
2284cdf0e10cSrcweir     {
2285cdf0e10cSrcweir         ImplGetFontAttrFromFile( rFontFileURL, aDFA );
2286cdf0e10cSrcweir         if( mpFontAttrCache && aDFA.maName.Len() )
2287cdf0e10cSrcweir             mpFontAttrCache->AddFontAttr( rFontFileURL, aDFA );
2288cdf0e10cSrcweir     }
2289cdf0e10cSrcweir 
2290cdf0e10cSrcweir     if ( !aDFA.maName.Len() )
2291cdf0e10cSrcweir         return false;
2292cdf0e10cSrcweir 
2293cdf0e10cSrcweir     // remember temp font for cleanup later
2294cdf0e10cSrcweir     if( !ImplAddTempFont( *GetSalData(), rFontFileURL ) )
2295cdf0e10cSrcweir         return false;
2296cdf0e10cSrcweir 
2297cdf0e10cSrcweir     UINT nPreferedCharSet = DEFAULT_CHARSET;
2298cdf0e10cSrcweir 
2299cdf0e10cSrcweir     // create matching FontData struct
2300cdf0e10cSrcweir     aDFA.mbSymbolFlag = false; // TODO: how to know it without accessing the font?
2301cdf0e10cSrcweir     aDFA.meFamily     = FAMILY_DONTKNOW;
2302cdf0e10cSrcweir     aDFA.meWidthType  = WIDTH_DONTKNOW;
2303cdf0e10cSrcweir     aDFA.meWeight     = WEIGHT_DONTKNOW;
2304cdf0e10cSrcweir     aDFA.meItalic     = ITALIC_DONTKNOW;
2305cdf0e10cSrcweir     aDFA.mePitch      = PITCH_DONTKNOW;;
2306cdf0e10cSrcweir     aDFA.mbSubsettable= true;
2307cdf0e10cSrcweir     aDFA.mbEmbeddable = false;
2308cdf0e10cSrcweir 
2309cdf0e10cSrcweir     /*
2310cdf0e10cSrcweir     // TODO: improve ImplDevFontAttributes using the "font resource file"
2311cdf0e10cSrcweir     aDFS.maName = // using "FONTRES:" from file
2312cdf0e10cSrcweir     if( rFontName != aDFS.maName )
2313cdf0e10cSrcweir         aDFS.maMapName = aFontName;
2314cdf0e10cSrcweir     */
2315cdf0e10cSrcweir 
2316cdf0e10cSrcweir     ImplWinFontData* pFontData = new ImplWinFontData( aDFA, 0,
2317cdf0e10cSrcweir         sal::static_int_cast<BYTE>(nPreferedCharSet),
2318cdf0e10cSrcweir         sal::static_int_cast<BYTE>(TMPF_VECTOR|TMPF_TRUETYPE) );
2319cdf0e10cSrcweir     pFontData->SetFontId( reinterpret_cast<sal_IntPtr>(pFontData) );
2320cdf0e10cSrcweir     pFontList->Add( pFontData );
2321cdf0e10cSrcweir     return true;
2322cdf0e10cSrcweir }
2323cdf0e10cSrcweir 
2324cdf0e10cSrcweir // -----------------------------------------------------------------------
2325cdf0e10cSrcweir 
2326cdf0e10cSrcweir void WinSalGraphics::GetDevFontList( ImplDevFontList* pFontList )
2327cdf0e10cSrcweir {
2328cdf0e10cSrcweir     // make sure all fonts are registered at least temporarily
2329cdf0e10cSrcweir     static bool bOnce = true;
2330cdf0e10cSrcweir     if( bOnce )
2331cdf0e10cSrcweir     {
2332cdf0e10cSrcweir         bOnce = false;
2333cdf0e10cSrcweir 
2334cdf0e10cSrcweir         // determine font path
2335cdf0e10cSrcweir         // since we are only interested in fonts that could not be
2336cdf0e10cSrcweir         // registered before because of missing administration rights
2337cdf0e10cSrcweir         // only the font path of the user installation is needed
2338cdf0e10cSrcweir         ::rtl::OUString aPath;
2339cdf0e10cSrcweir         osl_getExecutableFile( &aPath.pData );
2340cdf0e10cSrcweir 		::rtl::OUString aExecutableFile( aPath );
2341cdf0e10cSrcweir         aPath = aPath.copy( 0, aPath.lastIndexOf('/') );
2342cdf0e10cSrcweir         String aFontDirUrl = aPath.copy( 0, aPath.lastIndexOf('/') );
2343cdf0e10cSrcweir         aFontDirUrl += String( RTL_CONSTASCII_USTRINGPARAM("/Basis/share/fonts/truetype") );
2344cdf0e10cSrcweir 
2345cdf0e10cSrcweir         // collect fonts in font path that could not be registered
2346cdf0e10cSrcweir         osl::Directory aFontDir( aFontDirUrl );
2347cdf0e10cSrcweir         osl::FileBase::RC rcOSL = aFontDir.open();
2348cdf0e10cSrcweir         if( rcOSL == osl::FileBase::E_None )
2349cdf0e10cSrcweir         {
2350cdf0e10cSrcweir             osl::DirectoryItem aDirItem;
2351cdf0e10cSrcweir             String aEmptyString;
2352cdf0e10cSrcweir 
2353cdf0e10cSrcweir 			::rtl::OUString aBootStrap;
2354cdf0e10cSrcweir             rtl::Bootstrap::get( String( RTL_CONSTASCII_USTRINGPARAM( "BRAND_BASE_DIR" ) ), aBootStrap );
2355cdf0e10cSrcweir 			aBootStrap += String( RTL_CONSTASCII_USTRINGPARAM( "/program/" SAL_CONFIGFILE( "bootstrap" ) ) );
2356cdf0e10cSrcweir 			rtl::Bootstrap aBootstrap( aBootStrap );
2357cdf0e10cSrcweir 			::rtl::OUString aUserPath;
2358cdf0e10cSrcweir 			aBootstrap.getFrom( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UserInstallation" ) ), aUserPath );
2359cdf0e10cSrcweir 			aUserPath += String( RTL_CONSTASCII_USTRINGPARAM("/user/config/fontnames.dat") );
2360cdf0e10cSrcweir 			String aBaseURL = aPath.copy( 0, aPath.lastIndexOf('/')+1 );
2361cdf0e10cSrcweir 			mpFontAttrCache = new ImplFontAttrCache( aUserPath, aBaseURL );
2362cdf0e10cSrcweir 
2363cdf0e10cSrcweir 			while( aFontDir.getNextItem( aDirItem, 10 ) == osl::FileBase::E_None )
2364cdf0e10cSrcweir             {
2365cdf0e10cSrcweir                 osl::FileStatus aFileStatus( FileStatusMask_FileURL );
2366cdf0e10cSrcweir                 rcOSL = aDirItem.getFileStatus( aFileStatus );
2367cdf0e10cSrcweir                 if ( rcOSL == osl::FileBase::E_None )
2368cdf0e10cSrcweir                     AddTempDevFont( pFontList, aFileStatus.getFileURL(), aEmptyString );
2369cdf0e10cSrcweir             }
2370cdf0e10cSrcweir 
2371cdf0e10cSrcweir             delete mpFontAttrCache; // destructor rewrites the cache file if needed
2372cdf0e10cSrcweir             mpFontAttrCache = NULL;
2373cdf0e10cSrcweir         }
2374cdf0e10cSrcweir     }
2375cdf0e10cSrcweir 
2376cdf0e10cSrcweir     ImplEnumInfo aInfo;
2377cdf0e10cSrcweir     aInfo.mhDC          = mhDC;
2378cdf0e10cSrcweir     aInfo.mpList        = pFontList;
2379cdf0e10cSrcweir     aInfo.mpName        = NULL;
2380cdf0e10cSrcweir     aInfo.mpLogFontA    = NULL;
2381cdf0e10cSrcweir     aInfo.mpLogFontW    = NULL;
2382cdf0e10cSrcweir     aInfo.mbCourier     = false;
2383cdf0e10cSrcweir     aInfo.mbPrinter     = mbPrinter;
2384cdf0e10cSrcweir     aInfo.mnFontCount   = 0;
2385cdf0e10cSrcweir     if ( !mbPrinter )
2386cdf0e10cSrcweir     {
2387cdf0e10cSrcweir         aInfo.mbImplSalCourierScalable  = false;
2388cdf0e10cSrcweir         aInfo.mbImplSalCourierNew       = false;
2389cdf0e10cSrcweir     }
2390cdf0e10cSrcweir     else
2391cdf0e10cSrcweir     {
2392cdf0e10cSrcweir         aInfo.mbImplSalCourierScalable  = true;
2393cdf0e10cSrcweir         aInfo.mbImplSalCourierNew       = true;
2394cdf0e10cSrcweir     }
2395cdf0e10cSrcweir 
2396cdf0e10cSrcweir     aInfo.mnPreferedCharSet = DEFAULT_CHARSET;
2397cdf0e10cSrcweir     DWORD nCP = GetACP();
2398cdf0e10cSrcweir     CHARSETINFO aCharSetInfo;
2399cdf0e10cSrcweir     if ( TranslateCharsetInfo( (DWORD*)nCP, &aCharSetInfo, TCI_SRCCODEPAGE ) )
2400cdf0e10cSrcweir         aInfo.mnPreferedCharSet = aCharSetInfo.ciCharset;
2401cdf0e10cSrcweir 
2402cdf0e10cSrcweir     LOGFONTW aLogFont;
2403cdf0e10cSrcweir     memset( &aLogFont, 0, sizeof( aLogFont ) );
2404cdf0e10cSrcweir     aLogFont.lfCharSet = DEFAULT_CHARSET;
2405cdf0e10cSrcweir     aInfo.mpLogFontW = &aLogFont;
2406cdf0e10cSrcweir     EnumFontFamiliesExW( mhDC, &aLogFont,
2407cdf0e10cSrcweir             (FONTENUMPROCW)SalEnumFontsProcExW, (LPARAM)(void*)&aInfo, 0 );
2408cdf0e10cSrcweir 
2409cdf0e10cSrcweir     // Feststellen, was es fuer Courier-Schriften auf dem Bildschirm gibt,
2410cdf0e10cSrcweir     // um in SetFont() evt. Courier auf Courier New zu mappen
2411cdf0e10cSrcweir     if ( !mbPrinter )
2412cdf0e10cSrcweir     {
2413cdf0e10cSrcweir         bImplSalCourierScalable = aInfo.mbImplSalCourierScalable;
2414cdf0e10cSrcweir         bImplSalCourierNew      = aInfo.mbImplSalCourierNew;
2415cdf0e10cSrcweir     }
2416cdf0e10cSrcweir 
2417cdf0e10cSrcweir 	// set glyph fallback hook
2418cdf0e10cSrcweir 	static WinGlyphFallbackSubstititution aSubstFallback( mhDC );
2419cdf0e10cSrcweir 	pFontList->SetFallbackHook( &aSubstFallback );
2420cdf0e10cSrcweir }
2421cdf0e10cSrcweir 
2422cdf0e10cSrcweir // ----------------------------------------------------------------------------
2423cdf0e10cSrcweir 
2424cdf0e10cSrcweir void WinSalGraphics::GetDevFontSubstList( OutputDevice* )
2425cdf0e10cSrcweir {}
2426cdf0e10cSrcweir 
2427cdf0e10cSrcweir // -----------------------------------------------------------------------
2428cdf0e10cSrcweir 
2429cdf0e10cSrcweir sal_Bool WinSalGraphics::GetGlyphBoundRect( long nIndex, Rectangle& rRect )
2430cdf0e10cSrcweir {
2431cdf0e10cSrcweir     HDC hDC = mhDC;
2432cdf0e10cSrcweir 
2433cdf0e10cSrcweir     // use unity matrix
2434cdf0e10cSrcweir     MAT2 aMat;
2435cdf0e10cSrcweir     aMat.eM11 = aMat.eM22 = FixedFromDouble( 1.0 );
2436cdf0e10cSrcweir     aMat.eM12 = aMat.eM21 = FixedFromDouble( 0.0 );
2437cdf0e10cSrcweir 
2438cdf0e10cSrcweir     UINT nGGOFlags = GGO_METRICS;
2439cdf0e10cSrcweir     if( !(nIndex & GF_ISCHAR) )
2440cdf0e10cSrcweir         nGGOFlags |= GGO_GLYPH_INDEX;
2441cdf0e10cSrcweir     nIndex &= GF_IDXMASK;
2442cdf0e10cSrcweir 
2443cdf0e10cSrcweir     GLYPHMETRICS aGM;
2444cdf0e10cSrcweir     aGM.gmptGlyphOrigin.x = aGM.gmptGlyphOrigin.y = 0;
2445cdf0e10cSrcweir     aGM.gmBlackBoxX = aGM.gmBlackBoxY = 0;
2446cdf0e10cSrcweir     DWORD nSize = ::GetGlyphOutlineW( hDC, nIndex, nGGOFlags, &aGM, 0, NULL, &aMat );
2447cdf0e10cSrcweir     if( nSize == GDI_ERROR )
2448cdf0e10cSrcweir         return false;
2449cdf0e10cSrcweir 
2450cdf0e10cSrcweir     rRect = Rectangle( Point( +aGM.gmptGlyphOrigin.x, -aGM.gmptGlyphOrigin.y ),
2451cdf0e10cSrcweir         Size( aGM.gmBlackBoxX, aGM.gmBlackBoxY ) );
2452cdf0e10cSrcweir     rRect.Left()    = static_cast<int>( mfFontScale * rRect.Left() );
2453cdf0e10cSrcweir     rRect.Right()   = static_cast<int>( mfFontScale * rRect.Right() );
2454cdf0e10cSrcweir     rRect.Top()     = static_cast<int>( mfFontScale * rRect.Top() );
2455cdf0e10cSrcweir     rRect.Bottom()  = static_cast<int>( mfFontScale * rRect.Bottom() );
2456cdf0e10cSrcweir     return true;
2457cdf0e10cSrcweir }
2458cdf0e10cSrcweir 
2459cdf0e10cSrcweir // -----------------------------------------------------------------------
2460cdf0e10cSrcweir 
2461cdf0e10cSrcweir sal_Bool WinSalGraphics::GetGlyphOutline( long nIndex,
2462cdf0e10cSrcweir     ::basegfx::B2DPolyPolygon& rB2DPolyPoly )
2463cdf0e10cSrcweir {
2464cdf0e10cSrcweir     rB2DPolyPoly.clear();
2465cdf0e10cSrcweir 
2466cdf0e10cSrcweir     HDC  hDC = mhDC;
2467cdf0e10cSrcweir 
2468cdf0e10cSrcweir     // use unity matrix
2469cdf0e10cSrcweir     MAT2 aMat;
2470cdf0e10cSrcweir     aMat.eM11 = aMat.eM22 = FixedFromDouble( 1.0 );
2471cdf0e10cSrcweir     aMat.eM12 = aMat.eM21 = FixedFromDouble( 0.0 );
2472cdf0e10cSrcweir 
2473cdf0e10cSrcweir     UINT nGGOFlags = GGO_NATIVE;
2474cdf0e10cSrcweir     if( !(nIndex & GF_ISCHAR) )
2475cdf0e10cSrcweir         nGGOFlags |= GGO_GLYPH_INDEX;
2476cdf0e10cSrcweir     nIndex &= GF_IDXMASK;
2477cdf0e10cSrcweir 
2478cdf0e10cSrcweir     GLYPHMETRICS aGlyphMetrics;
2479cdf0e10cSrcweir     const DWORD nSize1 = ::GetGlyphOutlineW( hDC, nIndex, nGGOFlags, &aGlyphMetrics, 0, NULL, &aMat );
2480cdf0e10cSrcweir     if( !nSize1 )       // blank glyphs are ok
2481cdf0e10cSrcweir         return TRUE;
2482cdf0e10cSrcweir     else if( nSize1 == GDI_ERROR )
2483cdf0e10cSrcweir         return FALSE;
2484cdf0e10cSrcweir 
2485cdf0e10cSrcweir     BYTE*   pData = new BYTE[ nSize1 ];
2486cdf0e10cSrcweir     const DWORD nSize2 = ::GetGlyphOutlineW( hDC, nIndex, nGGOFlags,
2487cdf0e10cSrcweir               &aGlyphMetrics, nSize1, pData, &aMat );
2488cdf0e10cSrcweir 
2489cdf0e10cSrcweir     if( nSize1 != nSize2 )
2490cdf0e10cSrcweir         return FALSE;
2491cdf0e10cSrcweir 
2492cdf0e10cSrcweir     // TODO: avoid tools polygon by creating B2DPolygon directly
2493cdf0e10cSrcweir     int     nPtSize = 512;
2494cdf0e10cSrcweir     Point*  pPoints = new Point[ nPtSize ];
2495cdf0e10cSrcweir     BYTE*   pFlags = new BYTE[ nPtSize ];
2496cdf0e10cSrcweir 
2497cdf0e10cSrcweir     TTPOLYGONHEADER* pHeader = (TTPOLYGONHEADER*)pData;
2498cdf0e10cSrcweir     while( (BYTE*)pHeader < pData+nSize2 )
2499cdf0e10cSrcweir     {
2500cdf0e10cSrcweir         // only outline data is interesting
2501cdf0e10cSrcweir         if( pHeader->dwType != TT_POLYGON_TYPE )
2502cdf0e10cSrcweir             break;
2503cdf0e10cSrcweir 
2504cdf0e10cSrcweir         // get start point; next start points are end points
2505cdf0e10cSrcweir         // of previous segment
2506cdf0e10cSrcweir         USHORT nPnt = 0;
2507cdf0e10cSrcweir 
2508cdf0e10cSrcweir         long nX = IntTimes256FromFixed( pHeader->pfxStart.x );
2509cdf0e10cSrcweir         long nY = IntTimes256FromFixed( pHeader->pfxStart.y );
2510cdf0e10cSrcweir         pPoints[ nPnt ] = Point( nX, nY );
2511cdf0e10cSrcweir         pFlags[ nPnt++ ] = POLY_NORMAL;
2512cdf0e10cSrcweir 
2513cdf0e10cSrcweir         bool bHasOfflinePoints = false;
2514cdf0e10cSrcweir         TTPOLYCURVE* pCurve = (TTPOLYCURVE*)( pHeader + 1 );
2515cdf0e10cSrcweir         pHeader = (TTPOLYGONHEADER*)( (BYTE*)pHeader + pHeader->cb );
2516cdf0e10cSrcweir         while( (BYTE*)pCurve < (BYTE*)pHeader )
2517cdf0e10cSrcweir         {
2518cdf0e10cSrcweir             int nNeededSize = nPnt + 16 + 3 * pCurve->cpfx;
2519cdf0e10cSrcweir             if( nPtSize < nNeededSize )
2520cdf0e10cSrcweir             {
2521cdf0e10cSrcweir                 Point* pOldPoints = pPoints;
2522cdf0e10cSrcweir                 BYTE* pOldFlags = pFlags;
2523cdf0e10cSrcweir                 nPtSize = 2 * nNeededSize;
2524cdf0e10cSrcweir                 pPoints = new Point[ nPtSize ];
2525cdf0e10cSrcweir                 pFlags = new BYTE[ nPtSize ];
2526cdf0e10cSrcweir                 for( USHORT i = 0; i < nPnt; ++i )
2527cdf0e10cSrcweir                 {
2528cdf0e10cSrcweir                     pPoints[ i ] = pOldPoints[ i ];
2529cdf0e10cSrcweir                     pFlags[ i ] = pOldFlags[ i ];
2530cdf0e10cSrcweir                 }
2531cdf0e10cSrcweir                 delete[] pOldPoints;
2532cdf0e10cSrcweir                 delete[] pOldFlags;
2533cdf0e10cSrcweir             }
2534cdf0e10cSrcweir 
2535cdf0e10cSrcweir             int i = 0;
2536cdf0e10cSrcweir             if( TT_PRIM_LINE == pCurve->wType )
2537cdf0e10cSrcweir             {
2538cdf0e10cSrcweir                 while( i < pCurve->cpfx )
2539cdf0e10cSrcweir                 {
2540cdf0e10cSrcweir                     nX = IntTimes256FromFixed( pCurve->apfx[ i ].x );
2541cdf0e10cSrcweir                     nY = IntTimes256FromFixed( pCurve->apfx[ i ].y );
2542cdf0e10cSrcweir                     ++i;
2543cdf0e10cSrcweir                     pPoints[ nPnt ] = Point( nX, nY );
2544cdf0e10cSrcweir                     pFlags[ nPnt ] = POLY_NORMAL;
2545cdf0e10cSrcweir                     ++nPnt;
2546cdf0e10cSrcweir                 }
2547cdf0e10cSrcweir             }
2548cdf0e10cSrcweir             else if( TT_PRIM_QSPLINE == pCurve->wType )
2549cdf0e10cSrcweir             {
2550cdf0e10cSrcweir                 bHasOfflinePoints = true;
2551cdf0e10cSrcweir                 while( i < pCurve->cpfx )
2552cdf0e10cSrcweir                 {
2553cdf0e10cSrcweir                     // get control point of quadratic bezier spline
2554cdf0e10cSrcweir                     nX = IntTimes256FromFixed( pCurve->apfx[ i ].x );
2555cdf0e10cSrcweir                     nY = IntTimes256FromFixed( pCurve->apfx[ i ].y );
2556cdf0e10cSrcweir                     ++i;
2557cdf0e10cSrcweir                     Point aControlP( nX, nY );
2558cdf0e10cSrcweir 
2559cdf0e10cSrcweir                     // calculate first cubic control point
2560cdf0e10cSrcweir                     // P0 = 1/3 * (PBeg + 2 * PQControl)
2561cdf0e10cSrcweir                     nX = pPoints[ nPnt-1 ].X() + 2 * aControlP.X();
2562cdf0e10cSrcweir                     nY = pPoints[ nPnt-1 ].Y() + 2 * aControlP.Y();
2563cdf0e10cSrcweir                     pPoints[ nPnt+0 ] = Point( (2*nX+3)/6, (2*nY+3)/6 );
2564cdf0e10cSrcweir                     pFlags[ nPnt+0 ] = POLY_CONTROL;
2565cdf0e10cSrcweir 
2566cdf0e10cSrcweir                     // calculate endpoint of segment
2567cdf0e10cSrcweir                     nX = IntTimes256FromFixed( pCurve->apfx[ i ].x );
2568cdf0e10cSrcweir                     nY = IntTimes256FromFixed( pCurve->apfx[ i ].y );
2569cdf0e10cSrcweir 
2570cdf0e10cSrcweir                     if ( i+1 >= pCurve->cpfx )
2571cdf0e10cSrcweir                     {
2572cdf0e10cSrcweir                         // endpoint is either last point in segment => advance
2573cdf0e10cSrcweir                         ++i;
2574cdf0e10cSrcweir                     }
2575cdf0e10cSrcweir                     else
2576cdf0e10cSrcweir                     {
2577cdf0e10cSrcweir                         // or endpoint is the middle of two control points
2578cdf0e10cSrcweir                         nX += IntTimes256FromFixed( pCurve->apfx[ i-1 ].x );
2579cdf0e10cSrcweir                         nY += IntTimes256FromFixed( pCurve->apfx[ i-1 ].y );
2580cdf0e10cSrcweir                         nX = (nX + 1) / 2;
2581cdf0e10cSrcweir                         nY = (nY + 1) / 2;
2582cdf0e10cSrcweir                         // no need to advance, because the current point
2583cdf0e10cSrcweir                         // is the control point in next bezier spline
2584cdf0e10cSrcweir                     }
2585cdf0e10cSrcweir 
2586cdf0e10cSrcweir                     pPoints[ nPnt+2 ] = Point( nX, nY );
2587cdf0e10cSrcweir                     pFlags[ nPnt+2 ] = POLY_NORMAL;
2588cdf0e10cSrcweir 
2589cdf0e10cSrcweir                     // calculate second cubic control point
2590cdf0e10cSrcweir                     // P1 = 1/3 * (PEnd + 2 * PQControl)
2591cdf0e10cSrcweir                     nX = pPoints[ nPnt+2 ].X() + 2 * aControlP.X();
2592cdf0e10cSrcweir                     nY = pPoints[ nPnt+2 ].Y() + 2 * aControlP.Y();
2593cdf0e10cSrcweir                     pPoints[ nPnt+1 ] = Point( (2*nX+3)/6, (2*nY+3)/6 );
2594cdf0e10cSrcweir                     pFlags[ nPnt+1 ] = POLY_CONTROL;
2595cdf0e10cSrcweir 
2596cdf0e10cSrcweir                     nPnt += 3;
2597cdf0e10cSrcweir                 }
2598cdf0e10cSrcweir             }
2599cdf0e10cSrcweir 
2600cdf0e10cSrcweir             // next curve segment
2601cdf0e10cSrcweir             pCurve = (TTPOLYCURVE*)&pCurve->apfx[ i ];
2602cdf0e10cSrcweir         }
2603cdf0e10cSrcweir 
2604cdf0e10cSrcweir         // end point is start point for closed contour
2605cdf0e10cSrcweir         // disabled, because Polygon class closes the contour itself
2606cdf0e10cSrcweir         // pPoints[nPnt++] = pPoints[0];
2607cdf0e10cSrcweir         // #i35928#
2608cdf0e10cSrcweir         // Added again, but add only when not yet closed
2609cdf0e10cSrcweir         if(pPoints[nPnt - 1] != pPoints[0])
2610cdf0e10cSrcweir         {
2611cdf0e10cSrcweir             if( bHasOfflinePoints )
2612cdf0e10cSrcweir                 pFlags[nPnt] = pFlags[0];
2613cdf0e10cSrcweir 
2614cdf0e10cSrcweir             pPoints[nPnt++] = pPoints[0];
2615cdf0e10cSrcweir         }
2616cdf0e10cSrcweir 
2617cdf0e10cSrcweir         // convert y-coordinates W32 -> VCL
2618cdf0e10cSrcweir         for( int i = 0; i < nPnt; ++i )
2619cdf0e10cSrcweir             pPoints[i].Y() = -pPoints[i].Y();
2620cdf0e10cSrcweir 
2621cdf0e10cSrcweir         // insert into polypolygon
2622cdf0e10cSrcweir         Polygon aPoly( nPnt, pPoints, (bHasOfflinePoints ? pFlags : NULL) );
2623cdf0e10cSrcweir         // convert to B2DPolyPolygon
2624cdf0e10cSrcweir         // TODO: get rid of the intermediate PolyPolygon
2625cdf0e10cSrcweir         rB2DPolyPoly.append( aPoly.getB2DPolygon() );
2626cdf0e10cSrcweir     }
2627cdf0e10cSrcweir 
2628cdf0e10cSrcweir     delete[] pPoints;
2629cdf0e10cSrcweir     delete[] pFlags;
2630cdf0e10cSrcweir 
2631cdf0e10cSrcweir     delete[] pData;
2632cdf0e10cSrcweir 
2633cdf0e10cSrcweir     // rescaling needed for the PolyPolygon conversion
2634cdf0e10cSrcweir     if( rB2DPolyPoly.count() )
2635cdf0e10cSrcweir     {
2636cdf0e10cSrcweir 		const double fFactor(mfFontScale/256);
2637cdf0e10cSrcweir 		rB2DPolyPoly.transform(basegfx::tools::createScaleB2DHomMatrix(fFactor, fFactor));
2638cdf0e10cSrcweir     }
2639cdf0e10cSrcweir 
2640cdf0e10cSrcweir     return TRUE;
2641cdf0e10cSrcweir }
2642cdf0e10cSrcweir 
2643cdf0e10cSrcweir // -----------------------------------------------------------------------
2644cdf0e10cSrcweir 
2645cdf0e10cSrcweir class ScopedFont
2646cdf0e10cSrcweir {
2647cdf0e10cSrcweir public:
2648cdf0e10cSrcweir     explicit ScopedFont(WinSalGraphics & rData);
2649cdf0e10cSrcweir 
2650cdf0e10cSrcweir     ~ScopedFont();
2651cdf0e10cSrcweir 
2652cdf0e10cSrcweir private:
2653cdf0e10cSrcweir     WinSalGraphics & m_rData;
2654cdf0e10cSrcweir     HFONT m_hOrigFont;
2655cdf0e10cSrcweir };
2656cdf0e10cSrcweir 
2657cdf0e10cSrcweir ScopedFont::ScopedFont(WinSalGraphics & rData): m_rData(rData)
2658cdf0e10cSrcweir {
2659cdf0e10cSrcweir     m_hOrigFont = m_rData.mhFonts[0];
2660cdf0e10cSrcweir     m_rData.mhFonts[0] = 0; // avoid deletion of current font
2661cdf0e10cSrcweir }
2662cdf0e10cSrcweir 
2663cdf0e10cSrcweir ScopedFont::~ScopedFont()
2664cdf0e10cSrcweir {
2665cdf0e10cSrcweir     if( m_hOrigFont )
2666cdf0e10cSrcweir     {
2667cdf0e10cSrcweir         // restore original font, destroy temporary font
2668cdf0e10cSrcweir         HFONT hTempFont = m_rData.mhFonts[0];
2669cdf0e10cSrcweir         m_rData.mhFonts[0] = m_hOrigFont;
2670cdf0e10cSrcweir         SelectObject( m_rData.mhDC, m_hOrigFont );
2671cdf0e10cSrcweir         DeleteObject( hTempFont );
2672cdf0e10cSrcweir     }
2673cdf0e10cSrcweir }
2674cdf0e10cSrcweir 
2675cdf0e10cSrcweir class ScopedTrueTypeFont
2676cdf0e10cSrcweir {
2677cdf0e10cSrcweir public:
2678cdf0e10cSrcweir     inline ScopedTrueTypeFont(): m_pFont(0) {}
2679cdf0e10cSrcweir 
2680cdf0e10cSrcweir     ~ScopedTrueTypeFont();
2681cdf0e10cSrcweir 
2682cdf0e10cSrcweir     int open(void * pBuffer, sal_uInt32 nLen, sal_uInt32 nFaceNum);
2683cdf0e10cSrcweir 
2684cdf0e10cSrcweir     inline TrueTypeFont * get() const { return m_pFont; }
2685cdf0e10cSrcweir 
2686cdf0e10cSrcweir private:
2687cdf0e10cSrcweir     TrueTypeFont * m_pFont;
2688cdf0e10cSrcweir };
2689cdf0e10cSrcweir 
2690cdf0e10cSrcweir ScopedTrueTypeFont::~ScopedTrueTypeFont()
2691cdf0e10cSrcweir {
2692cdf0e10cSrcweir     if (m_pFont != 0)
2693cdf0e10cSrcweir         CloseTTFont(m_pFont);
2694cdf0e10cSrcweir }
2695cdf0e10cSrcweir 
2696cdf0e10cSrcweir int ScopedTrueTypeFont::open(void * pBuffer, sal_uInt32 nLen,
2697cdf0e10cSrcweir                              sal_uInt32 nFaceNum)
2698cdf0e10cSrcweir {
2699cdf0e10cSrcweir     OSL_ENSURE(m_pFont == 0, "already open");
2700cdf0e10cSrcweir     return OpenTTFontBuffer(pBuffer, nLen, nFaceNum, &m_pFont);
2701cdf0e10cSrcweir }
2702cdf0e10cSrcweir 
2703cdf0e10cSrcweir sal_Bool WinSalGraphics::CreateFontSubset( const rtl::OUString& rToFile,
2704cdf0e10cSrcweir     const ImplFontData* pFont, long* pGlyphIDs, sal_uInt8* pEncoding,
2705cdf0e10cSrcweir     sal_Int32* pGlyphWidths, int nGlyphCount, FontSubsetInfo& rInfo )
2706cdf0e10cSrcweir {
2707cdf0e10cSrcweir     // TODO: use more of the central font-subsetting code, move stuff there if needed
2708cdf0e10cSrcweir 
2709cdf0e10cSrcweir     // create matching ImplFontSelectData
2710cdf0e10cSrcweir     // we need just enough to get to the font file data
2711cdf0e10cSrcweir     // use height=1000 for easier debugging (to match psprint's font units)
2712cdf0e10cSrcweir     ImplFontSelectData aIFSD( *pFont, Size(0,1000), 1000.0, 0, false );
2713cdf0e10cSrcweir 
2714cdf0e10cSrcweir     // TODO: much better solution: move SetFont and restoration of old font to caller
2715cdf0e10cSrcweir     ScopedFont aOldFont(*this);
2716cdf0e10cSrcweir     float fScale = 1.0;
2717cdf0e10cSrcweir     HFONT hOldFont = 0;
2718cdf0e10cSrcweir     ImplDoSetFont( &aIFSD, fScale, hOldFont );
2719cdf0e10cSrcweir 
2720cdf0e10cSrcweir     ImplWinFontData* pWinFontData = (ImplWinFontData*)aIFSD.mpFontData;
2721cdf0e10cSrcweir 
2722cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
2723cdf0e10cSrcweir     // get font metrics
2724cdf0e10cSrcweir     TEXTMETRICA aWinMetric;
2725cdf0e10cSrcweir     if( !::GetTextMetricsA( mhDC, &aWinMetric ) )
2726cdf0e10cSrcweir         return FALSE;
2727cdf0e10cSrcweir 
2728cdf0e10cSrcweir     DBG_ASSERT( !(aWinMetric.tmPitchAndFamily & TMPF_DEVICE), "cannot subset device font" );
2729cdf0e10cSrcweir     DBG_ASSERT( aWinMetric.tmPitchAndFamily & TMPF_TRUETYPE, "can only subset TT font" );
2730cdf0e10cSrcweir #endif
2731cdf0e10cSrcweir 
2732cdf0e10cSrcweir     rtl::OUString aSysPath;
2733cdf0e10cSrcweir     if( osl_File_E_None != osl_getSystemPathFromFileURL( rToFile.pData, &aSysPath.pData ) )
2734cdf0e10cSrcweir         return FALSE;
2735cdf0e10cSrcweir     const rtl_TextEncoding aThreadEncoding = osl_getThreadTextEncoding();
2736cdf0e10cSrcweir     const ByteString aToFile( aSysPath.getStr(), (xub_StrLen)aSysPath.getLength(), aThreadEncoding );
2737cdf0e10cSrcweir 
2738cdf0e10cSrcweir 	// check if the font has a CFF-table
2739cdf0e10cSrcweir 	const DWORD nCffTag = CalcTag( "CFF " );
2740cdf0e10cSrcweir 	const RawFontData aRawCffData( mhDC, nCffTag );
2741cdf0e10cSrcweir 	if( aRawCffData.get() )
2742cdf0e10cSrcweir 	{
2743cdf0e10cSrcweir 		pWinFontData->UpdateFromHDC( mhDC );
2744cdf0e10cSrcweir 		const ImplFontCharMap* pCharMap = pWinFontData->GetImplFontCharMap();
2745cdf0e10cSrcweir 		pCharMap->AddReference();
2746cdf0e10cSrcweir 
2747cdf0e10cSrcweir 		long nRealGlyphIds[ 256 ];
2748cdf0e10cSrcweir 		for( int i = 0; i < nGlyphCount; ++i )
2749cdf0e10cSrcweir 		{
2750cdf0e10cSrcweir 			// TODO: remap notdef glyph if needed
2751cdf0e10cSrcweir 			// TODO: use GDI's GetGlyphIndices instead? Does it handle GSUB properly?
2752cdf0e10cSrcweir 			sal_uInt32 nGlyphIdx = pGlyphIDs[i] & GF_IDXMASK;
2753cdf0e10cSrcweir 			if( pGlyphIDs[i] & GF_ISCHAR ) // remaining pseudo-glyphs need to be translated
2754cdf0e10cSrcweir 				nGlyphIdx = pCharMap->GetGlyphIndex( nGlyphIdx );
2755cdf0e10cSrcweir 			if( (pGlyphIDs[i] & (GF_ROTMASK|GF_GSUB)) != 0)	// TODO: vertical substitution
2756cdf0e10cSrcweir 				{/*####*/}
2757cdf0e10cSrcweir 
2758cdf0e10cSrcweir 			nRealGlyphIds[i] = nGlyphIdx;
2759cdf0e10cSrcweir 		}
2760cdf0e10cSrcweir 
2761cdf0e10cSrcweir 		pCharMap->DeReference(); // TODO: and and use a RAII object
2762cdf0e10cSrcweir 
2763cdf0e10cSrcweir 		// provide a font subset from the CFF-table
2764cdf0e10cSrcweir 		FILE* pOutFile = fopen( aToFile.GetBuffer(), "wb" );
2765cdf0e10cSrcweir 		rInfo.LoadFont( FontSubsetInfo::CFF_FONT, aRawCffData.get(), aRawCffData.size() );
2766cdf0e10cSrcweir 		bool bRC = rInfo.CreateFontSubset( FontSubsetInfo::TYPE1_PFB, pOutFile, NULL,
2767cdf0e10cSrcweir 				nRealGlyphIds, pEncoding, nGlyphCount, pGlyphWidths );
2768cdf0e10cSrcweir 		fclose( pOutFile );
2769cdf0e10cSrcweir 		return bRC;
2770cdf0e10cSrcweir 	}
2771cdf0e10cSrcweir 
2772cdf0e10cSrcweir     // get raw font file data
2773cdf0e10cSrcweir     const RawFontData xRawFontData( mhDC, NULL );
2774cdf0e10cSrcweir     if( !xRawFontData.get() )
2775cdf0e10cSrcweir 		return FALSE;
2776cdf0e10cSrcweir 
2777cdf0e10cSrcweir     // open font file
2778cdf0e10cSrcweir     sal_uInt32 nFaceNum = 0;
2779cdf0e10cSrcweir     if( !*xRawFontData.get() )  // TTC candidate
2780cdf0e10cSrcweir         nFaceNum = ~0U;  // indicate "TTC font extracts only"
2781cdf0e10cSrcweir 
2782cdf0e10cSrcweir     ScopedTrueTypeFont aSftTTF;
2783cdf0e10cSrcweir     int nRC = aSftTTF.open( (void*)xRawFontData.get(), xRawFontData.size(), nFaceNum );
2784cdf0e10cSrcweir     if( nRC != SF_OK )
2785cdf0e10cSrcweir         return FALSE;
2786cdf0e10cSrcweir 
2787cdf0e10cSrcweir     TTGlobalFontInfo aTTInfo;
2788cdf0e10cSrcweir     ::GetTTGlobalFontInfo( aSftTTF.get(), &aTTInfo );
2789cdf0e10cSrcweir     rInfo.m_nFontType   = FontSubsetInfo::SFNT_TTF;
2790cdf0e10cSrcweir     rInfo.m_aPSName     = ImplSalGetUniString( aTTInfo.psname );
2791*59213536SEike Rathke     rInfo.m_nAscent	    = aTTInfo.winAscent;
2792*59213536SEike Rathke     rInfo.m_nDescent    = aTTInfo.winDescent;
2793cdf0e10cSrcweir     rInfo.m_aFontBBox	= Rectangle( Point( aTTInfo.xMin, aTTInfo.yMin ),
2794cdf0e10cSrcweir                                     Point( aTTInfo.xMax, aTTInfo.yMax ) );
2795cdf0e10cSrcweir     rInfo.m_nCapHeight	= aTTInfo.yMax; // Well ...
2796cdf0e10cSrcweir 
2797cdf0e10cSrcweir     // subset TTF-glyphs and get their properties
2798cdf0e10cSrcweir     // take care that subset fonts require the NotDef glyph in pos 0
2799cdf0e10cSrcweir     int nOrigCount = nGlyphCount;
2800cdf0e10cSrcweir     sal_uInt16    aShortIDs[ 256 ];
2801cdf0e10cSrcweir     sal_uInt8 aTempEncs[ 256 ];
2802cdf0e10cSrcweir 
2803cdf0e10cSrcweir     int nNotDef=-1, i;
2804cdf0e10cSrcweir     for( i = 0; i < nGlyphCount; ++i )
2805cdf0e10cSrcweir     {
2806cdf0e10cSrcweir         aTempEncs[i] = pEncoding[i];
2807cdf0e10cSrcweir         sal_uInt32 nGlyphIdx = pGlyphIDs[i] & GF_IDXMASK;
2808cdf0e10cSrcweir         if( pGlyphIDs[i] & GF_ISCHAR )
2809cdf0e10cSrcweir         {
2810cdf0e10cSrcweir         	sal_Unicode cChar = static_cast<sal_Unicode>(nGlyphIdx); // TODO: sal_UCS4
2811cdf0e10cSrcweir         	const bool bVertical = ((pGlyphIDs[i] & (GF_ROTMASK|GF_GSUB)) != 0);
2812cdf0e10cSrcweir             nGlyphIdx = ::MapChar( aSftTTF.get(), cChar, bVertical );
2813cdf0e10cSrcweir             if( (nGlyphIdx == 0) && pFont->IsSymbolFont() )
2814cdf0e10cSrcweir             {
2815cdf0e10cSrcweir                 // #i12824# emulate symbol aliasing U+FXXX <-> U+0XXX
2816cdf0e10cSrcweir                 cChar = (cChar & 0xF000) ? (cChar & 0x00FF) : (cChar | 0xF000);
2817cdf0e10cSrcweir                 nGlyphIdx = ::MapChar( aSftTTF.get(), cChar, bVertical );
2818cdf0e10cSrcweir             }
2819cdf0e10cSrcweir         }
2820cdf0e10cSrcweir         aShortIDs[i] = static_cast<sal_uInt16>( nGlyphIdx );
2821cdf0e10cSrcweir         if( !nGlyphIdx )
2822cdf0e10cSrcweir             if( nNotDef < 0 )
2823cdf0e10cSrcweir                 nNotDef = i; // first NotDef glyph found
2824cdf0e10cSrcweir     }
2825cdf0e10cSrcweir 
2826cdf0e10cSrcweir     if( nNotDef != 0 )
2827cdf0e10cSrcweir     {
2828cdf0e10cSrcweir         // add fake NotDef glyph if needed
2829cdf0e10cSrcweir         if( nNotDef < 0 )
2830cdf0e10cSrcweir             nNotDef = nGlyphCount++;
2831cdf0e10cSrcweir 
2832cdf0e10cSrcweir         // NotDef glyph must be in pos 0 => swap glyphids
2833cdf0e10cSrcweir         aShortIDs[ nNotDef ] = aShortIDs[0];
2834cdf0e10cSrcweir         aTempEncs[ nNotDef ] = aTempEncs[0];
2835cdf0e10cSrcweir         aShortIDs[0] = 0;
2836cdf0e10cSrcweir         aTempEncs[0] = 0;
2837cdf0e10cSrcweir     }
2838cdf0e10cSrcweir     DBG_ASSERT( nGlyphCount < 257, "too many glyphs for subsetting" );
2839cdf0e10cSrcweir 
2840cdf0e10cSrcweir     // fill pWidth array
2841cdf0e10cSrcweir     TTSimpleGlyphMetrics* pMetrics =
2842cdf0e10cSrcweir         ::GetTTSimpleGlyphMetrics( aSftTTF.get(), aShortIDs, nGlyphCount, aIFSD.mbVertical );
2843cdf0e10cSrcweir     if( !pMetrics )
2844cdf0e10cSrcweir         return FALSE;
2845cdf0e10cSrcweir     sal_uInt16 nNotDefAdv   = pMetrics[0].adv;
2846cdf0e10cSrcweir     pMetrics[0].adv         = pMetrics[nNotDef].adv;
2847cdf0e10cSrcweir     pMetrics[nNotDef].adv   = nNotDefAdv;
2848cdf0e10cSrcweir     for( i = 0; i < nOrigCount; ++i )
2849cdf0e10cSrcweir         pGlyphWidths[i] = pMetrics[i].adv;
2850cdf0e10cSrcweir     free( pMetrics );
2851cdf0e10cSrcweir 
2852cdf0e10cSrcweir     // write subset into destination file
2853cdf0e10cSrcweir     nRC = ::CreateTTFromTTGlyphs( aSftTTF.get(), aToFile.GetBuffer(), aShortIDs,
2854cdf0e10cSrcweir             aTempEncs, nGlyphCount, 0, NULL, 0 );
2855cdf0e10cSrcweir     return (nRC == SF_OK);
2856cdf0e10cSrcweir }
2857cdf0e10cSrcweir 
2858cdf0e10cSrcweir //--------------------------------------------------------------------------
2859cdf0e10cSrcweir 
2860cdf0e10cSrcweir const void* WinSalGraphics::GetEmbedFontData( const ImplFontData* pFont,
2861cdf0e10cSrcweir     const sal_Unicode* pUnicodes, sal_Int32* pCharWidths,
2862cdf0e10cSrcweir     FontSubsetInfo& rInfo, long* pDataLen )
2863cdf0e10cSrcweir {
2864cdf0e10cSrcweir     // create matching ImplFontSelectData
2865cdf0e10cSrcweir     // we need just enough to get to the font file data
2866cdf0e10cSrcweir     ImplFontSelectData aIFSD( *pFont, Size(0,1000), 1000.0, 0, false );
2867cdf0e10cSrcweir 
2868cdf0e10cSrcweir     // TODO: much better solution: move SetFont and restoration of old font to caller
2869cdf0e10cSrcweir     ScopedFont aOldFont(*this);
2870cdf0e10cSrcweir     SetFont( &aIFSD, 0 );
2871cdf0e10cSrcweir 
2872cdf0e10cSrcweir     // get the raw font file data
2873cdf0e10cSrcweir     RawFontData aRawFontData( mhDC );
2874cdf0e10cSrcweir     *pDataLen = aRawFontData.size();
2875cdf0e10cSrcweir     if( !aRawFontData.get() )
2876cdf0e10cSrcweir         return NULL;
2877cdf0e10cSrcweir 
2878cdf0e10cSrcweir     // get important font properties
2879cdf0e10cSrcweir     TEXTMETRICA aTm;
2880cdf0e10cSrcweir     if( !::GetTextMetricsA( mhDC, &aTm ) )
2881cdf0e10cSrcweir         *pDataLen = 0;
2882cdf0e10cSrcweir     const bool bPFA = (*aRawFontData.get() < 0x80);
2883cdf0e10cSrcweir     rInfo.m_nFontType = bPFA ? FontSubsetInfo::TYPE1_PFA : FontSubsetInfo::TYPE1_PFB;
2884cdf0e10cSrcweir     WCHAR aFaceName[64];
2885cdf0e10cSrcweir     int nFNLen = ::GetTextFaceW( mhDC, 64, aFaceName );
2886cdf0e10cSrcweir     // #i59854# strip eventual null byte
2887cdf0e10cSrcweir     while( nFNLen > 0 && aFaceName[nFNLen-1] == 0 )
2888cdf0e10cSrcweir         nFNLen--;
2889cdf0e10cSrcweir     if( nFNLen == 0 )
2890cdf0e10cSrcweir         *pDataLen = 0;
2891cdf0e10cSrcweir     rInfo.m_aPSName     = String( reinterpret_cast<const sal_Unicode*>(aFaceName), sal::static_int_cast<sal_uInt16>(nFNLen) );
2892cdf0e10cSrcweir     rInfo.m_nAscent     = +aTm.tmAscent;
2893cdf0e10cSrcweir     rInfo.m_nDescent    = -aTm.tmDescent;
2894cdf0e10cSrcweir     rInfo.m_aFontBBox   = Rectangle( Point( -aTm.tmOverhang, -aTm.tmDescent ),
2895cdf0e10cSrcweir               Point( aTm.tmMaxCharWidth, aTm.tmAscent+aTm.tmExternalLeading ) );
2896cdf0e10cSrcweir     rInfo.m_nCapHeight  = aTm.tmAscent; // Well ...
2897cdf0e10cSrcweir 
2898cdf0e10cSrcweir     // get individual character widths
2899cdf0e10cSrcweir     for( int i = 0; i < 256; ++i )
2900cdf0e10cSrcweir     {
2901cdf0e10cSrcweir         int nCharWidth = 0;
2902cdf0e10cSrcweir         const sal_Unicode cChar = pUnicodes[i];
2903cdf0e10cSrcweir         if( !::GetCharWidth32W( mhDC, cChar, cChar, &nCharWidth ) )
2904cdf0e10cSrcweir             *pDataLen = 0;
2905cdf0e10cSrcweir         pCharWidths[i] = nCharWidth;
2906cdf0e10cSrcweir     }
2907cdf0e10cSrcweir 
2908cdf0e10cSrcweir     if( !*pDataLen )
2909cdf0e10cSrcweir         return NULL;
2910cdf0e10cSrcweir 
2911cdf0e10cSrcweir 	const unsigned char* pData = aRawFontData.steal();
2912cdf0e10cSrcweir     return (void*)pData;
2913cdf0e10cSrcweir }
2914cdf0e10cSrcweir 
2915cdf0e10cSrcweir //--------------------------------------------------------------------------
2916cdf0e10cSrcweir 
2917cdf0e10cSrcweir void WinSalGraphics::FreeEmbedFontData( const void* pData, long /*nLen*/ )
2918cdf0e10cSrcweir {
2919cdf0e10cSrcweir     delete[] reinterpret_cast<char*>(const_cast<void*>(pData));
2920cdf0e10cSrcweir }
2921cdf0e10cSrcweir 
2922cdf0e10cSrcweir //--------------------------------------------------------------------------
2923cdf0e10cSrcweir 
2924cdf0e10cSrcweir const Ucs2SIntMap* WinSalGraphics::GetFontEncodingVector( const ImplFontData* pFont, const Ucs2OStrMap** pNonEncoded )
2925cdf0e10cSrcweir {
2926cdf0e10cSrcweir     // TODO: even for builtin fonts we get here... why?
2927cdf0e10cSrcweir     if( !pFont->IsEmbeddable() )
2928cdf0e10cSrcweir         return NULL;
2929cdf0e10cSrcweir 
2930cdf0e10cSrcweir     // fill the encoding vector
2931cdf0e10cSrcweir     // currently no nonencoded vector
2932cdf0e10cSrcweir     if( pNonEncoded )
2933cdf0e10cSrcweir         *pNonEncoded = NULL;
2934cdf0e10cSrcweir 
2935cdf0e10cSrcweir     const ImplWinFontData* pWinFontData = static_cast<const ImplWinFontData*>(pFont);
2936cdf0e10cSrcweir     const Ucs2SIntMap* pEncoding = pWinFontData->GetEncodingVector();
2937cdf0e10cSrcweir     if( pEncoding == NULL )
2938cdf0e10cSrcweir     {
2939cdf0e10cSrcweir         Ucs2SIntMap* pNewEncoding = new Ucs2SIntMap;
2940cdf0e10cSrcweir         #if 0
2941cdf0e10cSrcweir         // TODO: get correct encoding vector
2942cdf0e10cSrcweir         GLYPHSET aGlyphSet;
2943cdf0e10cSrcweir         aGlyphSet.cbThis = sizeof(aGlyphSet);
2944cdf0e10cSrcweir         DWORD aW = ::GetFontUnicodeRanges( mhDC, &aGlyphSet);
2945cdf0e10cSrcweir         #else
2946cdf0e10cSrcweir         for( sal_Unicode i = 32; i < 256; ++i )
2947cdf0e10cSrcweir             (*pNewEncoding)[i] = i;
2948cdf0e10cSrcweir         #endif
2949cdf0e10cSrcweir         pWinFontData->SetEncodingVector( pNewEncoding );
2950cdf0e10cSrcweir 	pEncoding = pNewEncoding;
2951cdf0e10cSrcweir     }
2952cdf0e10cSrcweir 
2953cdf0e10cSrcweir     return pEncoding;
2954cdf0e10cSrcweir }
2955cdf0e10cSrcweir 
2956cdf0e10cSrcweir //--------------------------------------------------------------------------
2957cdf0e10cSrcweir 
2958cdf0e10cSrcweir void WinSalGraphics::GetGlyphWidths( const ImplFontData* pFont,
2959cdf0e10cSrcweir                                      bool bVertical,
2960cdf0e10cSrcweir                                      Int32Vector& rWidths,
2961cdf0e10cSrcweir                                      Ucs2UIntMap& rUnicodeEnc )
2962cdf0e10cSrcweir {
2963cdf0e10cSrcweir     // create matching ImplFontSelectData
2964cdf0e10cSrcweir     // we need just enough to get to the font file data
2965cdf0e10cSrcweir     ImplFontSelectData aIFSD( *pFont, Size(0,1000), 1000.0, 0, false );
2966cdf0e10cSrcweir 
2967cdf0e10cSrcweir     // TODO: much better solution: move SetFont and restoration of old font to caller
2968cdf0e10cSrcweir     ScopedFont aOldFont(*this);
2969cdf0e10cSrcweir 
2970cdf0e10cSrcweir     float fScale = 0.0;
2971cdf0e10cSrcweir     HFONT hOldFont = 0;
2972cdf0e10cSrcweir     ImplDoSetFont( &aIFSD, fScale, hOldFont );
2973cdf0e10cSrcweir 
2974cdf0e10cSrcweir     if( pFont->IsSubsettable() )
2975cdf0e10cSrcweir     {
2976cdf0e10cSrcweir         // get raw font file data
2977cdf0e10cSrcweir         const RawFontData xRawFontData( mhDC );
2978cdf0e10cSrcweir 	    if( !xRawFontData.get() )
2979cdf0e10cSrcweir 			return;
2980cdf0e10cSrcweir 
2981cdf0e10cSrcweir         // open font file
2982cdf0e10cSrcweir         sal_uInt32 nFaceNum = 0;
2983cdf0e10cSrcweir         if( !*xRawFontData.get() )  // TTC candidate
2984cdf0e10cSrcweir             nFaceNum = ~0U;  // indicate "TTC font extracts only"
2985cdf0e10cSrcweir 
2986cdf0e10cSrcweir         ScopedTrueTypeFont aSftTTF;
2987cdf0e10cSrcweir         int nRC = aSftTTF.open( (void*)xRawFontData.get(), xRawFontData.size(), nFaceNum );
2988cdf0e10cSrcweir         if( nRC != SF_OK )
2989cdf0e10cSrcweir             return;
2990cdf0e10cSrcweir 
2991cdf0e10cSrcweir         int nGlyphs = GetTTGlyphCount( aSftTTF.get() );
2992cdf0e10cSrcweir         if( nGlyphs > 0 )
2993cdf0e10cSrcweir         {
2994cdf0e10cSrcweir             rWidths.resize(nGlyphs);
2995cdf0e10cSrcweir             std::vector<sal_uInt16> aGlyphIds(nGlyphs);
2996cdf0e10cSrcweir             for( int i = 0; i < nGlyphs; i++ )
2997cdf0e10cSrcweir                 aGlyphIds[i] = sal_uInt16(i);
2998cdf0e10cSrcweir             TTSimpleGlyphMetrics* pMetrics = ::GetTTSimpleGlyphMetrics( aSftTTF.get(),
2999cdf0e10cSrcweir                                                                         &aGlyphIds[0],
3000cdf0e10cSrcweir                                                                         nGlyphs,
3001cdf0e10cSrcweir                                                                         bVertical ? 1 : 0 );
3002cdf0e10cSrcweir             if( pMetrics )
3003cdf0e10cSrcweir             {
3004cdf0e10cSrcweir                 for( int i = 0; i< nGlyphs; i++ )
3005cdf0e10cSrcweir                     rWidths[i] = pMetrics[i].adv;
3006cdf0e10cSrcweir                 free( pMetrics );
3007cdf0e10cSrcweir                 rUnicodeEnc.clear();
3008cdf0e10cSrcweir             }
3009cdf0e10cSrcweir             const ImplWinFontData* pWinFont = static_cast<const ImplWinFontData*>(pFont);
3010cdf0e10cSrcweir             const ImplFontCharMap* pMap = pWinFont->GetImplFontCharMap();
3011cdf0e10cSrcweir 			DBG_ASSERT( pMap && pMap->GetCharCount(), "no map" );
3012cdf0e10cSrcweir 			pMap->AddReference();
3013cdf0e10cSrcweir 
3014cdf0e10cSrcweir 			int nCharCount = pMap->GetCharCount();
3015cdf0e10cSrcweir             sal_uInt32 nChar = pMap->GetFirstChar();
3016cdf0e10cSrcweir 			for( int i = 0; i < nCharCount; i++ )
3017cdf0e10cSrcweir             {
3018cdf0e10cSrcweir                 if( nChar < 0x00010000 )
3019cdf0e10cSrcweir                 {
3020cdf0e10cSrcweir                     sal_uInt16 nGlyph = ::MapChar( aSftTTF.get(),
3021cdf0e10cSrcweir                                                    static_cast<sal_Ucs>(nChar),
3022cdf0e10cSrcweir                                                    bVertical ? 1 : 0 );
3023cdf0e10cSrcweir                     if( nGlyph )
3024cdf0e10cSrcweir                         rUnicodeEnc[ static_cast<sal_Unicode>(nChar) ] = nGlyph;
3025cdf0e10cSrcweir                 }
3026cdf0e10cSrcweir 				nChar = pMap->GetNextChar( nChar );
3027cdf0e10cSrcweir             }
3028cdf0e10cSrcweir 
3029cdf0e10cSrcweir 			pMap->DeReference(); // TODO: and and use a RAII object
3030cdf0e10cSrcweir         }
3031cdf0e10cSrcweir     }
3032cdf0e10cSrcweir     else if( pFont->IsEmbeddable() )
3033cdf0e10cSrcweir     {
3034cdf0e10cSrcweir         // get individual character widths
3035cdf0e10cSrcweir         rWidths.clear();
3036cdf0e10cSrcweir         rUnicodeEnc.clear();
3037cdf0e10cSrcweir         rWidths.reserve( 224 );
3038cdf0e10cSrcweir         for( sal_Unicode i = 32; i < 256; ++i )
3039cdf0e10cSrcweir         {
3040cdf0e10cSrcweir             int nCharWidth = 0;
3041cdf0e10cSrcweir             if( ::GetCharWidth32W( mhDC, i, i, &nCharWidth ) )
3042cdf0e10cSrcweir             {
3043cdf0e10cSrcweir                 rUnicodeEnc[ i ] = rWidths.size();
3044cdf0e10cSrcweir                 rWidths.push_back( nCharWidth );
3045cdf0e10cSrcweir             }
3046cdf0e10cSrcweir         }
3047cdf0e10cSrcweir     }
3048cdf0e10cSrcweir }
3049cdf0e10cSrcweir 
3050cdf0e10cSrcweir //--------------------------------------------------------------------------
3051cdf0e10cSrcweir 
3052cdf0e10cSrcweir void WinSalGraphics::DrawServerFontLayout( const ServerFontLayout& )
3053cdf0e10cSrcweir {}
3054cdf0e10cSrcweir 
3055cdf0e10cSrcweir //--------------------------------------------------------------------------
3056cdf0e10cSrcweir 
3057cdf0e10cSrcweir SystemFontData WinSalGraphics::GetSysFontData( int nFallbacklevel ) const
3058cdf0e10cSrcweir {
3059cdf0e10cSrcweir     SystemFontData aSysFontData;
3060cdf0e10cSrcweir 
3061cdf0e10cSrcweir     if (nFallbacklevel >= MAX_FALLBACK) nFallbacklevel = MAX_FALLBACK - 1;
3062cdf0e10cSrcweir     if (nFallbacklevel < 0 ) nFallbacklevel = 0;
3063cdf0e10cSrcweir 
3064cdf0e10cSrcweir     aSysFontData.nSize = sizeof( SystemFontData );
3065cdf0e10cSrcweir     aSysFontData.hFont = mhFonts[nFallbacklevel];
3066cdf0e10cSrcweir     aSysFontData.bFakeBold = false;
3067cdf0e10cSrcweir     aSysFontData.bFakeItalic = false;
3068cdf0e10cSrcweir     aSysFontData.bAntialias = true;
3069cdf0e10cSrcweir     aSysFontData.bVerticalCharacterType = false;
3070cdf0e10cSrcweir 
3071cdf0e10cSrcweir     OSL_TRACE("\r\n:WinSalGraphics::GetSysFontData(): FontID: %p, Fallback level: %d",
3072cdf0e10cSrcweir               aSysFontData.hFont,
3073cdf0e10cSrcweir               nFallbacklevel);
3074cdf0e10cSrcweir 
3075cdf0e10cSrcweir     return aSysFontData;
3076cdf0e10cSrcweir }
3077cdf0e10cSrcweir 
3078cdf0e10cSrcweir //--------------------------------------------------------------------------
3079cdf0e10cSrcweir 
3080