1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*
* CBitmapFont.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CBitmapFont.h"
#include "SDL_Extensions.h"
#include "SDLImageScaler.h"
#include "../CGameInfo.h"
#include "../gui/CGuiHandler.h"
#include "../render/Colors.h"
#include "../render/IImage.h"
#include "../render/IScreenHandler.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/Rect.h"
#include "../../lib/VCMI_Lib.h"
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/modding/CModHandler.h"
#include "../../lib/texts/Languages.h"
#include "../../lib/texts/TextOperations.h"
#include "../../lib/vcmi_endian.h"
#include <SDL_surface.h>
#include <SDL_image.h>
struct AtlasLayout
{
Point dimensions;
std::map<int, Rect> images;
};
/// Attempts to pack provided list of images into 2d box of specified size
/// Returns resulting layout on success and empty optional on failure
static std::optional<AtlasLayout> tryAtlasPacking(Point dimensions, const std::map<int, Point> & images)
{
// Simple atlas packing algorithm. Can be extended if needed, however optimal solution is NP-complete problem, so 'perfect' solution is too costly
AtlasLayout result;
result.dimensions = dimensions;
// a little interval to prevent potential 'bleeding' into adjacent symbols
// should be unnecessary for base game, but may be needed for upscaled filters
constexpr int interval = 1;
int currentHeight = 0;
int nextHeight = 0;
int currentWidth = 0;
for (auto const & image : images)
{
int nextWidth = currentWidth + image.second.x + interval;
if (nextWidth > dimensions.x)
{
currentHeight = nextHeight;
currentWidth = 0;
nextWidth = currentWidth + image.second.x + interval;
}
nextHeight = std::max(nextHeight, currentHeight + image.second.y + interval);
if (nextHeight > dimensions.y)
return std::nullopt; // failure - ran out of space
result.images[image.first] = Rect(Point(currentWidth, currentHeight), image.second);
currentWidth = nextWidth;
}
return result;
}
/// Arranges images to fit into texture atlas with automatic selection of image size
/// Returns images arranged into 2d box
static AtlasLayout doAtlasPacking(const std::map<int, Point> & images)
{
// initial size of an atlas. Smaller size won't even fit tiniest H3 font
Point dimensions(128, 128);
for (;;)
{
auto result = tryAtlasPacking(dimensions, images);
if (result)
return *result;
// else - packing failed. Increase atlas size and try again
// increase width and height in alternating form: (64,64) -> (128,64) -> (128,128) ...
if (dimensions.x > dimensions.y)
dimensions.y *= 2;
else
dimensions.x *= 2;
}
}
void CBitmapFont::loadFont(const ResourcePath & resource, std::unordered_map<CodePoint, EntryFNT> & loadedChars)
{
auto data = CResourceHandler::get()->load(resource)->readAll();
std::string modEncoding = VLC->modh->findResourceEncoding(resource);
height = data.first[5];
constexpr size_t symbolsInFile = 0x100;
constexpr size_t baseIndex = 32;
constexpr size_t offsetIndex = baseIndex + symbolsInFile*12;
constexpr size_t dataIndex = offsetIndex + symbolsInFile*4;
for (uint32_t charIndex = 0; charIndex < symbolsInFile; ++charIndex)
{
CodePoint codepoint = TextOperations::getUnicodeCodepoint(static_cast<char>(charIndex), modEncoding);
EntryFNT symbol;
symbol.leftOffset = read_le_u32(data.first.get() + baseIndex + charIndex * 12 + 0);
symbol.width = read_le_u32(data.first.get() + baseIndex + charIndex * 12 + 4);
symbol.rightOffset = read_le_u32(data.first.get() + baseIndex + charIndex * 12 + 8);
symbol.height = height;
uint32_t pixelDataOffset = read_le_u32(data.first.get() + offsetIndex + charIndex * 4);
uint32_t pixelsCount = height * symbol.width;
symbol.pixels.resize(pixelsCount);
uint8_t * pixelData = data.first.get() + dataIndex + pixelDataOffset;
std::copy_n(pixelData, pixelsCount, symbol.pixels.data() );
loadedChars[codepoint] = symbol;
}
// Try to use symbol 'L' to detect font 'ascent' - number of pixels above text baseline
const auto & symbolL = loadedChars['L'];
uint32_t lastNonEmptyRow = 0;
for (uint32_t row = 0; row < symbolL.height; ++row)
{
for (uint32_t col = 0; col < symbolL.width; ++col)
if (symbolL.pixels.at(row * symbolL.width + col) == 255)
lastNonEmptyRow = std::max(lastNonEmptyRow, row);
}
ascent = lastNonEmptyRow + 1;
}
CBitmapFont::CBitmapFont(const std::string & filename):
height(0)
{
ResourcePath resource("data/" + filename, EResType::BMP_FONT);
std::unordered_map<CodePoint, EntryFNT> loadedChars;
loadFont(resource, loadedChars);
std::map<int, Point> atlasSymbol;
for (auto const & symbol : loadedChars)
atlasSymbol[symbol.first] = Point(symbol.second.width, symbol.second.height);
auto atlas = doAtlasPacking(atlasSymbol);
atlasImage = SDL_CreateRGBSurface(0, atlas.dimensions.x, atlas.dimensions.y, 8, 0, 0, 0, 0);
assert(atlasImage->format->palette != nullptr);
assert(atlasImage->format->palette->ncolors == 256);
atlasImage->format->palette->colors[0] = { 0, 255, 255, SDL_ALPHA_OPAQUE }; // transparency
atlasImage->format->palette->colors[1] = { 0, 0, 0, SDL_ALPHA_OPAQUE }; // black shadow
CSDL_Ext::fillSurface(atlasImage, CSDL_Ext::toSDL(Colors::CYAN));
CSDL_Ext::setColorKey(atlasImage, CSDL_Ext::toSDL(Colors::CYAN));
for (size_t i = 2; i < atlasImage->format->palette->ncolors; ++i)
atlasImage->format->palette->colors[i] = { 255, 255, 255, SDL_ALPHA_OPAQUE };
for (auto const & symbol : loadedChars)
{
BitmapChar storedEntry;
storedEntry.leftOffset = symbol.second.leftOffset;
storedEntry.rightOffset = symbol.second.rightOffset;
storedEntry.positionInAtlas = atlas.images.at(symbol.first);
// Copy pixel data to atlas
uint8_t *dstPixels = static_cast<uint8_t*>(atlasImage->pixels);
uint8_t *dstLine = dstPixels + storedEntry.positionInAtlas.y * atlasImage->pitch;
uint8_t *dst = dstLine + storedEntry.positionInAtlas.x;
for (size_t i = 0; i < storedEntry.positionInAtlas.h; ++i)
{
const uint8_t *srcPtr = symbol.second.pixels.data() + i * storedEntry.positionInAtlas.w;
uint8_t * dstPtr = dst + i * atlasImage->pitch;
std::copy_n(srcPtr, storedEntry.positionInAtlas.w, dstPtr);
}
chars[symbol.first] = storedEntry;
}
if (GH.screenHandler().getScalingFactor() != 1)
{
static const std::map<std::string, EScalingAlgorithm> filterNameToEnum = {
{ "nearest", EScalingAlgorithm::NEAREST},
{ "bilinear", EScalingAlgorithm::BILINEAR},
{ "xbrz", EScalingAlgorithm::XBRZ_ALPHA}
};
auto filterName = settings["video"]["fontUpscalingFilter"].String();
EScalingAlgorithm algorithm = filterNameToEnum.at(filterName);
SDLImageScaler scaler(atlasImage);
scaler.scaleSurfaceIntegerFactor(GH.screenHandler().getScalingFactor(), algorithm);
SDL_FreeSurface(atlasImage);
atlasImage = scaler.acquireResultSurface();
}
logGlobal->debug("Loaded BMP font: '%s', height %d, ascent %d",
filename,
getLineHeightScaled(),
getFontAscentScaled()
);
}
CBitmapFont::~CBitmapFont()
{
SDL_FreeSurface(atlasImage);
}
size_t CBitmapFont::getLineHeightScaled() const
{
return height * getScalingFactor();
}
size_t CBitmapFont::getGlyphWidthScaled(const char * data) const
{
CodePoint localChar = TextOperations::getUnicodeCodepoint(data, 4);
auto iter = chars.find(localChar);
if (iter == chars.end())
return 0;
return (iter->second.leftOffset + iter->second.positionInAtlas.w + iter->second.rightOffset) * getScalingFactor();
}
size_t CBitmapFont::getFontAscentScaled() const
{
return ascent * getScalingFactor();
}
bool CBitmapFont::canRepresentCharacter(const char *data) const
{
CodePoint localChar = TextOperations::getUnicodeCodepoint(data, 4);
auto iter = chars.find(localChar);
return iter != chars.end();
}
bool CBitmapFont::canRepresentString(const std::string & data) const
{
for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
if (!canRepresentCharacter(data.data() + i))
return false;
return true;
}
void CBitmapFont::renderCharacter(SDL_Surface * surface, const BitmapChar & character, const ColorRGBA & color, int &posX, int &posY) const
{
int scalingFactor = GH.screenHandler().getScalingFactor();
posX += character.leftOffset * scalingFactor;
auto sdlColor = CSDL_Ext::toSDL(color);
if (atlasImage->format->palette)
SDL_SetPaletteColors(atlasImage->format->palette, &sdlColor, 255, 1);
else
SDL_SetSurfaceColorMod(atlasImage, color.r, color.g, color.b);
CSDL_Ext::blitSurface(atlasImage, character.positionInAtlas * scalingFactor, surface, Point(posX, posY));
posX += character.positionInAtlas.w * scalingFactor;
posX += character.rightOffset * scalingFactor;
}
void CBitmapFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
{
if (data.empty())
return;
assert(surface);
int posX = pos.x;
int posY = pos.y;
for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
{
CodePoint codepoint = TextOperations::getUnicodeCodepoint(data.data() + i, data.size() - i);
auto iter = chars.find(codepoint);
if (iter != chars.end())
renderCharacter(surface, iter->second, color, posX, posY);
}
}
|