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

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 Created 3 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
« 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 subpixelAscentDescentForSmallSizes) {
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 bool subpixelAscentDescentAndFontSmall =
137 subpixelAscentDescentForSmallSizes &&
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 = subpixelAscentDescentAndFontSmall
142 // baseline for different types of text baselines (crbug.com/338908). 149 ? -metrics.fAscent
143 // Please see CanvasRenderingContext2D::getFontBaseline for the heuristic. 150 : SkScalarRoundToScalar(-metrics.fAscent);
144 if (subpixelAscentDescent && 151 descent = subpixelAscentDescentAndFontSmall
145 (-metrics.fAscent < 3 || -metrics.fAscent + metrics.fDescent < 2)) { 152 ? metrics.fDescent
146 ascent = -metrics.fAscent; 153 : SkScalarRoundToScalar(metrics.fDescent);
147 descent = metrics.fDescent;
148 } else {
149 ascent = SkScalarRoundToScalar(-metrics.fAscent);
150 descent = SkScalarRoundToScalar(metrics.fDescent);
151 }
152 #if OS(LINUX) || OS(ANDROID) 154 #if OS(LINUX) || OS(ANDROID)
153 // When subpixel positioning is enabled, if the descent is rounded down, the 155 // 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: 156 // 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 157 // hidden' container. To avoid that, borrow 1 unit from the ascent when
156 // possible. 158 // possible.
157 // FIXME: This can be removed if sub-pixel ascent/descent is supported. 159 // FIXME: This can be removed if sub-pixel ascent/descent is supported.
158 if (platformData().getFontRenderStyle().useSubpixelPositioning && 160 if (platformData().getFontRenderStyle().useSubpixelPositioning &&
159 descent < SkScalarToFloat(metrics.fDescent) && ascent >= 1) { 161 descent < SkScalarToFloat(metrics.fDescent) && ascent >= 1) {
160 ++descent; 162 ++descent;
161 --ascent; 163 --ascent;
162 } 164 }
163 #endif 165 #endif
164 } 166 }
165 167
166 #if OS(MACOSX) 168 #if OS(MACOSX)
167 // We are preserving this ascent hack to match Safari's ascent adjustment 169 // We are preserving this ascent hack to match Safari's ascent adjustment
168 // in their SimpleFontDataMac.mm, for details see crbug.com/445830. 170 // in their SimpleFontDataMac.mm, for details see crbug.com/445830.
169 // We need to adjust Times, Helvetica, and Courier to closely match the 171 // We need to adjust Times, Helvetica, and Courier to closely match the
170 // vertical metrics of their Microsoft counterparts that are the de facto 172 // vertical metrics of their Microsoft counterparts that are the de facto
171 // web standard. The AppKit adjustment of 20% is too big and is 173 // 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 174 // incorrectly added to line spacing, so we use a 15% adjustment instead
173 // and add it to the ascent. 175 // and add it to the ascent.
174 DEFINE_STATIC_LOCAL(AtomicString, timesName, ("Times")); 176 DEFINE_STATIC_LOCAL(AtomicString, timesName, ("Times"));
175 DEFINE_STATIC_LOCAL(AtomicString, helveticaName, ("Helvetica")); 177 DEFINE_STATIC_LOCAL(AtomicString, helveticaName, ("Helvetica"));
176 DEFINE_STATIC_LOCAL(AtomicString, courierName, ("Courier")); 178 DEFINE_STATIC_LOCAL(AtomicString, courierName, ("Courier"));
177 String familyName = m_platformData.fontFamilyName(); 179 String familyName = m_platformData.fontFamilyName();
178 if (familyName == timesName || familyName == helveticaName || 180 if (familyName == timesName || familyName == helveticaName ||
179 familyName == courierName) 181 familyName == courierName) {
180 ascent += floorf(((ascent + descent) * 0.15f) + 0.5f); 182 ascent += subpixelAscentDescentAndFontSmall
183 ? ((ascent + descent) * 0.15f)
184 : floorf(((ascent + descent) * 0.15f) + 0.5f);
185 }
181 #endif 186 #endif
182 187
183 m_fontMetrics.setAscent(ascent); 188 m_fontMetrics.setAscent(ascent);
184 m_fontMetrics.setDescent(descent); 189 m_fontMetrics.setDescent(descent);
185 190
186 float xHeight; 191 float xHeight;
187 if (metrics.fXHeight) { 192 if (metrics.fXHeight) {
188 xHeight = metrics.fXHeight; 193 xHeight = metrics.fXHeight;
189 #if OS(MACOSX) 194 #if OS(MACOSX)
190 // Mac OS CTFontGetXHeight reports the bounding box height of x, 195 // 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 { 346 const FontDescription& fontDescription) const {
342 if (!m_derivedFontData) 347 if (!m_derivedFontData)
343 m_derivedFontData = DerivedFontData::create(); 348 m_derivedFontData = DerivedFontData::create();
344 if (!m_derivedFontData->emphasisMark) 349 if (!m_derivedFontData->emphasisMark)
345 m_derivedFontData->emphasisMark = 350 m_derivedFontData->emphasisMark =
346 createScaledFontData(fontDescription, emphasisMarkFontSizeMultiplier); 351 createScaledFontData(fontDescription, emphasisMarkFontSizeMultiplier);
347 352
348 return m_derivedFontData->emphasisMark; 353 return m_derivedFontData->emphasisMark;
349 } 354 }
350 355
356 PassRefPtr<SimpleFontData> SimpleFontData::subpixelAscentDescentFontData()
357 const {
358 return SimpleFontData::create(m_platformData, m_customFontData,
359 m_isTextOrientationFallback, true);
360 }
361
351 bool SimpleFontData::isTextOrientationFallbackOf( 362 bool SimpleFontData::isTextOrientationFallbackOf(
352 const SimpleFontData* fontData) const { 363 const SimpleFontData* fontData) const {
353 if (!isTextOrientationFallback() || !fontData->m_derivedFontData) 364 if (!isTextOrientationFallback() || !fontData->m_derivedFontData)
354 return false; 365 return false;
355 return fontData->m_derivedFontData->uprightOrientation == this || 366 return fontData->m_derivedFontData->uprightOrientation == this ||
356 fontData->m_derivedFontData->verticalRightOrientation == this; 367 fontData->m_derivedFontData->verticalRightOrientation == this;
357 } 368 }
358 369
359 std::unique_ptr<SimpleFontData::DerivedFontData> 370 std::unique_ptr<SimpleFontData::DerivedFontData>
360 SimpleFontData::DerivedFontData::create() { 371 SimpleFontData::DerivedFontData::create() {
(...skipping 24 matching lines...) Expand all
385 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const { 396 float SimpleFontData::platformWidthForGlyph(Glyph glyph) const {
386 if (!m_platformData.size()) 397 if (!m_platformData.size())
387 return 0; 398 return 0;
388 399
389 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); 400 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated.");
390 401
391 return SkiaTextMetrics(&m_paint).getSkiaWidthForGlyph(glyph); 402 return SkiaTextMetrics(&m_paint).getSkiaWidthForGlyph(glyph);
392 } 403 }
393 404
394 } // namespace blink 405 } // 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