2014-10-14 13:11:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2016-02-13 13:11:30 +00:00
|
|
|
* Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-08-22 14:40:02 +00:00
|
|
|
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
2014-10-14 13:11:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see http://gnu.org/licenses
|
|
|
|
*/
|
2012-06-22 14:31:55 +00:00
|
|
|
|
|
|
|
|
2012-07-26 20:26:19 +00:00
|
|
|
#include "graphics/engine/text.h"
|
2012-06-22 14:31:55 +00:00
|
|
|
|
2012-08-03 21:23:13 +00:00
|
|
|
#include "app/app.h"
|
2013-02-16 21:37:43 +00:00
|
|
|
|
2012-08-03 21:23:13 +00:00
|
|
|
#include "common/image.h"
|
|
|
|
#include "common/logger.h"
|
|
|
|
#include "common/stringutils.h"
|
2015-08-02 11:09:48 +00:00
|
|
|
|
2014-06-20 21:41:38 +00:00
|
|
|
#include "common/resources/resourcemanager.h"
|
2013-02-16 21:37:43 +00:00
|
|
|
|
2015-10-03 20:05:14 +00:00
|
|
|
#include "graphics/engine/engine.h"
|
|
|
|
|
2012-08-03 21:23:13 +00:00
|
|
|
#include "math/func.h"
|
2012-06-22 14:31:55 +00:00
|
|
|
|
2013-03-22 17:19:53 +00:00
|
|
|
#include <SDL.h>
|
|
|
|
#include <SDL_ttf.h>
|
2012-08-03 21:23:13 +00:00
|
|
|
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
// Graphics module namespace
|
2015-08-02 09:40:47 +00:00
|
|
|
namespace Gfx
|
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
/**
|
|
|
|
* \struct MultisizeFont
|
|
|
|
* \brief Font with multiple possible sizes
|
|
|
|
*/
|
|
|
|
struct MultisizeFont
|
|
|
|
{
|
|
|
|
std::string fileName;
|
|
|
|
std::map<int, std::unique_ptr<CachedFont>> fonts;
|
|
|
|
|
|
|
|
explicit MultisizeFont(const std::string &fn)
|
|
|
|
: fileName(fn) {}
|
|
|
|
};
|
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
/**
|
|
|
|
* \struct FontTexture
|
|
|
|
* \brief Single texture filled with character textures
|
|
|
|
*/
|
|
|
|
struct FontTexture
|
|
|
|
{
|
|
|
|
unsigned int id = 0;
|
|
|
|
Math::IntPoint tileSize;
|
|
|
|
int freeSlots = 0;
|
|
|
|
};
|
2012-08-03 21:23:13 +00:00
|
|
|
|
|
|
|
/**
|
2012-09-19 21:50:28 +00:00
|
|
|
* \struct CachedFont
|
|
|
|
* \brief Base TTF font with UTF-8 char cache
|
|
|
|
*/
|
2012-08-03 21:23:13 +00:00
|
|
|
struct CachedFont
|
|
|
|
{
|
2016-02-11 15:12:16 +00:00
|
|
|
std::unique_ptr<CSDLMemoryWrapper> fontFile;
|
2015-08-12 19:07:16 +00:00
|
|
|
TTF_Font* font = nullptr;
|
2012-09-19 21:50:28 +00:00
|
|
|
std::map<UTF8Char, CharTexture> cache;
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2016-02-11 15:12:16 +00:00
|
|
|
CachedFont(std::unique_ptr<CSDLMemoryWrapper> fontFile, int pointSize)
|
2015-08-12 19:07:16 +00:00
|
|
|
: fontFile(std::move(fontFile))
|
|
|
|
{
|
|
|
|
font = TTF_OpenFontRW(this->fontFile->GetHandler(), 0, pointSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CachedFont()
|
|
|
|
{
|
|
|
|
if (font != nullptr)
|
|
|
|
TTF_CloseFont(font);
|
|
|
|
}
|
2012-08-03 21:23:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
namespace
|
|
|
|
{
|
2013-01-03 23:29:19 +00:00
|
|
|
const Math::IntPoint REFERENCE_SIZE(800, 600);
|
2016-03-18 01:01:06 +00:00
|
|
|
const Math::IntPoint FONT_TEXTURE_SIZE(256, 256);
|
2015-08-12 19:07:16 +00:00
|
|
|
} // anonymous namespace
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
|
2013-02-16 21:37:43 +00:00
|
|
|
CText::CText(CEngine* engine)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
m_device = nullptr;
|
|
|
|
m_engine = engine;
|
|
|
|
|
|
|
|
m_defaultSize = 12.0f;
|
2013-05-12 16:38:01 +00:00
|
|
|
m_tabSize = 4;
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
m_lastFontType = FONT_COLOBOT;
|
2012-08-03 21:23:13 +00:00
|
|
|
m_lastFontSize = 0;
|
|
|
|
m_lastCachedFont = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CText::~CText()
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
m_device = nullptr;
|
|
|
|
m_engine = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
bool CText::Create()
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
if (TTF_Init() != 0)
|
|
|
|
{
|
|
|
|
m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
m_fonts[FONT_COLOBOT] = MakeUnique<MultisizeFont>("fonts/dvu_sans.ttf");
|
|
|
|
m_fonts[FONT_COLOBOT_BOLD] = MakeUnique<MultisizeFont>("fonts/dvu_sans_bold.ttf");
|
|
|
|
m_fonts[FONT_COLOBOT_ITALIC] = MakeUnique<MultisizeFont>("fonts/dvu_sans_italic.ttf");
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
m_fonts[FONT_COURIER] = MakeUnique<MultisizeFont>("fonts/dvu_sans_mono.ttf");
|
|
|
|
m_fonts[FONT_COURIER_BOLD] = MakeUnique<MultisizeFont>("fonts/dvu_sans_mono_bold.ttf");
|
2012-08-03 21:23:13 +00:00
|
|
|
|
|
|
|
for (auto it = m_fonts.begin(); it != m_fonts.end(); ++it)
|
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
FontType type = (*it).first;
|
2012-08-03 21:23:13 +00:00
|
|
|
CachedFont* cf = GetOrOpenFont(type, m_defaultSize);
|
|
|
|
if (cf == nullptr || cf->font == nullptr)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
void CText::Destroy()
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
m_fonts.clear();
|
|
|
|
|
|
|
|
m_lastCachedFont = nullptr;
|
2015-08-12 19:07:16 +00:00
|
|
|
m_lastFontType = FONT_COLOBOT;
|
|
|
|
m_lastFontSize = 0;
|
2012-08-03 21:23:13 +00:00
|
|
|
|
|
|
|
TTF_Quit();
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
void CText::SetDevice(CDevice* device)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
m_device = device;
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
std::string CText::GetError()
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
return m_error;
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
void CText::FlushCache()
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2016-03-18 01:01:06 +00:00
|
|
|
for (auto& fontTexture : m_fontTextures)
|
|
|
|
{
|
|
|
|
Texture tex;
|
|
|
|
tex.id = fontTexture.id;
|
|
|
|
m_device->DestroyTexture(tex);
|
|
|
|
}
|
|
|
|
m_fontTextures.clear();
|
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
for (auto& multisizeFont : m_fonts)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2015-08-12 19:07:16 +00:00
|
|
|
for (auto& cachedFont : multisizeFont.second->fonts)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2015-08-12 19:07:16 +00:00
|
|
|
cachedFont.second->cache.clear();
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-03 23:29:19 +00:00
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
m_lastCachedFont = nullptr;
|
2013-01-03 23:29:19 +00:00
|
|
|
m_lastFontType = FONT_COLOBOT;
|
|
|
|
m_lastFontSize = 0;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2013-05-12 16:38:01 +00:00
|
|
|
int CText::GetTabSize()
|
|
|
|
{
|
|
|
|
return m_tabSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CText::SetTabSize(int tabSize)
|
|
|
|
{
|
|
|
|
m_tabSize = tabSize;
|
|
|
|
}
|
|
|
|
|
2013-02-09 22:49:38 +00:00
|
|
|
void CText::DrawText(const std::string &text, std::vector<FontMetaChar>::iterator format,
|
2013-02-24 00:40:55 +00:00
|
|
|
std::vector<FontMetaChar>::iterator end,
|
2012-09-26 14:31:04 +00:00
|
|
|
float size, Math::Point pos, float width, TextAlign align,
|
2012-09-30 08:56:35 +00:00
|
|
|
int eol, Color color)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
float sw = 0.0f;
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
if (align == TEXT_ALIGN_CENTER)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
2013-02-24 00:40:55 +00:00
|
|
|
sw = GetStringWidth(text, format, end, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
if (sw > width) sw = width;
|
|
|
|
pos.x -= sw / 2.0f;
|
|
|
|
}
|
2012-09-19 21:50:28 +00:00
|
|
|
else if (align == TEXT_ALIGN_RIGHT)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
2013-02-24 00:40:55 +00:00
|
|
|
sw = GetStringWidth(text, format, end, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
if (sw > width) sw = width;
|
|
|
|
pos.x -= sw;
|
|
|
|
}
|
|
|
|
|
2013-02-24 00:40:55 +00:00
|
|
|
DrawString(text, format, end, size, pos, width, eol, color);
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
void CText::DrawText(const std::string &text, FontType font,
|
2012-09-26 14:31:04 +00:00
|
|
|
float size, Math::Point pos, float width, TextAlign align,
|
2012-09-30 08:56:35 +00:00
|
|
|
int eol, Color color)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
float sw = 0.0f;
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
if (align == TEXT_ALIGN_CENTER)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
|
|
|
sw = GetStringWidth(text, font, size);
|
|
|
|
if (sw > width) sw = width;
|
|
|
|
pos.x -= sw / 2.0f;
|
|
|
|
}
|
2012-09-19 21:50:28 +00:00
|
|
|
else if (align == TEXT_ALIGN_RIGHT)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
|
|
|
sw = GetStringWidth(text, font, size);
|
|
|
|
if (sw > width) sw = width;
|
|
|
|
pos.x -= sw;
|
|
|
|
}
|
|
|
|
|
2012-09-30 08:56:35 +00:00
|
|
|
DrawString(text, font, size, pos, width, eol, color);
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 22:49:38 +00:00
|
|
|
void CText::SizeText(const std::string &text, std::vector<FontMetaChar>::iterator format,
|
2013-02-24 00:40:55 +00:00
|
|
|
std::vector<FontMetaChar>::iterator endFormat,
|
2012-09-26 14:31:04 +00:00
|
|
|
float size, Math::Point pos, TextAlign align,
|
|
|
|
Math::Point &start, Math::Point &end)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
start = end = pos;
|
|
|
|
|
2013-02-24 00:40:55 +00:00
|
|
|
float sw = GetStringWidth(text, format, endFormat, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
end.x += sw;
|
2012-09-19 21:50:28 +00:00
|
|
|
if (align == TEXT_ALIGN_CENTER)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
|
|
|
start.x -= sw/2.0f;
|
|
|
|
end.x -= sw/2.0f;
|
|
|
|
}
|
2012-09-19 21:50:28 +00:00
|
|
|
else if (align == TEXT_ALIGN_RIGHT)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
|
|
|
start.x -= sw;
|
|
|
|
end.x -= sw;
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
start.y -= GetDescent(FONT_COLOBOT, size);
|
|
|
|
end.y += GetAscent(FONT_COLOBOT, size);
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
void CText::SizeText(const std::string &text, FontType font,
|
2012-09-26 14:31:04 +00:00
|
|
|
float size, Math::Point pos, TextAlign align,
|
|
|
|
Math::Point &start, Math::Point &end)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
start = end = pos;
|
|
|
|
|
|
|
|
float sw = GetStringWidth(text, font, size);
|
|
|
|
end.x += sw;
|
2012-09-19 21:50:28 +00:00
|
|
|
if (align == TEXT_ALIGN_CENTER)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
|
|
|
start.x -= sw/2.0f;
|
|
|
|
end.x -= sw/2.0f;
|
|
|
|
}
|
2012-09-19 21:50:28 +00:00
|
|
|
else if (align == TEXT_ALIGN_RIGHT)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
|
|
|
start.x -= sw;
|
|
|
|
end.x -= sw;
|
|
|
|
}
|
|
|
|
|
|
|
|
start.y -= GetDescent(font, size);
|
|
|
|
end.y += GetAscent(font, size);
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
float CText::GetAscent(FontType font, float size)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
assert(font != FONT_BUTTON);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CachedFont* cf = GetOrOpenFont(font, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
assert(cf != nullptr);
|
2012-08-11 15:17:04 +00:00
|
|
|
Math::IntPoint wndSize;
|
|
|
|
wndSize.y = TTF_FontAscent(cf->font);
|
|
|
|
Math::Point ifSize = m_engine->WindowToInterfaceSize(wndSize);
|
|
|
|
return ifSize.y;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
float CText::GetDescent(FontType font, float size)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
assert(font != FONT_BUTTON);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CachedFont* cf = GetOrOpenFont(font, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
assert(cf != nullptr);
|
2012-08-11 15:17:04 +00:00
|
|
|
Math::IntPoint wndSize;
|
|
|
|
wndSize.y = TTF_FontDescent(cf->font);
|
|
|
|
Math::Point ifSize = m_engine->WindowToInterfaceSize(wndSize);
|
|
|
|
return ifSize.y;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
float CText::GetHeight(FontType font, float size)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
assert(font != FONT_BUTTON);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CachedFont* cf = GetOrOpenFont(font, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
assert(cf != nullptr);
|
2012-08-11 15:17:04 +00:00
|
|
|
Math::IntPoint wndSize;
|
|
|
|
wndSize.y = TTF_FontHeight(cf->font);
|
|
|
|
Math::Point ifSize = m_engine->WindowToInterfaceSize(wndSize);
|
|
|
|
return ifSize.y;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
float CText::GetStringWidth(const std::string &text,
|
2013-02-24 00:40:55 +00:00
|
|
|
std::vector<FontMetaChar>::iterator format,
|
|
|
|
std::vector<FontMetaChar>::iterator end, float size)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
float width = 0.0f;
|
|
|
|
unsigned int index = 0;
|
|
|
|
unsigned int fmtIndex = 0;
|
|
|
|
while (index < text.length())
|
|
|
|
{
|
2012-09-27 21:42:52 +00:00
|
|
|
FontType font = FONT_COLOBOT;
|
2013-02-24 00:40:55 +00:00
|
|
|
if (format + fmtIndex != end)
|
|
|
|
font = static_cast<FontType>(*(format + fmtIndex) & FONT_MASK_FONT);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
UTF8Char ch;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
int len = StrUtils::Utf8CharSizeAt(text, index);
|
|
|
|
if (len >= 1)
|
|
|
|
ch.c1 = text[index];
|
|
|
|
if (len >= 2)
|
|
|
|
ch.c2 = text[index+1];
|
|
|
|
if (len >= 3)
|
|
|
|
ch.c3 = text[index+2];
|
|
|
|
|
|
|
|
width += GetCharWidth(ch, font, size, width);
|
|
|
|
|
|
|
|
index += len;
|
|
|
|
fmtIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return width;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2013-05-12 16:38:01 +00:00
|
|
|
float CText::GetStringWidth(std::string text, FontType font, float size)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
assert(font != FONT_BUTTON);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2013-05-12 16:38:01 +00:00
|
|
|
// Skip special chars
|
|
|
|
for (char& c : text)
|
|
|
|
{
|
2014-03-02 00:15:59 +00:00
|
|
|
if (c < 32 && c >= 0)
|
2013-05-12 16:38:01 +00:00
|
|
|
c = ':';
|
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CachedFont* cf = GetOrOpenFont(font, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
assert(cf != nullptr);
|
2012-08-11 15:17:04 +00:00
|
|
|
Math::IntPoint wndSize;
|
|
|
|
TTF_SizeUTF8(cf->font, text.c_str(), &wndSize.x, &wndSize.y);
|
|
|
|
Math::Point ifSize = m_engine->WindowToInterfaceSize(wndSize);
|
|
|
|
return ifSize.x;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2015-07-16 18:24:54 +00:00
|
|
|
if (font == FONT_BUTTON)
|
|
|
|
{
|
2015-04-07 10:26:44 +00:00
|
|
|
Math::IntPoint windowSize = m_engine->GetWindowSize();
|
|
|
|
float height = GetHeight(FONT_COLOBOT, size);
|
|
|
|
float width = height*(static_cast<float>(windowSize.y)/windowSize.x);
|
|
|
|
return width;
|
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2013-05-12 16:38:01 +00:00
|
|
|
int width = 1;
|
2014-03-02 00:15:59 +00:00
|
|
|
if (ch.c1 < 32 && ch.c1 >= 0)
|
2013-05-12 16:38:01 +00:00
|
|
|
{
|
|
|
|
if (ch.c1 == '\t')
|
|
|
|
width = m_tabSize;
|
|
|
|
|
|
|
|
// TODO: tab sizing at intervals?
|
|
|
|
|
|
|
|
ch.c1 = ':';
|
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CachedFont* cf = GetOrOpenFont(font, size);
|
2012-08-06 18:20:50 +00:00
|
|
|
assert(cf != nullptr);
|
|
|
|
|
2013-10-12 19:12:54 +00:00
|
|
|
Math::Point charSize;
|
2012-08-06 18:20:50 +00:00
|
|
|
auto it = cf->cache.find(ch);
|
|
|
|
if (it != cf->cache.end())
|
2013-10-12 19:12:54 +00:00
|
|
|
{
|
2015-09-25 09:11:35 +00:00
|
|
|
charSize = m_engine->WindowToInterfaceSize((*it).second.charSize);
|
2013-10-12 19:12:54 +00:00
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
else
|
2013-10-12 19:12:54 +00:00
|
|
|
{
|
|
|
|
Math::IntPoint wndSize;
|
|
|
|
std::string text;
|
|
|
|
text.append({ch.c1, ch.c2, ch.c3});
|
|
|
|
TTF_SizeUTF8(cf->font, text.c_str(), &wndSize.x, &wndSize.y);
|
|
|
|
charSize = m_engine->WindowToInterfaceSize(wndSize);
|
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2013-10-12 19:12:54 +00:00
|
|
|
return charSize.x * width;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-09 22:49:38 +00:00
|
|
|
int CText::Justify(const std::string &text, std::vector<FontMetaChar>::iterator format,
|
2013-02-24 00:40:55 +00:00
|
|
|
std::vector<FontMetaChar>::iterator end,
|
2012-09-26 14:31:04 +00:00
|
|
|
float size, float width)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
float pos = 0.0f;
|
|
|
|
int cut = 0;
|
|
|
|
unsigned int index = 0;
|
|
|
|
unsigned int fmtIndex = 0;
|
|
|
|
while (index < text.length())
|
|
|
|
{
|
2012-09-27 21:42:52 +00:00
|
|
|
FontType font = FONT_COLOBOT;
|
2013-02-24 00:40:55 +00:00
|
|
|
if (format + fmtIndex != end)
|
|
|
|
font = static_cast<FontType>(*(format + fmtIndex) & FONT_MASK_FONT);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
UTF8Char ch;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
int len = StrUtils::Utf8CharSizeAt(text, index);
|
|
|
|
if (len >= 1)
|
|
|
|
ch.c1 = text[index];
|
|
|
|
if (len >= 2)
|
|
|
|
ch.c2 = text[index+1];
|
|
|
|
if (len >= 3)
|
|
|
|
ch.c3 = text[index+2];
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
if (font != FONT_BUTTON)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
|
|
|
if (ch.c1 == '\n')
|
|
|
|
return index+1;
|
|
|
|
if (ch.c1 == ' ')
|
|
|
|
cut = index+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += GetCharWidth(ch, font, size, pos);
|
|
|
|
if (pos > width)
|
|
|
|
{
|
|
|
|
if (cut == 0) return index;
|
|
|
|
else return cut;
|
|
|
|
}
|
|
|
|
|
|
|
|
index += len;
|
|
|
|
fmtIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
int CText::Justify(const std::string &text, FontType font, float size, float width)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
assert(font != FONT_BUTTON);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
float pos = 0.0f;
|
|
|
|
int cut = 0;
|
|
|
|
unsigned int index = 0;
|
|
|
|
while (index < text.length())
|
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
UTF8Char ch;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
int len = StrUtils::Utf8CharSizeAt(text, index);
|
|
|
|
if (len >= 1)
|
|
|
|
ch.c1 = text[index];
|
|
|
|
if (len >= 2)
|
|
|
|
ch.c2 = text[index+1];
|
|
|
|
if (len >= 3)
|
|
|
|
ch.c3 = text[index+2];
|
|
|
|
|
|
|
|
if (ch.c1 == '\n')
|
2012-10-11 21:09:29 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
return index+1;
|
2012-10-11 21:09:29 +00:00
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
if (ch.c1 == ' ' )
|
|
|
|
cut = index+1;
|
|
|
|
|
|
|
|
pos += GetCharWidth(ch, font, size, pos);
|
|
|
|
if (pos > width)
|
|
|
|
{
|
|
|
|
if (cut == 0) return index;
|
|
|
|
else return cut;
|
|
|
|
}
|
2012-10-11 21:09:29 +00:00
|
|
|
index += len;
|
2012-08-06 18:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 22:49:38 +00:00
|
|
|
int CText::Detect(const std::string &text, std::vector<FontMetaChar>::iterator format,
|
2013-02-24 00:40:55 +00:00
|
|
|
std::vector<FontMetaChar>::iterator end,
|
2012-09-26 14:31:04 +00:00
|
|
|
float size, float offset)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
float pos = 0.0f;
|
|
|
|
unsigned int index = 0;
|
|
|
|
unsigned int fmtIndex = 0;
|
|
|
|
while (index < text.length())
|
|
|
|
{
|
2012-09-27 21:42:52 +00:00
|
|
|
FontType font = FONT_COLOBOT;
|
2013-02-24 00:40:55 +00:00
|
|
|
|
|
|
|
if (format + fmtIndex != end)
|
|
|
|
font = static_cast<FontType>(*(format + fmtIndex) & FONT_MASK_FONT);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
// TODO: if (font == FONT_BUTTON)
|
2013-02-24 00:40:55 +00:00
|
|
|
//if (font == FONT_BUTTON) continue;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
UTF8Char ch;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
int len = StrUtils::Utf8CharSizeAt(text, index);
|
|
|
|
if (len >= 1)
|
|
|
|
ch.c1 = text[index];
|
|
|
|
if (len >= 2)
|
|
|
|
ch.c2 = text[index+1];
|
|
|
|
if (len >= 3)
|
|
|
|
ch.c3 = text[index+2];
|
|
|
|
|
|
|
|
if (ch.c1 == '\n')
|
|
|
|
return index;
|
|
|
|
|
|
|
|
float width = GetCharWidth(ch, font, size, pos);
|
|
|
|
if (offset <= pos + width/2.0f)
|
|
|
|
return index;
|
|
|
|
|
|
|
|
pos += width;
|
|
|
|
index += len;
|
|
|
|
fmtIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
int CText::Detect(const std::string &text, FontType font, float size, float offset)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
assert(font != FONT_BUTTON);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
float pos = 0.0f;
|
|
|
|
unsigned int index = 0;
|
|
|
|
while (index < text.length())
|
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
UTF8Char ch;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
int len = StrUtils::Utf8CharSizeAt(text, index);
|
|
|
|
if (len >= 1)
|
|
|
|
ch.c1 = text[index];
|
|
|
|
if (len >= 2)
|
|
|
|
ch.c2 = text[index+1];
|
|
|
|
if (len >= 3)
|
|
|
|
ch.c3 = text[index+2];
|
|
|
|
|
|
|
|
index += len;
|
|
|
|
|
|
|
|
if (ch.c1 == '\n')
|
|
|
|
return index;
|
|
|
|
|
|
|
|
float width = GetCharWidth(ch, font, size, pos);
|
|
|
|
if (offset <= pos + width/2.0f)
|
|
|
|
return index;
|
|
|
|
|
|
|
|
pos += width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2013-05-12 16:38:01 +00:00
|
|
|
UTF8Char CText::TranslateSpecialChar(int specialChar)
|
|
|
|
{
|
|
|
|
UTF8Char ch;
|
|
|
|
|
|
|
|
switch (specialChar)
|
|
|
|
{
|
|
|
|
case CHAR_TAB:
|
|
|
|
ch.c1 = ':';
|
|
|
|
ch.c2 = 0;
|
|
|
|
ch.c3 = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAR_NEWLINE:
|
|
|
|
// Unicode: U+21B2
|
|
|
|
ch.c1 = 0xE2;
|
|
|
|
ch.c2 = 0x86;
|
|
|
|
ch.c3 = 0xB2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAR_DOT:
|
|
|
|
// Unicode: U+23C5
|
|
|
|
ch.c1 = 0xE2;
|
|
|
|
ch.c2 = 0x8F;
|
|
|
|
ch.c3 = 0x85;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAR_SQUARE:
|
|
|
|
// Unicode: U+25FD
|
|
|
|
ch.c1 = 0xE2;
|
|
|
|
ch.c2 = 0x97;
|
|
|
|
ch.c3 = 0xBD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAR_SKIP_RIGHT:
|
|
|
|
// Unicode: U+25B6
|
|
|
|
ch.c1 = 0xE2;
|
|
|
|
ch.c2 = 0x96;
|
|
|
|
ch.c3 = 0xB6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHAR_SKIP_LEFT:
|
|
|
|
// Unicode: U+25C0
|
|
|
|
ch.c1 = 0xE2;
|
|
|
|
ch.c2 = 0x97;
|
|
|
|
ch.c3 = 0x80;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ch.c1 = '?';
|
|
|
|
ch.c2 = 0;
|
|
|
|
ch.c3 = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2013-02-09 22:49:38 +00:00
|
|
|
void CText::DrawString(const std::string &text, std::vector<FontMetaChar>::iterator format,
|
2013-02-24 00:40:55 +00:00
|
|
|
std::vector<FontMetaChar>::iterator end,
|
2012-09-30 08:56:35 +00:00
|
|
|
float size, Math::Point pos, float width, int eol, Color color)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
m_engine->SetState(ENG_RSTATE_TEXT);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
float start = pos.x;
|
|
|
|
|
|
|
|
unsigned int fmtIndex = 0;
|
2012-09-24 21:55:52 +00:00
|
|
|
|
2012-09-26 14:31:04 +00:00
|
|
|
std::vector<UTF8Char> chars;
|
2015-04-07 10:26:44 +00:00
|
|
|
StringToUTFCharList(text, chars, format, end);
|
2012-09-26 14:31:04 +00:00
|
|
|
for (auto it = chars.begin(); it != chars.end(); ++it)
|
|
|
|
{
|
2012-09-27 21:42:52 +00:00
|
|
|
FontType font = FONT_COLOBOT;
|
2013-02-24 00:40:55 +00:00
|
|
|
if (format + fmtIndex != end)
|
|
|
|
font = static_cast<FontType>(*(format + fmtIndex) & FONT_MASK_FONT);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-24 21:55:52 +00:00
|
|
|
UTF8Char ch = *it;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
float offset = pos.x - start;
|
|
|
|
float cw = GetCharWidth(ch, font, size, offset);
|
|
|
|
if (offset + cw > width) // exceeds the maximum width?
|
|
|
|
{
|
2013-05-27 20:26:44 +00:00
|
|
|
ch = TranslateSpecialChar(CHAR_SKIP_RIGHT);
|
2013-05-12 16:38:01 +00:00
|
|
|
cw = GetCharWidth(ch, font, size, offset);
|
|
|
|
pos.x = start + width - cw;
|
|
|
|
color = Color(1.0f, 0.0f, 0.0f);
|
|
|
|
DrawCharAndAdjustPos(ch, font, size, pos, color);
|
2012-08-06 18:20:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-18 14:00:44 +00:00
|
|
|
Color c = color;
|
2012-09-19 21:50:28 +00:00
|
|
|
FontHighlight hl = static_cast<FontHighlight>(format[fmtIndex] & FONT_MASK_HIGHLIGHT);
|
|
|
|
if (hl != FONT_HIGHLIGHT_NONE)
|
2012-08-06 18:20:50 +00:00
|
|
|
{
|
2015-08-18 14:00:44 +00:00
|
|
|
if (hl == FONT_HIGHLIGHT_TOKEN)
|
|
|
|
{
|
2015-08-18 17:52:51 +00:00
|
|
|
c = Color(0.490f, 0.380f, 0.165f, 1.0f); // #7D612A
|
2015-08-18 14:00:44 +00:00
|
|
|
}
|
|
|
|
else if (hl == FONT_HIGHLIGHT_TYPE)
|
|
|
|
{
|
2015-08-18 17:52:51 +00:00
|
|
|
c = Color(0.31f, 0.443f, 0.196f, 1.0f); // #4F7132
|
2015-08-18 14:00:44 +00:00
|
|
|
}
|
|
|
|
else if (hl == FONT_HIGHLIGHT_CONST)
|
|
|
|
{
|
|
|
|
c = Color(0.882f, 0.176f, 0.176f, 1.0f); // #E12D2D
|
|
|
|
}
|
|
|
|
else if (hl == FONT_HIGHLIGHT_THIS)
|
|
|
|
{
|
|
|
|
c = Color(0.545f, 0.329f, 0.608f, 1.0f); // #8B549B
|
|
|
|
}
|
|
|
|
else if (hl == FONT_HIGHLIGHT_COMMENT)
|
|
|
|
{
|
|
|
|
c = Color(0.251f, 0.271f, 0.306f, 1.0f); // #40454E
|
|
|
|
}
|
|
|
|
else if (hl == FONT_HIGHLIGHT_KEYWORD)
|
|
|
|
{
|
2015-08-18 17:52:51 +00:00
|
|
|
c = Color(0.239f, 0.431f, 0.588f, 1.0f); // #3D6E96
|
2015-08-18 14:00:44 +00:00
|
|
|
}
|
|
|
|
else if (hl == FONT_HIGHLIGHT_STRING)
|
|
|
|
{
|
2015-08-18 17:52:51 +00:00
|
|
|
c = Color(0.239f, 0.384f, 0.341f, 1.0f); // #3D6257
|
2015-08-18 14:00:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Math::Point charSize;
|
|
|
|
charSize.x = GetCharWidth(ch, font, size, offset);
|
|
|
|
charSize.y = GetHeight(font, size);
|
|
|
|
DrawHighlight(hl, pos, charSize);
|
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 14:00:44 +00:00
|
|
|
DrawCharAndAdjustPos(ch, font, size, pos, c);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2014-03-25 20:56:40 +00:00
|
|
|
// increment fmtIndex for each byte in multibyte character
|
|
|
|
if ( ch.c1 != 0 )
|
|
|
|
fmtIndex++;
|
|
|
|
if ( ch.c2 != 0 )
|
|
|
|
fmtIndex++;
|
|
|
|
if ( ch.c3 != 0 )
|
|
|
|
fmtIndex++;
|
2012-08-06 18:20:50 +00:00
|
|
|
}
|
|
|
|
|
2013-05-12 16:38:01 +00:00
|
|
|
if (eol != 0)
|
|
|
|
{
|
|
|
|
FontType font = FONT_COLOBOT;
|
|
|
|
UTF8Char ch = TranslateSpecialChar(eol);
|
|
|
|
color = Color(1.0f, 0.0f, 0.0f);
|
|
|
|
DrawCharAndAdjustPos(ch, font, size, pos, color);
|
|
|
|
}
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 14:31:04 +00:00
|
|
|
void CText::StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars)
|
|
|
|
{
|
|
|
|
unsigned int index = 0;
|
2012-09-29 08:40:11 +00:00
|
|
|
unsigned int totalLength = text.length();
|
|
|
|
while (index < totalLength)
|
2012-09-26 14:31:04 +00:00
|
|
|
{
|
|
|
|
UTF8Char ch;
|
|
|
|
|
|
|
|
int len = StrUtils::Utf8CharSizeAt(text, index);
|
|
|
|
if (len >= 1)
|
2012-09-29 08:40:11 +00:00
|
|
|
ch.c1 = text[index];
|
2012-09-26 14:31:04 +00:00
|
|
|
if (len >= 2)
|
2012-09-29 08:40:11 +00:00
|
|
|
ch.c2 = text[index+1];
|
2012-09-26 14:31:04 +00:00
|
|
|
if (len >= 3)
|
2012-09-29 08:40:11 +00:00
|
|
|
ch.c3 = text[index+2];
|
2012-09-26 14:31:04 +00:00
|
|
|
|
|
|
|
index += len;
|
|
|
|
|
|
|
|
chars.push_back(ch);
|
|
|
|
}
|
2012-09-24 21:55:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
void CText::StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars,
|
|
|
|
std::vector<FontMetaChar>::iterator format,
|
|
|
|
std::vector<FontMetaChar>::iterator end)
|
2015-04-07 10:26:44 +00:00
|
|
|
{
|
|
|
|
unsigned int index = 0;
|
|
|
|
unsigned int totalLength = text.length();
|
|
|
|
while (index < totalLength)
|
|
|
|
{
|
|
|
|
UTF8Char ch;
|
|
|
|
|
|
|
|
FontType font = FONT_COLOBOT;
|
2015-08-12 19:07:16 +00:00
|
|
|
if (format + index != end)
|
2015-04-07 10:26:44 +00:00
|
|
|
font = static_cast<FontType>(*(format + index) & FONT_MASK_FONT);
|
|
|
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if(font == FONT_BUTTON)
|
|
|
|
{
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = StrUtils::Utf8CharSizeAt(text, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len >= 1)
|
|
|
|
ch.c1 = text[index];
|
|
|
|
if (len >= 2)
|
|
|
|
ch.c2 = text[index+1];
|
|
|
|
if (len >= 3)
|
|
|
|
ch.c3 = text[index+2];
|
|
|
|
|
|
|
|
index += len;
|
|
|
|
|
|
|
|
chars.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
void CText::DrawString(const std::string &text, FontType font,
|
2012-09-30 08:56:35 +00:00
|
|
|
float size, Math::Point pos, float width, int eol, Color color)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
assert(font != FONT_BUTTON);
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
m_engine->SetState(ENG_RSTATE_TEXT);
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-09-26 14:31:04 +00:00
|
|
|
std::vector<UTF8Char> chars;
|
|
|
|
StringToUTFCharList(text, chars);
|
|
|
|
for (auto it = chars.begin(); it != chars.end(); ++it)
|
|
|
|
{
|
2012-09-30 08:56:35 +00:00
|
|
|
DrawCharAndAdjustPos(*it, font, size, pos, color);
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
void CText::DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-08-06 18:20:50 +00:00
|
|
|
// Gradient colors
|
2012-09-19 21:50:28 +00:00
|
|
|
Color grad[4];
|
2012-08-06 18:20:50 +00:00
|
|
|
|
|
|
|
// TODO: switch to alpha factors
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-08-06 18:20:50 +00:00
|
|
|
switch (hl)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
case FONT_HIGHLIGHT_LINK:
|
|
|
|
grad[0] = grad[1] = grad[2] = grad[3] = Color(0.0f, 0.0f, 1.0f, 0.5f);
|
2012-08-03 21:23:13 +00:00
|
|
|
break;
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
case FONT_HIGHLIGHT_KEY:
|
2012-08-06 18:20:50 +00:00
|
|
|
grad[0] = grad[1] = grad[2] = grad[3] =
|
2012-09-19 21:50:28 +00:00
|
|
|
Color(192.0f / 256.0f, 192.0f / 256.0f, 192.0f / 256.0f, 0.5f);
|
2012-08-06 18:20:50 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-11 15:17:04 +00:00
|
|
|
Math::IntPoint vsize = m_engine->GetWindowSize();
|
2012-08-06 18:20:50 +00:00
|
|
|
float h = 0.0f;
|
2012-08-11 15:17:04 +00:00
|
|
|
if (vsize.y <= 768.0f) // 1024x768 or less?
|
|
|
|
h = 1.01f / vsize.y; // 1 pixel
|
2012-08-06 18:20:50 +00:00
|
|
|
else // more than 1024x768?
|
2012-08-11 15:17:04 +00:00
|
|
|
h = 2.0f / vsize.y; // 2 pixels
|
2012-08-03 21:23:13 +00:00
|
|
|
|
|
|
|
Math::Point p1, p2;
|
|
|
|
p1.x = pos.x;
|
2012-08-11 15:17:04 +00:00
|
|
|
p2.x = pos.x + size.x;
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
if (hl == FONT_HIGHLIGHT_LINK)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
p1.y = pos.y;
|
|
|
|
p2.y = pos.y + h; // just emphasized
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p1.y = pos.y;
|
2012-08-11 15:17:04 +00:00
|
|
|
p2.y = pos.y + size.y;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-29 17:35:14 +00:00
|
|
|
m_device->SetTextureEnabled(0, false);
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
VertexCol quad[] =
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2012-09-19 21:50:28 +00:00
|
|
|
VertexCol(Math::Vector(p1.x, p1.y, 0.0f), grad[3]),
|
|
|
|
VertexCol(Math::Vector(p1.x, p2.y, 0.0f), grad[0]),
|
|
|
|
VertexCol(Math::Vector(p2.x, p1.y, 0.0f), grad[2]),
|
|
|
|
VertexCol(Math::Vector(p2.x, p2.y, 0.0f), grad[1])
|
2012-08-03 21:23:13 +00:00
|
|
|
};
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, quad, 4);
|
2012-08-03 21:23:13 +00:00
|
|
|
m_engine->AddStatisticTriangle(2);
|
|
|
|
|
2012-09-29 17:35:14 +00:00
|
|
|
m_device->SetTextureEnabled(0, true);
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-30 08:56:35 +00:00
|
|
|
void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos, Color color)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2015-04-07 10:06:43 +00:00
|
|
|
if(font == FONT_BUTTON)
|
|
|
|
{
|
|
|
|
Math::IntPoint windowSize = m_engine->GetWindowSize();
|
|
|
|
float height = GetHeight(FONT_COLOBOT, size);
|
|
|
|
float width = height*(static_cast<float>(windowSize.y)/windowSize.x);
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2015-04-07 10:06:43 +00:00
|
|
|
Math::Point p1(pos.x, pos.y);
|
|
|
|
Math::Point p2(pos.x + width, pos.y + height);
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2015-04-07 10:06:43 +00:00
|
|
|
Math::Vector n(0.0f, 0.0f, -1.0f); // normal
|
2012-09-30 08:56:35 +00:00
|
|
|
|
2015-04-07 10:06:43 +00:00
|
|
|
// For whatever reason ch.c1 is a SIGNED char, we need to fix that
|
2015-04-07 10:26:44 +00:00
|
|
|
unsigned char icon = static_cast<unsigned char>(ch.c1);
|
2015-08-18 20:56:18 +00:00
|
|
|
if ( icon >= 128 )
|
2015-04-07 10:06:43 +00:00
|
|
|
{
|
|
|
|
icon -= 128;
|
|
|
|
m_engine->SetTexture("textures/interface/button3.png");
|
|
|
|
m_engine->SetState(ENG_RSTATE_TTEXTURE_WHITE);
|
|
|
|
}
|
|
|
|
else if ( icon >= 64 )
|
|
|
|
{
|
|
|
|
icon -= 64;
|
|
|
|
m_engine->SetTexture("textures/interface/button2.png");
|
|
|
|
m_engine->SetState(ENG_RSTATE_TTEXTURE_WHITE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_engine->SetTexture("textures/interface/button1.png");
|
|
|
|
m_engine->SetState(ENG_RSTATE_TTEXTURE_WHITE);
|
|
|
|
}
|
2013-05-12 16:38:01 +00:00
|
|
|
|
2015-04-07 10:06:43 +00:00
|
|
|
Math::Point uv1, uv2;
|
|
|
|
uv1.x = (32.0f / 256.0f) * (icon%8);
|
|
|
|
uv1.y = (32.0f / 256.0f) * (icon/8);
|
|
|
|
uv2.x = (32.0f / 256.0f) + uv1.x;
|
|
|
|
uv2.y = (32.0f / 256.0f) + uv1.y;
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2015-04-07 10:06:43 +00:00
|
|
|
float dp = 0.5f / 256.0f;
|
|
|
|
uv1.x += dp;
|
|
|
|
uv1.y += dp;
|
|
|
|
uv2.x -= dp;
|
|
|
|
uv2.y -= dp;
|
|
|
|
|
|
|
|
Vertex quad[4] =
|
|
|
|
{
|
|
|
|
Vertex(Math::Vector(p1.x, p1.y, 0.0f), n, Math::Point(uv1.x, uv2.y)),
|
|
|
|
Vertex(Math::Vector(p1.x, p2.y, 0.0f), n, Math::Point(uv1.x, uv1.y)),
|
|
|
|
Vertex(Math::Vector(p2.x, p1.y, 0.0f), n, Math::Point(uv2.x, uv2.y)),
|
|
|
|
Vertex(Math::Vector(p2.x, p2.y, 0.0f), n, Math::Point(uv2.x, uv1.y))
|
|
|
|
};
|
|
|
|
|
|
|
|
m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, quad, 4, color);
|
|
|
|
m_engine->AddStatisticTriangle(2);
|
|
|
|
|
2015-04-07 10:26:44 +00:00
|
|
|
pos.x += width;
|
2015-04-07 10:06:43 +00:00
|
|
|
|
|
|
|
// Don't forget to restore the state!
|
|
|
|
m_engine->SetState(ENG_RSTATE_TEXT);
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-07 10:06:43 +00:00
|
|
|
int width = 1;
|
|
|
|
if (ch.c1 > 0 && ch.c1 < 32)
|
|
|
|
{
|
|
|
|
if (ch.c1 == '\t')
|
2015-08-07 12:16:01 +00:00
|
|
|
{
|
|
|
|
color = Color(1.0f, 0.0f, 0.0f, 1.0f);
|
2015-04-07 10:06:43 +00:00
|
|
|
width = m_tabSize;
|
2015-08-07 12:16:01 +00:00
|
|
|
}
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2015-04-07 10:06:43 +00:00
|
|
|
ch = TranslateSpecialChar(ch.c1);
|
|
|
|
}
|
2012-08-06 18:20:50 +00:00
|
|
|
|
2015-08-07 11:24:45 +00:00
|
|
|
CharTexture tex = GetCharTexture(ch, font, size);
|
2015-04-07 10:06:43 +00:00
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
Math::Point charInterfaceSize = m_engine->WindowToInterfaceSize(tex.charSize);
|
|
|
|
Math::Point texInterfaceSize = m_engine->WindowToInterfaceSize(tex.tileSize);
|
2015-09-25 09:11:35 +00:00
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
Math::Point p1(pos.x, pos.y + charInterfaceSize.y - texInterfaceSize.y);
|
|
|
|
Math::Point p2(pos.x + texInterfaceSize.x, pos.y + charInterfaceSize.y);
|
|
|
|
|
|
|
|
Math::Point texCoord1(static_cast<float>(tex.charPos.x) / FONT_TEXTURE_SIZE.x,
|
|
|
|
static_cast<float>(tex.charPos.y) / FONT_TEXTURE_SIZE.y);
|
|
|
|
Math::Point texCoord2(static_cast<float>(tex.charPos.x + tex.tileSize.x) / FONT_TEXTURE_SIZE.x,
|
|
|
|
static_cast<float>(tex.charPos.y + tex.tileSize.y) / FONT_TEXTURE_SIZE.y);
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2015-04-07 10:06:43 +00:00
|
|
|
Math::Vector n(0.0f, 0.0f, -1.0f); // normal
|
|
|
|
|
|
|
|
Vertex quad[4] =
|
|
|
|
{
|
2016-03-18 01:01:06 +00:00
|
|
|
Vertex(Math::Vector(p1.x, p1.y, 0.0f), n, Math::Point(texCoord1.x, texCoord2.y)),
|
|
|
|
Vertex(Math::Vector(p1.x, p2.y, 0.0f), n, Math::Point(texCoord1.x, texCoord1.y)),
|
|
|
|
Vertex(Math::Vector(p2.x, p1.y, 0.0f), n, Math::Point(texCoord2.x, texCoord2.y)),
|
|
|
|
Vertex(Math::Vector(p2.x, p2.y, 0.0f), n, Math::Point(texCoord2.x, texCoord1.y))
|
2015-04-07 10:06:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
m_device->SetTexture(0, tex.id);
|
|
|
|
m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, quad, 4, color);
|
|
|
|
m_engine->AddStatisticTriangle(2);
|
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
pos.x += charInterfaceSize.x * width;
|
2015-04-07 10:06:43 +00:00
|
|
|
}
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CachedFont* CText::GetOrOpenFont(FontType font, float size)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2013-01-03 23:29:19 +00:00
|
|
|
Math::IntPoint windowSize = m_engine->GetWindowSize();
|
|
|
|
int pointSize = static_cast<int>(size * (windowSize.Length() / REFERENCE_SIZE.Length()));
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
if (m_lastCachedFont != nullptr &&
|
|
|
|
m_lastFontType == font &&
|
|
|
|
m_lastFontSize == pointSize)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
2015-08-12 19:07:16 +00:00
|
|
|
return m_lastCachedFont;
|
2012-08-03 21:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto it = m_fonts.find(font);
|
|
|
|
if (it == m_fonts.end())
|
|
|
|
{
|
|
|
|
m_error = std::string("Invalid font type ") + StrUtils::ToString<int>(static_cast<int>(font));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
MultisizeFont* mf = it->second.get();
|
2012-08-03 21:23:13 +00:00
|
|
|
|
|
|
|
auto jt = mf->fonts.find(pointSize);
|
|
|
|
if (jt != mf->fonts.end())
|
|
|
|
{
|
2015-08-12 19:07:16 +00:00
|
|
|
m_lastCachedFont = jt->second.get();
|
2012-08-03 21:23:13 +00:00
|
|
|
m_lastFontType = font;
|
|
|
|
m_lastFontSize = pointSize;
|
|
|
|
return m_lastCachedFont;
|
|
|
|
}
|
|
|
|
|
2016-02-11 15:12:16 +00:00
|
|
|
auto file = CResourceManager::GetSDLMemoryHandler(mf->fileName);
|
2015-07-16 18:24:54 +00:00
|
|
|
if (!file->IsOpen())
|
2014-08-06 10:27:17 +00:00
|
|
|
{
|
2016-02-18 17:07:29 +00:00
|
|
|
m_error = std::string("Unable to open file '") + mf->fileName + "' (font size = " + StrUtils::ToString<float>(size) + ")";
|
2014-08-06 10:27:17 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-18 17:07:29 +00:00
|
|
|
GetLogger()->Debug("Loaded font file %s (font size = %.1f)\n", mf->fileName.c_str(), size);
|
2015-07-16 18:24:54 +00:00
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
auto newFont = MakeUnique<CachedFont>(std::move(file), pointSize);
|
|
|
|
if (newFont->font == nullptr)
|
2015-07-16 18:24:54 +00:00
|
|
|
{
|
2012-08-03 21:23:13 +00:00
|
|
|
m_error = std::string("TTF_OpenFont error ") + std::string(TTF_GetError());
|
2015-07-16 18:24:54 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2015-08-12 19:07:16 +00:00
|
|
|
m_lastCachedFont = newFont.get();
|
|
|
|
mf->fonts[pointSize] = std::move(newFont);
|
2012-08-03 21:23:13 +00:00
|
|
|
return m_lastCachedFont;
|
|
|
|
}
|
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
CharTexture CText::GetCharTexture(UTF8Char ch, FontType font, float size)
|
|
|
|
{
|
|
|
|
CachedFont* cf = GetOrOpenFont(font, size);
|
|
|
|
|
|
|
|
if (cf == nullptr)
|
|
|
|
return CharTexture();
|
|
|
|
|
|
|
|
auto it = cf->cache.find(ch);
|
|
|
|
CharTexture tex;
|
|
|
|
if (it != cf->cache.end())
|
|
|
|
{
|
|
|
|
tex = (*it).second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tex = CreateCharTexture(ch, cf);
|
|
|
|
|
|
|
|
if (tex.id == 0) // invalid
|
|
|
|
return CharTexture();
|
|
|
|
|
|
|
|
cf->cache[ch] = tex;
|
|
|
|
}
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::IntPoint CText::GetFontTextureSize()
|
|
|
|
{
|
|
|
|
return FONT_TEXTURE_SIZE;
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:50:28 +00:00
|
|
|
CharTexture CText::CreateCharTexture(UTF8Char ch, CachedFont* font)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
CharTexture texture;
|
|
|
|
|
|
|
|
SDL_Surface* textSurface = nullptr;
|
|
|
|
SDL_Color white = {255, 255, 255, 0};
|
2012-08-06 18:20:50 +00:00
|
|
|
char str[] = { ch.c1, ch.c2, ch.c3, '\0' };
|
2012-08-03 21:23:13 +00:00
|
|
|
textSurface = TTF_RenderUTF8_Blended(font->font, str, white);
|
|
|
|
|
|
|
|
if (textSurface == nullptr)
|
|
|
|
{
|
|
|
|
m_error = "TTF_Render error";
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
Math::IntPoint tileSize(Math::NextPowerOfTwo(textSurface->w),
|
|
|
|
Math::NextPowerOfTwo(textSurface->h));
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
FontTexture* fontTexture = GetOrCreateFontTexture(tileSize);
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
if (fontTexture == nullptr)
|
2012-08-03 21:23:13 +00:00
|
|
|
{
|
|
|
|
m_error = "Texture create error";
|
|
|
|
}
|
2012-09-16 18:00:25 +00:00
|
|
|
else
|
|
|
|
{
|
2016-03-18 01:01:06 +00:00
|
|
|
texture.id = fontTexture->id;
|
|
|
|
texture.charPos = GetNextTilePos(*fontTexture);
|
2015-09-25 09:11:35 +00:00
|
|
|
texture.charSize = Math::IntPoint(textSurface->w, textSurface->h);
|
2016-03-18 01:01:06 +00:00
|
|
|
texture.tileSize = tileSize;
|
|
|
|
|
|
|
|
ImageData imageData;
|
|
|
|
imageData.surface = textSurface;
|
|
|
|
|
|
|
|
Texture tex;
|
|
|
|
tex.id = texture.id;
|
|
|
|
m_device->UpdateTexture(tex, texture.charPos, &imageData, TEX_IMG_RGBA);
|
|
|
|
|
|
|
|
imageData.surface = nullptr;
|
|
|
|
|
|
|
|
--fontTexture->freeSlots;
|
2012-09-16 18:00:25 +00:00
|
|
|
}
|
2012-08-03 21:23:13 +00:00
|
|
|
|
2012-09-16 18:00:25 +00:00
|
|
|
SDL_FreeSurface(textSurface);
|
2012-08-03 21:23:13 +00:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
2012-09-19 21:50:28 +00:00
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
FontTexture* CText::GetOrCreateFontTexture(Math::IntPoint tileSize)
|
2015-08-07 11:24:45 +00:00
|
|
|
{
|
2016-03-18 01:01:06 +00:00
|
|
|
for (auto& fontTexture : m_fontTextures)
|
2015-08-07 11:24:45 +00:00
|
|
|
{
|
2016-03-18 01:01:06 +00:00
|
|
|
if (fontTexture.tileSize == tileSize && fontTexture.freeSlots > 0)
|
|
|
|
return &fontTexture;
|
2015-08-07 11:24:45 +00:00
|
|
|
}
|
2016-03-18 01:01:06 +00:00
|
|
|
|
|
|
|
FontTexture newFontTexture = CreateFontTexture(tileSize);
|
|
|
|
if (newFontTexture.id == 0)
|
2015-08-07 11:24:45 +00:00
|
|
|
{
|
2016-03-18 01:01:06 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-07 11:24:45 +00:00
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
m_fontTextures.push_back(newFontTexture);
|
|
|
|
return &m_fontTextures.back();
|
|
|
|
}
|
2015-08-07 11:24:45 +00:00
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
FontTexture CText::CreateFontTexture(Math::IntPoint tileSize)
|
|
|
|
{
|
|
|
|
SDL_Surface* textureSurface = SDL_CreateRGBSurface(0, FONT_TEXTURE_SIZE.x, FONT_TEXTURE_SIZE.y, 32,
|
|
|
|
0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
|
|
|
|
ImageData data;
|
|
|
|
data.surface = textureSurface;
|
|
|
|
|
|
|
|
TextureCreateParams createParams;
|
|
|
|
createParams.format = TEX_IMG_RGBA;
|
|
|
|
createParams.filter = TEX_FILTER_NEAREST;
|
|
|
|
createParams.mipmap = false;
|
|
|
|
|
|
|
|
Texture tex = m_device->CreateTexture(&data, createParams);
|
|
|
|
|
|
|
|
data.surface = nullptr;
|
|
|
|
SDL_FreeSurface(textureSurface);
|
|
|
|
|
|
|
|
FontTexture fontTexture;
|
|
|
|
fontTexture.id = tex.id;
|
|
|
|
fontTexture.tileSize = tileSize;
|
|
|
|
int horizontalTiles = FONT_TEXTURE_SIZE.x / tileSize.x;
|
|
|
|
int verticalTiles = FONT_TEXTURE_SIZE.y / tileSize.y;
|
|
|
|
fontTexture.freeSlots = horizontalTiles * verticalTiles;
|
|
|
|
return fontTexture;
|
2015-08-07 11:24:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 01:01:06 +00:00
|
|
|
Math::IntPoint CText::GetNextTilePos(const FontTexture& fontTexture)
|
|
|
|
{
|
|
|
|
int horizontalTiles = FONT_TEXTURE_SIZE.x / fontTexture.tileSize.x;
|
|
|
|
int verticalTiles = FONT_TEXTURE_SIZE.y / fontTexture.tileSize.y;
|
|
|
|
|
|
|
|
int totalTiles = horizontalTiles * verticalTiles;
|
|
|
|
int tileNumber = totalTiles - fontTexture.freeSlots;
|
|
|
|
|
|
|
|
int verticalTileIndex = tileNumber / horizontalTiles;
|
|
|
|
int horizontalTileIndex = tileNumber % horizontalTiles;
|
|
|
|
|
|
|
|
return Math::IntPoint(horizontalTileIndex * fontTexture.tileSize.x,
|
|
|
|
verticalTileIndex * fontTexture.tileSize.y);
|
|
|
|
}
|
2012-09-19 21:50:28 +00:00
|
|
|
|
|
|
|
} // namespace Gfx
|