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

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: Fixed windows layout tests 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
« no previous file with comments | « Source/core/html/canvas/CanvasRenderingContext2D.h ('k') | Source/platform/fonts/Font.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 2227 matching lines...) Expand 10 before | Expand all | Expand 10 after
2238 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text ) 2238 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text )
2239 { 2239 {
2240 RefPtr<TextMetrics> metrics = TextMetrics::create(); 2240 RefPtr<TextMetrics> metrics = TextMetrics::create();
2241 2241
2242 // The style resolution required for rendering text is not available in fram e-less documents. 2242 // The style resolution required for rendering text is not available in fram e-less documents.
2243 if (!canvas()->document().frame()) 2243 if (!canvas()->document().frame())
2244 return metrics.release(); 2244 return metrics.release();
2245 2245
2246 FontCachePurgePreventer fontCachePurgePreventer; 2246 FontCachePurgePreventer fontCachePurgePreventer;
2247 canvas()->document().updateStyleIfNeeded(); 2247 canvas()->document().updateStyleIfNeeded();
2248 metrics->setWidth(accessFont().width(TextRun(text))); 2248 const Font& font = accessFont();
2249 const TextRun textRun(text);
2250 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true);
2251
2252 // x direction
2253 metrics->setWidth(font.width(textRun));
2254 metrics->setActualBoundingBoxLeft(-textBounds.x());
2255 metrics->setActualBoundingBoxRight(textBounds.maxX());
2256
2257 // y direction
2258 const FontMetrics& fontMetrics = font.fontMetrics();
2259 const float ascent = fontMetrics.floatAscent();
2260 const float descent = fontMetrics.floatDescent();
2261 const float baselineY = getFontBaseline(fontMetrics);
2262
2263 metrics->setFontBoundingBoxAscent(ascent - baselineY);
2264 metrics->setFontBoundingBoxDescent(descent + baselineY);
2265 metrics->setActualBoundingBoxAscent(-textBounds.y() - baselineY);
2266 metrics->setActualBoundingBoxDescent(textBounds.maxY() + baselineY);
2267
2268 // Note : top/bottom and ascend/descend are currently the same, so there's n o difference
2269 // between the EM box's top and bottom and the font's ascend and desc end
2270 metrics->setEmHeightAscent(0);
2271 metrics->setEmHeightDescent(0);
2272
2273 metrics->setHangingBaseline(-0.8f * ascent + baselineY);
2274 metrics->setAlphabeticBaseline(baselineY);
2275 metrics->setIdeographicBaseline(descent + baselineY);
2249 return metrics.release(); 2276 return metrics.release();
2250 } 2277 }
2251 2278
2252 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement) 2279 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement)
2253 { 2280 {
2254 const size_t replacementLength = replacement.length(); 2281 const size_t replacementLength = replacement.length();
2255 size_t index = 0; 2282 size_t index = 0;
2256 while ((index = text.find(matchFunction, index)) != kNotFound) { 2283 while ((index = text.find(matchFunction, index)) != kNotFound) {
2257 text.replace(index, 1, replacement); 2284 text.replace(index, 1, replacement);
2258 index += replacementLength; 2285 index += replacementLength;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2299 2326
2300 // FIXME: Need to turn off font smoothing. 2327 // FIXME: Need to turn off font smoothing.
2301 2328
2302 RenderStyle* computedStyle = canvas()->computedStyle(); 2329 RenderStyle* computedStyle = canvas()->computedStyle();
2303 TextDirection direction = computedStyle ? computedStyle->direction() : LTR; 2330 TextDirection direction = computedStyle ? computedStyle->direction() : LTR;
2304 bool isRTL = direction == RTL; 2331 bool isRTL = direction == RTL;
2305 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2332 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2306 2333
2307 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding); 2334 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding);
2308 // Draw the item text at the correct point. 2335 // Draw the item text at the correct point.
2309 FloatPoint location(x, y); 2336 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2310 switch (state().m_textBaseline) {
2311 case TopTextBaseline:
2312 case HangingTextBaseline:
2313 location.setY(y + fontMetrics.ascent());
2314 break;
2315 case BottomTextBaseline:
2316 case IdeographicTextBaseline:
2317 location.setY(y - fontMetrics.descent());
2318 break;
2319 case MiddleTextBaseline:
2320 location.setY(y - fontMetrics.descent() + fontMetrics.height() / 2);
2321 break;
2322 case AlphabeticTextBaseline:
2323 default:
2324 // Do nothing.
2325 break;
2326 }
2327 2337
2328 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override)); 2338 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2329 2339
2330 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2340 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
2331 float width = useMaxWidth ? maxWidth : fontWidth; 2341 float width = useMaxWidth ? maxWidth : fontWidth;
2332 2342
2333 TextAlign align = state().m_textAlign; 2343 TextAlign align = state().m_textAlign;
2334 if (align == StartTextAlign) 2344 if (align == StartTextAlign)
2335 align = isRTL ? RightTextAlign : LeftTextAlign; 2345 align = isRTL ? RightTextAlign : LeftTextAlign;
2336 else if (align == EndTextAlign) 2346 else if (align == EndTextAlign)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2390 2400
2391 const Font& CanvasRenderingContext2D::accessFont() 2401 const Font& CanvasRenderingContext2D::accessFont()
2392 { 2402 {
2393 // This needs style to be up to date, but can't assert so because drawTextIn ternal 2403 // This needs style to be up to date, but can't assert so because drawTextIn ternal
2394 // can invalidate style before this is called (e.g. drawingContext invalidat es style). 2404 // can invalidate style before this is called (e.g. drawingContext invalidat es style).
2395 if (!state().m_realizedFont) 2405 if (!state().m_realizedFont)
2396 setFont(state().m_unparsedFont); 2406 setFont(state().m_unparsedFont);
2397 return state().m_font; 2407 return state().m_font;
2398 } 2408 }
2399 2409
2410 int CanvasRenderingContext2D::getFontBaseline(const FontMetrics& fontMetrics) co nst
2411 {
2412 switch (state().m_textBaseline) {
2413 case TopTextBaseline:
2414 return fontMetrics.ascent();
2415 case HangingTextBaseline:
2416 // According to http://wiki.apache.org/xmlgraphics-fop/LineLayout/Alignm entHandling
2417 // "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of the ascender height"
2418 return (fontMetrics.ascent() * 4) / 5;
2419 case BottomTextBaseline:
2420 case IdeographicTextBaseline:
2421 return -fontMetrics.descent();
2422 case MiddleTextBaseline:
2423 return -fontMetrics.descent() + fontMetrics.height() / 2;
2424 case AlphabeticTextBaseline:
2425 default:
2426 // Do nothing.
2427 break;
2428 }
2429 return 0;
2430 }
2431
2400 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const 2432 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const
2401 { 2433 {
2402 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0; 2434 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0;
2403 } 2435 }
2404 2436
2405 bool CanvasRenderingContext2D::imageSmoothingEnabled() const 2437 bool CanvasRenderingContext2D::imageSmoothingEnabled() const
2406 { 2438 {
2407 return state().m_imageSmoothingEnabled; 2439 return state().m_imageSmoothingEnabled;
2408 } 2440 }
2409 2441
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
2516 const int focusRingWidth = 5; 2548 const int focusRingWidth = 5;
2517 const int focusRingOutline = 0; 2549 const int focusRingOutline = 0;
2518 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor); 2550 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor);
2519 2551
2520 c->restore(); 2552 c->restore();
2521 2553
2522 didDraw(dirtyRect); 2554 didDraw(dirtyRect);
2523 } 2555 }
2524 2556
2525 } // namespace WebCore 2557 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/html/canvas/CanvasRenderingContext2D.h ('k') | Source/platform/fonts/Font.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698