RenderCombineText.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #include "config.h"
  21. #include "RenderCombineText.h"
  22. #include "RenderBlock.h"
  23. #include "StyleInheritedData.h"
  24. namespace WebCore {
  25. const float textCombineMargin = 1.15f; // Allow em + 15% margin
  26. RenderCombineText::RenderCombineText(Node* node, PassRefPtr<StringImpl> string)
  27. : RenderText(node, string)
  28. , m_combinedTextWidth(0)
  29. , m_isCombined(false)
  30. , m_needsFontUpdate(false)
  31. {
  32. }
  33. void RenderCombineText::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
  34. {
  35. setStyleInternal(RenderStyle::clone(style()));
  36. RenderText::styleDidChange(diff, oldStyle);
  37. if (m_isCombined) {
  38. RenderText::setTextInternal(originalText()); // This RenderCombineText has been combined once. Restore the original text for the next combineText().
  39. m_isCombined = false;
  40. }
  41. m_needsFontUpdate = true;
  42. }
  43. void RenderCombineText::setTextInternal(PassRefPtr<StringImpl> text)
  44. {
  45. RenderText::setTextInternal(text);
  46. m_needsFontUpdate = true;
  47. }
  48. float RenderCombineText::width(unsigned from, unsigned length, const Font& font, float xPosition, HashSet<const SimpleFontData*>* fallbackFonts, GlyphOverflow* glyphOverflow) const
  49. {
  50. if (!characters())
  51. return 0;
  52. if (m_isCombined)
  53. return font.size();
  54. return RenderText::width(from, length, font, xPosition, fallbackFonts, glyphOverflow);
  55. }
  56. void RenderCombineText::adjustTextOrigin(FloatPoint& textOrigin, const FloatRect& boxRect) const
  57. {
  58. if (m_isCombined)
  59. textOrigin.move(boxRect.height() / 2 - ceilf(m_combinedTextWidth) / 2, style()->font().pixelSize());
  60. }
  61. void RenderCombineText::getStringToRender(int start, String& string, int& length) const
  62. {
  63. ASSERT(start >= 0);
  64. if (m_isCombined) {
  65. string = originalText();
  66. length = string.length();
  67. return;
  68. }
  69. string = text();
  70. string = string.substringSharingImpl(static_cast<unsigned>(start), length);
  71. }
  72. void RenderCombineText::combineText()
  73. {
  74. if (!m_needsFontUpdate)
  75. return;
  76. m_isCombined = false;
  77. m_needsFontUpdate = false;
  78. // CSS3 spec says text-combine works only in vertical writing mode.
  79. if (style()->isHorizontalWritingMode())
  80. return;
  81. TextRun run = RenderBlock::constructTextRun(this, originalFont(), this, style());
  82. FontDescription description = originalFont().fontDescription();
  83. float emWidth = description.computedSize() * textCombineMargin;
  84. bool shouldUpdateFont = false;
  85. description.setOrientation(Horizontal); // We are going to draw combined text horizontally.
  86. m_combinedTextWidth = originalFont().width(run);
  87. m_isCombined = m_combinedTextWidth <= emWidth;
  88. FontSelector* fontSelector = style()->font().fontSelector();
  89. if (m_isCombined)
  90. shouldUpdateFont = style()->setFontDescription(description); // Need to change font orientation to horizontal.
  91. else {
  92. // Need to try compressed glyphs.
  93. static const FontWidthVariant widthVariants[] = { HalfWidth, ThirdWidth, QuarterWidth };
  94. for (size_t i = 0 ; i < WTF_ARRAY_LENGTH(widthVariants) ; ++i) {
  95. description.setWidthVariant(widthVariants[i]);
  96. Font compressedFont = Font(description, style()->font().letterSpacing(), style()->font().wordSpacing());
  97. compressedFont.update(fontSelector);
  98. float runWidth = compressedFont.width(run);
  99. if (runWidth <= emWidth) {
  100. m_combinedTextWidth = runWidth;
  101. m_isCombined = true;
  102. // Replace my font with the new one.
  103. shouldUpdateFont = style()->setFontDescription(description);
  104. break;
  105. }
  106. }
  107. }
  108. if (!m_isCombined)
  109. shouldUpdateFont = style()->setFontDescription(originalFont().fontDescription());
  110. if (shouldUpdateFont)
  111. style()->font().update(fontSelector);
  112. if (m_isCombined) {
  113. DEFINE_STATIC_LOCAL(String, objectReplacementCharacterString, (&objectReplacementCharacter, 1));
  114. RenderText::setTextInternal(objectReplacementCharacterString.impl());
  115. }
  116. }
  117. } // namespace WebCore