summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2011-06-12 19:03:58 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2011-06-12 19:03:58 +0800
commit2a31330d1c1309f28abff02e922d5e1877720411 (patch)
tree4b1d7a6d12ec9a645bbfdca444ec5ef5ebb76b13
parent618d9d3062aa2e9be38490dcc4fefb6e2da6419f (diff)
wrap-test: added no clip mode
-rw-r--r--src/wrap-test.cpp35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/wrap-test.cpp b/src/wrap-test.cpp
index f2d22a0..80fff06 100644
--- a/src/wrap-test.cpp
+++ b/src/wrap-test.cpp
@@ -16,7 +16,8 @@ enum TextFormat {
TF_CENTER = 1 << 1,
TF_LEFT = 1 << 2,
TF_RIGHT = 1 << 3,
- TF_SINGLELINE = 1 << 4
+ TF_SINGLELINE = 1 << 4,
+ TF_NOCLIP = 1 << 5
};
struct TextRect {
@@ -67,8 +68,9 @@ DrawText(sdl_freetype_font_t * font,
sdl_freetype_fixed_t y = 0;
sdl_freetype_fixed_t adv_x = 0;
sdl_freetype_fixed_t adv_y = 0;
- sdl_freetype_fixed_t width = format & TF_SINGLELINE ? 0 : rect->width << 16;
+ sdl_freetype_fixed_t width = rect->width << 16;
sdl_freetype_fixed_t maxwidth = 0;
+ bool clip = (format & TF_NOCLIP) == 0;
lines.push_back(TextLine());
TextLine *line = &lines.back();
@@ -81,6 +83,9 @@ DrawText(sdl_freetype_font_t * font,
if (text[i] == '\r')
continue;
+ if (clip && (y + exts.height) >> 16 > rect->height)
+ break;
+
if (text[i] == '\n') {
if (format & TF_SINGLELINE)
continue;
@@ -113,9 +118,12 @@ DrawText(sdl_freetype_font_t * font,
adv_y = metrics.y_advance;
}
if (format & TF_SINGLELINE) {
- line->glyphs.push_back(g);
+ if (!clip || x + metrics.width <= width)
+ line->glyphs.push_back(g);
+ else
+ break;
} else {
- if (x + metrics.width > width) {
+ if (clip && x + metrics.width > width) {
sdl_freetype_font_glyphs_fixed_extents(font, &line->exts,
&line->glyphs[0], line->glyphs.size());
if (line->exts.width > maxwidth)
@@ -158,8 +166,11 @@ DrawText(sdl_freetype_font_t * font,
}
}
- rect->width = maxwidth / 65536.0f;
- return (lines.size() * exts.height) / 65536.0f;
+ if (!lines[0].glyphs.size())
+ return 0;
+
+ rect->width = maxwidth >> 16;
+ return (lines.size() * exts.height) >> 16;
}
static void DrawLine(float x0, float y0, float x1, float y1)
@@ -264,13 +275,21 @@ main(int argc, char *argv[])
color.a = alpha * 255;
TextRect rect;
- rect.x = 100;
+ rect.x = 400;
rect.y = 100;
- rect.width = 100;
+ rect.width = 200;
rect.height = 100;
int h;
+ h = DrawText(font, text, -1, &rect, TextFormat(TF_SINGLELINE | TF_LEFT), color);
+ DrawRect(400, 100, 200, 100);
+ DrawLine(400, 100 + h, 400 + 200, 100 + h);
+
+ rect.x = 100;
+ rect.y = 100;
+ rect.width = 100;
+ rect.height = 100;
h = DrawText(font, text, -1, &rect, TF_LEFT, color);
DrawRect(100, 100, 200, 100);
DrawLine(100, 100 + h, 100 + 200, 100 + h);