Skip to content

Commit

Permalink
Add new options --texture-crop-width and texture-crop-height for crop…
Browse files Browse the repository at this point in the history
… unused parts of output textures
  • Loading branch information
vladimirgamalyan committed Dec 4, 2021
1 parent 99c2f6e commit 601cfd1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ option | default | comment
--spacing-vert | 0 | spacing vertical
--spacing-horiz | 0 | spacing horizontal
--texture-size | 32x32,64x32,64x64,128x64, 128x128,256x128,256x256, 512x256,512x512,1024x512, 1024x1024,2048x1024,2048x2048 | comma separated list of allowed texture sizes (without spaces), the first suitable size will be used
--texture-crop-width | | crop unused parts of output textures (width)
--texture-crop-height | | crop unused parts of output textures (height)
--monochrome | | disable anti-aliasing
--extra-info | | write extra information to data file
--disable-texture-name-zero-padding | | disable texture name zero padding
Expand Down
23 changes: 19 additions & 4 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ std::vector<Config::Size> App::arrangeGlyphs(Glyphs& glyphs, const Config& confi
glyphRectangles = glyphRectanglesCopy;

//TODO: check workAreaW,H
const auto workAreaW = ss.w - config.spacing.hor; // config.textureSize.w
const auto workAreaH = ss.h - config.spacing.ver; // config.textureSize.h
const auto workAreaW = ss.w - config.spacing.hor;
const auto workAreaH = ss.h - config.spacing.ver;

mrbp.Init(workAreaW, workAreaH);
mrbp.Insert(glyphRectangles, arrangedRectangles, rbp::MaxRectsBinPack::RectBestAreaFit);
Expand All @@ -96,12 +96,27 @@ std::vector<Config::Size> App::arrangeGlyphs(Glyphs& glyphs, const Config& confi
throw std::runtime_error("can not fit glyphs into texture");
break;
}

std::uint32_t maxX = 0;
std::uint32_t maxY = 0;
for (const auto& r: arrangedRectangles)
{
glyphs[r.tag].x = r.x + config.spacing.hor;
glyphs[r.tag].y = r.y + config.spacing.ver;
std::uint32_t x = r.x + config.spacing.hor;
std::uint32_t y = r.y + config.spacing.ver;

glyphs[r.tag].x = x;
glyphs[r.tag].y = y;
glyphs[r.tag].page = static_cast<std::uint32_t>(result.size());

if (maxX < x + r.width)
maxX = x + r.width;
if (maxY < y + r.height)
maxY = y + r.height;
}
if (config.cropTexturesWidth)
lastSize.w = maxX;
if (config.cropTexturesHeight)
lastSize.h = maxY;

result.push_back(lastSize);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ struct Config
bool monochrome = false;
bool extraInfo = false;
bool disableTextureNameZeroPadding = false;
bool cropTexturesWidth = false;
bool cropTexturesHeight = false;
};
38 changes: 15 additions & 23 deletions src/ProgramOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,28 @@ Config ProgramOptions::parseCommandLine(int argc, char* argv[])
options.add_options()
("help", "produce help message")
("font-file", "path to ttf file, required (may appear several times)", cxxopts::value<std::vector<std::string>>(config.fontFile))
(charsOptionName,
"required characters, for example: 32-64,92,120-126\ndefault value is 32-126 if 'chars-file' option is not defined",
cxxopts::value<std::string>(chars))
(charsFileOptionName,
"optional path to UTF-8 text file with required characters (will be combined with 'chars' option)",
cxxopts::value<std::string>(charsFile))
("color", "foreground RGB color, for example: 32,255,255, default value is 255,255,255",
cxxopts::value<std::string>(color)->default_value("255,255,255"))
(backgroundColorOptionName, "background color RGB color, for example: 0,0,128, transparent by default",
cxxopts::value<std::string>(backgroundColor))
(charsOptionName, "required characters, for example: 32-64,92,120-126\ndefault value is 32-126 if 'chars-file' option is not defined", cxxopts::value<std::string>(chars))
(charsFileOptionName, "optional path to UTF-8 text file with required characters (will be combined with 'chars' option)", cxxopts::value<std::string>(charsFile))
("color", "foreground RGB color, for example: 32,255,255, default value is 255,255,255", cxxopts::value<std::string>(color)->default_value("255,255,255"))
(backgroundColorOptionName, "background color RGB color, for example: 0,0,128, transparent by default", cxxopts::value<std::string>(backgroundColor))
("font-size", "font size, default value is 32", cxxopts::value<std::uint16_t>(config.fontSize)->default_value("32"))
("padding-up", "padding up, default value is 0",
cxxopts::value<std::uint32_t>(config.padding.up)->default_value("0"))
("padding-right", "padding right, default value is 0",
cxxopts::value<std::uint32_t>(config.padding.right)->default_value("0"))
("padding-down", "padding down, default value is 0",
cxxopts::value<std::uint32_t>(config.padding.down)->default_value("0"))
("padding-left", "padding left, default value is 0",
cxxopts::value<std::uint32_t>(config.padding.left)->default_value("0"))
("spacing-vert", "spacing vert, default value is 0",
cxxopts::value<std::uint32_t>(config.spacing.ver)->default_value("0"))
("spacing-horiz", "spacing horiz, default value is 0",
cxxopts::value<std::uint32_t>(config.spacing.hor)->default_value("0"))
("padding-up", "padding up, default value is 0", cxxopts::value<std::uint32_t>(config.padding.up)->default_value("0"))
("padding-right", "padding right, default value is 0", cxxopts::value<std::uint32_t>(config.padding.right)->default_value("0"))
("padding-down", "padding down, default value is 0", cxxopts::value<std::uint32_t>(config.padding.down)->default_value("0"))
("padding-left", "padding left, default value is 0", cxxopts::value<std::uint32_t>(config.padding.left)->default_value("0"))
("spacing-vert", "spacing vert, default value is 0", cxxopts::value<std::uint32_t>(config.spacing.ver)->default_value("0"))
("spacing-horiz", "spacing horiz, default value is 0", cxxopts::value<std::uint32_t>(config.spacing.hor)->default_value("0"))
("output", "output files name without extension, required", cxxopts::value<std::string>(config.output))
("data-format", R"(output data file format, "xml" or "txt", default "xml")", cxxopts::value<std::string>(dataFormat)->default_value("txt"))
("include-kerning-pairs", "include kerning pairs to output file", cxxopts::value<bool>(config.includeKerningPairs))
("monochrome", "disable antialiasing", cxxopts::value<bool>(config.monochrome))
("extra-info", "write extra information to data file", cxxopts::value<bool>(config.extraInfo))
("disable-texture-name-zero-padding", "disable texture name zero padding", cxxopts::value<bool>(config.disableTextureNameZeroPadding))
(textureSizeListOptionName, "list of texture sizes (will be tried from left to right to fit glyphs)", cxxopts::value<std::string>(textureSizeList))
("max-texture-count", "maximum generated textures", cxxopts::value<std::uint32_t>(config.maxTextureCount)->default_value("0"));
("texture-crop-width", "crop unused parts of output textures (width)", cxxopts::value<bool>(config.cropTexturesWidth))
("texture-crop-height", "crop unused parts of output textures (height)", cxxopts::value<bool>(config.cropTexturesHeight))
("max-texture-count", "maximum generated textures", cxxopts::value<std::uint32_t>(config.maxTextureCount)->default_value("0"))
;

auto result = options.parse(argc, argv);

Expand Down Expand Up @@ -110,6 +101,7 @@ Config ProgramOptions::parseCommandLine(int argc, char* argv[])
{128, 128},
{256, 128},
{256, 256},
{512, 256},
{512, 512},
{1024, 512},
{1024, 1024},
Expand Down

0 comments on commit 601cfd1

Please sign in to comment.