summaryrefslogtreecommitdiff
path: root/vcl/workben/listfonts.cxx
blob: 60b3209683e281faf410f5f2183aefb9cea693bb (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
 */

#include <osl/file.hxx>
#include <osl/process.h>
#include <rtl/textenc.h>
#include <sal/main.h>
#include <comphelper/processfactory.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <tools/diagnose_ex.h>
#include <tools/degree.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <i18nlangtag/mslangid.hxx>

#include <vcl/font/Feature.hxx>
#include <vcl/metric.hxx>
#include <vcl/svapp.hxx>
#include <vcl/vclmain.hxx>
#include <vcl/wrkwin.hxx>

#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>

#include <iostream>
#include <fstream>
#include <string>
#include <string_view>

namespace
{
OUString GetOctetTextEncodingName(sal_uInt16 encoding)
{
    switch (encoding)
    {
        case RTL_TEXTENCODING_APPLE_ARABIC:
            return "Arabic (Apple Macintosh)";

        case RTL_TEXTENCODING_IBM_864:
            return "Arabic (DOS/OS2-864)";

        case RTL_TEXTENCODING_ISO_8859_6:
            return "Arabic (ISO-8859-6)";

        case RTL_TEXTENCODING_MS_1256:
            return "Arabic (Windows-1256)";

        case RTL_TEXTENCODING_IBM_775:
            return "Baltic (DOS/OS2-775)";

        case RTL_TEXTENCODING_ISO_8859_4:
            return "Baltic (ISO-8859-4)";

        case RTL_TEXTENCODING_MS_1257:
            return "Baltic (Windows-1257)";

        case RTL_TEXTENCODING_APPLE_CENTEURO:
            return "Central European (Apple Macintosh)";

        case RTL_TEXTENCODING_APPLE_CROATIAN:
            return "Central European (Apple Macintosh/Croatian)";

        case RTL_TEXTENCODING_APPLE_ROMANIAN:
            return "Central European (Apple Macintosh/Romanian)";

        case RTL_TEXTENCODING_IBM_852:
            return "Central European (DOS/OS2-852)";

        case RTL_TEXTENCODING_ISO_8859_2:
            return "Central European (ISO-8859-2)";

        case RTL_TEXTENCODING_ISO_8859_10:
            return "Central European (ISO-8859-10)";

        case RTL_TEXTENCODING_ISO_8859_13:
            return "Central European (ISO-8859-13)";

        case RTL_TEXTENCODING_MS_1250:
            return "Central European (Windows-1250/WinLatin 2)";

        case RTL_TEXTENCODING_APPLE_CHINSIMP:
            return "Chinese Simplified (Apple Macintosh)";

        case RTL_TEXTENCODING_EUC_CN:
            return "Chinese Simplified (EUC-CN)";

        case RTL_TEXTENCODING_GB_2312:
            return "Chinese Simplified (GB-2312)";

        case RTL_TEXTENCODING_GBK:
            return "Chinese Simplified (GBK/GB-2312-80)";

        case RTL_TEXTENCODING_ISO_2022_CN:
            return "Chinese Simplified (ISO-2022-CN)";

        case RTL_TEXTENCODING_MS_936:
            return "Chinese Simplified (Windows-936)";

        case RTL_TEXTENCODING_GB_18030:
            return "Chinese Simplified (GB-18030)";

        case RTL_TEXTENCODING_APPLE_CHINTRAD:
            return "Chinese Traditional (Apple Macintosh)";

        case RTL_TEXTENCODING_BIG5:
            return "Chinese Traditional (BIG5)";

        case RTL_TEXTENCODING_EUC_TW:
            return "Chinese Traditional (EUC-TW)";

        case RTL_TEXTENCODING_GBT_12345:
            return "Chinese Traditional (GBT-12345)";

        case RTL_TEXTENCODING_MS_950:
            return "Chinese Traditional (Windows-950)";

        case RTL_TEXTENCODING_BIG5_HKSCS:
            return "Chinese Traditional (BIG5-HKSCS)";

        case RTL_TEXTENCODING_APPLE_CYRILLIC:
            return "Cyrillic (Apple Macintosh)";

        case RTL_TEXTENCODING_APPLE_UKRAINIAN:
            return "Cyrillic (Apple Macintosh/Ukrainian)";

        case RTL_TEXTENCODING_IBM_855:
            return "Cyrillic (DOS/OS2-855)";

        case RTL_TEXTENCODING_IBM_866:
            return "Cyrillic (DOS/OS2-866/Russian)";

        case RTL_TEXTENCODING_ISO_8859_5:
            return "Cyrillic (ISO-8859-5)";

        case RTL_TEXTENCODING_KOI8_R:
            return "Cyrillic (KOI8-R)";

        case RTL_TEXTENCODING_KOI8_U:
            return "Cyrillic (KOI8-U)";

        case RTL_TEXTENCODING_MS_1251:
            return "Cyrillic (Windows-1251)";

        case RTL_TEXTENCODING_APPLE_GREEK:
            return "Greek (Apple Macintosh)";

        case RTL_TEXTENCODING_IBM_737:
            return "Greek (DOS/OS2-737)";

        case RTL_TEXTENCODING_IBM_869:
            return "Greek (DOS/OS2-869/Modern)";

        case RTL_TEXTENCODING_ISO_8859_7:
            return "Greek (ISO-8859-7)";

        case RTL_TEXTENCODING_MS_1253:
            return "Greek (Windows-1253)";

        case RTL_TEXTENCODING_APPLE_HEBREW:
            return "Hebrew (Apple Macintosh)";

        case RTL_TEXTENCODING_IBM_862:
            return "Hebrew (DOS/OS2-862)";

        case RTL_TEXTENCODING_ISO_8859_8:
            return "Hebrew (ISO-8859-8)";

        case RTL_TEXTENCODING_MS_1255:
            return "Hebrew (Windows-1255)";

        case RTL_TEXTENCODING_APPLE_KOREAN:
            return "Korean (Apple Macintosh)";

        case RTL_TEXTENCODING_EUC_KR:
            return "Korean (EUC-KR)";

        case RTL_TEXTENCODING_ISO_2022_KR:
            return "Korean (ISO-2022-KR)";

        case RTL_TEXTENCODING_MS_949:
            return "Korean (Windows-Wansung-949)";

        case RTL_TEXTENCODING_MS_1361:
            return "Korean (Windows-Johab-1361)";

        case RTL_TEXTENCODING_ISO_8859_3:
            return "Latin 3 (ISO-8859-3)";

        case RTL_TEXTENCODING_ISCII_DEVANAGARI:
            return "Indian (ISCII Devanagari)";

        case RTL_TEXTENCODING_APPLE_JAPANESE:
            return "Japanese (Apple Macintosh)";

        case RTL_TEXTENCODING_EUC_JP:
            return "Japanese (EUC-JP)";

        case RTL_TEXTENCODING_ISO_2022_JP:
            return "Japanese (ISO-2022-JP)";

        case RTL_TEXTENCODING_SHIFT_JIS:
            return "Japanese (Shift-JIS)";

        case RTL_TEXTENCODING_MS_932:
            return "Japanese (Windows-932)";

        case RTL_TEXTENCODING_SYMBOL:
            return "Symbol";

        case RTL_TEXTENCODING_APPLE_THAI:
            return "Thai (Apple Macintosh)";

        case RTL_TEXTENCODING_MS_874:
            return "Thai (Dos/Windows-874)";

        case RTL_TEXTENCODING_TIS_620:
            return "Thai (TIS 620)";

        case RTL_TEXTENCODING_APPLE_TURKISH:
            return "Turkish (Apple Macintosh)";

        case RTL_TEXTENCODING_IBM_857:
            return "Turkish (DOS/OS2-857)";

        case RTL_TEXTENCODING_ISO_8859_9:
            return "Turkish (ISO-8859-9)";

        case RTL_TEXTENCODING_MS_1254:
            return "Turkish (Windows-1254)";

        case RTL_TEXTENCODING_UTF7:
            return "Unicode (UTF-7)";

        case RTL_TEXTENCODING_UTF8:
            return "Unicode (UTF-8)";

        case RTL_TEXTENCODING_JAVA_UTF8:
            return "Unicode (Java's modified UTF-8)";

        case RTL_TEXTENCODING_MS_1258:
            return "Vietnamese (Windows-1258)";

        case RTL_TEXTENCODING_APPLE_ROMAN:
            return "Western (Apple Macintosh)";

        case RTL_TEXTENCODING_APPLE_ICELAND:
            return "Western (Apple Macintosh/Icelandic)";

        case RTL_TEXTENCODING_ASCII_US:
            return "Western (ASCII/US)";

        case RTL_TEXTENCODING_IBM_437:
            return "Western (DOS/OS2-437/US)";

        case RTL_TEXTENCODING_IBM_850:
            return "Western (DOS/OS2-850/International)";

        case RTL_TEXTENCODING_IBM_860:
            return "Western (DOS/OS2-860/Portuguese)";

        case RTL_TEXTENCODING_IBM_861:
            return "Western (DOS/OS2-861/Icelandic)";

        case RTL_TEXTENCODING_IBM_863:
            return "Western (DOS/OS2-863/Canadian-French)";

        case RTL_TEXTENCODING_IBM_865:
            return "Western (DOS/OS2-865/Nordic)";

        case RTL_TEXTENCODING_ISO_8859_1:
            return "Western (ISO-8859-1)";

        case RTL_TEXTENCODING_ISO_8859_14:
            return "Western (ISO-8859-14)";

        case RTL_TEXTENCODING_ISO_8859_15:
            return "Western (ISO-8859-15/EURO)";

        case RTL_TEXTENCODING_MS_1252:
            return "Western (Window-1252/WinLatin 1)";

        case RTL_TEXTENCODING_UCS4:
            return "UCS4";

        case RTL_TEXTENCODING_UCS2:
            return "UCS2 (aka Unicode)";

        default:
        {
            OUString sUnknown = "Unknown (0x" + OUString::number(encoding, 16) + ")";
            return sUnknown;
        }
    }
}

class ListFontsWin : public WorkWindow
{
public:
    explicit ListFontsWin()
        : WorkWindow(nullptr, WB_HIDE)
    {
    }
};

class ListFonts : public Application
{
public:
    virtual int Main() override;

private:
    static void showHelp()
    {
        std::cerr << "Usage: listfonts --help | FILE | -v FILE\n";
        std::cerr << "Lists the current fonts installed on the system.\n";
        std::cerr << "To show the font features of each font, use -v before FILE.\n";
        std::cerr << "If outputting to stdout, use -- for FILE.\n";
        std::exit(0);
    }

    void Init() override;
    void DeInit() override;

    css::uno::Reference<css::lang::XMultiServiceFactory> xServiceManager;
    bool mbStdOut = false;
    bool mbShowFeatures = false;
    OUString maFilename;
};

int ListFonts::Main()
{
    try
    {
        VclPtrInstance<ListFontsWin> pWin;
        OutputDevice* pOutDev = pWin->GetOutDev();

        std::streambuf* coutbuf = nullptr;
        std::fstream out;

        if (!mbStdOut)
        {
            std::u16string_view filenamev = maFilename;
            std::string filename(filenamev.begin(), filenamev.end());

            out.open(filename, std::ios::out | std::ios::trunc);

            coutbuf = std::cout.rdbuf();
            std::cout.rdbuf(out.rdbuf());
        }

        for (int i = 0; i < pOutDev->GetFontFaceCollectionCount(); i++)
        {
            // note: to get the correct font metrics, you actually have to get the font metric from the
            // system, and *then* you must set it as the current font of OutputDevice... then you need
            // to get the font metric (which corrects a variety of things like the orientation, line
            // height, slant, etc. - including converting from logical coords to device coords)

            FontMetric aSystemFont = pOutDev->GetFontMetricFromCollection(i);
            pOutDev->SetFont(aSystemFont);

            FontMetric aFont = pOutDev->GetFontMetric();

            std::cout << aFont.GetFamilyName() << "\n\tFamily type: " << aFont.GetFamilyType()
                      << "\n\tStyle name: " << aFont.GetStyleName()
                      << "\n\tWeight: " << aFont.GetWeight() << "\n\tItalic: " << aFont.GetItalic()
                      << "\n\tPitch: " << aFont.GetPitch()
                      << "\n\tWidth type: " << aFont.GetWidthType()
                      << "\n\tAlignment: " << aFont.GetAlignment()
                      << "\n\tCharset: " << GetOctetTextEncodingName(aFont.GetCharSet())
                      << "\n\tSymbol font? " << (aFont.IsSymbolFont() ? "yes" : "no")
                      << "\n\tAscent: " << aFont.GetAscent()
                      << "\n\tDescent: " << aFont.GetDescent()
                      << "\n\tInternal leading: " << aFont.GetInternalLeading()
                      << "\n\tExternal leading: " << aFont.GetExternalLeading()
                      << "\n\tLine height: " << aFont.GetLineHeight()
                      << "\n\tSlant: " << aFont.GetSlant()
                      << "\n\tBullet offset: " << aFont.GetBulletOffset()
                      << "\n\tFullstop centered? " << (aFont.IsFullstopCentered() ? "yes" : "no")
                      << "\n\tOrientation: " << toDegrees(aFont.GetOrientation())
                      << " degrees\n\tQuality: " << aFont.GetQuality() << "\n";

            if (mbShowFeatures)
            {
                std::vector<vcl::font::Feature> features;
                pOutDev->GetFontFeatures(features);

                for (auto const& feature : features)
                {
                    std::ios init(nullptr);
                    init.copyfmt(std::cout);

                    std::cout << "\t"
                              << (feature.m_eType == vcl::font::FeatureType::OpenType ? "OpenType"
                                                                                      : "Graphite")
                              << " Feature:\n\t\tId = { feature code = "
                              << vcl::font::featureCodeAsString(feature.m_aID.m_aFeatureCode)
                              << ", script code = "
                              << vcl::font::featureCodeAsString(feature.m_aID.m_aScriptCode)
                              << ", language code = "
                              << vcl::font::featureCodeAsString(feature.m_aID.m_aLanguageCode)
                              << " }\n";

                    std::cout << "\t\tDescription: " << feature.m_aDefinition.getDescription()
                              << "\n";
                    std::cout << "\t\tType: "
                              << (feature.m_aDefinition.getType()
                                          == vcl::font::FeatureParameterType::BOOL
                                      ? "BOOL"
                                      : "ENUM")
                              << "\n";

                    std::cout.copyfmt(init);

                    if (feature.m_aDefinition.getType() == vcl::font::FeatureParameterType::ENUM)
                    {
                        for (auto const& param : feature.m_aDefinition.getEnumParameters())
                        {
                            std::cout << "\t\t\t" << param.getDescription() << ": "
                                      << param.getCode() << "\n";
                        }
                    }

                    std::cout << "\t\tDefault: 0x" << std::hex << feature.m_aDefinition.getDefault()
                              << "\n";

                    std::cout.copyfmt(init);
                }
            }
        }

        std::cout << std::flush;

        if (!mbStdOut)
        {
            std::cout.rdbuf(coutbuf);
            out.close();
        }

        std::exit(0);
    }
    catch (const css::uno::Exception&)
    {
        TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
        return 1;
    }
    catch (const std::exception& e)
    {
        SAL_WARN("vcl.app", "Fatal: " << e.what());
        return 1;
    }
    return 0;
}
}

void ListFonts::Init()
{
    const sal_uInt16 nCmdParams = GetCommandLineParamCount();
    OUString aArg;

    if (nCmdParams == 0)
    {
        showHelp();
        std::exit(1);
    }
    else
    {
        aArg = GetCommandLineParam(0);

        if (aArg == "--help" || aArg == "-h")
        {
            showHelp();
            std::exit(0);
        }
        else if (nCmdParams == 2 && (aArg == "--verbose" || aArg == "-v"))
        {
            aArg = GetCommandLineParam(1);
            mbShowFeatures = true;

            if (aArg == "--")
                mbStdOut = true;
        }
        else if (nCmdParams == 1)
        {
            if (aArg == "--")
                mbStdOut = true;
        }
        else
        {
            std::cerr << "invalid arguments\n";
            showHelp();
            std::exit(1);
        }
    }

    if (!mbStdOut)
    {
        maFilename = aArg;

        osl::File aFile(maFilename);

        if (!aFile.open(osl_File_OpenFlag_Create))
            throw css::uno::RuntimeException("Can not create file: " + aArg);

        aFile.close();
    }

    auto xContext = cppu::defaultBootstrap_InitialComponentContext();
    xServiceManager.set(xContext->getServiceManager(), css::uno::UNO_QUERY);

    if (!xServiceManager.is())
        Application::Abort("Bootstrap failure - no service manager");

    comphelper::setProcessServiceFactory(xServiceManager);

    LanguageTag::setConfiguredSystemLanguage(MsLangId::getSystemLanguage());
}

void ListFonts::DeInit()
{
    auto xContext = css::uno::Reference<css::lang::XComponent>(
        comphelper::getProcessComponentContext(), css::uno::UNO_QUERY_THROW);
    xContext->dispose();
    ::comphelper::setProcessServiceFactory(nullptr);
}

SAL_IMPLEMENT_MAIN()
{
    ListFonts aApp;
    InitVCL();
    int ret = aApp.Main();
    DeInitVCL();

    return ret;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */