Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
12 KB
Referenced Files
None
Subscribers
None
diff --git a/util/font.cpp b/util/font.cpp
index eca7535c..19ccc57c 100644
--- a/util/font.cpp
+++ b/util/font.cpp
@@ -1,153 +1,221 @@
#include <allegro.h>
#include "font.h"
#include "init.h"
#include "factory/font_factory.h"
+#include <string.h>
using namespace std;
Font::Font(){
}
+void Font::printfWrapLine(int x, int & y, int color, const Bitmap & work, int maxWidth, const char * line) const {
+ int height = getHeight();
+ while (*line != '\0'){
+ char tmp2[1024];
+ int left = strlen(line);
+ int min = 0;
+ int max = left;
+ int current = (min + max) / 2;
+ strncpy(tmp2, line, current);
+ tmp2[current] = '\0';
+ bool done = false;
+ while (!done){
+ int length = textLength(tmp2);
+ if (length >= maxWidth){
+ max = current;
+ current = (min + max) / 2;
+ strncpy(tmp2, line, current);
+ tmp2[current] = '\0';
+ } else if (length < maxWidth && current < max){
+ min = current;
+ current = (min + max) / 2;
+ if (current == max - 1){
+ current = max;
+ done = true;
+ }
+ strncpy(tmp2, line, current);
+ tmp2[current] = '\0';
+ } else {
+ done = true;
+ }
+ }
+ printf(x, y, color, work, string(tmp2), 0);
+ y += height;
+
+ line += current;
+ }
+
+ y += height / 2;
+}
+
+void Font::printfWrap(int x, int y, int color, const Bitmap & work, int maxWidth, const std::string & str, int marker, ... ) const {
+ char buf[4096];
+ va_list ap;
+
+ va_start(ap, marker);
+ uvszprintf(buf, sizeof(buf), str.c_str(), ap);
+ va_end(ap);
+
+ char * start = buf;
+ char * end = strchr(start, '\n');
+ while (end != NULL){
+ char tmp[1024];
+ int ender = end - start;
+ if (ender >= sizeof(tmp) - 1){
+ ender = sizeof(tmp) - 1;
+ }
+
+ strncpy(tmp, start, ender);
+ tmp[ender] = '\0';
+ printfWrapLine(x, y, color, work, maxWidth, tmp);
+
+ start = end + 1;
+ end = strchr(start, '\n');
+ }
+ printfWrapLine(x, y, color, work, maxWidth, start);
+}
+
Font::~Font(){
}
AllegroFont::AllegroFont( const FONT * const font ):
-font( font ){
+font(font){
}
AllegroFont::AllegroFont( const AllegroFont & copy ):
font( copy.getInternalFont() ){
}
AllegroFont::~AllegroFont(){
}
int AllegroFont::textLength( const char * text ) const{
- return text_length( getInternalFont(), text );
+ return text_length( getInternalFont(), text );
}
int AllegroFont::getHeight( const string & str ) const {
- return getHeight();
+ return getHeight();
}
int AllegroFont::getHeight() const {
- return text_height( getInternalFont() );
+ return text_height( getInternalFont() );
}
void AllegroFont::setSize( const int x, const int y ){
}
int AllegroFont::getSizeX() const {
- return 0;
+ return 0;
}
int AllegroFont::getSizeY() const {
- return 0;
+ return 0;
}
-
+
void AllegroFont::printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
uvszprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
- textout_ex( work.getBitmap(), getInternalFont(), buf, x, y, color, -1 );
+ textout_ex(work.getBitmap(), getInternalFont(), buf, x, y, color, -1);
}
void AllegroFont::printf( int x, int y, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
uvszprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
- textout_ex( work.getBitmap(), getInternalFont(), buf, x, y, color, -1 );
+ textout_ex(work.getBitmap(), getInternalFont(), buf, x, y, color, -1);
}
const Font & Font::getDefaultFont(){
// return getFont( "tmp/comic.ttf" );
return getFont( "bios", 16, 16 );
}
const Font & Font::getFont( const string & name, const int x, const int y ){
Font & font = *FontFactory::getFont(name, x, y);
/* sanity check */
if (font.getHeight("A") == 0){
return getDefaultFont();
}
return font;
}
FreeTypeFont::FreeTypeFont( const string & str ):
sizeX( 16 ),
sizeY( 16 ),
own(true){
this->font = new ftalleg::freetype( str, getSizeX(), getSizeY() );
}
int FreeTypeFont::getHeight( const string & str ) const {
return this->font->getHeight( str );
}
int FreeTypeFont::getHeight() const {
return getHeight( "A" );
}
int FreeTypeFont::textLength( const char * text ) const {
- return this->font->getLength( string( text ) );
+ return this->font->getLength(string(text));
}
void FreeTypeFont::printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
vsnprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
int old_x = 0;
int old_y = 0;
this->font->getSize(&old_x, &old_y);
this->font->setSize(xSize, ySize);
this->font->render( x, y, color, work, ftalleg::freetype::ftLeft, string( buf ), 0 );
this->font->setSize(old_x, old_y);
}
void FreeTypeFont::printf( int x, int y, int color, const Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
vsnprintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
- this->font->render( x, y, color, work, ftalleg::freetype::ftLeft, string( buf ), 0 );
+ this->font->render(x, y, color, work, ftalleg::freetype::ftLeft, string(buf), 0);
}
void FreeTypeFont::setSize( const int x, const int y ){
- this->sizeX = x;
- this->sizeY = y;
- this->font->setSize( this->sizeX, this->sizeY );
+ this->sizeX = x;
+ this->sizeY = y;
+ this->font->setSize( this->sizeX, this->sizeY );
}
int FreeTypeFont::getSizeX() const {
- return this->sizeX;
+ return this->sizeX;
}
int FreeTypeFont::getSizeY() const {
- return this->sizeY;
+ return this->sizeY;
}
FreeTypeFont::~FreeTypeFont(){
// cout << "Delete font " << this->font << endl;
if (own){
delete this->font;
}
}
diff --git a/util/font.h b/util/font.h
index 39137b2d..5393bced 100644
--- a/util/font.h
+++ b/util/font.h
@@ -1,85 +1,89 @@
#ifndef _paintown_font_h
#define _paintown_font_h
#include <string>
#include <vector>
#include "bitmap.h"
#include "ftalleg.h"
struct FONT;
/* handle allegro fonts and true type fonts */
class Font{
public:
Font();
virtual ~Font();
virtual void setSize( const int x, const int y ) = 0;
virtual int getSizeX() const = 0;
virtual int getSizeY() const = 0;
virtual int textLength( const char * text ) const = 0;
virtual int getHeight( const std::string & str ) const = 0;
virtual int getHeight() const = 0;
virtual void printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const std::string & str, int marker, ... ) const = 0;
virtual void printf( int x, int y, int color, const Bitmap & work, const std::string & str, int marker, ... ) const = 0;
+ virtual void printfWrap( int x, int y, int color, const Bitmap & work, int maxWidth, const std::string & str, int marker, ... ) const;
static const Font & getDefaultFont();
static const Font & getFont( const std::string & name, const int x = 32, const int y = 32 );
/* store all the freetype fonts forever */
static std::vector< ftalleg::freetype * > cacheFreeType;
+protected:
+
+ void printfWrapLine(int x, int & y, int color, const Bitmap & work, int maxWidth, const char * line) const;
};
class AllegroFont: public Font {
public:
AllegroFont( const FONT * const font );
AllegroFont( const AllegroFont & copy );
virtual ~AllegroFont();
virtual int getHeight() const;
virtual int getHeight( const std::string & str ) const;
virtual int textLength( const char * text ) const;
virtual void printf( int x, int y, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void setSize( const int x, const int y );
virtual int getSizeX() const;
virtual int getSizeY() const;
private:
inline const FONT * getInternalFont() const {
return font;
}
const FONT * const font;
};
class FreeTypeFont: public Font {
public:
FreeTypeFont( const std::string & filename );
FreeTypeFont( const FreeTypeFont & copy );
virtual ~FreeTypeFont();
virtual int getHeight() const;
virtual int getHeight( const std::string & str ) const;
virtual int textLength( const char * text ) const;
virtual void printf( int x, int y, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void printf( int x, int y, int xSize, int ySize, int color, const Bitmap & work, const std::string & str, int marker, ... ) const;
virtual void setSize( const int x, const int y );
virtual int getSizeX() const;
virtual int getSizeY() const;
private:
ftalleg::freetype * font;
int sizeX;
int sizeY;
bool own;
};
#endif
diff --git a/util/memory.h b/util/memory.h
index dd3fd2d2..def8823f 100644
--- a/util/memory.h
+++ b/util/memory.h
@@ -1,126 +1,128 @@
#ifndef _paintown_3ac0ba2587944345f2241085269c24f8
#define _paintown_3ac0ba2587944345f2241085269c24f8
#include <allegro.h>
namespace Memory{
struct memory{
memory(unsigned char * stream, int length):
stream(stream),
position(stream),
length(length){
}
int getSize() const {
return position - stream;
}
/* points to the head */
unsigned char * stream;
/* points to the current position */
unsigned char * position;
int length;
};
static int pf_fclose(void *userdata){
return 0;
/* nothing */
}
static int pf_getc(void *userdata){
memory * m = (memory*) userdata;
if (m->position < m->stream + m->length){
unsigned char x = *m->position;
m->position += 1;
return x;
}
return EOF;
}
static int pf_ungetc(int c, void *userdata){
memory * m = (memory*) userdata;
if (m->position > m->stream){
m->position -= 1;
return c;
} else {
return EOF;
}
}
static long pf_fread(void *p, long n, void *userdata){
memory *m = (memory*) userdata;
unsigned char *cp = (unsigned char *)p;
long i;
int c;
for (i=0; i<n; i++) {
if ((c = pf_getc(m)) == EOF)
break;
*(cp++) = c;
}
return i;
}
static int pf_putc(int c, void *userdata){
memory * m = (memory*) userdata;
if (m->position < m->stream + m->length){
*m->position = c;
m->position += 1;
return c;
}
return EOF;
}
static long pf_fwrite(const void *p, long n, void *userdata){
memory *m = (memory*) userdata;
const unsigned char * cp = (const unsigned char *) p;
long i;
int c;
+ /* probably should replace this with memcpy */
+
for (i=0; i<n; i++) {
if ((c = pf_putc(cp[i], userdata)) == EOF)
break;
}
return i;
}
static int pf_fseek(void *userdata, int offset){
memory * m = (memory*) userdata;
if (offset >= 0 && offset < m->length){
m->position = m->stream + offset;
return 0;
} else {
return -1;
}
}
static int pf_feof(void *userdata){
memory * m = (memory*) userdata;
return m->position >= m->stream + m->length;
}
static int pf_ferror(void *userdata){
memory * m = (memory*) userdata;
return m->position < m->stream || m->position >= m->stream + m->length;
}
static PACKFILE_VTABLE makeTable(){
PACKFILE_VTABLE table;
table.pf_fclose = Memory::pf_fclose;
table.pf_getc = Memory::pf_getc;
table.pf_ungetc = Memory::pf_ungetc;
table.pf_fread = Memory::pf_fread;
table.pf_putc = Memory::pf_putc;
table.pf_fwrite = Memory::pf_fwrite;
table.pf_fseek = Memory::pf_fseek;
table.pf_feof = Memory::pf_feof;
table.pf_ferror = Memory::pf_ferror;
return table;
}
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 1:22 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69224
Default Alt Text
(12 KB)

Event Timeline