ElementText.cpp 22 KB

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