Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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) | |
| 394 // ------------------------------------------- | |
| 395 LayoutUnit SimpleFontData::EmHeightAscent(FontBaseline baseline_type) const { | |
| 396 if (baseline_type == kAlphabeticBaseline) { | |
| 397 if (!em_height_ascent_) | |
| 398 ComputeEmHeightMetrics(); | |
| 399 return em_height_ascent_; | |
| 400 } | |
| 401 LayoutUnit em_height = LayoutUnit::FromFloatRound(PlatformData().size()); | |
| 402 return em_height - em_height / 2; | |
| 403 } | |
| 404 | |
| 405 LayoutUnit SimpleFontData::EmHeightDescent(FontBaseline baseline_type) const { | |
| 406 if (baseline_type == kAlphabeticBaseline) { | |
| 407 if (!em_height_descent_) | |
| 408 ComputeEmHeightMetrics(); | |
| 409 return em_height_descent_; | |
| 410 } | |
| 411 LayoutUnit em_height = LayoutUnit::FromFloatRound(PlatformData().size()); | |
| 412 return em_height / 2; | |
| 413 } | |
| 414 | |
| 415 static std::pair<int16_t, int16_t> TypoAscenderAndDescender( | |
| 416 SkTypeface* typeface) { | |
| 417 // TODO(kojii): This should move to Skia once finalized. We can then move | |
|
bungeman-chromium
2017/05/03 13:56:37
What is the reason for not using SkPaint::getFontM
| |
| 418 // EmHeightAscender/Descender to FontMetrics. | |
| 419 int16_t buffer[2]; | |
| 420 size_t size = typeface->getTableData(SkSetFourByteTag('O', 'S', '/', '2'), 68, | |
|
drott
2017/05/08 08:04:00
What's 68 here? Could you make a constant for this
kojii
2017/05/09 04:37:16
It's the offset. Skia has access to os2table so th
| |
| 421 sizeof(buffer), buffer); | |
| 422 if (size == sizeof(buffer)) { | |
| 423 return std::make_pair(static_cast<int16_t>(ntohs(buffer[0])), | |
| 424 -static_cast<int16_t>(ntohs(buffer[1]))); | |
| 425 } | |
| 426 return std::make_pair(0, 0); | |
| 427 } | |
| 428 | |
| 429 void SimpleFontData::ComputeEmHeightMetrics() const { | |
| 430 // Compute em height metrics from OS/2 sTypoAscender and sTypoDescender. | |
| 431 SkTypeface* typeface = platform_data_.Typeface(); | |
| 432 int16_t typo_ascender, typo_descender; | |
| 433 std::tie(typo_ascender, typo_descender) = TypoAscenderAndDescender(typeface); | |
| 434 if (typo_ascender > 0 && | |
| 435 NormalizeEmHeightMetrics(typo_ascender, typo_ascender + typo_descender)) { | |
| 436 return; | |
| 437 } | |
| 438 | |
| 439 // As the last resort, compute em height metrics from our ascent/descent. | |
| 440 const FontMetrics& font_metrics = GetFontMetrics(); | |
| 441 if (NormalizeEmHeightMetrics(font_metrics.FloatAscent(), | |
| 442 font_metrics.FloatHeight())) { | |
| 443 return; | |
| 444 } | |
| 445 NOTREACHED(); | |
| 446 } | |
| 447 | |
| 448 bool SimpleFontData::NormalizeEmHeightMetrics(float ascent, | |
| 449 float height) const { | |
| 450 if (height <= 0 || ascent < 0 || ascent > height) | |
| 451 return false; | |
| 452 // While the OpenType specification recommends the sum of sTypoAscender and | |
| 453 // sTypoDescender to equal 1em, most fonts do not follow. Most Latin fonts | |
| 454 // set to smaller than 1em, and many tall scripts set to larger than 1em. | |
| 455 // https://www.microsoft.com/typography/otspec/recom.htm#OS2 | |
| 456 // To ensure the sum of ascent and descent is the "em height", normalize by | |
| 457 // keeping the ratio of sTypoAscender:sTypoDescender. | |
| 458 // This matches to how Gecko computes "em height": | |
|
drott
2017/05/08 08:04:00
Would be great if you had a Mozilla DXR link to wh
kojii
2017/05/09 04:37:16
Yeah, not great at reading Mozilla code yet...
| |
| 459 // https://github.com/whatwg/html/issues/2470#issuecomment-291425136 | |
| 460 float em_height = PlatformData().size(); | |
| 461 em_height_ascent_ = LayoutUnit::FromFloatRound(ascent * em_height / height); | |
| 462 em_height_descent_ = | |
| 463 LayoutUnit::FromFloatRound(em_height) - em_height_ascent_; | |
| 464 return true; | |
| 465 } | |
| 466 | |
| 382 FloatRect SimpleFontData::PlatformBoundsForGlyph(Glyph glyph) const { | 467 FloatRect SimpleFontData::PlatformBoundsForGlyph(Glyph glyph) const { |
| 383 if (!platform_data_.size()) | 468 if (!platform_data_.size()) |
| 384 return FloatRect(); | 469 return FloatRect(); |
| 385 | 470 |
| 386 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); | 471 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); |
| 387 | 472 |
| 388 SkRect bounds; | 473 SkRect bounds; |
| 389 SkiaTextMetrics(&paint_).GetSkiaBoundsForGlyph(glyph, &bounds); | 474 SkiaTextMetrics(&paint_).GetSkiaBoundsForGlyph(glyph, &bounds); |
| 390 return FloatRect(bounds); | 475 return FloatRect(bounds); |
| 391 } | 476 } |
| 392 | 477 |
| 393 float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const { | 478 float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const { |
| 394 if (!platform_data_.size()) | 479 if (!platform_data_.size()) |
| 395 return 0; | 480 return 0; |
| 396 | 481 |
| 397 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); | 482 static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); |
| 398 | 483 |
| 399 return SkiaTextMetrics(&paint_).GetSkiaWidthForGlyph(glyph); | 484 return SkiaTextMetrics(&paint_).GetSkiaWidthForGlyph(glyph); |
| 400 } | 485 } |
| 401 | 486 |
| 402 } // namespace blink | 487 } // namespace blink |
| OLD | NEW |