Skip to content

Commit

Permalink
SLUDGE: use U32String to replace sludge utf8 library
Browse files Browse the repository at this point in the history
  • Loading branch information
yinsimei authored and sev- committed Jul 13, 2017
1 parent fe773c1 commit 302c946
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 706 deletions.
555 changes: 0 additions & 555 deletions engines/sludge/CommonCode/utf8.cpp

This file was deleted.

96 changes: 0 additions & 96 deletions engines/sludge/CommonCode/utf8.h

This file was deleted.

17 changes: 10 additions & 7 deletions engines/sludge/builtin.cpp
Expand Up @@ -50,7 +50,7 @@
#include "sludge/thumbnail.h"
#include "sludge/graphics.h"
#include "sludge/sludge.h"
#include "sludge/CommonCode/utf8.h"
#include "sludge/utf8.h"

namespace Sludge {

Expand Down Expand Up @@ -558,18 +558,21 @@ builtIn(substring) {
wholeString = getTextFromAnyVar(fun->stack->thisVar);
trimStack(fun->stack);

if (u8_strlen(wholeString) < start + length) {
length = u8_strlen(wholeString) - start;
if (u8_strlen(wholeString) < start) {
UTF8Converter convert(wholeString);
Common::U32String str32 = convert.getU32String();

if (str32.size() < start + length) {
length = str32.size() - start;
if (str32.size() < start) {
start = 0;
}
}
if (length < 0) {
length = 0;
}

int startoffset = u8_offset(wholeString, start);
int endoffset = u8_offset(wholeString, start + length);
int startoffset = convert.getOriginOffset(start);
int endoffset = convert.getOriginOffset(start + length);

newString = new char[endoffset - startoffset + 1];
if (!checkNew(newString)) {
Expand All @@ -580,7 +583,7 @@ builtIn(substring) {
newString[endoffset - startoffset] = 0;

makeTextVar(fun->reg, newString);
delete newString;
delete []newString;
return BR_CONTINUE;
}

Expand Down
89 changes: 42 additions & 47 deletions engines/sludge/fonttext.cpp
Expand Up @@ -28,17 +28,17 @@
#include "sludge/newfatal.h"
#include "sludge/moreio.h"
#include "sludge/platform-dependent.h"
#include "sludge/CommonCode/utf8.h"
#include "sludge/utf8.h"

namespace Sludge {

spriteBank theFont;
int fontHeight = 0, numFontColours, loadedFontNum;
char *fontOrderString = NULL;
Common::U32String fontOrderString;
short fontSpace = -1;

uint32 *fontTable = NULL;
unsigned int fontTableSize = 0;
uint fontTableSize = 0;

#define fontInTable(x) ((x<fontTableSize) ? fontTable[(uint32) x] : 0)

Expand All @@ -50,82 +50,80 @@ bool isInFont(char *theText) {
if (!theText[0])
return 0;

Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

// We don't want to compare strings. Only single characters allowed!
if (u8_strlen(theText) > 1)
if (str32.size() > 1)
return false;

int i = 0;
uint32 c = u8_nextchar(theText, &i);
uint32 c = str32[0];

return u8_strchr(fontOrderString, c, &i);
// check if font order contains the utf8 char
return fontOrderString.contains(c);
}

int stringLength(char *theText) {
return u8_strlen(theText);
Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
return str32.size();
}

int stringWidth(char *theText) {
int a = 0;
uint32 c;
int xOff = 0;

if (!fontTableSize)
return 0;

while (theText[a]) {
c = u8_nextchar(theText, &a);
Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

for (uint i = 0; i < str32.size(); ++i) {
uint32 c = str32[i];
xOff += theFont.sprites[fontInTable(c)].surface.w + fontSpace;
}

return xOff;
}

void pasteString(char *theText, int xOff, int y, spritePalette &thePal) {
sprite *mySprite;
int a = 0;
uint32 c;

if (!fontTableSize)
return;

xOff += (int)((float)(fontSpace >> 1) / cameraZoom);
while (theText[a]) {
c = u8_nextchar(theText, &a);
mySprite = &theFont.sprites[fontInTable(c)];

Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

for (uint32 i = 0; i < str32.size(); ++i) {
uint32 c = str32[i];
sprite *mySprite = &theFont.sprites[fontInTable(c)];
fontSprite(xOff, y, *mySprite, thePal);
xOff += (int)((double)(mySprite->surface.w + fontSpace) / cameraZoom);
}
}

void pasteStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) {
sprite *mySprite;
int a = 0;
uint32 c;

if (!fontTableSize)
return;

Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

xOff += fontSpace >> 1;
while (theText[a]) {
c = u8_nextchar(theText, &a);
mySprite = &theFont.sprites[fontInTable(c)];
for (uint32 i = 0; i < str32.size(); ++i) {
uint32 c = str32[i];
sprite *mySprite = &theFont.sprites[fontInTable(c)];
pasteSpriteToBackDrop(xOff, y, *mySprite, thePal);
xOff += mySprite->surface.w + fontSpace;
}
}

void burnStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) {
sprite *mySprite;
int a = 0;
uint32 c;

if (!fontTableSize)
return;

Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);

xOff += fontSpace >> 1;
while (theText[a]) {
c = u8_nextchar(theText, &a);
mySprite = &theFont.sprites[fontInTable(c)];
for (uint i = 0; i < str32.size(); ++i) {
uint32 c = str32[i];
sprite *mySprite = &theFont.sprites[fontInTable(c)];
burnSpriteToBackDrop(xOff, y, *mySprite, thePal);
xOff += mySprite->surface.w + fontSpace;
}
Expand Down Expand Up @@ -165,38 +163,35 @@ void setFontColour(spritePalette &sP, byte r, byte g, byte b) {
}

bool loadFont(int filenum, const char *charOrder, int h) {
int a = 0;
uint32 c;

delete[] fontOrderString;
fontOrderString = copyString(charOrder);
fontOrderString.clear();
fontOrderString = UTF8Converter::convertUtf8ToUtf32(charOrder);

forgetSpriteBank(theFont);

loadedFontNum = filenum;

// get max value among all utf8 chars
fontTableSize = 0;
while (charOrder[a]) {
c = u8_nextchar(charOrder, &a);
for (uint32 i = 0; i < fontOrderString.size(); ++i) {
uint32 c = fontOrderString[i];
if (c > fontTableSize)
fontTableSize = c;
}
fontTableSize++;

// create an index table from utf8 char to the index
delete[] fontTable;
fontTable = new uint32[fontTableSize];
if (!checkNew(fontTable))
return false;

for (a = 0; a < fontTableSize; a++) {
fontTable[a] = 0;
for (uint i = 0; i < fontTableSize; i++) {
fontTable[i] = 0;
}
a = 0;
int i = 0;
while (charOrder[a]) {
c = u8_nextchar(charOrder, &a);

for (uint i = 0; i < fontOrderString.size(); ++i) {
uint32 c = fontOrderString[i];
fontTable[c] = i;
i++;
}

if (!loadSpriteBank(filenum, theFont, true)) {
Expand Down
2 changes: 2 additions & 0 deletions engines/sludge/fonttext.h
Expand Up @@ -22,6 +22,8 @@
#ifndef SLUDGE_FONTTEXT_H
#define SLUDGE_FONTTEXT_H

#include "common/ustr.h"

namespace Sludge {

bool loadFont(int filenum, const char *charOrder, int);
Expand Down
2 changes: 1 addition & 1 deletion engines/sludge/module.mk
Expand Up @@ -39,9 +39,9 @@ MODULE_OBJS := \
thumbnail.o \
timing.o \
transition.o \
utf8.o \
variable.o \
zbuffer.o \
CommonCode/utf8.o \
# shaders.o \
# libwebm/mkvparser.o \
# libwebm/mkvreader.o \
Expand Down

0 comments on commit 302c946

Please sign in to comment.