ElementText.cpp 21 KB

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