summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/text.cpp
blob: 02cc3c5903061ec9aae7184305f05d175bb81c41 (plain) (blame)
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
581
582
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, Aaron Giles, Nathan Woods
/*********************************************************************

    text.cpp

    Text functionality for MAME's crude user interface

*********************************************************************/

#include "text.h"
#include "rendfont.h"
#include "render.h"

namespace ui {
/***************************************************************************
INLINE FUNCTIONS
***************************************************************************/

//-------------------------------------------------
//  is_space_character
//-------------------------------------------------

inline bool is_space_character(unicode_char ch)
{
	return ch == ' ';
}


//-------------------------------------------------
//  is_breakable_char - is a given unicode
//  character a possible line break?
//-------------------------------------------------

inline bool is_breakable_char(unicode_char ch)
{
	// regular spaces and hyphens are breakable
	if (is_space_character(ch) || ch == '-')
		return true;

	// In the following character sets, any character is breakable:
	//  Hiragana (3040-309F)
	//  Katakana (30A0-30FF)
	//  Bopomofo (3100-312F)
	//  Hangul Compatibility Jamo (3130-318F)
	//  Kanbun (3190-319F)
	//  Bopomofo Extended (31A0-31BF)
	//  CJK Strokes (31C0-31EF)
	//  Katakana Phonetic Extensions (31F0-31FF)
	//  Enclosed CJK Letters and Months (3200-32FF)
	//  CJK Compatibility (3300-33FF)
	//  CJK Unified Ideographs Extension A (3400-4DBF)
	//  Yijing Hexagram Symbols (4DC0-4DFF)
	//  CJK Unified Ideographs (4E00-9FFF)
	if (ch >= 0x3040 && ch <= 0x9fff)
		return true;

	// Hangul Syllables (AC00-D7AF) are breakable
	if (ch >= 0xac00 && ch <= 0xd7af)
		return true;

	// CJK Compatibility Ideographs (F900-FAFF) are breakable
	if (ch >= 0xf900 && ch <= 0xfaff)
		return true;

	return false;
}



/***************************************************************************
CORE IMPLEMENTATION
***************************************************************************/

//-------------------------------------------------
//  ctor
//-------------------------------------------------

text_layout::text_layout(render_font &font, float xscale, float yscale, float width, text_layout::text_justify justify, text_layout::word_wrapping wrap)
	: m_font(font), m_xscale(xscale), m_yscale(yscale), m_width(width), m_justify(justify), m_wrap(wrap), m_current_line(nullptr), m_last_break(0), m_text_position(0), m_truncating(false)

{
	invalidate_calculated_actual_width();
}


//-------------------------------------------------
//  ctor (move)
//-------------------------------------------------

text_layout::text_layout(text_layout &&that)
	: m_font(that.m_font), m_xscale(that.m_xscale), m_yscale(that.m_yscale), m_width(that.m_width), m_calculated_actual_width(that.m_calculated_actual_width), m_justify(that.m_justify), m_wrap(that.m_wrap), m_lines(std::move(that.m_lines)),
		m_current_line(that.m_current_line), m_last_break(that.m_last_break), m_text_position(that.m_text_position), m_truncating(false)
{
	that.invalidate_calculated_actual_width();
}


//-------------------------------------------------
//  dtor
//-------------------------------------------------

text_layout::~text_layout()
{
}


//-------------------------------------------------
//  add_text
//-------------------------------------------------

void text_layout::add_text(const char *text, const char_style &style)
{
	int position = 0;
	int text_length = strlen(text);

	while(position < text_length)
	{
		// adding a character - we might change the width
		invalidate_calculated_actual_width();

		// do we need to create a new line?
		if (m_current_line == nullptr)
		{
			// get the current character
			unicode_char schar;
			int scharcount;
			scharcount = uchar_from_utf8(&schar, &text[position], text_length - position);
			if (scharcount == -1)
				break;

			// if the line starts with a tab character, center it regardless
			text_justify line_justify = justify();
			if (schar == '\t')
			{
				position += scharcount;
				line_justify = text_layout::CENTER;
			}

			// start a new line
			start_new_line(line_justify, style.size);
		}

		// get the current character
		int scharcount;
		unicode_char ch;
		scharcount = uchar_from_utf8(&ch, &text[position], text_length - position);
		if (scharcount < 0)
			break;
		position += scharcount;

		// set up source information
		source_info source = { 0, };
		source.start = m_text_position;
		source.span = scharcount;
		m_text_position += scharcount;

		// is this an endline?
		if (ch == '\n')
		{
			// first, start a line if we have not already
			if (m_current_line == nullptr)
				start_new_line(LEFT, style.size);

			// and then close up the current line
			m_current_line = nullptr;
		}
		else if (!m_truncating)
		{
			// if we hit a space, remember the location and width *without* the space
			if (is_space_character(ch))
				m_last_break = m_current_line->character_count();

			// append the character
			m_current_line->add_character(ch, style, source);

			// do we have to wrap?
			if (wrap() != NEVER && m_current_line->width() > m_width)
			{
				switch (wrap())
				{
					case TRUNCATE:
						truncate_wrap();
						break;

					case WORD:
						word_wrap();
						break;

					default:
						fatalerror("invalid word wrapping value");
						break;
				}
			}
			else
			{
				// we didn't wrap - if we hit any non-space breakable character, remember the location and width
				// *with* the breakable character
				if (ch != ' ' && is_breakable_char(ch))
					m_last_break = m_current_line->character_count();
			}
		}
	}
}


//-------------------------------------------------
//  invalidate_calculated_actual_width
//-------------------------------------------------

void text_layout::invalidate_calculated_actual_width()
{
	m_calculated_actual_width = -1;
}


//-------------------------------------------------
//  actual_left
//-------------------------------------------------

float text_layout::actual_left() const
{
	float result;
	if (empty())
	{
		// degenerate scenario
		result = 0;
	}
	else
	{
		result = 1.0f;
		for (const auto &line : m_lines)
		{
			result = std::min(result, line->xoffset());

			// take an opportunity to break out easily
			if (result <= 0)
				break;
		}
	}
	return result;
}


//-------------------------------------------------
//  actual_width
//-------------------------------------------------

float text_layout::actual_width() const
{
	// do we need to calculate the width?
	if (m_calculated_actual_width < 0)
	{
		// calculate the actual width
		m_calculated_actual_width = 0;
		for (const auto &line : m_lines)
			m_calculated_actual_width = std::max(m_calculated_actual_width, line->width());

	}

	// return it
	return m_calculated_actual_width;
}


//-------------------------------------------------
//  actual_height
//-------------------------------------------------

float text_layout::actual_height() const
{
	line *last_line = (m_lines.size() > 0)
		? m_lines[m_lines.size() - 1].get()
		: nullptr;
	return last_line
		? last_line->yoffset() + last_line->height()
		: 0;
}


//-------------------------------------------------
//  start_new_line
//-------------------------------------------------

void text_layout::start_new_line(text_layout::text_justify justify, float height)
{
	// create a new line
	std::unique_ptr<line> new_line(global_alloc_clear<line>(*this, justify, actual_height(), height * yscale()));

	// update the current line
	m_current_line = new_line.get();
	m_last_break = 0;
	m_truncating = false;

	// append it
	m_lines.push_back(std::move(new_line));
}


//-------------------------------------------------
//  get_char_width
//-------------------------------------------------

float text_layout::get_char_width(unicode_char ch, float size)
{
	return font().char_width(size * yscale(), xscale() / yscale(), ch);
}


//-------------------------------------------------
//  truncate_wrap
//-------------------------------------------------

void text_layout::truncate_wrap()
{
	const unicode_char elipsis = 0x2026;

	// for now, lets assume that we're only truncating the last character
	size_t truncate_position = m_current_line->character_count() - 1;
	const auto& truncate_char = m_current_line->character(truncate_position);

	// copy style information
	char_style style = truncate_char.style;

	// copy source information
	source_info source = { 0, };
	source.start = truncate_char.source.start + truncate_char.source.span;
	source.span = 0;

	// figure out how wide an elipsis is
	float elipsis_width = get_char_width(elipsis, style.size);

	// where should we really truncate from?
	while (truncate_position > 0 && m_current_line->character(truncate_position).xoffset + elipsis_width > width())
		truncate_position--;

	// truncate!!!
	m_current_line->truncate(truncate_position);

	// and append the elipsis
	m_current_line->add_character(elipsis, style, source);

	// take note that we are truncating; supress new characters
	m_truncating = true;
}


//-------------------------------------------------
//  word_wrap
//-------------------------------------------------

void text_layout::word_wrap()
{
	// keep track of the last line and break
	line *last_line = m_current_line;
	size_t last_break = m_last_break;

	// start a new line with the same justification
	start_new_line(last_line->justify(), last_line->character(last_line->character_count() - 1).style.size);

	// find the begining of the word to wrap
	size_t position = last_break;
	while (position + 1 < last_line->character_count() && is_space_character(last_line->character(position).character))
		position++;

	// transcribe the characters
	for (size_t i = position; i < last_line->character_count(); i++)
	{
		auto &ch = last_line->character(i);
		m_current_line->add_character(ch.character, ch.style, ch.source);
	}

	// and finally, truncate the last line
	last_line->truncate(last_break);
}


//-------------------------------------------------
//  hit_test
//-------------------------------------------------

bool text_layout::hit_test(float x, float y, size_t &start, size_t &span) const
{
	for (const auto &line : m_lines)
	{
		if (y >= line->yoffset() && y < line->yoffset() + line->height())
		{
			float line_xoffset = line->xoffset();
			if (x >= line_xoffset && x < line_xoffset + line->width())
			{
				for (size_t i = 0; i < line->character_count(); i++)
				{
					const auto &ch = line->character(i);
					if (x >= ch.xoffset && x < ch.xoffset + ch.xwidth)
					{
						start = ch.source.start;
						span = ch.source.span;
						return true;
					}
				}
			}
		}
	}
	start = 0;
	span = 0;
	return false;
}


//-------------------------------------------------
//  restyle
//-------------------------------------------------

void text_layout::restyle(size_t start, size_t span, rgb_t *fgcolor, rgb_t *bgcolor)
{
	for (const auto &line : m_lines)
	{
		for (size_t i = 0; i < line->character_count(); i++)
		{
			auto &ch = line->character(i);
			if (ch.source.start >= start && ch.source.start + ch.source.span <= start + span)
			{
				if (fgcolor != nullptr)
					ch.style.fgcolor = *fgcolor;
				if (bgcolor != nullptr)
					ch.style.bgcolor = *bgcolor;
			}
		}
	}
}


//-------------------------------------------------
//  get_wrap_info
//-------------------------------------------------

int text_layout::get_wrap_info(std::vector<int> &xstart, std::vector<int> &xend) const
{
	// this is a hacky method (tailored to the need to implement
	// mame_ui_manager::wrap_text) but so be it
	int line_count = 0;
	for (const auto &line : m_lines)
	{
		int start_pos = 0;
		int end_pos = 0;

		auto line_character_count = line->character_count();
		if (line_character_count > 0)
		{
			start_pos = line->character(0).source.start;
			end_pos = line->character(line_character_count - 1).source.start
				+ line->character(line_character_count - 1).source.span;
		}

		line_count++;
		xstart.push_back(start_pos);
		xend.push_back(end_pos);
	}
	return line_count;
}


//-------------------------------------------------
//  emit
//-------------------------------------------------

void text_layout::emit(render_container *container, float x, float y)
{
	for (const auto &line : m_lines)
	{
		float line_xoffset = line->xoffset();

		// emit every single character
		for (auto i = 0; i < line->character_count(); i++)
		{
			auto &ch = line->character(i);

			// position this specific character correctly (TODO - this doesn't
			// handle differently sized text (yet)
			float char_x = x + line_xoffset + ch.xoffset;
			float char_y = y + line->yoffset();
			float char_width = ch.xwidth;
			float char_height = line->height();

			// render the background of the character (if present)
			if (ch.style.bgcolor.a() != 0)
				container->add_rect(char_x, char_y, char_x + char_width, char_y + char_height, ch.style.bgcolor, PRIMFLAG_BLENDMODE(BLENDMODE_ALPHA));

			// render the foreground
			container->add_char(
				char_x,
				char_y,
				char_height,
				xscale() / yscale(),
				ch.style.fgcolor,
				font(),
				ch.character);
		}
	}
}


//-------------------------------------------------
//  line::ctor
//-------------------------------------------------

text_layout::line::line(text_layout &layout, text_justify justify, float yoffset, float height)
	: m_layout(layout), m_justify(justify), m_yoffset(yoffset), m_width(0.0), m_height(height)
{
}


//-------------------------------------------------
//  line::add_character
//-------------------------------------------------

void text_layout::line::add_character(unicode_char ch, const char_style &style, const source_info &source)
{
	// get the width of this character
	float chwidth = m_layout.get_char_width(ch, style.size);

	// create the positioned character
	positioned_char positioned_char = { 0, };
	positioned_char.character = ch;
	positioned_char.xoffset = m_width;
	positioned_char.xwidth = chwidth;
	positioned_char.style = style;
	positioned_char.source = source;

	// append the character
	m_characters.push_back(positioned_char);
	m_width += chwidth;

	// we might be bigger
	m_height = std::max(m_height, style.size * m_layout.yscale());
}


//-------------------------------------------------
//  line::xoffset
//-------------------------------------------------

float text_layout::line::xoffset() const
{
	float result;
	switch (justify())
	{
		case LEFT:
		default:
			result = 0;
			break;
		case CENTER:
			result = (m_layout.width() - width()) / 2;
			break;
		case RIGHT:
			result = m_layout.width() - width();
			break;
	}
	return result;
}


//-------------------------------------------------
//  line::truncate
//-------------------------------------------------

void text_layout::line::truncate(size_t position)
{
	assert(position <= m_characters.size());

	// are we actually truncating?
	if (position < m_characters.size())
	{
		// set the width as appropriate
		m_width = m_characters[position].xoffset;

		// and resize the array
		m_characters.resize(position);
	}
}

} // namespace ui