From b4bb7d825566042ed64697be49457dbac060e6c4 Mon Sep 17 00:00:00 2001 From: bungeman Date: Wed, 27 Apr 2016 10:21:04 -0700 Subject: Add oblique as a slant. Some fonts have italic and oblique in the same family, see http://lucidafonts.com/fonts/family/lucida-sans http://www.gust.org.pl/projects/e-foundry/latin-modern GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1921903002 Chromium side change at https://crrev.com/1921503006/ . Review-Url: https://codereview.chromium.org/1921903002 --- src/core/SkFontMgr.cpp | 63 ++++++++++++++++++++++++++++++------------------ src/core/SkFontStyle.cpp | 2 +- src/core/SkTypeface.cpp | 9 +------ 3 files changed, 41 insertions(+), 33 deletions(-) (limited to 'src/core') diff --git a/src/core/SkFontMgr.cpp b/src/core/SkFontMgr.cpp index eba2b28fd6..ebb9a89662 100644 --- a/src/core/SkFontMgr.cpp +++ b/src/core/SkFontMgr.cpp @@ -211,6 +211,9 @@ SkTypeface* SkFontStyleSet::matchStyleCSS3(const SkFontStyle& pattern) { struct Score { int score; int index; + Score& operator +=(int rhs) { this->score += rhs; return *this; } + Score& operator <<=(int rhs) { this->score <<= rhs; return *this; } + bool operator <(const Score& that) { return this->score < that.score; } }; Score maxScore = { 0, 0 }; @@ -219,58 +222,70 @@ SkTypeface* SkFontStyleSet::matchStyleCSS3(const SkFontStyle& pattern) { this->getStyle(i, ¤t, nullptr); Score currentScore = { 0, i }; - // CSS stretch. (This is the width.) - // This has the highest priority. + // CSS stretch / SkFontStyle::Width + // Takes priority over everything else. if (pattern.width() <= SkFontStyle::kNormal_Width) { if (current.width() <= pattern.width()) { - currentScore.score += 10 - pattern.width() + current.width(); + currentScore += 10 - pattern.width() + current.width(); } else { - currentScore.score += 10 - current.width(); + currentScore += 10 - current.width(); } } else { if (current.width() > pattern.width()) { - currentScore.score += 10 + pattern.width() - current.width(); + currentScore += 10 + pattern.width() - current.width(); } else { - currentScore.score += current.width(); + currentScore += current.width(); } } - currentScore.score *= 1002; - - // CSS style (italic/oblique) - // Being italic trumps all valid weights which are not italic. - // Note that newer specs differentiate between italic and oblique. - if (pattern.isItalic() == current.isItalic()) { - currentScore.score += 1001; - } - - // Synthetics (weight/style) [no stretch synthetic?] - + currentScore <<= 8; + + // CSS style (normal, italic, oblique) / SkFontStyle::Slant (upright, italic, oblique) + // Takes priority over all valid weights. + static_assert(SkFontStyle::kUpright_Slant == 0 && + SkFontStyle::kItalic_Slant == 1 && + SkFontStyle::kOblique_Slant == 2, + "SkFontStyle::Slant values not as required."); + SkASSERT(0 <= pattern.slant() && pattern.slant() <= 2 && + 0 <= current.slant() && current.slant() <= 2); + static const int score[3][3] = { + /* Upright Italic Oblique [current]*/ + /* Upright */ { 3 , 1 , 2 }, + /* Italic */ { 1 , 3 , 2 }, + /* Oblique */ { 1 , 2 , 3 }, + /* [pattern] */ + }; + currentScore += score[pattern.slant()][current.slant()]; + currentScore <<= 8; + + // Synthetics (weight, style) [no stretch synthetic?] + + // CSS weight / SkFontStyle::Weight // The 'closer' to the target weight, the higher the score. // 1000 is the 'heaviest' recognized weight if (pattern.weight() == current.weight()) { - currentScore.score += 1000; + currentScore += 1000; } else if (pattern.weight() <= 500) { if (400 <= pattern.weight() && pattern.weight() < 450) { if (450 <= current.weight() && current.weight() <= 500) { // Artificially boost the 500 weight. // TODO: determine correct number to use. - currentScore.score += 500; + currentScore += 500; } } if (current.weight() <= pattern.weight()) { - currentScore.score += 1000 - pattern.weight() + current.weight(); + currentScore += 1000 - pattern.weight() + current.weight(); } else { - currentScore.score += 1000 - current.weight(); + currentScore += 1000 - current.weight(); } } else if (pattern.weight() > 500) { if (current.weight() > pattern.weight()) { - currentScore.score += 1000 + pattern.weight() - current.weight(); + currentScore += 1000 + pattern.weight() - current.weight(); } else { - currentScore.score += current.weight(); + currentScore += current.weight(); } } - if (currentScore.score > maxScore.score) { + if (maxScore < currentScore) { maxScore = currentScore; } } diff --git a/src/core/SkFontStyle.cpp b/src/core/SkFontStyle.cpp index c28e721337..01628ae9d9 100644 --- a/src/core/SkFontStyle.cpp +++ b/src/core/SkFontStyle.cpp @@ -20,7 +20,7 @@ SkFontStyle::SkFontStyle(int weight, int width, Slant slant) { fUnion.fU32 = 0; fUnion.fR.fWeight = SkTPin(weight, kThin_Weight, kBlack_Weight); fUnion.fR.fWidth = SkTPin(width, kUltraCondensed_Width, kUltaExpanded_Width); - fUnion.fR.fSlant = SkTPin(slant, kUpright_Slant, kItalic_Slant); + fUnion.fR.fSlant = SkTPin(slant, kUpright_Slant, kOblique_Slant); } /*static*/SkFontStyle SkFontStyle::FromOldStyle(unsigned oldStyle) { diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp index 855ef909e8..d768efc603 100644 --- a/src/core/SkTypeface.cpp +++ b/src/core/SkTypeface.cpp @@ -138,14 +138,7 @@ SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { } SkAutoTUnref fm(SkFontMgr::RefDefault()); - bool bold = s & SkTypeface::kBold; - bool italic = s & SkTypeface::kItalic; - SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight - : SkFontStyle::kNormal_Weight, - SkFontStyle::kNormal_Width, - italic ? SkFontStyle::kItalic_Slant - : SkFontStyle::kUpright_Slant); - return fm->matchFaceStyle(family, newStyle); + return fm->matchFaceStyle(family, SkFontStyle::FromOldStyle(s)); } SkTypeface* SkTypeface::CreateFromStream(SkStreamAsset* stream, int index) { -- cgit v1.2.3