1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 /** 18 * @author Ilya S. Okomin 19 * @version $Revision$ 20 */ 21 package org.apache.harmony.awt.gl.font; 22 23 import java.awt.Font; 24 import java.awt.Toolkit; 25 import java.awt.font.FontRenderContext; 26 import java.awt.font.LineMetrics; 27 import java.awt.geom.AffineTransform; 28 import java.awt.geom.Rectangle2D; 29 import java.io.File; 30 import java.util.Hashtable; 31 import java.util.Locale; 32 33 import org.apache.harmony.awt.gl.font.FontManager; 34 import org.apache.harmony.awt.gl.font.FontPeerImpl; 35 import org.apache.harmony.awt.gl.font.Glyph; 36 import org.apache.harmony.awt.gl.font.LineMetricsImpl; 37 import org.apache.harmony.awt.internal.nls.Messages; 38 39 /** 40 * Linux platform font peer implementation based on Xft and FreeType libraries. 41 */ 42 public class AndroidFont extends FontPeerImpl { 43 44 // Pairs of [begin, end],[..].. unicode ranges values 45 private int[] fontUnicodeRanges; 46 47 // table with loaded cached Glyphs 48 private Hashtable glyphs = new Hashtable(); 49 50 // X11 display value 51 private long display = 0; 52 53 // X11 screen value 54 private int screen = 0; 55 56 public AndroidFont(String fontName, int fontStyle, int fontSize) { 57 /* 58 * Workaround : to initialize awt platform-dependent fields and libraries. 59 */ 60 Toolkit.getDefaultToolkit(); 61 this.name = fontName; 62 this.size = fontSize; 63 this.style = fontStyle; 64 65 initAndroidFont(); 66 } 67 68 /** 69 * Initializes some native dependent font information, e.g. number of glyphs, 70 * font metrics, italic angle etc. 71 */ 72 public void initAndroidFont(){ 73 this.nlm = new AndroidLineMetrics(this, null, " "); //$NON-NLS-1$ 74 this.ascent = nlm.getLogicalAscent(); 75 this.descent = nlm.getLogicalDescent(); 76 this.height = nlm.getHeight(); 77 this.leading = nlm.getLogicalLeading(); 78 this.maxAdvance = nlm.getLogicalMaxCharWidth(); 79 80 if (this.fontType == FontManager.FONT_TYPE_T1){ 81 this.defaultChar = 1; 82 } else { 83 this.defaultChar = 0; 84 } 85 86 this.maxCharBounds = new Rectangle2D.Float(0, -nlm.getAscent(), nlm.getMaxCharWidth(), this.height); 87 } 88 89 90 public boolean canDisplay(char chr) { 91 // TODO: to improve performance there is a sence to implement get 92 // unicode ranges to check if char can be displayed without 93 // native calls in isGlyphExists() method 94 95 return isGlyphExists(chr); 96 } 97 98 public LineMetrics getLineMetrics(String str, FontRenderContext frc, AffineTransform at) { 99 100 // Initialize baseline offsets 101 nlm.getBaselineOffsets(); 102 103 LineMetricsImpl lm = (LineMetricsImpl)(this.nlm.clone()); 104 lm.setNumChars(str.length()); 105 106 if ((at != null) && (!at.isIdentity())){ 107 lm.scale((float)at.getScaleX(), (float)at.getScaleY()); 108 } 109 110 return lm; 111 } 112 113 public String getPSName() { 114 return psName; 115 } 116 117 public String getFamily(Locale l) { 118 // TODO: implement localized family 119 if (fontType == FontManager.FONT_TYPE_TT){ 120 return this.getFamily(); 121 } 122 123 return this.fontFamilyName; 124 } 125 126 public String getFontName(Locale l) { 127 if ((pFont == 0) || (this.fontType == FontManager.FONT_TYPE_T1)){ 128 return this.name; 129 } 130 131 return this.getFontName(); 132 } 133 134 135 public int getMissingGlyphCode() { 136 return getDefaultGlyph().getGlyphCode(); 137 } 138 139 public Glyph getGlyph(char index) { 140 Glyph result = null; 141 142 Object key = new Integer(index); 143 if (glyphs.containsKey(key)) { 144 result = (Glyph) glyphs.get(key); 145 } else { 146 if (this.addGlyph(index)) { 147 result = (Glyph) glyphs.get(key); 148 } else { 149 result = this.getDefaultGlyph(); 150 } 151 } 152 153 return result; 154 } 155 156 public Glyph getDefaultGlyph() { 157 throw new RuntimeException("DefaultGlyphs not implemented!"); 158 } 159 160 /** 161 * Disposes native font handle. If this font peer was created from InputStream 162 * temporary created font resource file is deleted. 163 */ 164 public void dispose(){ 165 String tempDirName; 166 if (pFont != 0){ 167 pFont = 0; 168 169 if (isCreatedFromStream()) { 170 File fontFile = new File(getTempFontFileName()); 171 tempDirName = fontFile.getParent(); 172 fontFile.delete(); 173 } 174 } 175 } 176 177 /** 178 * Add glyph to cached Glyph objects in this LinuxFont object. 179 * 180 * @param uChar the specified character 181 * @return true if glyph of the specified character exists in this 182 * LinuxFont or this character is escape sequence character. 183 */ 184 public boolean addGlyph(char uChar) { 185 throw new RuntimeException("Not implemented!"); 186 } 187 188 /** 189 * Adds range of existing glyphs to this LinuxFont object 190 * 191 * @param uFirst the lowest range's bound, inclusive 192 * @param uLast the highest range's bound, exclusive 193 */ 194 public void addGlyphs(char uFirst, char uLast) { 195 196 char index = uFirst; 197 if (uLast < uFirst) { 198 // awt.09=min range bound value is grater than max range bound 199 throw new IllegalArgumentException(Messages.getString("awt.09")); //$NON-NLS-1$ 200 } 201 while (index < uLast) { 202 addGlyph(index); 203 index++; 204 } 205 206 } 207 208 /** 209 * Returns true if specified character has corresopnding glyph, false otherwise. 210 * 211 * @param uIndex specified char 212 */ 213 public boolean isGlyphExists(char uIndex) { 214 throw new RuntimeException("DefaultGlyphs not implemented!"); 215 } 216 217 /** 218 * Returns an array of unicode ranges that are supported by this LinuxFont. 219 */ 220 public int[] getUnicodeRanges() { 221 int[] ranges = new int[fontUnicodeRanges.length]; 222 System.arraycopy(fontUnicodeRanges, 0, ranges, 0, 223 fontUnicodeRanges.length); 224 225 return ranges; 226 } 227 228 /** 229 * Return Font object if it was successfully embedded in System 230 */ 231 public static Font embedFont(String absolutePath){ 232 throw new RuntimeException("embedFont not implemented!"); 233 } 234 235 public String getFontName(){ 236 if ((pFont != 0) && (faceName == null)){ 237 if (this.fontType == FontManager.FONT_TYPE_T1){ 238 faceName = getFamily(); 239 } 240 } 241 return faceName; 242 } 243 244 public String getFamily() { 245 return fontFamilyName; 246 } 247 248 /** 249 * Returns initiated FontExtraMetrics instance of this WindowsFont. 250 */ 251 public FontExtraMetrics getExtraMetrics(){ 252 throw new RuntimeException("Not implemented!"); 253 } 254 } 255