Add JBUI.getFontScale, UISettings.defFontSize, UISettings.defFontScale

This commit is contained in:
Anton Tarasov
2017-05-16 17:32:29 +03:00
parent e0b6789009
commit 087aaa2fee
9 changed files with 45 additions and 17 deletions
@@ -489,7 +489,7 @@ public class IconUtil {
*/
@NotNull
public static Icon scaleByFont(@NotNull Icon icon, @Nullable Component ancestor, float fontSize) {
float scale = fontSize / UIUtil.DEF_SYSTEM_FONT_SIZE;
float scale = JBUI.getFontScale(fontSize);
if (icon instanceof ScalableIcon) {
if (icon instanceof JBUIScaleTrackable) {
JBUIScaleTrackable jbuiIcon = (JBUIScaleTrackable)icon;
@@ -54,7 +54,7 @@ class UISettings : BaseState(), PersistentStateComponent<UISettings> {
@get:Property(filter = FontFilter::class)
@get:OptionTag("FONT_SIZE")
var fontSize by storedProperty((UIUtil.DEF_SYSTEM_FONT_SIZE * UISettings.normalizingScale).toInt())
var fontSize by storedProperty(defFontSize)
@get:Property(filter = FontFilter::class)
@get:OptionTag("FONT_SCALE")
@@ -191,7 +191,7 @@ class UISettings : BaseState(), PersistentStateComponent<UISettings> {
val fontData = systemFontFaceAndSize
if (fontFace == null) fontFace = fontData.first
if (fontSize <= 0) fontSize = fontData.second
if (fontScale <= 0) fontScale = normalizingScale
if (fontScale <= 0) fontScale = defFontScale
}
class FontFilter : SerializationFilter {
@@ -232,7 +232,7 @@ class UISettings : BaseState(), PersistentStateComponent<UISettings> {
}
fontSize = restoreFontSize(fontSize, fontScale)
fontScale = normalizingScale
fontScale = defFontScale
initDefFont()
// 1. Sometimes system font cannot display standard ASCII symbols. If so we have
@@ -366,21 +366,39 @@ class UISettings : BaseState(), PersistentStateComponent<UISettings> {
instance.editorAAType?.let { GraphicsUtil.setAntialiasingType(component, it.textInfo) }
}
/**
* Returns the default font scale, which depends on the HiDPI mode (see JBUI#ScaleType).
* <p>
* The font is represented:
* - in relative (dpi-independent) points in the JRE-managed HiDPI mode, so the method returns 1.0f
* - in absolute (dpi-dependent) points in the IDE-managed HiDPI mode, so the method returns the default screen scale
*
* @return the system font scale
*/
@JvmStatic
val normalizingScale: Float
val defFontScale: Float
get() = if (UIUtil.isJreHiDPIEnabled()) 1f else JBUI.sysScale()
/**
* Returns the default font size scaled by #defFontScale
*
* @return the default scaled font size
*/
@JvmStatic
val defFontSize: Int
get() = Math.round(UIUtil.DEF_SYSTEM_FONT_SIZE * defFontScale)
@JvmStatic
fun restoreFontSize(readSize: Int, readScale: Float?): Int {
var size = readSize
if (readScale == null || readScale <= 0) {
// Reset font to default on switch from IDE-managed HiDPI to JRE-managed HiDPI. Doesn't affect OSX.
if (UIUtil.isJreHiDPIEnabled() && !SystemInfo.isMac) size = UIUtil.DEF_SYSTEM_FONT_SIZE.toInt()
if (UIUtil.isJreHiDPIEnabled() && !SystemInfo.isMac) size = defFontSize
}
else {
if (readScale != normalizingScale) size = Math.round((readSize / readScale) * normalizingScale)
if (readScale != defFontScale) size = Math.round((readSize / readScale) * defFontScale)
}
LOG.info("Loaded: fontSize=$readSize, fontScale=$readScale; restored: fontSize=$size, fontScale=$normalizingScale")
LOG.info("Loaded: fontSize=$readSize, fontScale=$readScale; restored: fontSize=$size, fontScale=$defFontScale")
return size
}
}
@@ -56,6 +56,7 @@ import static com.intellij.ui.ColorUtil.fromHex;
public abstract class AbstractColorsScheme extends EditorFontCacheImpl implements EditorColorsScheme, SerializableScheme {
private static final int CURR_VERSION = 142;
// todo: unify with UIUtil.DEF_SYSTEM_FONT_SIZE
private static final FontSize DEFAULT_FONT_SIZE = FontSize.SMALL;
protected EditorColorsScheme myParentScheme;
@@ -500,7 +501,7 @@ public abstract class AbstractColorsScheme extends EditorFontCacheImpl implement
private int readFontSize(Element element, boolean isDefault, Float fontScale) {
if (isDefault) {
return (int)(UISettings.getNormalizingScale() * DEFAULT_FONT_SIZE.getSize());
return UISettings.getDefFontSize();
}
Integer intSize = myValueReader.read(Integer.class, element);
if (intSize == null) {
@@ -551,7 +552,7 @@ public abstract class AbstractColorsScheme extends EditorFontCacheImpl implement
* FONT_SCALE value should also be written by that reason.
*/
if (!(myFontPreferences instanceof DelegatingFontPreferences) || !(myConsoleFontPreferences instanceof DelegatingFontPreferences)) {
JdomKt.addOptionTag(parentNode, FONT_SCALE, String.valueOf(UISettings.getNormalizingScale())); // must precede font options
JdomKt.addOptionTag(parentNode, FONT_SCALE, String.valueOf(UISettings.getDefFontScale())); // must precede font options
}
if (myParentScheme != null && myParentScheme != EmptyColorScheme.INSTANCE) {
@@ -56,7 +56,7 @@ public class AppEditorFontOptions implements PersistentStateComponent<AppEditorF
public PersistentFontPreferences(FontPreferences fontPreferences) {
FONT_FAMILY = fontPreferences.getFontFamily();
FONT_SIZE = fontPreferences.getSize(FONT_FAMILY);
FONT_SCALE = UISettings.getNormalizingScale();
FONT_SCALE = UISettings.getDefFontScale();
LINE_SPACING = fontPreferences.getLineSpacing();
USE_LIGATURES = fontPreferences.useLigatures();
List<String> fontFamilies = fontPreferences.getEffectiveFontFamilies();
@@ -116,7 +116,7 @@ class TextPainter extends BasePainter {
Printing Graphics is constructed with scale corresponding to the printer DPI settings (~600dpi),
the font size is expected to be in 96 dpi, so we should normalize it.
*/
int fontSize = Math.round(myPrintSettings.FONT_SIZE / UISettings.getNormalizingScale());
int fontSize = Math.round(myPrintSettings.FONT_SIZE / UISettings.getDefFontScale());
myPlainFont = new Font(fontName, Font.PLAIN, fontSize);
myBoldFont = new Font(fontName, Font.BOLD, fontSize);
myItalicFont = new Font(fontName, Font.ITALIC, fontSize);
@@ -25,6 +25,7 @@ import com.intellij.ui.paint.RectanglePainter;
import com.intellij.util.IconUtil;
import com.intellij.util.ui.AbstractLayoutManager;
import com.intellij.util.ui.JBInsets;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.MouseEventHandler;
import org.intellij.lang.annotations.JdkConstants.FontStyle;
@@ -441,7 +442,7 @@ public class Breadcrumbs extends JComponent {
}
if (parent != null && parent.background == background && !Registry.is("editor.breadcrumbs.marker")) {
Graphics2D g2 = (Graphics2D)g.create();
float stroke = getFontSize(getFont()) / DEF_SYSTEM_FONT_SIZE;
float stroke = JBUI.getFontScale(getFontSize(getFont()));
// calculate a visible width of separator (30% of a whole path)
int delta = (int)(scale * (.3 * getRightGap() + getLeftGap()));
g2.clipRect(bounds.x - delta, bounds.y, Short.MAX_VALUE, bounds.height);
@@ -148,7 +148,7 @@ public class TogglePresentationModeAction extends AnAction implements DumbAware
}
}
}
float scaleFactor = settings.getPresentationModeFontSize() / UIUtil.DEF_SYSTEM_FONT_SIZE;
float scaleFactor = JBUI.getFontScale(settings.getPresentationModeFontSize());
ourSavedScaleFactor = JBUI.scale(1f);
JBUI.setUserScaleFactor(scaleFactor);
for (Object key : ourSavedValues.keySet()) {
@@ -707,7 +707,7 @@ public final class LafManagerImpl extends LafManager implements PersistentStateC
if (uiSettings.getOverrideLafFonts()) {
storeOriginalFontDefaults(uiDefaults);
initFontDefaults(uiDefaults, uiSettings.getFontSize(), new FontUIResource(uiSettings.getFontFace(), Font.PLAIN, uiSettings.getFontSize()));
JBUI.setUserScaleFactor(uiSettings.getFontSize() / UIUtil.DEF_SYSTEM_FONT_SIZE);
JBUI.setUserScaleFactor(JBUI.getFontScale(uiSettings.getFontSize()));
}
else {
restoreOriginalFontDefaults(uiDefaults);
@@ -722,7 +722,7 @@ public final class LafManagerImpl extends LafManager implements PersistentStateC
defaults.put(resource, lfDefaults.get(resource));
}
}
JBUI.setUserScaleFactor(JBUI.Fonts.label().getSize() / UIUtil.DEF_SYSTEM_FONT_SIZE);
JBUI.setUserScaleFactor(JBUI.getFontScale(JBUI.Fonts.label().getSize()));
}
private void storeOriginalFontDefaults(UIDefaults defaults) {
@@ -198,7 +198,7 @@ public class JBUI {
Pair<String, Integer> fdata = UIUtil.getSystemFontData();
int size = fdata == null ? Fonts.label().getSize() : fdata.getSecond();
return size / UIUtil.DEF_SYSTEM_FONT_SIZE;
return getFontScale(size);
}
/**
@@ -365,6 +365,14 @@ public class JBUI {
return (int)scale(fontSize);
}
/**
* @param fontSize
* @return the scale factor of {@code fontSize} relative to the standard font size (currently 12pt)
*/
public static float getFontScale(float fontSize) {
return fontSize / UIUtil.DEF_SYSTEM_FONT_SIZE;
}
public static JBDimension size(int width, int height) {
return new JBDimension(width, height);
}