2016-10-06 21:23:46 +02:00
/* 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 "bladerunner/font.h"
# include "bladerunner/bladerunner.h"
# include "common/debug.h"
namespace BladeRunner {
2019-07-14 22:02:48 +02:00
Font : : Font ( ) {
2016-10-06 21:23:46 +02:00
reset ( ) ;
}
Font : : ~ Font ( ) {
close ( ) ;
}
2019-07-14 22:02:48 +02:00
Font * Font : : load ( BladeRunnerEngine * vm , const Common : : String & fileName , int spacing , bool useFontColor ) {
Font * font = new Font ( ) ;
font - > _spacing = spacing ;
font - > _useFontColor = useFontColor ;
2016-10-06 21:23:46 +02:00
2019-07-14 22:02:48 +02:00
Common : : ScopedPtr < Common : : SeekableReadStream > stream ( vm - > getResourceStream ( fileName ) ) ;
2016-10-06 21:23:46 +02:00
if ( ! stream ) {
2019-02-07 23:54:47 +01:00
warning ( " Font::open failed to open '%s' " , fileName . c_str ( ) ) ;
2019-07-14 22:02:48 +02:00
delete font ;
return nullptr ;
2016-10-06 21:23:46 +02:00
}
2019-07-14 22:02:48 +02:00
font - > _characterCount = stream - > readUint32LE ( ) ;
font - > _maxWidth = stream - > readUint32LE ( ) ;
font - > _maxHeight = stream - > readUint32LE ( ) ;
font - > _dataSize = stream - > readUint32LE ( ) ;
font - > _data = new uint16 [ font - > _dataSize ] ;
if ( ! font - > _data ) {
2019-02-07 23:54:47 +01:00
warning ( " Font::open failed to allocate font buffer " ) ;
2019-07-14 22:02:48 +02:00
delete font ;
return nullptr ;
2016-10-06 21:23:46 +02:00
}
2019-07-14 22:02:48 +02:00
font - > _characters . resize ( font - > _characterCount ) ;
2020-02-24 21:23:13 +02:00
for ( uint32 i = 0 ; i < font - > _characterCount ; + + i ) {
2019-07-14 22:02:48 +02:00
font - > _characters [ i ] . x = stream - > readUint32LE ( ) ;
font - > _characters [ i ] . y = stream - > readUint32LE ( ) ;
font - > _characters [ i ] . width = stream - > readUint32LE ( ) ;
font - > _characters [ i ] . height = stream - > readUint32LE ( ) ;
font - > _characters [ i ] . dataOffset = stream - > readUint32LE ( ) ;
2016-10-06 22:32:27 +02:00
}
2019-04-13 01:03:48 +02:00
2020-02-24 21:23:13 +02:00
for ( int i = 0 ; i < font - > _dataSize ; + + i ) {
2019-07-14 22:02:48 +02:00
font - > _data [ i ] = stream - > readUint16LE ( ) ;
2016-10-06 22:32:27 +02:00
}
2019-04-13 01:03:48 +02:00
2019-07-14 22:02:48 +02:00
return font ;
2016-10-06 21:23:46 +02:00
}
void Font : : close ( ) {
if ( _data ! = nullptr ) {
delete [ ] _data ;
}
reset ( ) ;
}
void Font : : reset ( ) {
_maxWidth = 0 ;
_maxHeight = 0 ;
_characterCount = 0 ;
_data = nullptr ;
_dataSize = 0 ;
_screenWidth = 0 ;
_screenHeight = 0 ;
2019-07-14 22:02:48 +02:00
_spacing = 0 ;
_useFontColor = false ;
2016-10-06 21:23:46 +02:00
2019-07-14 22:02:48 +02:00
_characters . clear ( ) ;
}
int Font : : getFontHeight ( ) const {
return _maxHeight ;
}
int Font : : getMaxCharWidth ( ) const {
return _maxWidth ;
}
int Font : : getCharWidth ( uint32 chr ) const {
if ( chr > = _characterCount ) {
return 0 ;
}
return _characters [ chr + 1 ] . width + _spacing ;
2016-10-06 21:23:46 +02:00
}
2019-07-14 22:02:48 +02:00
void Font : : drawChar ( Graphics : : Surface * dst , uint32 chr , int x , int y , uint32 color ) const {
uint32 characterIndex = chr + 1 ;
if ( x < 0 | | x > = dst - > w | | y < 0 | | y > = dst - > h | | ! _data | | characterIndex > = _characterCount ) {
2016-10-06 21:23:46 +02:00
return ;
}
2018-01-14 12:12:06 +01:00
uint16 * srcPtr = & _data [ _characters [ characterIndex ] . dataOffset ] ;
int width = _characters [ characterIndex ] . width ;
int height = _characters [ characterIndex ] . height ;
2016-10-06 21:23:46 +02:00
int endY = height + y - 1 ;
int currentY = y ;
2018-07-02 10:45:17 +03:00
// FIXME/TODO
// This width and height check were added as a temporary bug fix -- a sanity check which is only needed for the internal TAHOMA18.FON font.
// That font's glyph properties table is corrupted - the start of the file states that there are 0xF7 (=247) entries in the char properties table
// but that table get corrupted past the 176th entry. The image data glyph part of the FON file also only covers the 176 entries.
// So the following if clause-check will return here if the width and height values are unnaturally big.
// The bug only affects debug cases where all character glyph need to be displayed...
2018-12-16 19:45:19 +02:00
// ...or potential custom dialogue / translations that reference characters that are not within the range of ASCII values for the normal Latin characters.
2018-07-02 10:45:17 +03:00
if ( width > 100 | | height > 100 ) {
return ;
}
2019-07-14 22:02:48 +02:00
while ( currentY < = endY & & currentY < dst - > h ) {
2016-10-06 21:23:46 +02:00
int currentX = x ;
int endX = width + x - 1 ;
2019-07-14 22:02:48 +02:00
while ( currentX < = endX & & currentX < dst - > w ) {
2019-04-13 01:03:48 +02:00
uint8 a , r , g , b ;
2019-09-04 17:22:35 +02:00
getGameDataColor ( * srcPtr , a , r , g , b ) ;
2019-07-14 22:02:48 +02:00
if ( ! a ) { // Alpha is inversed
2019-08-31 23:06:30 +02:00
uint32 outColor = color ;
2019-07-14 22:02:48 +02:00
if ( _useFontColor ) {
2019-06-24 21:43:43 +02:00
// Ignore the alpha in the output as it is inversed in the input
2019-08-31 23:06:30 +02:00
outColor = dst - > format . RGBToColor ( r , g , b ) ;
2019-04-13 01:03:48 +02:00
}
2019-08-31 23:06:30 +02:00
void * dstPtr = dst - > getBasePtr ( CLIP ( currentX + _characters [ characterIndex ] . x , 0 , dst - > w - 1 ) , CLIP ( currentY + _characters [ characterIndex ] . y , 0 , dst - > h - 1 ) ) ;
drawPixel ( * dst , dstPtr , outColor ) ;
2016-10-06 21:23:46 +02:00
}
2020-02-24 21:23:13 +02:00
+ + srcPtr ;
+ + currentX ;
2016-10-06 21:23:46 +02:00
}
2020-02-24 21:23:13 +02:00
+ + currentY ;
2016-10-06 21:23:46 +02:00
}
}
2018-01-14 12:12:06 +01:00
2016-10-06 21:23:46 +02:00
} // End of namespace BladeRunner