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

Side by Side Diff: third_party/WebKit/Source/platform/fonts/SimpleFontData.cpp

Issue 2427773002: Fixing superscript and subscript baseline for tiny fonts in SVG
Patch Set: Rebaseline layout tests. Created 4 years, 1 month 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 | « third_party/WebKit/Source/platform/fonts/SimpleFontData.h ('k') | no next file » | 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) 2005, 2008, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov 3 * Copyright (C) 2006 Alexey Proskuryakov
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 float fontSize, 79 float fontSize,
80 bool syntheticBold, 80 bool syntheticBold,
81 bool syntheticItalic) 81 bool syntheticItalic)
82 : m_platformData( 82 : m_platformData(
83 FontPlatformData(fontSize, syntheticBold, syntheticItalic)), 83 FontPlatformData(fontSize, syntheticBold, syntheticItalic)),
84 m_isTextOrientationFallback(false), 84 m_isTextOrientationFallback(false),
85 m_verticalData(nullptr), 85 m_verticalData(nullptr),
86 m_hasVerticalGlyphs(false), 86 m_hasVerticalGlyphs(false),
87 m_customFontData(customData) {} 87 m_customFontData(customData) {}
88 88
89 void SimpleFontData::platformInit(bool subpixelAscentDescent) { 89 void SimpleFontData::platformInit(bool subpixelAscentDescent) {
eae 2016/11/04 18:25:28 bool subpixelAscentDescentForSmallSizes would be a
zakerinasab 2016/11/07 18:40:26 Done.
90 if (!m_platformData.size()) { 90 if (!m_platformData.size()) {
91 m_fontMetrics.reset(); 91 m_fontMetrics.reset();
92 m_avgCharWidth = 0; 92 m_avgCharWidth = 0;
93 m_maxCharWidth = 0; 93 m_maxCharWidth = 0;
94 return; 94 return;
95 } 95 }
96 96
97 SkPaint::FontMetrics metrics; 97 SkPaint::FontMetrics metrics;
98 98
99 m_platformData.setupPaint(&m_paint); 99 m_platformData.setupPaint(&m_paint);
(...skipping 23 matching lines...) Expand all
123 parseVDMX(&vdmxAscent, &vdmxDescent, vdmxTable, vdmxSize, pixelSize)) 123 parseVDMX(&vdmxAscent, &vdmxDescent, vdmxTable, vdmxSize, pixelSize))
124 isVDMXValid = true; 124 isVDMXValid = true;
125 WTF::Partitions::fastFree(vdmxTable); 125 WTF::Partitions::fastFree(vdmxTable);
126 } 126 }
127 } 127 }
128 #endif 128 #endif
129 129
130 float ascent; 130 float ascent;
131 float descent; 131 float descent;
132 132
133 // For tiny fonts, the rounding of fAscent and fDescent results in equal
134 // baseline for different types of text baselines (crbug.com/338908).
135 // Please see CanvasRenderingContext2D::getFontBaseline for the heuristic.
136 subpixelAscentDescent =
eae 2016/11/04 18:25:28 Please use a different name here to avoid the appe
zakerinasab 2016/11/07 18:40:26 Done.
137 subpixelAscentDescent &&
138 (-metrics.fAscent < 3 || -metrics.fAscent + metrics.fDescent < 2);
139
133 // Beware those who step here: This code is designed to match Win32 font 140 // Beware those who step here: This code is designed to match Win32 font
134 // metrics *exactly* except: 141 // metrics *exactly* except:
135 // - the adjustment of ascent/descent on Linux/Android 142 // - the adjustment of ascent/descent on Linux/Android
136 // - metrics.fAscent and .fDesscent are not rounded to int for tiny fonts 143 // - metrics.fAscent and .fDesscent are not rounded to int for tiny fonts
137 if (isVDMXValid) { 144 if (isVDMXValid) {
138 ascent = vdmxAscent; 145 ascent = vdmxAscent;
139 descent = -vdmxDescent; 146 descent = -vdmxDescent;
140 } else { 147 } else {
141 // For tiny fonts, the rounding of fAscent and fDescent results in equal 148 ascent = subpixelAscentDescent ? -metrics.fAscent
142 // baseline for different types of text baselines (crbug.com/338908). 149 : SkScalarRoundToScalar(-metrics.fAscent);
143 // Please see CanvasRenderingContext2D::getFontBaseline for the heuristic. 150 descent = subpixelAscentDescent ? metrics.fDescent
144 if (subpixelAscentDescent && 151 : SkScalarRoundToScalar(metrics.fDescent);
145 (-metrics.fAscent < 3 || -metrics.fAscent + metrics.fDescent < 2)) {
146 ascent = -metrics.fAscent;
147 descent = metrics.fDescent;
148 } else {
149 ascent = SkScalarRoundToScalar(-metrics.fAscent);
150 descent = SkScalarRoundToScalar(metrics.fDescent);
151 }
152 #if OS(LINUX) || OS(ANDROID) 152 #if OS(LINUX) || OS(ANDROID)
153 // When subpixel positioning is enabled, if the descent is rounded down, the 153 // When subpixel positioning is enabled, if the descent is rounded down, the
154 // descent part of the glyph may be truncated when displayed in a 'overflow: 154 // descent part of the glyph may be truncated when displayed in a 'overflow:
155 // hidden' container. To avoid that, borrow 1 unit from the ascent when 155 // hidden' container. To avoid that, borrow 1 unit from the ascent when
156 // possible. 156 // possible.
157 // FIXME: This can be removed if sub-pixel ascent/descent is supported. 157 // FIXME: This can be removed if sub-pixel ascent/descent is supported.
158 if (platformData().getFontRenderStyle().useSubpixelPositioning && 158 if (platformData().getFontRenderStyle().useSubpixelPositioning &&
159 descent < SkScalarToFloat(metrics.fDescent) && ascent >= 1) { 159 descent < SkScalarToFloat(metrics.fDescent) && ascent >= 1) {
160 ++descent; 160 ++descent;
161 --ascent; 161 --ascent;
162 } 162 }
163 #endif 163 #endif
164 } 164 }
165 165
166 #if OS(MACOSX) 166 #if OS(MACOSX)
167 // We are preserving this ascent hack to match Safari's ascent adjustment 167 // We are preserving this ascent hack to match Safari's ascent adjustment
168 // in their SimpleFontDataMac.mm, for details see crbug.com/445830. 168 // in their SimpleFontDataMac.mm, for details see crbug.com/445830.
169 // We need to adjust Times, Helvetica, and Courier to closely match the 169 // We need to adjust Times, Helvetica, and Courier to closely match the
170 // vertical metrics of their Microsoft counterparts that are the de facto 170 // vertical metrics of their Microsoft counterparts that are the de facto
171 // web standard. The AppKit adjustment of 20% is too big and is 171 // web standard. The AppKit adjustment of 20% is too big and is
172 // incorrectly added to line spacing, so we use a 15% adjustment instead 172 // incorrectly added to line spacing, so we use a 15% adjustment instead
173 // and add it to the ascent. 173 // and add it to the ascent.
174 DEFINE_STATIC_LOCAL(AtomicString, timesName, ("Times")); 174 DEFINE_STATIC_LOCAL(AtomicString, timesName, ("Times"));
175 DEFINE_STATIC_LOCAL(AtomicString, helveticaName, ("Helvetica")); 175 DEFINE_STATIC_LOCAL(AtomicString, helveticaName, ("Helvetica"));
176 DEFINE_STATIC_LOCAL(AtomicString, courierName, ("Courier")); 176 DEFINE_STATIC_LOCAL(AtomicString, courierName, ("Courier"));
177 String familyName = m_platformData.fontFamilyName(); 177 String familyName = m_platformData.fontFamilyName();
178 if (familyName == timesName || familyName == helveticaName || 178 if (familyName == timesName || familyName == helveticaName ||
179 familyName == courierName) 179 familyName == courierName) {
180 ascent += floorf(((ascent + descent) * 0.15f) + 0.5f); 180 ascent += subpixelAscentDescent
181 ? ((ascent + descent) * 0.15f)
182 : floorf(((ascent + descent) * 0.15f) + 0.5f);
183 }
181 #endif 184 #endif
182 185
183 m_fontMetrics.setAscent(ascent); 186 m_fontMetrics.setAscent(ascent);
184 m_fontMetrics.setDescent(descent); 187 m_fontMetrics.setDescent(descent);
185 188
186 float xHeight; 189 float xHeight;
187 if (metrics.fXHeight) { 190 if (metrics.fXHeight) {
188 xHeight = metrics.fXHeight; 191 xHeight = metrics.fXHeight;
189 #if OS(MACOSX) 192 #if OS(MACOSX)
190 // Mac OS CTFontGetXHeight reports the bounding box height of x, 193 // Mac OS CTFontGetXHeight reports the bounding box height of x,
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 const FontDescription& fontDescription) const { 344 const FontDescription& fontDescription) const {
342 if (!m_derivedFontData) 345 if (!m_derivedFontData)
343 m_derivedFontData = DerivedFontData::create(); 346 m_derivedFontData = DerivedFontData::create();
344 if (!m_derivedFontData->emphasisMark) 347 if (!m_derivedFontData->emphasisMark)
345 m_derivedFontData->emphasisMark = 348 m_derivedFontData->emphasisMark =
346 createScaledFontData(fontDescription, emphasisMarkFontSizeMultiplier); 349 createScaledFontData(fontDescription, emphasisMarkFontSizeMultiplier);
347 350
348 return m_derivedFontData->emphasisMark; 351 return m_derivedFontData->emphasisMark;
349 } 352 }
350 353
354 PassRefPtr<SimpleFontData> SimpleFontData::subpixelAscentDescentFontData()
355 const {
356 return SimpleFontData::create(m_platformData, m_customFontData,
357 m_isTextOrientationFallback, true);
358 }
359
351 bool SimpleFontData::isTextOrientationFallbackOf( 360 bool SimpleFontData::isTextOrientationFallbackOf(
352 const SimpleFontData* fontData) const { 361 const SimpleFontData* fontData) const {
353 if (!isTextOrientationFallback() || !fontData->m_derivedFontData) 362 if (!isTextOrientationFallback() || !fontData->m_derivedFontData)
354 return false; 363 return false;
355 return fontData->m_derivedFontData->uprightOrientation == this || 364 return fontData->m_derivedFontData->uprightOrientation == this ||
356 fontData->m_derivedFontData->verticalRightOrientation == this; 365 fontData->m_derivedFontData->verticalRightOrientation == this;
357 } 366 }
358 367
359 std::unique_ptr<SimpleFontData::DerivedFontData> 368 std::unique_ptr<SimpleFontData::DerivedFontData>
360 SimpleFontData::DerivedFontData::create() { 369 SimpleFontData::DerivedFontData::create() {
(...skipping 24 matching lines...) Expand all
385 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const { 394 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const {
386 if (!m_platformData.size()) 395 if (!m_platformData.size())
387 return 0; 396 return 0;
388 397
389 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); 398 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated.");
390 399
391 return SkiaTextMetrics(&m_paint).getSkiaWidthForGlyph(glyph); 400 return SkiaTextMetrics(&m_paint).getSkiaWidthForGlyph(glyph);
392 } 401 }
393 402
394 } // namespace blink 403 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/fonts/SimpleFontData.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698