xref: /trunk/main/unotools/inc/unotools/fontcfg.hxx (revision bae3752e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _UNOTOOLS_FONTCFG_HXX
24 #define _UNOTOOLS_FONTCFG_HXX
25 
26 #include <unotools/unotoolsdllapi.h>
27 #include <tools/string.hxx>
28 #include <tools/fontenum.hxx>
29 #include <com/sun/star/lang/Locale.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 
33 #include <hash_map>
34 #include <hash_set>
35 #include <vector>
36 
37 namespace com {
38 namespace sun {
39 namespace star {
40 namespace lang {
41 
42 // equality operator needed for hash_map;
43 // (-> why does this need to be in the namespace of Locale ? g++ fails to compile else)
operator ==(const com::sun::star::lang::Locale & rLeft,const com::sun::star::lang::Locale & rRight)44 inline bool operator==( const com::sun::star::lang::Locale& rLeft, const com::sun::star::lang::Locale& rRight )
45 {
46     return
47         rLeft.Language.equals( rRight.Language ) &&
48         rLeft.Country.equals( rRight.Country )  &&
49         rLeft.Variant.equals( rRight.Variant )
50         ;
51 }
52 }}}}
53 
54 namespace utl
55 {
56 
57 struct LocaleHash
58 {
operator ()utl::LocaleHash59     size_t operator()( const com::sun::star::lang::Locale& rLocale ) const
60     {
61         return
62             (size_t)rLocale.Language.hashCode() ^
63             (size_t)rLocale.Country.hashCode()  ^
64             (size_t)rLocale.Variant.hashCode();
65     }
66 };
67 
68 class UNOTOOLS_DLLPUBLIC DefaultFontConfiguration
69 {
70     com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
71             m_xConfigProvider;
72     com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >
73             m_xConfigAccess;
74 
75     struct LocaleAccess
76     {
77         // the real string used in the configuration
78         // used to get rid of upper/lower case problems
79         rtl::OUString aConfigLocaleString;
80         // xAccess is mutable to be able to be filled on demand
81         mutable com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > xAccess;
82     };
83 
84     std::hash_map< com::sun::star::lang::Locale,
85                    LocaleAccess,
86                    utl::LocaleHash >
87             m_aConfig;
88 
89     rtl::OUString tryLocale( const com::sun::star::lang::Locale& rLocale, const rtl::OUString& rType ) const;
90 
91     DefaultFontConfiguration();
92     public:
93     ~DefaultFontConfiguration();
94 
95     static DefaultFontConfiguration* get();
96 
97     rtl::OUString getDefaultFont( const com::sun::star::lang::Locale& rLocale, int nType ) const;
98     rtl::OUString getUserInterfaceFont( const com::sun::star::lang::Locale& rLocale ) const;
99 };
100 
101 // IMPL_FONT_ATTR_DEFAULT       - Default-Font like Andale Sans UI, Palace Script, Albany, Thorndale, Cumberland, ...
102 // IMPL_FONT_ATTR_STANDARD      - Standard-Font like Arial, Times, Courier, ...
103 // IMPL_FONT_ATTR_NORMAL        - normal Font for writing text like Arial, Verdana, Arial Narrow, Trebuchet, Times, Courier, ...
104 // IMPL_FONT_ATTR_SYMBOL        - Font with symbols
105 // IMPL_FONT_ATTR_DECORATIVE    - Readable and normally used for drawings
106 // IMPL_FONT_ATTR_SPECIAL       - very special design
107 // IMPL_FONT_ATTR_TITLING       - only uppercase characters
108 // IMPL_FONT_ATTR_FULL          - Font with normally all characters
109 // IMPL_FONT_ATTR_CAPITALS     - only uppercase characters, but lowercase characters smaller as the uppercase characters
110 // IMPL_FONT_ATTR_TYPEWRITER    - like a typewriter: Courier, ...
111 // IMPL_FONT_ATTR_SCRIPT        - Handwriting or Script
112 // IMPL_FONT_ATTR_HANDWRITING   - More Handwriting with normal letters
113 // IMPL_FONT_ATTR_CHANCERY      - Like Zapf Chancery
114 // IMPL_FONT_ATTR_COMIC         - Like Comic Sans MS
115 // IMPL_FONT_ATTR_BRUSHSCRIPT   - More Script
116 // IMPL_FONT_ATTR_OTHERSTYLE    - OldStyle, ... so negativ points
117 #define IMPL_FONT_ATTR_DEFAULT       ((sal_uLong)0x00000001)
118 #define IMPL_FONT_ATTR_STANDARD      ((sal_uLong)0x00000002)
119 #define IMPL_FONT_ATTR_NORMAL        ((sal_uLong)0x00000004)
120 #define IMPL_FONT_ATTR_SYMBOL        ((sal_uLong)0x00000008)
121 #define IMPL_FONT_ATTR_FIXED         ((sal_uLong)0x00000010)
122 #define IMPL_FONT_ATTR_SANSSERIF     ((sal_uLong)0x00000020)
123 #define IMPL_FONT_ATTR_SERIF         ((sal_uLong)0x00000040)
124 #define IMPL_FONT_ATTR_DECORATIVE    ((sal_uLong)0x00000080)
125 #define IMPL_FONT_ATTR_SPECIAL       ((sal_uLong)0x00000100)
126 #define IMPL_FONT_ATTR_ITALIC        ((sal_uLong)0x00000200)
127 #define IMPL_FONT_ATTR_TITLING       ((sal_uLong)0x00000400)
128 #define IMPL_FONT_ATTR_CAPITALS      ((sal_uLong)0x00000800)
129 #define IMPL_FONT_ATTR_CJK           ((sal_uLong)0x00001000)
130 #define IMPL_FONT_ATTR_CJK_JP        ((sal_uLong)0x00002000)
131 #define IMPL_FONT_ATTR_CJK_SC        ((sal_uLong)0x00004000)
132 #define IMPL_FONT_ATTR_CJK_TC        ((sal_uLong)0x00008000)
133 #define IMPL_FONT_ATTR_CJK_KR        ((sal_uLong)0x00010000)
134 #define IMPL_FONT_ATTR_CTL           ((sal_uLong)0x00020000)
135 #define IMPL_FONT_ATTR_NONELATIN     ((sal_uLong)0x00040000)
136 #define IMPL_FONT_ATTR_FULL          ((sal_uLong)0x00080000)
137 #define IMPL_FONT_ATTR_OUTLINE       ((sal_uLong)0x00100000)
138 #define IMPL_FONT_ATTR_SHADOW        ((sal_uLong)0x00200000)
139 #define IMPL_FONT_ATTR_ROUNDED       ((sal_uLong)0x00400000)
140 #define IMPL_FONT_ATTR_TYPEWRITER    ((sal_uLong)0x00800000)
141 #define IMPL_FONT_ATTR_SCRIPT        ((sal_uLong)0x01000000)
142 #define IMPL_FONT_ATTR_HANDWRITING   ((sal_uLong)0x02000000)
143 #define IMPL_FONT_ATTR_CHANCERY      ((sal_uLong)0x04000000)
144 #define IMPL_FONT_ATTR_COMIC         ((sal_uLong)0x08000000)
145 #define IMPL_FONT_ATTR_BRUSHSCRIPT   ((sal_uLong)0x10000000)
146 #define IMPL_FONT_ATTR_GOTHIC        ((sal_uLong)0x20000000)
147 #define IMPL_FONT_ATTR_SCHOOLBOOK    ((sal_uLong)0x40000000)
148 #define IMPL_FONT_ATTR_OTHERSTYLE    ((sal_uLong)0x80000000)
149 
150 #define IMPL_FONT_ATTR_CJK_ALLLANG   (IMPL_FONT_ATTR_CJK_JP | IMPL_FONT_ATTR_CJK_SC | IMPL_FONT_ATTR_CJK_TC | IMPL_FONT_ATTR_CJK_KR)
151 #define IMPL_FONT_ATTR_ALLSCRIPT     (IMPL_FONT_ATTR_SCRIPT | IMPL_FONT_ATTR_HANDWRITING | IMPL_FONT_ATTR_CHANCERY | IMPL_FONT_ATTR_COMIC | IMPL_FONT_ATTR_BRUSHSCRIPT)
152 #define IMPL_FONT_ATTR_ALLSUBSCRIPT  (IMPL_FONT_ATTR_HANDWRITING | IMPL_FONT_ATTR_CHANCERY | IMPL_FONT_ATTR_COMIC | IMPL_FONT_ATTR_BRUSHSCRIPT)
153 #define IMPL_FONT_ATTR_ALLSERIFSTYLE (IMPL_FONT_ATTR_ALLSCRIPT |\
154                                       IMPL_FONT_ATTR_SANSSERIF | IMPL_FONT_ATTR_SERIF |\
155                                       IMPL_FONT_ATTR_FIXED | IMPL_FONT_ATTR_ITALIC |\
156                                       IMPL_FONT_ATTR_GOTHIC | IMPL_FONT_ATTR_SCHOOLBOOK |\
157                                       IMPL_FONT_ATTR_SHADOW | IMPL_FONT_ATTR_OUTLINE)
158 
159 struct UNOTOOLS_DLLPUBLIC FontNameAttr
160 {
161     String								Name;
162     ::std::vector< String >				Substitutions;
163     ::std::vector< String >				MSSubstitutions;
164     ::std::vector< String >				PSSubstitutions;
165     ::std::vector< String >				HTMLSubstitutions;
166     FontWeight							Weight;
167     FontWidth							Width;
168     unsigned long						Type; // bitfield of IMPL_FONT_ATTR_*
169 };
170 
171 class UNOTOOLS_DLLPUBLIC FontSubstConfiguration
172 {
173 private:
174     com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
175             m_xConfigProvider;
176     com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >
177             m_xConfigAccess;
178     struct LocaleSubst
179     {
180         rtl::OUString                           aConfigLocaleString;
181         mutable bool                            bConfigRead;
182         // note: aSubstAttributes must be sorted alphabetically by Name
183         // searches on the substitutes are done with Name as key, where
184         // a minimal match is sufficient (that is e.g. "Thorndale" will match
185         // "Thorndale BlaBlub"). Also names must be lower case.
186         mutable std::vector< FontNameAttr >     aSubstAttributes;
187 
LocaleSubstutl::FontSubstConfiguration::LocaleSubst188         LocaleSubst() : bConfigRead( false ) {}
189     };
190     std::hash_map< com::sun::star::lang::Locale, LocaleSubst, utl::LocaleHash > m_aSubst;
191 	typedef std::hash_set< rtl::OUString, rtl::OUStringHash > UniqueSubstHash;
192 	mutable UniqueSubstHash maSubstHash;
193 
194 
195     void fillSubstVector( const com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > xFont,
196                           const rtl::OUString& rType,
197                           std::vector< String >& rSubstVector ) const;
198     FontWeight getSubstWeight( const com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > xFont,
199                           const rtl::OUString& rType ) const;
200     FontWidth getSubstWidth( const com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > xFont,
201                              const rtl::OUString& rType ) const;
202     unsigned long getSubstType( const com::sun::star::uno::Reference< com::sun::star::container::XNameAccess > xFont,
203                                 const rtl::OUString& rType ) const;
204     void readLocaleSubst( const com::sun::star::lang::Locale& rLocale ) const;
205     FontSubstConfiguration();
206 public:
207     ~FontSubstConfiguration();
208 
209     static FontSubstConfiguration* get();
210 
211     const FontNameAttr* getSubstInfo(
212                                      const String& rFontName,
213                                      const com::sun::star::lang::Locale& rLocale =
214                                      com::sun::star::lang::Locale( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "en" ) ),
215                                                                    rtl::OUString(),
216                                                                    rtl::OUString() )
217                                      ) const;
218     static void getMapName( const String& rOrgName, String& rShortName, String& rFamilyName, FontWeight& rWeight, FontWidth& rWidth, sal_uLong& rType );
219 };
220 
221 } // namespace utl
222 
223 #endif // _UNOTOOLS_FONTCFG_HXX
224