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

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: Created 4 years, 9 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 e6e7398cafff6187c393c7752ec90282ae0e69a4..23d57bcfa77ee75e0bc1ce9596bf14e6536e96db 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"
@@ -94,7 +95,7 @@ int DetermineBaselineCenteringText(const Rect& display_rect,
// Converts |Font::FontStyle| flags to |SkTypeface::Style| flags.
SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) {
msw 2016/03/22 01:53:44 Remove this function if it's no longer used.
Mikus 2016/03/22 14:19:51 Done.
int skia_style = SkTypeface::kNormal;
- skia_style |= (font_style & Font::BOLD) ? SkTypeface::kBold : 0;
+ // skia_style |= (font_style & Font::BOLD) ? SkTypeface::kBold : 0;
msw 2016/03/22 01:53:44 Remove this
Mikus 2016/03/22 14:19:51 Done.
skia_style |= (font_style & Font::ITALIC) ? SkTypeface::kItalic : 0;
return static_cast<SkTypeface::Style>(skia_style);
}
@@ -253,15 +254,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::SetFontWithStyle(const Font& font,
+ int style,
+ gfx::Font::FontWeight weight) {
+ skia::RefPtr<SkTypeface> typeface = CreateSkiaTypeface(font, style, 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 +388,15 @@ void SkiaTextRenderer::DiagonalStrike::Draw() {
StyleIterator::StyleIterator(const BreakList<SkColor>& colors,
const BreakList<BaselineStyle>& baselines,
+ const BreakList<gfx::Font::FontWeight>& 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 +406,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 +415,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,11 +431,17 @@ 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);
- return skia::AdoptRef(
- SkTypeface::CreateFromName(font.GetFontName().c_str(), skia_style));
+skia::RefPtr<SkTypeface> CreateSkiaTypeface(const gfx::Font& font,
+ int style,
+ gfx::Font::FontWeight weight) {
+ SkFontStyle skia_style(weight, SkFontStyle::kNormal_Width,
+ style & gfx::Font::ITALIC
msw 2016/03/22 01:53:44 nit: if |style| only handles italic, make it a boo
Mikus 2016/03/22 14:19:51 Done.
+ ? SkFontStyle::kItalic_Slant
+ : SkFontStyle::kUpright_Slant);
+ return skia::AdoptRef(SkTypeface::CreateFromNameAndStyle(
+ font.GetFontName().c_str(), skia_style));
}
+
#endif
void ApplyRenderParams(const FontRenderParams& params,
@@ -471,6 +488,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,7 +524,7 @@ 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);
+ weights_.SetValue(font_list.GetFontWeight());
SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0);
SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0);
baseline_ = kInvalidBaseline;
@@ -768,6 +786,20 @@ void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) {
OnLayoutTextAttributeChanged(false);
}
+void RenderText::SetWeight(gfx::Font::FontWeight weight) {
+ weights_.SetValue(weight);
+
+ cached_bounds_and_offset_valid_ = false;
+ OnLayoutTextAttributeChanged(false);
+}
+
+void RenderText::ApplyWeight(gfx::Font::FontWeight 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 +1031,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 +1046,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 +1317,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 +1421,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 +1479,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