ElementText.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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/GeometryUtilities.h"
  36. #include "../../Include/RmlUi/Core/Profiling.h"
  37. #include "../../Include/RmlUi/Core/Property.h"
  38. #include "ComputeProperty.h"
  39. #include "ElementDefinition.h"
  40. #include "ElementStyle.h"
  41. namespace Rml {
  42. static bool BuildToken(String& token, const char*& token_begin, const char* string_end, bool first_token, bool collapse_white_space,
  43. bool break_at_endline, Style::TextTransform text_transformation, bool decode_escape_characters);
  44. static bool LastToken(const char* token_begin, const char* string_end, bool collapse_white_space, bool break_at_endline);
  45. void LogMissingFontFace(Element* element)
  46. {
  47. const String font_family_property = element->GetProperty<String>("font-family");
  48. if (font_family_property.empty())
  49. {
  50. Log::Message(Log::LT_WARNING, "No font face defined. Missing 'font-family' property. On element %s", element->GetAddress().c_str());
  51. }
  52. else
  53. {
  54. const ComputedValues& computed = element->GetComputedValues();
  55. const String font_face_description = GetFontFaceDescription(font_family_property, computed.font_style(), computed.font_weight());
  56. Log::Message(Log::LT_WARNING,
  57. "No font face defined. Ensure (1) that Context::Update is run after new elements are constructed, before Context::Render, "
  58. "and (2) that the specified font face %s has been successfully loaded. "
  59. "Please see previous log messages for all successfully loaded fonts. On element %s",
  60. font_face_description.c_str(), element->GetAddress().c_str());
  61. }
  62. }
  63. ElementText::ElementText(const String& tag) :
  64. Element(tag), colour(255, 255, 255), opacity(1), font_handle_version(0), geometry_dirty(true), dirty_layout_on_change(true),
  65. generated_decoration(Style::TextDecoration::None), decoration_property(Style::TextDecoration::None), font_effects_dirty(true),
  66. font_effects_handle(0)
  67. {}
  68. ElementText::~ElementText() {}
  69. void ElementText::SetText(const String& _text)
  70. {
  71. if (text != _text)
  72. {
  73. text = _text;
  74. if (dirty_layout_on_change)
  75. DirtyLayout();
  76. }
  77. }
  78. const String& ElementText::GetText() const
  79. {
  80. return text;
  81. }
  82. void ElementText::OnRender()
  83. {
  84. RMLUI_ZoneScoped;
  85. FontFaceHandle font_face_handle = GetFontFaceHandle();
  86. if (font_face_handle == 0)
  87. return;
  88. // If our font effects have potentially changed, update it and force a geometry generation if necessary.
  89. if (font_effects_dirty && UpdateFontEffects())
  90. geometry_dirty = true;
  91. // Dirty geometry if font version has changed.
  92. int new_version = GetFontEngineInterface()->GetVersion(font_face_handle);
  93. if (new_version != font_handle_version)
  94. {
  95. font_handle_version = new_version;
  96. geometry_dirty = true;
  97. }
  98. // Regenerate the geometry if the colour or font configuration has altered.
  99. if (geometry_dirty)
  100. GenerateGeometry(font_face_handle);
  101. // Regenerate text decoration if necessary.
  102. if (decoration_property != generated_decoration)
  103. {
  104. if (decoration_property == Style::TextDecoration::None)
  105. {
  106. decoration.reset();
  107. }
  108. else
  109. {
  110. if (decoration)
  111. decoration->Release(true);
  112. else
  113. decoration = MakeUnique<Geometry>();
  114. GenerateDecoration(font_face_handle);
  115. }
  116. generated_decoration = decoration_property;
  117. }
  118. const Vector2f translation = GetAbsoluteOffset();
  119. bool render = true;
  120. Vector2i clip_origin;
  121. Vector2i clip_dimensions;
  122. if (GetContext()->GetActiveClipRegion(clip_origin, clip_dimensions))
  123. {
  124. const FontMetrics& font_metrics = GetFontEngineInterface()->GetFontMetrics(GetFontFaceHandle());
  125. float clip_top = (float)clip_origin.y;
  126. float clip_left = (float)clip_origin.x;
  127. float clip_right = (float)(clip_origin.x + clip_dimensions.x);
  128. float clip_bottom = (float)(clip_origin.y + clip_dimensions.y);
  129. float ascent = font_metrics.ascent;
  130. float descent = font_metrics.descent;
  131. render = false;
  132. for (const Line& line : lines)
  133. {
  134. float x_left = translation.x + line.position.x;
  135. float x_right = x_left + line.width;
  136. float y = translation.y + line.position.y;
  137. float y_top = y - ascent;
  138. float y_bottom = y + descent;
  139. render = !(x_left > clip_right || x_right < clip_left || y_top > clip_bottom || y_bottom < clip_top);
  140. if (render)
  141. break;
  142. }
  143. }
  144. if (render)
  145. {
  146. for (size_t i = 0; i < geometry.size(); ++i)
  147. geometry[i].Render(translation);
  148. }
  149. if (decoration)
  150. decoration->Render(translation);
  151. }
  152. bool ElementText::GenerateLine(String& line, int& line_length, float& line_width, int line_begin, float maximum_line_width, float right_spacing_width,
  153. bool trim_whitespace_prefix, bool decode_escape_characters, bool allow_empty)
  154. {
  155. RMLUI_ZoneScoped;
  156. RMLUI_ASSERT(
  157. maximum_line_width >= 0.f); // TODO: Check all callers for conformance, check break at line condition below. Possibly check for FLT_MAX.
  158. FontFaceHandle font_face_handle = GetFontFaceHandle();
  159. // Initialise the output variables.
  160. line.clear();
  161. line_length = 0;
  162. line_width = 0;
  163. // Bail if we don't have a valid font face.
  164. if (font_face_handle == 0)
  165. {
  166. LogMissingFontFace(GetParentNode() ? GetParentNode() : this);
  167. return true;
  168. }
  169. // Determine how we are processing white-space while formatting the text.
  170. using namespace Style;
  171. auto& computed = GetComputedValues();
  172. WhiteSpace white_space_property = computed.white_space();
  173. bool collapse_white_space =
  174. white_space_property == WhiteSpace::Normal || white_space_property == WhiteSpace::Nowrap || white_space_property == WhiteSpace::Preline;
  175. bool break_at_line = (maximum_line_width >= 0) &&
  176. (white_space_property == WhiteSpace::Normal || white_space_property == WhiteSpace::Prewrap || white_space_property == WhiteSpace::Preline);
  177. bool break_at_endline =
  178. white_space_property == WhiteSpace::Pre || white_space_property == WhiteSpace::Prewrap || white_space_property == WhiteSpace::Preline;
  179. float letter_spacing = computed.letter_spacing();
  180. TextTransform text_transform_property = computed.text_transform();
  181. WordBreak word_break = computed.word_break();
  182. FontEngineInterface* font_engine_interface = GetFontEngineInterface();
  183. // Starting at the line_begin character, we generate sections of the text (we'll call them tokens) depending on the
  184. // white-space parsing parameters. Each section is then appended to the line if it can fit. If not, or if an
  185. // endline is found (and we're processing them), then the line is ended. kthxbai!
  186. const char* token_begin = text.c_str() + line_begin;
  187. const char* string_end = text.c_str() + text.size();
  188. while (token_begin != string_end)
  189. {
  190. String token;
  191. const char* next_token_begin = token_begin;
  192. Character previous_codepoint = Character::Null;
  193. if (!line.empty())
  194. previous_codepoint = StringUtilities::ToCharacter(StringUtilities::SeekBackwardUTF8(&line.back(), line.data()));
  195. // Generate the next token and determine its pixel-length.
  196. bool break_line = BuildToken(token, next_token_begin, string_end, line.empty() && trim_whitespace_prefix, collapse_white_space,
  197. break_at_endline, text_transform_property, decode_escape_characters);
  198. int token_width = font_engine_interface->GetStringWidth(font_face_handle, token, letter_spacing, previous_codepoint);
  199. // If we're breaking to fit a line box, check if the token can fit on the line before we add it.
  200. if (break_at_line)
  201. {
  202. const bool is_last_token = LastToken(next_token_begin, string_end, collapse_white_space, break_at_endline);
  203. int max_token_width = int(maximum_line_width - (is_last_token ? line_width + right_spacing_width : line_width));
  204. if (token_width > max_token_width)
  205. {
  206. if (word_break == WordBreak::BreakAll || (word_break == WordBreak::BreakWord && line.empty()))
  207. {
  208. // Try to break up the word
  209. max_token_width = int(maximum_line_width - line_width);
  210. const int token_max_size = int(next_token_begin - token_begin);
  211. bool force_loop_break_after_next = false;
  212. // @performance: Can be made much faster. Use string width heuristics and logarithmic search.
  213. for (int i = token_max_size - 1; i > 0; --i)
  214. {
  215. token.clear();
  216. next_token_begin = token_begin;
  217. const char* partial_string_end = StringUtilities::SeekBackwardUTF8(token_begin + i, token_begin);
  218. BuildToken(token, next_token_begin, partial_string_end, line.empty() && trim_whitespace_prefix, collapse_white_space,
  219. break_at_endline, text_transform_property, decode_escape_characters);
  220. token_width = font_engine_interface->GetStringWidth(font_face_handle, token, letter_spacing, previous_codepoint);
  221. if (force_loop_break_after_next || token_width <= max_token_width)
  222. {
  223. break;
  224. }
  225. else if (next_token_begin == token_begin)
  226. {
  227. // This means the first character of the token doesn't fit. Let it overflow into the next line if we can.
  228. if (allow_empty || !line.empty())
  229. return false;
  230. // Not even the first character of the line fits. Go back to consume the first character even though it will overflow.
  231. i += 2;
  232. force_loop_break_after_next = true;
  233. }
  234. }
  235. break_line = true;
  236. }
  237. else if (allow_empty || !line.empty())
  238. {
  239. // Let the token overflow into the next line.
  240. return false;
  241. }
  242. }
  243. }
  244. // The token can fit on the end of the line, so add it onto the end and increment our width and length counters.
  245. line += token;
  246. line_length += (int)(next_token_begin - token_begin);
  247. line_width += token_width;
  248. // Break out of the loop if an endline was forced.
  249. if (break_line && (allow_empty || !line.empty()))
  250. return false;
  251. // Set the beginning of the next token.
  252. token_begin = next_token_begin;
  253. }
  254. return true;
  255. }
  256. void ElementText::ClearLines()
  257. {
  258. // Clear the rendering information.
  259. for (size_t i = 0; i < geometry.size(); ++i)
  260. geometry[i].Release(true);
  261. lines.clear();
  262. generated_decoration = Style::TextDecoration::None;
  263. }
  264. void ElementText::AddLine(Vector2f line_position, String line)
  265. {
  266. if (font_effects_dirty)
  267. UpdateFontEffects();
  268. lines.emplace_back(std::move(line), line_position);
  269. geometry_dirty = true;
  270. }
  271. void ElementText::SuppressAutoLayout()
  272. {
  273. dirty_layout_on_change = false;
  274. }
  275. void ElementText::OnPropertyChange(const PropertyIdSet& changed_properties)
  276. {
  277. RMLUI_ZoneScoped;
  278. Element::OnPropertyChange(changed_properties);
  279. bool colour_changed = false;
  280. bool font_face_changed = false;
  281. auto& computed = GetComputedValues();
  282. if (changed_properties.Contains(PropertyId::Color) || changed_properties.Contains(PropertyId::Opacity))
  283. {
  284. const float new_opacity = computed.opacity();
  285. const bool opacity_changed = opacity != new_opacity;
  286. Colourb new_colour = computed.color();
  287. new_colour.alpha = byte(new_opacity * float(new_colour.alpha));
  288. colour_changed = colour != new_colour;
  289. if (colour_changed)
  290. {
  291. colour = new_colour;
  292. }
  293. if (opacity_changed)
  294. {
  295. opacity = new_opacity;
  296. font_effects_dirty = true;
  297. geometry_dirty = true;
  298. }
  299. }
  300. if (changed_properties.Contains(PropertyId::FontFamily) || //
  301. changed_properties.Contains(PropertyId::FontWeight) || //
  302. changed_properties.Contains(PropertyId::FontStyle) || //
  303. changed_properties.Contains(PropertyId::FontSize) || //
  304. changed_properties.Contains(PropertyId::LetterSpacing))
  305. {
  306. font_face_changed = true;
  307. geometry.clear();
  308. geometry_dirty = true;
  309. font_effects_handle = 0;
  310. font_effects_dirty = true;
  311. font_handle_version = 0;
  312. }
  313. if (changed_properties.Contains(PropertyId::FontEffect))
  314. {
  315. font_effects_dirty = true;
  316. }
  317. if (changed_properties.Contains(PropertyId::TextDecoration))
  318. {
  319. decoration_property = computed.text_decoration();
  320. if (decoration && decoration_property == Style::TextDecoration::None)
  321. decoration.reset();
  322. }
  323. if (font_face_changed)
  324. {
  325. // We have to let our document know we need to be regenerated.
  326. if (dirty_layout_on_change)
  327. DirtyLayout();
  328. }
  329. else if (colour_changed)
  330. {
  331. // Force the geometry to be regenerated.
  332. geometry_dirty = true;
  333. // Re-colour the decoration geometry.
  334. if (decoration)
  335. {
  336. Vector<Vertex>& vertices = decoration->GetVertices();
  337. for (size_t i = 0; i < vertices.size(); ++i)
  338. vertices[i].colour = colour;
  339. decoration->Release();
  340. }
  341. }
  342. }
  343. void ElementText::GetRML(String& content)
  344. {
  345. content += StringUtilities::EncodeRml(text);
  346. }
  347. bool ElementText::UpdateFontEffects()
  348. {
  349. RMLUI_ZoneScoped;
  350. if (GetFontFaceHandle() == 0)
  351. return false;
  352. font_effects_dirty = false;
  353. static const FontEffectList empty_font_effects;
  354. // Fetch the font-effect for this text element
  355. const FontEffectList* font_effects = &empty_font_effects;
  356. if (GetComputedValues().has_font_effect())
  357. {
  358. if (const Property* p = GetProperty(PropertyId::FontEffect))
  359. if (FontEffectsPtr effects = p->Get<FontEffectsPtr>())
  360. font_effects = &effects->list;
  361. }
  362. // Request a font layer configuration to match this set of effects. If this is different from
  363. // our old configuration, then return true to indicate we'll need to regenerate geometry.
  364. FontEffectsHandle new_font_effects_handle = GetFontEngineInterface()->PrepareFontEffects(GetFontFaceHandle(), *font_effects);
  365. if (new_font_effects_handle != font_effects_handle)
  366. {
  367. font_effects_handle = new_font_effects_handle;
  368. return true;
  369. }
  370. return false;
  371. }
  372. void ElementText::GenerateGeometry(const FontFaceHandle font_face_handle)
  373. {
  374. RMLUI_ZoneScopedC(0xD2691E);
  375. // Release the old geometry ...
  376. for (size_t i = 0; i < geometry.size(); ++i)
  377. geometry[i].Release(true);
  378. // ... and generate it all again!
  379. for (size_t i = 0; i < lines.size(); ++i)
  380. GenerateGeometry(font_face_handle, lines[i]);
  381. generated_decoration = Style::TextDecoration::None;
  382. geometry_dirty = false;
  383. }
  384. void ElementText::GenerateGeometry(const FontFaceHandle font_face_handle, Line& line)
  385. {
  386. const float letter_spacing = GetComputedValues().letter_spacing();
  387. line.width = GetFontEngineInterface()->GenerateString(font_face_handle, font_effects_handle, line.text, line.position, colour, opacity,
  388. letter_spacing, geometry);
  389. }
  390. void ElementText::GenerateDecoration(const FontFaceHandle font_face_handle)
  391. {
  392. RMLUI_ZoneScopedC(0xA52A2A);
  393. RMLUI_ASSERT(decoration);
  394. const FontMetrics& metrics = GetFontEngineInterface()->GetFontMetrics(font_face_handle);
  395. float offset = 0.f;
  396. switch (decoration_property)
  397. {
  398. case Style::TextDecoration::Underline: offset = metrics.underline_position; break;
  399. case Style::TextDecoration::Overline: offset = -1.1f * metrics.ascent; break;
  400. case Style::TextDecoration::LineThrough: offset = -0.65f * metrics.x_height; break;
  401. case Style::TextDecoration::None: return;
  402. }
  403. for (const Line& line : lines)
  404. {
  405. const Vector2f position = {line.position.x, line.position.y + offset};
  406. const Vector2f size = {(float)line.width, metrics.underline_thickness};
  407. GeometryUtilities::GenerateLine(decoration.get(), position, size, colour);
  408. }
  409. }
  410. static bool BuildToken(String& token, const char*& token_begin, const char* string_end, bool first_token, bool collapse_white_space,
  411. bool break_at_endline, Style::TextTransform text_transformation, bool decode_escape_characters)
  412. {
  413. RMLUI_ASSERT(token_begin != string_end);
  414. token.reserve(string_end - token_begin + token.size());
  415. // Check what the first character of the token is; all we need to know is if it is white-space or not.
  416. bool parsing_white_space = StringUtilities::IsWhitespace(*token_begin);
  417. // Loop through the string from the token's beginning until we find an end to the token. This can occur in various
  418. // places, depending on the white-space processing;
  419. // - at the end of a section of non-white-space characters,
  420. // - at the end of a section of white-space characters, if we're not collapsing white-space,
  421. // - at an endline token, if we're breaking on endlines.
  422. while (token_begin != string_end)
  423. {
  424. bool force_non_whitespace = false;
  425. char character = *token_begin;
  426. const char* escape_begin = token_begin;
  427. // Check for an ampersand; if we find one, we've got an HTML escaped character.
  428. if (decode_escape_characters && character == '&')
  429. {
  430. // Find the terminating ';'.
  431. while (token_begin != string_end && *token_begin != ';')
  432. ++token_begin;
  433. // If we couldn't find the ';', print the token like normal text.
  434. if (token_begin == string_end)
  435. {
  436. token_begin = escape_begin;
  437. }
  438. // We could find a ';', parse the escape code. If the escape code is recognised, set the parsed character
  439. // to the appropriate one. If it is a non-breaking space, prevent it being picked up as whitespace. If it
  440. // is not recognised, print the token like normal text.
  441. else
  442. {
  443. String escape_code(escape_begin + 1, token_begin);
  444. if (escape_code == "lt")
  445. character = '<';
  446. else if (escape_code == "gt")
  447. character = '>';
  448. else if (escape_code == "amp")
  449. character = '&';
  450. else if (escape_code == "quot")
  451. character = '"';
  452. else if (escape_code == "nbsp")
  453. {
  454. character = ' ';
  455. force_non_whitespace = true;
  456. }
  457. else
  458. token_begin = escape_begin;
  459. }
  460. }
  461. // Check for an endline token; if we're breaking on endlines and we find one, then return true to indicate a
  462. // forced break.
  463. if (break_at_endline && character == '\n')
  464. {
  465. token += '\n';
  466. token_begin++;
  467. return true;
  468. }
  469. // If we've transitioned from white-space characters to non-white-space characters, or vice-versa, then check
  470. // if should terminate the token; if we're not collapsing white-space, then yes (as sections of white-space are
  471. // non-breaking), otherwise only if we've transitioned from characters to white-space.
  472. bool white_space = !force_non_whitespace && StringUtilities::IsWhitespace(character);
  473. if (white_space != parsing_white_space)
  474. {
  475. if (!collapse_white_space)
  476. {
  477. // Restore pointer to the beginning of the escaped token, if we processed an escape code.
  478. token_begin = escape_begin;
  479. return false;
  480. }
  481. // We're collapsing white-space; we only tokenise words, not white-space, so we're only done tokenising
  482. // once we've begun parsing non-white-space and then found white-space.
  483. if (!parsing_white_space)
  484. {
  485. // However, if we are the last non-whitespace character in the string, and there are trailing
  486. // whitespace characters after this token, then we need to append a single space to the end of this
  487. // token.
  488. if (token_begin != string_end && LastToken(token_begin, string_end, collapse_white_space, break_at_endline))
  489. token += ' ';
  490. return false;
  491. }
  492. // We've transitioned from white-space to non-white-space, so we append a single white-space character.
  493. if (!first_token)
  494. token += ' ';
  495. parsing_white_space = false;
  496. }
  497. // If the current character is white-space, we'll append a space character to the token if we're not collapsing
  498. // sections of white-space.
  499. if (white_space)
  500. {
  501. if (!collapse_white_space)
  502. token += ' ';
  503. }
  504. else
  505. {
  506. if (text_transformation == Style::TextTransform::Uppercase)
  507. {
  508. if (character >= 'a' && character <= 'z')
  509. character += ('A' - 'a');
  510. }
  511. else if (text_transformation == Style::TextTransform::Lowercase)
  512. {
  513. if (character >= 'A' && character <= 'Z')
  514. character -= ('A' - 'a');
  515. }
  516. token += character;
  517. }
  518. ++token_begin;
  519. }
  520. return false;
  521. }
  522. static bool LastToken(const char* token_begin, const char* string_end, bool collapse_white_space, bool break_at_endline)
  523. {
  524. bool last_token = (token_begin == string_end);
  525. if (collapse_white_space && !last_token)
  526. {
  527. last_token = true;
  528. const char* character = token_begin;
  529. while (character != string_end)
  530. {
  531. if (!StringUtilities::IsWhitespace(*character) || (break_at_endline && *character == '\n'))
  532. {
  533. last_token = false;
  534. break;
  535. }
  536. character++;
  537. }
  538. }
  539. return last_token;
  540. }
  541. } // namespace Rml