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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/* 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/>.
*
*/
//#define TETRAEDGE_DUMP_RENDERED_FONTS
#ifdef TETRAEDGE_DUMP_RENDERED_FONTS
#include "image/png.h"
#endif
#include "tetraedge/te/te_text_base2.h"
namespace Tetraedge {
TeTextBase2::TeTextBase2() : _drawRect(0, 0), _size(0, 0),
_alignStyle(TeFont3::AlignLeft), _interLine(0.0f), _globalColor(0xff, 0xff, 0xff, 0xff),
_wrapMode(WrapModeFixed), _strikethrough(false), _fontSize(10), _valueWasSet(true) {
_mesh = TeMesh::makeInstance();
_mesh->setglTexEnvBlend();
_mesh->setShouldDraw(true);
}
TeTextBase2::~TeTextBase2() {
delete _mesh;
}
#ifdef TETRAEDGE_DUMP_RENDERED_FONTS
static int dumpCount = 0;
#endif
void TeTextBase2::build() {
if (!_text.size() || !_fontSize)
return;
TeIntrusivePtr<TeFont3> font = _fonts[0];
if (!font.get()) {
warning("[TeTextBase2::build()] Warning : font missing");
return;
}
_valueWasSet = false;
_size = TeVector2s32(0, 0);
_wrappedLines.clear();
Common::Array<uint32> endPts = _lineBreaks;
uint32 npos = Common::String::npos;
endPts.push_back(npos);
uint32 start = 0;
for (uint32 end : endPts) {
Common::Array<Common::String> lines;
Common::String txt = _text.substr(start, end - start);
if (txt.find(' ') != Common::String::npos)
font->wordWrapText(txt, _fontSize, _drawRect._x, lines);
else
lines.push_back(txt);
_wrappedLines.push_back(lines);
start = end;
}
Common::Array<float> lineoffsets;
float lineHeight = font->getHeight(_fontSize);
float height = 0;
for (const Common::String &line : _wrappedLines) {
if (_alignStyle == TeFont3::AlignJustify) {
warning("TODO: Implement TeTextBase2::computeNbSpaces for Justify");
//computeNbSpaces(&line, offset, line.endOffset);
}
Common::Rect lineSize = font->getBoundingBox(line, _fontSize);
if (lineSize.right > _size._x)
_size._x = lineSize.right;
lineoffsets.push_back(height);
height += lineHeight + _interLine;
}
// Round up to the nearest 2 pixels so it centres in the
// area without a half pixel offset.
_size._y = (((int)ceilf(height) + 1) / 2) * 2;
_size._x = ((_size._x + 1) / 2) * 2;
TeImage img;
Common::SharedPtr<TePalette> nullpal;
img.createImg(_size._x, _size._y, nullpal, TeImage::RGBA8);
img.fill(_globalColor.r(), _globalColor.g(), _globalColor.b(), 0);
for (uint i = 0; i < _wrappedLines.size(); i++) {
drawLine(img, _wrappedLines[i], lineoffsets[i]);
}
TeIntrusivePtr<Te3DTexture> texture = Te3DTexture::makeInstance();
texture->load(img);
#ifdef TETRAEDGE_DUMP_RENDERED_FONTS
Common::DumpFile dumpFile;
dumpFile.open(Common::String::format("/tmp/rendered-font-dump-%04d.png", dumpCount));
dumpCount++;
Image::writePNG(dumpFile, img);
#endif
_mesh->setConf(4, 4, TeMesh::MeshMode_TriangleStrip, 0, 0);
_mesh->defaultMaterial(texture);
_mesh->setglTexEnvBlend();
_mesh->setShouldDraw(true);
_mesh->setColor(_globalColor);
_mesh->setVertex(0, TeVector3f32(_size._x * -0.5f, _size._y * -0.5f, 0.0f));
_mesh->setTextureUV(0, TeVector2f32(0, 1));
_mesh->setNormal(0, TeVector3f32(0.0f, 0.0f, 1.0f));
_mesh->setColor(0, _globalColor);
_mesh->setVertex(1, TeVector3f32(_size._x * 0.5f, _size._y * -0.5f, 0.0f));
_mesh->setTextureUV(1, TeVector2f32(1, 1));
_mesh->setNormal(1, TeVector3f32(0.0f, 0.0f, 1.0f));
_mesh->setColor(1, _globalColor);
_mesh->setVertex(2, TeVector3f32(_size._x * 0.5f, _size._y * 0.5f, 0.0f));
_mesh->setTextureUV(2, TeVector2f32(1, 0));
_mesh->setNormal(2, TeVector3f32(0.0f, 0.0f, 1.0f));
_mesh->setColor(2, _globalColor);
_mesh->setVertex(3, TeVector3f32(_size._x * -0.5f, _size._y * 0.5f, 0.0f));
_mesh->setTextureUV(3, TeVector2f32(0, 0));
_mesh->setNormal(3, TeVector3f32(0.0f, 0.0f, 1.0f));
_mesh->setColor(3, _globalColor);
_mesh->setIndex(0, 0);
_mesh->setIndex(1, 1);
_mesh->setIndex(2, 3);
_mesh->setIndex(3, 2);
_mesh->setHasAlpha(true);
}
void TeTextBase2::clear() {
clearText();
clearStyles();
_valueWasSet = true;
}
void TeTextBase2::clearStyles() {
_lineBreaks.clear();
_fonts.clear();
_colors.clear();
_valueWasSet = true;
}
void TeTextBase2::clearText() {
_text.clear();
_valueWasSet = true;
}
void TeTextBase2::computeNbSpaces(Line &line, uint startOffset, uint endOffset) {
// only needed if we implement Justify
error("TODO: Implement TeTextBase2::computeNbSpaces");
}
TeColor TeTextBase2::currentColor(uint offset) const {
if (_colors.size() == 0)
return _globalColor;
int closest_off = -1;
TeColor result;
// Find closest without going over.
for (auto &pair : _colors) {
if ((int)pair._key > closest_off && pair._key <= offset) {
result = pair._value;
closest_off = pair._key;
}
}
if (closest_off == -1)
return _globalColor;
return result;
}
TeIntrusivePtr<TeFont3> TeTextBase2::currentFont(uint offset) {
if (_fonts.size() == 0)
return TeIntrusivePtr<TeFont3>();
int closest_off = -1;
TeIntrusivePtr<TeFont3> result;
// Find closest without going over.
for (auto &pair : _fonts) {
if ((int)pair._key > closest_off && pair._key <= offset) {
result = pair._value;
closest_off = pair._key;
}
}
if (closest_off == -1)
return TeIntrusivePtr<TeFont3>();
return result;
}
void TeTextBase2::draw() {
if (_text.empty() || (_drawRect._x <= 0 && _wrapMode == WrapModeFixed))
return;
if (_valueWasSet)
build();
_mesh->draw();
//if (_strikethrough)
// warning("TODO: Implement TeTextBase2::draw strikethrough support");
}
void TeTextBase2::drawEmptyChar(uint offset) {
error("TODO: Implement TeTextBase2::drawEmptychar");
}
void TeTextBase2::drawLine(TeImage &img, const Common::String &str, int yoffset) {
TeIntrusivePtr<TeFont3> font = _fonts[0];
// Note: We draw this with black because the global color will be applied on
// the mesh.
font->draw(img, str, _fontSize, yoffset, TeColor(0, 0, 0, 255), _alignStyle);
}
uint TeTextBase2::endOfWord(uint offset) const {
while (offset < _text.size() && !newLines(offset) && !isASpace(offset))
offset++;
return offset;
}
void TeTextBase2::insertNewLine(uint offset) {
_lineBreaks.push_back(offset);
}
bool TeTextBase2::isASpace(uint offset) const {
char c = _text[offset];
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
}
int TeTextBase2::newLines(uint offset) const {
int result = 0;
for (uint off : _lineBreaks) {
if (off == offset)
result++;
}
return result;
}
int TeTextBase2::nextNonSpaceChar(uint offset) {
while (isASpace(offset))
offset++;
return offset; // TODO: or offset - 1?
}
void TeTextBase2::setAlignStyle(TeFont3::AlignStyle style) {
_alignStyle = style;
_valueWasSet = true;
}
void TeTextBase2::setColor(uint offset, const TeColor &color) {
_colors.setVal(offset, color);
_valueWasSet = true;
}
void TeTextBase2::setFont(uint offset, const TeIntrusivePtr<TeFont3> &newfont) {
_fonts.setVal(offset, newfont);
_valueWasSet = true;
}
void TeTextBase2::setFontSize(unsigned long newSize) {
if (_fontSize != newSize) {
_fontSize = newSize;
_valueWasSet = true;
}
}
void TeTextBase2::setGlobalColor(const TeColor &color) {
_globalColor = color;
_valueWasSet = true;
}
void TeTextBase2::setInterLine(float val) {
_interLine = val;
_valueWasSet = true;
}
void TeTextBase2::setRect(const TeVector2s32 &rect) {
_drawRect = rect;
_valueWasSet = true;
}
void TeTextBase2::setText(const Common::String &newText) {
_valueWasSet = true;
_text = newText;
//int len = newText.size();
//_mesh.setConf(len * 4, len * 6, TeMesh::MeshMode_Triangles, 1, len * 2);
}
void TeTextBase2::setWrapMode(TeTextBase2::WrapMode &mode) {
_wrapMode = mode;
_valueWasSet = true;
}
TeVector2s32 TeTextBase2::size() {
if (_valueWasSet)
build();
return _size;
}
void TeTextBase2::strikethrough(bool val) {
if (_strikethrough != val) {
_strikethrough = val;
_valueWasSet = true;
}
if (val) {
warning("TODO: Implement TeTextBase2::draw strikethrough support");
}
}
} // end namespace Tetraedge
|