diff options
Diffstat (limited to 'graphics')
| -rw-r--r-- | graphics/cursorman.cpp | 2 | ||||
| -rw-r--r-- | graphics/font.cpp | 766 | ||||
| -rw-r--r-- | graphics/font.h | 63 | ||||
| -rw-r--r-- | graphics/fontman.cpp | 6 | ||||
| -rw-r--r-- | graphics/fonts/bdf.cpp | 796 | ||||
| -rw-r--r-- | graphics/fonts/bdf.h | 100 | ||||
| -rw-r--r-- | graphics/fonts/consolefont.cpp | 4 | ||||
| -rw-r--r-- | graphics/fonts/newfont.cpp | 4 | ||||
| -rw-r--r-- | graphics/fonts/newfont_big.cpp | 4 | ||||
| -rw-r--r-- | graphics/module.mk | 1 | ||||
| -rw-r--r-- | graphics/sjis.cpp | 377 | ||||
| -rw-r--r-- | graphics/sjis.h | 44 | ||||
| -rw-r--r-- | graphics/yuv_to_rgb.cpp | 2 |
13 files changed, 1175 insertions, 994 deletions
diff --git a/graphics/cursorman.cpp b/graphics/cursorman.cpp index a5498903e2..1d4e482bf4 100644 --- a/graphics/cursorman.cpp +++ b/graphics/cursorman.cpp @@ -24,7 +24,9 @@ #include "common/system.h" #include "common/stack.h" +namespace Common { DECLARE_SINGLETON(Graphics::CursorManager); +} namespace Graphics { diff --git a/graphics/font.cpp b/graphics/font.cpp index 0c180ee47d..3f7152a95e 100644 --- a/graphics/font.cpp +++ b/graphics/font.cpp @@ -19,775 +19,13 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "common/stream.h" -#include "common/file.h" -#include "common/endian.h" +#include "graphics/font.h" + #include "common/array.h" -#include "common/textconsole.h" #include "common/util.h" -#include "graphics/font.h" -#include "graphics/surface.h" namespace Graphics { -void free_font(NewFontData *pf); - -NewFont::~NewFont() { - if (_font) { - free_font(_font); - } -} - -int NewFont::getCharWidth(byte chr) const { - // If no width table is specified, return the maximum width - if (!_desc.width) - return _desc.maxwidth; - // If this character is not included in the font, use the default char. - if (chr < _desc.firstchar || _desc.firstchar + _desc.size < chr) { - chr = _desc.defaultchar; - } - return _desc.width[chr - _desc.firstchar]; -} - - -template <typename PixelType> -void drawCharIntern(byte *ptr, uint pitch, const bitmap_t *src, int h, int minX, int maxX, const PixelType color) { - const bitmap_t maxXMask = ~((1 << (16-maxX)) - 1); - while (h-- > 0) { - bitmap_t buffer = READ_UINT16(src); - src++; - - buffer &= maxXMask; - buffer <<= minX; - PixelType *tmp = (PixelType *)ptr; - while (buffer != 0) { - if ((buffer & 0x8000) != 0) - *tmp = color; - tmp++; - buffer <<= 1; - } - - ptr += pitch; - } -} - -void NewFont::drawChar(Surface *dst, byte chr, const int tx, const int ty, const uint32 color) const { - assert(dst != 0); - - assert(_desc.bits != 0 && _desc.maxwidth <= 16); - assert(dst->format.bytesPerPixel == 1 || dst->format.bytesPerPixel == 2); - - // If this character is not included in the font, use the default char. - if (chr < _desc.firstchar || chr >= _desc.firstchar + _desc.size) { - chr = _desc.defaultchar; - } - - chr -= _desc.firstchar; - - int bbw, bbh, bbx, bby; - - // Get the bounding box of the character - if (!_desc.bbx) { - bbw = _desc.fbbw; - bbh = _desc.fbbh; - bbx = _desc.fbbx; - bby = _desc.fbby; - } else { - bbw = _desc.bbx[chr].w; - bbh = _desc.bbx[chr].h; - bbx = _desc.bbx[chr].x; - bby = _desc.bbx[chr].y; - } - - byte *ptr = (byte *)dst->getBasePtr(tx + bbx, ty + _desc.ascent - bby - bbh); - - const bitmap_t *tmp = _desc.bits + (_desc.offset ? _desc.offset[chr] : (chr * _desc.fbbh)); - - int y = MIN(bbh, ty + _desc.ascent - bby); - tmp += bbh - y; - y -= MAX(0, ty + _desc.ascent - bby - dst->h); - - if (dst->format.bytesPerPixel == 1) - drawCharIntern<byte>(ptr, dst->pitch, tmp, y, MAX(0, -(tx + bbx)), MIN(bbw, dst->w - tx - bbx), color); - else if (dst->format.bytesPerPixel == 2) - drawCharIntern<uint16>(ptr, dst->pitch, tmp, y, MAX(0, -(tx + bbx)), MIN(bbw, dst->w - tx - bbx), color); -} - - -#pragma mark - - -/* BEGIN font.h*/ -/* bitmap_t helper macros*/ -#define BITMAP_WORDS(x) (((x)+15)/16) /* image size in words*/ -#define BITMAP_BYTES(x) (BITMAP_WORDS(x)*sizeof(bitmap_t)) -#define BITMAP_BITSPERIMAGE (sizeof(bitmap_t) * 8) -#define BITMAP_BITVALUE(n) ((bitmap_t) (((bitmap_t) 1) << (n))) -#define BITMAP_FIRSTBIT (BITMAP_BITVALUE(BITMAP_BITSPERIMAGE - 1)) -#define BITMAP_TESTBIT(m) ((m) & BITMAP_FIRSTBIT) -#define BITMAP_SHIFTBIT(m) ((bitmap_t) ((m) << 1)) - -/* builtin C-based proportional/fixed font structure */ -/* based on The Microwindows Project http://microwindows.org */ -struct NewFontData { - char * name; /* font name*/ - int maxwidth; /* max width in pixels*/ - int height; /* height in pixels*/ - int ascent; /* ascent (baseline) height*/ - int firstchar; /* first character in bitmap*/ - int size; /* font size in glyphs*/ - bitmap_t* bits; /* 16-bit right-padded bitmap data*/ - unsigned long* offset; /* offsets into bitmap data*/ - unsigned char* width; /* character widths or NULL if fixed*/ - BBX* bbx; /* character bounding box or NULL if fixed */ - int defaultchar; /* default char (not glyph index)*/ - long bits_size; /* # words of bitmap_t bits*/ - - /* unused by runtime system, read in by convbdf*/ - char * facename; /* facename of font*/ - char * copyright; /* copyright info for loadable fonts*/ - int pixel_size; - int descent; - int fbbw, fbbh, fbbx, fbby; -}; -/* END font.h*/ - -#define isprefix(buf,str) (!strncmp(buf, str, strlen(str))) -#define strequal(s1,s2) (!strcmp(s1, s2)) - -#define EXTRA 300 - -int start_char = 0; -int limit_char = 255; - -NewFontData* bdf_read_font(Common::SeekableReadStream &fp); -int bdf_read_header(Common::SeekableReadStream &fp, NewFontData* pf); -int bdf_read_bitmaps(Common::SeekableReadStream &fp, NewFontData* pf); -char * bdf_getline(Common::SeekableReadStream &fp, char *buf, int len); -bitmap_t bdf_hexval(unsigned char *buf); - -void free_font(NewFontData* pf) { - if (!pf) - return; - free(pf->name); - free(pf->facename); - free(pf->bits); - free(pf->offset); - free(pf->width); - free(pf->bbx); - free(pf); -} - -/* build incore structure from .bdf file*/ -NewFontData* bdf_read_font(Common::SeekableReadStream &fp) { - NewFontData* pf; - uint32 pos = fp.pos(); - - pf = (NewFontData*)calloc(1, sizeof(NewFontData)); - if (!pf) - goto errout; - - if (!bdf_read_header(fp, pf)) { - warning("Error reading font header"); - goto errout; - } - - fp.seek(pos, SEEK_SET); - - if (!bdf_read_bitmaps(fp, pf)) { - warning("Error reading font bitmaps"); - goto errout; - } - - return pf; - - errout: - free_font(pf); - return NULL; -} - -/* read bdf font header information, return 0 on error*/ -int bdf_read_header(Common::SeekableReadStream &fp, NewFontData* pf) { - int encoding = 0; - int nchars = 0, maxwidth, maxheight; - int firstchar = 65535; - int lastchar = -1; - char buf[256]; - char facename[256]; - char copyright[256]; - memset(facename, 0, sizeof(facename)); - memset(copyright, 0, sizeof(copyright)); - - /* set certain values to errors for later error checking*/ - pf->defaultchar = -1; - pf->ascent = -1; - pf->descent = -1; - - for (;;) { - if (!bdf_getline(fp, buf, sizeof(buf))) { - warning("Error: EOF on file"); - return 0; - } - - /* note: the way sscanf is used here ensures that a terminating null - character is automatically added. Refer to: - http://pubs.opengroup.org/onlinepubs/009695399/functions/fscanf.html */ - - if (isprefix(buf, "FONT ")) { /* not required*/ - if (sscanf(buf, "FONT %[^\n]", facename) != 1) { - warning("Error: bad 'FONT'"); - return 0; - } - - pf->facename = strdup(facename); - continue; - } - if (isprefix(buf, "COPYRIGHT ")) { /* not required*/ - if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) { - warning("Error: bad 'COPYRIGHT'"); - return 0; - } - - pf->copyright = strdup(copyright); - continue; - } - if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required*/ - if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) { - warning("Error: bad 'DEFAULT_CHAR'"); - return 0; - } - } - if (isprefix(buf, "FONT_DESCENT ")) { - if (sscanf(buf, "FONT_DESCENT %d", &pf->descent) != 1) { - warning("Error: bad 'FONT_DESCENT'"); - return 0; - } - continue; - } - if (isprefix(buf, "FONT_ASCENT ")) { - if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent) != 1) { - warning("Error: bad 'FONT_ASCENT'"); - return 0; - } - continue; - } - if (isprefix(buf, "FONTBOUNDINGBOX ")) { - if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d", - &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) { - warning("Error: bad 'FONTBOUNDINGBOX'"); - return 0; - } - continue; - } - if (isprefix(buf, "CHARS ")) { - if (sscanf(buf, "CHARS %d", &nchars) != 1) { - warning("Error: bad 'CHARS'"); - return 0; - } - continue; - } - - /* - * Reading ENCODING is necessary to get firstchar/lastchar - * which is needed to pre-calculate our offset and widths - * array sizes. - */ - if (isprefix(buf, "ENCODING ")) { - if (sscanf(buf, "ENCODING %d", &encoding) != 1) { - warning("Error: bad 'ENCODING'"); - return 0; - } - if (encoding >= 0 && - encoding <= limit_char && - encoding >= start_char) { - - if (firstchar > encoding) - firstchar = encoding; - if (lastchar < encoding) - lastchar = encoding; - } - continue; - } - if (strequal(buf, "ENDFONT")) - break; - } - - /* calc font height*/ - if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) { - warning("Error: Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING"); - return 0; - } - pf->height = pf->ascent + pf->descent; - - /* calc default char*/ - if (pf->defaultchar < 0 || - pf->defaultchar < firstchar || - pf->defaultchar > limit_char ) - pf->defaultchar = firstchar; - - /* calc font size (offset/width entries)*/ - pf->firstchar = firstchar; - pf->size = lastchar - firstchar + 1; - - /* use the font boundingbox to get initial maxwidth*/ - /*maxwidth = pf->fbbw - pf->fbbx;*/ - maxwidth = pf->fbbw; - maxheight = pf->fbbh; - - /* initially use font bounding box for bits allocation*/ - pf->bits_size = nchars * BITMAP_WORDS(maxwidth) * maxheight; - - /* allocate bits, offset, and width arrays*/ - pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA); - pf->offset = (unsigned long *)malloc(pf->size * sizeof(unsigned long)); - pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char)); - pf->bbx = (BBX *)malloc(pf->size * sizeof(BBX)); - - if (!pf->bits || !pf->offset || !pf->width) { - warning("Error: no memory for font load"); - return 0; - } - - return 1; -} - -/* read bdf font bitmaps, return 0 on error*/ -int bdf_read_bitmaps(Common::SeekableReadStream &fp, NewFontData* pf) { - long ofs = 0; - int maxwidth = 0; - int i, k, encoding = 0, width = 0; - int bbw = 0, bbh = 0, bbx = 0, bby = 0; - int proportional = 0; - int need_bbx = 0; - int encodetable = 0; - long l; - char buf[256]; - - /* initially mark offsets as not used*/ - for (i = 0; i < pf->size; ++i) - pf->offset[i] = (unsigned long)-1; - - for (;;) { - if (!bdf_getline(fp, buf, sizeof(buf))) { - warning("Error: EOF on file"); - return 0; - } - if (isprefix(buf, "STARTCHAR")) { - encoding = width = bbw = bbh = bbx = bby = -1; - continue; - } - if (isprefix(buf, "ENCODING ")) { - if (sscanf(buf, "ENCODING %d", &encoding) != 1) { - warning("Error: bad 'ENCODING'"); - return 0; - } - if (encoding < start_char || encoding > limit_char) - encoding = -1; - continue; - } - if (isprefix(buf, "DWIDTH ")) { - if (sscanf(buf, "DWIDTH %d", &width) != 1) { - warning("Error: bad 'DWIDTH'"); - return 0; - } - /* use font boundingbox width if DWIDTH <= 0*/ - if (width <= 0) - width = pf->fbbw - pf->fbbx; - continue; - } - if (isprefix(buf, "BBX ")) { - if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) { - warning("Error: bad 'BBX'"); - return 0; - } - continue; - } - if (strequal(buf, "BITMAP")) { - bitmap_t *ch_bitmap = pf->bits + ofs; - int ch_words; - - if (encoding < 0) - continue; - - /* set bits offset in encode map*/ - if (pf->offset[encoding-pf->firstchar] != (unsigned long)-1) { - warning("Error: duplicate encoding for character %d (0x%02x), ignoring duplicate", - encoding, encoding); - continue; - } - pf->offset[encoding-pf->firstchar] = ofs; - pf->width[encoding-pf->firstchar] = width; - - pf->bbx[encoding-pf->firstchar].w = bbw; - pf->bbx[encoding-pf->firstchar].h = bbh; - pf->bbx[encoding-pf->firstchar].x = bbx; - pf->bbx[encoding-pf->firstchar].y = bby; - - if (width > maxwidth) - maxwidth = width; - - /* clear bitmap*/ - memset(ch_bitmap, 0, BITMAP_BYTES(bbw) * bbh); - - ch_words = BITMAP_WORDS(bbw); - - /* read bitmaps*/ - for (i = 0; i < bbh; ++i) { - if (!bdf_getline(fp, buf, sizeof(buf))) { - warning("Error: EOF reading BITMAP data"); - return 0; - } - if (isprefix(buf, "ENDCHAR")) - break; - - for (k = 0; k < ch_words; ++k) { - bitmap_t value; - - value = bdf_hexval((unsigned char *)buf); - if (bbw > 8) { - WRITE_UINT16(ch_bitmap, value); - } else { - WRITE_UINT16(ch_bitmap, value << 8); - } - ch_bitmap++; - } - } - - ofs += ch_words * bbh; - continue; - } - if (strequal(buf, "ENDFONT")) - break; - } - - /* set max width*/ - pf->maxwidth = maxwidth; - - /* change unused offset/width values to default char values*/ - for (i = 0; i < pf->size; ++i) { - int defchar = pf->defaultchar - pf->firstchar; - - if (pf->offset[i] == (unsigned long)-1) { - pf->offset[i] = pf->offset[defchar]; - pf->width[i] = pf->width[defchar]; - pf->bbx[i].w = pf->bbx[defchar].w; - pf->bbx[i].h = pf->bbx[defchar].h; - pf->bbx[i].x = pf->bbx[defchar].x; - pf->bbx[i].y = pf->bbx[defchar].y; - } - } - - /* determine whether font doesn't require encode table*/ - l = 0; - for (i = 0; i < pf->size; ++i) { - if (pf->offset[i] != (unsigned long)l) { - encodetable = 1; - break; - } - l += BITMAP_WORDS(pf->bbx[i].w) * pf->bbx[i].h; - } - if (!encodetable) { - free(pf->offset); - pf->offset = NULL; - } - - /* determine whether font is fixed-width*/ - for (i = 0; i < pf->size; ++i) { - if (pf->width[i] != maxwidth) { - proportional = 1; - break; - } - } - if (!proportional) { - free(pf->width); - pf->width = NULL; - } - - /* determine if the font needs a bbx table */ - for (i = 0; i < pf->size; ++i) { - if (pf->bbx[i].w != pf->fbbw || pf->bbx[i].h != pf->fbbh || pf->bbx[i].x != pf->fbbx || pf->bbx[i].y != pf->fbby) { - need_bbx = 1; - break; - } - } - if (!need_bbx) { - free(pf->bbx); - pf->bbx = NULL; - } - - /* reallocate bits array to actual bits used*/ - if (ofs < pf->bits_size) { - bitmap_t *tmp = (bitmap_t *)realloc(pf->bits, ofs * sizeof(bitmap_t)); - if (tmp != NULL || ofs == 0) - pf->bits = tmp; - else - error("bdf_read_bitmaps: Error while reallocating memory"); - pf->bits_size = ofs; - } - else { - if (ofs > pf->bits_size) { - warning("Warning: DWIDTH spec > max FONTBOUNDINGBOX"); - if (ofs > pf->bits_size+EXTRA) { - warning("Error: Not enough bits initially allocated"); - return 0; - } - pf->bits_size = ofs; - } - } - - return 1; -} - -/* read the next non-comment line, returns buf or NULL if EOF*/ -// TODO: Can we use SeekableReadStream::readLine instead? -char *bdf_getline(Common::SeekableReadStream &fp, char *buf, int len) { - int c; - char *b; - - for (;;) { - b = buf; - while (!fp.eos()) { - c = fp.readByte(); - if (c == '\r') - continue; - if (c == '\n') - break; - if (b - buf >= (len - 1)) - break; - *b++ = c; - } - *b = '\0'; - if (fp.eos() && b == buf) - return NULL; - if (b != buf && !isprefix(buf, "COMMENT")) - break; - } - return buf; -} - -/* return hex value of buffer */ -bitmap_t bdf_hexval(unsigned char *buf) { - bitmap_t val = 0; - - for (unsigned char *ptr = buf; *ptr; ptr++) { - int c = *ptr; - - if (c >= '0' && c <= '9') - c -= '0'; - else if (c >= 'A' && c <= 'F') - c = c - 'A' + 10; - else if (c >= 'a' && c <= 'f') - c = c - 'a' + 10; - else - c = 0; - val = (val << 4) | c; - } - return val; -} - -NewFont *NewFont::loadFont(Common::SeekableReadStream &stream) { - NewFontData *data = bdf_read_font(stream); - if (!data || stream.err()) { - free_font(data); - return 0; - } - - FontDesc desc; - desc.name = data->name; - desc.maxwidth = data->maxwidth; - desc.height = data->height; - desc.fbbw = data->fbbw; - desc.fbbh = data->fbbh; - desc.fbbx = data->fbbx; - desc.fbby = data->fbby; - desc.ascent = data->ascent; - desc.firstchar = data->firstchar; - desc.size = data->size; - desc.bits = data->bits; - desc.offset = data->offset; - desc.width = data->width; - desc.bbx = data->bbx; - desc.defaultchar = data->defaultchar; - desc.bits_size = data->bits_size; - - return new NewFont(desc, data); -} - -bool NewFont::cacheFontData(const NewFont &font, const Common::String &filename) { - Common::DumpFile cacheFile; - if (!cacheFile.open(filename)) { - warning("Couldn't open file '%s' for writing", filename.c_str()); - return false; - } - - cacheFile.writeUint16BE(font._desc.maxwidth); - cacheFile.writeUint16BE(font._desc.height); - cacheFile.writeUint16BE(font._desc.fbbw); - cacheFile.writeUint16BE(font._desc.fbbh); - cacheFile.writeSint16BE(font._desc.fbbx); - cacheFile.writeSint16BE(font._desc.fbby); - cacheFile.writeUint16BE(font._desc.ascent); - cacheFile.writeUint16BE(font._desc.firstchar); - cacheFile.writeUint16BE(font._desc.size); - cacheFile.writeUint16BE(font._desc.defaultchar); - cacheFile.writeUint32BE(font._desc.bits_size); - - for (long i = 0; i < font._desc.bits_size; ++i) { - cacheFile.writeUint16BE(font._desc.bits[i]); - } - - if (font._desc.offset) { - cacheFile.writeByte(1); - for (int i = 0; i < font._desc.size; ++i) { - cacheFile.writeUint32BE(font._desc.offset[i]); - } - } else { - cacheFile.writeByte(0); - } - - if (font._desc.width) { - cacheFile.writeByte(1); - for (int i = 0; i < font._desc.size; ++i) { - cacheFile.writeByte(font._desc.width[i]); - } - } else { - cacheFile.writeByte(0); - } - - if (font._desc.bbx) { - cacheFile.writeByte(1); - for (int i = 0; i < font._desc.size; ++i) { - cacheFile.writeByte(font._desc.bbx[i].w); - cacheFile.writeByte(font._desc.bbx[i].h); - cacheFile.writeByte(font._desc.bbx[i].x); - cacheFile.writeByte(font._desc.bbx[i].y); - } - } else { - cacheFile.writeByte(0); - } - - return !cacheFile.err(); -} - -NewFont *NewFont::loadFromCache(Common::SeekableReadStream &stream) { - NewFont *font = 0; - - NewFontData *data = (NewFontData *)malloc(sizeof(NewFontData)); - if (!data) - return 0; - - memset(data, 0, sizeof(NewFontData)); - - data->maxwidth = stream.readUint16BE(); - data->height = stream.readUint16BE(); - data->fbbw = stream.readUint16BE(); - data->fbbh = stream.readUint16BE(); - data->fbbx = stream.readSint16BE(); - data->fbby = stream.readSint16BE(); - data->ascent = stream.readUint16BE(); - data->firstchar = stream.readUint16BE(); - data->size = stream.readUint16BE(); - data->defaultchar = stream.readUint16BE(); - data->bits_size = stream.readUint32BE(); - - data->bits = (bitmap_t *)malloc(sizeof(bitmap_t) * data->bits_size); - if (!data->bits) { - free(data); - return 0; - } - - for (long i = 0; i < data->bits_size; ++i) { - data->bits[i] = stream.readUint16BE(); - } - - bool hasOffsetTable = (stream.readByte() != 0); - if (hasOffsetTable) { - data->offset = (unsigned long *)malloc(sizeof(unsigned long) * data->size); - if (!data->offset) { - free(data->bits); - free(data); - return 0; - } - - for (int i = 0; i < data->size; ++i) { - data->offset[i] = stream.readUint32BE(); - } - } - - bool hasWidthTable = (stream.readByte() != 0); - if (hasWidthTable) { - data->width = (unsigned char *)malloc(sizeof(unsigned char) * data->size); - if (!data->width) { - free(data->bits); - free(data->offset); - free(data); - return 0; - } - - for (int i = 0; i < data->size; ++i) { - data->width[i] = stream.readByte(); - } - } - - bool hasBBXTable = (stream.readByte() != 0); - if (hasBBXTable) { - data->bbx = (BBX *)malloc(sizeof(BBX) * data->size); - if (!data->bbx) { - free(data->bits); - free(data->offset); - free(data->width); - free(data); - return 0; - } - - for (int i = 0; i < data->size; ++i) { - data->bbx[i].w = (int8)stream.readByte(); - data->bbx[i].h = (int8)stream.readByte(); - data->bbx[i].x = (int8)stream.readByte(); - data->bbx[i].y = (int8)stream.readByte(); - } - } - - if (stream.err() || stream.eos()) { - free(data->bits); - free(data->offset); - free(data->width); - free(data); - return 0; - } - - FontDesc desc; - desc.name = data->name; - desc.maxwidth = data->maxwidth; - desc.height = data->height; - desc.fbbw = data->fbbw; - desc.fbbh = data->fbbh; - desc.fbbx = data->fbbx; - desc.fbby = data->fbby; - desc.ascent = data->ascent; - desc.firstchar = data->firstchar; - desc.size = data->size; - desc.bits = data->bits; - desc.offset = data->offset; - desc.width = data->width; - desc.bbx = data->bbx; - desc.defaultchar = data->defaultchar; - desc.bits_size = data->bits_size; - - font = new NewFont(desc, data); - if (!font) { - free(data->bits); - free(data->offset); - free(data->width); - free(data); - return 0; - } - - return font; -} - -#pragma mark - - - int Font::getStringWidth(const Common::String &str) const { int space = 0; diff --git a/graphics/font.h b/graphics/font.h index dc75f86e1f..9c0b4affc1 100644 --- a/graphics/font.h +++ b/graphics/font.h @@ -25,7 +25,6 @@ #include "common/str.h" namespace Common { -class SeekableReadStream; template<class T> class Array; } @@ -114,68 +113,6 @@ public: int wordWrapText(const Common::String &str, int maxWidth, Common::Array<Common::String> &lines) const; }; -typedef uint16 bitmap_t; /* bitmap image unit size*/ - -struct BBX { - int8 w; - int8 h; - int8 x; - int8 y; -}; - -/* builtin C-based proportional/fixed font structure */ -/* based on The Microwindows Project http://microwindows.org */ -struct FontDesc { - const char * name; /* font name*/ - int maxwidth; /* max width in pixels*/ - int height; /* height in pixels*/ - int fbbw, fbbh, fbbx, fbby; /* max bounding box */ - int ascent; /* ascent (baseline) height*/ - int firstchar; /* first character in bitmap*/ - int size; /* font size in glyphs*/ - const bitmap_t* bits; /* 16-bit right-padded bitmap data*/ - const unsigned long* offset; /* offsets into bitmap data*/ - const unsigned char* width; /* character widths or NULL if fixed*/ - const BBX* bbx; /* character bounding box or NULL if fixed */ - int defaultchar; /* default char (not glyph index)*/ - long bits_size; /* # words of bitmap_t bits*/ -}; - -struct NewFontData; - -class NewFont : public Font { -protected: - FontDesc _desc; - NewFontData *_font; - -public: - NewFont(const FontDesc &desc, NewFontData *font = 0) : _desc(desc), _font(font) {} - ~NewFont(); - - virtual int getFontHeight() const { return _desc.height; } - virtual int getMaxCharWidth() const { return _desc.maxwidth; } - - virtual int getCharWidth(byte chr) const; - virtual void drawChar(Surface *dst, byte chr, int x, int y, uint32 color) const; - - static NewFont *loadFont(Common::SeekableReadStream &stream); - static bool cacheFontData(const NewFont &font, const Common::String &filename); - static NewFont *loadFromCache(Common::SeekableReadStream &stream); -}; - -#define DEFINE_FONT(n) \ - const NewFont *n = 0; \ - void create_##n() { \ - n = new NewFont(desc); \ - } - -#define FORWARD_DECLARE_FONT(n) \ - extern const NewFont *n; \ - extern void create_##n() - -#define INIT_FONT(n) \ - create_##n() - } // End of namespace Graphics #endif diff --git a/graphics/fontman.cpp b/graphics/fontman.cpp index f40cf97602..e59e5a33c5 100644 --- a/graphics/fontman.cpp +++ b/graphics/fontman.cpp @@ -19,11 +19,15 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "graphics/font.h" #include "graphics/fontman.h" +#include "graphics/font.h" +#include "graphics/fonts/bdf.h" + #include "common/translation.h" +namespace Common { DECLARE_SINGLETON(Graphics::FontManager); +} namespace Graphics { diff --git a/graphics/fonts/bdf.cpp b/graphics/fonts/bdf.cpp new file mode 100644 index 0000000000..b7d0f7749e --- /dev/null +++ b/graphics/fonts/bdf.cpp @@ -0,0 +1,796 @@ +/* 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 "graphics/fonts/bdf.h" + +#include "common/file.h" +#include "common/endian.h" +#include "common/textconsole.h" + +#include "graphics/surface.h" + +namespace Graphics { + +void free_font(BdfFontData *pf); + +BdfFont::~BdfFont() { + if (_font) { + free_font(_font); + } +} + +int BdfFont::getFontHeight() const { + return _desc.height; +} + +int BdfFont::getMaxCharWidth() const { + return _desc.maxwidth; +} + +int BdfFont::getCharWidth(byte chr) const { + // If no width table is specified, return the maximum width + if (!_desc.width) + return _desc.maxwidth; + // If this character is not included in the font, use the default char. + if (chr < _desc.firstchar || _desc.firstchar + _desc.size < chr) { + chr = _desc.defaultchar; + } + return _desc.width[chr - _desc.firstchar]; +} + + +template<typename PixelType> +void drawCharIntern(byte *ptr, uint pitch, const bitmap_t *src, int h, int minX, int maxX, const PixelType color) { + const bitmap_t maxXMask = ~((1 << (16 - maxX)) - 1); + while (h-- > 0) { + bitmap_t buffer = READ_UINT16(src); + src++; + + buffer &= maxXMask; + buffer <<= minX; + PixelType *tmp = (PixelType *)ptr; + while (buffer != 0) { + if ((buffer & 0x8000) != 0) + *tmp = color; + tmp++; + buffer <<= 1; + } + + ptr += pitch; + } +} + +void BdfFont::drawChar(Surface *dst, byte chr, const int tx, const int ty, const uint32 color) const { + assert(dst != 0); + + assert(_desc.bits != 0 && _desc.maxwidth <= 16); + assert(dst->format.bytesPerPixel == 1 || dst->format.bytesPerPixel == 2); + + // If this character is not included in the font, use the default char. + if (chr < _desc.firstchar || chr >= _desc.firstchar + _desc.size) { + chr = _desc.defaultchar; + } + + chr -= _desc.firstchar; + + int bbw, bbh, bbx, bby; + + // Get the bounding box of the character + if (!_desc.bbx) { + bbw = _desc.fbbw; + bbh = _desc.fbbh; + bbx = _desc.fbbx; + bby = _desc.fbby; + } else { + bbw = _desc.bbx[chr].w; + bbh = _desc.bbx[chr].h; + bbx = _desc.bbx[chr].x; + bby = _desc.bbx[chr].y; + } + + byte *ptr = (byte *)dst->getBasePtr(tx + bbx, ty + _desc.ascent - bby - bbh); + + const bitmap_t *tmp = _desc.bits + (_desc.offset ? _desc.offset[chr] : (chr * _desc.fbbh)); + + int y = MIN(bbh, ty + _desc.ascent - bby); + tmp += bbh - y; + y -= MAX(0, ty + _desc.ascent - bby - dst->h); + + if (dst->format.bytesPerPixel == 1) + drawCharIntern<byte>(ptr, dst->pitch, tmp, y, MAX(0, -(tx + bbx)), MIN(bbw, dst->w - tx - bbx), color); + else if (dst->format.bytesPerPixel == 2) + drawCharIntern<uint16>(ptr, dst->pitch, tmp, y, MAX(0, -(tx + bbx)), MIN(bbw, dst->w - tx - bbx), color); +} + + +#pragma mark - + +/* BEGIN font.h*/ +/* bitmap_t helper macros*/ +#define BITMAP_WORDS(x) (((x)+15)/16) /* image size in words*/ +#define BITMAP_BYTES(x) (BITMAP_WORDS(x)*sizeof(bitmap_t)) +#define BITMAP_BITSPERIMAGE (sizeof(bitmap_t) * 8) +#define BITMAP_BITVALUE(n) ((bitmap_t) (((bitmap_t) 1) << (n))) +#define BITMAP_FIRSTBIT (BITMAP_BITVALUE(BITMAP_BITSPERIMAGE - 1)) +#define BITMAP_TESTBIT(m) ((m) & BITMAP_FIRSTBIT) +#define BITMAP_SHIFTBIT(m) ((bitmap_t) ((m) << 1)) + +/* builtin C-based proportional/fixed font structure */ +/* based on The Microwindows Project http://microwindows.org */ +struct BdfFontData { + char *name; /* font name */ + int maxwidth; /* max width in pixels */ + int height; /* height in pixels */ + int ascent; /* ascent (baseline) height */ + int firstchar; /* first character in bitmap */ + int size; /* font size in glyphs */ + bitmap_t *bits; /* 16-bit right-padded bitmap data */ + unsigned long *offset; /* offsets into bitmap data */ + unsigned char *width; /* character widths or NULL if fixed */ + BBX *bbx; /* character bounding box or NULL if fixed */ + int defaultchar; /* default char (not glyph index) */ + long bits_size; /* # words of bitmap_t bits */ + + /* unused by runtime system, read in by convbdf */ + char *facename; /* facename of font */ + char *copyright; /* copyright info for loadable fonts */ + int pixel_size; + int descent; + int fbbw, fbbh, fbbx, fbby; +}; +/* END font.h */ + +#define isprefix(buf,str) (!strncmp(buf, str, strlen(str))) +#define strequal(s1,s2) (!strcmp(s1, s2)) + +#define EXTRA 300 + +int start_char = 0; +int limit_char = 255; + +BdfFontData *bdf_read_font(Common::SeekableReadStream &fp); +int bdf_read_header(Common::SeekableReadStream &fp, BdfFontData *pf); +int bdf_read_bitmaps(Common::SeekableReadStream &fp, BdfFontData *pf); +char *bdf_getline(Common::SeekableReadStream &fp, char *buf, int len); +bitmap_t bdf_hexval(unsigned char *buf); + +void free_font(BdfFontData *pf) { + if (!pf) + return; + free(pf->name); + free(pf->facename); + free(pf->copyright); + free(pf->bits); + free(pf->offset); + free(pf->width); + free(pf->bbx); + free(pf); +} + +/* build incore structure from .bdf file*/ +BdfFontData *bdf_read_font(Common::SeekableReadStream &fp) { + BdfFontData *pf; + uint32 pos = fp.pos(); + + pf = (BdfFontData *)calloc(1, sizeof(BdfFontData)); + if (!pf) + goto errout; + + if (!bdf_read_header(fp, pf)) { + warning("Error reading font header"); + goto errout; + } + + fp.seek(pos, SEEK_SET); + + if (!bdf_read_bitmaps(fp, pf)) { + warning("Error reading font bitmaps"); + goto errout; + } + + return pf; + +errout: + free_font(pf); + return NULL; +} + +/* read bdf font header information, return 0 on error*/ +int bdf_read_header(Common::SeekableReadStream &fp, BdfFontData *pf) { + int encoding = 0; + int nchars = 0, maxwidth, maxheight; + int firstchar = 65535; + int lastchar = -1; + char buf[256]; + char facename[256]; + char copyright[256]; + memset(facename, 0, sizeof(facename)); + memset(copyright, 0, sizeof(copyright)); + + /* set certain values to errors for later error checking*/ + pf->defaultchar = -1; + pf->ascent = -1; + pf->descent = -1; + + for (;;) { + if (!bdf_getline(fp, buf, sizeof(buf))) { + warning("Error: EOF on file"); + return 0; + } + + /* note: the way sscanf is used here ensures that a terminating null + character is automatically added. Refer to: + http://pubs.opengroup.org/onlinepubs/009695399/functions/fscanf.html */ + + if (isprefix(buf, "FONT ")) { /* not required*/ + if (sscanf(buf, "FONT %[^\n]", facename) != 1) { + warning("Error: bad 'FONT'"); + return 0; + } + + pf->facename = strdup(facename); + continue; + } + if (isprefix(buf, "COPYRIGHT ")) { /* not required*/ + if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) { + warning("Error: bad 'COPYRIGHT'"); + return 0; + } + + pf->copyright = strdup(copyright); + continue; + } + if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required*/ + if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) { + warning("Error: bad 'DEFAULT_CHAR'"); + return 0; + } + } + if (isprefix(buf, "FONT_DESCENT ")) { + if (sscanf(buf, "FONT_DESCENT %d", &pf->descent) != 1) { + warning("Error: bad 'FONT_DESCENT'"); + return 0; + } + continue; + } + if (isprefix(buf, "FONT_ASCENT ")) { + if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent) != 1) { + warning("Error: bad 'FONT_ASCENT'"); + return 0; + } + continue; + } + if (isprefix(buf, "FONTBOUNDINGBOX ")) { + if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d", + &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) { + warning("Error: bad 'FONTBOUNDINGBOX'"); + return 0; + } + continue; + } + if (isprefix(buf, "CHARS ")) { + if (sscanf(buf, "CHARS %d", &nchars) != 1) { + warning("Error: bad 'CHARS'"); + return 0; + } + continue; + } + + /* + * Reading ENCODING is necessary to get firstchar/lastchar + * which is needed to pre-calculate our offset and widths + * array sizes. + */ + if (isprefix(buf, "ENCODING ")) { + if (sscanf(buf, "ENCODING %d", &encoding) != 1) { + warning("Error: bad 'ENCODING'"); + return 0; + } + if (encoding >= 0 && + encoding <= limit_char && + encoding >= start_char) { + + if (firstchar > encoding) + firstchar = encoding; + if (lastchar < encoding) + lastchar = encoding; + } + continue; + } + if (strequal(buf, "ENDFONT")) + break; + } + + /* calc font height*/ + if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) { + warning("Error: Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING"); + return 0; + } + pf->height = pf->ascent + pf->descent; + + /* calc default char*/ + if (pf->defaultchar < 0 || + pf->defaultchar < firstchar || + pf->defaultchar > limit_char) + pf->defaultchar = firstchar; + + /* calc font size (offset/width entries)*/ + pf->firstchar = firstchar; + pf->size = lastchar - firstchar + 1; + + /* use the font boundingbox to get initial maxwidth*/ + /*maxwidth = pf->fbbw - pf->fbbx;*/ + maxwidth = pf->fbbw; + maxheight = pf->fbbh; + + /* initially use font bounding box for bits allocation*/ + pf->bits_size = nchars * BITMAP_WORDS(maxwidth) * maxheight; + + /* allocate bits, offset, and width arrays*/ + pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA); + pf->offset = (unsigned long *)malloc(pf->size * sizeof(unsigned long)); + pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char)); + pf->bbx = (BBX *)malloc(pf->size * sizeof(BBX)); + + if (!pf->bits || !pf->offset || !pf->width) { + warning("Error: no memory for font load"); + return 0; + } + + return 1; +} + +/* read bdf font bitmaps, return 0 on error*/ +int bdf_read_bitmaps(Common::SeekableReadStream &fp, BdfFontData *pf) { + long ofs = 0; + int maxwidth = 0; + int i, k, encoding = 0, width = 0; + int bbw = 0, bbh = 0, bbx = 0, bby = 0; + int proportional = 0; + int need_bbx = 0; + int encodetable = 0; + long l; + char buf[256]; + + /* initially mark offsets as not used*/ + for (i = 0; i < pf->size; ++i) + pf->offset[i] = (unsigned long)-1; + + for (;;) { + if (!bdf_getline(fp, buf, sizeof(buf))) { + warning("Error: EOF on file"); + return 0; + } + if (isprefix(buf, "STARTCHAR")) { + encoding = width = bbw = bbh = bbx = bby = -1; + continue; + } + if (isprefix(buf, "ENCODING ")) { + if (sscanf(buf, "ENCODING %d", &encoding) != 1) { + warning("Error: bad 'ENCODING'"); + return 0; + } + if (encoding < start_char || encoding > limit_char) + encoding = -1; + continue; + } + if (isprefix(buf, "DWIDTH ")) { + if (sscanf(buf, "DWIDTH %d", &width) != 1) { + warning("Error: bad 'DWIDTH'"); + return 0; + } + /* use font boundingbox width if DWIDTH <= 0*/ + if (width <= 0) + width = pf->fbbw - pf->fbbx; + continue; + } + if (isprefix(buf, "BBX ")) { + if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) { + warning("Error: bad 'BBX'"); + return 0; + } + continue; + } + if (strequal(buf, "BITMAP")) { + bitmap_t *ch_bitmap = pf->bits + ofs; + int ch_words; + + if (encoding < 0) + continue; + + /* set bits offset in encode map*/ + if (pf->offset[encoding - pf->firstchar] != (unsigned long)-1) { + warning("Error: duplicate encoding for character %d (0x%02x), ignoring duplicate", + encoding, encoding); + continue; + } + pf->offset[encoding - pf->firstchar] = ofs; + pf->width[encoding - pf->firstchar] = width; + + pf->bbx[encoding - pf->firstchar].w = bbw; + pf->bbx[encoding - pf->firstchar].h = bbh; + pf->bbx[encoding - pf->firstchar].x = bbx; + pf->bbx[encoding - pf->firstchar].y = bby; + + if (width > maxwidth) + maxwidth = width; + + /* clear bitmap*/ + memset(ch_bitmap, 0, BITMAP_BYTES(bbw) * bbh); + + ch_words = BITMAP_WORDS(bbw); + + /* read bitmaps*/ + for (i = 0; i < bbh; ++i) { + if (!bdf_getline(fp, buf, sizeof(buf))) { + warning("Error: EOF reading BITMAP data"); + return 0; + } + if (isprefix(buf, "ENDCHAR")) + break; + + for (k = 0; k < ch_words; ++k) { + bitmap_t value; + + value = bdf_hexval((unsigned char *)buf); + if (bbw > 8) { + WRITE_UINT16(ch_bitmap, value); + } else { + WRITE_UINT16(ch_bitmap, value << 8); + } + ch_bitmap++; + } + } + + ofs += ch_words * bbh; + continue; + } + if (strequal(buf, "ENDFONT")) + break; + } + + /* set max width*/ + pf->maxwidth = maxwidth; + + /* change unused offset/width values to default char values*/ + for (i = 0; i < pf->size; ++i) { + int defchar = pf->defaultchar - pf->firstchar; + + if (pf->offset[i] == (unsigned long)-1) { + pf->offset[i] = pf->offset[defchar]; + pf->width[i] = pf->width[defchar]; + pf->bbx[i].w = pf->bbx[defchar].w; + pf->bbx[i].h = pf->bbx[defchar].h; + pf->bbx[i].x = pf->bbx[defchar].x; + pf->bbx[i].y = pf->bbx[defchar].y; + } + } + + /* determine whether font doesn't require encode table*/ + l = 0; + for (i = 0; i < pf->size; ++i) { + if (pf->offset[i] != (unsigned long)l) { + encodetable = 1; + break; + } + l += BITMAP_WORDS(pf->bbx[i].w) * pf->bbx[i].h; + } + if (!encodetable) { + free(pf->offset); + pf->offset = NULL; + } + + /* determine whether font is fixed-width*/ + for (i = 0; i < pf->size; ++i) { + if (pf->width[i] != maxwidth) { + proportional = 1; + break; + } + } + if (!proportional) { + free(pf->width); + pf->width = NULL; + } + + /* determine if the font needs a bbx table */ + for (i = 0; i < pf->size; ++i) { + if (pf->bbx[i].w != pf->fbbw || pf->bbx[i].h != pf->fbbh || pf->bbx[i].x != pf->fbbx || pf->bbx[i].y != pf->fbby) { + need_bbx = 1; + break; + } + } + if (!need_bbx) { + free(pf->bbx); + pf->bbx = NULL; + } + + /* reallocate bits array to actual bits used*/ + if (ofs < pf->bits_size) { + bitmap_t *tmp = (bitmap_t *)realloc(pf->bits, ofs * sizeof(bitmap_t)); + if (tmp != NULL || ofs == 0) + pf->bits = tmp; + else + error("bdf_read_bitmaps: Error while reallocating memory"); + pf->bits_size = ofs; + } else { + if (ofs > pf->bits_size) { + warning("Warning: DWIDTH spec > max FONTBOUNDINGBOX"); + if (ofs > pf->bits_size + EXTRA) { + warning("Error: Not enough bits initially allocated"); + return 0; + } + pf->bits_size = ofs; + } + } + + return 1; +} + +/* read the next non-comment line, returns buf or NULL if EOF*/ +// TODO: Can we use SeekableReadStream::readLine instead? +char *bdf_getline(Common::SeekableReadStream &fp, char *buf, int len) { + int c; + char *b; + + for (;;) { + b = buf; + while (!fp.eos()) { + c = fp.readByte(); + if (c == '\r') + continue; + if (c == '\n') + break; + if (b - buf >= (len - 1)) + break; + *b++ = c; + } + *b = '\0'; + if (fp.eos() && b == buf) + return NULL; + if (b != buf && !isprefix(buf, "COMMENT")) + break; + } + return buf; +} + +/* return hex value of buffer */ +bitmap_t bdf_hexval(unsigned char *buf) { + bitmap_t val = 0; + + for (unsigned char *ptr = buf; *ptr; ptr++) { + int c = *ptr; + + if (c >= '0' && c <= '9') + c -= '0'; + else if (c >= 'A' && c <= 'F') + c = c - 'A' + 10; + else if (c >= 'a' && c <= 'f') + c = c - 'a' + 10; + else + c = 0; + val = (val << 4) | c; + } + return val; +} + +BdfFont *BdfFont::loadFont(Common::SeekableReadStream &stream) { + BdfFontData *data = bdf_read_font(stream); + if (!data || stream.err()) { + free_font(data); + return 0; + } + + BdfFontDesc desc; + desc.name = data->name; + desc.maxwidth = data->maxwidth; + desc.height = data->height; + desc.fbbw = data->fbbw; + desc.fbbh = data->fbbh; + desc.fbbx = data->fbbx; + desc.fbby = data->fbby; + desc.ascent = data->ascent; + desc.firstchar = data->firstchar; + desc.size = data->size; + desc.bits = data->bits; + desc.offset = data->offset; + desc.width = data->width; + desc.bbx = data->bbx; + desc.defaultchar = data->defaultchar; + desc.bits_size = data->bits_size; + + return new BdfFont(desc, data); +} + +bool BdfFont::cacheFontData(const BdfFont &font, const Common::String &filename) { + Common::DumpFile cacheFile; + if (!cacheFile.open(filename)) { + warning("Couldn't open file '%s' for writing", filename.c_str()); + return false; + } + + cacheFile.writeUint16BE(font._desc.maxwidth); + cacheFile.writeUint16BE(font._desc.height); + cacheFile.writeUint16BE(font._desc.fbbw); + cacheFile.writeUint16BE(font._desc.fbbh); + cacheFile.writeSint16BE(font._desc.fbbx); + cacheFile.writeSint16BE(font._desc.fbby); + cacheFile.writeUint16BE(font._desc.ascent); + cacheFile.writeUint16BE(font._desc.firstchar); + cacheFile.writeUint16BE(font._desc.size); + cacheFile.writeUint16BE(font._desc.defaultchar); + cacheFile.writeUint32BE(font._desc.bits_size); + + for (long i = 0; i < font._desc.bits_size; ++i) { + cacheFile.writeUint16BE(font._desc.bits[i]); + } + + if (font._desc.offset) { + cacheFile.writeByte(1); + for (int i = 0; i < font._desc.size; ++i) { + cacheFile.writeUint32BE(font._desc.offset[i]); + } + } else { + cacheFile.writeByte(0); + } + + if (font._desc.width) { + cacheFile.writeByte(1); + for (int i = 0; i < font._desc.size; ++i) { + cacheFile.writeByte(font._desc.width[i]); + } + } else { + cacheFile.writeByte(0); + } + + if (font._desc.bbx) { + cacheFile.writeByte(1); + for (int i = 0; i < font._desc.size; ++i) { + cacheFile.writeByte(font._desc.bbx[i].w); + cacheFile.writeByte(font._desc.bbx[i].h); + cacheFile.writeByte(font._desc.bbx[i].x); + cacheFile.writeByte(font._desc.bbx[i].y); + } + } else { + cacheFile.writeByte(0); + } + + return !cacheFile.err(); +} + +BdfFont *BdfFont::loadFromCache(Common::SeekableReadStream &stream) { + BdfFont *font = 0; + + BdfFontData *data = (BdfFontData *)malloc(sizeof(BdfFontData)); + if (!data) + return 0; + + memset(data, 0, sizeof(BdfFontData)); + + data->maxwidth = stream.readUint16BE(); + data->height = stream.readUint16BE(); + data->fbbw = stream.readUint16BE(); + data->fbbh = stream.readUint16BE(); + data->fbbx = stream.readSint16BE(); + data->fbby = stream.readSint16BE(); + data->ascent = stream.readUint16BE(); + data->firstchar = stream.readUint16BE(); + data->size = stream.readUint16BE(); + data->defaultchar = stream.readUint16BE(); + data->bits_size = stream.readUint32BE(); + + data->bits = (bitmap_t *)malloc(sizeof(bitmap_t) * data->bits_size); + if (!data->bits) { + free(data); + return 0; + } + + for (long i = 0; i < data->bits_size; ++i) { + data->bits[i] = stream.readUint16BE(); + } + + bool hasOffsetTable = (stream.readByte() != 0); + if (hasOffsetTable) { + data->offset = (unsigned long *)malloc(sizeof(unsigned long) * data->size); + if (!data->offset) { + free(data->bits); + free(data); + return 0; + } + + for (int i = 0; i < data->size; ++i) { + data->offset[i] = stream.readUint32BE(); + } + } + + bool hasWidthTable = (stream.readByte() != 0); + if (hasWidthTable) { + data->width = (unsigned char *)malloc(sizeof(unsigned char) * data->size); + if (!data->width) { + free(data->bits); + free(data->offset); + free(data); + return 0; + } + + for (int i = 0; i < data->size; ++i) { + data->width[i] = stream.readByte(); + } + } + + bool hasBBXTable = (stream.readByte() != 0); + if (hasBBXTable) { + data->bbx = (BBX *)malloc(sizeof(BBX) * data->size); + if (!data->bbx) { + free(data->bits); + free(data->offset); + free(data->width); + free(data); + return 0; + } + + for (int i = 0; i < data->size; ++i) { + data->bbx[i].w = (int8)stream.readByte(); + data->bbx[i].h = (int8)stream.readByte(); + data->bbx[i].x = (int8)stream.readByte(); + data->bbx[i].y = (int8)stream.readByte(); + } + } + + if (stream.err() || stream.eos()) { + free(data->bits); + free(data->offset); + free(data->width); + free(data); + return 0; + } + + BdfFontDesc desc; + desc.name = data->name; + desc.maxwidth = data->maxwidth; + desc.height = data->height; + desc.fbbw = data->fbbw; + desc.fbbh = data->fbbh; + desc.fbbx = data->fbbx; + desc.fbby = data->fbby; + desc.ascent = data->ascent; + desc.firstchar = data->firstchar; + desc.size = data->size; + desc.bits = data->bits; + desc.offset = data->offset; + desc.width = data->width; + desc.bbx = data->bbx; + desc.defaultchar = data->defaultchar; + desc.bits_size = data->bits_size; + + font = new BdfFont(desc, data); + if (!font) { + free(data->bits); + free(data->offset); + free(data->width); + free(data); + return 0; + } + + return font; +} + +} // End of namespace Graphics + diff --git a/graphics/fonts/bdf.h b/graphics/fonts/bdf.h new file mode 100644 index 0000000000..31c009eb27 --- /dev/null +++ b/graphics/fonts/bdf.h @@ -0,0 +1,100 @@ +/* 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. + */ + +#ifndef GRAPHICS_FONTS_BDF_H +#define GRAPHICS_FONTS_BDF_H + +#include "common/system.h" + +#include "graphics/font.h" + +namespace Common { +class SeekableReadStream; +} + +namespace Graphics { + +typedef uint16 bitmap_t; /* bitmap image unit size*/ + +struct BBX { + int8 w; + int8 h; + int8 x; + int8 y; +}; + +/* builtin C-based proportional/fixed font structure */ +/* based on The Microwindows Project http://microwindows.org */ +struct BdfFontDesc { + const char *name; /* font name */ + int maxwidth; /* max width in pixels */ + int height; /* height in pixels */ + int fbbw, fbbh, fbbx, fbby; /* max bounding box */ + int ascent; /* ascent (baseline) height */ + int firstchar; /* first character in bitmap */ + int size; /* font size in glyphs */ + const bitmap_t *bits; /* 16-bit right-padded bitmap data */ + const unsigned long *offset; /* offsets into bitmap data */ + const unsigned char *width; /* character widths or NULL if fixed */ + const BBX *bbx; /* character bounding box or NULL if fixed */ + int defaultchar; /* default char (not glyph index) */ + long bits_size; /* # words of bitmap_t bits */ +}; + +struct BdfFontData; + +class BdfFont : public Font { +protected: + BdfFontDesc _desc; + BdfFontData *_font; + +public: + BdfFont(const BdfFontDesc &desc, BdfFontData *font = 0) : _desc(desc), _font(font) {} + ~BdfFont(); + + virtual int getFontHeight() const; + virtual int getMaxCharWidth() const; + + virtual int getCharWidth(byte chr) const; + virtual void drawChar(Surface *dst, byte chr, int x, int y, uint32 color) const; + + static BdfFont *loadFont(Common::SeekableReadStream &stream); + static bool cacheFontData(const BdfFont &font, const Common::String &filename); + static BdfFont *loadFromCache(Common::SeekableReadStream &stream); +}; + +#define DEFINE_FONT(n) \ + const BdfFont *n = 0; \ + void create_##n() { \ + n = new BdfFont(desc); \ + } + +#define FORWARD_DECLARE_FONT(n) \ + extern const BdfFont *n; \ + extern void create_##n() + +#define INIT_FONT(n) \ + create_##n() + +} // End of namespace Graphics + +#endif + diff --git a/graphics/fonts/consolefont.cpp b/graphics/fonts/consolefont.cpp index 65ccd3ec70..5b4768327a 100644 --- a/graphics/fonts/consolefont.cpp +++ b/graphics/fonts/consolefont.cpp @@ -1,5 +1,5 @@ /* Generated by convbdf on Sat Jun 17 01:37:46 2006. */ -#include "graphics/font.h" +#include "graphics/fonts/bdf.h" /* Font information: name: 5x8-L1 @@ -5635,7 +5635,7 @@ static const unsigned long _sysfont_offset[] = { }; /* Exported structure definition. */ -static const FontDesc desc = { +static const BdfFontDesc desc = { "5x8-L1", 5, 8, diff --git a/graphics/fonts/newfont.cpp b/graphics/fonts/newfont.cpp index 0327c0997a..f02baba2de 100644 --- a/graphics/fonts/newfont.cpp +++ b/graphics/fonts/newfont.cpp @@ -1,5 +1,5 @@ /* Generated by convbdf on Sat Jun 17 01:34:15 2006. */ -#include "graphics/font.h" +#include "graphics/fonts/bdf.h" /* Font information: name: clR6x12-L1 @@ -7419,7 +7419,7 @@ static const unsigned long _sysfont_offset[] = { }; /* Exported structure definition. */ -static const FontDesc desc = { +static const BdfFontDesc desc = { "clR6x12-L1", 6, 12, diff --git a/graphics/fonts/newfont_big.cpp b/graphics/fonts/newfont_big.cpp index 7b15a6ab38..59c54a4551 100644 --- a/graphics/fonts/newfont_big.cpp +++ b/graphics/fonts/newfont_big.cpp @@ -1,5 +1,5 @@ /* Generated by convbdf on Tue Jun 13 00:00:22 2006. */ -#include "graphics/font.h" +#include "graphics/fonts/bdf.h" /* Font information: name: helvB12-L1 @@ -5523,7 +5523,7 @@ static const BBX _sysfont_bbx[] = { }; /* Exported structure definition. */ -static const FontDesc desc = { +static const BdfFontDesc desc = { "helvB12-L1", 13, 14, diff --git a/graphics/module.mk b/graphics/module.mk index 32658c96bd..469ee42047 100644 --- a/graphics/module.mk +++ b/graphics/module.mk @@ -6,6 +6,7 @@ MODULE_OBJS := \ dither.o \ font.o \ fontman.o \ + fonts/bdf.o \ fonts/consolefont.o \ fonts/newfont_big.o \ fonts/newfont.o \ diff --git a/graphics/sjis.cpp b/graphics/sjis.cpp index 09e1746df4..10c780b156 100644 --- a/graphics/sjis.cpp +++ b/graphics/sjis.cpp @@ -46,7 +46,7 @@ FontSJIS *FontSJIS::createFont(const Common::Platform platform) { } // Try ScummVM's font. - ret = new FontSjisSVM(); + ret = new FontSjisSVM(platform); if (ret && ret->loadData()) return ret; delete ret; @@ -58,6 +58,51 @@ void FontSJIS::drawChar(Graphics::Surface &dst, uint16 ch, int x, int y, uint32 drawChar(dst.getBasePtr(x, y), ch, dst.pitch, dst.format.bytesPerPixel, c1, c2, dst.w - x, dst.h - y); } +FontSJISBase::FontSJISBase() + : _drawMode(kDefaultMode), _flippedMode(false), _fontWidth(16), _fontHeight(16) { +} + +void FontSJISBase::setDrawingMode(DrawingMode mode) { + _drawMode = mode; +} + +void FontSJISBase::toggleFlippedMode(bool enable) { + _flippedMode = enable; +} + +uint FontSJISBase::getFontHeight() const { + switch (_drawMode) { + case kOutlineMode: + return _fontHeight + 2; + + case kDefaultMode: + return _fontHeight; + + default: + return _fontHeight + 1; + } +} + +uint FontSJISBase::getMaxFontWidth() const { + switch (_drawMode) { + case kOutlineMode: + return _fontWidth + 2; + + case kDefaultMode: + return _fontWidth; + + default: + return _fontWidth + 1; + } +} + +uint FontSJISBase::getCharWidth(uint16 ch) const { + if (isASCII(ch)) + return (_drawMode == kOutlineMode) ? 10 : (_drawMode == kDefaultMode ? 8 : 9); + else + return getMaxFontWidth(); +} + template<typename Color> void FontSJISBase::blitCharacter(const uint8 *glyph, const int w, const int h, uint8 *dst, int pitch, Color c) const { for (int y = 0; y < h; ++y) { @@ -131,6 +176,9 @@ const uint8 *FontSJISBase::flipCharacter(const uint8 *glyph, const int w) const 0x0F, 0x8F, 0x4F, 0xC7, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x97, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF }; + // TODO: This code looks like it will only work with 16 pixel wide + // characters we should really take care that we only call it on these + // or we fix this to support a generic width. for (int i = 0; i < w; i++) { _tempGlyph[i] = flipData[glyph[(w * 2 - 1) - i]]; _tempGlyph[(w * 2 - 1) - i] = flipData[glyph[i]]; @@ -146,14 +194,14 @@ void FontSJISBase::drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, int outlineExtraWidth = 2, outlineExtraHeight = 2; int outlineXOffset = 0, outlineYOffset = 0; - if (is8x16(ch)) { - glyphSource = getCharData8x16(ch); + if (isASCII(ch)) { + glyphSource = getCharData(ch); width = 8; - height = 16; + height = _fontHeight; } else { glyphSource = getCharData(ch); - width = 16; - height = 16; + width = _fontWidth; + height = _fontHeight; } if (maxW != -1 && maxW < width) { @@ -177,6 +225,9 @@ void FontSJISBase::drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, } #ifndef DISABLE_FLIPPED_MODE + // TODO: This code inside flopCharater looks like it will only work with + // 16 pixel wide characters we should really take care that we only call + // it on these or we fix it to support a generic width. if (_flippedMode) glyphSource = flipCharacter(glyphSource, width); #endif @@ -220,14 +271,7 @@ void FontSJISBase::drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, } } -uint FontSJISBase::getCharWidth(uint16 ch) const { - if (is8x16(ch)) - return (_drawMode == kOutlineMode) ? 10 : (_drawMode == kDefaultMode ? 8 : 9); - else - return getMaxFontWidth(); -} - -bool FontSJISBase::is8x16(uint16 ch) const { +bool FontSJISBase::isASCII(uint16 ch) const { if (ch >= 0xFF) return false; else if (ch <= 0x7F || (ch >= 0xA1 && ch <= 0xDF)) @@ -253,105 +297,119 @@ bool FontTowns::loadData() { } const uint8 *FontTowns::getCharData(uint16 ch) const { - uint8 f = ch & 0xFF; - uint8 s = ch >> 8; - - // copied from scumm\charset.cpp - enum { - KANA = 0, - KANJI = 1, - EKANJI = 2 - }; + if (ch < kFont8x16Chars) { + return _fontData8x16 + ch * 16; + } else { + uint8 f = ch & 0xFF; + uint8 s = ch >> 8; + + // copied from scumm\charset.cpp + enum { + KANA = 0, + KANJI = 1, + EKANJI = 2 + }; + + int base = s - ((s + 1) % 32); + int c = 0, p = 0, chunk_f = 0, chunk = 0, cr = 0, kanjiType = KANA; + + if (f >= 0x81 && f <= 0x84) kanjiType = KANA; + if (f >= 0x88 && f <= 0x9f) kanjiType = KANJI; + if (f >= 0xe0 && f <= 0xea) kanjiType = EKANJI; + + if ((f > 0xe8 || (f == 0xe8 && base >= 0x9f)) || (f > 0x90 || (f == 0x90 && base >= 0x9f))) { + c = 48; //correction + p = -8; //correction + } - int base = s - ((s + 1) % 32); - int c = 0, p = 0, chunk_f = 0, chunk = 0, cr = 0, kanjiType = KANA; + if (kanjiType == KANA) {//Kana + chunk_f = (f - 0x81) * 2; + } else if (kanjiType == KANJI) {//Standard Kanji + p += f - 0x88; + chunk_f = c + 2 * p; + } else if (kanjiType == EKANJI) {//Enhanced Kanji + p += f - 0xe0; + chunk_f = c + 2 * p; + } - if (f >= 0x81 && f <= 0x84) kanjiType = KANA; - if (f >= 0x88 && f <= 0x9f) kanjiType = KANJI; - if (f >= 0xe0 && f <= 0xea) kanjiType = EKANJI; + // Base corrections + if (base == 0x7f && s == 0x7f) + base -= 0x20; + if (base == 0x9f && s == 0xbe) + base += 0x20; + if (base == 0xbf && s == 0xde) + base += 0x20; + //if (base == 0x7f && s == 0x9e) + // base += 0x20; + + switch (base) { + case 0x3f: + cr = 0; //3f + if (kanjiType == KANA) chunk = 1; + else if (kanjiType == KANJI) chunk = 31; + else if (kanjiType == EKANJI) chunk = 111; + break; + case 0x5f: + cr = 0; //5f + if (kanjiType == KANA) chunk = 17; + else if (kanjiType == KANJI) chunk = 47; + else if (kanjiType == EKANJI) chunk = 127; + break; + case 0x7f: + cr = -1; //80 + if (kanjiType == KANA) chunk = 9; + else if (kanjiType == KANJI) chunk = 63; + else if (kanjiType == EKANJI) chunk = 143; + break; + case 0x9f: + cr = 1; //9e + if (kanjiType == KANA) chunk = 2; + else if (kanjiType == KANJI) chunk = 32; + else if (kanjiType == EKANJI) chunk = 112; + break; + case 0xbf: + cr = 1; //be + if (kanjiType == KANA) chunk = 18; + else if (kanjiType == KANJI) chunk = 48; + else if (kanjiType == EKANJI) chunk = 128; + break; + case 0xdf: + cr = 1; //de + if (kanjiType == KANA) chunk = 10; + else if (kanjiType == KANJI) chunk = 64; + else if (kanjiType == EKANJI) chunk = 144; + break; + default: + debug(4, "Invalid Char! f %x s %x base %x c %d p %d", f, s, base, c, p); + } - if ((f > 0xe8 || (f == 0xe8 && base >= 0x9f)) || (f > 0x90 || (f == 0x90 && base >= 0x9f))) { - c = 48; //correction - p = -8; //correction + debug(6, "Kanji: %c%c f 0x%x s 0x%x base 0x%x c %d p %d chunk %d cr %d index %d", f, s, f, s, base, c, p, chunk, cr, ((chunk_f + chunk) * 32 + (s - base)) + cr); + const int chunkNum = (((chunk_f + chunk) * 32 + (s - base)) + cr); + if (chunkNum < 0 || chunkNum >= kFont16x16Chars) + return 0; + else + return _fontData16x16 + chunkNum * 32; } +} - if (kanjiType == KANA) {//Kana - chunk_f = (f - 0x81) * 2; - } else if (kanjiType == KANJI) {//Standard Kanji - p += f - 0x88; - chunk_f = c + 2 * p; - } else if (kanjiType == EKANJI) {//Enhanced Kanji - p += f - 0xe0; - chunk_f = c + 2 * p; - } +// ScummVM SJIS font - // Base corrections - if (base == 0x7f && s == 0x7f) - base -= 0x20; - if (base == 0x9f && s == 0xbe) - base += 0x20; - if (base == 0xbf && s == 0xde) - base += 0x20; - //if (base == 0x7f && s == 0x9e) - // base += 0x20; - - switch (base) { - case 0x3f: - cr = 0; //3f - if (kanjiType == KANA) chunk = 1; - else if (kanjiType == KANJI) chunk = 31; - else if (kanjiType == EKANJI) chunk = 111; - break; - case 0x5f: - cr = 0; //5f - if (kanjiType == KANA) chunk = 17; - else if (kanjiType == KANJI) chunk = 47; - else if (kanjiType == EKANJI) chunk = 127; - break; - case 0x7f: - cr = -1; //80 - if (kanjiType == KANA) chunk = 9; - else if (kanjiType == KANJI) chunk = 63; - else if (kanjiType == EKANJI) chunk = 143; - break; - case 0x9f: - cr = 1; //9e - if (kanjiType == KANA) chunk = 2; - else if (kanjiType == KANJI) chunk = 32; - else if (kanjiType == EKANJI) chunk = 112; - break; - case 0xbf: - cr = 1; //be - if (kanjiType == KANA) chunk = 18; - else if (kanjiType == KANJI) chunk = 48; - else if (kanjiType == EKANJI) chunk = 128; - break; - case 0xdf: - cr = 1; //de - if (kanjiType == KANA) chunk = 10; - else if (kanjiType == KANJI) chunk = 64; - else if (kanjiType == EKANJI) chunk = 144; - break; - default: - debug(4, "Invalid Char! f %x s %x base %x c %d p %d", f, s, base, c, p); - } +FontSjisSVM::FontSjisSVM(const Common::Platform platform) + : _fontData16x16(0), _fontData16x16Size(0), _fontData8x16(0), _fontData8x16Size(0), + _fontData12x12(0), _fontData12x12Size(0) { - debug(6, "Kanji: %c%c f 0x%x s 0x%x base 0x%x c %d p %d chunk %d cr %d index %d", f, s, f, s, base, c, p, chunk, cr, ((chunk_f + chunk) * 32 + (s - base)) + cr); - const int chunkNum = (((chunk_f + chunk) * 32 + (s - base)) + cr); - if (chunkNum < 0 || chunkNum >= kFont16x16Chars) - return 0; - else - return _fontData16x16 + chunkNum * 32; + if (platform == Common::kPlatformPCEngine) { + _fontWidth = 12; + _fontHeight = 12; + } } -const uint8 *FontTowns::getCharData8x16(uint16 c) const { - if (c >= kFont8x16Chars) - return 0; - return _fontData8x16 + c * 16; +FontSjisSVM::~FontSjisSVM() { + delete[] _fontData16x16; + delete[] _fontData8x16; + delete[] _fontData12x12; } -// ScummVM SJIS font - bool FontSjisSVM::loadData() { Common::SeekableReadStream *data = SearchMan.createReadStreamForMember("SJIS.FNT"); if (!data) @@ -366,23 +424,33 @@ bool FontSjisSVM::loadData() { } uint32 version = data->readUint32BE(); - if (version != 2) { + if (version != 3) { delete data; return false; } uint numChars16x16 = data->readUint16BE(); uint numChars8x16 = data->readUint16BE(); + uint numChars12x12 = data->readUint16BE(); + + if (_fontWidth == 16) { + _fontData16x16Size = numChars16x16 * 32; + _fontData16x16 = new uint8[_fontData16x16Size]; + assert(_fontData16x16); + data->read(_fontData16x16, _fontData16x16Size); + + _fontData8x16Size = numChars8x16 * 16; + _fontData8x16 = new uint8[numChars8x16 * 16]; + assert(_fontData8x16); + data->read(_fontData8x16, _fontData8x16Size); + } else { + data->skip(numChars16x16 * 32); + data->skip(numChars8x16 * 16); - _fontData16x16Size = numChars16x16 * 32; - _fontData16x16 = new uint8[_fontData16x16Size]; - assert(_fontData16x16); - data->read(_fontData16x16, _fontData16x16Size); - - _fontData8x16Size = numChars8x16 * 16; - _fontData8x16 = new uint8[numChars8x16 * 16]; - assert(_fontData8x16); - - data->read(_fontData8x16, _fontData8x16Size); + _fontData12x12Size = numChars12x12 * 24; + _fontData12x12 = new uint8[_fontData12x12Size]; + assert(_fontData12x12); + data->read(_fontData12x12, _fontData12x12Size); + } bool retValue = !data->err(); delete data; @@ -390,49 +458,76 @@ bool FontSjisSVM::loadData() { } const uint8 *FontSjisSVM::getCharData(uint16 c) const { - const uint8 fB = c & 0xFF; - const uint8 sB = c >> 8; + if (_fontWidth == 12) + return getCharDataPCE(c); + else + return getCharDataDefault(c); +} - // We only allow 2 byte SJIS characters. - if (fB <= 0x80 || fB >= 0xF0 || (fB >= 0xA0 && fB <= 0xDF) || sB == 0x7F) +const uint8 *FontSjisSVM::getCharDataPCE(uint16 c) const { + if (isASCII(c)) return 0; - int base = fB; - base -= 0x81; - if (base >= 0x5F) - base -= 0x40; + const uint8 fB = c & 0xFF; + const uint8 sB = c >> 8; - int index = sB; - index -= 0x40; - if (index >= 0x3F) - --index; + int base, index; + mapKANJIChar(fB, sB, base, index); - // Another check if the passed character was an - // correctly encoded SJIS character. - if (index < 0 || index >= 0xBC || base < 0) + if (base == -1) return 0; - const uint offset = (base * 0xBC + index) * 32; - assert(offset + 16 <= _fontData16x16Size); - return _fontData16x16 + offset; + const uint offset = (base * 0xBC + index) * 24; + assert(offset + 16 <= _fontData12x12Size); + return _fontData12x12 + offset; } -const uint8 *FontSjisSVM::getCharData8x16(uint16 c) const { +const uint8 *FontSjisSVM::getCharDataDefault(uint16 c) const { const uint8 fB = c & 0xFF; const uint8 sB = c >> 8; - if (!is8x16(c) || sB) - return 0; + if (isASCII(c)) { + int index = fB; + + // half-width katakana + if (fB >= 0xA1 && fB <= 0xDF) + index -= 0x21; - int index = fB; + const uint offset = index * 16; + assert(offset <= _fontData8x16Size); + return _fontData8x16 + offset; + } else { + int base, index; + mapKANJIChar(fB, sB, base, index); - // half-width katakana - if (fB >= 0xA1 && fB <= 0xDF) - index -= 0x21; + if (base == -1) + return 0; - const uint offset = index * 16; - assert(offset <= _fontData8x16Size); - return _fontData8x16 + offset; + const uint offset = (base * 0xBC + index) * 32; + assert(offset + 16 <= _fontData16x16Size); + return _fontData16x16 + offset; + } +} + +void FontSjisSVM::mapKANJIChar(const uint8 fB, const uint8 sB, int &base, int &index) const { + base = index = -1; + + // We only allow 2 byte SJIS characters. + if (fB <= 0x80 || fB >= 0xF0 || (fB >= 0xA0 && fB <= 0xDF) || sB == 0x7F) + return; + + base = fB - 0x81; + if (base >= 0x5F) + base -= 0x40; + + index = sB - 0x40; + if (index >= 0x3F) + --index; + + // Another check if the passed character was an + // correctly encoded SJIS character. + if (index < 0 || index >= 0xBC || base < 0) + base = index = -1; } } // End of namespace Graphics diff --git a/graphics/sjis.h b/graphics/sjis.h index 0c3b057cc4..62e68013da 100644 --- a/graphics/sjis.h +++ b/graphics/sjis.h @@ -123,30 +123,30 @@ public: * @param bpp bytes per pixel of the destination buffer * @param c1 forground color * @param c2 outline color - * @param maxW max draw width (to ensure that character drawing takes place within surface boundaries) - * @param maxH max draw height (to ensure that character drawing takes place within surface boundaries) + * @param maxW max draw width (to ensure that character drawing takes place within surface boundaries), -1 = no check + * @param maxH max draw height (to ensure that character drawing takes place within surface boundaries), -1 = no check */ - virtual void drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, uint32 c2, int maxW = -1, int maxH = -1) const = 0; + virtual void drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, uint32 c2, int maxW, int maxH) const = 0; }; /** - * A base class to render 16x16 (2 byte chars), 8x16 (1 byte chars) monochrome SJIS fonts. + * A base class to render monochrome SJIS fonts. */ class FontSJISBase : public FontSJIS { public: - FontSJISBase() : _drawMode(kDefaultMode), _flippedMode(false) {} + FontSJISBase(); - void setDrawingMode(DrawingMode mode) { _drawMode = mode; } + virtual void setDrawingMode(DrawingMode mode); - void toggleFlippedMode(bool enable) { _flippedMode = enable; } + virtual void toggleFlippedMode(bool enable); - uint getFontHeight() const { return (_drawMode == kOutlineMode) ? 18 : (_drawMode == kDefaultMode ? 16 : 17); } + virtual uint getFontHeight() const; - uint getMaxFontWidth() const { return (_drawMode == kOutlineMode) ? 18 : (_drawMode == kDefaultMode ? 16 : 17); } + virtual uint getMaxFontWidth() const; - uint getCharWidth(uint16 ch) const; + virtual uint getCharWidth(uint16 ch) const; - void drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, uint32 c2, int maxW = -1, int maxH = -1) const; + virtual void drawChar(void *dst, uint16 ch, int pitch, int bpp, uint32 c1, uint32 c2, int maxW, int maxH) const; private: template<typename Color> void blitCharacter(const uint8 *glyph, const int w, const int h, uint8 *dst, int pitch, Color c) const; @@ -161,11 +161,11 @@ private: protected: DrawingMode _drawMode; bool _flippedMode; + int _fontWidth, _fontHeight; - bool is8x16(uint16 ch) const; + bool isASCII(uint16 ch) const; virtual const uint8 *getCharData(uint16 c) const = 0; - virtual const uint8 *getCharData8x16(uint16 c) const = 0; }; /** @@ -188,8 +188,7 @@ private: uint8 _fontData16x16[kFont16x16Chars * 32]; uint8 _fontData8x16[kFont8x16Chars * 32]; - const uint8 *getCharData(uint16 c) const; - const uint8 *getCharData8x16(uint16 c) const; + virtual const uint8 *getCharData(uint16 c) const; }; /** @@ -197,8 +196,8 @@ private: */ class FontSjisSVM : public FontSJISBase { public: - FontSjisSVM() : _fontData16x16(0), _fontData16x16Size(0), _fontData8x16(0), _fontData8x16Size(0) {} - ~FontSjisSVM() { delete[] _fontData16x16; delete[] _fontData8x16; } + FontSjisSVM(const Common::Platform platform); + ~FontSjisSVM(); /** * Load the font data from "SJIS.FNT". @@ -211,8 +210,15 @@ private: uint8 *_fontData8x16; uint _fontData8x16Size; - const uint8 *getCharData(uint16 c) const; - const uint8 *getCharData8x16(uint16 c) const; + uint8 *_fontData12x12; + uint _fontData12x12Size; + + virtual const uint8 *getCharData(uint16 c) const; + + const uint8 *getCharDataPCE(uint16 c) const; + const uint8 *getCharDataDefault(uint16 c) const; + + void mapKANJIChar(const uint8 fB, const uint8 sB, int &base, int &index) const; }; // TODO: Consider adding support for PC98 ROM diff --git a/graphics/yuv_to_rgb.cpp b/graphics/yuv_to_rgb.cpp index 037ea9a007..bdc481016e 100644 --- a/graphics/yuv_to_rgb.cpp +++ b/graphics/yuv_to_rgb.cpp @@ -189,7 +189,9 @@ const YUVToRGBLookup *YUVToRGBManager::getLookup(Graphics::PixelFormat format) { } // End of namespace Graphics +namespace Common { DECLARE_SINGLETON(Graphics::YUVToRGBManager); +} #define YUVToRGBMan (Graphics::YUVToRGBManager::instance()) |
