1 /** 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 3 * (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 * 20 */ 21 22 #include "config.h" 23 #include "core/rendering/RenderTextControl.h" 24 25 #include "core/html/HTMLTextFormControlElement.h" 26 #include "core/rendering/HitTestResult.h" 27 #include "core/rendering/RenderTheme.h" 28 #include "platform/scroll/ScrollbarTheme.h" 29 #include "wtf/unicode/CharacterNames.h" 30 31 using namespace std; 32 33 namespace WebCore { 34 35 RenderTextControl::RenderTextControl(HTMLTextFormControlElement* element) 36 : RenderBlockFlow(element) 37 { 38 ASSERT(element); 39 } 40 41 RenderTextControl::~RenderTextControl() 42 { 43 } 44 45 HTMLTextFormControlElement* RenderTextControl::textFormControlElement() const 46 { 47 return toHTMLTextFormControlElement(node()); 48 } 49 50 HTMLElement* RenderTextControl::innerTextElement() const 51 { 52 return textFormControlElement()->innerTextElement(); 53 } 54 55 void RenderTextControl::addChild(RenderObject* newChild, RenderObject* beforeChild) 56 { 57 // FIXME: This is a terrible hack to get the caret over the placeholder text since it'll 58 // make us paint the placeholder first. (See https://trac.webkit.org/changeset/118733) 59 Node* node = newChild->node(); 60 if (node && node->isElementNode() && toElement(node)->pseudo() == "-webkit-input-placeholder") 61 RenderBlock::addChild(newChild, firstChild()); 62 else 63 RenderBlock::addChild(newChild, beforeChild); 64 } 65 66 void RenderTextControl::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) 67 { 68 RenderBlock::styleDidChange(diff, oldStyle); 69 Element* innerText = innerTextElement(); 70 if (!innerText) 71 return; 72 RenderBlock* innerTextRenderer = toRenderBlock(innerText->renderer()); 73 if (innerTextRenderer) { 74 // We may have set the width and the height in the old style in layout(). 75 // Reset them now to avoid getting a spurious layout hint. 76 innerTextRenderer->style()->setHeight(Length()); 77 innerTextRenderer->style()->setWidth(Length()); 78 innerTextRenderer->setStyle(createInnerTextStyle(style())); 79 innerText->setNeedsStyleRecalc(); 80 } 81 textFormControlElement()->updatePlaceholderVisibility(false); 82 } 83 84 static inline void updateUserModifyProperty(HTMLTextFormControlElement* node, RenderStyle* style) 85 { 86 style->setUserModify(node->isDisabledOrReadOnly() ? READ_ONLY : READ_WRITE_PLAINTEXT_ONLY); 87 } 88 89 void RenderTextControl::adjustInnerTextStyle(RenderStyle* textBlockStyle) const 90 { 91 // The inner block, if present, always has its direction set to LTR, 92 // so we need to inherit the direction and unicode-bidi style from the element. 93 textBlockStyle->setDirection(style()->direction()); 94 textBlockStyle->setUnicodeBidi(style()->unicodeBidi()); 95 96 updateUserModifyProperty(textFormControlElement(), textBlockStyle); 97 } 98 99 int RenderTextControl::textBlockLogicalHeight() const 100 { 101 return logicalHeight() - borderAndPaddingLogicalHeight(); 102 } 103 104 int RenderTextControl::textBlockLogicalWidth() const 105 { 106 Element* innerText = innerTextElement(); 107 ASSERT(innerText); 108 109 LayoutUnit unitWidth = logicalWidth() - borderAndPaddingLogicalWidth(); 110 if (innerText->renderer()) 111 unitWidth -= innerText->renderBox()->paddingStart() + innerText->renderBox()->paddingEnd(); 112 113 return unitWidth; 114 } 115 116 void RenderTextControl::updateFromElement() 117 { 118 Element* innerText = innerTextElement(); 119 if (innerText && innerText->renderer()) 120 updateUserModifyProperty(textFormControlElement(), innerText->renderer()->style()); 121 } 122 123 int RenderTextControl::scrollbarThickness() const 124 { 125 // FIXME: We should get the size of the scrollbar from the RenderTheme instead. 126 return ScrollbarTheme::theme()->scrollbarThickness(); 127 } 128 129 void RenderTextControl::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const 130 { 131 HTMLElement* innerText = innerTextElement(); 132 ASSERT(innerText); 133 if (RenderBox* innerTextBox = innerText->renderBox()) { 134 LayoutUnit nonContentHeight = innerTextBox->borderAndPaddingHeight() + innerTextBox->marginHeight(); 135 logicalHeight = computeControlLogicalHeight(innerTextBox->lineHeight(true, HorizontalLine, PositionOfInteriorLineBoxes), nonContentHeight) + borderAndPaddingHeight(); 136 137 // We are able to have a horizontal scrollbar if the overflow style is scroll, or if its auto and there's no word wrap. 138 if ((isHorizontalWritingMode() && (style()->overflowX() == OSCROLL || (style()->overflowX() == OAUTO && innerText->renderer()->style()->overflowWrap() == NormalOverflowWrap))) 139 || (!isHorizontalWritingMode() && (style()->overflowY() == OSCROLL || (style()->overflowY() == OAUTO && innerText->renderer()->style()->overflowWrap() == NormalOverflowWrap)))) 140 logicalHeight += scrollbarThickness(); 141 } 142 143 RenderBox::computeLogicalHeight(logicalHeight, logicalTop, computedValues); 144 } 145 146 void RenderTextControl::hitInnerTextElement(HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset) 147 { 148 HTMLElement* innerText = innerTextElement(); 149 if (!innerText->renderer()) 150 return; 151 152 LayoutPoint adjustedLocation = accumulatedOffset + location(); 153 LayoutPoint localPoint = pointInContainer - toLayoutSize(adjustedLocation + innerText->renderBox()->location()); 154 if (hasOverflowClip()) 155 localPoint += scrolledContentOffset(); 156 result.setInnerNode(innerText); 157 result.setInnerNonSharedNode(innerText); 158 result.setLocalPoint(localPoint); 159 } 160 161 static const char* const fontFamiliesWithInvalidCharWidth[] = { 162 "American Typewriter", 163 "Arial Hebrew", 164 "Chalkboard", 165 "Cochin", 166 "Corsiva Hebrew", 167 "Courier", 168 "Euphemia UCAS", 169 "Geneva", 170 "Gill Sans", 171 "Hei", 172 "Helvetica", 173 "Hoefler Text", 174 "InaiMathi", 175 "Kai", 176 "Lucida Grande", 177 "Marker Felt", 178 "Monaco", 179 "Mshtakan", 180 "New Peninim MT", 181 "Osaka", 182 "Raanana", 183 "STHeiti", 184 "Symbol", 185 "Times", 186 "Apple Braille", 187 "Apple LiGothic", 188 "Apple LiSung", 189 "Apple Symbols", 190 "AppleGothic", 191 "AppleMyungjo", 192 "#GungSeo", 193 "#HeadLineA", 194 "#PCMyungjo", 195 "#PilGi", 196 }; 197 198 // For font families where any of the fonts don't have a valid entry in the OS/2 table 199 // for avgCharWidth, fallback to the legacy webkit behavior of getting the avgCharWidth 200 // from the width of a '0'. This only seems to apply to a fixed number of Mac fonts, 201 // but, in order to get similar rendering across platforms, we do this check for 202 // all platforms. 203 bool RenderTextControl::hasValidAvgCharWidth(AtomicString family) 204 { 205 static HashSet<AtomicString>* fontFamiliesWithInvalidCharWidthMap = 0; 206 207 if (family.isEmpty()) 208 return false; 209 210 if (!fontFamiliesWithInvalidCharWidthMap) { 211 fontFamiliesWithInvalidCharWidthMap = new HashSet<AtomicString>; 212 213 for (size_t i = 0; i < WTF_ARRAY_LENGTH(fontFamiliesWithInvalidCharWidth); ++i) 214 fontFamiliesWithInvalidCharWidthMap->add(AtomicString(fontFamiliesWithInvalidCharWidth[i])); 215 } 216 217 return !fontFamiliesWithInvalidCharWidthMap->contains(family); 218 } 219 220 float RenderTextControl::getAvgCharWidth(AtomicString family) 221 { 222 if (hasValidAvgCharWidth(family)) 223 return roundf(style()->font().primaryFont()->avgCharWidth()); 224 225 const UChar ch = '0'; 226 const String str = String(&ch, 1); 227 const Font& font = style()->font(); 228 TextRun textRun = constructTextRun(this, font, str, style(), TextRun::AllowTrailingExpansion); 229 textRun.disableRoundingHacks(); 230 return font.width(textRun); 231 } 232 233 float RenderTextControl::scaleEmToUnits(int x) const 234 { 235 // This matches the unitsPerEm value for MS Shell Dlg and Courier New from the "head" font table. 236 float unitsPerEm = 2048.0f; 237 return roundf(style()->font().size() * x / unitsPerEm); 238 } 239 240 void RenderTextControl::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const 241 { 242 // Use average character width. Matches IE. 243 AtomicString family = style()->font().family().family(); 244 maxLogicalWidth = preferredContentLogicalWidth(const_cast<RenderTextControl*>(this)->getAvgCharWidth(family)); 245 if (RenderBox* innerTextRenderBox = innerTextElement()->renderBox()) 246 maxLogicalWidth += innerTextRenderBox->paddingStart() + innerTextRenderBox->paddingEnd(); 247 if (!style()->logicalWidth().isPercent()) 248 minLogicalWidth = maxLogicalWidth; 249 } 250 251 void RenderTextControl::computePreferredLogicalWidths() 252 { 253 ASSERT(preferredLogicalWidthsDirty()); 254 255 m_minPreferredLogicalWidth = 0; 256 m_maxPreferredLogicalWidth = 0; 257 258 if (style()->logicalWidth().isFixed() && style()->logicalWidth().value() >= 0) 259 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(style()->logicalWidth().value()); 260 else 261 computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth); 262 263 if (style()->logicalMinWidth().isFixed() && style()->logicalMinWidth().value() > 0) { 264 m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMinWidth().value())); 265 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMinWidth().value())); 266 } 267 268 if (style()->logicalMaxWidth().isFixed()) { 269 m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMaxWidth().value())); 270 m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMaxWidth().value())); 271 } 272 273 LayoutUnit toAdd = borderAndPaddingLogicalWidth(); 274 275 m_minPreferredLogicalWidth += toAdd; 276 m_maxPreferredLogicalWidth += toAdd; 277 278 clearPreferredLogicalWidthsDirty(); 279 } 280 281 void RenderTextControl::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*) 282 { 283 if (!size().isEmpty()) 284 rects.append(pixelSnappedIntRect(additionalOffset, size())); 285 } 286 287 RenderObject* RenderTextControl::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope& layoutScope) 288 { 289 HTMLElement* placeholder = toHTMLTextFormControlElement(node())->placeholderElement(); 290 RenderObject* placeholderRenderer = placeholder ? placeholder->renderer() : 0; 291 if (!placeholderRenderer) 292 return 0; 293 if (relayoutChildren) 294 layoutScope.setChildNeedsLayout(placeholderRenderer); 295 return placeholderRenderer; 296 } 297 298 } // namespace WebCore 299