Skip to content

Commit

Permalink
Add FontRenderParamsQuery.
Browse files Browse the repository at this point in the history
Add a new struct that encapsulates the parameters used to
initialize FontRenderParams.

Also remove all the existing FontRenderParams functions in
favor of a single GetFontRenderParams() function that takes
a FontRenderParamsQuery.

BUG=395313,396659
TBR=boliu@chromium.org

Review URL: https://codereview.chromium.org/413003002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285425 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
derat@chromium.org committed Jul 25, 2014
1 parent d455fd3 commit 516b2c7
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 173 deletions.
4 changes: 3 additions & 1 deletion android_webview/native/aw_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "android_webview/native/aw_contents.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/macros.h"
#include "base/supports_user_data.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
Expand Down Expand Up @@ -74,7 +75,8 @@ void PopulateFixedRendererPreferences(RendererPreferences* prefs) {
content::TAP_MULTIPLE_TARGETS_STRATEGY_NONE;

// TODO(boliu): Deduplicate with chrome/ code.
const gfx::FontRenderParams& params = gfx::GetDefaultWebKitFontRenderParams();
CR_DEFINE_STATIC_LOCAL(const gfx::FontRenderParams, params,
(gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(true), NULL)));
prefs->should_antialias_text = params.antialiasing;
prefs->use_subpixel_positioning = params.subpixel_positioning;
prefs->hinting = GetRendererPreferencesHintingEnum(params.hinting);
Expand Down
4 changes: 3 additions & 1 deletion chrome/browser/renderer_preferences_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chrome/browser/renderer_preferences_util.h"

#include "base/macros.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
Expand Down Expand Up @@ -131,7 +132,8 @@ void UpdateFromSystemSettings(
#endif

#if defined(OS_LINUX) || defined(OS_ANDROID)
const gfx::FontRenderParams& params = gfx::GetDefaultWebKitFontRenderParams();
CR_DEFINE_STATIC_LOCAL(const gfx::FontRenderParams, params,
(gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(true), NULL)));
prefs->should_antialias_text = params.antialiasing;
prefs->use_subpixel_positioning = params.subpixel_positioning;
prefs->hinting = GetRendererPreferencesHintingEnum(params.hinting);
Expand Down
6 changes: 4 additions & 2 deletions content/browser/renderer_host/sandbox_ipc_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/command_line.h"
#include "base/files/scoped_file.h"
#include "base/linux_util.h"
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/shared_memory.h"
#include "base/posix/eintr_wrapper.h"
Expand Down Expand Up @@ -41,8 +42,9 @@ SandboxIPCHandler::SandboxIPCHandler(int lifeline_fd, int browser_socket)
: lifeline_fd_(lifeline_fd), browser_socket_(browser_socket) {
// FontConfig doesn't provide a standard property to control subpixel
// positioning, so we pass the current setting through to WebKit.
WebFontInfo::setSubpixelPositioning(
gfx::GetDefaultWebKitFontRenderParams().subpixel_positioning);
CR_DEFINE_STATIC_LOCAL(const gfx::FontRenderParams, params,
(gfx::GetFontRenderParams(gfx::FontRenderParamsQuery(true), NULL)));
WebFontInfo::setSubpixelPositioning(params.subpixel_positioning);
}

