Font:setFlipEnabled; Font:isFlipEnabled;

This commit is contained in:
bjorn 2019-01-29 21:49:49 -08:00
parent f5a903a805
commit 03e3e14718
4 changed files with 32 additions and 5 deletions

View File

@ -51,6 +51,18 @@ int l_lovrFontSetLineHeight(lua_State* L) {
return 0;
}
int l_lovrFontIsFlipEnabled(lua_State* L) {
Font* font = luax_checktype(L, 1, Font);
lua_pushboolean(L, lovrFontIsFlipEnabled(font));
return 1;
}
int l_lovrFontSetFlipEnabled(lua_State* L) {
Font* font = luax_checktype(L, 1, Font);
lovrFontSetFlipEnabled(font, lua_toboolean(L, 2));
return 0;
}
int l_lovrFontGetPixelDensity(lua_State* L) {
Font* font = luax_checktype(L, 1, Font);
lua_pushnumber(L, lovrFontGetPixelDensity(font));
@ -97,6 +109,8 @@ const luaL_Reg lovrFont[] = {
{ "getBaseline", l_lovrFontGetBaseline },
{ "getLineHeight", l_lovrFontGetLineHeight },
{ "setLineHeight", l_lovrFontSetLineHeight },
{ "isFlipEnabled", l_lovrFontIsFlipEnabled },
{ "setFlipEnabled", l_lovrFontSetFlipEnabled },
{ "getPixelDensity", l_lovrFontGetPixelDensity },
{ "setPixelDensity", l_lovrFontSetPixelDensity },
{ "getRasterizer", l_lovrFontGetRasterizer},

View File

@ -69,9 +69,10 @@ Rasterizer* lovrFontGetRasterizer(Font* font) {
void lovrFontRender(Font* font, const char* str, size_t length, float wrap, HorizontalAlign halign, float* vertices, uint16_t* indices, uint16_t baseVertex) {
FontAtlas* atlas = &font->atlas;
bool flip = font->flip;
float cx = 0;
float cy = -font->rasterizer->height * .8;
float cy = -font->rasterizer->height * .8 * (flip ? -1 : 1);
float u = atlas->width;
float v = atlas->height;
float scale = 1 / font->pixelDensity;
@ -93,7 +94,7 @@ void lovrFontRender(Font* font, const char* str, size_t length, float wrap, Hori
if (codepoint == '\n' || (wrap && cx * scale > wrap && codepoint == ' ')) {
lineStart = lovrFontAlignLine(lineStart, vertexCursor, cx, halign);
cx = 0;
cy -= font->rasterizer->height * font->lineHeight;
cy -= font->rasterizer->height * font->lineHeight * (flip ? -1 : 1);
previous = '\0';
str += bytes;
continue;
@ -123,9 +124,9 @@ void lovrFontRender(Font* font, const char* str, size_t length, float wrap, Hori
// Triangles
if (glyph->w > 0 && glyph->h > 0) {
float x1 = cx + glyph->dx - GLYPH_PADDING;
float y1 = cy + glyph->dy + GLYPH_PADDING;
float y1 = cy + (glyph->dy + GLYPH_PADDING) * (flip ? -1 : 1);
float x2 = x1 + glyph->tw;
float y2 = y1 - glyph->th;
float y2 = y1 - glyph->th * (flip ? -1 : 1);
float s1 = glyph->x / u;
float t1 = (glyph->y + glyph->th) / v;
float s2 = (glyph->x + glyph->tw) / u;
@ -221,6 +222,14 @@ void lovrFontSetLineHeight(Font* font, float lineHeight) {
font->lineHeight = lineHeight;
}
bool lovrFontIsFlipEnabled(Font* font) {
return font->flip;
}
void lovrFontSetFlipEnabled(Font* font, bool flip) {
font->flip = flip;
}
int lovrFontGetKerning(Font* font, unsigned int left, unsigned int right) {
char key[12];
snprintf(key, 12, "%d,%d", left, right);

View File

@ -3,6 +3,7 @@
#include "graphics/texture.h"
#include "lib/map/map.h"
#include <stdint.h>
#include <stdbool.h>
#pragma once
@ -36,6 +37,7 @@ typedef struct {
map_int_t kerning;
float lineHeight;
float pixelDensity;
bool flip;
} Font;
Font* lovrFontInit(Font* font, Rasterizer* rasterizer);
@ -50,6 +52,8 @@ float lovrFontGetDescent(Font* font);
float lovrFontGetBaseline(Font* font);
float lovrFontGetLineHeight(Font* font);
void lovrFontSetLineHeight(Font* font, float lineHeight);
bool lovrFontIsFlipEnabled(Font* font);
void lovrFontSetFlipEnabled(Font* font, bool flip);
int lovrFontGetKerning(Font* font, unsigned int a, unsigned int b);
float lovrFontGetPixelDensity(Font* font);
void lovrFontSetPixelDensity(Font* font, float pixelDensity);

View File

@ -1015,7 +1015,7 @@ void lovrGraphicsPrint(const char* str, size_t length, mat4 transform, float wra
lovrFontMeasure(font, str, length, wrap, &width, &lineCount, &glyphCount);
float scale = 1.f / font->pixelDensity;
float offsetY = ((lineCount + 1) * font->rasterizer->height * font->lineHeight) * (valign / 2.f);
float offsetY = ((lineCount + 1) * font->rasterizer->height * font->lineHeight) * (valign / 2.f) * (font->flip ? -1 : 1);
mat4_scale(transform, scale, scale, scale);
mat4_translate(transform, 0.f, offsetY, 0.f);