chrome.fontSettings
The Font Settings API allows you to manage Chrome's font settings.
Manifest
To use the Font Settings API, you must declare the "fontSettings" permission
in the extension manifest.
For example:
{
"name": "My Font Settings Extension",
"description": "Customize your fonts",
"version": "0.2",
"permissions": ["fontSettings"]
}
Generic Font Families and Scripts
Chrome allows for some font settings to depend on certain generic font
families and language scripts. For example, the font used for sans-serif
Simplified Chinese may be different than the font used for serif Japanese.
The generic font families supported by Chrome are based on
CSS generic font families
and are listed in the API reference below. When a webpage specifies a generic
font family, Chrome selects the font based on the corresponding setting. If no
generic font family is specified, Chrome uses the setting for the "standard"
generic font family.
When a webpage specifies a language, Chrome selects the font based on the
setting for the corresponding language script. If no language is specified,
Chrome uses the setting for the default, or global, script.
The supported language scripts are specified by ISO 15924 script code and
listed in the API reference below. Technically, Chrome settings are not strictly
per-script but also depend on language. For example, Chrome chooses the font for
Cyrillic (ISO 15924 script code "Cyrl") when a webpage specifies the Russian
language, and uses this font not just for Cyrillic script but for everything the
font covers, such as Latin.
Examples
The following code gets the standard font for Arabic.
chrome.fontSettings.getFont(
{ genericFamily: 'standard', script: 'Arab' },
function(details) { console.log(details.fontId); }
);
The next snippet sets the sans-serif font for Japanese.
chrome.fontSettings.setFont(
{ genericFamily: 'sansserif', script: 'Jpan', fontId: 'MS PGothic' }
);
You can find a sample extension using the Font Settings API in the
examples/api/fontSettings
directory. For other examples and for help in viewing the source code, see
Samples.
chrome.fontSettings reference
Types
FontName
Represents a font name.
-
fontId
(
string
)
-
The font ID.
-
displayName
(
string
)
-
The display name of the font.
ScriptCode
An ISO 15924 script code. The default, or global, script is represented by script code "Zyyy".
GenericFamily
A CSS generic font family.
LevelOfControl
One of
not_controllable: cannot be controlled by any extension
controlled_by_other_extensions: controlled by extensions with higher precedence
controllable_by_this_extension: can be controlled by this extension
controlled_by_this_extension: controlled by this extension
Methods
clearFont
chrome.fontSettings.clearFont(object details, function callback)
Clears the font set by this extension, if any.
Parameters
-
details
(
object
)
-
-
script
(
optional
ScriptCode
)
-
The script for which the font should be cleared. If omitted, the global script font setting is cleared.
-
genericFamily
(
GenericFamily
)
-
The generic font family for which the font should be cleared.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
getFont
chrome.fontSettings.getFont(object details, function callback)
Gets the font for a given script and generic font family.
Parameters
-
details
(
object
)
-
-
script
(
optional
ScriptCode
)
-
The script for which the font should be retrieved. If omitted, the font setting for the global script (script code "Zyyy") is retrieved.
-
genericFamily
(
GenericFamily
)
-
The generic font family for which the font should be retrieved.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function(object details) {...};
-
details
(
object
)
-
-
fontId
(
string
)
-
The font ID. Rather than the literal font ID preference value, this may be the ID of the font that the system resolves the preference value to. So, fontId can differ from the font passed to
setFont, if, for example, the font is not available on the system. The empty string signifies fallback to the global script font setting.
-
levelOfControl
(
LevelOfControl
)
-
The level of control this extension has over the setting.
setFont
chrome.fontSettings.setFont(object details, function callback)
Sets the font for a given script and generic font family.
Parameters
-
details
(
object
)
-
-
script
(
optional
ScriptCode
)
-
The script code which the font should be set. If omitted, the font setting for the global script (script code "Zyyy") is set.
-
genericFamily
(
GenericFamily
)
-
The generic font family for which the font should be set.
-
fontId
(
string
)
-
The font ID. The empty string means to fallback to the global script font setting.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
getFontList
chrome.fontSettings.getFontList(function callback)
Gets a list of fonts on the system.
Parameters
Callback
The callback parameter should specify a function
that looks like this:
function(array of FontName results) {...};
clearDefaultFontSize
chrome.fontSettings.clearDefaultFontSize(object details, function callback)
Clears the default font size set by this extension, if any.
Parameters
-
details
(
optional
object
)
-
This parameter is currently unused.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
getDefaultFontSize
chrome.fontSettings.getDefaultFontSize(object details, function callback)
Gets the default font size.
Parameters
-
details
(
optional
object
)
-
This parameter is currently unused.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function(object details) {...};
-
details
(
object
)
-
-
pixelSize
(
integer
)
-
The font size in pixels.
-
levelOfControl
(
LevelOfControl
)
-
The level of control this extension has over the setting.
setDefaultFontSize
chrome.fontSettings.setDefaultFontSize(object details, function callback)
Sets the default font size.
Parameters
-
details
(
object
)
-
-
pixelSize
(
integer
)
-
The font size in pixels.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
clearDefaultFixedFontSize
chrome.fontSettings.clearDefaultFixedFontSize(object details, function callback)
Clears the default fixed font size set by this extension, if any.
Parameters
-
details
(
optional
object
)
-
This parameter is currently unused.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
getDefaultFixedFontSize
chrome.fontSettings.getDefaultFixedFontSize(object details, function callback)
Gets the default size for fixed width fonts.
Parameters
-
details
(
optional
object
)
-
This parameter is currently unused.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function(object details) {...};
-
details
(
object
)
-
-
pixelSize
(
integer
)
-
The font size in pixels.
-
levelOfControl
(
LevelOfControl
)
-
The level of control this extension has over the setting.
setDefaultFixedFontSize
chrome.fontSettings.setDefaultFixedFontSize(object details, function callback)
Sets the default size for fixed width fonts.
Parameters
-
details
(
object
)
-
-
pixelSize
(
integer
)
-
The font size in pixels.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
clearMinimumFontSize
chrome.fontSettings.clearMinimumFontSize(object details, function callback)
Clears the minimum font size set by this extension, if any.
Parameters
-
details
(
optional
object
)
-
This parameter is currently unused.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
getMinimumFontSize
chrome.fontSettings.getMinimumFontSize(object details, function callback)
Gets the minimum font size.
Parameters
-
details
(
optional
object
)
-
This parameter is currently unused.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function(object details) {...};
-
details
(
object
)
-
-
pixelSize
(
integer
)
-
The font size in pixels.
-
levelOfControl
(
LevelOfControl
)
-
The level of control this extension has over the setting.
setMinimumFontSize
chrome.fontSettings.setMinimumFontSize(object details, function callback)
Sets the minimum font size.
Parameters
-
details
(
object
)
-
-
pixelSize
(
integer
)
-
The font size in pixels.
-
callback
(
optional
function
)
Callback
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
Events
onFontChanged
chrome.fontSettings.onFontChanged.addListener(function(object details) {...});
Fired when a font setting changes.
Listener Parameters
- details ( object )
-
- fontId ( string )
- The font ID. See the description in
getFont. - script ( optional ScriptCode )
- The script code for which the font setting has changed.
- genericFamily ( GenericFamily )
- The generic font family for which the font setting has changed.
- levelOfControl ( LevelOfControl )
- The level of control this extension has over the setting.
onDefaultFontSizeChanged
chrome.fontSettings.onDefaultFontSizeChanged.addListener(function(object details) {...});
Fired when the default font size setting changes.
Listener Parameters
- details ( object )
-
- pixelSize ( integer )
- The font size in pixels.
- levelOfControl ( LevelOfControl )
- The level of control this extension has over the setting.
onDefaultFixedFontSizeChanged
chrome.fontSettings.onDefaultFixedFontSizeChanged.addListener(function(object details) {...});
Fired when the default fixed font size setting changes.
Listener Parameters
- details ( object )
-
- pixelSize ( integer )
- The font size in pixels.
- levelOfControl ( LevelOfControl )
- The level of control this extension has over the setting.
onMinimumFontSizeChanged
chrome.fontSettings.onMinimumFontSizeChanged.addListener(function(object details) {...});
Fired when the minimum font size setting changes.
Listener Parameters
- details ( object )
-
- pixelSize ( integer )
- The font size in pixels.
- levelOfControl ( LevelOfControl )
- The level of control this extension has over the setting.
Sample Extensions that use chrome.fontSettings
Advanced Font Settings
– Customize per-script font settings.