Skip to content

Commit

Permalink
GRAPHICS: Use ScopedPtr in the font classes
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent 3327005 commit 650cebd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 67 deletions.
65 changes: 29 additions & 36 deletions src/graphics/aurora/abcfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* An ABC/SBM font, as used by Jade Empire.
*/

#include "src/common/scopedptr.h"
#include "src/common/ustring.h"
#include "src/common/error.h"
#include "src/common/readstream.h"
Expand Down Expand Up @@ -73,7 +74,7 @@ void ABCFont::draw(uint32 c) const {
}

void ABCFont::load(const Common::UString &name) {
Common::SeekableReadStream *abc = ResMan.getResource(name, ::Aurora::kFileTypeABC);
Common::ScopedPtr<Common::SeekableReadStream> abc(ResMan.getResource(name, ::Aurora::kFileTypeABC));
if (!abc)
throw Common::Exception("No such font \"%s\"", name.c_str());

Expand All @@ -95,55 +96,47 @@ void ABCFont::load(const Common::UString &name) {

bool hasInvalid = false;

try {
if (abc->size() != 524280)
throw Common::Exception("Invalid font (%u)", (uint)abc->size());
if (abc->size() != 524280)
throw Common::Exception("Invalid font (%u)", (uint)abc->size());

_base = abc->readByte();
_base = abc->readByte();

abc->skip(7); // Probably random garbage
abc->skip(7); // Probably random garbage

// Read the ASCII character
for (int i = 1; i < 128; i++) {
Char &c = _ascii[i];
// Read the ASCII character
for (int i = 1; i < 128; i++) {
Char &c = _ascii[i];

readCharDesc(c, *abc);
calcCharVertices(c);
readCharDesc(c, *abc);
calcCharVertices(c);

// Points to the "invalid character"
if (!hasInvalid && (c.dataX == 0) && (c.dataY == 0)) {
_invalid = c;
hasInvalid = true;
}
// Points to the "invalid character"
if (!hasInvalid && (c.dataX == 0) && (c.dataY == 0)) {
_invalid = c;
hasInvalid = true;
}
}

// Read the UTF16 extended characters
for (int i = 128; i < 65535; i++) {
Char c;

readCharDesc(c, *abc);
// Read the UTF16 extended characters
for (int i = 128; i < 65535; i++) {
Char c;

// Points to the "invalid character"
if ((c.dataX == 0) && (c.dataY == 0)) {
if (!hasInvalid) {
calcCharVertices(c);
_invalid = c;
hasInvalid = true;
}
readCharDesc(c, *abc);

continue;
// Points to the "invalid character"
if ((c.dataX == 0) && (c.dataY == 0)) {
if (!hasInvalid) {
calcCharVertices(c);
_invalid = c;
hasInvalid = true;
}

calcCharVertices(c);
_extended.insert(std::make_pair(Common::UString::fromUTF16((uint16) i), c));
continue;
}

} catch (...) {
delete abc;
throw;
calcCharVertices(c);
_extended.insert(std::make_pair(Common::UString::fromUTF16((uint16) i), c));
}

delete abc;
}

void ABCFont::readCharDesc(Char &c, Common::SeekableReadStream &abc) {
Expand Down
23 changes: 5 additions & 18 deletions src/graphics/aurora/nftrfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <cassert>
#include <cstring>

#include "src/common/scopedptr.h"
#include "src/common/util.h"
#include "src/common/maths.h"
#include "src/common/ustring.h"
Expand Down Expand Up @@ -90,16 +91,9 @@ NFTRFont::NFTRFont(Common::SeekableReadStream *nftr, bool invertPalette) :

assert(nftr);

Common::SeekableSubReadStreamEndian *nftrEndian = 0;
try {
nftrEndian = open(nftr);
load(*nftrEndian);
} catch (...) {
delete nftrEndian;
throw;
}
Common::ScopedPtr<Common::SeekableSubReadStreamEndian> nftrEndian(open(nftr));

delete nftrEndian;
load(*nftrEndian);
}

NFTRFont::NFTRFont(const Common::UString &name, bool invertPalette) :
Expand All @@ -109,16 +103,9 @@ NFTRFont::NFTRFont(const Common::UString &name, bool invertPalette) :
if (!nftr)
throw Common::Exception("No such font \"%s\"", name.c_str());

Common::SeekableSubReadStreamEndian *nftrEndian = 0;
try {
nftrEndian = open(nftr);
load(*nftrEndian);
} catch (...) {
delete nftrEndian;
throw;
}
Common::ScopedPtr<Common::SeekableSubReadStreamEndian> nftrEndian(open(nftr));

delete nftrEndian;
load(*nftrEndian);
}

NFTRFont::~NFTRFont() {
Expand Down
16 changes: 4 additions & 12 deletions src/graphics/aurora/ttffont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void TTFFont::Page::rebuild() {
}


TTFFont::TTFFont(Common::SeekableReadStream *ttf, int height) : _ttf(0) {
TTFFont::TTFFont(Common::SeekableReadStream *ttf, int height) {
try {
load(ttf, height);
} catch (...) {
Expand All @@ -73,7 +73,7 @@ TTFFont::TTFFont(Common::SeekableReadStream *ttf, int height) : _ttf(0) {
}
}

TTFFont::TTFFont(const Common::UString &name, int height) : _ttf(0) {
TTFFont::TTFFont(const Common::UString &name, int height) {
Common::SeekableReadStream *ttf = ResMan.getResource(name, ::Aurora::kFileTypeTTF);
if (!ttf)
throw Common::Exception("No such font \"%s\"", name.c_str());
Expand All @@ -91,24 +91,16 @@ TTFFont::~TTFFont() {
}

void TTFFont::clear() {
delete _ttf;
_ttf = 0;

for (std::vector<Page *>::iterator p = _pages.begin(); p != _pages.end(); ++p)
delete *p;

_pages.clear();
}

void TTFFont::load(Common::SeekableReadStream *ttf, int height) {
try {
_ttf = new TTFRenderer(*ttf, height);
} catch (...) {
delete ttf;
throw;
}
Common::ScopedPtr<Common::SeekableReadStream> ttfStream(ttf);

delete ttf;
_ttf.reset(new TTFRenderer(*ttfStream, height));

_height = _ttf->getHeight();
if (_height > kPageHeight)
Expand Down
3 changes: 2 additions & 1 deletion src/graphics/aurora/ttffont.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <map>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

#include "src/graphics/font.h"

Expand Down Expand Up @@ -88,7 +89,7 @@ class TTFFont : public Graphics::Font {
};


TTFRenderer *_ttf;
Common::ScopedPtr<TTFRenderer> _ttf;

std::vector<Page *> _pages;
std::map<uint32, Char> _chars;
Expand Down

0 comments on commit 650cebd

Please sign in to comment.