summaryrefslogtreecommitdiff
path: root/lib/font.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/font.cc')
-rw-r--r--lib/font.cc307
1 files changed, 306 insertions, 1 deletions
diff --git a/lib/font.cc b/lib/font.cc
index cd04177..4062c37 100644
--- a/lib/font.cc
+++ b/lib/font.cc
@@ -232,6 +232,44 @@ void mogltk::font::drawentry(Uint16 entry, int x, int y, ColorP c) {
SDL_UnlockSurface(mogltk::engine::base_o->getsurface());
}
+void mogltk::font::drawtotex(texture * t, Uint16 entry, int x, int y, ColorP c) {
+ bool locked = false;
+ int trueentry, cx, cy, px, py;
+ SDL_Rect src, dst;
+
+ if (SDL_MUSTLOCK(t->GetSurface())) {
+ locked = true;
+ SDL_LockSurface(t->GetSurface());
+ }
+
+ if (shadow) {
+ int os = shadow;
+ shadow = 0;
+
+ drawtotex(t, entry, x + os, y + os, BLACK);
+
+ shadow = os;
+ }
+
+ checknbind(entry / nbcT, c);
+
+ y -= base;
+
+ trueentry = entry % nbcT;
+ cx = trueentry % nbcU;
+ cy = trueentry / nbcU;
+ px = cx * maxX;
+ py = cy * maxY;
+
+ src.x = px; src.y = py; src.w = maxX; src.h = maxY;
+ dst.x = x; dst.y = y; dst.w = maxX; dst.h = maxY;
+
+ SDL_BlitSurface(fonttex[entry / nbcT]->GetSurface(), &src, t->GetSurface(), &dst);
+
+ if (locked)
+ SDL_UnlockSurface(t->GetSurface());
+}
+
void mogltk::font::putcursor(int x, int y) {
cx = ox = x;
cy = y;
@@ -242,6 +280,11 @@ void mogltk::font::putentry(Uint16 entry, ColorP c) {
cx += sizes[entry] + wspace;
}
+void mogltk::font::putentryontex(texture * t, Uint16 entry, ColorP c) {
+ drawtotex(t, entry, cx, cy, c);
+ cx += sizes[entry] + wspace;
+}
+
void mogltk::font::drawchar(char ch, ColorP c) {
Uint16 * p;
int i;
@@ -254,6 +297,18 @@ void mogltk::font::drawchar(char ch, ColorP c) {
}
}
+void mogltk::font::drawcharontex(texture * t, char ch, ColorP c) {
+ Uint16 * p;
+ int i;
+
+ for (i = 0, p = corresp; i < nbentries; i++, p++) {
+ if (*(p++) == ch) {
+ putentryontex(t, *p, c);
+ return;
+ }
+ }
+}
+
int mogltk::font::findchar(char ch) const {
Uint16 * p;
int i;
@@ -319,6 +374,256 @@ int mogltk::font::printf(const char * p, ...) {
return r;
}
+mogltk::rect mogltk::font::size(const ugly_string & m, va_list ap) {
+ char * p;
+ static char buffer[STRBUFSIZ + 1];
+ rect r;
+ int mw, w;
+
+ r.x = cx;
+ r.y = cy;
+ r.h = inter;
+ r.w = 0;
+ mw = 0;
+ w = 0;
+
+#ifdef HAVE_VSNPRINTF
+ vsnprintf(buffer, STRBUFSIZ, m.p, ap);
+#else
+ vsprintf(buffer, m.p, ap);
+#endif
+
+ for (p = buffer; *p; p++) {
+ if (*p == '\n') {
+ if (*(p+1)) {
+ r.h += inter;
+ if (w > mw) {
+ mw = w;
+ }
+ w = 0;
+ }
+ } else {
+ w += sizes[findchar(*p)] + wspace;
+ }
+ }
+
+ if (w > mw) {
+ mw = w;
+ }
+
+ r.w = mw;
+
+ return r;
+}
+
+mogltk::rect mogltk::font::size(const ugly_string & m, ...) {
+ va_list ap;
+ rect r;
+
+ va_start(ap, m);
+ r = size(m, ap);
+ va_end(ap);
+
+ return r;
+}
+
+mogltk::rect mogltk::font::size(const char * p, ...) {
+ ugly_string m;
+ va_list ap;
+ rect r;
+
+ m.p = p;
+
+ va_start(ap, p);
+ r = size(m, ap);
+ va_end(ap);
+
+ return r;
+}
+
+mogltk::rect mogltk::font::printtotex(texture * t, const ugly_string & m, va_list ap) {
+ char * p;
+ static char buffer[STRBUFSIZ + 1];
+ rect r;
+ int mw, w;
+
+ r.x = cx;
+ r.y = cy;
+ r.h = inter;
+ r.w = 0;
+ mw = 0;
+ w = 0;
+
+#ifdef HAVE_VSNPRINTF
+ vsnprintf(buffer, STRBUFSIZ, m.p, ap);
+#else
+ vsprintf(buffer, m.p, ap);
+#endif
+
+ for (p = buffer; *p; p++) {
+ if (*p == '\n') {
+ if (*(p+1)) {
+ r.h += inter;
+ if (w > mw) {
+ mw = w;
+ }
+ w = 0;
+ }
+ } else {
+ w += sizes[findchar(*p)] + wspace;
+ }
+ }
+
+ if (w > mw) {
+ mw = w;
+ }
+
+ r.w = mw;
+
+ for (p = buffer; *p; p++) {
+ if (*p == '\n') {
+ newline();
+ } else {
+ drawcharontex(t, *p, textcolor);
+ }
+ }
+
+ return r;
+}
+
+mogltk::rect mogltk::font::printtotex(texture * t, const ugly_string & m, ...) {
+ va_list ap;
+ rect r;
+
+ va_start(ap, m);
+ r = printtotex(t, m, ap);
+ va_end(ap);
+
+ return r;
+}
+
+mogltk::rect mogltk::font::printtotex(texture * t, const char * p, ...) {
+ ugly_string m;
+ va_list ap;
+ rect r;
+
+ m.p = p;
+
+ va_start(ap, p);
+ r = printtotex(t, m, ap);
+ va_end(ap);
+
+ return r;
+}
+
+inline unsigned int nextpower(unsigned int n) {
+ unsigned int i;
+
+ if (!n)
+ return n;
+
+ if (ISPOT(n))
+ return n;
+
+ for (i = 31; i >= 0; i--) {
+ if ((n >> i) & 1) {
+ return 1 << (i + 1);
+ }
+ }
+}
+
+mogltk::texture * mogltk::font::printtex(rect * _r, const ugly_string & m, va_list ap) {
+ rect r;
+ char * p;
+ static char buffer[STRBUFSIZ + 1];
+ int mw, w, pw, ph;
+ texture * t;
+ int ocx, ocy, oox;
+
+ ocx = cx;
+ ocy = cy;
+ oox = ox;
+
+ cx = ox = 0;
+ cy = base;
+
+ r.x = cx;
+ r.y = cy;
+ r.h = inter;
+ r.w = 0;
+ mw = 0;
+ w = 0;
+
+#ifdef HAVE_VSNPRINTF
+ vsnprintf(buffer, STRBUFSIZ, m.p, ap);
+#else
+ vsprintf(buffer, m.p, ap);
+#endif
+
+ for (p = buffer; *p; p++) {
+ if (*p == '\n') {
+ if (*(p+1)) {
+ r.h += inter;
+ if (w > mw) {
+ mw = w;
+ }
+ w = 0;
+ }
+ } else {
+ w += sizes[findchar(*p)] + wspace;
+ }
+ }
+
+ if (w > mw) {
+ mw = w;
+ }
+
+ r.w = mw;
+
+ pw = nextpower(r.w);
+ ph = nextpower(r.h);
+
+ t = new texture(pw, ph);
+
+ for (p = buffer; *p; p++) {
+ if (*p == '\n') {
+ newline();
+ } else {
+ drawcharontex(t, *p, textcolor);
+ }
+ }
+
+ if (_r)
+ *_r = r;
+
+ return t;
+}
+
+mogltk::texture * mogltk::font::printtex(rect * r, const ugly_string & m, ...) {
+ va_list ap;
+ texture * t;
+
+ va_start(ap, m);
+ t = printtex(r, m, ap);
+ va_end(ap);
+
+ return t;
+}
+
+mogltk::texture * mogltk::font::printtex(rect * r, const char * p, ...) {
+ ugly_string m;
+ va_list ap;
+ texture * t;
+
+ m.p = p;
+
+ va_start(ap, p);
+ t = printtex(r, m, ap);
+ va_end(ap);
+
+ return t;
+}
+
void mogltk::font::setcolor(ColorP c) {
textcolor = c;
}
@@ -336,7 +641,7 @@ int mogltk::font::singletextsize(const String & s) const {
int r = 0;
for (i = 0; i < s.strlen(); i++) {
- r += sizes[findchar(s[i])];
+ r += sizes[findchar(s[i])] + wspace;
}
return r;