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

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

Issue 2643413002: Fix 'text-underline-position: under' to use em height ascent/descent (Closed)
Patch Set: Rebaseline Created 3 years, 7 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) 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 21 matching lines...) Expand all
32 #include <unicode/unorm.h> 32 #include <unicode/unorm.h>
33 #include <unicode/utf16.h> 33 #include <unicode/utf16.h>
34 #include <memory> 34 #include <memory>
35 #include "SkPath.h" 35 #include "SkPath.h"
36 #include "SkTypeface.h" 36 #include "SkTypeface.h"
37 #include "SkTypes.h" 37 #include "SkTypes.h"
38 #include "platform/fonts/FontDescription.h" 38 #include "platform/fonts/FontDescription.h"
39 #include "platform/fonts/VDMXParser.h" 39 #include "platform/fonts/VDMXParser.h"
40 #include "platform/fonts/skia/SkiaTextMetrics.h" 40 #include "platform/fonts/skia/SkiaTextMetrics.h"
41 #include "platform/geometry/FloatRect.h" 41 #include "platform/geometry/FloatRect.h"
42 #include "platform/wtf/ByteOrder.h"
42 #include "platform/wtf/MathExtras.h" 43 #include "platform/wtf/MathExtras.h"
43 #include "platform/wtf/PtrUtil.h" 44 #include "platform/wtf/PtrUtil.h"
44 #include "platform/wtf/allocator/Partitions.h" 45 #include "platform/wtf/allocator/Partitions.h"
45 #include "platform/wtf/text/CharacterNames.h" 46 #include "platform/wtf/text/CharacterNames.h"
46 #include "platform/wtf/text/Unicode.h" 47 #include "platform/wtf/text/Unicode.h"
47 48
48 namespace blink { 49 namespace blink {
49 50
50 const float kSmallCapsFontSizeMultiplier = 0.7f; 51 const float kSmallCapsFontSizeMultiplier = 0.7f;
51 const float kEmphasisMarkFontSizeMultiplier = 0.5f; 52 const float kEmphasisMarkFontSizeMultiplier = 0.5f;
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 PassRefPtr<SimpleFontData> SimpleFontData::CreateScaledFontData( 373 PassRefPtr<SimpleFontData> SimpleFontData::CreateScaledFontData(
373 const FontDescription& font_description, 374 const FontDescription& font_description,
374 float scale_factor) const { 375 float scale_factor) const {
375 const float scaled_size = 376 const float scaled_size =
376 lroundf(font_description.ComputedSize() * scale_factor); 377 lroundf(font_description.ComputedSize() * scale_factor);
377 return SimpleFontData::Create( 378 return SimpleFontData::Create(
378 FontPlatformData(platform_data_, scaled_size), 379 FontPlatformData(platform_data_, scaled_size),
379 IsCustomFont() ? CustomFontData::Create() : nullptr); 380 IsCustomFont() ? CustomFontData::Create() : nullptr);
380 } 381 }
381 382
383 // Internal leadings can be distributed to ascent and descent.
384 // -------------------------------------------
385 // | - Internal Leading (in ascent)
386 // |--------------------------------
387 // Ascent - | |
388 // | |
389 // | | - Em height
390 // ----------|--------------|
391 // | |
392 // Descent - |--------------------------------
393 // | - Internal Leading (in descent)
eae 2017/05/01 16:21:48 This illustration really helps, thank you!
394 // -------------------------------------------
395 // How Gecko computes "em height":
396 // https://github.com/whatwg/html/issues/2470#issuecomment-291425136
397 LayoutUnit SimpleFontData::EmHeightAscent(FontBaseline baseline_type) const {
398 if (baseline_type == kAlphabeticBaseline) {
399 if (!em_height_ascent_)
400 ComputeEmHeightMetrics();
401 return em_height_ascent_;
402 }
403 LayoutUnit em_height = LayoutUnit::FromFloatRound(PlatformData().size());
404 return em_height - em_height / 2;
405 }
406
407 LayoutUnit SimpleFontData::EmHeightDescent(FontBaseline baseline_type) const {
408 if (baseline_type == kAlphabeticBaseline) {
409 if (!em_height_descent_)
410 ComputeEmHeightMetrics();
411 return em_height_descent_;
412 }
413 LayoutUnit em_height = LayoutUnit::FromFloatRound(PlatformData().size());
414 return em_height / 2;
415 }
416
417 static std::pair<int16_t, int16_t> TypoAscenderAndDescender(
418 SkTypeface* typeface) {
419 // TODO(kojii): This should move to Skia once finalized. We can then move
420 // EmHeightAscender/Descender to FontMetrics.
421 int16_t buffer[2];
422 size_t size = typeface->getTableData(SkSetFourByteTag('O', 'S', '/', '2'), 68,
423 sizeof(buffer), buffer);
424 if (size == sizeof(buffer)) {
425 return std::make_pair(static_cast<int16_t>(ntohs(buffer[0])),
426 -static_cast<int16_t>(ntohs(buffer[1])));
427 }
428 return std::make_pair(0, 0);
429 }
430
431 void SimpleFontData::ComputeEmHeightMetrics() const {
432 // Compute em height metrics from OS/2 sTypoAscender and sTypoDescender.
433 SkTypeface* typeface = platform_data_.Typeface();
434 int16_t typo_ascender, typo_descender;
435 std::tie(typo_ascender, typo_descender) = TypoAscenderAndDescender(typeface);
436 if (typo_ascender > 0 &&
437 NormalizeEmHeightMetrics(typo_ascender, typo_ascender + typo_descender)) {
438 return;
439 }
440
441 // As the last resort, compute em height metrics from our ascent/descent.
442 const FontMetrics& font_metrics = GetFontMetrics();
443 if (NormalizeEmHeightMetrics(font_metrics.FloatAscent(),
444 font_metrics.FloatHeight())) {
445 return;
446 }
447 NOTREACHED();
448 }
449
450 bool SimpleFontData::NormalizeEmHeightMetrics(float ascent,
eae 2017/05/01 16:21:48 A comment explaining why normalization might be ne
kojii 2017/05/02 22:56:52 Done.
451 float height) const {
452 if (height <= 0 || ascent < 0 || ascent > height)
453 return false;
454 float em_height = PlatformData().size();
455 em_height_ascent_ = LayoutUnit::FromFloatRound(ascent * em_height / height);
456 em_height_descent_ =
457 LayoutUnit::FromFloatRound(em_height) - em_height_ascent_;
458 return true;
459 }
460
382 FloatRect SimpleFontData::PlatformBoundsForGlyph(Glyph glyph) const { 461 FloatRect SimpleFontData::PlatformBoundsForGlyph(Glyph glyph) const {
383 if (!platform_data_.size()) 462 if (!platform_data_.size())
384 return FloatRect(); 463 return FloatRect();
385 464
386 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); 465 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated.");
387 466
388 SkRect bounds; 467 SkRect bounds;
389 SkiaTextMetrics(&paint_).GetSkiaBoundsForGlyph(glyph, &bounds); 468 SkiaTextMetrics(&paint_).GetSkiaBoundsForGlyph(glyph, &bounds);
390 return FloatRect(bounds); 469 return FloatRect(bounds);
391 } 470 }
392 471
393 float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const { 472 float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const {
394 if (!platform_data_.size()) 473 if (!platform_data_.size())
395 return 0; 474 return 0;
396 475
397 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); 476 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated.");
398 477
399 return SkiaTextMetrics(&paint_).GetSkiaWidthForGlyph(glyph); 478 return SkiaTextMetrics(&paint_).GetSkiaWidthForGlyph(glyph);
400 } 479 }
401 480
402 } // namespace blink 481 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698