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

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, 9 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 2184 matching lines...) Expand 10 before | Expand all | Expand 10 after
2195 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth) 2195 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth)
2196 { 2196 {
2197 drawTextInternal(text, x, y, false, maxWidth, true); 2197 drawTextInternal(text, x, y, false, maxWidth, true);
2198 } 2198 }
2199 2199
2200 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text ) 2200 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text )
2201 { 2201 {
2202 FontCachePurgePreventer fontCachePurgePreventer; 2202 FontCachePurgePreventer fontCachePurgePreventer;
2203 RefPtr<TextMetrics> metrics = TextMetrics::create(); 2203 RefPtr<TextMetrics> metrics = TextMetrics::create();
2204 canvas()->document().updateStyleIfNeeded(); 2204 canvas()->document().updateStyleIfNeeded();
2205 metrics->setWidth(accessFont().width(TextRun(text))); 2205 const Font& font = accessFont();
2206 const TextRun textRun(text);
2207 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true);
2208
2209 // x direction
2210 metrics->setWidth(font.width(textRun));
2211 metrics->setActualBoundingBoxLeft(-textBounds.x());
2212 metrics->setActualBoundingBoxRight(textBounds.maxX());
2213
2214 // y direction
2215 const FontMetrics& fontMetrics = font.fontMetrics();
2216 const float ascent = fontMetrics.floatAscent();
2217 const float descent = fontMetrics.floatDescent();
2218 const float baselineY = getFontBaseline(fontMetrics);
2219
2220 metrics->setFontBoundingBoxAscent(ascent - baselineY);
2221 metrics->setFontBoundingBoxDescent(descent + baselineY);
2222 metrics->setActualBoundingBoxAscent(-textBounds.y() - baselineY);
2223 metrics->setActualBoundingBoxDescent(textBounds.maxY() + baselineY);
2224
2225 // Note : top/bottom and ascend/descend are currently the same, so there's n o difference
2226 // between the EM box's top and bottom and the font's ascend and desc end
2227 metrics->setEmHeightAscent(0);
2228 metrics->setEmHeightDescent(0);
2229
2230 metrics->setHangingBaseline(-0.8f * ascent + baselineY);
2231 metrics->setAlphabeticBaseline(baselineY);
2232 metrics->setIdeographicBaseline(descent + baselineY);
2206 return metrics.release(); 2233 return metrics.release();
2207 } 2234 }
2208 2235
2209 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement) 2236 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement)
2210 { 2237 {
2211 const size_t replacementLength = replacement.length(); 2238 const size_t replacementLength = replacement.length();
2212 size_t index = 0; 2239 size_t index = 0;
2213 while ((index = text.find(matchFunction, index)) != kNotFound) { 2240 while ((index = text.find(matchFunction, index)) != kNotFound) {
2214 text.replace(index, 1, replacement); 2241 text.replace(index, 1, replacement);
2215 index += replacementLength; 2242 index += replacementLength;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2252 2279
2253 // FIXME: Need to turn off font smoothing. 2280 // FIXME: Need to turn off font smoothing.
2254 2281
2255 RenderStyle* computedStyle = canvas()->computedStyle(); 2282 RenderStyle* computedStyle = canvas()->computedStyle();
2256 TextDirection direction = computedStyle ? computedStyle->direction() : LTR; 2283 TextDirection direction = computedStyle ? computedStyle->direction() : LTR;
2257 bool isRTL = direction == RTL; 2284 bool isRTL = direction == RTL;
2258 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2285 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2259 2286
2260 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding); 2287 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding);
2261 // Draw the item text at the correct point. 2288 // Draw the item text at the correct point.
2262 FloatPoint location(x, y); 2289 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2263 switch (state().m_textBaseline) {
2264 case TopTextBaseline:
2265 case HangingTextBaseline:
2266 location.setY(y + fontMetrics.ascent());
2267 break;
2268 case BottomTextBaseline:
2269 case IdeographicTextBaseline:
2270 location.setY(y - fontMetrics.descent());
2271 break;
2272 case MiddleTextBaseline:
2273 location.setY(y - fontMetrics.descent() + fontMetrics.height() / 2);
2274 break;
2275 case AlphabeticTextBaseline:
2276 default:
2277 // Do nothing.
2278 break;
2279 }
2280 2290
2281 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override)); 2291 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2282 2292
2283 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2293 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
2284 float width = useMaxWidth ? maxWidth : fontWidth; 2294 float width = useMaxWidth ? maxWidth : fontWidth;
2285 2295
2286 TextAlign align = state().m_textAlign; 2296 TextAlign align = state().m_textAlign;
2287 if (align == StartTextAlign) 2297 if (align == StartTextAlign)
2288 align = isRTL ? RightTextAlign : LeftTextAlign; 2298 align = isRTL ? RightTextAlign : LeftTextAlign;
2289 else if (align == EndTextAlign) 2299 else if (align == EndTextAlign)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2343 2353
2344 const Font& CanvasRenderingContext2D::accessFont() 2354 const Font& CanvasRenderingContext2D::accessFont()
2345 { 2355 {
2346 // This needs style to be up to date, but can't assert so because drawTextIn ternal 2356 // This needs style to be up to date, but can't assert so because drawTextIn ternal
2347 // can invalidate style before this is called (e.g. drawingContext invalidat es style). 2357 // can invalidate style before this is called (e.g. drawingContext invalidat es style).
2348 if (!state().m_realizedFont) 2358 if (!state().m_realizedFont)
2349 setFont(state().m_unparsedFont); 2359 setFont(state().m_unparsedFont);
2350 return state().m_font; 2360 return state().m_font;
2351 } 2361 }
2352 2362
2363 int CanvasRenderingContext2D::getFontBaseline(const FontMetrics& fontMetrics) co nst
2364 {
2365 switch (state().m_textBaseline) {
2366 case TopTextBaseline:
2367 return fontMetrics.ascent();
2368 case HangingTextBaseline:
2369 // According to http://wiki.apache.org/xmlgraphics-fop/LineLayout/Alignm entHandling
2370 // "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of the ascender height"
2371 return (fontMetrics.ascent() * 4) / 5;
2372 case BottomTextBaseline:
2373 case IdeographicTextBaseline:
2374 return -fontMetrics.descent();
2375 case MiddleTextBaseline:
2376 return -fontMetrics.descent() + fontMetrics.height() / 2;
2377 case AlphabeticTextBaseline:
2378 default:
2379 // Do nothing.
2380 break;
2381 }
2382 return 0;
2383 }
2384
2353 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const 2385 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const
2354 { 2386 {
2355 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0; 2387 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0;
2356 } 2388 }
2357 2389
2358 bool CanvasRenderingContext2D::imageSmoothingEnabled() const 2390 bool CanvasRenderingContext2D::imageSmoothingEnabled() const
2359 { 2391 {
2360 return state().m_imageSmoothingEnabled; 2392 return state().m_imageSmoothingEnabled;
2361 } 2393 }
2362 2394
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
2469 const int focusRingWidth = 5; 2501 const int focusRingWidth = 5;
2470 const int focusRingOutline = 0; 2502 const int focusRingOutline = 0;
2471 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor); 2503 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor);
2472 2504
2473 c->restore(); 2505 c->restore();
2474 2506
2475 didDraw(dirtyRect); 2507 didDraw(dirtyRect);
2476 } 2508 }
2477 2509
2478 } // namespace WebCore 2510 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698