/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "scumm.h"
#include "nut_renderer.h"

NutRenderer::NutRenderer(Scumm *vm) {
	_vm = vm;
	_initialized = false;
	_loaded = false;
	_dataSrc = NULL;
}

NutRenderer::~NutRenderer() {
	if (_dataSrc != NULL)
		free(_dataSrc);
}

void NutRenderer::decodeCodec44(byte *dst, byte *src, uint32 length) {
	byte val;
	uint16 size_line;
	uint16 num;

	do {
		size_line = READ_LE_UINT16(src);
		src += 2;
		length -= 2;

		while (size_line != 0) {
			num = *src++;
			val = *src++;
			memset(dst, val, num);
			dst += num;
			length -= 2;
			size_line -= 2;
			if (size_line == 0) break;

			num = READ_LE_UINT16(src) + 1;
			src += 2;
			memcpy(dst, src, num);
			dst += num;
			src += num;
			length -= num + 2;
			size_line -= num + 2;

		}
		dst--;

	} while (length > 1);
}

bool NutRenderer::loadFont(const char *filename, const char *dir) {
	debug(2,  "NutRenderer::loadFont() called");
	if (_loaded == true) {
		debug(2, "NutRenderer::loadFont() Font already loaded, ok, loading...");
	}
	
	File file;
	file.open(filename, dir);
	if (file.isOpen() == false) {
		error("NutRenderer::loadFont() Can't open font file: %s/%s", dir, filename);
		return false;
	}

	uint32 tag = file.readUint32BE();
	if (tag != 'ANIM') {
		debug(2, "NutRenderer::loadFont() there is no ANIM chunk in font header");
		return false;
	}

	if (_dataSrc != NULL)
		free(_dataSrc);

	uint32 length = file.readUint32BE();
	_dataSrc = (byte *)malloc(length);
	file.read(_dataSrc, length);
	file.close();

	if (READ_BE_UINT32(_dataSrc) != 'AHDR') {
		debug(2, "NutRenderer::loadFont() there is no AHDR chunk in font header");
		free(_dataSrc);
		return false;
	}
	
	int32 l;
	uint32 offset = READ_BE_UINT32(_dataSrc + 4) + 8;
	for (l = 0; l < 256; l++) {
		if (READ_BE_UINT32(_dataSrc + offset) == 'FRME') {
			offset += 8;
			if (READ_BE_UINT32(_dataSrc + offset) == 'FOBJ') {
				_offsets[l] = offset + 8;
				offset += READ_BE_UINT32(_dataSrc + offset + 4) + 8;
			}
			else {
				debug(2, "NutRenderer::loadFont() there is no FRME chunk");
				free(_dataSrc);
				return false;
			}
		}
		else {
			debug(2, "NutRenderer::loadFont() there is no FOBJ chunk in FRME chunk");
			free(_dataSrc);
			return false;
		}
	}

	_loaded = true;
	return true;
}

int32 NutRenderer::getCharWidth(char c) {
	debug(2,  "NutRenderer::getCharWidth() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::getCharWidth() Font is not loaded");
		return 0;
	}

	return READ_LE_UINT16(_dataSrc + _offsets[c] + 6);
}

int32 NutRenderer::getCharHeight(char c) {
	debug(2,  "NutRenderer::getCharHeight() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::getCharHeight() Font is not loaded");
		return 0;
	}

	return READ_LE_UINT16(_dataSrc + _offsets[c] + 8);
}

int32 NutRenderer::getStringWidth(char *string) {
	debug(2,  "NutRenderer::getStringWidth() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::getStringWidth() Font is not loaded");
		return 0;
	}
	int32 length = 0;
	int32 l = 0;
	
	do {
		length += getCharWidth(string[l]);
		l++;
	} while (string[l] != 0);

	return length;
}

void NutRenderer::drawString(const char *string, int32 x, int32 y, byte color, int32 mode) {
	debug(2,  "NutRenderer::drawString() called");
	if (_loaded == false) {
		debug(2, "NutRenderer::drawString() Font is not loaded");
		return;
	}

	int l = 0;
	int left = x;
	int height = 0, tmp;
	do {
		if ((x < 0) || (y < 0) || (x > _vm->_realWidth) || (y > _vm->_realHeight)) {
			debug(2, "NutRenderer::drawString() position x, y out of range");
			return;
		}

		drawChar(string[l], x, y, color);
		x += getCharWidth(string[l]);
		tmp = getCharHeight(string[l]);
		if (height < tmp)
			height = tmp;
		l++;
	} while (string[l] != 0);

	_vm->updateDirtyRect(0, left, x, y, y + height, 0);
}

void NutRenderer::drawChar(char c, int32 x, int32 y, byte color) {
	debug(2,  "NutRenderer::drawChar('%c', %d, %d, %d) called", c, x, y, (int)color);
	if (_loaded == false) {
		debug(2, "NutRenderer::drawChar() Font is not loaded");
		return;
	}

	const uint32 length = READ_BE_UINT32(_dataSrc + _offsets[c] - 4) - 14;
	const int32 width = READ_LE_UINT16(_dataSrc + _offsets[c] + 6);
	const int32 height = READ_LE_UINT16(_dataSrc + _offsets[c] + 8);

	byte *src = (byte*)(_dataSrc + _offsets[c] + 14);
	decodeCodec44(_tmpCodecBuffer, src, length);
	
	// HACK: we draw the character a total of 7 times: 6 times shifted
	// and in black for the shadow, and once in the right color and position.
	// This way we achieve the exact look as the original CMI had. However,
	// the question remains whether they did it this way, too, or if there is
	// some "font shadow" resource we don't know yet.
	// One problem remains: the fonts on the save/load screen don't have a
	// shadow. So how do we know whether to draw text with or without shadow?
	
	int offsetX[7] = { -1,  0, 1, 0, 1, 2, 0 };
	int offsetY[7] = {  0, -1, 0, 1, 2, 1, 0 };
	int cTable[7] =  {  0,  0, 0, 0, 0, 0, color };
	
	for (int i = 0; i < 7; i++) {
		x += offsetX[i];
		y += offsetY[i];
		color = cTable[i];
	
		byte *dst = _vm->virtscr[0].screenPtr + y * _vm->_realWidth + x + _vm->virtscr[0].xstart;
		byte *mask = _vm->getResourceAddress(rtBuffer, 9)
						+ (y * _vm->_realWidth + x) / 8 + _vm->_screenStartStrip;
		byte maskmask;
		int maskpos;
	
		src = _tmpCodecBuffer;
	
		for (int32 ty = 0; ty < height; ty++) {
			maskmask = revBitMask[x & 7];
			maskpos = 0;
			for (int32 tx = 0; tx < width; tx++) {
				byte pixel = *src++;
#if 1
				if (pixel != 0) {
					dst[tx] = color;
					mask[maskpos] |= maskmask;
				}
#else
				if (pixel != 0) {
					if (pixel == 0x01)
						pixel = (color == 0) ? 0xf : color;
					if (pixel == 0xff)
						pixel = 0x0;
					dst[tx] = pixel;
					mask[maskpos] |= maskmask;
				}
#endif
				maskmask >>= 1;
				if (maskmask == 0) {
					maskmask = 0x80;
					maskpos++;
				}
			}
			dst += _vm->_realWidth;
			mask += _vm->gdi._numStrips;
		}
	
		x -= offsetX[i];
		y -= offsetY[i];
	}
}