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

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: Adding Layout tests. Should work for simple text. 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 2142 matching lines...) Expand 10 before | Expand all | Expand 10 after
2153 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth) 2153 void CanvasRenderingContext2D::strokeText(const String& text, float x, float y, float maxWidth)
2154 { 2154 {
2155 drawTextInternal(text, x, y, false, maxWidth, true); 2155 drawTextInternal(text, x, y, false, maxWidth, true);
2156 } 2156 }
2157 2157
2158 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text ) 2158 PassRefPtr<TextMetrics> CanvasRenderingContext2D::measureText(const String& text )
2159 { 2159 {
2160 FontCachePurgePreventer fontCachePurgePreventer; 2160 FontCachePurgePreventer fontCachePurgePreventer;
2161 RefPtr<TextMetrics> metrics = TextMetrics::create(); 2161 RefPtr<TextMetrics> metrics = TextMetrics::create();
2162 canvas()->document().updateStyleIfNeeded(); 2162 canvas()->document().updateStyleIfNeeded();
2163 metrics->setWidth(accessFont().width(TextRun(text))); 2163 const Font& font = accessFont();
2164 const TextRun textRun(text);
2165 FloatRect textBounds = font.selectionRectForText(textRun, FloatPoint(), font .fontDescription().computedSize(), 0, -1, true);
2166
2167 // x direction
2168 metrics->setWidth(font.width(textRun));
2169 metrics->setActualBoundingBoxLeft(-textBounds.x());
2170 metrics->setActualBoundingBoxRight(textBounds.maxX());
2171
2172 // y direction
2173 const FontMetrics& fontMetrics = font.fontMetrics();
2174 const float ascent = fontMetrics.floatAscent();
2175 const float descent = fontMetrics.floatDescent();
2176 const float baselineY = getFontBaseline(fontMetrics);
2177
2178 metrics->setFontBoundingBoxAscent(ascent - baselineY);
2179 metrics->setFontBoundingBoxDescent(descent + baselineY);
2180 metrics->setActualBoundingBoxAscent(-textBounds.y() - baselineY);
2181 metrics->setActualBoundingBoxDescent(textBounds.maxY() + baselineY);
2182
2183 // Note : top/bottom and ascend/descend are currently the same, so there's n o difference
2184 // between the EM box's top and bottom and the font's ascend and desc end
2185 metrics->setEmHeightAscent(0);
2186 metrics->setEmHeightDescent(0);
2187
2188 metrics->setHangingBaseline(-0.8f * ascent + baselineY);
2189 metrics->setAlphabeticBaseline(baselineY);
2190 metrics->setIdeographicBaseline(descent + baselineY);
2164 return metrics.release(); 2191 return metrics.release();
2165 } 2192 }
2166 2193
2167 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement) 2194 static void replaceCharacterInString(String& text, WTF::CharacterMatchFunctionPt r matchFunction, const String& replacement)
2168 { 2195 {
2169 const size_t replacementLength = replacement.length(); 2196 const size_t replacementLength = replacement.length();
2170 size_t index = 0; 2197 size_t index = 0;
2171 while ((index = text.find(matchFunction, index)) != kNotFound) { 2198 while ((index = text.find(matchFunction, index)) != kNotFound) {
2172 text.replace(index, 1, replacement); 2199 text.replace(index, 1, replacement);
2173 index += replacementLength; 2200 index += replacementLength;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2210 2237
2211 // FIXME: Need to turn off font smoothing. 2238 // FIXME: Need to turn off font smoothing.
2212 2239
2213 RenderStyle* computedStyle = canvas()->computedStyle(); 2240 RenderStyle* computedStyle = canvas()->computedStyle();
2214 TextDirection direction = computedStyle ? computedStyle->direction() : LTR; 2241 TextDirection direction = computedStyle ? computedStyle->direction() : LTR;
2215 bool isRTL = direction == RTL; 2242 bool isRTL = direction == RTL;
2216 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse; 2243 bool override = computedStyle ? isOverride(computedStyle->unicodeBidi()) : f alse;
2217 2244
2218 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding); 2245 TextRun textRun(normalizedText, 0, 0, TextRun::AllowTrailingExpansion, direc tion, override, true, TextRun::NoRounding);
2219 // Draw the item text at the correct point. 2246 // Draw the item text at the correct point.
2220 FloatPoint location(x, y); 2247 FloatPoint location(x, y + getFontBaseline(fontMetrics));
2221 switch (state().m_textBaseline) {
2222 case TopTextBaseline:
2223 case HangingTextBaseline:
2224 location.setY(y + fontMetrics.ascent());
2225 break;
2226 case BottomTextBaseline:
2227 case IdeographicTextBaseline:
2228 location.setY(y - fontMetrics.descent());
2229 break;
2230 case MiddleTextBaseline:
2231 location.setY(y - fontMetrics.descent() + fontMetrics.height() / 2);
2232 break;
2233 case AlphabeticTextBaseline:
2234 default:
2235 // Do nothing.
2236 break;
2237 }
2238 2248
2239 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override)); 2249 float fontWidth = font.width(TextRun(normalizedText, 0, 0, TextRun::AllowTra ilingExpansion, direction, override));
2240 2250
2241 useMaxWidth = (useMaxWidth && maxWidth < fontWidth); 2251 useMaxWidth = (useMaxWidth && maxWidth < fontWidth);
2242 float width = useMaxWidth ? maxWidth : fontWidth; 2252 float width = useMaxWidth ? maxWidth : fontWidth;
2243 2253
2244 TextAlign align = state().m_textAlign; 2254 TextAlign align = state().m_textAlign;
2245 if (align == StartTextAlign) 2255 if (align == StartTextAlign)
2246 align = isRTL ? RightTextAlign : LeftTextAlign; 2256 align = isRTL ? RightTextAlign : LeftTextAlign;
2247 else if (align == EndTextAlign) 2257 else if (align == EndTextAlign)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2301 2311
2302 const Font& CanvasRenderingContext2D::accessFont() 2312 const Font& CanvasRenderingContext2D::accessFont()
2303 { 2313 {
2304 // This needs style to be up to date, but can't assert so because drawTextIn ternal 2314 // This needs style to be up to date, but can't assert so because drawTextIn ternal
2305 // can invalidate style before this is called (e.g. drawingContext invalidat es style). 2315 // can invalidate style before this is called (e.g. drawingContext invalidat es style).
2306 if (!state().m_realizedFont) 2316 if (!state().m_realizedFont)
2307 setFont(state().m_unparsedFont); 2317 setFont(state().m_unparsedFont);
2308 return state().m_font; 2318 return state().m_font;
2309 } 2319 }
2310 2320
2321 int CanvasRenderingContext2D::getFontBaseline(const FontMetrics& fontMetrics) co nst
2322 {
2323 switch (state().m_textBaseline) {
2324 case TopTextBaseline:
2325 return fontMetrics.ascent();
2326 case HangingTextBaseline:
2327 // According to http://wiki.apache.org/xmlgraphics-fop/LineLayout/Alignm entHandling
2328 // "FOP (Formatting Objects Processor) puts the hanging baseline at 80% of the ascender height"
2329 return (fontMetrics.ascent() * 4) / 5;
sugoi1 2014/01/31 21:56:05 I modified the hanging baseline from "ascent" to "
2330 case BottomTextBaseline:
2331 case IdeographicTextBaseline:
2332 return -fontMetrics.descent();
2333 case MiddleTextBaseline:
2334 return -fontMetrics.descent() + fontMetrics.height() / 2;
2335 case AlphabeticTextBaseline:
2336 default:
2337 // Do nothing.
2338 break;
2339 }
2340 return 0;
2341 }
2342
2311 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const 2343 blink::WebLayer* CanvasRenderingContext2D::platformLayer() const
2312 { 2344 {
2313 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0; 2345 return canvas()->buffer() ? canvas()->buffer()->platformLayer() : 0;
2314 } 2346 }
2315 2347
2316 bool CanvasRenderingContext2D::imageSmoothingEnabled() const 2348 bool CanvasRenderingContext2D::imageSmoothingEnabled() const
2317 { 2349 {
2318 return state().m_imageSmoothingEnabled; 2350 return state().m_imageSmoothingEnabled;
2319 } 2351 }
2320 2352
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
2427 const int focusRingWidth = 5; 2459 const int focusRingWidth = 5;
2428 const int focusRingOutline = 0; 2460 const int focusRingOutline = 0;
2429 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor); 2461 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor);
2430 2462
2431 c->restore(); 2463 c->restore();
2432 2464
2433 didDraw(dirtyRect); 2465 didDraw(dirtyRect);
2434 } 2466 }
2435 2467
2436 } // namespace WebCore 2468 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698