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

Side by Side Diff: third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp

Issue 2416033003: Remove unsafe getFontMetrics methods (Closed)
Patch Set: Address wkroman suggestions Created 4 years, 2 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. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc.
3 * All rights reserved. 3 * All rights reserved.
4 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) 4 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
5 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 5 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
6 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 6 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
7 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> 7 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
8 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 8 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
9 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved. 9 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
10 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. 10 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after
746 TextMetrics* CanvasRenderingContext2D::measureText(const String& text) { 746 TextMetrics* CanvasRenderingContext2D::measureText(const String& text) {
747 TextMetrics* metrics = TextMetrics::create(); 747 TextMetrics* metrics = TextMetrics::create();
748 748
749 // The style resolution required for fonts is not available in frame-less 749 // The style resolution required for fonts is not available in frame-less
750 // documents. 750 // documents.
751 if (!canvas()->document().frame()) 751 if (!canvas()->document().frame())
752 return metrics; 752 return metrics;
753 753
754 canvas()->document().updateStyleAndLayoutTreeForNode(canvas()); 754 canvas()->document().updateStyleAndLayoutTreeForNode(canvas());
755 const Font& font = accessFont(); 755 const Font& font = accessFont();
756 const SimpleFontData* fontData = font.primaryFont();
757 DCHECK(fontData);
758 if (!fontData)
759 return metrics;
756 760
757 TextDirection direction; 761 TextDirection direction;
758 if (state().getDirection() == CanvasRenderingContext2DState::DirectionInherit) 762 if (state().getDirection() == CanvasRenderingContext2DState::DirectionInherit)
759 direction = determineDirectionality(text); 763 direction = determineDirectionality(text);
760 else 764 else
761 direction = toTextDirection(state().getDirection(), canvas()); 765 direction = toTextDirection(state().getDirection(), canvas());
762 TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion | 766 TextRun textRun(text, 0, 0, TextRun::AllowTrailingExpansion |
763 TextRun::ForbidLeadingExpansion, 767 TextRun::ForbidLeadingExpansion,
764 direction, false); 768 direction, false);
765 textRun.setNormalizeSpace(true); 769 textRun.setNormalizeSpace(true);
766 FloatRect textBounds = font.selectionRectForText( 770 FloatRect textBounds = font.selectionRectForText(
767 textRun, FloatPoint(), font.getFontDescription().computedSize(), 0, -1, 771 textRun, FloatPoint(), font.getFontDescription().computedSize(), 0, -1,
768 true); 772 true);
769 773
770 // x direction 774 // x direction
771 metrics->setWidth(font.width(textRun)); 775 metrics->setWidth(font.width(textRun));
772 metrics->setActualBoundingBoxLeft(-textBounds.x()); 776 metrics->setActualBoundingBoxLeft(-textBounds.x());
773 metrics->setActualBoundingBoxRight(textBounds.maxX()); 777 metrics->setActualBoundingBoxRight(textBounds.maxX());
774 778
775 // y direction 779 // y direction
776 const FontMetrics& fontMetrics = font.getFontMetrics(); 780 const FontMetrics& fontMetrics = fontData->getFontMetrics();
777 const float ascent = fontMetrics.floatAscent(); 781 const float ascent = fontMetrics.floatAscent();
778 const float descent = fontMetrics.floatDescent(); 782 const float descent = fontMetrics.floatDescent();
779 const float baselineY = getFontBaseline(fontMetrics); 783 const float baselineY = getFontBaseline(fontMetrics);
780 784
781 metrics->setFontBoundingBoxAscent(ascent - baselineY); 785 metrics->setFontBoundingBoxAscent(ascent - baselineY);
782 metrics->setFontBoundingBoxDescent(descent + baselineY); 786 metrics->setFontBoundingBoxDescent(descent + baselineY);
783 metrics->setActualBoundingBoxAscent(-textBounds.y() - baselineY); 787 metrics->setActualBoundingBoxAscent(-textBounds.y() - baselineY);
784 metrics->setActualBoundingBoxDescent(textBounds.maxY() + baselineY); 788 metrics->setActualBoundingBoxDescent(textBounds.maxY() + baselineY);
785 789
786 // Note : top/bottom and ascend/descend are currently the same, so there's no 790 // Note : top/bottom and ascend/descend are currently the same, so there's no
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 826
823 // Currently, SkPictureImageFilter does not support subpixel text 827 // Currently, SkPictureImageFilter does not support subpixel text
824 // anti-aliasing, which is expected when !creationAttributes().alpha(), so we 828 // anti-aliasing, which is expected when !creationAttributes().alpha(), so we
825 // need to fall out of display list mode when drawing text to an opaque 829 // need to fall out of display list mode when drawing text to an opaque
826 // canvas. crbug.com/583809 830 // canvas. crbug.com/583809
827 if (!creationAttributes().alpha() && !isAccelerated()) 831 if (!creationAttributes().alpha() && !isAccelerated())
828 canvas()->disableDeferral( 832 canvas()->disableDeferral(
829 DisableDeferralReasonSubPixelTextAntiAliasingSupport); 833 DisableDeferralReasonSubPixelTextAntiAliasingSupport);
830 834
831 const Font& font = accessFont(); 835 const Font& font = accessFont();
832 if (!font.primaryFont()) 836 const SimpleFontData* fontData = font.primaryFont();
837 DCHECK(fontData);
838 if (!fontData)
833 return; 839 return;
834 840 const FontMetrics& fontMetrics = fontData->getFontMetrics();
835 const FontMetrics& fontMetrics = font.getFontMetrics();
836 841
837 // FIXME: Need to turn off font smoothing. 842 // FIXME: Need to turn off font smoothing.
838 843
839 const ComputedStyle* computedStyle = 0; 844 const ComputedStyle* computedStyle = 0;
840 TextDirection direction = 845 TextDirection direction =
841 toTextDirection(state().getDirection(), canvas(), &computedStyle); 846 toTextDirection(state().getDirection(), canvas(), &computedStyle);
842 bool isRTL = direction == RTL; 847 bool isRTL = direction == RTL;
843 bool override = 848 bool override =
844 computedStyle ? isOverride(computedStyle->unicodeBidi()) : false; 849 computedStyle ? isOverride(computedStyle->unicodeBidi()) : false;
845 850
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
1140 } 1145 }
1141 return true; 1146 return true;
1142 } 1147 }
1143 1148
1144 void CanvasRenderingContext2D::resetUsageTracking() { 1149 void CanvasRenderingContext2D::resetUsageTracking() {
1145 UsageCounters newCounters; 1150 UsageCounters newCounters;
1146 m_usageCounters = newCounters; 1151 m_usageCounters = newCounters;
1147 } 1152 }
1148 1153
1149 } // namespace blink 1154 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/testing/Internals.cpp ('k') | third_party/WebKit/Source/platform/DragImage.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698