From 96b878a9791cd4a24a365dd4ed7a0fde97193348 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Wed, 4 Jun 2003 21:30:17 +0000 Subject: [PATCH] better font layout --- glft/font.c | 18 ++++++++-- glft/glft.h | 2 ++ glft/render.c | 2 ++ render2/font.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++- render2/font.h | 9 +++++ render2/paint.c | 17 +++++++--- render2/planar.c | 23 +++++++------ render2/planar.h | 2 ++ render2/texture.c | 17 +++------- render2/texture.h | 3 +- render2/theme.c | 10 ++++++ render2/theme.h | 2 +- 12 files changed, 158 insertions(+), 33 deletions(-) diff --git a/glft/font.c b/glft/font.c index ba7bbd25..2d641ab6 100644 --- a/glft/font.c +++ b/glft/font.c @@ -101,7 +101,7 @@ struct GlftFont *GlftFontOpen(Display *d, int screen, const char *name) case FcResultNoMatch: font->index = 0; break; - case FcResultMatch: + case FcResultMatch: break; default: GlftDebug("error getting FC_INDEX from pattern\n"); @@ -235,8 +235,10 @@ struct GlftFont *GlftFontOpen(Display *d, int screen, const char *name) font->max_advance_width = font->face->size->metrics.max_advance >> 6; font->descent = -(font->face->size->metrics.descender >> 6); font->ascent = font->face->size->metrics.ascender >> 6; - if (font->minspace) font->height = font->ascent + font->descent; - else font->height = font->face->size->metrics.height >> 6; + if (font->minspace) + font->height = font->ascent + font->descent; + else + font->height = font->face->size->metrics.height >> 6; font->kerning = FT_HAS_KERNING(font->face); @@ -336,3 +338,13 @@ int GlftFontMaxCharWidth(struct GlftFont *font) { return font->max_advance_width; } + +int GlftFontAscent(struct GlftFont *font) +{ + return font->ascent; +} + +int GlftFontDescent(struct GlftFont *font) +{ + return font->descent; +} diff --git a/glft/glft.h b/glft/glft.h index c6c2f00b..4d57e6b9 100644 --- a/glft/glft.h +++ b/glft/glft.h @@ -42,6 +42,8 @@ void GlftMeasureString(struct GlftFont *font, int *w, int *h); +int GlftFontAscent(struct GlftFont *font); +int GlftFontDescent(struct GlftFont *font); int GlftFontHeight(struct GlftFont *font); int GlftFontMaxCharWidth(struct GlftFont *font); diff --git a/glft/render.c b/glft/render.c index 3590ad6a..77609346 100644 --- a/glft/render.c +++ b/glft/render.c @@ -56,6 +56,8 @@ static void drawstring(struct GlftFont *font, const char *str, int bytes, const char *c; struct GlftGlyph *g, *p = NULL; + y += font->descent - 1; /* XXX why -1? it works tho, it seems.. */ + glColor4f(color->r, color->g, color->b, color->a); glPushMatrix(); glTranslatef(x, y, 0.0); diff --git a/render2/font.c b/render2/font.c index 44ed5597..341d2678 100644 --- a/render2/font.c +++ b/render2/font.c @@ -1,16 +1,29 @@ #include "render.h" #include "instance.h" +#include "surface.h" #include "font.h" #include #include +#include + +#define ALPHAS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "1234567890`-=\\!@#$%^&*()~_+|[]{};':\",./<>?" +#define ELIPSES "..." struct RrFont *RrFontOpen(struct RrInstance *inst, const char *fontstring) { struct RrFont *font; + int w, h; font = malloc(sizeof(struct RrFont)); font->inst = inst; font->font = GlftFontOpen(RrDisplay(inst), RrScreen(inst), fontstring); + + GlftMeasureString(font->font, ELIPSES, strlen(ELIPSES), &w, &h); + font->elipses = w; + GlftMeasureString(font->font, ALPHAS, strlen(ALPHAS), &w, &h); + font->height = h; + return font; } @@ -31,10 +44,81 @@ int RrFontMeasureString(struct RrFont *font, const char *string) int RrFontHeight(struct RrFont *font) { - return GlftFontHeight(font->font); + return font->height; } int RrFontMaxCharWidth(struct RrFont *font) { return GlftFontMaxCharWidth(font->font); } + +void RrFontRenderString(struct RrSurface *sur, struct RrFont *font, + struct RrColor *color, enum RrLayout layout, + const char *string, int x, int y, int w, int h) +{ + struct GlftColor col; + int fh = RrFontHeight(font); + int l, m; + GString *text; + int shortened = 0; + + switch (layout) { + case RR_TOP_LEFT: + case RR_TOP: + case RR_TOP_RIGHT: + y += h - fh; + break; + case RR_LEFT: + case RR_CENTER: + case RR_RIGHT: + y += (h - fh) / 2; + break; + case RR_BOTTOM_LEFT: + case RR_BOTTOM: + case RR_BOTTOM_RIGHT: + break; + } + + text = g_string_new(string); + l = g_utf8_strlen(text->str, -1); + m = RrFontMeasureString(font, text->str); + if (font->elipses > w) + l = 0; /* nothing fits.. */ + else { + while (l && m > w) { + shortened = 1; + /* remove a character from the middle */ + text = g_string_erase(text, l-- / 2, 1); + /* if the elipses are too large, don't show them at all */ + m = RrFontMeasureString(font, text->str) + font->elipses; + } + if (shortened) { + text = g_string_insert(text, (l + 1) / 2, ELIPSES); + l += 3; + } + } + if (!l) return; + + switch (layout) { + case RR_TOP_LEFT: + case RR_LEFT: + case RR_BOTTOM_LEFT: + break; + case RR_TOP: + case RR_CENTER: + case RR_BOTTOM: + x += (w - m) / 2; + break; + case RR_TOP_RIGHT: + case RR_RIGHT: + case RR_BOTTOM_RIGHT: + x += w - m; + break; + } + + col.r = color->r; + col.g = color->g; + col.b = color->b; + col.a = color->a; + GlftRenderString(font->font, text->str, strlen(text->str), &col, x, y); +} diff --git a/render2/font.h b/render2/font.h index f73e4790..33e45a4f 100644 --- a/render2/font.h +++ b/render2/font.h @@ -8,6 +8,15 @@ struct RrInstance; struct RrFont { struct RrInstance *inst; struct GlftFont *font; + + int height; + int elipses; }; +#define RrFontElipsesLength(f) ((f)->elipses) + +void RrFontRenderString(struct RrSurface *sur, struct RrFont *font, + struct RrColor *color, enum RrLayout layout, + const char *string, int x, int y, int w, int h); + #endif diff --git a/render2/paint.c b/render2/paint.c index e22f1f20..1981072f 100644 --- a/render2/paint.c +++ b/render2/paint.c @@ -72,6 +72,7 @@ void RrPaint(struct RrSurface *sur, int recurse_always) struct RrSurface *p; int ok, i; int surx, sury; + int x, y, w, h, e; GSList *it; inst = RrSurfaceInstance(sur); @@ -82,8 +83,6 @@ void RrPaint(struct RrSurface *sur, int recurse_always) if (!RrSurfaceVisible(sur)) return; - g_message("PAINTING SURFACE %p", sur); - ok = glXMakeCurrent(RrDisplay(inst), RrSurfaceWindow(sur),RrContext(inst)); assert(ok); @@ -99,8 +98,7 @@ void RrPaint(struct RrSurface *sur, int recurse_always) */ glPushMatrix(); - glTranslatef(-RrSurfaceX(sur), - -RrSurfaceY(sur), 0); + glTranslatef(-RrSurfaceX(sur), -RrSurfaceY(sur), 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); p = sur; @@ -114,16 +112,25 @@ void RrPaint(struct RrSurface *sur, int recurse_always) switch (RrSurfaceType(sur)) { case RR_SURFACE_PLANAR: RrPlanarPaint(sur, surx, sury); + e = RrPlanarEdgeWidth(sur); + x = RrSurfaceX(sur) + e; + y = RrSurfaceY(sur) + e; + w = RrSurfaceWidth(sur) - e * 2; + h = RrSurfaceHeight(sur) - e * 2; break; case RR_SURFACE_NONPLANAR: assert(0); break; case RR_SURFACE_NONE: + x = RrSurfaceX(sur); + y = RrSurfaceY(sur); + w = RrSurfaceWidth(sur); + h = RrSurfaceHeight(sur); break; } for (i = 0; i < sur->ntextures; ++i) - RrTexturePaint(sur, &sur->texture[i]); + RrTexturePaint(sur, &sur->texture[i], x, y, w, h); glPopMatrix(); diff --git a/render2/planar.c b/render2/planar.c index 2f9ce670..ab3f252e 100644 --- a/render2/planar.c +++ b/render2/planar.c @@ -351,27 +351,30 @@ void RrPlanarPaint(struct RrSurface *sur, int absx, int absy) RrPlanarBorderWidth(sur), &RrPlanarBorderColor(sur)); } -void RrPlanarMinSize(struct RrSurface *sur, int *w, int *h) +int RrPlanarEdgeWidth(struct RrSurface *sur) { - *w = *h = RrPlanarBorderWidth(sur); + int w; + w = RrPlanarBorderWidth(sur); switch (RrPlanarBevelType(sur)) { case RR_SUNKEN_OUTER: - (*w)++; - (*h)++; + w++; break; case RR_SUNKEN_INNER: - (*w)+=2; - (*h)+=2; + w += 2; break; case RR_RAISED_OUTER: - (*w)++; - (*h)++; + w += 2; break; case RR_RAISED_INNER: - (*w)+=2; - (*h)+=2; + w++; break; case RR_BEVEL_NONE: break; } + return w; +} + +void RrPlanarMinSize(struct RrSurface *sur, int *w, int *h) +{ + *w = *h = 2 * RrPlanarEdgeWidth(sur); } diff --git a/render2/planar.h b/render2/planar.h index acaae812..0cd52f6b 100644 --- a/render2/planar.h +++ b/render2/planar.h @@ -27,4 +27,6 @@ void RrPlanarPaint(struct RrSurface *sur, int absx, int absy); void RrPlanarMinSize(struct RrSurface *sur, int *w, int *h); +int RrPlanarEdgeWidth(struct RrSurface *sur); + #endif diff --git a/render2/texture.c b/render2/texture.c index 54062fd5..01b224ad 100644 --- a/render2/texture.c +++ b/render2/texture.c @@ -84,25 +84,18 @@ void RrTextureSetNone(struct RrSurface *sur, RrTextureFreeContents(tex); } -void RrTexturePaint(struct RrSurface *sur, struct RrTexture *tex) +void RrTexturePaint(struct RrSurface *sur, struct RrTexture *tex, + int x, int y, int w, int h) { - struct GlftColor col; - glEnable(GL_TEXTURE_2D); switch (tex->type) { case RR_TEXTURE_NONE: break; case RR_TEXTURE_TEXT: - assert(tex->data.text.font); - col.r = tex->data.text.color.r; - col.g = tex->data.text.color.g; - col.b = tex->data.text.color.b; - col.a = tex->data.text.color.a; - - GlftRenderString(tex->data.text.font->font, tex->data.text.string, - strlen(tex->data.text.string), &col, - RrSurfaceX(sur) + 2, RrSurfaceY(sur) + 4); + RrFontRenderString(sur, tex->data.text.font, &tex->data.text.color, + tex->data.text.layout, tex->data.text.string, + x, y, w, h); break; } glDisable(GL_TEXTURE_2D); diff --git a/render2/texture.h b/render2/texture.h index 5d535e3b..a98efef1 100644 --- a/render2/texture.h +++ b/render2/texture.h @@ -39,6 +39,7 @@ struct RrTexture { union RrTextureData data; }; -void RrTexturePaint(struct RrSurface *sur, struct RrTexture *tex); +void RrTexturePaint(struct RrSurface *sur, struct RrTexture *tex, + int x, int y, int w, int h); #endif diff --git a/render2/theme.c b/render2/theme.c index 9539902c..144a7bcb 100644 --- a/render2/theme.c +++ b/render2/theme.c @@ -1,5 +1,6 @@ #include "render.h" #include "theme.h" +#include "planar.h" #include struct RrTheme *RrThemeLoad(struct RrInstance *inst, const char *name) @@ -308,3 +309,12 @@ void RrThemeDestroy(struct RrTheme *theme) free(theme); } } + +int RrThemeLabelHeight(struct RrTheme *t) +{ + int h; + h = RrFontHeight(t->title_font); + h += 2 * MAX(RrPlanarEdgeWidth(t->label), + RrPlanarEdgeWidth(t->label_f)); + return h; +} diff --git a/render2/theme.h b/render2/theme.h index 6637b948..718d8b5b 100644 --- a/render2/theme.h +++ b/render2/theme.h @@ -92,7 +92,7 @@ struct RrTheme { struct RrSurface *app_icon; }; -#define RrThemeLabelHeight(t) (RrFontHeight((t)->title_font)) +int RrThemeLabelHeight(struct RrTheme *t); #define RrThemeTitleHeight(t) (RrThemeLabelHeight(t) + \ ((t)->bevel + (t)->bwidth) * 2) #define RrThemeButtonSize(t) (RrThemeLabelHeight(t) - (t)->bevel * 2) -- 2.34.1