mirror of
https://github.com/polybar/polybar.git
synced 2024-12-09 14:10:19 -05:00
refactor(font): More robust font size calculation
Originally the size function returned the scaled `size` property for scalable fonts and the non-scaled `pixelsize` property for non-scalable fonts. This caused lots of issues when that property was 0 (empty bars, characters not drawn without warning, see references at the bottom). This behavior was mostly observed on debian where `size` is set to 0 if `pixelsize` is set. We now try to use both properties for both types, but prefering `size` for scalable fonts and `pixelsize` for non-scalable ones. This behavior doesn't break existing correct behavior but now never returns 0. It will always try to fall back to the other property or to some fallback value if both properties are 0. I originally thought this could also make font patterns more expressive by being able to specify the size of scalable fonts directly in pixels like so: Unifont:size=0:pixelsize=20 or to scale non-scalable fonts by forcing polybar to fall back to the `size` property (which is always scaled): Wuncon Siji:pixelsize=0:size=20 But how these two patterns are matched by `fc-match` depends both on the font and on the distro/fontconfig setup. Ref #706 Ref #1450 Ref #1257
This commit is contained in:
parent
a3e2e51ac9
commit
5be532c51b
1 changed files with 65 additions and 9 deletions
|
@ -112,19 +112,75 @@ namespace cairo {
|
||||||
return m_offset;
|
return m_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the font size in pixels for the given dpi
|
||||||
|
*
|
||||||
|
* We use the two font properties size and pixelsize. size is in points and
|
||||||
|
* needs to be scaled with the given dpi. pixelsize is not scaled.
|
||||||
|
*
|
||||||
|
* If both size properties are 0, we fall back to a default value of 10
|
||||||
|
* points for scalable fonts or 10 pixel for non-scalable ones. This should
|
||||||
|
* only happen if both properties are purposefully set to 0
|
||||||
|
*
|
||||||
|
* For scalable fonts we try to use the size property scaled according to
|
||||||
|
* the dpi.
|
||||||
|
* For non-scalable fonts we try to use the pixelsize property as-is
|
||||||
|
*/
|
||||||
double size(double dpi) const override {
|
double size(double dpi) const override {
|
||||||
bool scalable;
|
bool scalable;
|
||||||
double px;
|
double fc_pixelsize = 0, fc_size = 0;
|
||||||
|
|
||||||
property(FC_SCALABLE, &scalable);
|
property(FC_SCALABLE, &scalable);
|
||||||
if (scalable) {
|
|
||||||
// convert from pt to px using the provided dpi
|
// Size in points
|
||||||
property(FC_SIZE, &px);
|
property(FC_SIZE, &fc_size);
|
||||||
px = static_cast<int>(px * dpi / 72.0 + 0.5);
|
|
||||||
} else {
|
// Size in pixels
|
||||||
property(FC_PIXEL_SIZE, &px);
|
property(FC_PIXEL_SIZE, &fc_pixelsize);
|
||||||
px = static_cast<int>(px + 0.5);
|
|
||||||
|
// Fall back to a default value if the size is 0
|
||||||
|
double pixelsize = fc_pixelsize == 0? 10 : fc_pixelsize;
|
||||||
|
double size = fc_size == 0? 10 : fc_size;
|
||||||
|
|
||||||
|
// Font size in pixels if we use the pixelsize property
|
||||||
|
int px_pixelsize = pixelsize + 0.5;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Font size in pixels if we use the size property. Since the size
|
||||||
|
* specifies the font size in points, this is converted to pixels
|
||||||
|
* according to the dpi given.
|
||||||
|
* One point is 1/72 inches, thus this gives us the number of 'dots'
|
||||||
|
* (or pixels) for this font
|
||||||
|
*/
|
||||||
|
int px_size = size / 72.0 * dpi + 0.5;
|
||||||
|
|
||||||
|
if (fc_size == 0 && fc_pixelsize == 0) {
|
||||||
|
return scalable? px_size : px_pixelsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scalable) {
|
||||||
|
/*
|
||||||
|
* Use the point size if it's not 0. The pixelsize is only used if the
|
||||||
|
* size property is 0 and pixelsize is not
|
||||||
|
*/
|
||||||
|
if (fc_size != 0) {
|
||||||
|
return px_size;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return px_pixelsize;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/*
|
||||||
|
* Non-scalable fonts do it the other way around, here the size
|
||||||
|
* property is only used if pixelsize is 0 and size is not
|
||||||
|
*/
|
||||||
|
if (fc_pixelsize != 0) {
|
||||||
|
return px_pixelsize;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return px_size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void use() override {
|
void use() override {
|
||||||
|
|
Loading…
Reference in a new issue