Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

188 lines (144 sloc) 3.171 kB
#ifndef com_sleepless_font_cpp
#define com_sleepless_font_cpp
/* Copyright 2004 Sleepless Software Inc. All Rights Reserved */
#ifdef WIN32
#include "windows.cpp"
#endif
#include "str.cpp"
#include "image.cpp"
struct Font
{
int height;
int italic;
int underline;
int bold;
char face[100];
#ifdef WIN32
HFONT font;
#endif
Font()
{
height = 0;
italic = 0;
underline = 0;
bold = 0;
face[0] = 0;
}
// Returns a font with the requested properties or NULL if unable.
// Args:
// h - height in pixels
// i - true if you want italic
// u - true if you want underline
// b - true if you want bold
// f - name of type face, like "helvetica".
Font(int h, int i, int u, int b, const char *f)
{
#ifdef WIN32
font = Windows::createFont(h, i, u, b, f);
if(font)
{
height = h;
italic = i;
underline = u;
bold = b;
Str::copy(face, f, sizeof(face));
}
#endif
}
virtual ~Font()
{
#ifdef WIN32
if(font)
DeleteObject(font);
#endif
}
int getHeight()
{
return height;
}
virtual void textSize(const char *s, int len, int *pw, int *ph, int k, int lead)
{
*pw = 0;
*ph = 0;
#ifdef WIN32
if(font && s)
{
HDC dc = GetDC(0);
if(dc)
{
Windows::textSize(dc, font, s, len, pw, ph);
ReleaseDC(0, dc);
}
}
#endif
}
virtual void textSize(const char *txt, int l, int *wp, int *hp)
{
textSize(txt, l, wp, hp, 0, 0);
}
virtual void textSize(const char *t, int *wp, int *hp)
{
textSize(t, (int)strlen(t), wp, hp);
}
virtual void textSize(int n, int *wp, int *hp)
{
char buf[32];
sprintf(buf, "%d", n);
textSize(buf, wp, hp);
}
// Render text in this font into an image
virtual void draw(Image *img, const char *t, int x, int y, int c, int lead, int kern, float a)
{
if(img->width < 1)
return;
if(img->height < 1)
return;
// XXX
#ifdef WIN32
// windows color ref is like 0x00bbggrr instead of 0x00rrggbb
// so swap it round.
int c2 = ((c & 0xff0000) >> 16) | (c & 0x00ff00) | ((c & 0x0000ff) << 16);
Windows::drawText(img->dc, font, c2, t, x, y);
#endif
}
void draw(Image *img, const char *t, int x, int y, int c)
{
draw(img, t, x, y, c, 0, 0, 1);
}
// draw text horizontally centered over x, y
void drawCentered(Image *img, const char *t, int x, int y, int c)
{
int tw, th;
textSize(t, (int)strlen(t), &tw, &th, 0, 0);
draw(img, t, x - (tw / 2), y - (th / 2), c);
}
// draws text right justified
void drawRight(Image *img, const char *t, int x, int y, int c)
{
int tw, th;
textSize(t, (int)strlen(t), &tw, &th, 0, 0);
draw(img, t, x - tw, y, c);
}
// draws an integer
void draw(Image *img, int n, int x, int y, int c)
{
char buf[32];
sprintf(buf, "%d", n);
draw(img, buf, x, y, c);
}
// draws integer centered
void drawCenter(Image *img, int n, int x, int y, int c)
{
char buf[32];
sprintf(buf, "%d", n);
drawCentered(img, buf, x, y, c);
}
// draws integer right justified
void drawRight(Image *img, int n, int x, int y, int c)
{
char buf[32];
sprintf(buf, "%d", n);
drawRight(img, buf, x, y, c);
}
};
#endif // com_sleepless_font_cpp
Jump to Line
Something went wrong with that request. Please try again.