Index: ui/gfx/render_text.cc |
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc |
index e6e7398cafff6187c393c7752ec90282ae0e69a4..c5e3de7409f961fe3dd658f05641178649ce197f 100644 |
--- a/ui/gfx/render_text.cc |
+++ b/ui/gfx/render_text.cc |
@@ -20,6 +20,7 @@ |
#include "third_party/icu/source/common/unicode/rbbi.h" |
#include "third_party/icu/source/common/unicode/utf16.h" |
#include "third_party/skia/include/core/SkDrawLooper.h" |
+#include "third_party/skia/include/core/SkFontStyle.h" |
#include "third_party/skia/include/core/SkTypeface.h" |
#include "third_party/skia/include/effects/SkGradientShader.h" |
#include "ui/gfx/canvas.h" |
@@ -90,16 +91,6 @@ int DetermineBaselineCenteringText(const Rect& display_rect, |
return baseline + std::max(min_shift, std::min(max_shift, baseline_shift)); |
} |
-#if !defined(OS_MACOSX) |
-// Converts |Font::FontStyle| flags to |SkTypeface::Style| flags. |
-SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) { |
- int skia_style = SkTypeface::kNormal; |
- skia_style |= (font_style & Font::BOLD) ? SkTypeface::kBold : 0; |
- skia_style |= (font_style & Font::ITALIC) ? SkTypeface::kItalic : 0; |
- return static_cast<SkTypeface::Style>(skia_style); |
-} |
-#endif |
- |
int round(float value) { |
return static_cast<int>(floor(value + 0.5f)); |
} |
@@ -253,15 +244,18 @@ void SkiaTextRenderer::SetTextSize(SkScalar size) { |
paint_.setTextSize(size); |
} |
-void SkiaTextRenderer::SetFontWithStyle(const Font& font, int style) { |
- skia::RefPtr<SkTypeface> typeface = CreateSkiaTypeface(font, style); |
+void SkiaTextRenderer::SetFont(const Font& font, |
+ bool italic, |
+ gfx::Font::Weight weight) { |
+ skia::RefPtr<SkTypeface> typeface = CreateSkiaTypeface(font, italic, weight); |
if (typeface) { |
// |paint_| adds its own ref. So don't |release()| it from the ref ptr here. |
SetTypeface(typeface.get()); |
// Enable fake bold text if bold style is needed but new typeface does not |
// have it. |
- paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); |
+ paint_.setFakeBoldText(weight >= gfx::Font::Weight::SEMIBOLD && |
+ !typeface->isBold()); |
} |
} |
@@ -384,10 +378,15 @@ void SkiaTextRenderer::DiagonalStrike::Draw() { |
StyleIterator::StyleIterator(const BreakList<SkColor>& colors, |
const BreakList<BaselineStyle>& baselines, |
+ const BreakList<gfx::Font::Weight>& weights, |
const std::vector<BreakList<bool>>& styles) |
- : colors_(colors), baselines_(baselines), styles_(styles) { |
+ : colors_(colors), |
+ baselines_(baselines), |
+ weights_(weights), |
+ styles_(styles) { |
color_ = colors_.breaks().begin(); |
baseline_ = baselines_.breaks().begin(); |
+ weight_ = weights_.breaks().begin(); |
for (size_t i = 0; i < styles_.size(); ++i) |
style_.push_back(styles_[i].breaks().begin()); |
} |
@@ -397,6 +396,7 @@ StyleIterator::~StyleIterator() {} |
Range StyleIterator::GetRange() const { |
Range range(colors_.GetRange(color_)); |
range = range.Intersect(baselines_.GetRange(baseline_)); |
+ range = range.Intersect(weights_.GetRange(weight_)); |
for (size_t i = 0; i < NUM_TEXT_STYLES; ++i) |
range = range.Intersect(styles_[i].GetRange(style_[i])); |
return range; |
@@ -405,6 +405,7 @@ Range StyleIterator::GetRange() const { |
void StyleIterator::UpdatePosition(size_t position) { |
color_ = colors_.GetBreak(position); |
baseline_ = baselines_.GetBreak(position); |
+ weight_ = weights_.GetBreak(position); |
for (size_t i = 0; i < NUM_TEXT_STYLES; ++i) |
style_[i] = styles_[i].GetBreak(position); |
} |
@@ -420,8 +421,12 @@ Line::Line(const Line& other) = default; |
Line::~Line() {} |
#if !defined(OS_MACOSX) |
-skia::RefPtr<SkTypeface> CreateSkiaTypeface(const gfx::Font& font, int style) { |
- SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style); |
+skia::RefPtr<SkTypeface> CreateSkiaTypeface(const gfx::Font& font, |
+ bool italic, |
+ gfx::Font::Weight weight) { |
+ SkFontStyle skia_style( |
+ static_cast<int>(weight), SkFontStyle::kNormal_Width, |
+ italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant); |
return skia::AdoptRef( |
SkTypeface::CreateFromName(font.GetFontName().c_str(), skia_style)); |
} |
@@ -471,6 +476,7 @@ void RenderText::SetText(const base::string16& text) { |
// the first style to the whole text instead. |
colors_.SetValue(colors_.breaks().begin()->second); |
baselines_.SetValue(baselines_.breaks().begin()->second); |
+ weights_.SetValue(weights_.breaks().begin()->second); |
for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) |
styles_[style].SetValue(styles_[style].breaks().begin()->second); |
cached_bounds_and_offset_valid_ = false; |
@@ -506,9 +512,9 @@ void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { |
void RenderText::SetFontList(const FontList& font_list) { |
font_list_ = font_list; |
const int font_style = font_list.GetFontStyle(); |
- SetStyle(BOLD, (font_style & gfx::Font::BOLD) != 0); |
- SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0); |
- SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0); |
+ weights_.SetValue(font_list.GetFontWeight()); |
+ styles_[ITALIC].SetValue((font_style & gfx::Font::ITALIC) != 0); |
+ styles_[UNDERLINE].SetValue((font_style & gfx::Font::UNDERLINE) != 0); |
baseline_ = kInvalidBaseline; |
cached_bounds_and_offset_valid_ = false; |
OnLayoutTextAttributeChanged(false); |
@@ -768,6 +774,20 @@ void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) { |
OnLayoutTextAttributeChanged(false); |
} |
+void RenderText::SetWeight(gfx::Font::Weight weight) { |
+ weights_.SetValue(weight); |
+ |
+ cached_bounds_and_offset_valid_ = false; |
+ OnLayoutTextAttributeChanged(false); |
+} |
+ |
+void RenderText::ApplyWeight(gfx::Font::Weight weight, const Range& range) { |
+ weights_.ApplyValue(weight, range); |
+ |
+ cached_bounds_and_offset_valid_ = false; |
+ OnLayoutTextAttributeChanged(false); |
+} |
+ |
bool RenderText::GetStyle(TextStyle style) const { |
return (styles_[style].breaks().size() == 1) && |
styles_[style].breaks().front().second; |
@@ -999,6 +1019,7 @@ RenderText::RenderText() |
composition_range_(Range::InvalidRange()), |
colors_(kDefaultColor), |
baselines_(NORMAL_BASELINE), |
+ weights_(gfx::Font::Weight::NORMAL), |
styles_(NUM_TEXT_STYLES), |
composition_and_selection_styles_applied_(false), |
obscured_(false), |
@@ -1013,8 +1034,7 @@ RenderText::RenderText() |
subpixel_rendering_suppressed_(false), |
clip_to_display_rect_(true), |
baseline_(kInvalidBaseline), |
- cached_bounds_and_offset_valid_(false) { |
-} |
+ cached_bounds_and_offset_valid_(false) {} |
SelectionModel RenderText::GetAdjacentSelectionModel( |
const SelectionModel& current, |
@@ -1285,6 +1305,7 @@ void RenderText::UpdateStyleLengths() { |
const size_t text_length = text_.length(); |
colors_.SetMax(text_length); |
baselines_.SetMax(text_length); |
+ weights_.SetMax(text_length); |
for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) |
styles_[style].SetMax(text_length); |
} |
@@ -1388,6 +1409,7 @@ base::string16 RenderText::Elide(const base::string16& text, |
render_text->styles_ = styles_; |
render_text->baselines_ = baselines_; |
render_text->colors_ = colors_; |
+ render_text->weights_ = weights_; |
if (text_width == 0) { |
render_text->SetText(text); |
text_width = render_text->GetContentWidthF(); |
@@ -1445,6 +1467,7 @@ base::string16 RenderText::Elide(const base::string16& text, |
for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) |
RestoreBreakList(render_text.get(), &render_text->styles_[style]); |
RestoreBreakList(render_text.get(), &render_text->baselines_); |
+ RestoreBreakList(render_text.get(), &render_text->weights_); |
// We check the width of the whole desired string at once to ensure we |
// handle kerning/ligatures/etc. correctly. |