Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(539)

Side by Side Diff: Source/core/html/canvas/CanvasRenderingContext2D.cpp

Issue 141433026: Font metrics, version 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Update to ToT Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> 6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved. 8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
9 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. 9 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
10 * 10 *
(...skipping 2107 matching lines...) Expand 10 before | Expand all | Expand 10 after
2118 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth) 2118 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth)
2119 { 2119 {
2120 drawTextInternal(text, x, y, false, maxWidth, true); 2120 drawTextInternal(text, x, y, false, maxWidth, true);
2121 } 2121 }
2122 2122
2123 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text ) 2123 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text )
2124 { 2124 {
2125 FontCachePurgePreventer fontCachePurgePreventer; 2125 FontCachePurgePreventer fontCachePurgePreventer;
2126 RefPtr<TextMetrics> metrics = TextMetrics::create(); 2126 RefPtr<TextMetrics> metrics = TextMetrics::create();
2127 canvas()->document().updateStyleIfNeeded(); 2127 canvas()->document().updateStyleIfNeeded();
2128 metrics->setWidth(accessFont().width(TextRun(text))); 2128 const Font& font = accessFont();
2129 const TextRun textRun(text);
2130 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true);
2131
2132 // x direction
2133 metrics->setWidth(font.width(textRun));
2134 metrics->setActualBoundingBoxLeft(-textBounds.x());
2135 metrics->setActualBoundingBoxRight(textBounds.maxX());
2136
2137 // y direction
2138 const FontMetrics& fontMetrics = font.fontMetrics();
2139 const float ascent = fontMetrics.floatAscent();
2140 const float descent = fontMetrics.floatDescent();
2141 const float baselineY = getFontBaseline(fontMetrics);
2142
2143 metrics->setFontBoundingBoxAscent(ascent - baselineY);
2144 metrics->setFontBoundingBoxDescent(descent + baselineY);
2145 metrics->setActualBoundingBoxAscent(-textBounds.y() - baselineY);
2146 metrics->setActualBoundingBoxDescent(textBounds.maxY() + baselineY);
2147
2148 // Note : top/bottom and ascend/descend are currently the same, so there's n o difference
2149 // between the EM box's top and bottom and the font's ascend and desc end
2150 metrics->setEmHeightAscent(0);
2151 metrics->setEmHeightDescent(0);
2152
2153 metrics->setHangingBaseline(-0.8f * ascent + baselineY);
2154 metrics->setAlphabeticBaseline(baselineY);
2155 metrics->setIdeographicBaseline(descent + baselineY);
2129 return metrics.release(); 2156 return metrics.release();
2130 } 2157 }
2131 2158
2132 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement) 2159 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement)
2133 { 2160 {
2134 const size_t replacementLength = replacement.length(); 2161 const size_t replacementLength = replacement.length();
2135 size_t index = 0; 2162 size_t index = 0;
2136 while ((index = text.find(matchFunction, index)) != kNotFound) { 2163 while ((index = text.find(matchFunction, index)) != kNotFound) {
2137 text.replace(index, 1, replacement); 2164 text.replace(index, 1, replacement);
2138 index += replacementLength; 2165 index += replacementLength;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2175 2202
2176 // FIXME: Need to turn off font smoothing. 2203 // FIXME: Need to turn off font smoothing.
2177 2204
2178 RenderStyle* computedStyle = canvas()->computedStyle(); 2205 RenderStyle* computedStyle = canvas()->computedStyle();
2179 TextDirection direction = computedStyle ? computedStyle->direction() : LTR; 2206 TextDirection direction = computedStyle ? computedStyle->direction() : LTR;
2180 bool isRTL = direction == RTL; 2207 bool isRTL = direction == RTL;
2181 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2208 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2182 2209
2183 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding); 2210 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding);
2184 // Draw the item text at the correct point. 2211 // Draw the item text at the correct point.
2185 FloatPoint location(x, y); 2212 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2186 switch (state().m_textBaseline) {
2187 case TopTextBaseline:
2188 case HangingTextBaseline:
2189 location.setY(y + fontMetrics.ascent());
2190 break;
2191 case BottomTextBaseline:
2192 case IdeographicTextBaseline:
2193 location.setY(y - fontMetrics.descent());
2194 break;
2195 case MiddleTextBaseline:
2196 location.setY(y - fontMetrics.descent() + fontMetrics.height() / 2);
2197 break;
2198 case AlphabeticTextBaseline:
2199 default:
2200 // Do nothing.
2201 break;
2202 }
2203 2213
2204 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override)); 2214 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2205 2215
2206 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2216 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
2207 float width = useMaxWidth ? maxWidth : fontWidth; 2217 float width = useMaxWidth ? maxWidth : fontWidth;
2208 2218
2209 TextAlign align = state().m_textAlign; 2219 TextAlign align = state().m_textAlign;
2210 if (align == StartTextAlign) 2220 if (align == StartTextAlign)
2211 align = isRTL ? RightTextAlign : LeftTextAlign; 2221 align = isRTL ? RightTextAlign : LeftTextAlign;
2212 else if (align == EndTextAlign) 2222 else if (align == EndTextAlign)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2266 2276
2267 const Font& CanvasRenderingContext2D::accessFont() 2277 const Font& CanvasRenderingContext2D::accessFont()
2268 { 2278 {
2269 // This needs style to be up to date, but can't assert so because drawTextIn ternal 2279 // This needs style to be up to date, but can't assert so because drawTextIn ternal
2270 // can invalidate style before this is called (e.g. drawingContext invalidat es style). 2280 // can invalidate style before this is called (e.g. drawingContext invalidat es style).
2271 if (!state().m_realizedFont) 2281 if (!state().m_realizedFont)
2272 setFont(state().m_unparsedFont); 2282 setFont(state().m_unparsedFont);
2273 return state().m_font; 2283 return state().m_font;
2274 } 2284 }
2275 2285
2286 int CanvasRenderingContext2D::getFontBaseline(const FontMetrics& fontMetrics) co nst
2287 {
2288 switch (state().m_textBaseline) {
2289 case TopTextBaseline:
2290 return fontMetrics.ascent();
2291 case HangingTextBaseline:
2292 // According to http://wiki.apache.org/xmlgraphics-fop/LineLayout/Alignm entHandling
2293 // "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of the ascender height"
2294 return (fontMetrics.ascent() * 4) / 5;
2295 case BottomTextBaseline:
2296 case IdeographicTextBaseline:
2297 return -fontMetrics.descent();
2298 case MiddleTextBaseline:
2299 return -fontMetrics.descent() + fontMetrics.height() / 2;
2300 case AlphabeticTextBaseline:
2301 default:
2302 // Do nothing.
2303 break;
2304 }
2305 return 0;
2306 }
2307
2276 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const 2308 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const
2277 { 2309 {
2278 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0; 2310 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0;
2279 } 2311 }
2280 2312
2281 bool CanvasRenderingContext2D::imageSmoothingEnabled() const 2313 bool CanvasRenderingContext2D::imageSmoothingEnabled() const
2282 { 2314 {
2283 return state().m_imageSmoothingEnabled; 2315 return state().m_imageSmoothingEnabled;
2284 } 2316 }
2285 2317
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
2392 const int focusRingWidth = 5; 2424 const int focusRingWidth = 5;
2393 const int focusRingOutline = 0; 2425 const int focusRingOutline = 0;
2394 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor); 2426 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor);
2395 2427
2396 c->restore(); 2428 c->restore();
2397 2429
2398 didDraw(dirtyRect); 2430 didDraw(dirtyRect);
2399 } 2431 }
2400 2432
2401 } // namespace WebCore 2433 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698