Permalink
Browse files

SLUDGE: use U32String to replace sludge utf8 library

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

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,96 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/system.h"
/* is c the start of a utf8 sequence? */
#define isutf(c) (((c)&0xC0)!=0x80)
/* convert UTF-8 data to wide character */
int u8_toucs(uint32 *dest, int sz, char *src, int srcsz);
/* the opposite conversion */
int u8_toutf8(char *dest, int sz, uint32 *src, int srcsz);
/* single character to UTF-8 */
int u8_wc_toutf8(char *dest, uint32 ch);
/* character number to byte offset */
int u8_offset(char *str, int charnum);
/* byte offset to character number */
int u8_charnum(char *s, int offset);
/* return next character, updating an index variable */
uint32 u8_nextchar(const char *s, int *i);
/* move to next character */
void u8_inc(char *s, int *i);
/* move to previous character */
void u8_dec(char *s, int *i);
/* returns length of next utf-8 sequence */
int u8_seqlen(char *s);
/* assuming src points to the character after a backslash, read an
escape sequence, storing the result in dest and returning the number of
input characters processed */
int u8_read_escape_sequence(char *src, uint32 *dest);
/* given a wide character, convert it to an ASCII escape sequence stored in
buf, where buf is "sz" bytes. returns the number of characters output. */
int u8_escape_wchar(char *buf, int sz, uint32 ch);
/* convert a string "src" containing escape sequences to UTF-8 */
int u8_unescape(char *buf, int sz, char *src);
/* convert UTF-8 "src" to ASCII with escape sequences.
if escape_quotes is nonzero, quote characters will be preceded by
backslashes as well. */
int u8_escape(char *buf, int sz, char *src, int escape_quotes);
/* utility predicates used by the above */
int octal_digit(char c);
int hex_digit(char c);
/* return a pointer to the first occurrence of ch in s, or NULL if not
found. character index of found character returned in *charn. */
char *u8_strchr(char *s, uint32 ch, int *charn);
/* same as the above, but searches a buffer of a given size instead of
a NUL-terminated string. */
char *u8_memchr(char *s, uint32 ch, size_t sz, int *charn);
/* count the number of characters in a UTF-8 string */
int u8_strlen(char *s);
int u8_is_locale_utf8(char *locale);
/* printf where the format string and arguments may be in UTF-8.
you can avoid this function and just use ordinary printf() if the current
locale is UTF-8. */
int u8_vprintf(char *fmt, va_list ap);
int u8_printf(char *fmt, ...);
int u8_isvalid(const char *input);
View
@@ -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 {
@@ -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)) {
@@ -580,7 +583,7 @@ builtIn(substring) {
newString[endoffset - startoffset] = 0;
makeTextVar(fun->reg, newString);
delete newString;
delete []newString;
return BR_CONTINUE;
}
@@ -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)
@@ -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;
}
@@ -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)) {
@@ -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);
View
@@ -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 \
Oops, something went wrong.

0 comments on commit 72b5269

Please sign in to comment.