LibreOffice Module vcl (master) 1
fontmanager.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include <sal/config.h>
23#include <config_options.h>
24
25#include <tools/fontenum.hxx>
26#include <vcl/dllapi.h>
27#include <vcl/timer.hxx>
28#include <com/sun/star/lang/Locale.hpp>
30
31#include <glyphid.hxx>
32
33#include <map>
34#include <set>
35#include <memory>
36#include <string_view>
37#include <vector>
38#include <unordered_map>
39
40/*
41 * some words on metrics: every length returned by PrintFontManager and
42 * friends are PostScript afm style, that is they are 1/1000 font height
43 */
44
45class FontAttributes;
46class FontSubsetInfo;
48namespace vcl::font
49{
50class FontSelectPattern;
51}
53
54namespace psp {
55class PPDParser;
56
57typedef int fontID;
58
59/*
60 * the difference between FastPrintFontInfo and PrintFontInfo
61 * is that the information in FastPrintFontInfo can usually
62 * be gathered from fontconfig results.
63 * if only FastPrintFontInfo is gathered and PrintFontInfo
64 * on demand and for less fonts, then performance in startup
65 * increases considerably
66 */
67
69{
70 fontID m_nID; // FontID
71
72 // font attributes
73 OUString m_aFamilyName;
74 OUString m_aStyleName;
75 std::vector< OUString > m_aAliases;
81 rtl_TextEncoding m_aEncoding;
82
84 : m_nID(0)
90 , m_aEncoding(RTL_TEXTENCODING_DONTKNOW)
91 {}
92};
93
95{
98
101 m_nAscend( 0 ),
102 m_nDescend( 0 )
103 {}
104};
105
106// a class to manage printable fonts
107
109{
110 struct PrintFont;
111 friend struct PrintFont;
112
114 {
115 // font attributes
117 std::vector<OUString> m_aAliases;
118 OUString m_aPSName;
119 OUString m_aStyleName;
125 rtl_TextEncoding m_aEncoding;
129 int m_nXMin; // font bounding box
133
134 int m_nDirectory; // atom containing system dependent path
135 OString m_aFontFile; // relative to directory
136 int m_nCollectionEntry; // 0 for regular fonts, 0 to ... for fonts stemming from collections
137 int m_nVariationEntry; // 0 for regular fonts, 0 to ... for fonts stemming from font variations
138
139 explicit PrintFont();
140 };
141
143 std::unordered_map< fontID, PrintFont > m_aFonts;
144 // for speeding up findFontFileID
145 std::unordered_map< OString, std::set< fontID > >
147
148 std::unordered_map< OString, int >
150 std::unordered_map< int, OString > m_aAtomToDir;
152
153 OString getFontFile(const PrintFont& rFont) const;
154
155 std::vector<PrintFont> analyzeFontFile(int nDirID, const OString& rFileName, const char *pFormat=nullptr) const;
156 static OUString convertSfntName( void* pNameRecord ); // actually a NameRecord* format font subsetting code
157 static void analyzeSfntFamilyName( void const * pTTFont, std::vector< OUString >& rnames ); // actually a TrueTypeFont* from font subsetting code
158 bool analyzeSfntFile(PrintFont& rFont) const;
159 // finds the font id for the nFaceIndex face in this font file
160 // There may be multiple font ids for font collections
161 fontID findFontFileID(int nDirID, const OString& rFile, int nFaceIndex, int nVariationIndex) const;
162
163 // There may be multiple font ids for font collections
164 std::vector<fontID> findFontFileIDs( int nDirID, const OString& rFile ) const;
165
166 static FontFamily matchFamilyName( std::u16string_view rFamily );
167
168 const PrintFont* getFont( fontID nID ) const
169 {
170 auto it = m_aFonts.find( nID );
171 return it == m_aFonts.end() ? nullptr : &it->second;
172 }
174 {
175 auto it = m_aFonts.find( nID );
176 return it == m_aFonts.end() ? nullptr : &it->second;
177 }
178 static void fillPrintFontInfo(const PrintFont& rFont, FastPrintFontInfo& rInfo);
179 void fillPrintFontInfo( PrintFont& rFont, PrintFontInfo& rInfo ) const;
180
181 OString getDirectory( int nAtom ) const;
182 int getDirectoryAtom( const OString& rDirectory );
183
184 /* try to initialize fonts from libfontconfig
185
186 called from <code>initialize()</code>
187 */
188 static void initFontconfig();
190 /* deinitialize fontconfig
191 */
192 static void deinitFontconfig();
193
194 /* register an application specific font directory for libfontconfig
195
196 since fontconfig is asked for font substitutes before OOo will check for font availability
197 and fontconfig will happily substitute fonts it doesn't know (e.g. "Arial Narrow" -> "DejaVu Sans Book"!)
198 it becomes necessary to tell the library about all the hidden font treasures
199 */
200 static void addFontconfigDir(const OString& rDirectory);
201
203 std::vector<OUString> m_aCurrentRequests;
205
206 DECL_DLLPRIVATE_LINK( autoInstallFontLangSupport, Timer*, void );
208public:
210 friend class ::GenericUnixSalData;
211 static PrintFontManager& get(); // one instance only
212
213 // There may be multiple font ids for font collections
214 std::vector<fontID> addFontFile( std::u16string_view rFileUrl );
215
217
218 // returns the ids of all managed fonts.
219 void getFontList( std::vector< fontID >& rFontIDs );
220
221 // get font info for a specific font
222 bool getFontInfo( fontID nFontID, PrintFontInfo& rInfo ) const;
223 // get fast font info for a specific font
224 bool getFontFastInfo( fontID nFontID, FastPrintFontInfo& rInfo ) const;
225
226 // routines to get font info in small pieces
227
228 // get a specific fonts PSName name
229 OUString getPSName( fontID nFontID );
230
231 // get a specific fonts italic type
233 {
234 const PrintFont* pFont = getFont( nFontID );
235 return pFont ? pFont->m_eItalic : ITALIC_DONTKNOW;
236 }
237
238 // get a specific fonts weight type
240 {
241 const PrintFont* pFont = getFont( nFontID );
242 return pFont ? pFont->m_eWeight : WEIGHT_DONTKNOW;
243 }
244
245 // get a specific fonts system dependent filename
246 OString getFontFileSysPath( fontID nFontID ) const
247 {
248 return getFontFile( *getFont( nFontID ) );
249 }
250
251 // get the ttc face number
252 int getFontFaceNumber( fontID nFontID ) const;
253
254 // get the ttc face variation
255 int getFontFaceVariation( fontID nFontID ) const;
256
257 // get a specific fonts ascend
258 int getFontAscend( fontID nFontID );
259
260 // get a specific fonts descent
261 int getFontDescend( fontID nFontID );
262
263 // get a fonts glyph bounding box
264 void getFontBoundingBox( fontID nFont, int& xMin, int& yMin, int& xMax, int& yMax );
265
266 // creates a new font subset of an existing SFNT font
267 // returns true in case of success, else false
268 // nFont: the font to be subsetted
269 // rOutFile: the file to put the new subset into;
270 // must be a valid osl file URL
271 // pGlyphIDs: input array of glyph ids for new font
272 // pNewEncoding: the corresponding encoding in the new font
273 // pWidths: output array of widths of requested glyphs
274 // nGlyphs: number of glyphs in arrays
275 // pCapHeight:: capital height of the produced font
276 // pXMin, pYMin, pXMax, pYMax: outgoing font bounding box
277 // TODO: callers of this method should use its FontSubsetInfo counterpart directly
279 fontID nFont,
280 const OUString& rOutFile,
281 const sal_GlyphId* pGlyphIDs,
282 const sal_uInt8* pNewEncoding,
283 sal_Int32* pWidths,
284 int nGlyphs
285 );
287 bool bVertical,
288 std::vector< sal_Int32 >& rWidths,
289 std::map< sal_Unicode, sal_uInt32 >& rUnicodeEnc );
290
291 // font administration functions
292
293 /* system dependent font matching
294
295 <p>
296 <code>matchFont</code> matches a pattern of font characteristics
297 and returns the closest match if possible. If a match was found
298 the <code>FastPrintFontInfo</code> passed in as parameter
299 will be update to the found matching font.
300 </p>
301 <p>
302 implementation note: currently the function is only implemented
303 for fontconfig.
304 </p>
305
306 @param rInfo
307 out of the FastPrintFontInfo structure the following
308 fields will be used for the match:
309 <ul>
310 <li>family name</li>
311 <li>italic</li>
312 <li>width</li>
313 <li>weight</li>
314 <li>pitch</li>
315 </ul>
316
317 @param rLocale
318 if <code>rLocal</code> contains non empty strings the corresponding
319 locale will be used for font matching also; e.g. "Sans" can result
320 in different fonts in e.g. english and japanese
321 */
322 void matchFont( FastPrintFontInfo& rInfo, const css::lang::Locale& rLocale );
323
324 static std::unique_ptr<FontConfigFontOptions> getFontOptions(const FontAttributes& rFontAttributes, int nSize);
325
326 void Substitute(vcl::font::FontSelectPattern &rPattern, OUString& rMissingCodes);
327
328};
329
330} // namespace
331
332/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: timer.hxx:27
void getFontBoundingBox(fontID nFont, int &xMin, int &yMin, int &xMax, int &yMax)
FontWeight getFontWeight(fontID nFontID) const
void getGlyphWidths(fontID nFont, bool bVertical, std::vector< sal_Int32 > &rWidths, std::map< sal_Unicode, sal_uInt32 > &rUnicodeEnc)
static void initFontconfig()
std::set< OString > m_aPreviousLangSupportRequests
OString getFontFileSysPath(fontID nFontID) const
int getFontFaceVariation(fontID nFontID) const
DECL_DLLPRIVATE_LINK(autoInstallFontLangSupport, Timer *, void)
bool analyzeSfntFile(PrintFont &rFont) const
static void fillPrintFontInfo(const PrintFont &rFont, FastPrintFontInfo &rInfo)
static OUString convertSfntName(void *pNameRecord)
static void addFontconfigDir(const OString &rDirectory)
std::unordered_map< fontID, PrintFont > m_aFonts
bool getFontInfo(fontID nFontID, PrintFontInfo &rInfo) const
static FontFamily matchFamilyName(std::u16string_view rFamily)
static void deinitFontconfig()
void getFontList(std::vector< fontID > &rFontIDs)
std::vector< fontID > addFontFile(std::u16string_view rFileUrl)
const PrintFont * getFont(fontID nID) const
std::vector< OUString > m_aCurrentRequests
int getDirectoryAtom(const OString &rDirectory)
FontItalic getFontItalic(fontID nFontID) const
void fillPrintFontInfo(PrintFont &rFont, PrintFontInfo &rInfo) const
std::unordered_map< OString, std::set< fontID > > m_aFontFileToFontID
OString getDirectory(int nAtom) const
bool createFontSubset(FontSubsetInfo &, fontID nFont, const OUString &rOutFile, const sal_GlyphId *pGlyphIDs, const sal_uInt8 *pNewEncoding, sal_Int32 *pWidths, int nGlyphs)
int getFontDescend(fontID nFontID)
static void analyzeSfntFamilyName(void const *pTTFont, std::vector< OUString > &rnames)
void matchFont(FastPrintFontInfo &rInfo, const css::lang::Locale &rLocale)
OUString getPSName(fontID nFontID)
static std::unique_ptr< FontConfigFontOptions > getFontOptions(const FontAttributes &rFontAttributes, int nSize)
std::vector< PrintFont > analyzeFontFile(int nDirID, const OString &rFileName, const char *pFormat=nullptr) const
OString getFontFile(const PrintFont &rFont) const
bool getFontFastInfo(fontID nFontID, FastPrintFontInfo &rInfo) const
fontID findFontFileID(int nDirID, const OString &rFile, int nFaceIndex, int nVariationIndex) const
std::vector< fontID > findFontFileIDs(int nDirID, const OString &rFile) const
std::unordered_map< int, OString > m_aAtomToDir
std::unordered_map< OString, int > m_aDirToAtom
int getFontAscend(fontID nFontID)
PrintFont * getFont(fontID nID)
int getFontFaceNumber(fontID nFontID) const
void Substitute(vcl::font::FontSelectPattern &rPattern, OUString &rMissingCodes)
static PrintFontManager & get()
#define VCL_DLLPRIVATE
Definition: dllapi.h:31
#define VCL_PLUGIN_PUBLIC
Definition: dllapi.h:40
FontPitch
PITCH_DONTKNOW
FontItalic
ITALIC_DONTKNOW
FontWidth
WIDTH_DONTKNOW
FontFamily
FAMILY_DONTKNOW
WEIGHT_DONTKNOW
sal_uInt16 sal_GlyphId
Definition: glyphid.hxx:24
int fontID
Definition: fontmanager.hxx:55
FontWeight
A PhysicalFontFaceCollection is created by a PhysicalFontCollection and becomes invalid when original...
rtl_TextEncoding m_aEncoding
Definition: fontmanager.hxx:81
std::vector< OUString > m_aAliases
Definition: fontmanager.hxx:75
std::vector< OUString > m_aAliases
unsigned char sal_uInt8