ElementText.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #include "../../Include/RmlUi/Core/ElementText.h"
  2. #include "../../Include/RmlUi/Core/Context.h"
  3. #include "../../Include/RmlUi/Core/Core.h"
  4. #include "../../Include/RmlUi/Core/ElementDocument.h"
  5. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  6. #include "../../Include/RmlUi/Core/Event.h"
  7. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  8. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  9. #include "../../Include/RmlUi/Core/Profiling.h"
  10. #include "../../Include/RmlUi/Core/Property.h"
  11. #include "../../Include/RmlUi/Core/RenderManager.h"
  12. #include "../../Include/RmlUi/Core/TextShapingContext.h"
  13. #include "ComputeProperty.h"
  14. #include "ElementDefinition.h"
  15. #include "ElementStyle.h"
  16. #include "TransformState.h"
  17. #include <limits>
  18. namespace Rml {
  19. static bool BuildToken(String& token, const char*& token_begin, const char* string_end, bool first_token, bool collapse_white_space,
  20. bool break_at_endline, Style::TextTransform text_transformation, bool decode_escape_characters);
  21. static bool LastToken(const char* token_begin, const char* string_end, bool collapse_white_space, bool break_at_endline);
  22. static int RoundDownToIntegerClamped(float value)
  23. {
  24. constexpr int clamp = (1 << std::numeric_limits<float>::digits);
  25. constexpr float clamp_f = float{clamp};
  26. if (value >= clamp_f)
  27. return clamp;
  28. if (value <= -clamp_f)
  29. return -clamp;
  30. return static_cast<int>(value);
  31. }
  32. struct TextOverflowResolved {
  33. bool enabled = false;
  34. float overflow_width = 0.f;
  35. String overflow_text;
  36. };
  37. static TextOverflowResolved ResolveTextOverflow(Element* parent, FontFaceHandle font_face_handle)
  38. {
  39. if (!parent)
  40. return {};
  41. const auto& parent_computed = parent->GetComputedValues();
  42. if (parent_computed.overflow_x() == Style::Overflow::Visible && parent_computed.overflow_y() == Style::Overflow::Visible)
  43. return {};
  44. const Style::TextOverflow text_overflow = parent_computed.text_overflow();
  45. if (text_overflow == Style::TextOverflow::Clip)
  46. return {};
  47. const Box& box = parent->GetBox();
  48. const BoxArea clip_area = parent->GetClipArea();
  49. auto AccumulateRightSideEdgesUpTo = [](const Box& box, BoxArea up_to_area) -> float {
  50. float result = 0;
  51. for (int i = (int)up_to_area; i < int(BoxArea::Content); i++)
  52. result += box.GetEdge((BoxArea)i, BoxEdge::Right);
  53. return result;
  54. };
  55. const float overflow_width = parent->GetScrollLeft() + box.GetSize().x + AccumulateRightSideEdgesUpTo(box, clip_area);
  56. constexpr char ellipsis_chars[] = "\xE2\x80\xA6"; // U+2026
  57. constexpr char dots_chars[] = "...";
  58. String overflow_text = (text_overflow == Style::TextOverflow::String
  59. ? parent->GetComputedValues().text_overflow_string()
  60. : (GetFontEngineInterface()->GetFontMetrics(font_face_handle).has_ellipsis ? ellipsis_chars : dots_chars));
  61. return TextOverflowResolved{
  62. true,
  63. overflow_width,
  64. std::move(overflow_text),
  65. };
  66. }
  67. void LogMissingFontFace(Element* element)
  68. {
  69. const String font_family_property = element->GetProperty<String>("font-family");
  70. if (font_family_property.empty())
  71. {
  72. Log::Message(Log::LT_WARNING, "No font face defined. Missing 'font-family' property. On element %s", element->GetAddress().c_str());
  73. }
  74. else
  75. {
  76. const ComputedValues& computed = element->GetComputedValues();
  77. const String font_face_description = GetFontFaceDescription(font_family_property, computed.font_style(), computed.font_weight());
  78. Log::Message(Log::LT_WARNING,
  79. "No font face defined. Ensure (1) that Context::Update is run after new elements are constructed, before Context::Render, "
  80. "and (2) that the specified font face %s has been successfully loaded. "
  81. "Please see previous log messages for all successfully loaded fonts. On element %s",
  82. font_face_description.c_str(), element->GetAddress().c_str());
  83. }
  84. }
  85. ElementText::ElementText(const String& tag) :
  86. Element(tag), colour(255, 255, 255), opacity(1), font_handle_version(0), geometry_dirty(true), dirty_layout_on_change(true),
  87. generated_decoration(Style::TextDecoration::None), decoration_property(Style::TextDecoration::None), font_effects_dirty(true),
  88. font_effects_handle(0)
  89. {}
  90. ElementText::~ElementText() {}
  91. void ElementText::SetText(const String& _text)
  92. {
  93. if (text != _text)
  94. {
  95. text = _text;
  96. if (dirty_layout_on_change)
  97. DirtyLayout();
  98. }
  99. }
  100. const String& ElementText::GetText() const
  101. {
  102. return text;
  103. }
  104. void ElementText::OnRender()
  105. {
  106. RMLUI_ZoneScoped;
  107. FontFaceHandle font_face_handle = GetFontFaceHandle();
  108. if (font_face_handle == 0)
  109. return;
  110. RenderManager& render_manager = GetContext()->GetRenderManager();
  111. // If our font effects have potentially changed, update it and force a geometry generation if necessary.
  112. if (font_effects_dirty && UpdateFontEffects())
  113. geometry_dirty = true;
  114. // Dirty geometry if font version has changed.
  115. int new_version = GetFontEngineInterface()->GetVersion(font_face_handle);
  116. if (new_version != font_handle_version)
  117. {
  118. font_handle_version = new_version;
  119. geometry_dirty = true;
  120. }
  121. // Regenerate the geometry if the colour or font configuration has altered.
  122. if (geometry_dirty)
  123. GenerateGeometry(render_manager, font_face_handle);
  124. // Regenerate text decoration if necessary.
  125. if (decoration_property != generated_decoration)
  126. {
  127. if (decoration_property == Style::TextDecoration::None)
  128. {
  129. decoration.reset();
  130. }
  131. else
  132. {
  133. Mesh mesh;
  134. if (decoration)
  135. mesh = decoration->Release(Geometry::ReleaseMode::ClearMesh);
  136. else
  137. decoration = MakeUnique<Geometry>();
  138. GenerateDecoration(mesh, font_face_handle);
  139. *decoration = GetRenderManager()->MakeGeometry(std::move(mesh));
  140. }
  141. generated_decoration = decoration_property;
  142. }
  143. const Vector2f translation = GetAbsoluteOffset();
  144. bool render = true;
  145. // Do a visibility test against the scissor region to avoid unnecessary render calls. Instead of handling
  146. // culling in complicated transform cases, for simplicity we always proceed to render if one is detected.
  147. Rectanglei scissor_region = render_manager.GetScissorRegion();
  148. if (!scissor_region.Valid())
  149. scissor_region = Rectanglei::FromSize(render_manager.GetViewport());
  150. if (!GetTransformState() || !GetTransformState()->GetTransform())
  151. {
  152. const FontMetrics& font_metrics = GetFontEngineInterface()->GetFontMetrics(GetFontFaceHandle());
  153. const int ascent = Math::RoundUpToInteger(font_metrics.ascent);
  154. const int descent = Math::RoundUpToInteger(font_metrics.descent);
  155. render = false;
  156. for (const Line& line : lines)
  157. {
  158. const Vector2i baseline = Vector2i(translation + line.position);
  159. const Rectanglei line_region = Rectanglei::FromCorners(baseline - Vector2i(0, ascent), baseline + Vector2i(line.width, descent));
  160. if (line_region.Valid() && scissor_region.Intersects(line_region))
  161. {
  162. render = true;
  163. break;
  164. }
  165. }
  166. }
  167. if (render)
  168. {
  169. for (size_t i = 0; i < geometry.size(); ++i)
  170. geometry[i].geometry.Render(translation, geometry[i].texture);
  171. }
  172. if (decoration)
  173. decoration->Render(translation);
  174. }
  175. bool ElementText::GenerateLine(String& line, int& line_length, float& line_width, int line_begin, float maximum_line_width, float right_spacing_width,
  176. bool trim_whitespace_prefix, bool decode_escape_characters, bool allow_empty)
  177. {
  178. RMLUI_ZoneScoped;
  179. RMLUI_ASSERT(maximum_line_width >= 0.f);
  180. FontFaceHandle font_face_handle = GetFontFaceHandle();
  181. // Initialise the output variables.
  182. line.clear();
  183. line_length = 0;
  184. line_width = 0;
  185. // Bail if we don't have a valid font face.
  186. if (font_face_handle == 0)
  187. {
  188. LogMissingFontFace(GetParentNode() ? GetParentNode() : this);
  189. return true;
  190. }
  191. // Determine how we are processing white-space while formatting the text.
  192. using namespace Style;
  193. const auto& computed = GetComputedValues();
  194. const WhiteSpace white_space_property = computed.white_space();
  195. const bool collapse_white_space =
  196. (white_space_property == WhiteSpace::Normal || white_space_property == WhiteSpace::Nowrap || white_space_property == WhiteSpace::Preline);
  197. const bool break_at_line =
  198. (white_space_property == WhiteSpace::Normal || white_space_property == WhiteSpace::Prewrap || white_space_property == WhiteSpace::Preline);
  199. const bool break_at_endline =
  200. (white_space_property == WhiteSpace::Pre || white_space_property == WhiteSpace::Prewrap || white_space_property == WhiteSpace::Preline);
  201. const TextShapingContext text_shaping_context{computed.language(), computed.direction(), computed.font_kerning(), computed.letter_spacing()};
  202. TextTransform text_transform_property = computed.text_transform();
  203. WordBreak word_break = computed.word_break();
  204. FontEngineInterface* font_engine_interface = GetFontEngineInterface();
  205. // Starting at the line_begin character, we generate sections of the text (we'll call them tokens) depending on the
  206. // white-space parsing parameters. Each section is then appended to the line if it can fit. If not, or if an
  207. // endline is found (and we're processing them), then the line is ended. kthxbai!
  208. const char* token_begin = text.c_str() + line_begin;
  209. const char* string_end = text.c_str() + text.size();
  210. while (token_begin != string_end)
  211. {
  212. String token;
  213. const char* next_token_begin = token_begin;
  214. Character previous_codepoint = Character::Null;
  215. if (!line.empty())
  216. previous_codepoint =
  217. StringUtilities::ToCharacter(StringUtilities::SeekBackwardUTF8(&line.back(), line.data()), line.data() + line.size());
  218. // Generate the next token and determine its pixel-length.
  219. bool break_line = BuildToken(token, next_token_begin, string_end, line.empty() && trim_whitespace_prefix, collapse_white_space,
  220. break_at_endline, text_transform_property, decode_escape_characters);
  221. int token_width = font_engine_interface->GetStringWidth(font_face_handle, token, text_shaping_context, previous_codepoint);
  222. // If we're breaking to fit a line box, check if the token can fit on the line before we add it.
  223. if (break_at_line)
  224. {
  225. const bool is_last_token = LastToken(next_token_begin, string_end, collapse_white_space, break_at_endline);
  226. int max_token_width = RoundDownToIntegerClamped(maximum_line_width - (is_last_token ? line_width + right_spacing_width : line_width));
  227. if (token_width > max_token_width)
  228. {
  229. if (word_break == WordBreak::BreakAll || (word_break == WordBreak::BreakWord && line.empty()))
  230. {
  231. // Try to break up the word
  232. max_token_width = RoundDownToIntegerClamped(maximum_line_width - line_width);
  233. const int token_max_size = int(next_token_begin - token_begin);
  234. const char* partial_string_end = token_begin + token_max_size;
  235. // @performance: Can be made much faster. Use string width heuristics and logarithmic search.
  236. while (true)
  237. {
  238. partial_string_end = StringUtilities::SeekBackwardUTF8(partial_string_end - 1, token_begin);
  239. bool force_loop_break_at_end = false;
  240. if (partial_string_end == token_begin)
  241. {
  242. // Not even the first character of the token fits. Let it overflow onto the next line if we can.
  243. if (allow_empty || !line.empty())
  244. return false;
  245. // Continue by forcing the first character to be consumed, even though it will overflow.
  246. partial_string_end = StringUtilities::SeekForwardUTF8(token_begin + 1, token_begin + token_max_size);
  247. force_loop_break_at_end = true;
  248. }
  249. token.clear();
  250. next_token_begin = token_begin;
  251. BuildToken(token, next_token_begin, partial_string_end, line.empty() && trim_whitespace_prefix, collapse_white_space,
  252. break_at_endline, text_transform_property, decode_escape_characters);
  253. token_width = font_engine_interface->GetStringWidth(font_face_handle, token, text_shaping_context, previous_codepoint);
  254. if (force_loop_break_at_end || token_width <= max_token_width)
  255. break;
  256. }
  257. break_line = true;
  258. }
  259. else if (allow_empty || !line.empty())
  260. {
  261. // Let the token overflow into the next line.
  262. return false;
  263. }
  264. }
  265. }
  266. // The token can fit on the end of the line, so add it onto the end and increment our width and length counters.
  267. line += token;
  268. line_length += (int)(next_token_begin - token_begin);
  269. line_width += token_width;
  270. // Break out of the loop if an endline was forced.
  271. if (break_line && (allow_empty || !line.empty()))
  272. return false;
  273. // Set the beginning of the next token.
  274. token_begin = next_token_begin;
  275. }
  276. return true;
  277. }
  278. void ElementText::ClearLines()
  279. {
  280. RMLUI_ZoneScoped;
  281. lines.clear();
  282. generated_decoration = Style::TextDecoration::None;
  283. geometry_dirty = true;
  284. }
  285. void ElementText::AddLine(Vector2f line_position, String line)
  286. {
  287. if (font_effects_dirty)
  288. UpdateFontEffects();
  289. lines.emplace_back(std::move(line), line_position);
  290. geometry_dirty = true;
  291. }
  292. void ElementText::SuppressAutoLayout()
  293. {
  294. dirty_layout_on_change = false;
  295. }
  296. void ElementText::OnPropertyChange(const PropertyIdSet& changed_properties)
  297. {
  298. RMLUI_ZoneScoped;
  299. Element::OnPropertyChange(changed_properties);
  300. bool colour_changed = false;
  301. bool font_face_changed = false;
  302. auto& computed = GetComputedValues();
  303. if (changed_properties.Contains(PropertyId::Color) || changed_properties.Contains(PropertyId::Opacity))
  304. {
  305. const float new_opacity = computed.opacity();
  306. const bool opacity_changed = opacity != new_opacity;
  307. ColourbPremultiplied new_colour = computed.color().ToPremultiplied(new_opacity);
  308. colour_changed = colour != new_colour;
  309. if (colour_changed)
  310. {
  311. colour = new_colour;
  312. }
  313. if (opacity_changed)
  314. {
  315. opacity = new_opacity;
  316. font_effects_dirty = true;
  317. geometry_dirty = true;
  318. }
  319. }
  320. if (changed_properties.Contains(PropertyId::FontFamily) || //
  321. changed_properties.Contains(PropertyId::FontWeight) || //
  322. changed_properties.Contains(PropertyId::FontStyle) || //
  323. changed_properties.Contains(PropertyId::FontSize) || //
  324. changed_properties.Contains(PropertyId::FontKerning) || //
  325. changed_properties.Contains(PropertyId::LetterSpacing) || //
  326. changed_properties.Contains(PropertyId::RmlUi_Language) || //
  327. changed_properties.Contains(PropertyId::RmlUi_Direction) || //
  328. changed_properties.Contains(PropertyId::TextOverflow))
  329. {
  330. font_face_changed = true;
  331. geometry_dirty = true;
  332. font_effects_handle = 0;
  333. font_effects_dirty = true;
  334. font_handle_version = 0;
  335. }
  336. if (changed_properties.Contains(PropertyId::FontEffect))
  337. {
  338. font_effects_dirty = true;
  339. }
  340. if (changed_properties.Contains(PropertyId::TextDecoration))
  341. {
  342. decoration_property = computed.text_decoration();
  343. if (decoration && decoration_property == Style::TextDecoration::None)
  344. decoration.reset();
  345. }
  346. if (font_face_changed)
  347. {
  348. // We have to let our document know we need to be regenerated.
  349. if (dirty_layout_on_change)
  350. DirtyLayout();
  351. }
  352. else if (colour_changed)
  353. {
  354. // Force the geometry to be regenerated.
  355. geometry_dirty = true;
  356. // Re-colour the decoration geometry.
  357. if (decoration)
  358. {
  359. Mesh mesh = decoration->Release();
  360. for (Vertex& vertex : mesh.vertices)
  361. vertex.colour = colour;
  362. if (RenderManager* render_manager = GetRenderManager())
  363. *decoration = render_manager->MakeGeometry(std::move(mesh));
  364. }
  365. }
  366. }
  367. void ElementText::GetRML(String& content)
  368. {
  369. content += StringUtilities::EncodeRml(text);
  370. }
  371. bool ElementText::UpdateFontEffects()
  372. {
  373. RMLUI_ZoneScoped;
  374. if (GetFontFaceHandle() == 0)
  375. return false;
  376. font_effects_dirty = false;
  377. static const FontEffectList empty_font_effects;
  378. // Fetch the font-effect for this text element
  379. const FontEffectList* font_effects = &empty_font_effects;
  380. if (GetComputedValues().has_font_effect())
  381. {
  382. if (const Property* p = GetProperty(PropertyId::FontEffect))
  383. if (FontEffectsPtr effects = p->Get<FontEffectsPtr>())
  384. font_effects = &effects->list;
  385. }
  386. // Request a font layer configuration to match this set of effects. If this is different from
  387. // our old configuration, then return true to indicate we'll need to regenerate geometry.
  388. FontEffectsHandle new_font_effects_handle = GetFontEngineInterface()->PrepareFontEffects(GetFontFaceHandle(), *font_effects);
  389. if (new_font_effects_handle != font_effects_handle)
  390. {
  391. font_effects_handle = new_font_effects_handle;
  392. return true;
  393. }
  394. return false;
  395. }
  396. void ElementText::GenerateGeometry(RenderManager& render_manager, const FontFaceHandle font_face_handle)
  397. {
  398. RMLUI_ZoneScopedC(0xD2691E);
  399. const TextOverflowResolved text_overflow = ResolveTextOverflow(GetParentNode(), font_face_handle);
  400. const auto& computed = GetComputedValues();
  401. const TextShapingContext text_shaping_context{computed.language(), computed.direction(), computed.font_kerning(), computed.letter_spacing()};
  402. TexturedMeshList mesh_list;
  403. mesh_list.reserve(geometry.size());
  404. for (Line& line : lines)
  405. {
  406. line.width = GetFontEngineInterface()->GenerateString(render_manager, font_face_handle, font_effects_handle, line.text, line.position, colour,
  407. opacity, text_shaping_context, mesh_list);
  408. }
  409. const auto text_overflows_on_line = [&](const Line& line) { return line.position.x + line.width > text_overflow.overflow_width; };
  410. if (text_overflow.enabled && std::any_of(lines.begin(), lines.end(), text_overflows_on_line))
  411. {
  412. mesh_list.clear();
  413. for (Line& line : lines)
  414. {
  415. if (line.text.empty())
  416. continue;
  417. String abbreviated_text;
  418. StringView text_submit_view = line.text;
  419. StringIteratorU8 view(line.text, line.text.size());
  420. --view;
  421. // If we have text overflow, reduce the string one character at a time, append the ellipsis or custom
  422. // string, and try again until it fits. @performance Can be improved by e.g. logarithmic search. Consider
  423. // combining it with the word-breaking algorithm of 'GenerateLine'.
  424. for (; text_overflows_on_line(line) && view && view.get() != line.text.c_str(); --view)
  425. {
  426. abbreviated_text.reserve(line.text.size() + text_overflow.overflow_text.size());
  427. abbreviated_text.assign(line.text.c_str(), view.get());
  428. abbreviated_text.append(text_overflow.overflow_text);
  429. line.width = GetFontEngineInterface()->GetStringWidth(font_face_handle, abbreviated_text, text_shaping_context);
  430. text_submit_view = abbreviated_text;
  431. }
  432. line.width = GetFontEngineInterface()->GenerateString(render_manager, font_face_handle, font_effects_handle, text_submit_view,
  433. line.position, colour, opacity, text_shaping_context, mesh_list);
  434. }
  435. }
  436. // Apply the new geometry and textures. Reuse the old geometry if the mesh matches, which can be relatively common
  437. // where the layout is changed in a way that does not visually affect this element.
  438. geometry.resize(mesh_list.size());
  439. for (size_t i = 0; i < mesh_list.size(); i++)
  440. {
  441. if (!geometry[i].geometry || geometry[i].geometry.GetMesh() != mesh_list[i].mesh)
  442. geometry[i].geometry = render_manager.MakeGeometry(std::move(mesh_list[i].mesh));
  443. geometry[i].texture = mesh_list[i].texture;
  444. }
  445. generated_decoration = Style::TextDecoration::None;
  446. geometry_dirty = false;
  447. }
  448. void ElementText::GenerateDecoration(Mesh& mesh, const FontFaceHandle font_face_handle)
  449. {
  450. RMLUI_ZoneScopedC(0xA52A2A);
  451. RMLUI_ASSERT(decoration);
  452. const FontMetrics& metrics = GetFontEngineInterface()->GetFontMetrics(font_face_handle);
  453. float offset = 0.f;
  454. switch (decoration_property)
  455. {
  456. case Style::TextDecoration::Underline: offset = metrics.underline_position; break;
  457. case Style::TextDecoration::Overline: offset = -1.1f * metrics.ascent; break;
  458. case Style::TextDecoration::LineThrough: offset = -0.65f * metrics.x_height; break;
  459. case Style::TextDecoration::None: return;
  460. }
  461. for (const Line& line : lines)
  462. {
  463. const Vector2f position = {line.position.x, line.position.y + offset};
  464. const Vector2f size = {(float)line.width, metrics.underline_thickness};
  465. MeshUtilities::GenerateLine(mesh, position, size, colour);
  466. }
  467. }
  468. static bool BuildToken(String& token, const char*& token_begin, const char* string_end, bool first_token, bool collapse_white_space,
  469. bool break_at_endline, Style::TextTransform text_transformation, bool decode_escape_characters)
  470. {
  471. RMLUI_ASSERT(token_begin != string_end);
  472. token.reserve(string_end - token_begin + token.size());
  473. // Check what the first character of the token is; all we need to know is if it is white-space or not.
  474. bool parsing_white_space = StringUtilities::IsWhitespace(*token_begin);
  475. // Loop through the string from the token's beginning until we find an end to the token. This can occur in various
  476. // places, depending on the white-space processing;
  477. // - at the end of a section of non-white-space characters,
  478. // - at the end of a section of white-space characters, if we're not collapsing white-space,
  479. // - at an endline token, if we're breaking on endlines.
  480. while (token_begin != string_end)
  481. {
  482. bool force_non_whitespace = false;
  483. char character = *token_begin;
  484. const char* escape_begin = token_begin;
  485. // Check for an ampersand; if we find one, we've got an HTML escaped character.
  486. if (decode_escape_characters && character == '&')
  487. {
  488. // Find the terminating ';'.
  489. while (token_begin != string_end && *token_begin != ';')
  490. ++token_begin;
  491. // If we couldn't find the ';', print the token like normal text.
  492. if (token_begin == string_end)
  493. {
  494. token_begin = escape_begin;
  495. }
  496. // We could find a ';', parse the escape code. If the escape code is recognised, set the parsed character
  497. // to the appropriate one. If it is a non-breaking space, prevent it being picked up as whitespace. If it
  498. // is not recognised, print the token like normal text.
  499. else
  500. {
  501. String escape_code(escape_begin + 1, token_begin);
  502. if (escape_code == "lt")
  503. character = '<';
  504. else if (escape_code == "gt")
  505. character = '>';
  506. else if (escape_code == "amp")
  507. character = '&';
  508. else if (escape_code == "quot")
  509. character = '"';
  510. else if (escape_code == "nbsp")
  511. {
  512. character = ' ';
  513. force_non_whitespace = true;
  514. }
  515. else
  516. token_begin = escape_begin;
  517. }
  518. }
  519. // Check for an endline token; if we're breaking on endlines and we find one, then return true to indicate a
  520. // forced break.
  521. if (break_at_endline && character == '\n')
  522. {
  523. token += '\n';
  524. token_begin++;
  525. return true;
  526. }
  527. // If we've transitioned from white-space characters to non-white-space characters, or vice-versa, then check
  528. // if should terminate the token; if we're not collapsing white-space, then yes (as sections of white-space are
  529. // non-breaking), otherwise only if we've transitioned from characters to white-space.
  530. bool white_space = !force_non_whitespace && StringUtilities::IsWhitespace(character);
  531. if (white_space != parsing_white_space)
  532. {
  533. if (!collapse_white_space)
  534. {
  535. // Restore pointer to the beginning of the escaped token, if we processed an escape code.
  536. token_begin = escape_begin;
  537. return false;
  538. }
  539. // We're collapsing white-space; we only tokenise words, not white-space, so we're only done tokenising
  540. // once we've begun parsing non-white-space and then found white-space.
  541. if (!parsing_white_space)
  542. {
  543. // However, if we are the last non-whitespace character in the string, and there are trailing
  544. // whitespace characters after this token, then we need to append a single space to the end of this
  545. // token.
  546. if (token_begin != string_end && LastToken(token_begin, string_end, collapse_white_space, break_at_endline))
  547. token += ' ';
  548. return false;
  549. }
  550. // We've transitioned from white-space to non-white-space, so we append a single white-space character.
  551. if (!first_token)
  552. token += ' ';
  553. parsing_white_space = false;
  554. }
  555. // If the current character is white-space, we'll append a space character to the token if we're not collapsing
  556. // sections of white-space.
  557. if (white_space)
  558. {
  559. if (!collapse_white_space)
  560. token += ' ';
  561. }
  562. else
  563. {
  564. if (text_transformation == Style::TextTransform::Uppercase)
  565. {
  566. if (character >= 'a' && character <= 'z')
  567. character += ('A' - 'a');
  568. }
  569. else if (text_transformation == Style::TextTransform::Lowercase)
  570. {
  571. if (character >= 'A' && character <= 'Z')
  572. character -= ('A' - 'a');
  573. }
  574. token += character;
  575. }
  576. ++token_begin;
  577. }
  578. return false;
  579. }
  580. static bool LastToken(const char* token_begin, const char* string_end, bool collapse_white_space, bool break_at_endline)
  581. {
  582. bool last_token = (token_begin == string_end);
  583. if (collapse_white_space && !last_token)
  584. {
  585. last_token = true;
  586. const char* character = token_begin;
  587. while (character != string_end)
  588. {
  589. if (!StringUtilities::IsWhitespace(*character) || (break_at_endline && *character == '\n'))
  590. {
  591. last_token = false;
  592. break;
  593. }
  594. character++;
  595. }
  596. }
  597. return last_token;
  598. }
  599. } // namespace Rml