summaryrefslogtreecommitdiffstats
path: root/vcl/workben/listglyphs.cxx
blob: 341006d433ddf837c4aecbfa9bcc40fa206f073f (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
/* -*- 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 <comphelper/diagnose_ex.hxx>
#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 <font/LogicalFontInstance.hxx>
#include <font/PhysicalFontFace.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
{
class ListGlyphsWin : public WorkWindow
{
public:
    explicit ListGlyphsWin()
        : WorkWindow(nullptr, WB_HIDE)
    {
    }
};

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

private:
    static void showHelp()
    {
        std::cerr << "Usage: listglyphs --help | <fontname> FILE\n";
        std::cerr << "Lists the current glyphs in a font installed on the system.\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;
    OUString maFilename;
    OUString maFontname;
};

int ListGlyphs::Main()
{
    try
    {
        VclPtrInstance<ListGlyphsWin> 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());
        }

        bool bFontExists = false;

        for (sal_Int32 nFont = 0; nFont < pOutDev->GetFontFaceCollectionCount(); nFont++)
        {
            FontMetric aSystemFont = pOutDev->GetFontMetricFromCollection(nFont);
            if (aSystemFont.GetFamilyName() == maFontname)
            {
                bFontExists = true;
                break;
            }
        }

        if (!bFontExists)
        {
            std::cerr << maFontname << " does not exist\n";
            std::exit(1);
        }

        pOutDev->SetFont(vcl::Font(maFontname, Size(0, 11)));

        LogicalFontInstance const* pFontInstance = pOutDev->GetFontInstance();
        vcl::font::PhysicalFontFace const* pFontFace = pFontInstance->GetFontFace();
        FontCharMapRef pCharMap = pFontFace->GetFontCharMap();

        sal_UCS4 nLastChar = pCharMap->GetLastChar();
        for (sal_UCS4 nChar = pCharMap->GetFirstChar(); nChar < nLastChar;
             nChar = pCharMap->GetNextChar(nChar))
        {
            auto nGlyphIndex = pFontInstance->GetGlyphIndex(nChar);
            basegfx::B2DRectangle aGlyphBounds;
            pFontInstance->GetGlyphBoundRect(nGlyphIndex, aGlyphBounds, false);
            std::cout << "Codepoint: " << pFontFace->GetGlyphName(nGlyphIndex)
                      << "; glyph bounds: " << aGlyphBounds << "\n";
        }

        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 ListGlyphs::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);
        }

        if (nCmdParams == 2)
        {
            maFontname = GetCommandLineParam(0);

            aArg = GetCommandLineParam(1);

            if (aArg == "--")
                mbStdOut = true;

            maFilename = aArg;
        }
        else
        {
            std::cerr << "invalid arguments\n";
            showHelp();
            std::exit(1);
        }
    }

    if (!mbStdOut)
    {
        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 ListGlyphs::DeInit()
{
    auto xContext = css::uno::Reference<css::lang::XComponent>(
        comphelper::getProcessComponentContext(), css::uno::UNO_QUERY_THROW);
    xContext->dispose();
    ::comphelper::setProcessServiceFactory(nullptr);
}
}

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

    return ret;
}

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