summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/text.h
blob: a9169faa186364422b6c64de65bc5f07ae1ffb96 (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
// license:BSD-3-Clause
// copyright-holders:Nathan Woods, Vas Crabb
/***************************************************************************

    text.h

    Text functionality for MAME's crude user interface

***************************************************************************/
#ifndef MAME_FRONTEND_UI_TEXT_H
#define MAME_FRONTEND_UI_TEXT_H

#pragma once

#include <memory>
#include <string_view>
#include <vector>


class render_font;
class render_container;


namespace ui {

/***************************************************************************
TYPE DEFINITIONS
***************************************************************************/

class text_layout
{
public:
	// justification options for text
	enum class text_justify
	{
		LEFT = 0,
		CENTER,
		RIGHT
	};

	// word wrapping options
	enum class word_wrapping
	{
		NEVER,
		TRUNCATE,
		WORD
	};

	// ctor/dtor
	text_layout(render_font &font, float xscale, float yscale, float width, text_justify justify, word_wrapping wrap);
	text_layout(text_layout &&that);
	~text_layout();

	// accessors
	render_font &font() const { return m_font; }
	float xscale() const { return m_xscale;  }
	float yscale() const { return m_yscale; }
	float width() const { return m_width; }
	text_justify justify() const { return m_justify; }
	word_wrapping wrap() const { return m_wrap; }

	// methods
	float actual_left();
	float actual_width();
	float actual_height();
	bool empty() const { return m_lines.empty(); }
	size_t lines() const { return m_lines.size(); }
	bool hit_test(float x, float y, size_t &start, size_t &span);
	void restyle(size_t start, size_t span, rgb_t const *fgcolor, rgb_t const *bgcolor);
	void emit(render_container &container, float x, float y);
	void emit(render_container &container, size_t start, size_t lines, float x, float y);
	void add_text(std::string_view text, rgb_t fgcolor = rgb_t::white(), rgb_t bgcolor = rgb_t::transparent(), float size = 1.0)
	{
		add_text(text, justify(), char_style{ fgcolor, bgcolor, size });
	}
	void add_text(std::string_view text, text_justify line_justify, rgb_t fgcolor = rgb_t::white(), rgb_t bgcolor = rgb_t::transparent(), float size = 1.0)
	{
		add_text(text, line_justify, char_style{ fgcolor, bgcolor, size });
	}

private:
	// text style information - in a struct to facilitate copying
	struct char_style
	{
		rgb_t fgcolor;
		rgb_t bgcolor;
		float size;
	};

	// class to represent a line
	struct source_info;
	struct positioned_char;
	class line;

	// instance variables
	render_font &m_font;
	float m_xscale;
	float m_yscale;
	float m_width;
	mutable float m_calculated_actual_width;
	text_justify m_justify;
	word_wrapping m_wrap;
	std::vector<std::unique_ptr<line> > m_lines;
	line *m_current_line;
	size_t m_last_break;
	size_t m_text_position;
	bool m_truncating;

	// methods
	void add_text(std::string_view text, text_justify line_justify, char_style const &style);
	void start_new_line(float height);
	float get_char_width(char32_t ch, float size);
	void truncate_wrap();
	void word_wrap();
	void invalidate_calculated_actual_width();
};

} // namespace ui

#endif // MAME_FRONTEND_UI_TEXT_H