ElementText.cpp 21 KB

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