1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "illusions/illusions.h"
24 #include "illusions/resources/fontresource.h"
25 #include "illusions/dictionary.h"
26 
27 namespace Illusions {
28 
29 // FontResourceLoader
30 
load(Resource * resource)31 void FontResourceLoader::load(Resource *resource) {
32 	FontInstance *fontInstance = new FontInstance(_vm);
33 	fontInstance->load(resource);
34 	resource->_instance = fontInstance;
35 }
36 
isFlag(int flag)37 bool FontResourceLoader::isFlag(int flag) {
38 	return
39 		flag == kRlfLoadFile;
40 }
41 
42 // CharInfo
43 
load(byte * dataStart,Common::SeekableReadStream & stream)44 void CharInfo::load(byte *dataStart, Common::SeekableReadStream &stream) {
45 	_width = stream.readUint16LE();
46 	_field_2 = stream.readUint16LE();
47 	uint32 pixelsOffs = stream.readUint32LE();
48 	_pixels = dataStart + pixelsOffs;
49 	debug(2, "CharInfo::load() _width: %d; _field_2: %d; pixelsOffs: %08X",
50 		_width, _field_2, pixelsOffs);
51 }
52 
53 // CharRange
54 
load(byte * dataStart,Common::SeekableReadStream & stream)55 void CharRange::load(byte *dataStart, Common::SeekableReadStream &stream) {
56 	_firstChar = stream.readUint16LE();
57 	_lastChar = stream.readUint16LE();
58 	uint count = _lastChar - _firstChar + 1;
59 	uint32 charInfosOffs = stream.readUint32LE();
60 	_charInfos = new CharInfo[count];
61 	for (uint i = 0; i < count; ++i) {
62 		stream.seek(charInfosOffs + i * 8);
63 		_charInfos[i].load(dataStart, stream);
64 	}
65 	debug(2, "CharRange::load() _firstChar: %d; _lastChar: %d; charInfosOffs: %08X",
66 		_firstChar, _lastChar, charInfosOffs);
67 }
68 
getCharInfo(uint16 c)69 CharInfo *CharRange::getCharInfo(uint16 c) {
70 	return &_charInfos[c - _firstChar];
71 }
72 
containsChar(uint16 c)73 bool CharRange::containsChar(uint16 c) {
74 	return c >= _firstChar && c <= _lastChar;
75 }
76 
77 // FontResource
78 
FontResource()79 FontResource::FontResource() {
80 }
81 
~FontResource()82 FontResource::~FontResource() {
83 }
84 
load(Resource * resource)85 void FontResource::load(Resource *resource) {
86 	byte *data = resource->_data;
87 	uint32 dataSize = resource->_dataSize;
88 	Common::MemoryReadStream stream(data, dataSize, DisposeAfterUse::NO);
89 	_totalSize = stream.readUint32LE();
90 	_charHeight = stream.readUint16LE();
91 	_defaultChar = stream.readUint16LE();
92 	_colorIndex = stream.readUint16LE();
93 	_lineIncr = stream.readUint16LE();
94 	_widthC = stream.readUint16LE();
95 	_charRangesCount = stream.readUint16LE();
96 	uint32 charRangesOffs = stream.pos();
97 	_charRanges = new CharRange[_charRangesCount];
98 	for (uint i = 0; i < _charRangesCount; ++i) {
99 		stream.seek(charRangesOffs + i * 8);
100 		_charRanges[i].load(data, stream);
101 	}
102 	debug(2, "FontResource::load() _charHeight: %d; _defaultChar: %d; _colorIndex: %d; _lineIncr: %d; _widthC: %d; _charRangesCount: %d",
103 		_charHeight, _defaultChar, _colorIndex, _lineIncr, _widthC, _charRangesCount);
104 }
105 
getCharInfo(uint16 c)106 CharInfo *FontResource::getCharInfo(uint16 c) {
107 	for (uint i = 0; i < _charRangesCount; ++i) {
108 		if (_charRanges[i].containsChar(c))
109 			return _charRanges[i].getCharInfo(c);
110 	}
111 	return 0;
112 }
113 
calculateRectForText(uint16 * text,uint textLength)114 const Common::Rect FontResource::calculateRectForText(uint16 *text, uint textLength) {
115 	int16 width = 0;
116 	for (uint i = 0; i < textLength && *text; i++) {
117 		width += getCharInfo(*text)->_width;
118 		text++;
119 	}
120 	return Common::Rect(width, getCharHeight() + getLineIncr());
121 }
122 
123 // FontInstance
124 
FontInstance(IllusionsEngine * vm)125 FontInstance::FontInstance(IllusionsEngine *vm) : _vm(vm) {
126 }
127 
load(Resource * resource)128 void FontInstance::load(Resource *resource) {
129 	_fontResource = new FontResource();
130 	_fontResource->load(resource);
131 	_resId = resource->_resId;
132 	_vm->_dict->addFont(resource->_resId, _fontResource);
133 }
134 
unload()135 void FontInstance::unload() {
136 	delete _fontResource;
137 	_vm->_dict->removeFont(_resId);
138 }
139 
140 } // End of namespace Illusions
141