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