File: font.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (580 lines) | stat: -rw-r--r-- 19,071 bytes parent folder | download | duplicates (2)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* 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/>.
 *
 */

#include "graphics/font.h"
#include "graphics/managed_surface.h"

#include "common/array.h"
#include "common/util.h"

namespace Graphics {

int Font::getFontAscent() const {
	return -1;
}

int Font::getFontDescent() const {
	return -1;
}

int Font::getFontLeading() const {
	return -1;
}

int Font::getKerningOffset(uint32 left, uint32 right) const {
	return 0;
}

Common::Rect Font::getBoundingBox(uint32 chr) const {
	return Common::Rect(getCharWidth(chr), getFontHeight());
}

namespace {

template<class StringType>
Common::Rect getBoundingBoxImpl(const Font &font, const StringType &str, int x, int y, int w, TextAlign align, int deltax) {
	// We follow the logic of drawStringImpl here. The only exception is
	// that we do allow an empty width to be specified here. This allows us
	// to obtain the complete bounding box of a string.
	const int leftX = x, rightX = w ? (x + w + 1) : 0x7FFFFFFF;
	int width = font.getStringWidth(str);

	if (align == kTextAlignCenter)
		x = x + (w - width)/2;
	else if (align == kTextAlignRight)
		x = x + w - width;
	x += deltax;

	bool first = true;
	Common::Rect bbox;

	typename StringType::unsigned_type last = 0;
	for (typename StringType::const_iterator i = str.begin(), end = str.end(); i != end; ++i) {
		const typename StringType::unsigned_type cur = *i;
		x += font.getKerningOffset(last, cur);
		last = cur;

		Common::Rect charBox = font.getBoundingBox(cur);
		if (x + charBox.right > rightX)
			break;
		if (x + charBox.right >= leftX) {
			charBox.translate(x, y);
			if (first) {
				bbox = charBox;
				first = false;
			} else {
				bbox.extend(charBox);
			}
		}

		x += font.getCharWidth(cur);
	}

	return bbox;
}

template<class StringType>
int getStringWidthImpl(const Font &font, const StringType &str) {
	int space = 0;
	typename StringType::unsigned_type last = 0;

	for (uint i = 0; i < str.size(); ++i) {
		const typename StringType::unsigned_type cur = str[i];
		space += font.getCharWidth(cur) + font.getKerningOffset(last, cur);
		last = cur;
	}

	return space;
}

template<class SurfaceType, class StringType>
void drawStringImpl(const Font &font, SurfaceType *dst, const StringType &str, int x, int y, int w, uint32 color, TextAlign align, int deltax) {
	// The logic in getBoundingImpl is the same as we use here. In case we
	// ever change something here we will need to change it there too.
	assert(dst != 0);

	const int leftX = x, rightX = x + w + 1;
	int width = font.getStringWidth(str);

	if (align == kTextAlignCenter)
		x = x + (w - width)/2;
	else if (align == kTextAlignRight)
		x = x + w - width;
	x += deltax;

	typename StringType::unsigned_type last = 0;
	for (typename StringType::const_iterator i = str.begin(), end = str.end(); i != end; ++i) {
		const typename StringType::unsigned_type cur = *i;
		x += font.getKerningOffset(last, cur);
		last = cur;

		Common::Rect charBox = font.getBoundingBox(cur);
		if (x + charBox.right > rightX)
			break;
		if (x + charBox.right >= leftX)
			font.drawChar(dst, cur, x, y, color);

		x += font.getCharWidth(cur);
	}
}

template<class StringType>
struct WordWrapper {
	Common::Array<StringType> &lines;
	Common::Array<bool> &lineContinuation;
	int actualMaxLineWidth;

	WordWrapper(Common::Array<StringType> &l, Common::Array<bool> &lC) : lines(l), lineContinuation(lC), actualMaxLineWidth(0) {
	}

	void add(StringType &line, bool &wordContinuation, int &w) {
		if (actualMaxLineWidth < w)
			actualMaxLineWidth = w;

		lines.push_back(line);
		lineContinuation.push_back(wordContinuation);

		line.clear();
		wordContinuation = false;
		w = 0;
	}

