bump product version to 6.4.0.3
[LibreOffice.git] / vcl / inc / unx / fontmanager.hxx
blob3c67adc70d8ffbd9a93431f6bafaffdee9ac8bfd
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_INC_FONTMANAGER_HXX
21 #define INCLUDED_VCL_INC_FONTMANAGER_HXX
23 #include <tools/fontenum.hxx>
24 #include <vcl/dllapi.h>
25 #include <vcl/glyphitem.hxx>
26 #include <vcl/timer.hxx>
27 #include <com/sun/star/lang/Locale.hpp>
28 #include <unx/fc_fontoptions.hxx>
30 #include <map>
31 #include <set>
32 #include <memory>
33 #include <vector>
34 #include <unordered_map>
37 * some words on metrics: every length returned by PrintFontManager and
38 * friends are PostScript afm style, that is they are 1/1000 font height
41 class FontSubsetInfo;
42 class FontConfigFontOptions;
43 class FontSelectPattern;
44 class GenericUnixSalData;
46 namespace psp {
47 class PPDParser;
49 typedef int fontID;
52 * the difference between FastPrintFontInfo and PrintFontInfo
53 * is that the information in FastPrintFontInfo can usually
54 * be gathered without opening either the font file, they are
55 * gathered from fonts.dir alone.
56 * if only FastPrintFontInfo is gathered and PrintFontInfo
57 * on demand and for less fonts, then performance in startup
58 * increases considerably
61 struct FastPrintFontInfo
63 fontID m_nID; // FontID
65 // font attributes
66 OUString m_aFamilyName;
67 OUString m_aStyleName;
68 std::vector< OUString > m_aAliases;
69 FontFamily m_eFamilyStyle;
70 FontItalic m_eItalic;
71 FontWidth m_eWidth;
72 FontWeight m_eWeight;
73 FontPitch m_ePitch;
74 rtl_TextEncoding m_aEncoding;
76 FastPrintFontInfo()
77 : m_nID(0)
78 , m_eFamilyStyle(FAMILY_DONTKNOW)
79 , m_eItalic(ITALIC_DONTKNOW)
80 , m_eWidth(WIDTH_DONTKNOW)
81 , m_eWeight(WEIGHT_DONTKNOW)
82 , m_ePitch(PITCH_DONTKNOW)
83 , m_aEncoding(RTL_TEXTENCODING_DONTKNOW)
87 struct PrintFontInfo : public FastPrintFontInfo
89 int m_nAscend;
90 int m_nDescend;
92 PrintFontInfo() :
93 FastPrintFontInfo(),
94 m_nAscend( 0 ),
95 m_nDescend( 0 )
99 // a class to manage printable fonts
101 class VCL_PLUGIN_PUBLIC PrintFontManager
103 struct PrintFont;
104 friend struct PrintFont;
106 struct PrintFont
108 // font attributes
109 OUString m_aFamilyName;
110 std::vector<OUString> m_aAliases;
111 OUString m_aPSName;
112 OUString m_aStyleName;
113 FontFamily m_eFamilyStyle;
114 FontItalic m_eItalic;
115 FontWidth m_eWidth;
116 FontWeight m_eWeight;
117 FontPitch m_ePitch;
118 rtl_TextEncoding m_aEncoding;
119 int m_nAscend;
120 int m_nDescend;
121 int m_nLeading;
122 int m_nXMin; // font bounding box
123 int m_nYMin;
124 int m_nXMax;
125 int m_nYMax;
127 int m_nDirectory; // atom containing system dependent path
128 OString m_aFontFile; // relative to directory
129 int m_nCollectionEntry; // 0 for regular fonts, 0 to ... for fonts stemming from collections
130 int m_nVariationEntry; // 0 for regular fonts, 0 to ... for fonts stemming from font variations
132 explicit PrintFont();
135 fontID m_nNextFontID;
136 std::unordered_map< fontID, std::unique_ptr<PrintFont> > m_aFonts;
137 // for speeding up findFontFileID
138 std::unordered_map< OString, std::set< fontID > >
139 m_aFontFileToFontID;
141 std::unordered_map< OString, int >
142 m_aDirToAtom;
143 std::unordered_map< int, OString > m_aAtomToDir;
144 int m_nNextDirAtom;
146 OString getFontFile(const PrintFont* pFont) const;
148 std::vector<std::unique_ptr<PrintFont>> analyzeFontFile(int nDirID, const OString& rFileName, const char *pFormat=nullptr) const;
149 static OUString convertSfntName( void* pNameRecord ); // actually a NameRecord* format font subsetting code
150 static void analyzeSfntFamilyName( void const * pTTFont, std::vector< OUString >& rnames ); // actually a TrueTypeFont* from font subsetting code
151 bool analyzeSfntFile(PrintFont* pFont) const;
152 // finds the font id for the nFaceIndex face in this font file
153 // There may be multiple font ids for font collections
154 fontID findFontFileID(int nDirID, const OString& rFile, int nFaceIndex, int nVariationIndex) const;
156 // There may be multiple font ids for font collections
157 std::vector<fontID> findFontFileIDs( int nDirID, const OString& rFile ) const;
159 static FontFamily matchFamilyName( const OUString& rFamily );
161 PrintFont* getFont( fontID nID ) const
163 auto it = m_aFonts.find( nID );
164 return it == m_aFonts.end() ? nullptr : it->second.get();
166 static void fillPrintFontInfo(PrintFont* pFont, FastPrintFontInfo& rInfo);
167 void fillPrintFontInfo( PrintFont* pFont, PrintFontInfo& rInfo ) const;
169 OString getDirectory( int nAtom ) const;
170 int getDirectoryAtom( const OString& rDirectory );
172 /* try to initialize fonts from libfontconfig
174 called from <code>initialize()</code>
176 static void initFontconfig();
177 void countFontconfigFonts( std::unordered_map<OString, int>& o_rVisitedPaths );
178 /* deinitialize fontconfig
180 static void deinitFontconfig();
182 /* register an application specific font directory for libfontconfig
184 since fontconfig is asked for font substitutes before OOo will check for font availability
185 and fontconfig will happily substitute fonts it doesn't know (e.g. "Arial Narrow" -> "DejaVu Sans Book"!)
186 it becomes necessary to tell the library about all the hidden font treasures
188 static void addFontconfigDir(const OString& rDirectory);
190 std::set<OString> m_aPreviousLangSupportRequests;
191 std::vector<OUString> m_aCurrentRequests;
192 Timer m_aFontInstallerTimer;
194 DECL_LINK( autoInstallFontLangSupport, Timer*, void );
195 PrintFontManager();
196 public:
197 ~PrintFontManager();
198 friend class ::GenericUnixSalData;
199 static PrintFontManager& get(); // one instance only
201 // There may be multiple font ids for font collections
202 std::vector<fontID> addFontFile( const OUString& rFileUrl );
204 void initialize();
206 // returns the ids of all managed fonts.
207 void getFontList( std::vector< fontID >& rFontIDs );
209 // get font info for a specific font
210 bool getFontInfo( fontID nFontID, PrintFontInfo& rInfo ) const;
211 // get fast font info for a specific font
212 bool getFontFastInfo( fontID nFontID, FastPrintFontInfo& rInfo ) const;
214 // routines to get font info in small pieces
216 // get a specific fonts PSName name
217 OUString getPSName( fontID nFontID ) const;
219 // get a specific fonts italic type
220 FontItalic getFontItalic( fontID nFontID ) const
222 PrintFont* pFont = getFont( nFontID );
223 return pFont ? pFont->m_eItalic : ITALIC_DONTKNOW;
226 // get a specific fonts weight type
227 FontWeight getFontWeight( fontID nFontID ) const
229 PrintFont* pFont = getFont( nFontID );
230 return pFont ? pFont->m_eWeight : WEIGHT_DONTKNOW;
233 // get a specific fonts system dependent filename
234 OString getFontFileSysPath( fontID nFontID ) const
236 return getFontFile( getFont( nFontID ) );
239 // get the ttc face number
240 int getFontFaceNumber( fontID nFontID ) const;
242 // get the ttc face variation
243 int getFontFaceVariation( fontID nFontID ) const;
245 // get a specific fonts ascend
246 int getFontAscend( fontID nFontID ) const;
248 // get a specific fonts descent
249 int getFontDescend( fontID nFontID ) const;
251 // get a fonts glyph bounding box
252 void getFontBoundingBox( fontID nFont, int& xMin, int& yMin, int& xMax, int& yMax );
254 // creates a new font subset of an existing SFNT font
255 // returns true in case of success, else false
256 // nFont: the font to be subsetted
257 // rOutFile: the file to put the new subset into;
258 // must be a valid osl file URL
259 // pGlyphIDs: input array of glyph ids for new font
260 // pNewEncoding: the corresponding encoding in the new font
261 // pWidths: output array of widths of requested glyphs
262 // nGlyphs: number of glyphs in arrays
263 // pCapHeight:: capital height of the produced font
264 // pXMin, pYMin, pXMax, pYMax: outgoing font bounding box
265 // TODO: callers of this method should use its FontSubsetInfo counterpart directly
266 bool createFontSubset( FontSubsetInfo&,
267 fontID nFont,
268 const OUString& rOutFile,
269 const sal_GlyphId* pGlyphIDs,
270 const sal_uInt8* pNewEncoding,
271 sal_Int32* pWidths,
272 int nGlyphs
274 void getGlyphWidths( fontID nFont,
275 bool bVertical,
276 std::vector< sal_Int32 >& rWidths,
277 std::map< sal_Unicode, sal_uInt32 >& rUnicodeEnc );
279 // font administration functions
281 /* system dependent font matching
284 <code>matchFont</code> matches a pattern of font characteristics
285 and returns the closest match if possible. If a match was found
286 the <code>FastPrintFontInfo</code> passed in as parameter
287 will be update to the found matching font.
288 </p>
290 implementation note: currently the function is only implemented
291 for fontconfig.
292 </p>
294 @param rInfo
295 out of the FastPrintFontInfo structure the following
296 fields will be used for the match:
297 <ul>
298 <li>family name</li>
299 <li>italic</li>
300 <li>width</li>
301 <li>weight</li>
302 <li>pitch</li>
303 </ul>
305 @param rLocale
306 if <code>rLocal</code> contains non empty strings the corresponding
307 locale will be used for font matching also; e.g. "Sans" can result
308 in different fonts in e.g. english and japanese
310 void matchFont( FastPrintFontInfo& rInfo, const css::lang::Locale& rLocale );
311 static std::unique_ptr<FontConfigFontOptions> getFontOptions( const FastPrintFontInfo&, int nSize);
313 void Substitute(FontSelectPattern &rPattern, OUString& rMissingCodes);
317 } // namespace
319 #endif // INCLUDED_VCL_INC_FONTMANAGER_HXX
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */