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 "glk/frotz/bitmap_font.h"
24
25 namespace Glk {
26 namespace Frotz {
27
BitmapFont(const Graphics::Surface & src,const Common::Point & size,uint srcWidth,uint srcHeight,unsigned char startingChar,bool isFixedWidth)28 BitmapFont::BitmapFont(const Graphics::Surface &src, const Common::Point &size,
29 uint srcWidth, uint srcHeight, unsigned char startingChar, bool isFixedWidth) :
30 _startingChar(startingChar), _size(size) {
31 assert(src.format.bytesPerPixel == 1);
32 assert((src.w % srcWidth) == 0);
33 assert((src.h % srcHeight) == 0);
34
35 // Set up a characters array
36 _chars.resize((src.w / srcWidth) * (src.h / srcHeight));
37
38 // Iterate through loading characters
39 Common::Rect r(srcWidth, srcHeight);
40 int charsPerRow = src.w / srcWidth;
41 for (uint idx = 0; idx < _chars.size(); ++idx) {
42 r.moveTo((idx % charsPerRow) * srcWidth, (idx / charsPerRow) * srcHeight);
43 int srcCharWidth = isFixedWidth ? r.width() : getSourceCharacterWidth(idx, src, r);
44 int destCharWidth = (size.x * srcCharWidth + (srcWidth - 1)) / srcWidth;
45 Common::Rect charBounds(r.left, r.top, r.left + srcCharWidth, r.bottom);
46
47 _chars[idx].create(destCharWidth, size.y, src.format);
48 _chars[idx].transBlitFrom(src, charBounds, Common::Rect(0, 0, _chars[idx].w, _chars[idx].h));
49 }
50 }
51
drawChar(Graphics::Surface * dst,uint32 chr,int x,int y,uint32 color) const52 void BitmapFont::drawChar(Graphics::Surface *dst, uint32 chr, int x, int y, uint32 color) const {
53 const Graphics::ManagedSurface &c = _chars[chr - _startingChar];
54 for (int yCtr = 0; yCtr < c.h; ++yCtr) {
55 const byte *srcP = (const byte *)c.getBasePtr(0, yCtr);
56
57 for (int xCtr = 0; xCtr < c.w; ++xCtr, ++srcP) {
58 if (!*srcP)
59 dst->hLine(x + xCtr, y + yCtr, x + xCtr, color);
60 }
61 }
62 }
63
getSourceCharacterWidth(uint charIndex,const Graphics::Surface & src,const Common::Rect & charBounds)64 int BitmapFont::getSourceCharacterWidth(uint charIndex, const Graphics::Surface &src,
65 const Common::Rect &charBounds) {
66 if (charIndex == 0)
67 // The space character is treated as half the width of bounding area
68 return charBounds.width() / 2;
69
70 // Scan through the rows to find the right most pixel, getting the width from that
71 int maxWidth = 0, rowX;
72 for (int y = charBounds.top; y < charBounds.bottom; ++y) {
73 rowX = 0;
74 const byte *srcP = (const byte *)src.getBasePtr(charBounds.left, y);
75
76 for (int x = 0; x < charBounds.width(); ++x, ++srcP) {
77 if (!*srcP)
78 rowX = x;
79 }
80
81 maxWidth = MAX(maxWidth, MIN(rowX + 2, (int)charBounds.width()));
82 }
83
84 return maxWidth;
85 }
86
87 } // End of namespace Frotz
88 } // End of namespace Glk
89