1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/config.h>
24 #include <o3tl/sorted_vector.hxx>
25 #include <tools/fontenum.hxx>
26 #include <vcl/dllapi.h>
27 #include <vcl/timer.hxx>
28 #include <com/sun/star/lang/Locale.hpp>
29 #include <unx/fc_fontoptions.hxx>
31 #include <font/PhysicalFontFace.hxx>
35 #include <string_view>
37 #include <unordered_map>
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
45 class FontConfigFontOptions
;
48 class FontSelectPattern
;
50 namespace vcl
{ struct NameRecord
; }
51 class GenericUnixSalData
;
58 // a class to manage printable fonts
60 class VCL_PLUGIN_PUBLIC PrintFontManager
63 friend struct PrintFont
;
65 struct VCL_DLLPRIVATE PrintFont
67 FontAttributes m_aFontAttributes
;
69 int m_nDirectory
; // atom containing system dependent path
70 OString m_aFontFile
; // relative to directory
71 int m_nCollectionEntry
; // 0 for regular fonts, 0 to ... for fonts stemming from collections
72 int m_nVariationEntry
; // 0 for regular fonts, 0 to ... for fonts stemming from font variations
78 std::unordered_map
< fontID
, PrintFont
> m_aFonts
;
79 // for speeding up findFontFileID
80 std::unordered_map
< OString
, o3tl::sorted_vector
< fontID
> >
83 std::unordered_map
< OString
, int >
85 std::unordered_map
< int, OString
> m_aAtomToDir
;
88 OString
getFontFile(const PrintFont
& rFont
) const;
90 std::vector
<PrintFont
> analyzeFontFile(int nDirID
, const OString
& rFileName
, const char *pFormat
=nullptr) const;
91 bool analyzeSfntFile(PrintFont
& rFont
) const;
92 // finds the font id for the nFaceIndex face in this font file
93 // There may be multiple font ids for font collections
94 fontID
findFontFileID(int nDirID
, const OString
& rFile
, int nFaceIndex
, int nVariationIndex
) const;
96 // There may be multiple font ids for font collections
97 std::vector
<fontID
> findFontFileIDs( int nDirID
, const OString
& rFile
) const;
99 static FontFamily
matchFamilyName( std::u16string_view rFamily
);
101 OString
getDirectory( int nAtom
) const;
102 int getDirectoryAtom( const OString
& rDirectory
);
104 /* try to initialize fonts from libfontconfig
106 called from <code>initialize()</code>
108 static void initFontconfig();
109 void countFontconfigFonts();
110 /* deinitialize fontconfig
112 static void deinitFontconfig();
114 /* register an application specific font directory for libfontconfig
116 since fontconfig is asked for font substitutes before OOo will check for font availability
117 and fontconfig will happily substitute fonts it doesn't know (e.g. "Arial Narrow" -> "DejaVu Sans Book"!)
118 it becomes necessary to tell the library about all the hidden font treasures
120 static void addFontconfigDir(const OString
& rDirectory
);
122 /* register an application specific font file for libfontconfig */
123 static void addFontconfigFile(const OString
& rFile
);
125 std::set
<OString
> m_aPreviousLangSupportRequests
;
126 std::vector
<OUString
> m_aCurrentRequests
;
127 Timer m_aFontInstallerTimer
;
129 DECL_DLLPRIVATE_LINK( autoInstallFontLangSupport
, Timer
*, void );
133 friend class ::GenericUnixSalData
;
134 static PrintFontManager
& get(); // one instance only
136 // There may be multiple font ids for font collections
137 std::vector
<fontID
> addFontFile( std::u16string_view rFileUrl
);
141 const PrintFont
* getFont( fontID nID
) const
143 auto it
= m_aFonts
.find( nID
);
144 return it
== m_aFonts
.end() ? nullptr : &it
->second
;
147 // returns the ids of all managed fonts.
148 void getFontList( std::vector
< fontID
>& rFontIDs
);
150 // routines to get font info in small pieces
152 // get a specific fonts system dependent filename
153 OString
getFontFileSysPath( fontID nFontID
) const
155 return getFontFile( *getFont( nFontID
) );
158 // get the ttc face number
159 int getFontFaceNumber( fontID nFontID
) const;
161 // get the ttc face variation
162 int getFontFaceVariation( fontID nFontID
) const;
164 // font administration functions
166 /* system dependent font matching
169 <code>matchFont</code> matches a pattern of font characteristics
170 and returns the closest match if possible. If a match was found
171 it will update rDFA to the found matching font.
174 implementation note: currently the function is only implemented
179 out of the FontAttributes structure the following
180 fields will be used for the match:
190 if <code>rLocal</code> contains non empty strings the corresponding
191 locale will be used for font matching also; e.g. "Sans" can result
192 in different fonts in e.g. english and japanese
194 bool matchFont(FontAttributes
& rDFA
, const css::lang::Locale
& rLocale
);
196 static std::unique_ptr
<FontConfigFontOptions
> getFontOptions(const FontAttributes
& rFontAttributes
, int nSize
);
198 void Substitute(vcl::font::FontSelectPattern
&rPattern
, OUString
& rMissingCodes
);
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */