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 "ags/shared/ac/common.h" // our_eip
24 #include "ags/shared/core/asset_manager.h"
25 #include "ags/shared/debugging/out.h"
26 #include "ags/shared/font/wfn_font.h"
27 #include "ags/shared/font/wfn_font_renderer.h"
28 #include "ags/shared/gfx/bitmap.h"
29 #include "ags/shared/util/stream.h"
30 #include "ags/globals.h"
31
32 namespace AGS3 {
33
34 using namespace AGS::Shared;
35
GetCharCode(unsigned char wanted_code,const WFNFont * font)36 static unsigned char GetCharCode(unsigned char wanted_code, const WFNFont *font) {
37 return wanted_code < font->GetCharCount() ? wanted_code : '?';
38 }
39
40 static int RenderChar(Bitmap *ds, const int at_x, const int at_y, const WFNChar &wfn_char, const int scale, const color_t text_color);
41
AdjustYCoordinateForFont(int * ycoord,int fontNumber)42 void WFNFontRenderer::AdjustYCoordinateForFont(int *ycoord, int fontNumber) {
43 // Do nothing
44 }
45
EnsureTextValidForFont(char * text,int fontNumber)46 void WFNFontRenderer::EnsureTextValidForFont(char *text, int fontNumber) {
47 const WFNFont *font = _fontData[fontNumber].Font;
48 // replace any extended characters with question marks
49 for (; *text; ++text) {
50 if ((unsigned char)*text >= font->GetCharCount()) {
51 *text = '?';
52 }
53 }
54 }
55
GetTextWidth(const char * text,int fontNumber)56 int WFNFontRenderer::GetTextWidth(const char *text, int fontNumber) {
57 const WFNFont *font = _fontData[fontNumber].Font;
58 const FontRenderParams ¶ms = _fontData[fontNumber].Params;
59 int text_width = 0;
60
61 for (; *text; ++text) {
62 const WFNChar &wfn_char = font->GetChar(GetCharCode(*text, font));
63 text_width += wfn_char.Width;
64 }
65 return text_width * params.SizeMultiplier;
66 }
67
GetTextHeight(const char * text,int fontNumber)68 int WFNFontRenderer::GetTextHeight(const char *text, int fontNumber) {
69 const WFNFont *font = _fontData[fontNumber].Font;
70 const FontRenderParams ¶ms = _fontData[fontNumber].Params;
71 int max_height = 0;
72
73 for (; *text; ++text) {
74 const WFNChar &wfn_char = font->GetChar(GetCharCode(*text, font));
75 const uint16_t height = wfn_char.Height;
76 if (height > max_height)
77 max_height = height;
78 }
79 return max_height * params.SizeMultiplier;
80 }
81
82 Bitmap render_wrapper;
RenderText(const char * text,int fontNumber,BITMAP * destination,int x,int y,int colour)83 void WFNFontRenderer::RenderText(const char *text, int fontNumber, BITMAP *destination, int x, int y, int colour) {
84 int oldeip = get_our_eip();
85 set_our_eip(415);
86
87 const WFNFont *font = _fontData[fontNumber].Font;
88 const FontRenderParams ¶ms = _fontData[fontNumber].Params;
89 render_wrapper.WrapAllegroBitmap(destination, true);
90
91 for (; *text; ++text)
92 x += RenderChar(&render_wrapper, x, y, font->GetChar(GetCharCode(*text, font)), params.SizeMultiplier, colour);
93
94 set_our_eip(oldeip);
95 }
96
RenderChar(Bitmap * ds,const int at_x,const int at_y,const WFNChar & wfn_char,const int scale,const color_t text_color)97 int RenderChar(Bitmap *ds, const int at_x, const int at_y, const WFNChar &wfn_char, const int scale, const color_t text_color) {
98 const int width = wfn_char.Width;
99 const int height = wfn_char.Height;
100 const unsigned char *actdata = wfn_char.Data;
101 const int bytewid = wfn_char.GetRowByteCount();
102
103 int x = at_x;
104 int y = at_y;
105 for (int h = 0; h < height; ++h) {
106 for (int w = 0; w < width; ++w) {
107 if (((actdata[h * bytewid + (w / 8)] & (0x80 >> (w % 8))) != 0)) {
108 if (scale > 1) {
109 ds->FillRect(Rect(x + w, y + h, x + w + (scale - 1),
110 y + h + (scale - 1)), text_color);
111 } else {
112 ds->PutPixel(x + w, y + h, text_color);
113 }
114 }
115
116 x += scale - 1;
117 }
118 y += scale - 1;
119 x = at_x;
120 }
121 return width * scale;
122 }
123
LoadFromDisk(int fontNumber,int fontSize)124 bool WFNFontRenderer::LoadFromDisk(int fontNumber, int fontSize) {
125 return LoadFromDiskEx(fontNumber, fontSize, nullptr);
126 }
127
IsBitmapFont()128 bool WFNFontRenderer::IsBitmapFont() {
129 return true;
130 }
131
LoadFromDiskEx(int fontNumber,int fontSize,const FontRenderParams * params)132 bool WFNFontRenderer::LoadFromDiskEx(int fontNumber, int fontSize, const FontRenderParams *params) {
133 String file_name;
134 Stream *ffi = nullptr;
135 soff_t asset_size = 0;
136
137 file_name.Format("agsfnt%d.wfn", fontNumber);
138 ffi = _GP(AssetMgr)->OpenAsset(file_name, &asset_size);
139 if (ffi == nullptr) {
140 // actual font not found, try font 0 instead
141 file_name = "agsfnt0.wfn";
142 ffi = _GP(AssetMgr)->OpenAsset(file_name, &asset_size);
143 if (ffi == nullptr)
144 return false;
145 }
146
147 WFNFont *font = new WFNFont();
148 WFNError err = font->ReadFromFile(ffi, asset_size);
149 delete ffi;
150 if (err == kWFNErr_HasBadCharacters)
151 Debug::Printf(kDbgMsg_Warn, "WARNING: font '%s' has mistakes in data format, some characters may be displayed incorrectly", file_name.GetCStr());
152 else if (err != kWFNErr_NoError) {
153 delete font;
154 return false;
155 }
156 _fontData[fontNumber].Font = font;
157 _fontData[fontNumber].Params = params ? *params : FontRenderParams();
158 return true;
159 }
160
FreeMemory(int fontNumber)161 void WFNFontRenderer::FreeMemory(int fontNumber) {
162 delete _fontData[fontNumber].Font;
163 _fontData.erase(fontNumber);
164 }
165
SupportsExtendedCharacters(int fontNumber)166 bool WFNFontRenderer::SupportsExtendedCharacters(int fontNumber) {
167 return _fontData[fontNumber].Font->GetCharCount() > 128;
168 }
169
170 } // namespace AGS3
171