|
| 1 | +/* |
| 2 | +
|
| 3 | + Copyright (c) 2005-2019, Carlos Amengual. |
| 4 | +
|
| 5 | + SPDX-License-Identifier: BSD-3-Clause |
| 6 | +
|
| 7 | + Licensed under a BSD-style License. You can find the license here: |
| 8 | + https://carte.sourceforge.io/LICENSE.txt |
| 9 | +
|
| 10 | + */ |
| 11 | + |
| 12 | +package io.sf.carte.doc.style.css.awt; |
| 13 | + |
| 14 | +import java.awt.GraphicsConfiguration; |
| 15 | +import java.awt.GraphicsEnvironment; |
| 16 | + |
| 17 | +import org.w3c.dom.DOMException; |
| 18 | +import org.w3c.dom.css.CSSPrimitiveValue; |
| 19 | + |
| 20 | +import io.sf.carte.doc.style.css.AbstractStyleDatabase; |
| 21 | + |
| 22 | +/** |
| 23 | + * CSS style database for use with AWT objects. |
| 24 | + * <p> |
| 25 | + * |
| 26 | + * @author Carlos Amengual |
| 27 | + * |
| 28 | + */ |
| 29 | +public class AWTStyleDatabase extends AbstractStyleDatabase { |
| 30 | + |
| 31 | + private GraphicsConfiguration gConfiguration = null; |
| 32 | + |
| 33 | + /* |
| 34 | + * A4 defaults |
| 35 | + */ |
| 36 | + private float defaultWidth = 595f; |
| 37 | + private float defaultHeight = 842f; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructs a default style database with no graphics configuration. |
| 41 | + */ |
| 42 | + public AWTStyleDatabase() { |
| 43 | + this(null); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructs a style database for the given graphics configuration. |
| 48 | + * |
| 49 | + * @param gConf |
| 50 | + * the graphics configuration. |
| 51 | + */ |
| 52 | + public AWTStyleDatabase(GraphicsConfiguration gConf) { |
| 53 | + super(); |
| 54 | + gConfiguration = gConf; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets the name of the default font used when a generic font family (serif, |
| 59 | + * sans-serif, monospace, cursive, fantasy) is specified. |
| 60 | + * <p> |
| 61 | + * This class attempts to map the generic name to a "logical font" name. |
| 62 | + * </p> |
| 63 | + * <p> |
| 64 | + * As, in Java, logical font names are internally mapped to physical fonts |
| 65 | + * by the Java runtime environment, the name of the corresponding "logical |
| 66 | + * font" is returned, and no further mapping is attempted. |
| 67 | + * </p> |
| 68 | + * |
| 69 | + * @param genericFamily |
| 70 | + * the name of the logical font. |
| 71 | + * @return the name of the associated logical font, or null if none. |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public String getDefaultGenericFontFamily(String genericFamily) { |
| 75 | + String fontName = null; |
| 76 | + if (genericFamily.equals("serif")) { |
| 77 | + fontName = "Serif"; |
| 78 | + } else if (genericFamily.equals("sans serif")) { |
| 79 | + fontName = "SansSerif"; |
| 80 | + } else if (genericFamily.equals("monospace")) { |
| 81 | + fontName = "Monospaced"; |
| 82 | + } |
| 83 | + return fontName; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected boolean isFontFamilyAvailable(String fontFamily) { |
| 88 | + String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); |
| 89 | + for (int i = 0; i < fontNames.length; i++) { |
| 90 | + if (fontNames[i].equalsIgnoreCase(fontFamily)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Gets the GraphicsConfiguration for this style database. |
| 99 | + * |
| 100 | + * @return the GraphicsConfiguration object previously set, the default one |
| 101 | + * if none was set, or null if the graphics environment is headless. |
| 102 | + */ |
| 103 | + protected GraphicsConfiguration getGraphicsConfiguration() { |
| 104 | + return gConfiguration; |
| 105 | + } |
| 106 | + |
| 107 | + public void setGraphicsConfiguration(GraphicsConfiguration configuration) { |
| 108 | + gConfiguration = configuration; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * This method is used to normalize sizes to a 595pt width. |
| 113 | + * |
| 114 | + * @return the factor by which screensize-dependent quantities can be |
| 115 | + * multiplied to be normalized. |
| 116 | + */ |
| 117 | + protected float deviceResolutionFactor() { |
| 118 | + return getDeviceWidth() / 595f; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int getFontSizeFromIdentifier(String familyName, String fontSizeIdentifier) throws DOMException { |
| 123 | + // Normalize to device resolution |
| 124 | + float factor = Math.max(0.9f, deviceResolutionFactor()); |
| 125 | + float sz; |
| 126 | + if (fontSizeIdentifier.equals("xx-small")) { |
| 127 | + sz = 8f * factor; |
| 128 | + } else if (fontSizeIdentifier.equals("x-small")) { |
| 129 | + sz = 9f * factor; |
| 130 | + } else if (fontSizeIdentifier.equals("small")) { |
| 131 | + sz = 10f * factor; |
| 132 | + } else if (fontSizeIdentifier.equals("medium")) { |
| 133 | + sz = 12f * factor; |
| 134 | + } else if (fontSizeIdentifier.equals("large")) { |
| 135 | + sz = 14f * factor; |
| 136 | + } else if (fontSizeIdentifier.equals("x-large")) { |
| 137 | + sz = 18f * factor; |
| 138 | + } else if (fontSizeIdentifier.equals("xx-large")) { |
| 139 | + sz = 24f * factor; |
| 140 | + } else { |
| 141 | + throw new DOMException(DOMException.INVALID_ACCESS_ERR, "Unknown size identifier: " + fontSizeIdentifier); |
| 142 | + } |
| 143 | + return Math.round(sz); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public float getWidthSize(String widthIdentifier, float fontSize) throws DOMException { |
| 148 | + // Normalize to device resolution |
| 149 | + float factor = Math.max(0.62f, deviceResolutionFactor()); |
| 150 | + if ("thin".equalsIgnoreCase(widthIdentifier)) { |
| 151 | + return 1f * factor; |
| 152 | + } else if ("thick".equalsIgnoreCase(widthIdentifier)) { |
| 153 | + return 4f * factor; |
| 154 | + } else if ("medium".equalsIgnoreCase(widthIdentifier)) { |
| 155 | + return 2f * factor; |
| 156 | + } else { |
| 157 | + throw new DOMException(DOMException.SYNTAX_ERR, "Unknown identifier " + widthIdentifier); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public short getNaturalUnit() { |
| 163 | + return CSSPrimitiveValue.CSS_PT; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public float getDeviceHeight() { |
| 168 | + float height; |
| 169 | + if (gConfiguration != null) { |
| 170 | + height = (float) getGraphicsConfiguration().getBounds().getHeight(); |
| 171 | + } else { |
| 172 | + height = defaultHeight; |
| 173 | + } |
| 174 | + return height; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public float getDeviceWidth() { |
| 179 | + float width; |
| 180 | + if (gConfiguration != null) { |
| 181 | + width = (float) getGraphicsConfiguration().getBounds().getWidth(); |
| 182 | + } else { |
| 183 | + width = defaultWidth; |
| 184 | + } |
| 185 | + return width; |
| 186 | + } |
| 187 | + |
| 188 | + public void setDefaultWidth(float defaultWidth) { |
| 189 | + this.defaultWidth = defaultWidth; |
| 190 | + } |
| 191 | + |
| 192 | + public void setDefaultHeight(float defaultHeight) { |
| 193 | + this.defaultHeight = defaultHeight; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public int getColorDepth() { |
| 198 | + return getPixelDepth(); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public int getPixelDepth() { |
| 203 | + int bpc = 255; |
| 204 | + if (gConfiguration != null) { |
| 205 | + int[] comp = gConfiguration.getColorModel().getComponentSize(); |
| 206 | + for (int i = 0; i < 3; i++) { |
| 207 | + if (bpc > comp[i]) { |
| 208 | + bpc = comp[i]; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + return bpc; |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments