1 /******************************************************************************* 2 * Copyright (c) 2008, 2011 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 15 package org.eclipse.ui.internal.intro.impl; 16 17 import org.eclipse.core.runtime.IProduct; 18 import org.eclipse.core.runtime.Platform; 19 import org.eclipse.core.runtime.preferences.DefaultScope; 20 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 21 import org.eclipse.core.runtime.preferences.InstanceScope; 22 import org.eclipse.jface.resource.JFaceResources; 23 import org.eclipse.swt.graphics.Font; 24 import org.eclipse.swt.graphics.FontData; 25 import org.osgi.service.prefs.BackingStoreException; 26 27 public class FontSelection { 28 29 public static final String VAR_FONT_STYLE = "fontStyle"; //$NON-NLS-1$ 30 public static final String FONT_ABSOLUTE = "absolute"; //$NON-NLS-1$ 31 public static final String FONT_RELATIVE = "relative"; //$NON-NLS-1$ 32 private static final String SCALE_FACTOR = "scaleFactor"; //$NON-NLS-1$ 33 public static final String ATT_SCALABLE = "scalable"; //$NON-NLS-1$ 34 35 private static final int MIN_HEIGHT = 10; 36 private static final int MAX_HEIGHT = 16; 37 38 /* 39 * Returns the height in points of the default SWT font 40 */ getDefaultFontHeight()41 private static int getDefaultFontHeight() { 42 Font defaultFont = JFaceResources.getDefaultFont(); 43 FontData[] fontData = defaultFont.getFontData(); 44 int height = MIN_HEIGHT; 45 for (int i=0; i< fontData.length; i++) { 46 FontData data = fontData[i]; 47 height = Math.max(height, data.getHeight()); 48 } 49 return Math.min(height, MAX_HEIGHT); 50 } 51 generatePageFontStyle()52 public static String generatePageFontStyle() { 53 int defaultFontHeight = getDefaultFontHeight(); 54 int scale = getScalePercentage(); 55 String result = getFontSizeDeclaration("", defaultFontHeight, 100, scale); //$NON-NLS-1$ 56 result += getFontSizeDeclaration("h1", defaultFontHeight, 200, scale); //$NON-NLS-1$ 57 result += getFontSizeDeclaration("h2", defaultFontHeight, 150, scale); //$NON-NLS-1$ 58 result += getFontSizeDeclaration("h3", defaultFontHeight, 120, scale); //$NON-NLS-1$ 59 result += getFontSizeDeclaration("h4", defaultFontHeight, 100, scale); //$NON-NLS-1$ 60 result += getFontSizeDeclaration("h5", defaultFontHeight, 80, scale); //$NON-NLS-1$ 61 result += getFontSizeDeclaration("h6", defaultFontHeight, 70, scale); //$NON-NLS-1$ 62 return result; 63 } 64 getScalePercentage()65 public static final int getScalePercentage() { 66 int scale = Platform.getPreferencesService().getInt(IntroPlugin.PLUGIN_ID, (SCALE_FACTOR), 0, null); 67 return scale; 68 } 69 getFontSizeDeclaration(String element, int baseSize, int percentage, int scale)70 private static String getFontSizeDeclaration(String element, int baseSize, int percentage, int scale) { 71 if (scale > 75) scale = 75; 72 int newSize = (int) ((baseSize * percentage *1.25) / (100 - scale)); 73 return " body " + element + "{ font-size : " + newSize + "px; } "; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ 74 } 75 setScalePercentage(int i)76 public static void setScalePercentage(int i) { 77 IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(IntroPlugin.PLUGIN_ID); 78 prefs.putInt(SCALE_FACTOR, i); 79 try { 80 prefs.flush(); 81 } catch (BackingStoreException e) { 82 } 83 } 84 85 // Set the scale factor to it's default resetScalePercentage()86 public static void resetScalePercentage() { 87 IEclipsePreferences iprefs = InstanceScope.INSTANCE.getNode(IntroPlugin.PLUGIN_ID); 88 IEclipsePreferences dprefs = DefaultScope.INSTANCE.getNode(IntroPlugin.PLUGIN_ID); 89 String defaultScale = dprefs.get(SCALE_FACTOR, "0"); //$NON-NLS-1$ 90 iprefs.put(SCALE_FACTOR, defaultScale); 91 } 92 getFontStyle()93 public static String getFontStyle() { 94 IProduct product = Platform.getProduct(); 95 if (product != null) { 96 String pid = product.getId(); 97 String style = Platform.getPreferencesService().getString 98 (IntroPlugin.PLUGIN_ID, pid + "_" +FontSelection.VAR_FONT_STYLE, "", null); //$NON-NLS-1$ //$NON-NLS-2$ 99 if (style.length() > 0) { 100 return style; 101 } 102 style = Platform.getPreferencesService().getString 103 (IntroPlugin.PLUGIN_ID, (FontSelection.VAR_FONT_STYLE), "", null); //$NON-NLS-1$ 104 if (style.length() > 0) { 105 return style; 106 } 107 } 108 // Use default for font style if not specified 109 return FontSelection.FONT_RELATIVE; 110 } 111 } 112