2013-08-30 14:44:23 +02:00
|
|
|
// draw_text
|
|
|
|
|
|
|
|
|
|
// Uses system fonts to draw text.
|
|
|
|
|
// Platform support will be added over time, initially just Win32.
|
|
|
|
|
|
|
|
|
|
// Caches strings in individual textures. Might later combine them into a big one
|
|
|
|
|
// with dynamically allocated space but too much trouble for now.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2017-02-25 00:25:46 +01:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
|
2017-01-16 19:08:26 +07:00
|
|
|
#include <memory>
|
2020-09-29 12:44:47 +02:00
|
|
|
#include <cstdint>
|
2013-08-30 14:44:23 +02:00
|
|
|
|
2020-10-01 13:05:04 +02:00
|
|
|
#include "Common/Data/Text/WrapText.h"
|
2020-10-05 00:05:28 +02:00
|
|
|
#include "Common/Render/DrawBuffer.h"
|
2014-08-22 20:47:40 +02:00
|
|
|
|
2016-12-25 18:18:19 +01:00
|
|
|
namespace Draw {
|
2016-12-25 21:01:57 +01:00
|
|
|
class DrawContext;
|
2016-12-25 20:54:37 +01:00
|
|
|
class Texture;
|
2016-12-25 18:18:19 +01:00
|
|
|
}
|
2013-08-30 14:44:23 +02:00
|
|
|
|
|
|
|
|
struct TextStringEntry {
|
2016-12-25 20:54:37 +01:00
|
|
|
Draw::Texture *texture;
|
2013-08-30 14:44:23 +02:00
|
|
|
int width;
|
|
|
|
|
int height;
|
|
|
|
|
int bmWidth;
|
|
|
|
|
int bmHeight;
|
|
|
|
|
int lastUsedFrame;
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-04 09:10:50 -07:00
|
|
|
struct TextMeasureEntry {
|
|
|
|
|
int width;
|
|
|
|
|
int height;
|
|
|
|
|
int lastUsedFrame;
|
|
|
|
|
};
|
|
|
|
|
|
2013-08-30 14:44:23 +02:00
|
|
|
class TextDrawer {
|
|
|
|
|
public:
|
2017-06-04 00:35:37 +02:00
|
|
|
virtual ~TextDrawer();
|
2013-08-30 14:44:23 +02:00
|
|
|
|
2017-06-05 13:04:30 +02:00
|
|
|
virtual bool IsReady() const { return true; }
|
2017-06-04 00:35:37 +02:00
|
|
|
virtual uint32_t SetFont(const char *fontName, int size, int flags) = 0;
|
|
|
|
|
virtual void SetFont(uint32_t fontHandle) = 0; // Shortcut once you've set the font once.
|
2017-06-04 10:43:34 +02:00
|
|
|
void SetFontScale(float xscale, float yscale);
|
2017-06-04 00:35:37 +02:00
|
|
|
virtual void MeasureString(const char *str, size_t len, float *w, float *h) = 0;
|
|
|
|
|
virtual void MeasureStringRect(const char *str, size_t len, const Bounds &bounds, float *w, float *h, int align = ALIGN_TOPLEFT) = 0;
|
|
|
|
|
virtual void DrawString(DrawBuffer &target, const char *str, float x, float y, uint32_t color, int align = ALIGN_TOPLEFT) = 0;
|
2020-03-10 08:58:15 -07:00
|
|
|
void DrawStringRect(DrawBuffer &target, const char *str, const Bounds &bounds, uint32_t color, int align);
|
|
|
|
|
virtual void DrawStringBitmap(std::vector<uint8_t> &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, int align = ALIGN_TOPLEFT) = 0;
|
2020-03-10 17:53:30 -07:00
|
|
|
void DrawStringBitmapRect(std::vector<uint8_t> &bitmapData, TextStringEntry &entry, Draw::DataFormat texFormat, const char *str, const Bounds &bounds, int align);
|
2013-08-30 14:44:23 +02:00
|
|
|
// Use for housekeeping like throwing out old strings.
|
2017-06-04 00:35:37 +02:00
|
|
|
virtual void OncePerFrame() = 0;
|
2017-06-04 10:43:34 +02:00
|
|
|
|
|
|
|
|
float CalculateDPIScale();
|
2020-03-10 09:20:14 -07:00
|
|
|
void SetForcedDPIScale(float dpi) {
|
|
|
|
|
dpiScale_ = dpi;
|
|
|
|
|
ignoreGlobalDpi_ = true;
|
|
|
|
|
}
|
2013-08-30 14:44:23 +02:00
|
|
|
|
2017-06-04 10:57:46 +02:00
|
|
|
// Factory function that selects implementation.
|
|
|
|
|
static TextDrawer *Create(Draw::DrawContext *draw);
|
|
|
|
|
|
2017-06-04 00:35:37 +02:00
|
|
|
protected:
|
2017-06-04 10:57:46 +02:00
|
|
|
TextDrawer(Draw::DrawContext *draw);
|
|
|
|
|
|
2017-01-30 14:33:38 +01:00
|
|
|
Draw::DrawContext *draw_;
|
2017-06-04 00:35:37 +02:00
|
|
|
virtual void ClearCache() = 0;
|
2020-03-10 19:06:30 -07:00
|
|
|
void WrapString(std::string &out, const char *str, float maxWidth, int flags);
|
2016-07-04 11:46:21 -07:00
|
|
|
|
2017-11-22 22:14:06 +01:00
|
|
|
struct CacheKey {
|
|
|
|
|
bool operator < (const CacheKey &other) const {
|
|
|
|
|
if (fontHash < other.fontHash)
|
|
|
|
|
return true;
|
|
|
|
|
if (fontHash > other.fontHash)
|
|
|
|
|
return false;
|
|
|
|
|
return text < other.text;
|
|
|
|
|
}
|
|
|
|
|
std::string text;
|
|
|
|
|
uint32_t fontHash;
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-10 09:20:14 -07:00
|
|
|
int frameCount_ = 0;
|
|
|
|
|
float fontScaleX_ = 1.0f;
|
|
|
|
|
float fontScaleY_ = 1.0f;
|
|
|
|
|
float dpiScale_ = 1.0f;
|
|
|
|
|
bool ignoreGlobalDpi_ = false;
|
2013-11-18 01:57:06 +10:00
|
|
|
};
|
2017-06-04 00:35:37 +02:00
|
|
|
|
2017-06-04 10:43:34 +02:00
|
|
|
class TextDrawerWordWrapper : public WordWrapper {
|
|
|
|
|
public:
|
2020-09-29 12:44:47 +02:00
|
|
|
TextDrawerWordWrapper(TextDrawer *drawer, const char *str, float maxW, int flags)
|
|
|
|
|
: WordWrapper(str, maxW, flags), drawer_(drawer) {}
|
2017-06-04 10:43:34 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
float MeasureWidth(const char *str, size_t bytes) override;
|
|
|
|
|
|
|
|
|
|
TextDrawer *drawer_;
|
2020-03-10 08:58:15 -07:00
|
|
|
};
|