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

Unified Diff: ui/gfx/render_text.cc

Issue 1819753003: Allow various font weights in gfx. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Alexei's issues Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: ui/gfx/render_text.cc
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index b133b976d9657666d05cdb5200062eee1aae0c01..1abdf5b7be258f1fbd4ba783acf7db5176437e7f 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));
}
@@ -257,15 +248,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) {
Alexei Svitkine (slow) 2016/04/05 16:38:53 Remove gfx::
+ 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 &&
Alexei Svitkine (slow) 2016/04/05 16:38:52 Remove gfx::
+ !typeface->isBold());
}
}
@@ -388,10 +382,15 @@ void SkiaTextRenderer::DiagonalStrike::Draw() {
StyleIterator::StyleIterator(const BreakList<SkColor>& colors,
const BreakList<BaselineStyle>& baselines,
+ const BreakList<gfx::Font::Weight>& weights,
Alexei Svitkine (slow) 2016/04/05 16:38:53 Remove gfx::
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());
}
@@ -401,6 +400,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;
@@ -409,6 +409,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);
}
@@ -424,8 +425,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,
Alexei Svitkine (slow) 2016/04/05 16:38:53 Remove gfx::
+ bool italic,
+ gfx::Font::Weight weight) {
Alexei Svitkine (slow) 2016/04/05 16:38:53 Remove gfx::
+ 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));
}
@@ -475,6 +480,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;
@@ -510,9 +516,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);
@@ -772,6 +778,20 @@ void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) {
OnLayoutTextAttributeChanged(false);
}
+void RenderText::SetWeight(gfx::Font::Weight weight) {
Alexei Svitkine (slow) 2016/04/05 16:38:53 Remove gfx::
+ weights_.SetValue(weight);
+
+ cached_bounds_and_offset_valid_ = false;
+ OnLayoutTextAttributeChanged(false);
+}
+
+void RenderText::ApplyWeight(gfx::Font::Weight weight, const Range& range) {
Alexei Svitkine (slow) 2016/04/05 16:38:52 Remove gfx::
+ 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;
@@ -1003,6 +1023,7 @@ RenderText::RenderText()
composition_range_(Range::InvalidRange()),
colors_(kDefaultColor),
baselines_(NORMAL_BASELINE),
+ weights_(gfx::Font::Weight::NORMAL),
Alexei Svitkine (slow) 2016/04/05 16:38:53 Remove gfx::
styles_(NUM_TEXT_STYLES),
composition_and_selection_styles_applied_(false),
obscured_(false),
@@ -1017,8 +1038,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,
@@ -1286,6 +1306,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);
}
@@ -1389,6 +1410,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();
@@ -1446,6 +1468,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.

Powered by Google App Engine
This is Rietveld 408576698