mirror of https://github.com/mosra/magnum.git
Browse Source
Again borrowed from Push the Box, modified and made slightly more configurable. Will write proper docs for the utilities when I have some more time.pull/34/head
5 changed files with 183 additions and 5 deletions
@ -0,0 +1,25 @@
|
||||
/* |
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#define MAGNUM_PLUGINS_DIR "${MAGNUM_PLUGINS_DIR}" |
||||
@ -0,0 +1,131 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include <PluginManager/Manager.h> |
||||
#include <Utility/Arguments.h> |
||||
#include <Utility/Directory.h> |
||||
|
||||
#include "Platform/WindowlessGlxApplication.h" |
||||
#include "Text/AbstractFont.h" |
||||
#include "Text/AbstractFontConverter.h" |
||||
#include "Text/DistanceFieldGlyphCache.h" |
||||
#include "Trade/AbstractImageConverter.h" |
||||
|
||||
#include "configure.h" |
||||
|
||||
namespace Magnum { namespace Text { |
||||
|
||||
class FontConverter: public Platform::WindowlessApplication { |
||||
public: |
||||
explicit FontConverter(const Arguments& arguments); |
||||
|
||||
int exec() override; |
||||
|
||||
private: |
||||
Utility::Arguments args; |
||||
}; |
||||
|
||||
FontConverter::FontConverter(const Arguments& arguments): Platform::WindowlessApplication(arguments, nullptr) { |
||||
args.addArgument("input").setHelp("input", "input font") |
||||
.addArgument("output").setHelp("output", "output filename prefix") |
||||
.addNamedArgument("font").setHelp("font", "plugin for opening the font") |
||||
.addNamedArgument("converter").setHelp("converter", "plugin for converting the font") |
||||
.addOption("plugin-dir", MAGNUM_PLUGINS_DIR).setHelpKey("plugin-dir", "DIR").setHelp("plugin-dir", "base plugin dir") |
||||
.addOption("characters", "abcdefghijklmnopqrstuvwxyz" |
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||||
"0123456789?!:,. ").setHelp("characters", "characters to include in the output") |
||||
.addOption("font-size", "128").setHelpKey("font-size", "\"X Y\"").setHelp("font-size", "TTF font size") |
||||
.addOption("atlas-size", "2048 2048").setHelpKey("atlas-size", "\"X Y\"").setHelp("atlas-size", "glyph atlas size") |
||||
.addOption("output-size", "256 256").setHelpKey("output-size", "\"X Y\"").setHelp("output-size", "output atlas size. If set to zero size, distance field computation will not be used.") |
||||
.addOption("radius", "24").setHelpKey("radius", "N").setHelp("radius", "distance field computation radius") |
||||
.setHelp("Converts font to raster one of given atlas size.") |
||||
.parse(arguments.argc, arguments.argv); |
||||
|
||||
createContext({}); |
||||
} |
||||
|
||||
int FontConverter::exec() { |
||||
/* Font converter dependencies */ |
||||
PluginManager::Manager<Trade::AbstractImageConverter> imageConverterManager(Utility::Directory::join(MAGNUM_PLUGINS_DIR, "imageconverters/")); |
||||
|
||||
/* Load font */ |
||||
PluginManager::Manager<Text::AbstractFont> fontManager(Utility::Directory::join(MAGNUM_PLUGINS_DIR, "fonts/")); |
||||
std::unique_ptr<Text::AbstractFont> font; |
||||
if(!(fontManager.load(args.value("font")) & PluginManager::LoadState::Loaded) || |
||||
!(font = fontManager.instance(args.value("font")))) { |
||||
Error() << "Cannot load plugin" << args.value("font") << "from" << fontManager.pluginDirectory(); |
||||
std::exit(1); |
||||
} |
||||
|
||||
/* Load font converter */ |
||||
PluginManager::Manager<Text::AbstractFontConverter> converterManager(Utility::Directory::join(MAGNUM_PLUGINS_DIR, "fontconverters/")); |
||||
std::unique_ptr<Text::AbstractFontConverter> converter; |
||||
if(!(converterManager.load(args.value("converter")) & PluginManager::LoadState::Loaded) || |
||||
!(converter = converterManager.instance(args.value("converter")))) { |
||||
Error() << "Cannot load plugin" << args.value("converter") << "from" << converterManager.pluginDirectory(); |
||||
std::exit(1); |
||||
} |
||||
|
||||
/* Open font */ |
||||
if(!font->openFile(args.value("input"), args.value<Float>("font-size"))) { |
||||
Error() << "Cannot open font" << args.value("input"); |
||||
std::exit(1); |
||||
} |
||||
|
||||
/* Create distance field glyph cache if radius is specified */ |
||||
std::unique_ptr<Text::GlyphCache> cache; |
||||
if(!args.value<Vector2i>("output-size").isZero()) { |
||||
Debug() << "Populating distance field glyph cache..."; |
||||
|
||||
cache.reset(new Text::DistanceFieldGlyphCache( |
||||
args.value<Vector2i>("atlas-size"), |
||||
args.value<Vector2i>("output-size"), |
||||
args.value<Int>("radius"))); |
||||
|
||||
/* Otherwise use normal cache */ |
||||
} else { |
||||
Debug() << "Zero-size distance field output specified, populating normal glyph cache..."; |
||||
|
||||
cache.reset(new Text::GlyphCache(args.value<Vector2i>("atlas-size"))); |
||||
} |
||||
|
||||
/* Fill the cache */ |
||||
font->fillGlyphCache(*cache, args.value("characters")); |
||||
|
||||
Debug() << "Converting font..."; |
||||
|
||||
/* Convert the font */ |
||||
if(!converter->exportFontToFile(*font, *cache, args.value("output"), args.value("characters"))) { |
||||
Error() << "Cannot export font to" << args.value("output"); |
||||
std::exit(1); |
||||
} |
||||
|
||||
Debug() << "Done."; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
}} |
||||
|
||||
MAGNUM_WINDOWLESSAPPLICATION_MAIN(Magnum::Text::FontConverter) |
||||
Loading…
Reference in new issue