	void clear() {
		lines.clear();
		lineContinuation.clear();
		actualMaxLineWidth = 0;
	}
};

template<class StringType>
int wordWrapTextImpl(const Font &font, const StringType &str, int maxWidth, Common::Array<StringType> &lines, Common::Array<bool> &lineContinuation, int initWidth, uint32 mode) {
	WordWrapper<StringType> wrapper(lines, lineContinuation);
	StringType line;
	StringType tmpStr;
	bool wordContinuation = false;
	int lineWidth = initWidth;
	int tmpWidth = 0;
	int fullTextWidthEWL = initWidth; // this replaces new line characters (if any) with single spaces - it is used in Even Width Lines mode

	// The rough idea behind this algorithm is as follows:
	// We accumulate characters into the string tmpStr. Whenever a full word
	// has been gathered together this way, we 'commit' it to the line buffer
	// 'line', i.e. we add tmpStr to the end of line, then clear it. Before
	// we do that, we check whether it would cause 'line' to exceed maxWidth;
	// in that case, we first add line to lines, then reset it.
	//
	// If a newline character is read, then we also add line to lines and clear it.
	//
	// Special care has to be taken to account for 'words' that exceed the width
	// of a line. If we encounter such a word, we have to wrap it over multiple
	// lines.

	typename StringType::unsigned_type last = 0;

	// When EvenWidthLines mode is enabled then we require an early loop over the entire string
	// in order to get the full width of the text
	//
	// If both "Wrap On Explicit New Lines" and "Even Width Lines" modes are set,
	// and there are new line characters in the text,
	// then "Wrap On Explicit New Lines" takes precedence and "Even Width Lines" is ignored for that text.
	// However, if both are set, but there are no new lines in the text,
	// then the "Even Width Lines" auto-wrapping is applied.
	//
	if (mode & kWordWrapEvenWidthLines) {
		// Early loop to get the full width of the text
		for (typename StringType::const_iterator x = str.begin(); x != str.end(); ++x) {
			typename StringType::unsigned_type c = *x;

			// Check for Windows and Mac line breaks
			if (c == '\r') {
				if (x != str.end() && *(x + 1) == '\n') {
					++x;
				}
				c = '\n';
			}

			if (c == '\n') {
				if (!(mode & kWordWrapOnExplicitNewLines)) {
					c = ' ';
				} else {
					mode &= ~kWordWrapEvenWidthLines;
					break;
				}
			}

			const int w = font.getCharWidth(c) + font.getKerningOffset(last, c);
			last = c;
			fullTextWidthEWL += w;
		}
	}

	int targetTotalLinesNumberEWL = 0;
	int targetMaxLineWidth = 0;
	do {
		if (mode & kWordWrapEvenWidthLines) {
			wrapper.clear();
			targetTotalLinesNumberEWL += 1;
			// We add +2 to the fullTextWidthEWL to account for possible shadow pixels
			// We add +10 * font.getCharWidth(' ') to the quotient since we want to allow some extra margin (about an extra wprd's length)
			// since that yields better looking results
			targetMaxLineWidth = ((fullTextWidthEWL + 2) / targetTotalLinesNumberEWL) + 10 * font.getCharWidth(' ');
			if (targetMaxLineWidth > maxWidth) {
				// repeat the loop with increased targetTotalLinesNumberEWL
				continue;
			}
		} else {
			targetMaxLineWidth = maxWidth;
		}

		last = 0;
		tmpWidth = 0;

		for (typename StringType::const_iterator x = str.begin(); x != str.end(); ++x) {
			typename StringType::unsigned_type c = *x;

			// Convert Windows and Mac line breaks into plain \n
			if (c == '\r') {
				if (x != str.end() && *(x + 1) == '\n') {
					++x;
				}
				c = '\n';
			}
			// if wrapping on explicit new lines is disabled, then new line characters should be treated as a single white space char
			if (!(mode & kWordWrapOnExplicitNewLines) && c == '\n')  {
				c = ' ';
			}

			const int currentCharWidth = font.getCharWidth(c);
			const int w = currentCharWidth + font.getKerningOffset(last, c);
			last = c;
			const bool wouldExceedWidth =
				(lineWidth + tmpWidth + w > targetMaxLineWidth) &&
				!(mode & kWordWrapAllowTrailingWhitespace && Common::isSpace(c));

			// If this char is a whitespace, then it represents a potential
			// 'wrap point' where wrapping could take place. Everything that
			// came before it can now safely be added to the line, as we know
			// that it will not have to be wrapped.
			if (Common::isSpace(c)) {
				line += tmpStr;
				lineWidth += tmpWidth;

				tmpStr.clear();
				tmpWidth = 0;

				// If we encounter a line break (\n), or if the new space would
				// cause the line to overflow: start a new line
				if (((mode & kWordWrapOnExplicitNewLines) && c == '\n') || wouldExceedWidth) {
					wrapper.add(line, wordContinuation, lineWidth);
					continue;
				}
			}

			// If the max line width would be exceeded by adding this char,
			// insert a line break.
			if (wouldExceedWidth) {
				// Commit what we have so far, *if* we have anything.
				// If line is empty, then we are looking at a word
				// which exceeds the maximum line width.
				if (lineWidth > 0) {
					wrapper.add(line, wordContinuation, lineWidth);
					// Trim left side
					while (tmpStr.size() && Common::isSpace(tmpStr[0])) {
						tmpStr.deleteChar(0);
						// This is not very fast, but it is the simplest way to
						// assure we do not mess something up because of kerning.
						tmpWidth = font.getStringWidth(tmpStr);
					}

					if (tmpStr.empty()) {
						// If tmpStr is empty, we might have removed the space before 'c'.
						// That means we have to recompute the kerning.

						tmpWidth += currentCharWidth + font.getKerningOffset(0, c);
						tmpStr += c;
						continue;
					}
				} else {
					wordContinuation = true;
					wrapper.add(tmpStr, wordContinuation, tmpWidth);
				}
			}

			tmpWidth += w;
			tmpStr += c;
		}

		// If some text is left over, add it as the final line
		line += tmpStr;
		lineWidth += tmpWidth;
		if (lineWidth > 0) {
			wrapper.add(line, wordContinuation, lineWidth);
		}
	} while ((mode & kWordWrapEvenWidthLines)
	         && (targetMaxLineWidth > maxWidth));
	return wrapper.actualMaxLineWidth;
}

template<typename StringType>
StringType handleEllipsis(const Font &font, const StringType &input, int w) {
	StringType s = input;
	int width = font.getStringWidth(s);
	bool hasEllipsisAtEnd = false;

	if (s.size() > 3 && s[s.size() - 1] == '.' && s[s.size() - 2] == '.' && s[s.size() - 3] == '.') {
		hasEllipsisAtEnd = true;
	}

	if (width > w && hasEllipsisAtEnd) {
		// String is too wide. Check whether it ends in an ellipsis
		// ("..."). If so, remove that and try again!
		s.deleteLastChar();
		s.deleteLastChar();
		s.deleteLastChar();
		width = font.getStringWidth(s);
	}

	if (width > w) {
		StringType str;
		StringType ellipsis("...");

		// String is too wide. So we shorten it "intelligently" by
		// replacing parts of the string by an ellipsis. There are
		// three possibilities for this: replace the start, the end, or
		// the middle of the string. What is best really depends on the
		// context; but unless we want to make this configurable,
		// replacing the middle seems to be a good compromise.

		const int ellipsisWidth = font.getStringWidth(ellipsis);

		// SLOW algorithm to remove enough of the middle. But it is good enough
		// for now.
		const int halfWidth = (w - ellipsisWidth) / 2;
		int w2 = 0;
		typename StringType::unsigned_type last = 0;
		uint i = 0;

		for (; i < s.size(); ++i) {
			const typename StringType::unsigned_type cur = s[i];
			int charWidth = font.getCharWidth(cur) + font.getKerningOffset(last, cur);
			if (w2 + charWidth > halfWidth)
				break;
			last = cur;
			w2 += charWidth;
			str += cur;
		}

		// At this point we know that the first 'i' chars are together 'w2'
		// pixels wide. We took the first i-1, and add "..." to them.
		str += ellipsis;
		last = '.';

		// The original string is width wide. Of those we already skipped past
		// w2 pixels, which means (width - w2) remain.
		// The new str is (w2+ellipsisWidth) wide, so we can accommodate about
		// (w - (w2+ellipsisWidth)) more pixels.
		// Thus we skip ((width - w2) - (w - (w2+ellipsisWidth))) =
		// (width + ellipsisWidth - w)
		int skip = width + ellipsisWidth - w;
		for (; i < s.size() && skip > 0; ++i) {
			const typename StringType::unsigned_type cur = s[i];
			skip -= font.getCharWidth(cur) + font.getKerningOffset(last, cur);
			last = cur;
		}

		// Append the remaining chars, if any
		for (; i < s.size(); ++i) {
			str += s[i];
		}

		return str;
	}

	return s;
}

} // End of anonymous namespace

Common::Rect Font::getBoundingBox(const Common::String &input, int x, int y, const int w, TextAlign align, int deltax, bool useEllipsis) const {
	// In case no width was given we cannot use ellipsis or any alignment
	// apart from left alignment.
	if (w == 0) {
		if (useEllipsis) {
			warning("Font::getBoundingBox: Requested ellipsis when no width was specified");
		}

		if (align != kTextAlignLeft) {
			warning("Font::getBoundingBox: Requested text alignment when no width was specified");
		}

		useEllipsis = false;
		align = kTextAlignLeft;
	}

	const Common::String str = useEllipsis ? handleEllipsis(*this, input, w) : input;
	return getBoundingBoxImpl(*this, str, x, y, w, align, deltax);
}

Common::Rect Font::getBoundingBox(const Common::U32String &input, int x, int y, const int w, TextAlign align, int deltax, bool useEllipsis) const {
	// In case no width was given we cannot any alignment apart from left
	// alignment.
	if (w == 0) {
		if (useEllipsis) {
			warning("Font::getBoundingBox: Requested ellipsis when no width was specified");
		}

		if (align != kTextAlignLeft) {
			warning("Font::getBoundingBox: Requested text alignment when no width was specified");
		}

		useEllipsis = false;
		align = kTextAlignLeft;
	}

	const Common::U32String str = useEllipsis ? handleEllipsis(*this, input, w) : input;
	return getBoundingBoxImpl(*this, str, x, y, w, align, 0);
}

int Font::getStringWidth(const Common::String &str) const {
	return getStringWidthImpl(*this, str);
}

int Font::getStringWidth(const Common::U32String &str) const {
	return getStringWidthImpl(*this, str);
}

void Font::drawChar(ManagedSurface *dst, uint32 chr, int x, int y, uint32 color) const {
	drawChar(dst->surfacePtr(), chr, x, y, color);

	Common::Rect charBox = getBoundingBox(chr);
	charBox.translate(x, y);
	dst->addDirtyRect(charBox);
}

void Font::drawString(Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
	Common::String renderStr = useEllipsis ? handleEllipsis(*this, str, w) : str;
	drawStringImpl(*this, dst, renderStr, x, y, w, color, align, deltax);
}

void Font::drawString(Surface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
	Common::U32String renderStr = useEllipsis ? handleEllipsis(*this, str, w) : str;
	drawStringImpl(*this, dst, renderStr, x, y, w, color, align, deltax);
}

void Font::drawString(ManagedSurface *dst, const Common::String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
	Common::String renderStr = useEllipsis ? handleEllipsis(*this, str, w) : str;
	drawStringImpl(*this, dst, renderStr, x, y, w, color, align, deltax);

	if (w != 0) {
		dst->addDirtyRect(getBoundingBox(str, x, y, w, align, deltax, useEllipsis));
	}
}

void Font::drawString(ManagedSurface *dst, const Common::U32String &str, int x, int y, int w, uint32 color, TextAlign align, int deltax, bool useEllipsis) const {
	Common::U32String renderStr = useEllipsis ? handleEllipsis(*this, str, w) : str;
	drawStringImpl(*this, dst, renderStr, x, y, w, color, align, deltax);

	if (w != 0) {
		dst->addDirtyRect(getBoundingBox(str, x, y, w, align, useEllipsis));
	}
}

int Font::wordWrapText(const Common::String &str, int maxWidth, Common::Array<Common::String> &lines, int initWidth, uint32 mode) const {
	Common::Array<bool> dummyLineContinuation;
	return wordWrapTextImpl(*this, str, maxWidth, lines, dummyLineContinuation, initWidth, mode);
}

int Font::wordWrapText(const Common::U32String &str, int maxWidth, Common::Array<Common::U32String> &lines, int initWidth, uint32 mode) const {
	Common::Array<bool> dummyLineContinuation;
	return wordWrapTextImpl(*this, str, maxWidth, lines, dummyLineContinuation, initWidth, mode);
}

int Font::wordWrapText(const Common::U32String &str, int maxWidth, Common::Array<Common::U32String> &lines, Common::Array<bool> &lineContinuation, int initWidth, uint32 mode) const {
	return wordWrapTextImpl(*this, str, maxWidth, lines, lineContinuation, initWidth, mode);
}

TextAlign convertTextAlignH(TextAlign alignH, bool rtl) {
	switch (alignH) {
	case kTextAlignStart:
		return rtl ? kTextAlignRight : kTextAlignLeft;
	case kTextAlignEnd:
		return rtl ? kTextAlignLeft : kTextAlignRight;
	default:
		return alignH;
	}
}

#define wholedivide(x, y)	(((x)+((y)-1))/(y))

static void countupScore(int *dstGray, int x, int y, int bbw, int bbh, float scale) {
	int newbbw = bbw * scale;
	int newbbh = bbh * scale;
	int x_ = x * newbbw;
	int y_ = y * newbbh;
	int x1 = x_ + newbbw;
	int y1 = y_ + newbbh;

	int newxbegin = x_ / bbw;
	int newybegin = y_ / bbh;
	int newxend = wholedivide(x1, bbw);
	int newyend = wholedivide(y1, bbh);

	for (int newy = newybegin; newy < newyend; newy++) {
		for (int newx = newxbegin; newx < newxend; newx++) {
			int newX = newx * bbw;
			int newY = newy * bbh;
			int newX1 = newX + bbw;
			int newY1 = newY + bbh;
			dstGray[newy * newbbw + newx] += (MIN(x1, newX1) - MAX(x_, newX)) *
											 (MIN(y1, newY1) - MAX(y_, newY));
		}
	}
}

static void magnifyGray(Surface *src, int *dstGray, int width, int height, float scale) {
	for (uint16 y = 0; y < height; y++) {
		for (uint16 x = 0; x < width; x++) {
			if (*((byte *)src->getBasePtr(x, y)) == 1)
				countupScore(dstGray, x, y, width, height, scale);
		}
	}
}

void Font::scaleSingleGlyph(Surface *scaleSurface, int *grayScaleMap, int grayScaleMapSize, int width, int height, int xOffset, int yOffset, int grayLevel, int chr, int srcheight, int srcwidth, float scale) const {

	scaleSurface->fillRect(Common::Rect(scaleSurface->w, scaleSurface->h), 0);
	drawChar(scaleSurface, chr, xOffset, yOffset, 1);
	memset(grayScaleMap, 0, grayScaleMapSize * sizeof(int));
	magnifyGray(scaleSurface, grayScaleMap, srcwidth, srcheight, scale);
	int *grayPtr = grayScaleMap;
	for (int y = 0; y < height; y++) {
		byte *dst = (byte *)scaleSurface->getBasePtr(0, y);
		for (int x = 0; x < width; x++, grayPtr++, dst++) {
			if (*grayPtr > grayLevel)
				*dst = 1;
			else
				*dst = 0;
		}
	}

}

} // End of namespace Graphics