2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2023-02-01 16:42:03 +02:00
|
|
|
* CBitmapFont.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2012-12-19 20:24:53 +03:00
|
|
|
#include "StdInc.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "CBitmapFont.h"
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2023-01-30 20:06:08 +02:00
|
|
|
#include "SDL_Extensions.h"
|
2023-02-25 01:11:42 +02:00
|
|
|
#include "../CGameInfo.h"
|
2024-07-22 20:58:43 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-02-25 01:11:42 +02:00
|
|
|
#include "../render/Colors.h"
|
2024-07-22 20:58:43 +02:00
|
|
|
#include "../render/IScreenHandler.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
|
|
|
|
#include "../../lib/Rect.h"
|
2023-02-25 01:11:42 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../../lib/modding/CModHandler.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/Languages.h"
|
|
|
|
#include "../../lib/texts/TextOperations.h"
|
2023-02-25 01:11:42 +02:00
|
|
|
#include "../../lib/vcmi_endian.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../../lib/VCMI_Lib.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
|
|
|
|
#include <SDL_surface.h>
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
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
|
2024-08-17 22:54:29 +02:00
|
|
|
static std::optional<AtlasLayout> tryAtlasPacking(Point dimensions, const std::map<int, Point> & images)
|
2024-07-22 20:58:43 +02:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2024-08-17 22:54:29 +02:00
|
|
|
/// Arranges images to fit into texture atlas with automatic selection of image size
|
2024-07-22 20:58:43 +02:00
|
|
|
/// Returns images arranged into 2d box
|
2024-08-17 22:54:29 +02:00
|
|
|
static AtlasLayout doAtlasPacking(const std::map<int, Point> & images)
|
2024-07-22 20:58:43 +02:00
|
|
|
{
|
|
|
|
// 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::loadModFont(const std::string & modName, const ResourcePath & resource, std::unordered_map<CodePoint, EntryFNT> & loadedChars)
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2023-08-06 16:47:12 +02:00
|
|
|
if (!CResourceHandler::get(modName)->existsResource(resource))
|
|
|
|
{
|
|
|
|
logGlobal->error("Failed to load font %s from mod %s", resource.getName(), modName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
auto data = CResourceHandler::get(modName)->load(resource)->readAll();
|
|
|
|
std::string modLanguage = CGI->modh->getModLanguage(modName);
|
|
|
|
std::string modEncoding = Languages::getLanguageOptions(modLanguage).encoding;
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
uint32_t dataHeight = data.first[5];
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
maxHeight = std::max(maxHeight, dataHeight);
|
|
|
|
|
|
|
|
constexpr size_t symbolsInFile = 0x100;
|
|
|
|
constexpr size_t baseIndex = 32;
|
|
|
|
constexpr size_t offsetIndex = baseIndex + symbolsInFile*12;
|
|
|
|
constexpr size_t dataIndex = offsetIndex + symbolsInFile*4;
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
for (uint32_t charIndex = 0; charIndex < symbolsInFile; ++charIndex)
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2023-02-25 01:11:42 +02:00
|
|
|
CodePoint codepoint = TextOperations::getUnicodeCodepoint(static_cast<char>(charIndex), modEncoding);
|
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
EntryFNT symbol;
|
2023-02-25 01:11:42 +02:00
|
|
|
|
|
|
|
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 = dataHeight;
|
|
|
|
|
|
|
|
uint32_t pixelDataOffset = read_le_u32(data.first.get() + offsetIndex + charIndex * 4);
|
|
|
|
uint32_t pixelsCount = dataHeight * symbol.width;
|
|
|
|
|
|
|
|
symbol.pixels.resize(pixelsCount);
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
uint8_t * pixelData = data.first.get() + dataIndex + pixelDataOffset;
|
|
|
|
|
|
|
|
std::copy_n(pixelData, pixelsCount, symbol.pixels.data() );
|
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
loadedChars[codepoint] = symbol;
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CBitmapFont::CBitmapFont(const std::string & filename):
|
2023-02-25 01:11:42 +02:00
|
|
|
maxHeight(0)
|
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
ResourcePath resource("data/" + filename, EResType::BMP_FONT);
|
2023-02-25 01:11:42 +02:00
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
std::unordered_map<CodePoint, EntryFNT> loadedChars;
|
|
|
|
loadModFont("core", resource, loadedChars);
|
2023-02-25 01:11:42 +02:00
|
|
|
|
2023-04-04 21:23:32 +02:00
|
|
|
for(const auto & modName : VLC->modh->getActiveMods())
|
2023-02-25 01:11:42 +02:00
|
|
|
{
|
|
|
|
if (CResourceHandler::get(modName)->existsResource(resource))
|
2024-07-22 20:58:43 +02:00
|
|
|
loadModFont(modName, 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);
|
|
|
|
|
2024-08-17 22:54:29 +02:00
|
|
|
// 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)
|
2024-07-22 20:58:43 +02:00
|
|
|
{
|
2024-08-17 22:54:29 +02:00
|
|
|
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);
|
2024-07-22 20:58:43 +02:00
|
|
|
}
|
2024-08-17 22:54:29 +02:00
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
chars[symbol.first] = storedEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GH.screenHandler().getScalingFactor() != 1)
|
|
|
|
{
|
|
|
|
auto scaledSurface = CSDL_Ext::scaleSurfaceIntegerFactor(atlasImage, GH.screenHandler().getScalingFactor());
|
|
|
|
SDL_FreeSurface(atlasImage);
|
|
|
|
atlasImage = scaledSurface;
|
2023-02-25 01:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
CBitmapFont::~CBitmapFont()
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(atlasImage);
|
|
|
|
}
|
|
|
|
|
2012-12-19 20:24:53 +03:00
|
|
|
size_t CBitmapFont::getLineHeight() const
|
|
|
|
{
|
2023-02-25 01:11:42 +02:00
|
|
|
return maxHeight;
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2013-09-08 19:49:23 +03:00
|
|
|
size_t CBitmapFont::getGlyphWidth(const char * data) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2023-02-25 01:11:42 +02:00
|
|
|
CodePoint localChar = TextOperations::getUnicodeCodepoint(data, 4);
|
2013-10-26 00:45:14 +03:00
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
auto iter = chars.find(localChar);
|
|
|
|
|
|
|
|
if (iter == chars.end())
|
|
|
|
return 0;
|
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
return iter->second.leftOffset + iter->second.positionInAtlas.w + iter->second.rightOffset;
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2023-04-04 21:23:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void CBitmapFont::renderCharacter(SDL_Surface * surface, const BitmapChar & character, const ColorRGBA & color, int &posX, int &posY) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2024-07-22 20:58:43 +02:00
|
|
|
int scalingFactor = GH.screenHandler().getScalingFactor();
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
posX += character.leftOffset * scalingFactor;
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-08-03 22:14:51 +02:00
|
|
|
auto sdlColor = CSDL_Ext::toSDL(color);
|
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
if (atlasImage->format->palette)
|
2024-08-03 22:14:51 +02:00
|
|
|
SDL_SetPaletteColors(atlasImage->format->palette, &sdlColor, 255, 1);
|
2024-07-22 20:58:43 +02:00
|
|
|
else
|
|
|
|
SDL_SetSurfaceColorMod(atlasImage, color.r, color.g, color.b);
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
CSDL_Ext::blitSurface(atlasImage, character.positionInAtlas * scalingFactor, surface, Point(posX, posY));
|
2012-12-19 20:24:53 +03:00
|
|
|
|
2024-07-22 20:58:43 +02:00
|
|
|
posX += character.positionInAtlas.w * scalingFactor;
|
|
|
|
posX += character.rightOffset * scalingFactor;
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void CBitmapFont::renderText(SDL_Surface * surface, const std::string & data, const ColorRGBA & color, const Point & pos) const
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
|
|
|
if (data.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(surface);
|
|
|
|
|
|
|
|
int posX = pos.x;
|
|
|
|
int posY = pos.y;
|
|
|
|
|
2023-02-12 23:52:35 +02:00
|
|
|
for(size_t i=0; i<data.size(); i += TextOperations::getUnicodeCharacterSize(data[i]))
|
2012-12-19 20:24:53 +03:00
|
|
|
{
|
2023-02-25 01:11:42 +02:00
|
|
|
CodePoint codepoint = TextOperations::getUnicodeCodepoint(data.data() + i, data.size() - i);
|
|
|
|
|
|
|
|
auto iter = chars.find(codepoint);
|
2013-10-26 00:45:14 +03:00
|
|
|
|
2023-02-25 01:11:42 +02:00
|
|
|
if (iter != chars.end())
|
|
|
|
renderCharacter(surface, iter->second, color, posX, posY);
|
2012-12-19 20:24:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|