1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #include <osl/file.hxx>
11 #include <osl/process.h>
12 #include <rtl/textenc.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>
37 #include <string_view>
41 class ListGlyphsWin
: public WorkWindow
44 explicit ListGlyphsWin()
45 : WorkWindow(nullptr, WB_HIDE
)
50 class ListGlyphs
: public Application
53 virtual int Main() override
;
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";
65 void DeInit() override
;
67 css::uno::Reference
<css::lang::XMultiServiceFactory
> xServiceManager
;
68 bool mbStdOut
= false;
73 int ListGlyphs::Main()
77 VclPtrInstance
<ListGlyphsWin
> pWin
;
78 OutputDevice
* pOutDev
= pWin
->GetOutDev();
80 std::streambuf
* coutbuf
= nullptr;
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
)
108 std::cerr
<< maFontname
<< " does not exist\n";
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
;
133 std::cout
.rdbuf(coutbuf
);
139 catch (const css::uno::Exception
&)
141 TOOLS_WARN_EXCEPTION("vcl.app", "Fatal");
144 catch (const std::exception
& e
)
146 SAL_WARN("vcl.app", "Fatal: " << e
.what());
152 void ListGlyphs::Init()
154 const sal_uInt16 nCmdParams
= GetCommandLineParamCount();
164 aArg
= GetCommandLineParam(0);
166 if (aArg
== "--help" || aArg
== "-h")
174 maFontname
= GetCommandLineParam(0);
176 aArg
= GetCommandLineParam(1);
185 std::cerr
<< "invalid arguments\n";
193 osl::File
aFile(maFilename
);
195 if (!aFile
.open(osl_File_OpenFlag_Create
))
196 throw css::uno::RuntimeException("Can not create file: " + aArg
);
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
);
217 ::comphelper::setProcessServiceFactory(nullptr);
225 int ret
= aApp
.Main();
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */