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/platform/ScrollbarTheme.h" 27 #include "core/rendering/HitTestResult.h" 28 #include "core/rendering/RenderTheme.h" 29 #include "wtf/unicode/CharacterNames.h" 30 31 using namespace std; 32 33 namespace WebCore { 34 35 RenderTextControl::RenderTextControl(HTMLTextFormControlElement* element) 36 : RenderBlock(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::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle) 56 { 57 RenderBlock::styleDidChange(diff, oldStyle); 58 Element* innerText = innerTextElement(); 59 if (!innerText) 60 return; 61 RenderBlock* innerTextRenderer = toRenderBlock(innerText->renderer()); 62 if (innerTextRenderer) { 63 // We may have set the width and the height in the old style in layout(). 64 // Reset them now to avoid getting a spurious layout hint. 65 innerTextRenderer->style()->setHeight(Length()); 66 innerTextRenderer->style()->setWidth(Length()); 67 innerTextRenderer->setStyle(createInnerTextStyle(style())); 68 innerText->setNeedsStyleRecalc(); 69 } 70 textFormControlElement()->updatePlaceholderVisibility(false); 71 } 72 73 static inline void updateUserModifyProperty(HTMLTextFormControlElement* node, RenderStyle* style) 74 { 75 style->setUserModify(node->isDisabledOrReadOnly() ? READ_ONLY : READ_WRITE_PLAINTEXT_ONLY); 76 } 77 78 void RenderTextControl::adjustInnerTextStyle(RenderStyle* textBlockStyle) const 79 { 80 // The inner block, if present, always has its direction set to LTR, 81 // so we need to inherit the direction and unicode-bidi style from the element. 82 textBlockStyle->setDirection(style()->direction()); 83 textBlockStyle->setUnicodeBidi(style()->unicodeBidi()); 84 85 updateUserModifyProperty(textFormControlElement(), textBlockStyle); 86 } 87 88 int RenderTextControl::textBlockLogicalHeight() const 89 { 90 return logicalHeight() - borderAndPaddingLogicalHeight(); 91 } 92 93 int RenderTextControl::textBlockLogicalWidth() const 94 { 95 Element* innerText = innerTextElement(); 96 ASSERT(innerText); 97 98 LayoutUnit unitWidth = logicalWidth() - borderAndPaddingLogicalWidth(); 99 if (innerText->renderer()) 100 unitWidth -= innerText->renderBox()->paddingStart() + innerText->renderBox()->paddingEnd(); 101 102 return unitWidth; 103 } 104 105 void RenderTextControl::updateFromElement() 106 { 107 Element* innerText = innerTextElement(); 108 if (innerText && innerText->renderer()) 109 updateUserModifyProperty(textFormControlElement(), innerText->renderer()->style()); 110 } 111 112 int RenderTextControl::scrollbarThickness() const 113 { 114 // FIXME: We should get the size of the scrollbar from the RenderTheme instead. 115 return ScrollbarTheme::theme()->scrollbarThickness(); 116 } 117 118 void RenderTextControl::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const 119 { 120 HTMLElement* innerText = innerTextElement(); 121 ASSERT(innerText); 122 if (RenderBox* innerTextBox = innerText->renderBox()) { 123 LayoutUnit nonContentHeight = innerTextBox->borderAndPaddingHeight() + innerTextBox->marginHeight(); 124 logicalHeight = computeControlLogicalHeight(innerTextBox->lineHeight(true, HorizontalLine, PositionOfInteriorLineBoxes), nonContentHeight) + borderAndPaddingHeight(); 125 126 // We are able to have a horizontal scrollbar if the overflow style is scroll, or if its auto and there's no word wrap. 127 if ((isHorizontalWritingMode() && (style()->overflowX() == OSCROLL || (style()->overflowX() == OAUTO && innerText->renderer()->style()->overflowWrap() == NormalOverflowWrap))) 128 || (!isHorizontalWritingMode() && (style()->overflowY() == OSCROLL || (style()->overflowY() == OAUTO && innerText->renderer()->style()->overflowWrap() == NormalOverflowWrap)))) 129 logicalHeight += scrollbarThickness(); 130 } 131 132 RenderBox::computeLogicalHeight(logicalHeight, logicalTop, computedValues); 133 } 134 135 void RenderTextControl::hitInnerTextElement(HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset) 136 { 137 HTMLElement* innerText = innerTextElement(); 138 if (!innerText->renderer()) 139 return; 140 141 LayoutPoint adjustedLocation = accumulatedOffset + location(); 142 LayoutPoint localPoint = pointInContainer - toLayoutSize(adjustedLocation + innerText->renderBox()->location()); 143 if (hasOverflowClip()) 144 localPoint += scrolledContentOffset(); 145 result.setInnerNode(innerText); 146 result.setInnerNonSharedNode(innerText); 147 result.setLocalPoint(localPoint); 148 } 149 150 static const char* fontFamiliesWithInvalidCharWidth[] = { 151 "American Typewriter", 152 "Arial Hebrew", 153 "Chalkboard", 154 "Cochin", 155 "Corsiva Hebrew", 156 "Courier", 157 "Euphemia UCAS", 158 "Geneva", 159 "Gill Sans", 160 "Hei", 161 "Helvetica", 162 "Hoefler Text", 163 "InaiMathi", 164 "Kai", 165 "Lucida Grande", 166 "Marker Felt", 167 "Monaco", 168 "Mshtakan", 169 "New Peninim MT", 170 "Osaka", 171 "Raanana", 172 "STHeiti", 173 "Symbol", 174 "Times", 175 "Apple Braille", 176 "Apple LiGothic", 177 "Apple LiSung", 178 "Apple Symbols", 179 "AppleGothic", 180 "AppleMyungjo", 181 "#GungSeo", 182 "#HeadLineA", 183 "#PCMyungjo", 184 "#PilGi", 185 }; 186 187 // For font families where any of the fonts don't have a valid entry in the OS/2 table 188 // for avgCharWidth, fallback to the legacy webkit behavior of getting the avgCharWidth 189 // from the width of a '0'. This only seems to apply to a fixed number of Mac fonts, 190 // but, in order to get similar rendering across platforms, we do this check for 191 // all platforms. 192 bool RenderTextControl::hasValidAvgCharWidth(AtomicString family) 193 { 194 static HashSet<AtomicString>* fontFamiliesWithInvalidCharWidthMap = 0; 195 196 if (family.isEmpty()) 197 return false; 198 199 if (!fontFamiliesWithInvalidCharWidthMap) { 200 fontFamiliesWithInvalidCharWidthMap = new HashSet<AtomicString>; 201 202 for (size_t i = 0; i < WTF_ARRAY_LENGTH(fontFamiliesWithInvalidCharWidth); ++i) 203 fontFamiliesWithInvalidCharWidthMap->add(AtomicString(fontFamiliesWithInvalidCharWidth[i])); 204 } 205 206 return !fontFamiliesWithInvalidCharWidthMap->contains(family); 207 } 208 209 float RenderTextControl::getAvgCharWidth(AtomicString family) 210 { 211 if (hasValidAvgCharWidth(family)) 212 return roundf(style()->font().primaryFont()->avgCharWidth()); 213 214 const UChar ch = '0'; 215 const String str = String(&ch, 1); 216 const Font& font = style()->font(); 217 TextRun textRun = constructTextRun(this, font, str, style(), TextRun::AllowTrailingExpansion); 218 textRun.disableRoundingHacks(); 219 return font.width(textRun); 220 } 221 222 float RenderTextControl::scaleEmToUnits(int x) const 223 { 224 // This matches the unitsPerEm value for MS Shell Dlg and Courier New from the "head" font table. 225 float unitsPerEm = 2048.0f; 226 return roundf(style()->font().size() * x / unitsPerEm); 227 } 228 229 void RenderTextControl::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const 230 { 231 // Use average character width. Matches IE. 232 AtomicString family = style()->font().family().family(); 233 maxLogicalWidth = preferredContentLogicalWidth(const_cast<RenderTextControl*>(this)->getAvgCharWidth(family)); 234 if (RenderBox* innerTextRenderBox = innerTextElement()->renderBox()) 235 maxLogicalWidth += innerTextRenderBox->paddingStart() + innerTextRenderBox->paddingEnd(); 236 if (!style()->logicalWidth().isPercent()) 237 minLogicalWidth = maxLogicalWidth; 238 } 239 240 void RenderTextControl::computePreferredLogicalWidths() 241 { 242 ASSERT(preferredLogicalWidthsDirty()); 243 244 m_minPreferredLogicalWidth = 0; 245 m_maxPreferredLogicalWidth = 0; 246 247 if (style()->logicalWidth().isFixed() && style()->logicalWidth().value() >= 0) 248 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(style()->logicalWidth().value()); 249 else 250 computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth); 251 252 if (style()->logicalMinWidth().isFixed() && style()->logicalMinWidth().value() > 0) { 253 m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMinWidth().value())); 254 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMinWidth().value())); 255 } 256 257 if (style()->logicalMaxWidth().isFixed()) { 258 m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMaxWidth().value())); 259 m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->logicalMaxWidth().value())); 260 } 261 262 LayoutUnit toAdd = borderAndPaddingLogicalWidth(); 263 264 m_minPreferredLogicalWidth += toAdd; 265 m_maxPreferredLogicalWidth += toAdd; 266 267 setPreferredLogicalWidthsDirty(false); 268 } 269 270 void RenderTextControl::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*) 271 { 272 if (!size().isEmpty()) 273 rects.append(pixelSnappedIntRect(additionalOffset, size())); 274 } 275 276 RenderObject* RenderTextControl::layoutSpecialExcludedChild(bool relayoutChildren) 277 { 278 HTMLElement* placeholder = toHTMLTextFormControlElement(node())->placeholderElement(); 279 RenderObject* placeholderRenderer = placeholder ? placeholder->renderer() : 0; 280 if (!placeholderRenderer) 281 return 0; 282 if (relayoutChildren) { 283 // The markParents arguments should be false because this function is 284 // called from layout() of the parent and the placeholder layout doesn't 285 // affect the parent layout. 286 placeholderRenderer->setChildNeedsLayout(MarkOnlyThis); 287 } 288 return placeholderRenderer; 289 } 290 291 bool RenderTextControl::canBeReplacedWithInlineRunIn() const 292 { 293 return false; 294 } 295 296 } // namespace WebCore 297