bump product version to 4.2.0.1
[LibreOffice.git] / include / vcl / fontmanager.hxx
blob7f8511da21c6e5537c2b6a1f4c5df999d22a6ed0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_VCL_FONTMANAGER_HXX
21 #define INCLUDED_VCL_FONTMANAGER_HXX
23 #include <boost/unordered_map.hpp>
24 #include <map>
25 #include <list>
26 #include <set>
28 #include <vcl/dllapi.h>
29 #include <vcl/helper.hxx>
30 #include <vcl/timer.hxx>
31 #include <vcl/vclenum.hxx>
32 #include <com/sun/star/lang/Locale.hpp>
34 #include <vector>
36 #define ATOM_FAMILYNAME 2
37 #define ATOM_PSNAME 3
40 * some words on metrics: every length returned by PrintFontManager and
41 * friends are PostScript afm style, that is they are 1/1000 font height
44 // forward declarations
45 namespace utl { class MultiAtomProvider; } // see unotools/atom.hxx
46 class FontSubsetInfo;
47 class ImplFontOptions;
48 class FontSelectPattern;
50 namespace psp {
51 class PPDParser; // see ppdparser.hxx
53 namespace fonttype
55 enum type {
56 Unknown = 0,
57 Type1 = 1,
58 TrueType = 2,
59 Builtin = 3
64 * the difference between FastPrintFontInfo and PrintFontInfo
65 * is that the information in FastPrintFontInfo can usually
66 * be gathered without openening either the font file or
67 * an afm metric file. they are gathered from fonts.dir alone.
68 * if only FastPrintFontInfo is gathered and PrintFontInfo
69 * on demand and for less fonts, then performance in startup
70 * increases considerably
73 struct FastPrintFontInfo
75 fontID m_nID; // FontID
76 fonttype::type m_eType;
78 // font attributes
79 OUString m_aFamilyName;
80 OUString m_aStyleName;
81 std::list< OUString > m_aAliases;
82 FontFamily m_eFamilyStyle;
83 FontItalic m_eItalic;
84 FontWidth m_eWidth;
85 FontWeight m_eWeight;
86 FontPitch m_ePitch;
87 rtl_TextEncoding m_aEncoding;
88 bool m_bSubsettable;
89 bool m_bEmbeddable;
91 FastPrintFontInfo() :
92 m_nID( 0 ),
93 m_eType( fonttype::Unknown ),
94 m_eFamilyStyle( FAMILY_DONTKNOW ),
95 m_eItalic( ITALIC_DONTKNOW ),
96 m_eWidth( WIDTH_DONTKNOW ),
97 m_eWeight( WEIGHT_DONTKNOW ),
98 m_ePitch( PITCH_DONTKNOW ),
99 m_aEncoding( RTL_TEXTENCODING_DONTKNOW )
103 struct PrintFontInfo : public FastPrintFontInfo
105 int m_nAscend;
106 int m_nDescend;
107 int m_nLeading;
108 int m_nWidth;
110 PrintFontInfo() :
111 FastPrintFontInfo(),
112 m_nAscend( 0 ),
113 m_nDescend( 0 ),
114 m_nLeading( 0 ),
115 m_nWidth( 0 )
119 // the values are per thousand of the font size
120 // note: width, height contain advances, not bounding box
121 struct CharacterMetric
123 short int width, height;
125 CharacterMetric() : width( 0 ), height( 0 ) {}
126 bool operator==( const CharacterMetric& rOther ) const
127 { return rOther.width == width && rOther.height == height; }
128 bool operator!=( const CharacterMetric& rOther ) const
129 { return rOther.width != width || rOther.height != height; }
132 class FontCache;
134 // a class to manage printable fonts
135 // aims are type1 and truetype fonts
137 class FontCache;
139 class VCL_PLUGIN_PUBLIC PrintFontManager
141 struct PrintFont;
142 struct TrueTypeFontFile;
143 struct Type1FontFile;
144 struct BuiltinFont;
145 friend struct PrintFont;
146 friend struct TrueTypeFontFile;
147 friend struct Type1FontFile;
148 friend struct BuiltinFont;
149 friend class FontCache;
151 struct PrintFontMetrics
153 // character metrics are stored by the following keys:
154 // lower two bytes contain a sal_Unicode (a UCS2 character)
155 // upper byte contains: 0 for horizontal metric
156 // 1 for vertical metric
157 // highest byte: 0 for now
158 boost::unordered_map< int, CharacterMetric > m_aMetrics;
159 // contains the unicode blocks for which metrics were queried
160 // this implies that metrics should be queried in terms of
161 // unicode blocks. here a unicode block is identified
162 // by the upper byte of the UCS2 encoding.
163 // note that the corresponding bit should be set even
164 // if the font does not support a single character of that page
165 // this map shows, which pages were queried already
166 // if (like in AFM metrics) all metrics are queried in
167 // a single pass, then all bits should be set
168 char m_aPages[32];
170 boost::unordered_map< sal_Unicode, bool > m_bVerticalSubstitutions;
172 PrintFontMetrics() {}
174 bool isEmpty() const { return m_aMetrics.empty(); }
177 struct PrintFont
179 fonttype::type m_eType;
181 // font attributes
182 int m_nFamilyName; // atom
183 std::list< int > m_aAliases;
184 int m_nPSName; // atom
185 OUString m_aStyleName;
186 FontItalic m_eItalic;
187 FontWidth m_eWidth;
188 FontWeight m_eWeight;
189 FontPitch m_ePitch;
190 rtl_TextEncoding m_aEncoding;
191 bool m_bFontEncodingOnly; // set if font should be only accessed by builtin encoding
192 CharacterMetric m_aGlobalMetricX;
193 CharacterMetric m_aGlobalMetricY;
194 PrintFontMetrics* m_pMetrics;
195 int m_nAscend;
196 int m_nDescend;
197 int m_nLeading;
198 int m_nXMin; // font bounding box
199 int m_nYMin;
200 int m_nXMax;
201 int m_nYMax;
202 bool m_bHaveVerticalSubstitutedGlyphs;
203 bool m_bUserOverride;
205 std::map< sal_Unicode, sal_Int32 > m_aEncodingVector;
206 std::map< sal_Unicode, OString > m_aNonEncoded;
208 explicit PrintFont( fonttype::type eType );
209 virtual ~PrintFont();
210 virtual bool queryMetricPage( int nPage, utl::MultiAtomProvider* pProvider ) = 0;
212 bool readAfmMetrics( utl::MultiAtomProvider* pProvider, bool bFillEncodingvector, bool bOnlyGlobalAttributes );
215 struct Type1FontFile : public PrintFont
217 int m_nDirectory; // atom containing system dependent path
218 OString m_aFontFile; // relative to directory
219 OString m_aMetricFile; // dito
221 /* note: m_aFontFile and Metric file are not atoms
222 because they should be fairly unique */
224 Type1FontFile() : PrintFont( fonttype::Type1 ), m_nDirectory( 0 ) {}
225 virtual ~Type1FontFile();
226 virtual bool queryMetricPage( int nPage, utl::MultiAtomProvider* pProvider );
229 struct TrueTypeFontFile : public PrintFont
231 int m_nDirectory; // atom containing system dependent path
232 OString m_aFontFile; // relative to directory
233 int m_nCollectionEntry; // 0 for regular fonts, 0 to ... for fonts stemming from collections
234 unsigned int m_nTypeFlags; // copyright bits and PS-OpenType flag
236 TrueTypeFontFile();
237 virtual ~TrueTypeFontFile();
238 virtual bool queryMetricPage( int nPage, utl::MultiAtomProvider* pProvider );
241 struct BuiltinFont : public PrintFont
243 int m_nDirectory; // atom containing system dependent path
244 OString m_aMetricFile;
246 BuiltinFont() : PrintFont( fonttype::Builtin ) {}
247 virtual ~BuiltinFont();
248 virtual bool queryMetricPage( int nPage, utl::MultiAtomProvider* pProvider );
251 fontID m_nNextFontID;
252 boost::unordered_map< fontID, PrintFont* > m_aFonts;
253 boost::unordered_map< int, FontFamily > m_aFamilyTypes;
254 std::list< OUString > m_aPrinterDrivers;
255 std::list< OString > m_aFontDirectories;
256 std::list< int > m_aPrivateFontDirectories;
257 utl::MultiAtomProvider* m_pAtoms;
258 // for speeding up findFontFileID
259 boost::unordered_map< OString, std::set< fontID >, OStringHash >
260 m_aFontFileToFontID;
262 boost::unordered_map< OString, int, OStringHash >
263 m_aDirToAtom;
264 boost::unordered_map< int, OString > m_aAtomToDir;
265 int m_nNextDirAtom;
267 boost::unordered_multimap< OString, sal_Unicode, OStringHash >
268 m_aAdobenameToUnicode;
269 boost::unordered_multimap< sal_Unicode, OString >
270 m_aUnicodeToAdobename;
271 boost::unordered_multimap< sal_Unicode, sal_uInt8 > m_aUnicodeToAdobecode;
272 boost::unordered_multimap< sal_uInt8, sal_Unicode > m_aAdobecodeToUnicode;
274 mutable FontCache* m_pFontCache;
276 OString getAfmFile( PrintFont* pFont ) const;
277 OString getFontFile( PrintFont* pFont ) const;
279 bool analyzeFontFile( int nDirID, const OString& rFileName, std::list< PrintFont* >& rNewFonts, const char *pFormat=NULL ) const;
280 OUString convertTrueTypeName( void* pNameRecord ) const; // actually a NameRecord* formt font subsetting code
281 void analyzeTrueTypeFamilyName( void* pTTFont, std::list< OUString >& rnames ) const; // actually a TrueTypeFont* from font subsetting code
282 bool analyzeTrueTypeFile( PrintFont* pFont ) const;
283 // finds the font id for the nFaceIndex face in this font file
284 // There may be multiple font ids for TrueType collections
285 fontID findFontFileID( int nDirID, const OString& rFile, int nFaceIndex ) const;
287 // There may be multiple font ids for TrueType collections
288 std::vector<fontID> findFontFileIDs( int nDirID, const OString& rFile ) const;
290 bool knownFontFile( int nDirID, const OString& rFile ) const
292 return findFontFileID(nDirID, rFile, 0) != 0;
295 fontID findFontBuiltinID( int nPSNameAtom ) const;
297 FontFamily matchFamilyName( const OUString& rFamily ) const;
299 PrintFont* getFont( fontID nID ) const
301 boost::unordered_map< fontID, PrintFont* >::const_iterator it;
302 it = m_aFonts.find( nID );
303 return it == m_aFonts.end() ? NULL : it->second;
305 void fillPrintFontInfo( PrintFont* pFont, FastPrintFontInfo& rInfo ) const;
306 void fillPrintFontInfo( PrintFont* pFont, PrintFontInfo& rInfo ) const;
308 OString getDirectory( int nAtom ) const;
309 int getDirectoryAtom( const OString& rDirectory, bool bCreate = false );
311 /* try to initialize fonts from libfontconfig
313 called from <code>initialize()</code>
315 void initFontconfig();
316 void countFontconfigFonts( boost::unordered_map<OString, int, OStringHash>& o_rVisitedPaths );
317 /* deinitialize fontconfig
319 void deinitFontconfig();
321 /* register an application specific font directory for libfontconfig
323 since fontconfig is asked for font substitutes before OOo will check for font availability
324 and fontconfig will happily substitute fonts it doesn't know (e.g. "Arial Narrow" -> "DejaVu Sans Book"!)
325 it becomes necessary to tell the library about all the hidden font treasures
327 @returns
328 true if libfontconfig accepted the directory
329 false else (e.g. no libfontconfig found)
331 bool addFontconfigDir(const OString& rDirectory);
333 std::set<OString> m_aPreviousLangSupportRequests;
334 std::vector<OString> m_aCurrentRequests;
335 Timer m_aFontInstallerTimer;
337 DECL_LINK( autoInstallFontLangSupport, void* );
339 PrintFontManager();
340 ~PrintFontManager();
341 public:
342 static PrintFontManager& get(); // one instance only
344 // There may be multiple font ids for TrueType collections
345 std::vector<fontID> addFontFile( const OString& rFileName );
347 void initialize();
349 // returns the number of managed fonts
350 int getFontCount() const { return m_aFonts.size(); }
352 // caution: the getFontList* methods can change the font list on demand
353 // depending on the pParser argument. That is getFontCount() may
354 // return a larger value after getFontList()
356 // returns the ids of all managed fonts. on pParser != NULL
357 // all fonttype::Builtin type fonts are not listed
358 // which do not occur in the PPD of pParser
359 void getFontList( std::list< fontID >& rFontIDs, const PPDParser* pParser = NULL );
360 // get the font list and fast font info. see getFontList for pParser
361 void getFontListWithFastInfo( std::list< FastPrintFontInfo >& rFonts, const PPDParser* pParser = NULL );
363 // get font info for a specific font
364 bool getFontInfo( fontID nFontID, PrintFontInfo& rInfo ) const;
365 // get fast font info for a specific font
366 bool getFontFastInfo( fontID nFontID, FastPrintFontInfo& rInfo ) const;
368 // routines to get font info in small pieces
370 // get a specific fonts PSName name
371 const OUString& getPSName( fontID nFontID ) const;
373 // get a specific fonts family name aliases
374 void getFontFamilyAliases( fontID nFontID ) const;
376 // get a specific fonts type
377 fonttype::type getFontType( fontID nFontID ) const
379 PrintFont* pFont = getFont( nFontID );
380 return pFont ? pFont->m_eType : fonttype::Unknown;
383 // get a specific fonts italic type
384 FontItalic getFontItalic( fontID nFontID ) const
386 PrintFont* pFont = getFont( nFontID );
387 return pFont ? pFont->m_eItalic : ITALIC_DONTKNOW;
390 // get a specific fonts width type
391 FontWidth getFontWidth( fontID nFontID ) const
393 PrintFont* pFont = getFont( nFontID );
394 return pFont ? pFont->m_eWidth : WIDTH_DONTKNOW;
397 // get a specific fonts weight type
398 FontWeight getFontWeight( fontID nFontID ) const
400 PrintFont* pFont = getFont( nFontID );
401 return pFont ? pFont->m_eWeight : WEIGHT_DONTKNOW;
404 // get a specific fonts pitch type
405 FontPitch getFontPitch( fontID nFontID ) const
407 PrintFont* pFont = getFont( nFontID );
408 return pFont ? pFont->m_ePitch : PITCH_DONTKNOW;
411 // get a specific fonts encoding
412 rtl_TextEncoding getFontEncoding( fontID nFontID ) const
414 PrintFont* pFont = getFont( nFontID );
415 return pFont ? pFont->m_aEncoding : RTL_TEXTENCODING_DONTKNOW;
418 // should i only use font's builtin encoding ?
419 bool getUseOnlyFontEncoding( fontID nFontID ) const
421 PrintFont* pFont = getFont( nFontID );
422 return pFont ? pFont->m_bFontEncodingOnly : false;
425 // get a specific fonts system dependent filename
426 OString getFontFileSysPath( fontID nFontID ) const
428 return getFontFile( getFont( nFontID ) );
431 // get the ttc face number
432 int getFontFaceNumber( fontID nFontID ) const;
434 // get a specific fonts ascend
435 int getFontAscend( fontID nFontID ) const;
437 // get a specific fonts descent
438 int getFontDescend( fontID nFontID ) const;
440 // get a fonts glyph bounding box
441 bool getFontBoundingBox( fontID nFont, int& xMin, int& yMin, int& xMax, int& yMax );
443 // info whether an array of glyphs has vertical substitutions
444 void hasVerticalSubstitutions( fontID nFontID, const sal_Unicode* pCharacters,
445 int nCharacters, bool* pHasSubst ) const;
447 // get a specific fonts metrics
449 // get metrics for a sal_Unicode range
450 // the user is responsible to allocate pArray large enough
451 bool getMetrics( fontID nFontID, sal_Unicode minCharacter, sal_Unicode maxCharacter, CharacterMetric* pArray, bool bVertical = false ) const;
452 // get metrics for an array of sal_Unicode characters
453 // the user is responsible to allocate pArray large enough
454 bool getMetrics( fontID nFontID, const sal_Unicode* pString, int nLen, CharacterMetric* pArray, bool bVertical = false ) const;
456 // get encoding vector of font, currently only for Type1 and Builtin fonts
457 // returns NULL if encoding vector is empty or font is neither type1 or
458 // builtin; if ppNonEncoded is set and non encoded type1 glyphs exist
459 // then *ppNonEncoded is set to the mapping for nonencoded glyphs.
460 // the encoding vector contains -1 for non encoded glyphs
461 const std::map< sal_Unicode, sal_Int32 >* getEncodingMap( fontID nFontID, const std::map< sal_Unicode, OString >** ppNonEncoded ) const;
463 // evaluates copyright flags for TrueType fonts for printing/viewing
464 // type1 fonts do not have such a feature, so return for them is true
465 // returns true for builtin fonts (surprise!)
466 bool isFontDownloadingAllowedForPrinting( fontID nFont ) const;
468 // helper for type 1 fonts
469 std::list< OString > getAdobeNameFromUnicode( sal_Unicode aChar ) const;
471 std::pair< boost::unordered_multimap< sal_Unicode, sal_uInt8 >::const_iterator,
472 boost::unordered_multimap< sal_Unicode, sal_uInt8 >::const_iterator >
473 getAdobeCodeFromUnicode( sal_Unicode aChar ) const
475 return m_aUnicodeToAdobecode.equal_range( aChar );
477 std::list< sal_Unicode > getUnicodeFromAdobeName( const OString& rName ) const;
478 std::pair< boost::unordered_multimap< sal_uInt8, sal_Unicode >::const_iterator,
479 boost::unordered_multimap< sal_uInt8, sal_Unicode >::const_iterator >
480 getUnicodeFromAdobeCode( sal_uInt8 aChar ) const
482 return m_aAdobecodeToUnicode.equal_range( aChar );
485 // creates a new font subset of an existing TrueType font
486 // returns true in case of success, else false
487 // nFont: the font to be subsetted
488 // rOutFile: the file to put the new subset into;
489 // must be a valid osl file URL
490 // pGlyphIDs: input array of glyph ids for new font
491 // pNewEncoding: the corresponding encoding in the new font
492 // pWidths: output array of widths of requested glyphs
493 // nGlyphs: number of glyphs in arrays
494 // pCapHeight:: capital height of the produced font
495 // pXMin, pYMin, pXMax, pYMax: outgoing font bounding box
496 // TODO: callers of this method should use its FontSubsetInfo counterpart directly
497 bool createFontSubset( FontSubsetInfo&,
498 fontID nFont,
499 const OUString& rOutFile,
500 sal_Int32* pGlyphIDs,
501 sal_uInt8* pNewEncoding,
502 sal_Int32* pWidths,
503 int nGlyphs,
504 bool bVertical = false
506 void getGlyphWidths( fontID nFont,
507 bool bVertical,
508 std::vector< sal_Int32 >& rWidths,
509 std::map< sal_Unicode, sal_uInt32 >& rUnicodeEnc );
512 // font administration functions
514 /* system dependendent font matching
517 <code>matchFont</code> matches a pattern of font characteristics
518 and returns the closest match if possibe. If a match was found
519 the <code>FastPrintFontInfo</code> passed in as parameter
520 will be update to the found matching font.
521 </p>
523 implementation note: currently the function is only implemented
524 for fontconfig.
525 </p>
527 @param rInfo
528 out of the FastPrintFontInfo structure the following
529 fields will be used for the match:
530 <ul>
531 <li>family name</li>
532 <li>italic</li>
533 <li>width</li>
534 <li>weight</li>
535 <li>pitch</li>
536 </ul>
538 @param rLocale
539 if <code>rLocal</code> contains non empty strings the corresponding
540 locale will be used for font matching also; e.g. "Sans" can result
541 in different fonts in e.g. english and japanese
543 @returns
544 true if a match was found
545 false else
547 bool matchFont( FastPrintFontInfo& rInfo, const com::sun::star::lang::Locale& rLocale );
548 ImplFontOptions* getFontOptions( const FastPrintFontInfo&, int nSize, void (*subcallback)(void*)) const;
550 bool Substitute( FontSelectPattern &rPattern, OUString& rMissingCodes );
552 int FreeTypeCharIndex( void *pFace, sal_uInt32 aChar );
555 } // namespace
557 #endif // INCLUDED_VCL_FONTMANAGER_HXX
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */