Permalink
Browse files

SLUDGE: use U32String to replace sludge utf8 library

  • Loading branch information...
yinsimei committed Jul 6, 2017
1 parent 526d0a8 commit 72b52698521bb03fa8b3dbee4f8521a334916f48

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
View
@@ -50,7 +50,7 @@
#include "sludge/thumbnail.h" #include "sludge/thumbnail.h"
#include "sludge/graphics.h" #include "sludge/graphics.h"
#include "sludge/sludge.h" #include "sludge/sludge.h"
#include "sludge/CommonCode/utf8.h" #include "sludge/utf8.h"
namespace Sludge { namespace Sludge {
@@ -558,18 +558,21 @@ builtIn(substring) {
wholeString = getTextFromAnyVar(fun->stack->thisVar); wholeString = getTextFromAnyVar(fun->stack->thisVar);
trimStack(fun->stack); trimStack(fun->stack);
if (u8_strlen(wholeString) < start + length) { UTF8Converter convert(wholeString);
length = u8_strlen(wholeString) - start; Common::U32String str32 = convert.getU32String();
if (u8_strlen(wholeString) < start) {
if (str32.size() < start + length) {
length = str32.size() - start;
if (str32.size() < start) {
start = 0; start = 0;
} }
} }
if (length < 0) { if (length < 0) {
length = 0; length = 0;
} }
int startoffset = u8_offset(wholeString, start); int startoffset = convert.getOriginOffset(start);
int endoffset = u8_offset(wholeString, start + length); int endoffset = convert.getOriginOffset(start + length);
newString = new char[endoffset - startoffset + 1]; newString = new char[endoffset - startoffset + 1];
if (!checkNew(newString)) { if (!checkNew(newString)) {
@@ -580,7 +583,7 @@ builtIn(substring) {
newString[endoffset - startoffset] = 0; newString[endoffset - startoffset] = 0;
makeTextVar(fun->reg, newString); makeTextVar(fun->reg, newString);
delete newString; delete []newString;
return BR_CONTINUE; return BR_CONTINUE;
} }
@@ -28,17 +28,17 @@
#include "sludge/newfatal.h" #include "sludge/newfatal.h"
#include "sludge/moreio.h" #include "sludge/moreio.h"
#include "sludge/platform-dependent.h" #include "sludge/platform-dependent.h"
#include "sludge/CommonCode/utf8.h" #include "sludge/utf8.h"
namespace Sludge { namespace Sludge {
spriteBank theFont; spriteBank theFont;
int fontHeight = 0, numFontColours, loadedFontNum; int fontHeight = 0, numFontColours, loadedFontNum;
char *fontOrderString = NULL; Common::U32String fontOrderString;
short fontSpace = -1; short fontSpace = -1;
uint32 *fontTable = NULL; uint32 *fontTable = NULL;
unsigned int fontTableSize = 0; uint fontTableSize = 0;
#define fontInTable(x) ((x<fontTableSize) ? fontTable[(uint32) x] : 0) #define fontInTable(x) ((x<fontTableSize) ? fontTable[(uint32) x] : 0)
@@ -50,82 +50,80 @@ bool isInFont(char *theText) {
if (!theText[0]) if (!theText[0])
return 0; return 0;
Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
// We don't want to compare strings. Only single characters allowed! // We don't want to compare strings. Only single characters allowed!
if (u8_strlen(theText) > 1) if (str32.size() > 1)
return false; return false;
int i = 0; uint32 c = str32[0];
uint32 c = u8_nextchar(theText, &i);
return u8_strchr(fontOrderString, c, &i); // check if font order contains the utf8 char
return fontOrderString.contains(c);
} }
int stringLength(char *theText) { int stringLength(char *theText) {
return u8_strlen(theText); Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
return str32.size();
} }
int stringWidth(char *theText) { int stringWidth(char *theText) {
int a = 0;
uint32 c;
int xOff = 0; int xOff = 0;
if (!fontTableSize) if (!fontTableSize)
return 0; return 0;
while (theText[a]) { Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
c = u8_nextchar(theText, &a);
for (uint i = 0; i < str32.size(); ++i) {
uint32 c = str32[i];
xOff += theFont.sprites[fontInTable(c)].surface.w + fontSpace; xOff += theFont.sprites[fontInTable(c)].surface.w + fontSpace;
} }
return xOff; return xOff;
} }
void pasteString(char *theText, int xOff, int y, spritePalette &thePal) { void pasteString(char *theText, int xOff, int y, spritePalette &thePal) {
sprite *mySprite;
int a = 0;
uint32 c;
if (!fontTableSize) if (!fontTableSize)
return; return;
xOff += (int)((float)(fontSpace >> 1) / cameraZoom); xOff += (int)((float)(fontSpace >> 1) / cameraZoom);
while (theText[a]) {
c = u8_nextchar(theText, &a); Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
mySprite = &theFont.sprites[fontInTable(c)];
for (uint32 i = 0; i < str32.size(); ++i) {
uint32 c = str32[i];
sprite *mySprite = &theFont.sprites[fontInTable(c)];
fontSprite(xOff, y, *mySprite, thePal); fontSprite(xOff, y, *mySprite, thePal);
xOff += (int)((double)(mySprite->surface.w + fontSpace) / cameraZoom); xOff += (int)((double)(mySprite->surface.w + fontSpace) / cameraZoom);
} }
} }
void pasteStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) { void pasteStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) {
sprite *mySprite;
int a = 0;
uint32 c;
if (!fontTableSize) if (!fontTableSize)
return; return;
Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
xOff += fontSpace >> 1; xOff += fontSpace >> 1;
while (theText[a]) { for (uint32 i = 0; i < str32.size(); ++i) {
c = u8_nextchar(theText, &a); uint32 c = str32[i];
mySprite = &theFont.sprites[fontInTable(c)]; sprite *mySprite = &theFont.sprites[fontInTable(c)];
pasteSpriteToBackDrop(xOff, y, *mySprite, thePal); pasteSpriteToBackDrop(xOff, y, *mySprite, thePal);
xOff += mySprite->surface.w + fontSpace; xOff += mySprite->surface.w + fontSpace;
} }
} }
void burnStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) { void burnStringToBackdrop(char *theText, int xOff, int y, spritePalette &thePal) {
sprite *mySprite;
int a = 0;
uint32 c;
if (!fontTableSize) if (!fontTableSize)
return; return;
Common::U32String str32 = UTF8Converter::convertUtf8ToUtf32(theText);
xOff += fontSpace >> 1; xOff += fontSpace >> 1;
while (theText[a]) { for (uint i = 0; i < str32.size(); ++i) {
c = u8_nextchar(theText, &a); uint32 c = str32[i];
mySprite = &theFont.sprites[fontInTable(c)]; sprite *mySprite = &theFont.sprites[fontInTable(c)];
burnSpriteToBackDrop(xOff, y, *mySprite, thePal); burnSpriteToBackDrop(xOff, y, *mySprite, thePal);
xOff += mySprite->surface.w + fontSpace; xOff += mySprite->surface.w + fontSpace;
} }
@@ -165,38 +163,35 @@ void setFontColour(spritePalette &sP, byte r, byte g, byte b) {
} }
bool loadFont(int filenum, const char *charOrder, int h) { bool loadFont(int filenum, const char *charOrder, int h) {
int a = 0; fontOrderString.clear();
uint32 c; fontOrderString = UTF8Converter::convertUtf8ToUtf32(charOrder);
delete[] fontOrderString;
fontOrderString = copyString(charOrder);
forgetSpriteBank(theFont); forgetSpriteBank(theFont);
loadedFontNum = filenum; loadedFontNum = filenum;
// get max value among all utf8 chars
fontTableSize = 0; fontTableSize = 0;
while (charOrder[a]) { for (uint32 i = 0; i < fontOrderString.size(); ++i) {
c = u8_nextchar(charOrder, &a); uint32 c = fontOrderString[i];
if (c > fontTableSize) if (c > fontTableSize)
fontTableSize = c; fontTableSize = c;
} }
fontTableSize++; fontTableSize++;
// create an index table from utf8 char to the index
delete[] fontTable; delete[] fontTable;
fontTable = new uint32[fontTableSize]; fontTable = new uint32[fontTableSize];
if (!checkNew(fontTable)) if (!checkNew(fontTable))
return false; return false;
for (a = 0; a < fontTableSize; a++) { for (uint i = 0; i < fontTableSize; i++) {
fontTable[a] = 0; fontTable[i] = 0;
} }
a = 0;
int i = 0; for (uint i = 0; i < fontOrderString.size(); ++i) {
while (charOrder[a]) { uint32 c = fontOrderString[i];
c = u8_nextchar(charOrder, &a);
fontTable[c] = i; fontTable[c] = i;
i++;
} }
if (!loadSpriteBank(filenum, theFont, true)) { if (!loadSpriteBank(filenum, theFont, true)) {
@@ -22,6 +22,8 @@
#ifndef SLUDGE_FONTTEXT_H #ifndef SLUDGE_FONTTEXT_H
#define SLUDGE_FONTTEXT_H #define SLUDGE_FONTTEXT_H
#include "common/ustr.h"
namespace Sludge { namespace Sludge {
bool loadFont(int filenum, const char *charOrder, int); bool loadFont(int filenum, const char *charOrder, int);
View
@@ -39,9 +39,9 @@ MODULE_OBJS := \
thumbnail.o \ thumbnail.o \
timing.o \ timing.o \
transition.o \ transition.o \
utf8.o \
variable.o \ variable.o \
zbuffer.o \ zbuffer.o \
CommonCode/utf8.o \
# shaders.o \ # shaders.o \
# libwebm/mkvparser.o \ # libwebm/mkvparser.o \
# libwebm/mkvreader.o \ # libwebm/mkvreader.o \
Oops, something went wrong.

0 comments on commit 72b5269

Please sign in to comment.