qskinny/tools/glyph2qvg/main.cpp

108 lines
2.5 KiB
C++
Raw Normal View History

2025-02-03 10:50:40 +00:00
/******************************************************************************
* QSkinny - Copyright (C) The authors
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#if defined( QSK_STANDALONE )
#include <QskGraphic.cpp>
#include <QskRgbValue.cpp>
#include <QskColorFilter.cpp>
#include <QskPainterCommand.cpp>
#include <QskGraphicPaintEngine.cpp>
#include <QskGraphicIO.cpp>
#else
#include <QskGraphicIO.h>
#include <QskGraphic.h>
#endif
#include <QGuiApplication>
#include <QRawFont>
#include <QPainter>
#include <QPainterPath>
#include <QDebug>
2025-02-21 11:41:08 +00:00
enum Options
{
ViewBox = 1 << 0,
Antialiasing = 1 << 1
};
2025-02-03 10:50:40 +00:00
static void usage( const char* appName )
{
2025-02-21 11:41:08 +00:00
qWarning() << "usage: " << appName << "<fontfile> <pixelsize> <glyphindex> <qvgfile>";
}
static QPainterPath glyphPath( const QRawFont& font,
const qreal pixelSize, const uint glyphIndex )
{
auto path = font.pathForGlyph( glyphIndex );
path = path.simplified();
path = path.translated( 0.0, pixelSize );
return path;
}
static QskGraphic icon( const QPainterPath& path,
const qreal pixelSize, const int options )
{
QskGraphic graphic;
if ( options & ViewBox )
graphic.setViewBox( QRectF( 0.0, 0.0, pixelSize, pixelSize ) );
QPainter painter( &graphic );
if ( options & Antialiasing )
painter.setRenderHint( QPainter::Antialiasing, true );
painter.fillPath( path, Qt::black );
return graphic;
2025-02-03 10:50:40 +00:00
}
int main( int argc, char* argv[] )
{
2025-02-21 11:41:08 +00:00
QGuiApplication app( argc, argv );
if ( argc != 5 )
2025-02-03 10:50:40 +00:00
{
usage( argv[0] );
return -1;
}
2025-02-21 11:41:08 +00:00
bool ok;
2025-02-03 10:50:40 +00:00
2025-02-21 11:41:08 +00:00
const auto pixelSize = QString( argv[2] ).toDouble( &ok );
if ( !ok || ( pixelSize <= 0 ) )
2025-02-03 10:50:40 +00:00
{
2025-02-21 11:41:08 +00:00
qWarning() << "invalid pixel size:" << argv[2];
return -3;
2025-02-03 10:50:40 +00:00
}
2025-02-21 11:41:08 +00:00
const auto glyphIndex = QString( argv[3] ).toUInt( &ok );
2025-02-03 10:50:40 +00:00
if ( !ok )
{
2025-02-21 11:41:08 +00:00
qWarning() << "invalid glyph index:" << argv[3];
2025-02-03 10:50:40 +00:00
return -3;
}
2025-02-21 11:41:08 +00:00
QRawFont font( QString( argv[1] ), pixelSize );
if ( !font.isValid() )
{
qWarning() << "invalid font name:" << argv[1];
return -2;
}
const auto path = glyphPath( font, pixelSize, glyphIndex );
2025-02-03 10:50:40 +00:00
if ( path.isEmpty() )
{
2025-02-21 11:41:08 +00:00
qWarning() << "no glyph for index:" << argv[3];
2025-02-03 10:50:40 +00:00
return -3;
}
2025-02-21 11:41:08 +00:00
const auto graphic = icon( path, pixelSize, ViewBox | Antialiasing );
QskGraphicIO::write( graphic, argv[4] );
2025-02-03 10:50:40 +00:00
return 0;
}