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 <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>
29 
30 #include <map>
31 #include <set>
32 #include <memory>
33 #include <vector>
34 #include <unordered_map>
35 
36 /*
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
39  */
40 
41 class FontSubsetInfo;
43 class FontSelectPattern;
44 class GenericUnixSalData;
45 
46 namespace psp {
47 class PPDParser;
48 
49 typedef int fontID;
50 
51 /*
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
59  */
60 
62 {
63  fontID m_nID; // FontID
64 
65  // font attributes
66  OUString m_aFamilyName;
67  OUString m_aStyleName;
68  std::vector< OUString > m_aAliases;
74  rtl_TextEncoding m_aEncoding;
75 
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)
84  {}
85 };
86 
88 {
89  int m_nAscend;
91 
94  m_nAscend( 0 ),
95  m_nDescend( 0 )
96  {}
97 };
98 
99 // a class to manage printable fonts
100 
102 {
103  struct PrintFont;
104  friend struct PrintFont;
105 
106  struct PrintFont
107  {
108  // font attributes
109  OUString m_aFamilyName;
110  std::vector<OUString> m_aAliases;
111  OUString m_aPSName;
112  OUString m_aStyleName;
118  rtl_TextEncoding m_aEncoding;
122  int m_nXMin; // font bounding box
123  int m_nYMin;
124  int m_nXMax;
125  int m_nYMax;
126 
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
131 
132  explicit PrintFont();
133  };
134 
136  std::unordered_map< fontID, std::unique_ptr<PrintFont> > m_aFonts;
137  // for speeding up findFontFileID
138  std::unordered_map< OString, std::set< fontID > >
140 
141  std::unordered_map< OString, int >
143  std::unordered_map< int, OString > m_aAtomToDir;
145 
146  OString getFontFile(const PrintFont* pFont) const;
147 
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;
155 
156  // There may be multiple font ids for font collections
157  std::vector<fontID> findFontFileIDs( int nDirID, const OString& rFile ) const;
158 
159  static FontFamily matchFamilyName( const OUString& rFamily );
160 
161  PrintFont* getFont( fontID nID ) const
162  {
163  auto it = m_aFonts.find( nID );
164  return it == m_aFonts.end() ? nullptr : it->second.get();
165  }
166  static void fillPrintFontInfo(PrintFont* pFont, FastPrintFontInfo& rInfo);
167  void fillPrintFontInfo( PrintFont* pFont, PrintFontInfo& rInfo ) const;
168 
169  OString getDirectory( int nAtom ) const;
170  int getDirectoryAtom( const OString& rDirectory );
171 
172  /* try to initialize fonts from libfontconfig
173 
174  called from <code>initialize()</code>
175  */
176  static void initFontconfig();
177  void countFontconfigFonts( std::unordered_map<OString, int>& o_rVisitedPaths );
178  /* deinitialize fontconfig
179  */
180  static void deinitFontconfig();
181 
182  /* register an application specific font directory for libfontconfig
183 
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
187  */
188  static void addFontconfigDir(const OString& rDirectory);
189 
191  std::vector<OUString> m_aCurrentRequests;
193 
194  DECL_LINK( autoInstallFontLangSupport, Timer*, void );
196 public:
197  ~PrintFontManager();
198  friend class ::GenericUnixSalData;
199  static PrintFontManager& get(); // one instance only
200 
201  // There may be multiple font ids for font collections
202  std::vector<fontID> addFontFile( const OUString& rFileUrl );
203 
204  void initialize();
205 
206  // returns the ids of all managed fonts.
207  void getFontList( std::vector< fontID >& rFontIDs );
208 
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;
213 
214  // routines to get font info in small pieces
215 
216  // get a specific fonts PSName name
217  OUString getPSName( fontID nFontID ) const;
218 
219  // get a specific fonts italic type
220  FontItalic getFontItalic( fontID nFontID ) const
221  {
222  PrintFont* pFont = getFont( nFontID );
223  return pFont ? pFont->m_eItalic : ITALIC_DONTKNOW;
224  }
225 
226  // get a specific fonts weight type
227  FontWeight getFontWeight( fontID nFontID ) const
228  {
229  PrintFont* pFont = getFont( nFontID );
230  return pFont ? pFont->m_eWeight : WEIGHT_DONTKNOW;
231  }
232 
233  // get a specific fonts system dependent filename
234  OString getFontFileSysPath( fontID nFontID ) const
235  {
236  return getFontFile( getFont( nFontID ) );
237  }
238 
239  // get the ttc face number
240  int getFontFaceNumber( fontID nFontID ) const;
241 
242  // get the ttc face variation
243  int getFontFaceVariation( fontID nFontID ) const;
244 
245  // get a specific fonts ascend
246  int getFontAscend( fontID nFontID ) const;
247 
248  // get a specific fonts descent
249  int getFontDescend( fontID nFontID ) const;
250 
251  // get a fonts glyph bounding box
252  void getFontBoundingBox( fontID nFont, int& xMin, int& yMin, int& xMax, int& yMax );
253 
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
273  );
274  void getGlyphWidths( fontID nFont,
275  bool bVertical,
276  std::vector< sal_Int32 >& rWidths,
277  std::map< sal_Unicode, sal_uInt32 >& rUnicodeEnc );
278 
279  // font administration functions
280 
281  /* system dependent font matching
282 
283  <p>
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>
289  <p>
290  implementation note: currently the function is only implemented
291  for fontconfig.
292  </p>
293 
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>
304 
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
309  */
310  void matchFont( FastPrintFontInfo& rInfo, const css::lang::Locale& rLocale );
311  static std::unique_ptr<FontConfigFontOptions> getFontOptions( const FastPrintFontInfo&, int nSize);
312 
313  void Substitute(FontSelectPattern &rPattern, OUString& rMissingCodes);
314 
315 };
316 
317 } // namespace
318 
319 #endif // INCLUDED_VCL_INC_FONTMANAGER_HXX
320 
321 /* 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:74
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:68
std::unordered_map< OString, std::set< fontID > > m_aFontFileToFontID
int fontID
Definition: fontmanager.hxx:47
Definition: timer.hxx:26
FontItalic