~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/kfontinst/lib/WritingSystems.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * KFontInst - KDE Font Installer
 
3
 *
 
4
 * Copyright 2003-2009 Craig Drummond <craig@kde.org>
 
5
 *
 
6
 * ----
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; see the file COPYING.  If not, write to
 
20
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
21
 * Boston, MA 02110-1301, USA.
 
22
 */
 
23
 
 
24
#include <KDE/KGlobal>
 
25
#include <QtGui/QFontDatabase>
 
26
#include <fontconfig/fontconfig.h>
 
27
#include "WritingSystems.h"
 
28
 
 
29
namespace KFI
 
30
{
 
31
 
 
32
K_GLOBAL_STATIC(WritingSystems, theInstance)
 
33
 
 
34
WritingSystems * WritingSystems::instance()
 
35
{
 
36
    return theInstance;
 
37
}
 
38
 
 
39
static const struct
 
40
{
 
41
    QFontDatabase::WritingSystem ws;
 
42
    const FcChar8                *lang;
 
43
} constLanguageForWritingSystem[]=
 
44
{
 
45
    { QFontDatabase::Latin, (const FcChar8 *)"en" },
 
46
    { QFontDatabase::Greek, (const FcChar8 *)"el" },
 
47
    { QFontDatabase::Cyrillic, (const FcChar8 *)"ru" },
 
48
    { QFontDatabase::Armenian, (const FcChar8 *)"hy" },
 
49
    { QFontDatabase::Hebrew, (const FcChar8 *)"he" },
 
50
    { QFontDatabase::Arabic, (const FcChar8 *)"ar" },
 
51
    { QFontDatabase::Syriac, (const FcChar8 *)"syr" },
 
52
    { QFontDatabase::Thaana, (const FcChar8 *)"div" },
 
53
    { QFontDatabase::Devanagari, (const FcChar8 *)"hi" },
 
54
    { QFontDatabase::Bengali, (const FcChar8 *)"bn" },
 
55
    { QFontDatabase::Gurmukhi, (const FcChar8 *)"pa" },
 
56
    { QFontDatabase::Gujarati, (const FcChar8 *)"gu" },
 
57
    { QFontDatabase::Oriya, (const FcChar8 *)"or" },
 
58
    { QFontDatabase::Tamil, (const FcChar8 *)"ta" },
 
59
    { QFontDatabase::Telugu, (const FcChar8 *)"te" },
 
60
    { QFontDatabase::Kannada, (const FcChar8 *)"kn" },
 
61
    { QFontDatabase::Malayalam, (const FcChar8 *)"ml" },
 
62
    { QFontDatabase::Sinhala, (const FcChar8 *)"si" },
 
63
    { QFontDatabase::Thai, (const FcChar8 *)"th" },
 
64
    { QFontDatabase::Lao, (const FcChar8 *)"lo" },
 
65
    { QFontDatabase::Tibetan, (const FcChar8 *)"bo" },
 
66
    { QFontDatabase::Myanmar, (const FcChar8 *)"my" },
 
67
    { QFontDatabase::Georgian, (const FcChar8 *)"ka" },
 
68
    { QFontDatabase::Khmer, (const FcChar8 *)"km" },
 
69
    { QFontDatabase::SimplifiedChinese, (const FcChar8 *)"zh-cn" },
 
70
    { QFontDatabase::TraditionalChinese, (const FcChar8 *)"zh-tw" },
 
71
    { QFontDatabase::Japanese, (const FcChar8 *)"ja" },
 
72
    { QFontDatabase::Korean, (const FcChar8 *)"ko" },
 
73
    { QFontDatabase::Vietnamese, (const FcChar8 *)"vi" },
 
74
    { QFontDatabase::Other, NULL },
 
75
 
 
76
    // The following is only used to save writing system data for disabled fonts...
 
77
    { QFontDatabase::Telugu, (const FcChar8 *)"Qt-Telugu" },
 
78
    { QFontDatabase::Kannada, (const FcChar8 *)"Qt-Kannada" },
 
79
    { QFontDatabase::Malayalam, (const FcChar8 *)"Qt-Malayalam" },
 
80
    { QFontDatabase::Sinhala, (const FcChar8 *)"Qt-Sinhala" },
 
81
    { QFontDatabase::Myanmar, (const FcChar8 *)"Qt-Myanmar" },
 
82
    { QFontDatabase::Ogham, (const FcChar8 *)"Qt-Ogham" },
 
83
    { QFontDatabase::Runic, (const FcChar8 *)"Qt-Runic" },
 
84
 
 
85
    { QFontDatabase::Any, NULL }
 
86
};
 
87
 
 
88
 
 
89
inline qulonglong toBit(QFontDatabase::WritingSystem ws)
 
90
{
 
91
    return ((qulonglong)1) << (int)ws;
 
92
}
 
93
 
 
94
//.........the following section is inspired by qfontdatabase_x11.cpp / loadFontConfig
 
95
 
 
96
// Unfortunately FontConfig doesn't know about some languages. We have to test these through the
 
97
// charset. The lists below contain the systems where we need to do this.
 
98
static const struct
 
99
{
 
100
    QFontDatabase::WritingSystem ws;
 
101
    ushort                       ch;
 
102
} sampleCharForWritingSystem[] =
 
103
{
 
104
    { QFontDatabase::Telugu, 0xc15 },
 
105
    { QFontDatabase::Kannada, 0xc95 },
 
106
    { QFontDatabase::Malayalam, 0xd15 },
 
107
    { QFontDatabase::Sinhala, 0xd9a },
 
108
    { QFontDatabase::Myanmar, 0x1000 },
 
109
    { QFontDatabase::Ogham, 0x1681 },
 
110
    { QFontDatabase::Runic, 0x16a0 },
 
111
    { QFontDatabase::Any, 0x0 }
 
112
};
 
113
 
 
114
qulonglong WritingSystems::get(FcPattern *pat) const
 
115
{
 
116
    qulonglong ws(0);
 
117
    FcLangSet  *langset(0);
 
118
 
 
119
    if (FcResultMatch==FcPatternGetLangSet(pat, FC_LANG, 0, &langset))
 
120
    {
 
121
        for (int i = 0; constLanguageForWritingSystem[i].lang; ++i)
 
122
            if (FcLangDifferentLang!=FcLangSetHasLang(langset, constLanguageForWritingSystem[i].lang))
 
123
                ws|=toBit(constLanguageForWritingSystem[i].ws);
 
124
    }
 
125
    else
 
126
        ws|=toBit(QFontDatabase::Other);
 
127
 
 
128
    FcCharSet *cs(0);
 
129
 
 
130
    if (FcResultMatch == FcPatternGetCharSet(pat, FC_CHARSET, 0, &cs))
 
131
    {
 
132
        // some languages are not supported by FontConfig, we rather check the
 
133
        // charset to detect these
 
134
        for (int i = 0; QFontDatabase::Any!=sampleCharForWritingSystem[i].ws; ++i)
 
135
            if (FcCharSetHasChar(cs, sampleCharForWritingSystem[i].ch))
 
136
                ws|=toBit(sampleCharForWritingSystem[i].ws);
 
137
    }
 
138
 
 
139
    return ws;
 
140
}
 
141
//.........
 
142
 
 
143
qulonglong WritingSystems::get(const QStringList &langs) const
 
144
{
 
145
    qulonglong ws(0);
 
146
 
 
147
    QStringList::ConstIterator it(langs.begin()),
 
148
                               end(langs.end());
 
149
 
 
150
    for(; it!=end; ++it)
 
151
        ws|=itsMap[*it];
 
152
 
 
153
    return ws;
 
154
}
 
155
 
 
156
QStringList WritingSystems::getLangs(qulonglong ws) const
 
157
{
 
158
    QMap<QString, qulonglong>::ConstIterator wit(itsMap.begin()),
 
159
                                             wend(itsMap.end());
 
160
    QStringList                              systems;
 
161
 
 
162
    for(; wit!=wend; ++wit)
 
163
        if(ws&wit.value())
 
164
            systems+=wit.key();
 
165
    return systems;
 
166
}
 
167
 
 
168
WritingSystems::WritingSystems()
 
169
{
 
170
    for(int i=0; QFontDatabase::Any!=constLanguageForWritingSystem[i].ws; ++i)
 
171
        if(constLanguageForWritingSystem[i].lang)
 
172
            itsMap[(const char *)constLanguageForWritingSystem[i].lang]=
 
173
                ((qulonglong)1)<<constLanguageForWritingSystem[i].ws;
 
174
}
 
175
 
 
176
}