summaryrefslogtreecommitdiffstats
path: root/include/unotools/fontcfg.hxx
blob: 1f77d484531a7a3f4a21c24e3d4f4b2b3a7cc985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#ifndef INCLUDED_UNOTOOLS_FONTCFG_HXX
#define INCLUDED_UNOTOOLS_FONTCFG_HXX

#include <config_options.h>
#include <unotools/unotoolsdllapi.h>
#include <tools/fontenum.hxx>
#include <com/sun/star/uno/Reference.hxx>
#include <o3tl/typed_flags_set.hxx>
#include <i18nlangtag/languagetag.hxx>

#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace com::sun::star::container { class XNameAccess; }
namespace com::sun::star::lang { class XMultiServiceFactory; }

enum class DefaultFontType;

// If you think the below concept of classifying a font (typeface) as possibly being one or several
// of "Default", "Standard", and "Normal", etc, is madness, you are probably right. On the other
// hand we have in officecfg/registry/data/org/openoffice/VCL.xcu carefully (or not) assigned to
// each font mentioned in that file what its attributes are, so it is quite possible that touching
// this would cause a regression that nobody would notice until after many years.

// Note that the bit flags must match the entries in the pAttribNames array in
// unotools/source/config/fontcfg.cxx.

enum class ImplFontAttrs : sal_uInt32
{
    None          = 0x00000000,
    Default       = 0x00000001, ///< Default-Font like Andale Sans UI, Palace Script, Albany, Thorndale, Cumberland, ...
    Standard      = 0x00000002, ///< Standard-Font like Arial, Times, Courier, ...
    Normal        = 0x00000004, ///< Normal Font for writing text like Arial, Verdana, Arial Narrow, Trebuchet, Times, Courier, ...
    Symbol        = 0x00000008, ///< Font with symbols
    Fixed         = 0x00000010,
    SansSerif     = 0x00000020,
    Serif         = 0x00000040,
    Decorative    = 0x00000080, ///< Readable and normally used for drawings
    Special       = 0x00000100, ///< Very special design
    Italic        = 0x00000200,
    Titling       = 0x00000400, ///< Only uppercase characters
    Capitals      = 0x00000800, ///< Only uppercase characters, but lowercase characters smaller as the uppercase characters
    CJK           = 0x00001000,
    CJK_JP        = 0x00002000,
    CJK_SC        = 0x00004000,
    CJK_TC        = 0x00008000,
    CJK_KR        = 0x00010000,
    CTL           = 0x00020000,
    NoneLatin     = 0x00040000,
    Full          = 0x00080000, ///< Font with normally all characters
    Outline       = 0x00100000,
    Shadow        = 0x00200000,
    Rounded       = 0x00400000,
    Typewriter    = 0x00800000, ///< Like a typewriter: Courier, ...
    Script        = 0x01000000, ///< Handwriting or Script
    Handwriting   = 0x02000000, ///< More Handwriting with normal letters
    Chancery      = 0x04000000, ///< Like Zapf Chancery
    Comic         = 0x08000000, ///< Like Comic Sans MS
    BrushScript   = 0x10000000, ///< More Script
    Gothic        = 0x20000000,
    Schoolbook    = 0x40000000,
    OtherStyle    = 0x80000000, ///< OldStyle, ... so negative points
    CJK_AllLang   = CJK_JP | CJK_SC | CJK_TC | CJK_KR,
    AllScript     = Script | Handwriting | Chancery | Comic | BrushScript,
    AllSubscript  = Handwriting | Chancery | Comic | BrushScript,
    AllSerifStyle = AllScript | SansSerif | Serif | Fixed | Italic | Gothic | Schoolbook | Shadow | Outline,
};
namespace o3tl
{
    template<> struct typed_flags<ImplFontAttrs> : is_typed_flags<ImplFontAttrs, 0xffffffff> {};
}


namespace utl
{

class UNOTOOLS_DLLPUBLIC DefaultFontConfiguration
{
    css::uno::Reference< css::lang::XMultiServiceFactory >
            m_xConfigProvider;
    css::uno::Reference< css::container::XNameAccess >
            m_xConfigAccess;

    struct LocaleAccess
    {
        // the real string used in the configuration
        // used to get rid of upper/lower case problems
        OUString aConfigLocaleString;
        // xAccess is mutable to be able to be filled on demand
        mutable css::uno::Reference< css::container::XNameAccess > xAccess;
    };

    std::unordered_map< OUString, LocaleAccess > m_aConfig;

    OUString tryLocale( const OUString& rBcp47, const OUString& rType ) const;

    public:
    DefaultFontConfiguration();
    ~DefaultFontConfiguration();

    static DefaultFontConfiguration& get();

    OUString getDefaultFont( const LanguageTag& rLanguageTag, DefaultFontType nType ) const;
    OUString getUserInterfaceFont( const LanguageTag& rLanguageTag ) const;
};

struct UNOTOOLS_DLLPUBLIC FontNameAttr
{
    OUString                            Name;
    ::std::vector< OUString >           Substitutions;
    ::std::vector< OUString >           MSSubstitutions;
    FontWeight                          Weight;
    FontWidth                           Width;
    ImplFontAttrs                       Type;
};

class UNLESS_MERGELIBS(UNOTOOLS_DLLPUBLIC) FontSubstConfiguration
{
private:
    css::uno::Reference< css::lang::XMultiServiceFactory >
            m_xConfigProvider;
    css::uno::Reference< css::container::XNameAccess >
            m_xConfigAccess;
    struct LocaleSubst
    {
        OUString                                aConfigLocaleString;
        mutable bool                            bConfigRead;
        // note: aSubstAttributes must be sorted alphabetically by Name
        // searches on the substitutes are done with Name as key, where
        // a minimal match is sufficient (that is e.g. "Thorndale" will match
        // "Thorndale BlaBlub"). Also names must be lower case.
        mutable std::vector< FontNameAttr >     aSubstAttributes;

        LocaleSubst() : bConfigRead( false ) {}
    };
    std::unordered_map< OUString, LocaleSubst > m_aSubst;
    typedef std::unordered_set< OUString > UniqueSubstHash;
    mutable UniqueSubstHash maSubstHash;
    LanguageTag maLanguageTag;

    void fillSubstVector( const css::uno::Reference< css::container::XNameAccess >& rFont,
                          const OUString& rType,
                          std::vector< OUString >& rSubstVector ) const;
    FontWeight getSubstWeight( const css::uno::Reference< css::container::XNameAccess >& rFont,
                          const OUString& rType ) const;
    FontWidth getSubstWidth( const css::uno::Reference< css::container::XNameAccess >& rFont,
                             const OUString& rType ) const;
    ImplFontAttrs getSubstType( const css::uno::Reference< css::container::XNameAccess >& rFont,
                                const OUString& rType ) const;
    void readLocaleSubst( const OUString& rBcp47 ) const;
public:
    FontSubstConfiguration();
    ~FontSubstConfiguration();

    static FontSubstConfiguration& get();

    const FontNameAttr* getSubstInfo( const OUString& rFontName ) const;
    static void getMapName( const OUString& rOrgName, OUString& rShortName, OUString& rFamilyName, FontWeight& rWeight, FontWidth& rWidth, ImplFontAttrs& rType );
};

} // namespace utl

#endif // INCLUDED_UNOTOOLS_FONTCFG_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */