File: big5.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (134 lines) | stat: -rw-r--r-- 4,533 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "graphics/big5.h"

namespace Graphics {

Big5Font::Big5Font() : _chineseTraditionalHeight(0) {
}

Big5Font::~Big5Font() {
	_chineseTraditionalFont.clear();
	_chineseTraditionalIndex.clear();
}

void Big5Font::ChineseTraditionalGlyph::makeOutline(int height) {
	outline[0][0] = 0;
	outline[0][1] = 0;
	// OR into outline the original bitmap moved by 1 pixel
	// 1 pixel down
	for (int y = 0; y < height - 1; y++) {
		outline[y+1][0] = bitmap[y][0];
		outline[y+1][1] = bitmap[y][1];
	}
	// 1 pixel up
	for (int y = 0; y < height - 1; y++) {
		outline[y][0] |= bitmap[y+1][0];
		outline[y][1] |= bitmap[y+1][1];
	}
	for (int y = 0; y < height; y++) {
		// 1 pixel right
		outline[y][0] |= bitmap[y][0] >> 1;
		outline[y][1] |= bitmap[y][0] << 7;
		outline[y][1] |= bitmap[y][1] >> 1;

		// 1 pixel left
		outline[y][0] |= bitmap[y][0] << 1;
		outline[y][0] |= bitmap[y][1] >> 7;
		outline[y][1] |= bitmap[y][1] << 1;
	}

	// Then AND-out the original bitmap
	for (int y = 0; y < height; y++) {
		outline[y][0] &= ~bitmap[y][0];
		outline[y][1] &= ~bitmap[y][1];
	}
}

void Big5Font::loadPrefixedRaw(Common::ReadStream &input, int height) {
	_chineseTraditionalHeight = height;
	_chineseTraditionalFont.clear();

	_chineseTraditionalIndex = Common::move(Common::Array<int>(0x8000, -1));
	// So far the smallest version had 1981 glyphs. Optimize a little bit for this number
	// but don't rely on it in any way
	_chineseTraditionalFont.reserve(1981);
	while(!input.eos()) {
		// Big-endian because it's not really a u16 but a big5 sequence.
		uint16 ch = input.readUint16BE();
		ChineseTraditionalGlyph glyph;
		if (input.eos())
			break;
		if (ch == 0xffff)
			break;
		if (!(ch & 0x8000))
			continue;
		memset(&glyph.bitmap, 0, sizeof(glyph.bitmap));
		memset(&glyph.outline, 0, sizeof(glyph.outline));
		input.read(&glyph.bitmap, (kChineseTraditionalWidth / 8) * _chineseTraditionalHeight);
		glyph.makeOutline(height);
		_chineseTraditionalIndex[ch & 0x7fff] = _chineseTraditionalFont.size();
		_chineseTraditionalFont.push_back(glyph);
	}
}

template <class T> bool Big5Font::drawReal(byte *dest, uint16 textChar, int maxX, int maxY, uint32 destPitch, byte color, byte outlineColor, bool outline) const {
	int glyphIdx = _chineseTraditionalIndex[textChar & 0x7fff];
	if (glyphIdx < 0) {
		return false;
	}

	const ChineseTraditionalGlyph& glyph = _chineseTraditionalFont[glyphIdx];

	for (int y = 0; y < _chineseTraditionalHeight && y < maxY; y++) {
		T *cur = (T*) (dest + y * destPitch);
		T *curMax = cur + maxX;

		for (int byte = 0; byte < 2; byte++)
			for (int bit = 0; bit < 8 && cur < curMax; bit++, cur++)
				if ((glyph.bitmap[y][byte] << bit) & 0x80)
					*cur = color;
				else if (outline && (((glyph.outline[y][byte] << bit) & 0x80)))
					*cur = outlineColor;
	}
	return true;
}

bool Big5Font::drawBig5Char(byte *dest, uint16 ch, int maxX, int maxY, uint32 destPitch, byte color, byte outlineColor, bool outline, int bpp) const {
	switch(bpp) {
	case 4:
		return drawReal<uint32>(dest, ch, maxX, maxY, destPitch, color, outlineColor, outline);
	case 2:
		return drawReal<uint16>(dest, ch, maxX, maxY, destPitch, color, outlineColor, outline);
	case 1:
		return drawReal<uint8>(dest, ch, maxX, maxY, destPitch, color, outlineColor, outline);
	default:
		error("Big5 font for bpp=%d is not supported", bpp);
	}

}

bool Big5Font::drawBig5Char(Graphics::Surface *surf, uint16 ch, const Common::Point &pt, uint32 color, byte outlineColor, bool outline) const {
	return drawBig5Char((byte*)surf->getBasePtr(pt.x, pt.y), ch, surf->w - pt.x, surf->h - pt.y, surf->pitch, color, outlineColor, outline, surf->format.bytesPerPixel);
}

}