persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
TextLayout.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include "types/TextTypes.hpp"
14
15namespace TextLayout
16{
17inline void applyStyle(Font& font, unsigned char flags, bool& bold, bool& italic, bool& underline)
18{
19 bold = flags != TextInstruction::Reset && (flags & TextInstruction::StyleBold) && font.boldLoaded;
20 italic = flags != TextInstruction::Reset && (flags & TextInstruction::StyleItalic);
21 underline = flags != TextInstruction::Reset && (flags & TextInstruction::StyleUnderline);
22}
23
24// Measure visible pixels, not trailing letter spacing or embedded instruction bytes.
25inline int wordWidth(const Text& text, std::size_t position)
26{
27 bool bold = text.bold;
28 bool italic = text.italic;
29 bool underline = text.underline;
30 int advance = 0;
31 int width = 0;
32 while (position < text.content.size())
33 {
34 const auto c = static_cast<unsigned char>(text.content[position++]);
35 if (c == ' ' || c == '\n')
36 {
37 break;
38 }
39 if (c == TextInstructionMarker)
40 {
41 if (position + 1 >= text.content.size())
42 {
43 break;
44 }
45 const auto instruction = static_cast<unsigned char>(text.content[position++]);
46 const auto argument = static_cast<unsigned char>(text.content[position++]);
47 if (instruction == TextInstruction::StyleChange)
48 {
49 applyStyle(*text.font, argument, bold, italic, underline);
50 }
51 continue;
52 }
53 const Glyph& glyph = bold ? text.font->boldGlyphs[c] : text.font->glyphs[c];
54 if (glyph.width == 0)
55 {
56 advance += text.spaceWidth;
57 width = advance;
58 }
59 else
60 {
61 const int slant = italic ? ((glyph.height - 1) * 64) >> 8 : 0;
62 width = advance + glyph.width + slant;
63 advance += glyph.width + text.letterSpacing;
64 }
65 }
66 return width;
67}
68
69inline void newLine(Text& text)
70{
71 text.cursorX = text.startX;
72 text.cursorY += text.font->lineHeight + text.lineSpacing;
73}
74
75inline bool pageFull(Text& text)
76{
77 text.pageBreak = text.maxY > 0 && text.cursorY + text.font->lineHeight > text.maxY;
78 return text.pageBreak;
79}
80
81inline bool shouldWrapWord(const Text& text)
82{
83 return text.cursorX + text.spaceWidth + wordWidth(text, text.cursorPos + 1) > text.maxX;
84}
85
86// Called before drawing a glyph. For dialogue, move the current glyph to a new
87// line when the rest of its word will not fit. This is a visual character-based
88// break, not linguistic hyphenation; the hyphen is drawn separately by the caller.
89// Returns true when the caller should draw that hyphen at the old cursor.
90inline bool prepareGlyph(Text& text, const Glyph& glyph)
91{
92 // Leave ordinary text wrapping and pagination to their existing paths.
93 if (!text.hyphenate || pageFull(text))
94 {
95 return false;
96 }
97
98 const Glyph& hyphen = text.bold ? text.font->boldGlyphs['-'] : text.font->glyphs['-'];
99 const int slant = text.italic ? ((hyphen.height - 1) * 64) >> 8 : 0;
100 const int hyphenWidth = hyphen.width > 0 ? hyphen.width + slant : 0;
101 const int remaining = wordWidth(text, text.cursorPos);
102 const bool wordOverflows = text.cursorX + remaining > text.maxX;
103 const int glyphSlant = text.italic ? ((glyph.height - 1) * 64) >> 8 : 0;
104 const bool glyphOverflows = text.cursorX + glyph.width + glyphSlant > text.maxX;
105 // Reserve room for a hyphen only when this break actually splits an overlong word.
106 const bool needsHyphenSpace =
107 hyphen.width > 0 && wordOverflows && text.cursorX + glyph.width + text.letterSpacing + hyphenWidth > text.maxX;
108 if (text.cursorX > text.startX && (glyphOverflows || needsHyphenSpace))
109 {
110 const bool drawHyphen = hyphen.width > 0 && text.cursorX + hyphenWidth <= text.maxX;
111 // Keep cursorPos unchanged so the caller retries this glyph on the new line/page.
112 newLine(text);
113 pageFull(text);
114 return drawHyphen;
115 }
116 return false;
117}
118} // namespace TextLayout
@ StyleUnderline
Definition TextTypes.hpp:45
@ StyleChange
Definition TextTypes.hpp:42
@ Reset
Definition TextTypes.hpp:46
@ StyleBold
Definition TextTypes.hpp:43
@ StyleItalic
Definition TextTypes.hpp:44
constexpr unsigned char TextInstructionMarker
Definition TextTypes.hpp:49
bool pageFull(Text &text)
void applyStyle(Font &font, unsigned char flags, bool &bold, bool &italic, bool &underline)
void newLine(Text &text)
int wordWidth(const Text &text, std::size_t position)
bool prepareGlyph(Text &text, const Glyph &glyph)
bool shouldWrapWord(const Text &text)
Stores data for a font.
Definition TextTypes.hpp:69
Glyph glyphs[256]
Definition TextTypes.hpp:89
Glyph boldGlyphs[256]
Definition TextTypes.hpp:90
int lineHeight
Definition TextTypes.hpp:88
bool boldLoaded
Definition TextTypes.hpp:91
Stores data for a single glyph (character) in a font.
Definition TextTypes.hpp:55
int height
Used to check if the glyph was read in correctly. Setting it to 0 here wipes any old data.
Definition TextTypes.hpp:59
int width
Definition TextTypes.hpp:58
A struct that represents a block of text being rendered on the screen.
Definition TextTypes.hpp:98
int startX
int letterSpacing
bool hyphenate
int cursorX
Definition TextTypes.hpp:99
bool underline
bool bold
int maxY
Bottom edge used for pagination. 0 disables pagination.
bool pageBreak
Set when the next glyph would not fit above maxY. Output pauses until the next page is requested.
int maxX
Right edge (exclusive) used for word wrapping.
int cursorPos
std::string content
int lineSpacing
int spaceWidth
int cursorY
Font * font
bool italic