void SandboxIPCHandler::Run() {
Expand Down
9 changes: 9 additions & 0 deletions ui/gfx/font_render_params.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ FontRenderParams::FontRenderParams()

FontRenderParams::~FontRenderParams() {}

FontRenderParamsQuery::FontRenderParamsQuery(bool for_web_contents)
: for_web_contents(for_web_contents),
pixel_size(0),
point_size(0),
style(-1) {
}

FontRenderParamsQuery::~FontRenderParamsQuery() {}

} // namespace gfx
49 changes: 32 additions & 17 deletions ui/gfx/font_render_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ struct GFX_EXPORT FontRenderParams {

// Should subpixel positioning (i.e. fractional X positions for glyphs) be
// used?
// TODO(derat): Remove this; we don't set it in the browser and mostly ignore
// it in Blink: http://crbug.com/396659
bool subpixel_positioning;

// Should FreeType's autohinter be used (as opposed to Freetype's bytecode
Expand All @@ -57,23 +59,36 @@ struct GFX_EXPORT FontRenderParams {
SubpixelRendering subpixel_rendering;
};

// Returns the system's default parameters for font rendering.
GFX_EXPORT const FontRenderParams& GetDefaultFontRenderParams();

// Returns the system's default parameters for WebKit font rendering.
// TODO(derat): Rename to GetDefaultFontRenderParamsForWebContents().
GFX_EXPORT const FontRenderParams& GetDefaultWebKitFontRenderParams();

// Returns the appropriate parameters for rendering the font described by the
// passed-in-arguments, any of which may be NULL. If |family_out| is non-NULL,
// it will be updated to contain the recommended font family from |family_list|.
// |style| optionally points to a bit field of Font::FontStyle values.
GFX_EXPORT FontRenderParams GetCustomFontRenderParams(
bool for_web_contents,
const std::vector<std::string>* family_list,
const int* pixel_size,
const int* point_size,
const int* style,
// A query used to determine the appropriate FontRenderParams.
struct GFX_EXPORT FontRenderParamsQuery {
explicit FontRenderParamsQuery(bool for_web_contents);
~FontRenderParamsQuery();

bool is_empty() const {
return families.empty() && pixel_size <= 0 && point_size <= 0 && style < 0;
}

// True if rendering text for the web.
// TODO(derat): Remove this once FontRenderParams::subpixel_positioning is
// gone: http://crbug.com/396659
bool for_web_contents;

// Requested font families, or empty if unset.
std::vector<std::string> families;

// Font size in pixels or points, or 0 if unset.
int pixel_size;
int point_size;

// gfx::Font::FontStyle bit field, or -1 if unset.
int style;
};

// Returns the appropriate parameters for rendering the font described by
// |query|. If |family_out| is non-NULL, it will be updated to contain the
// recommended font family from |query.families|.
GFX_EXPORT FontRenderParams GetFontRenderParams(
const FontRenderParamsQuery& query,
std::string* family_out);

} // namespace gfx
Expand Down
27 changes: 9 additions & 18 deletions ui/gfx/font_render_params_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ui/gfx/font_render_params.h"

#include "base/logging.h"
#include "base/macros.h"

namespace gfx {

Expand All @@ -29,24 +30,14 @@ FontRenderParams LoadDefaults() {

} // namespace

const FontRenderParams& GetDefaultFontRenderParams() {
static FontRenderParams default_params = LoadDefaults();
return default_params;
}

const FontRenderParams& GetDefaultWebKitFontRenderParams() {
return GetDefaultFontRenderParams();
}

FontRenderParams GetCustomFontRenderParams(
bool for_web_contents,
const std::vector<std::string>* family_list,
const int* pixel_size,
const int* point_size,
const int* style,
std::string* family_out) {
NOTIMPLEMENTED();
return GetDefaultFontRenderParams();
FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
std::string* family_out) {
// Customized font rendering settings are not supported, only defaults.
if (!query.is_empty() || family_out)
NOTIMPLEMENTED();
CR_DEFINE_STATIC_LOCAL(
const gfx::FontRenderParams, params, (LoadDefaults()));
return params;
}

} // namespace gfx
67 changes: 19 additions & 48 deletions ui/gfx/font_render_params_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,29 @@ FontRenderParams::SubpixelRendering ConvertFontconfigRgba(int rgba) {
}

// Queries Fontconfig for rendering settings and updates |params_out| and
// |family_out| (if non-NULL). Returns false on failure. See
// GetCustomFontRenderParams() for descriptions of arguments.
bool QueryFontconfig(const std::vector<std::string>* family_list,
const int* pixel_size,
const int* point_size,
const int* style,
// |family_out| (if non-NULL). Returns false on failure.
bool QueryFontconfig(const FontRenderParamsQuery& query,
FontRenderParams* params_out,
std::string* family_out) {
FcPattern* pattern = FcPatternCreate();
CHECK(pattern);

FcPatternAddBool(pattern, FC_SCALABLE, FcTrue);

if (family_list) {
for (std::vector<std::string>::const_iterator it = family_list->begin();
it != family_list->end(); ++it) {
FcPatternAddString(
pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(it->c_str()));
}
for (std::vector<std::string>::const_iterator it = query.families.begin();
it != query.families.end(); ++it) {
FcPatternAddString(
pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(it->c_str()));
}
if (pixel_size)
FcPatternAddDouble(pattern, FC_PIXEL_SIZE, *pixel_size);
if (point_size)
FcPatternAddInteger(pattern, FC_SIZE, *point_size);
if (style) {
if (query.pixel_size > 0)
FcPatternAddDouble(pattern, FC_PIXEL_SIZE, query.pixel_size);
if (query.point_size > 0)
FcPatternAddInteger(pattern, FC_SIZE, query.point_size);
if (query.style >= 0) {
FcPatternAddInteger(pattern, FC_SLANT,
(*style & Font::ITALIC) ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
(query.style & Font::ITALIC) ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
FcPatternAddInteger(pattern, FC_WEIGHT,
(*style & Font::BOLD) ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL);
(query.style & Font::BOLD) ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL);
}

FcConfigSubstitute(NULL, pattern, FcMatchPattern);
Expand Down Expand Up @@ -120,31 +114,10 @@ bool QueryFontconfig(const std::vector<std::string>* family_list,
return true;
}

// Returns the system's default settings.
FontRenderParams LoadDefaults(bool for_web_contents) {
return GetCustomFontRenderParams(
for_web_contents, NULL, NULL, NULL, NULL, NULL);
}

} // namespace

const FontRenderParams& GetDefaultFontRenderParams() {
static FontRenderParams default_params = LoadDefaults(false);
return default_params;
}

const FontRenderParams& GetDefaultWebKitFontRenderParams() {
static FontRenderParams default_params = LoadDefaults(true);
return default_params;
}

FontRenderParams GetCustomFontRenderParams(
bool for_web_contents,
const std::vector<std::string>* family_list,
const int* pixel_size,
const int* point_size,
const int* style,
std::string* family_out) {
FontRenderParams GetFontRenderParams(const FontRenderParamsQuery& query,
std::string* family_out) {
if (family_out)
family_out->clear();

Expand All @@ -153,12 +126,11 @@ FontRenderParams GetCustomFontRenderParams(
const LinuxFontDelegate* delegate = LinuxFontDelegate::instance();
if (delegate)
params = delegate->GetDefaultFontRenderParams();
QueryFontconfig(
family_list, pixel_size, point_size, style, &params, family_out);
QueryFontconfig(query, &params, family_out);

// Fontconfig doesn't support configuring subpixel positioning; check a flag.
params.subpixel_positioning = CommandLine::ForCurrentProcess()->HasSwitch(
for_web_contents ?
query.for_web_contents ?
switches::kEnableWebkitTextSubpixelPositioning :
switches::kEnableBrowserTextSubpixelPositioning);

Expand All @@ -167,9 +139,8 @@ FontRenderParams GetCustomFontRenderParams(
params.hinting = FontRenderParams::HINTING_NONE;

// Use the first family from the list if Fontconfig didn't suggest a family.
if (family_out && family_out->empty() &&
family_list && !family_list->empty())
*family_out = (*family_list)[0];
if (family_out && family_out->empty() && !query.families.empty())
*family_out = query.families[0];

return params;
}
Expand Down
Loading

0 comments on commit 516b2c7

Please sign in to comment.