Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
17 KB
Referenced Files
None
Subscribers
None
diff --git a/util/exceptions/exception.cpp b/util/exceptions/exception.cpp
index 7db40dea..b7a5be5b 100644
--- a/util/exceptions/exception.cpp
+++ b/util/exceptions/exception.cpp
@@ -1,95 +1,119 @@
#include "exception.h"
#include <string>
#include <exception>
#include <sstream>
namespace Exception{
Base::Base(const std::string & file, int line):
file(file),
line(line),
nested(NULL){
}
Base::Base(const std::string & file, int line, const Base & nested):
file(file),
line(line),
nested(nested.copy()){
}
Base::~Base() throw (){
if (nested){
delete nested;
}
}
const std::string Base::getReason() const {
return "reason not given";
}
const std::string Base::getTrace() const {
std::ostringstream out;
out << file << ":" << line << " " << getReason();
if (nested != NULL){
out << "\n";
out << nested->getTrace();
}
return out.str();
}
Base::Base(const Base & copy):
file(copy.file),
line(copy.line),
nested(NULL){
if (copy.nested != NULL){
nested = copy.nested->copy();
}
}
void Base::set(const Base & him){
nested = him.copy();
}
Base * Base::copy() const {
return new Base(*this);
}
Return::Return(const std::string & file, int line):
Base(file, line){
}
Return::Return(const std::string & file, int line, const Base & nested):
Base(file, line, nested){
}
Return::~Return() throw(){
}
void Return::throwSelf() const {
throw *this;
}
Base * Return::copy() const {
return new Return(*this);
}
Quit::Quit(const std::string & file, int line):
Base(file, line){
}
Quit::Quit(const std::string & file, int line, const Base & nested):
Base(file, line, nested){
}
Quit::~Quit() throw(){
}
void Quit::throwSelf() const {
throw *this;
}
Base * Quit::copy() const {
return new Quit(*this);
}
+FontException::FontException(const std::string & file, int line, const std::string & reason):
+Base(file, line),
+reason(reason){
+}
+
+FontException::FontException(const std::string & file, int line, const Base & nested, const std::string & reason):
+Base(file, line, nested),
+reason(reason){
+}
+
+FontException::~FontException() throw(){
+}
+
+const std::string FontException::getReason() const {
+ return reason;
+}
+
+void FontException::throwSelf() const {
+ throw *this;
+}
+
+Base * FontException::copy() const {
+}
+
}
diff --git a/util/exceptions/exception.h b/util/exceptions/exception.h
index 478c3e45..7fe12e22 100644
--- a/util/exceptions/exception.h
+++ b/util/exceptions/exception.h
@@ -1,68 +1,80 @@
#ifndef _paintown_exception_12345_h
#define _paintown_exception_12345_h
#include <string>
#include <exception>
namespace Exception{
/* Base class for all exceptions */
class Base: public std::exception {
public:
Base(const std::string & file, int line);
Base(const std::string & file, int line, const Base & nested);
Base(const Base & copy);
/* if we use operator= then we get a bunch of warnings from gcc */
virtual void set(const Base & nested);
virtual void throwSelf() const {
throw *this;
}
virtual Base * copy() const;
const std::string getTrace() const;
virtual ~Base() throw ();
protected:
virtual const std::string getReason() const;
std::string file;
int line;
Base * nested;
};
/* This exception is thrown when the user wants to return to the previous menu or
* whatever from some menu or the game by through some abnormal means (like
* pressing ESC). If there is an "exit" button in the menu then usually you shouldn't
* throw this exception, just return as normal.
*/
class Return: public Base {
public:
Return(const std::string & file, int line);
Return(const std::string & file, int line, const Base & nested);
virtual ~Return() throw();
virtual void throwSelf() const;
protected:
virtual Base * copy() const;
};
/* try to quit out of a menu or something. Maybe this should derive from
* MenuException?
*/
class Quit: public Base {
public:
Quit(const std::string & file, int line);
Quit(const std::string & file, int line, const Base & nested);
virtual ~Quit() throw();
virtual void throwSelf() const;
protected:
virtual Base * copy() const;
};
+class FontException: public Base {
+public:
+ FontException(const std::string & file, int line, const std::string & reason);
+ FontException(const std::string & file, int line, const Base & nested, const std::string & reason);
+ virtual ~FontException() throw();
+ virtual void throwSelf() const;
+protected:
+ virtual const std::string getReason() const;
+ virtual Base * copy() const;
+ std::string reason;
+};
+
}
#endif
diff --git a/util/font.cpp b/util/font.cpp
index 9a7462bb..d54815d3 100644
--- a/util/font.cpp
+++ b/util/font.cpp
@@ -1,317 +1,318 @@
#ifdef USE_ALLEGRO
/* for textout_* and whatnot */
#include <allegro.h>
#endif
#include "graphics/bitmap.h"
#include "font.h"
#include "funcs.h"
#include "init.h"
#include "ftalleg.h"
#include "font_factory.h"
+#include "exceptions/exception.h"
#include <string.h>
using namespace std;
Util::Parameter<Util::ReferenceCount<Filesystem::RelativePath> > Font::defaultFont;
Font::Font(){
}
/* copy/pasted from network/message.cpp */
static vector<string> wrapStrings(const string & left, const string & right, const Font & font, int max, vector< string > accum){
if (left == ""){
return accum;
}
int length = font.textLength(left.c_str());
if (length >= max){
return wrapStrings(left.substr(0, left.length() / 2), left.substr(left.length() / 2) + right, font, max, accum);
} else if (right == "" || font.textLength((left + right.substr(0, 1)).c_str()) >= max){
accum.push_back(left);
return wrapStrings(right, "", font, max, accum);
} else {
return wrapStrings(left + right.substr(0, 1), right.substr(1), font, max, accum);
}
}
void Font::printfWrapLine(int x, int & y, Graphics::Color color, const Graphics::Bitmap & work, int maxWidth, const char * line) const {
vector< string > all;
all = wrapStrings(string(line), "", *this, maxWidth, all );
for ( vector< string >::iterator str = all.begin(); str != all.end(); str++ ){
printf(x, y, color, work, *str, 0);
y += getHeight();
}
y += getHeight() / 2;
}
#if 0
void Font::printfWrapLine2(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;
}
#endif
void Font::printfWrap(int x, int y, Graphics::Color color, const Graphics::Bitmap & work, int maxWidth, const std::string & str, int marker, ... ) const {
char buf[4096];
va_list ap;
va_start(ap, marker);
Util::limitPrintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
char * start = buf;
char * end = strchr(start, '\n');
while (end != NULL){
char tmp[1024];
unsigned 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(){
}
#ifdef USE_ALLEGRO
AllegroFont::AllegroFont( const FONT * const 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 );
}
int AllegroFont::getHeight( const string & str ) const {
return getHeight();
}
int AllegroFont::getHeight() const {
return text_height( getInternalFont() );
}
void AllegroFont::setSize( const int x, const int y ){
}
int AllegroFont::getSizeX() const {
return 0;
}
int AllegroFont::getSizeY() const {
return 0;
}
void AllegroFont::printf( int x, int y, int xSize, int ySize, int color, const Graphics::Bitmap & work, const string & str, int marker, ... ) const {
char buf[512];
va_list ap;
va_start(ap, marker);
Util::limitPrintf(buf, sizeof(buf), str.c_str(), ap);
va_end(ap);
textout_ex(work.getData()->getBitmap(), getInternalFont(), buf, x, y, color, -1);
}
void AllegroFont::printf( int x, int y, int color, const Graphics::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.getData()->getBitmap(), getInternalFont(), buf, x, y, color, -1);
}
#endif
const Font & Font::getDefaultFont(){
return getDefaultFont(16, 16);
}
const Path::RelativePath & Font::getDefaultFontPath(){
if (defaultFont.current() == NULL){
- throw Exception::Base(__FILE__, __LINE__);
+ throw Exception::FontException(__FILE__, __LINE__, "No default font set");
}
return *defaultFont.current();
}
const Font & Font::getDefaultFont(int width, int height){
Font * font = FontFactory::getFont(getDefaultFontPath(), width, height);
if (font == NULL){
- throw Exception::Base(__FILE__, __LINE__);
+ throw Exception::FontException(__FILE__, __LINE__, "No default font set");
}
return *font;
}
/* name should be the path of a .ttf file in the fonts/ directory.
* something like 'arial.ttf'
*/
const Font & Font::getFont(const Filesystem::RelativePath & name, const int x, const int y){
Font * check = FontFactory::getFont(name, x, y);
if (check == NULL){
- throw Exception::Base(__FILE__, __LINE__);
+ throw Exception::FontException(__FILE__, __LINE__, "Could not get font");
}
Font & font = *check;
/* sanity check */
if (font.getHeight("A") == 0){
return getDefaultFont();
}
return font;
}
const Font & Font::getFont( const Filesystem::AbsolutePath & name, const int x, const int y){
return *FontFactory::getFont(name, x, y);
}
FreeTypeFont::FreeTypeFont(const Filesystem::AbsolutePath & 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));
}
void FreeTypeFont::printf( int x, int y, int xSize, int ySize, Graphics::Color color, const Graphics::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, Graphics::Color color, const Graphics::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);
}
void FreeTypeFont::setSize( const int x, const int y ){
this->sizeX = x;
this->sizeY = y;
this->font->setSize( this->sizeX, this->sizeY );
}
int FreeTypeFont::getSizeX() const {
return this->sizeX;
}
int FreeTypeFont::getSizeY() const {
return this->sizeY;
}
FreeTypeFont::~FreeTypeFont(){
// cout << "Delete font " << this->font << endl;
if (own){
delete this->font;
}
}
NullFont::NullFont(){
}
NullFont::~NullFont(){
}
void NullFont::setSize( const int x, const int y ){
}
int NullFont::getSizeX() const {
return 0;
}
int NullFont::getSizeY() const {
return 0;
}
int NullFont::textLength( const char * text ) const {
return 0;
}
int NullFont::getHeight( const std::string & str ) const {
return 0;
}
int NullFont::getHeight() const {
return 0;
}
void NullFont::printf( int x, int y, int xSize, int ySize, Graphics::Color color, const Graphics::Bitmap & work, const std::string & str, int marker, ... ) const {
}
void NullFont::printf( int x, int y, Graphics::Color color, const Graphics::Bitmap & work, const std::string & str, int marker, ... ) const {
}
diff --git a/util/font_factory.cpp b/util/font_factory.cpp
index f39cb56f..a79468f0 100644
--- a/util/font_factory.cpp
+++ b/util/font_factory.cpp
@@ -1,125 +1,129 @@
#include <string>
#ifdef USE_ALLEGRO
#include <allegro.h>
#endif
#include <map>
#include "font.h"
#include "funcs.h"
#include "file-system.h"
#include "thread.h"
#include "font_factory.h"
#include "debug.h"
#include "exceptions/exception.h"
#include "exceptions/load_exception.h"
#include "ftalleg.h"
// #include "fonts.h"
using namespace std;
FontFactory * FontFactory::my_factory = NULL;
Font * FontFactory::getFont(const Filesystem::RelativePath & path, const int x, const int y ){
if ( my_factory == NULL ){
my_factory = new FontFactory();
}
return my_factory->getRealFont(path, x, y );
}
Font * FontFactory::getFont(const Filesystem::AbsolutePath & path, const int x, const int y ){
if ( my_factory == NULL ){
my_factory = new FontFactory();
}
return my_factory->getRealFont(path, x, y );
}
void FontFactory::destroy(){
if ( my_factory != NULL ){
delete my_factory;
my_factory = NULL;
}
}
void FontFactory::clear(){
if (my_factory != NULL){
my_factory->_clear();
}
}
Font * FontFactory::getRealFont(const Filesystem::AbsolutePath & path, int x, int y){
Util::Thread::ScopedLock locked(lock);
if (font_mapper.find(path.path()) == font_mapper.end()){
try{
FreeTypeFont * font = new FreeTypeFont(path);
font_mapper[path.path()] = font;
} catch (const ftalleg::Exception & failure){
throw LoadException(__FILE__, __LINE__, failure.getReason());
}
}
Font * font = font_mapper[path.path()];
if (font == NULL){
- throw Exception::Base(__FILE__, __LINE__);
+ std::ostringstream out;
+ out << "No font for " << path.path();
+ throw Exception::FontException(__FILE__, __LINE__, out.str());
}
if (x < 1){
x = 1;
}
if (y < 1){
y = 1;
}
font->setSize(x, y);
return font;
}
Font * FontFactory::getRealFont(const Filesystem::RelativePath & path, const int x, const int y ){
Util::Thread::ScopedLock locked(lock);
try{
if (font_mapper.find(path.path()) == font_mapper.end()){
font_mapper[path.path()] = new FreeTypeFont(Storage::instance().find(path));
}
Font * f = font_mapper[path.path()];
if (f == NULL){
- throw Exception::Base(__FILE__, __LINE__);
+ std::ostringstream out;
+ out << "No font for " << path.path();
+ throw Exception::FontException(__FILE__, __LINE__, out.str());
}
f->setSize(x, y);
return f;
} catch (const Filesystem::NotFound & e){
Global::debug(0) << "Warning: could not find font " << path.path() << ": " << e.getTrace() << endl;
return &nullFont;
}
// return font_mapper[ str ];
}
void FontFactory::_clear(){
for (map<string,Font*>::iterator it = font_mapper.begin(); it != font_mapper.end(); it++){
Font * s = (*it).second;
delete s;
}
font_mapper.clear();
#ifdef USE_ALLEGRO
font_mapper["bios"] = new AllegroFont(::font);
#endif
}
FontFactory::FontFactory(){
// my_data = load_datafile(Filesystem::find(Filesystem::RelativePath("fonts.dat")).path().c_str());
#ifdef USE_ALLEGRO
font_mapper[ "bios" ] = new AllegroFont(::font);
#endif
}
FontFactory::~FontFactory(){
for ( map<string,Font*>::iterator it = font_mapper.begin(); it != font_mapper.end(); it++ ){
Font * s = (*it).second;
delete s;
}
/*
if ( my_data != NULL ){
unload_datafile( my_data );
}
*/
}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Jun 20, 7:38 PM (1 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72964
Default Alt Text
(17 KB)

Event Timeline