mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Moved the default Font logic from the graphics.lua script to the Graphics and Font module C++ code. Moved love.graphics.setNewFont from the graphics.lua script to the Graphics module Lua wrapper code. Moved the Vera.ttf data from the graphics.lua script to its own file inside the Font module.
This commit is contained in:
@@ -293,6 +293,7 @@
|
||||
FAC86E641724552C00EED715 /* wrap_Quad.h in Headers */ = {isa = PBXBuildFile; fileRef = FAC86E621724552C00EED715 /* wrap_Quad.h */; };
|
||||
FAC86E6B1724555D00EED715 /* Quad.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAC86E671724555D00EED715 /* Quad.cpp */; };
|
||||
FAC86E6C1724555D00EED715 /* Quad.h in Headers */ = {isa = PBXBuildFile; fileRef = FAC86E681724555D00EED715 /* Quad.h */; };
|
||||
FAD8E84F1A7C949B0084E24D /* Vera.ttf.h in Headers */ = {isa = PBXBuildFile; fileRef = FAD8E84E1A7C949B0084E24D /* Vera.ttf.h */; };
|
||||
FADD58DD18C30367005FC3BF /* FormatHandler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FADD58DC18C30367005FC3BF /* FormatHandler.cpp */; };
|
||||
FAE010DB170DDE99006F29D0 /* ddsinfo.h in Headers */ = {isa = PBXBuildFile; fileRef = FAE010D8170DDE99006F29D0 /* ddsinfo.h */; };
|
||||
FAE010DC170DDE99006F29D0 /* ddsparse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FAE010D9170DDE99006F29D0 /* ddsparse.cpp */; };
|
||||
@@ -856,6 +857,7 @@
|
||||
FAC86E621724552C00EED715 /* wrap_Quad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_Quad.h; sourceTree = "<group>"; };
|
||||
FAC86E671724555D00EED715 /* Quad.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Quad.cpp; sourceTree = "<group>"; };
|
||||
FAC86E681724555D00EED715 /* Quad.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Quad.h; sourceTree = "<group>"; };
|
||||
FAD8E84E1A7C949B0084E24D /* Vera.ttf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Vera.ttf.h; sourceTree = "<group>"; };
|
||||
FADD58DC18C30367005FC3BF /* FormatHandler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FormatHandler.cpp; sourceTree = "<group>"; };
|
||||
FAE010D8170DDE99006F29D0 /* ddsinfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ddsinfo.h; sourceTree = "<group>"; };
|
||||
FAE010D9170DDE99006F29D0 /* ddsparse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ddsparse.cpp; sourceTree = "<group>"; };
|
||||
@@ -1154,6 +1156,7 @@
|
||||
36465ABA28FB06F4333C3F07 /* ImageRasterizer.h */,
|
||||
1B1C4E4D288A1D2F29E57B1B /* Rasterizer.cpp */,
|
||||
08D24B70441A2496160C0849 /* Rasterizer.h */,
|
||||
FAD8E84E1A7C949B0084E24D /* Vera.ttf.h */,
|
||||
1B4E22F1388E2B2E76E3377B /* wrap_GlyphData.cpp */,
|
||||
3547706F2E7D43212CB40D04 /* wrap_GlyphData.h */,
|
||||
11745DE315E859F71E881D76 /* wrap_Rasterizer.cpp */,
|
||||
@@ -2004,6 +2007,7 @@
|
||||
FA7AA59317F6AC1F00704BE2 /* wrap_Mesh.h in Headers */,
|
||||
FA01BE1F1878E35B00640047 /* wrap_Texture.h in Headers */,
|
||||
FAEC808B1710FEA60057279A /* ImageData.h in Headers */,
|
||||
FAD8E84F1A7C949B0084E24D /* Vera.ttf.h in Headers */,
|
||||
FAEC808F1711E76C0057279A /* CompressedData.h in Headers */,
|
||||
FA636D8B171B70920065623F /* RandomGenerator.h in Headers */,
|
||||
FA636D8F171B72A70065623F /* wrap_RandomGenerator.h in Headers */,
|
||||
|
||||
@@ -30,10 +30,19 @@ namespace filesystem
|
||||
{
|
||||
|
||||
FileData::FileData(uint64 size, const std::string &filename)
|
||||
: data(new char[(size_t) size])
|
||||
: data(nullptr)
|
||||
, size((size_t) size)
|
||||
, filename(filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
data = new char[(size_t) size];
|
||||
}
|
||||
catch (std::bad_alloc &)
|
||||
{
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
if (filename.rfind('.') != std::string::npos)
|
||||
extension = filename.substr(filename.rfind('.')+1);
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ public:
|
||||
// Implements Module.
|
||||
virtual ModuleType getModuleType() const { return M_FONT; }
|
||||
|
||||
virtual Rasterizer *newRasterizer(int size) = 0;
|
||||
virtual Rasterizer *newRasterizer(Data *data, int size) = 0;
|
||||
virtual Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &glyphs) = 0;
|
||||
virtual Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int length) = 0;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -25,6 +25,8 @@
|
||||
|
||||
#include "libraries/utf8/utf8.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace font
|
||||
@@ -32,10 +34,13 @@ namespace font
|
||||
namespace freetype
|
||||
{
|
||||
|
||||
// Default TrueType font.
|
||||
#include "font/Vera.ttf.h"
|
||||
|
||||
Font::Font()
|
||||
{
|
||||
if (FT_Init_FreeType(&library))
|
||||
throw love::Exception("TrueTypeFont Loading error: FT_Init_FreeType failed\n");
|
||||
throw love::Exception("TrueTypeFont Loading error: FT_Init_FreeType failed");
|
||||
}
|
||||
|
||||
Font::~Font()
|
||||
@@ -43,6 +48,16 @@ Font::~Font()
|
||||
FT_Done_FreeType(library);
|
||||
}
|
||||
|
||||
Rasterizer *Font::newRasterizer(int size)
|
||||
{
|
||||
StrongRef<filesystem::FileData> data(new filesystem::FileData(sizeof(Vera_ttf), "Vera.ttf"));
|
||||
data->release();
|
||||
|
||||
memcpy(data->getData(), Vera_ttf, sizeof(Vera_ttf));
|
||||
|
||||
return new TrueTypeRasterizer(library, data.get(), size);
|
||||
}
|
||||
|
||||
Rasterizer *Font::newRasterizer(Data *data, int size)
|
||||
{
|
||||
return new TrueTypeRasterizer(library, data, size);
|
||||
|
||||
@@ -52,6 +52,7 @@ public:
|
||||
virtual ~Font();
|
||||
|
||||
// Implements Font
|
||||
Rasterizer *newRasterizer(int size);
|
||||
Rasterizer *newRasterizer(Data *data, int size);
|
||||
Rasterizer *newRasterizer(love::image::ImageData *data, const std::string &text);
|
||||
Rasterizer *newRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs);
|
||||
|
||||
@@ -49,17 +49,21 @@ int w_newRasterizer(lua_State *L)
|
||||
std::string glyphs(g);
|
||||
luax_catchexcept(L, [&](){ t = instance()->newRasterizer(d, glyphs); });
|
||||
}
|
||||
else if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
|
||||
else if (lua_type(L, 1) == LUA_TSTRING || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
|
||||
{
|
||||
love::filesystem::FileData *d = love::filesystem::luax_getfiledata(L, 1);
|
||||
int size = luaL_checkint(L, 2);
|
||||
int size = luaL_optint(L, 2, 12);
|
||||
luax_catchexcept(L,
|
||||
[&]() { t = instance()->newRasterizer(d, size); },
|
||||
[&]() { d->release(); }
|
||||
);
|
||||
}
|
||||
else
|
||||
return luaL_argerror(L, 1, "expected ImageData, filename, or FileData");
|
||||
{
|
||||
// Default font (Vera.)
|
||||
int size = luaL_optint(L, 1, 12);
|
||||
luax_catchexcept(L, [&]() { t = instance()->newRasterizer(size); });
|
||||
}
|
||||
|
||||
luax_pushtype(L, "Rasterizer", FONT_RASTERIZER_T, t);
|
||||
t->release();
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "Graphics.h"
|
||||
#include "window/sdl/Window.h"
|
||||
#include "font/Font.h"
|
||||
#include "Polyline.h"
|
||||
|
||||
// C++
|
||||
@@ -37,6 +38,7 @@
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace graphics
|
||||
@@ -70,6 +72,7 @@ Graphics::~Graphics()
|
||||
{
|
||||
// We do this manually so the love objects get released before the window.
|
||||
states.clear();
|
||||
defaultFont.set(nullptr);
|
||||
|
||||
currentWindow->release();
|
||||
}
|
||||
@@ -163,6 +166,31 @@ void Graphics::restoreStateChecked(const DisplayState &s)
|
||||
setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
|
||||
}
|
||||
|
||||
void Graphics::checkSetDefaultFont()
|
||||
{
|
||||
// We don't create or set the default Font if an existing font is in use.
|
||||
if (states.back().font.get() != nullptr)
|
||||
return;
|
||||
|
||||
// Create a new default font if we don't have one yet.
|
||||
if (!defaultFont.get())
|
||||
{
|
||||
font::Font *fontmodule = Module::getInstance<font::Font>(M_FONT);
|
||||
if (!fontmodule)
|
||||
throw love::Exception("Font module has not been loaded.");
|
||||
|
||||
StrongRef<font::Rasterizer> r(fontmodule->newRasterizer(12));
|
||||
r->release();
|
||||
|
||||
defaultFont.set(newFont(r.get()));
|
||||
defaultFont->release();
|
||||
|
||||
::printf("created default font\n");
|
||||
}
|
||||
|
||||
states.back().font.set(defaultFont.get());
|
||||
}
|
||||
|
||||
void Graphics::setViewportSize(int width, int height)
|
||||
{
|
||||
this->width = width;
|
||||
@@ -612,18 +640,16 @@ Color Graphics::getBackgroundColor() const
|
||||
|
||||
void Graphics::setFont(Font *font)
|
||||
{
|
||||
// Hack: the Lua-facing love.graphics.print function will set the current
|
||||
// font if needed, but only on its first call... we want to make sure a nil
|
||||
// font is never accidentally set (e.g. via love.graphics.reset.)
|
||||
if (font == nullptr)
|
||||
return;
|
||||
// We don't need to set a default font here if null is passed in, since we
|
||||
// only care about the default font in getFont and print.
|
||||
|
||||
DisplayState &state = states.back();
|
||||
state.font.set(font);
|
||||
}
|
||||
|
||||
Font *Graphics::getFont() const
|
||||
Font *Graphics::getFont()
|
||||
{
|
||||
checkSetDefaultFont();
|
||||
return states.back().font.get();
|
||||
}
|
||||
|
||||
@@ -883,6 +909,8 @@ bool Graphics::isWireframe() const
|
||||
|
||||
void Graphics::print(const std::string &str, float x, float y , float angle, float sx, float sy, float ox, float oy, float kx, float ky)
|
||||
{
|
||||
checkSetDefaultFont();
|
||||
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (state.font.get() != nullptr)
|
||||
@@ -891,6 +919,8 @@ void Graphics::print(const std::string &str, float x, float y , float angle, flo
|
||||
|
||||
void Graphics::printf(const std::string &str, float x, float y, float wrap, AlignMode align, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
|
||||
{
|
||||
checkSetDefaultFont();
|
||||
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (state.font.get() == nullptr)
|
||||
|
||||
@@ -190,15 +190,8 @@ public:
|
||||
**/
|
||||
Color getBackgroundColor() const;
|
||||
|
||||
/**
|
||||
* Sets the current font.
|
||||
* @param font A Font object.
|
||||
**/
|
||||
void setFont(Font *font);
|
||||
/**
|
||||
* Gets the current Font, or nil if none.
|
||||
**/
|
||||
Font *getFont() const;
|
||||
Font *getFont();
|
||||
|
||||
void setShader(Shader *shader);
|
||||
void setShader();
|
||||
@@ -482,8 +475,12 @@ private:
|
||||
void restoreState(const DisplayState &s);
|
||||
void restoreStateChecked(const DisplayState &s);
|
||||
|
||||
void checkSetDefaultFont();
|
||||
|
||||
love::window::Window *currentWindow;
|
||||
|
||||
StrongRef<Font> defaultFont;
|
||||
|
||||
std::vector<double> pixel_size_stack; // stores current size of a pixel (needed for line drawing)
|
||||
|
||||
int width;
|
||||
|
||||
@@ -246,7 +246,9 @@ int w_newQuad(lua_State *L)
|
||||
|
||||
int w_newFont(lua_State *L)
|
||||
{
|
||||
// Convert to Rasterizer, if necessary.
|
||||
// Convert to Rasterizer, if necessary. Note that lua_isstring returns true
|
||||
// if the value is a number, which we rely on for the variant that uses the
|
||||
// default Font rather than a font file.
|
||||
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
|
||||
{
|
||||
int idxs[] = {1, 2};
|
||||
@@ -634,6 +636,14 @@ int w_getBackgroundColor(lua_State *L)
|
||||
return 4;
|
||||
}
|
||||
|
||||
int w_setNewFont(lua_State *L)
|
||||
{
|
||||
int ret = w_newFont(L);
|
||||
Font *font = luax_checktype<Font>(L, -1, "Font", GRAPHICS_FONT_T);
|
||||
instance()->setFont(font);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int w_setFont(lua_State *L)
|
||||
{
|
||||
Font *font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
|
||||
@@ -643,10 +653,8 @@ int w_setFont(lua_State *L)
|
||||
|
||||
int w_getFont(lua_State *L)
|
||||
{
|
||||
Font *f = instance()->getFont();
|
||||
|
||||
if (f == 0)
|
||||
return 0;
|
||||
Font *f = nullptr;
|
||||
luax_catchexcept(L, [&](){ f = instance()->getFont(); });
|
||||
|
||||
luax_pushtype(L, "Font", GRAPHICS_FONT_T, f);
|
||||
return 1;
|
||||
@@ -1438,6 +1446,7 @@ static const luaL_Reg functions[] =
|
||||
{ "setBackgroundColor", w_setBackgroundColor },
|
||||
{ "getBackgroundColor", w_getBackgroundColor },
|
||||
|
||||
{ "setNewFont", w_setNewFont },
|
||||
{ "setFont", w_setFont },
|
||||
{ "getFont", w_getFont },
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ int w_setColor(lua_State *L);
|
||||
int w_getColor(lua_State *L);
|
||||
int w_setBackgroundColor(lua_State *L);
|
||||
int w_getBackgroundColor(lua_State *L);
|
||||
int w_setNewFont(lua_State *L);
|
||||
int w_setFont(lua_State *L);
|
||||
int w_getFont(lua_State *L);
|
||||
int w_setColorMask(lua_State *L);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user