Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / vcl / workben / listglyphs.cxx
blobf9dac2f4a97eb7fc29d9f3fe098ab7f4c96ab234
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <osl/file.hxx>
11 #include <osl/process.h>
12 #include <rtl/textenc.h>
13 #include <sal/main.h>
14 #include <comphelper/processfactory.hxx>
15 #include <cppuhelper/bootstrap.hxx>
16 #include <comphelper/diagnose_ex.hxx>
17 #include <tools/degree.hxx>
18 #include <i18nlangtag/languagetag.hxx>
19 #include <i18nlangtag/mslangid.hxx>
21 #include <vcl/font/Feature.hxx>
22 #include <vcl/metric.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/vclmain.hxx>
25 #include <vcl/wrkwin.hxx>
27 #include <font/LogicalFontInstance.hxx>
28 #include <font/PhysicalFontFace.hxx>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <iostream>
35 #include <fstream>
36 #include <string>
37 #include <string_view>
39 namespace
41 class ListGlyphsWin : public WorkWindow
43 public:
44 explicit ListGlyphsWin()
45 : WorkWindow(nullptr, WB_HIDE)
50 class ListGlyphs : public Application
52 public:
53 virtual int Main() override;
55 private:
56 static void showHelp()
58 std::cerr << "Usage: listglyphs --help | <fontname> FILE\n";
59 std::cerr << "Lists the current glyphs in a font installed on the system.\n";
60 std::cerr << "If outputting to stdout, use -- for FILE.\n";
61 std::exit(0);
64 void Init() override;
65 void DeInit() override;
67 css::uno::Reference<css::lang::XMultiServiceFactory> xServiceManager;
68 bool mbStdOut = false;
69 OUString maFilename;
70 OUString maFontname;
73 int ListGlyphs::Main()
75 try
77 VclPtrInstance<ListGlyphsWin> pWin;
78 OutputDevice* pOutDev = pWin->GetOutDev();
80 std::streambuf* coutbuf = nullptr;
81 std::fstream out;
83 if (!mbStdOut)
85 std::u16string_view filenamev = maFilename;
86 std::string filename(filenamev.begin(), filenamev.end());
88 out.open(filename, std::ios::out | std::ios::trunc);
90 coutbuf = std::cout.rdbuf();
91 std::cout.rdbuf(out.rdbuf());
94 bool bFontExists = false;
96 for (sal_Int32 nFont = 0; nFont < pOutDev->GetFontFaceCollectionCount(); nFont++)
98 FontMetric aSystemFont = pOutDev->GetFontMetricFromCollection(nFont);
99 if (aSystemFont.GetFamilyName() == maFontname)
101 bFontExists = true;
102 break;
106 if (!bFontExists)
108 std::cerr << maFontname << " does not exist\n";
109 std::exit(1);
112 pOutDev->SetFont(vcl::Font(maFontname, Size(0, 11)));
114 LogicalFontInstance const* pFontInstance = pOutDev->GetFontInstance();
115 vcl::font::PhysicalFontFace const* pFontFace = pFontInstance->GetFontFace();
116 FontCharMapRef pCharMap = pFontFace->GetFontCharMap();
118 sal_UCS4 nLastChar = pCharMap->GetLastChar();
119 for (sal_UCS4 nChar = pCharMap->GetFirstChar(); nChar < nLastChar;
120 nChar = pCharMap->GetNextChar(nChar))
122 auto nGlyphIndex = pFontInstance->GetGlyphIndex(nChar);
123 basegfx::B2DRectangle aGlyphBounds;
124 pFontInstance->GetGlyphBoundRect(nGlyphIndex, aGlyphBounds, false);
125 std::cout << "Codepoint: " << pFontFace->GetGlyphName(nGlyphIndex)
126 << "; glyph bounds: " << aGlyphBounds << "\n";
129 std::cout << std::flush;
131 if (!mbStdOut)
133 std::cout.rdbuf(coutbuf);
134 out.close();
137 std::exit(0);
139 catch (const css::uno::Exception&)
141 TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
142 return 1;
144 catch (const std::exception& e)
146 SAL_WARN("vcl.app", "Fatal: " << e.what());
147 return 1;
149 return 0;
152 void ListGlyphs::Init()
154 const sal_uInt16 nCmdParams = GetCommandLineParamCount();
155 OUString aArg;
157 if (nCmdParams == 0)
159 showHelp();
160 std::exit(1);
162 else
164 aArg = GetCommandLineParam(0);
166 if (aArg == "--help" || aArg == "-h")
168 showHelp();
169 std::exit(0);
172 if (nCmdParams == 2)
174 maFontname = GetCommandLineParam(0);
176 aArg = GetCommandLineParam(1);
178 if (aArg == "--")
179 mbStdOut = true;
181 maFilename = aArg;
183 else
185 std::cerr << "invalid arguments\n";
186 showHelp();
187 std::exit(1);
191 if (!mbStdOut)
193 osl::File aFile(maFilename);
195 if (!aFile.open(osl_File_OpenFlag_Create))
196 throw css::uno::RuntimeException("Can not create file: " + aArg);
198 aFile.close();
201 auto xContext = cppu::defaultBootstrap_InitialComponentContext();
202 xServiceManager.set(xContext->getServiceManager(), css::uno::UNO_QUERY);
204 if (!xServiceManager.is())
205 Application::Abort(u"Bootstrap failure - no service manager"_ustr);
207 comphelper::setProcessServiceFactory(xServiceManager);
209 LanguageTag::setConfiguredSystemLanguage(MsLangId::getSystemLanguage());
212 void ListGlyphs::DeInit()
214 auto xContext = css::uno::Reference<css::lang::XComponent>(
215 comphelper::getProcessComponentContext(), css::uno::UNO_QUERY_THROW);
216 xContext->dispose();
217 ::comphelper::setProcessServiceFactory(nullptr);
221 SAL_IMPLEMENT_MAIN()
223 ListGlyphs aApp;
224 InitVCL();
225 int ret = aApp.Main();
226 DeInitVCL();
228 return ret;
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */