ElementTextDefault.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementTextDefault.h"
  29. #include "ElementDefinition.h"
  30. #include "ElementStyle.h"
  31. #include "FontFaceHandle.h"
  32. #include "RCSS.h"
  33. #include "../../Include/Rocket/Core/ElementDocument.h"
  34. #include "../../Include/Rocket/Core/ElementUtilities.h"
  35. #include "../../Include/Rocket/Core/Event.h"
  36. #include "../../Include/Rocket/Core/FontDatabase.h"
  37. #include "../../Include/Rocket/Core/Property.h"
  38. #include "../../Include/Rocket/Core/StyleSheetKeywords.h"
  39. namespace Rocket {
  40. namespace Core {
  41. static bool BuildToken(WString& token, const word*& token_begin, const word* string_end, bool first_token, bool collapse_white_space, bool break_at_endline, int text_transformation);
  42. static bool LastToken(const word* token_begin, const word* string_end, bool collapse_white_space, bool break_at_endline);
  43. ElementTextDefault::ElementTextDefault(const String& tag) : ElementText(tag), colour(255, 255, 255), decoration(this)
  44. {
  45. dirty_layout_on_change = true;
  46. generated_decoration = TEXT_DECORATION_NONE;
  47. decoration_property = TEXT_DECORATION_NONE;
  48. geometry_dirty = true;
  49. font_configuration = -1;
  50. font_dirty = true;
  51. }
  52. ElementTextDefault::~ElementTextDefault()
  53. {
  54. }
  55. void ElementTextDefault::SetText(const WString& _text)
  56. {
  57. if (text != _text)
  58. {
  59. text = _text;
  60. if (dirty_layout_on_change)
  61. DirtyLayout();
  62. }
  63. }
  64. const WString& ElementTextDefault::GetText() const
  65. {
  66. return text;
  67. }
  68. void ElementTextDefault::OnRender()
  69. {
  70. FontFaceHandle* font_face_handle = GetFontFaceHandle();
  71. if (!font_face_handle)
  72. return;
  73. // If our font configuration has potentially changed, update it and force a geometry
  74. // generation if necessary.
  75. if (font_dirty &&
  76. UpdateFontConfiguration())
  77. {
  78. geometry_dirty = true;
  79. }
  80. // Regenerate the geometry if the colour or font configuration has altered.
  81. if (geometry_dirty)
  82. GenerateGeometry(font_face_handle);
  83. Vector2f translation = GetAbsoluteOffset();
  84. bool render = true;
  85. Vector2i clip_origin;
  86. Vector2i clip_dimensions;
  87. if (GetContext()->GetActiveClipRegion(clip_origin, clip_dimensions))
  88. {
  89. float clip_top = (float)clip_origin.y;
  90. float clip_left = (float)clip_origin.x;
  91. float clip_right = (float)(clip_origin.x + clip_dimensions.x);
  92. float clip_bottom = (float)(clip_origin.y + clip_dimensions.y);
  93. float line_height = (float)GetFontFaceHandle()->GetLineHeight();
  94. render = false;
  95. for (size_t i = 0; i < lines.size(); ++i)
  96. {
  97. const Line& line = lines[i];
  98. float x = translation.x + line.position.x;
  99. float y = translation.y + line.position.y;
  100. bool render_line = !(x > clip_right);
  101. render_line = render_line && !(x + line.width < clip_left);
  102. render_line = render_line && !(y - line_height > clip_bottom);
  103. render_line = render_line && !(y < clip_top);
  104. if (render_line)
  105. {
  106. render = true;
  107. break;
  108. }
  109. }
  110. }
  111. translation = translation.Round();
  112. if (render)
  113. {
  114. for (size_t i = 0; i < geometry.size(); ++i)
  115. geometry[i].Render(translation);
  116. }
  117. if (decoration_property != TEXT_DECORATION_NONE)
  118. decoration.Render(translation);
  119. }
  120. // Generates a token of text from this element, returning only the width.
  121. bool ElementTextDefault::GenerateToken(float& token_width, int line_begin)
  122. {
  123. // Bail if we don't have a valid font face.
  124. FontFaceHandle* font_face_handle = GetFontFaceHandle();
  125. if (font_face_handle == NULL ||
  126. line_begin >= (int) text.size())
  127. return 0;
  128. // Determine how we are processing white-space while formatting the text.
  129. int white_space_property = GetWhitespace();
  130. bool collapse_white_space = white_space_property == WHITE_SPACE_NORMAL ||
  131. white_space_property == WHITE_SPACE_NOWRAP ||
  132. white_space_property == WHITE_SPACE_PRE_LINE;
  133. bool break_at_endline = white_space_property == WHITE_SPACE_PRE ||
  134. white_space_property == WHITE_SPACE_PRE_WRAP ||
  135. white_space_property == WHITE_SPACE_PRE_LINE;
  136. const word* token_begin = text.c_str() + line_begin;
  137. WString token;
  138. BuildToken(token, token_begin, text.c_str() + text.size(), true, collapse_white_space, break_at_endline, GetTextTransform());
  139. token_width = (float) font_face_handle->GetStringWidth(token, 0);
  140. return LastToken(token_begin, text.c_str() + text.size(), collapse_white_space, break_at_endline);
  141. }
  142. // Generates a line of text rendered from this element
  143. bool ElementTextDefault::GenerateLine(WString& line, int& line_length, float& line_width, int line_begin, float maximum_line_width, float right_spacing_width, bool trim_whitespace_prefix)
  144. {
  145. FontFaceHandle* font_face_handle = GetFontFaceHandle();
  146. // Initialise the output variables.
  147. line.clear();
  148. line_length = 0;
  149. line_width = 0;
  150. // Bail if we don't have a valid font face.
  151. if (font_face_handle == NULL)
  152. return true;
  153. // Determine how we are processing white-space while formatting the text.
  154. int white_space_property = GetWhitespace();
  155. bool collapse_white_space = white_space_property == WHITE_SPACE_NORMAL ||
  156. white_space_property == WHITE_SPACE_NOWRAP ||
  157. white_space_property == WHITE_SPACE_PRE_LINE;
  158. bool break_at_line = maximum_line_width >= 0 &&
  159. (white_space_property == WHITE_SPACE_NORMAL ||
  160. white_space_property == WHITE_SPACE_PRE_WRAP ||
  161. white_space_property == WHITE_SPACE_PRE_LINE);
  162. bool break_at_endline = white_space_property == WHITE_SPACE_PRE ||
  163. white_space_property == WHITE_SPACE_PRE_WRAP ||
  164. white_space_property == WHITE_SPACE_PRE_LINE;
  165. // Determine what (if any) text transformation we are putting the characters through.
  166. int text_transform_property = GetTextTransform();
  167. // Starting at the line_begin character, we generate sections of the text (we'll call them tokens) depending on the
  168. // white-space parsing parameters. Each section is then appended to the line if it can fit. If not, or if an
  169. // endline is found (and we're processing them), then the line is ended. kthxbai!
  170. const word* token_begin = text.c_str() + line_begin;
  171. const word* string_end = text.c_str() + text.size();
  172. while (token_begin != string_end)
  173. {
  174. WString token;
  175. const word* next_token_begin = token_begin;
  176. // Generate the next token and determine its pixel-length.
  177. bool break_line = BuildToken(token, next_token_begin, string_end, line.empty() && trim_whitespace_prefix, collapse_white_space, break_at_endline, text_transform_property);
  178. int token_width = font_face_handle->GetStringWidth(token, line.empty() ? 0 : line[line.size() - 1]);
  179. // If we're breaking to fit a line box, check if the token can fit on the line before we add it.
  180. if (break_at_line)
  181. {
  182. if (!line.empty() &&
  183. (line_width + token_width > maximum_line_width ||
  184. LastToken(next_token_begin, string_end, collapse_white_space, break_at_endline) && line_width + token_width > maximum_line_width - right_spacing_width))
  185. {
  186. return false;
  187. }
  188. }
  189. // The token can fit on the end of the line, so add it onto the end and increment our width and length
  190. // counters.
  191. line += token;
  192. line_length += (int)(next_token_begin - token_begin);
  193. line_width += token_width;
  194. // Break out of the loop if an endline was forced.
  195. if (break_line)
  196. return false;
  197. // Set the beginning of the next token.
  198. token_begin = next_token_begin;
  199. }
  200. return true;
  201. }
  202. // Clears all lines of generated text and prepares the element for generating new lines.
  203. void ElementTextDefault::ClearLines()
  204. {
  205. // Clear the rendering information.
  206. for (size_t i = 0; i < geometry.size(); ++i)
  207. geometry[i].Release(true);
  208. lines.clear();
  209. decoration.Release(true);
  210. }
  211. // Adds a new line into the text element.
  212. void ElementTextDefault::AddLine(const Vector2f& line_position, const WString& line)
  213. {
  214. FontFaceHandle* font_face_handle = GetFontFaceHandle();
  215. if (font_face_handle == NULL)
  216. return;
  217. if (font_dirty)
  218. UpdateFontConfiguration();
  219. Vector2f baseline_position = line_position + Vector2f(0.0f, (float) font_face_handle->GetLineHeight() - font_face_handle->GetBaseline());
  220. lines.push_back(Line(line, baseline_position));
  221. GenerateGeometry(font_face_handle, lines.back());
  222. geometry_dirty = false;
  223. if (decoration_property != TEXT_DECORATION_NONE)
  224. GenerateDecoration(font_face_handle, lines.back());
  225. }
  226. // Prevents the element from dirtying its document's layout when its text is changed.
  227. void ElementTextDefault::SuppressAutoLayout()
  228. {
  229. dirty_layout_on_change = false;
  230. }
  231. void ElementTextDefault::OnPropertyChange(const PropertyNameList& changed_properties)
  232. {
  233. Element::OnPropertyChange(changed_properties);
  234. bool colour_changed = false;
  235. bool font_face_changed = false;
  236. auto& computed = GetComputedValues();
  237. if (changed_properties.find(COLOR) != changed_properties.end() ||
  238. changed_properties.find(OPACITY) != changed_properties.end())
  239. {
  240. // Fetch our (potentially) new colour.
  241. Colourb new_colour = computed.color;
  242. float opacity = computed.opacity;
  243. new_colour.alpha = byte(opacity * float(new_colour.alpha));
  244. colour_changed = colour != new_colour;
  245. if (colour_changed)
  246. colour = new_colour;
  247. }
  248. if (changed_properties.find(FONT_FAMILY) != changed_properties.end() ||
  249. changed_properties.find(FONT_CHARSET) != changed_properties.end() ||
  250. changed_properties.find(FONT_WEIGHT) != changed_properties.end() ||
  251. changed_properties.find(FONT_STYLE) != changed_properties.end() ||
  252. changed_properties.find(FONT_SIZE) != changed_properties.end())
  253. {
  254. font_face_changed = true;
  255. geometry.clear();
  256. font_dirty = true;
  257. }
  258. if (changed_properties.find(TEXT_DECORATION) != changed_properties.end())
  259. {
  260. decoration_property = (int)computed.text_decoration;
  261. if (decoration_property != TEXT_DECORATION_NONE)
  262. {
  263. if (decoration_property != generated_decoration)
  264. {
  265. decoration.Release(true);
  266. FontFaceHandle* font_face_handle = GetFontFaceHandle();
  267. if (font_face_handle != NULL)
  268. {
  269. for (size_t i = 0; i < lines.size(); ++i)
  270. GenerateDecoration(font_face_handle, lines[i]);
  271. }
  272. generated_decoration = decoration_property;
  273. }
  274. }
  275. }
  276. if (font_face_changed)
  277. {
  278. // We have to let our document know we need to be regenerated.
  279. if (dirty_layout_on_change)
  280. DirtyLayout();
  281. }
  282. else if (colour_changed)
  283. {
  284. // Force the geometry to be regenerated.
  285. geometry_dirty = true;
  286. // Re-colour the decoration geometry.
  287. std::vector< Vertex >& vertices = decoration.GetVertices();
  288. for (size_t i = 0; i < vertices.size(); ++i)
  289. vertices[i].colour = colour;
  290. decoration.Release();
  291. }
  292. }
  293. // Returns the RML of this element
  294. void ElementTextDefault::GetRML(String& content)
  295. {
  296. content += ToUTF8(text);
  297. }
  298. // Forces a reevaluation of applicable font effects.
  299. void ElementTextDefault::DirtyFont()
  300. {
  301. font_dirty = true;
  302. }
  303. // Updates the configuration this element uses for its font.
  304. bool ElementTextDefault::UpdateFontConfiguration()
  305. {
  306. if (GetFontFaceHandle() == NULL)
  307. return false;
  308. font_dirty = false;
  309. // Build up a list of all applicable font effects set by our parent nodes.
  310. FontEffectMap font_effects;
  311. Element* element = GetParentNode();
  312. while (element != NULL)
  313. {
  314. const ElementDefinition* element_definition = element->GetDefinition();
  315. if (element_definition != NULL)
  316. element_definition->GetFontEffects(font_effects, element->GetStyle()->GetActivePseudoClasses());
  317. element = element->GetParentNode();
  318. }
  319. // Request a font layer configuration to match this set of effects. If this is different from
  320. // our old configuration, then return true to indicate we'll need to regenerate geometry.
  321. int new_configuration = GetFontFaceHandle()->GenerateLayerConfiguration(font_effects);
  322. if (new_configuration != font_configuration)
  323. {
  324. font_configuration = new_configuration;
  325. return true;
  326. }
  327. return false;
  328. }
  329. // Clears and regenerates all of the text's geometry.
  330. void ElementTextDefault::GenerateGeometry(FontFaceHandle* font_face_handle)
  331. {
  332. // Release the old geometry ...
  333. for (size_t i = 0; i < geometry.size(); ++i)
  334. geometry[i].Release(true);
  335. /// ... and generate it all again!
  336. for (size_t i = 0; i < lines.size(); ++i)
  337. GenerateGeometry(font_face_handle, lines[i]);
  338. geometry_dirty = false;
  339. }
  340. void ElementTextDefault::GenerateGeometry(FontFaceHandle* font_face_handle, Line& line)
  341. {
  342. line.width = font_face_handle->GenerateString(geometry, line.text, line.position, colour, font_configuration);
  343. for (size_t i = 0; i < geometry.size(); ++i)
  344. geometry[i].SetHostElement(this);
  345. }
  346. // Generates any geometry necessary for rendering a line decoration (underline, strike-through, etc).
  347. void ElementTextDefault::GenerateDecoration(FontFaceHandle* font_face_handle, const Line& line)
  348. {
  349. Font::Line line_height;
  350. if (decoration_property == TEXT_DECORATION_OVERLINE)
  351. line_height = Font::OVERLINE;
  352. else if (decoration_property == TEXT_DECORATION_LINE_THROUGH)
  353. line_height = Font::STRIKE_THROUGH;
  354. else
  355. line_height = Font::UNDERLINE;
  356. font_face_handle->GenerateLine(&decoration, line.position, line.width, line_height, colour);
  357. }
  358. static bool BuildToken(WString& token, const word*& token_begin, const word* string_end, bool first_token, bool collapse_white_space, bool break_at_endline, int text_transformation)
  359. {
  360. ROCKET_ASSERT(token_begin != string_end);
  361. token.reserve(string_end - token_begin + token.size());
  362. // Check what the first character of the token is; all we need to know is if it is white-space or not.
  363. bool parsing_white_space = StringUtilities::IsWhitespace(*token_begin);
  364. // Loop through the string from the token's beginning until we find an end to the token. This can occur in various
  365. // places, depending on the white-space processing;
  366. // - at the end of a section of non-white-space characters,
  367. // - at the end of a section of white-space characters, if we're not collapsing white-space,
  368. // - at an endline token, if we're breaking on endlines.
  369. while (token_begin != string_end)
  370. {
  371. bool force_non_whitespace = false;
  372. word character = *token_begin;
  373. const word* escape_begin = token_begin;
  374. // Check for an ampersand; if we find one, we've got an HTML escaped character.
  375. if (character == '&')
  376. {
  377. // Find the terminating ';'.
  378. while (token_begin != string_end &&
  379. *token_begin != ';')
  380. ++token_begin;
  381. // If we couldn't find the ';', print the token like normal text.
  382. if (token_begin == string_end)
  383. {
  384. token_begin = escape_begin;
  385. }
  386. // We could find a ';', parse the escape code. If the escape code is recognised, set the parsed character
  387. // to the appropriate one. If it is a non-breaking space, prevent it being picked up as whitespace. If it
  388. // is not recognised, print the token like normal text.
  389. else
  390. {
  391. WString ucs2_escape_code(escape_begin + 1, token_begin);
  392. if (ucs2_escape_code == L"lt")
  393. character = '<';
  394. else if (ucs2_escape_code == L"gt")
  395. character = '>';
  396. else if (ucs2_escape_code == L"amp")
  397. character = '&';
  398. else if (ucs2_escape_code == L"quot")
  399. character = '"';
  400. else if (ucs2_escape_code == L"nbsp")
  401. {
  402. character = ' ';
  403. force_non_whitespace = true;
  404. }
  405. else
  406. token_begin = escape_begin;
  407. }
  408. }
  409. // Check for an endline token; if we're breaking on endlines and we find one, then return true to indicate a
  410. // forced break.
  411. if (break_at_endline &&
  412. character == '\n')
  413. {
  414. token += '\n';
  415. token_begin++;
  416. return true;
  417. }
  418. // If we've transitioned from white-space characters to non-white-space characters, or vice-versa, then check
  419. // if should terminate the token; if we're not collapsing white-space, then yes (as sections of white-space are
  420. // non-breaking), otherwise only if we've transitioned from characters to white-space.
  421. bool white_space = !force_non_whitespace && StringUtilities::IsWhitespace(character);
  422. if (white_space != parsing_white_space)
  423. {
  424. if (!collapse_white_space)
  425. {
  426. // Restore pointer to the beginning of the escaped token, if we processed an escape code.
  427. token_begin = escape_begin;
  428. return false;
  429. }
  430. // We're collapsing white-space; we only tokenise words, not white-space, so we're only done tokenising
  431. // once we've begun parsing non-white-space and then found white-space.
  432. if (!parsing_white_space)
  433. {
  434. // However, if we are the last non-whitespace character in the string, and there are trailing
  435. // whitespace characters after this token, then we need to append a single space to the end of this
  436. // token.
  437. if (token_begin != string_end &&
  438. LastToken(token_begin, string_end, collapse_white_space, break_at_endline))
  439. token += ' ';
  440. return false;
  441. }
  442. // We've transitioned from white-space to non-white-space, so we append a single white-space character.
  443. if (!first_token)
  444. token += ' ';
  445. parsing_white_space = false;
  446. }
  447. // If the current character is white-space, we'll append a space character to the token if we're not collapsing
  448. // sections of white-space.
  449. if (white_space)
  450. {
  451. if (!collapse_white_space)
  452. token += ' ';
  453. }
  454. else
  455. {
  456. if (text_transformation == TEXT_TRANSFORM_UPPERCASE)
  457. {
  458. if (character >= 'a' && character <= 'z')
  459. character += (Rocket::Core::word)('A' - 'a');
  460. }
  461. else if (text_transformation == TEXT_TRANSFORM_LOWERCASE)
  462. {
  463. if (character >= 'A' && character <= 'Z')
  464. character -= (Rocket::Core::word)('A' - 'a');
  465. }
  466. token += character;
  467. }
  468. ++token_begin;
  469. }
  470. return false;
  471. }
  472. static bool LastToken(const word* token_begin, const word* string_end, bool collapse_white_space, bool break_at_endline)
  473. {
  474. bool last_token = (token_begin == string_end);
  475. if (collapse_white_space &&
  476. !last_token)
  477. {
  478. last_token = true;
  479. const word* character = token_begin;
  480. while (character != string_end)
  481. {
  482. if (!StringUtilities::IsWhitespace(*character) ||
  483. (break_at_endline && *character == '\n'))
  484. {
  485. last_token = false;
  486. break;
  487. }
  488. character++;
  489. }
  490. }
  491. return last_token;
  492. }
  493. }
  494. }