1 /* 2 * Copyright 2011 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.typography.font.tools.fontinfo; 18 19 import com.google.typography.font.sfntly.Font; 20 import com.google.typography.font.sfntly.FontFactory; 21 import com.google.typography.font.sfntly.Tag; 22 import com.google.typography.font.sfntly.table.Table; 23 import com.google.typography.font.sfntly.table.core.CMap; 24 import com.google.typography.font.sfntly.table.core.CMapTable; 25 import com.google.typography.font.sfntly.table.truetype.GlyphTable; 26 import com.google.typography.font.sfntly.table.truetype.LocaTable; 27 28 import com.ibm.icu.lang.UCharacter; 29 30 import java.io.FileInputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 34 /** 35 * Font Utility functions 36 * @author Brian Stell, Han-Wen Yeh 37 */ 38 public class FontUtils { 39 /** 40 * Gets a Font object for a font file in the given path 41 * 42 * @param fontFile 43 * the path to the font file 44 * @return the Font object representing the font 45 * @throws IOException 46 * if font file does not exist or is invalid 47 */ getFonts(String fontFile)48 public static Font[] getFonts(String fontFile) throws IOException { 49 return getFonts(new FileInputStream(fontFile)); 50 } 51 52 /** 53 * Gets a Font object for a font file in the InputStream 54 * 55 * @param is 56 * an InputStream containing the font file 57 * @return the Font object representing the font 58 * @throws IOException 59 * if font file or is invalid 60 */ getFonts(InputStream is)61 public static Font[] getFonts(InputStream is) throws IOException { 62 FontFactory fontFactory = FontFactory.getInstance(); 63 fontFactory.fingerprintFont(true); 64 Font[] fonts = null; 65 66 try { 67 fonts = fontFactory.loadFonts(is); 68 } finally { 69 is.close(); 70 } 71 72 return fonts; 73 } 74 75 /** 76 * Gets the table with the specified tag for the given font 77 * 78 * @param font 79 * the source font 80 * @param tag 81 * the tag for the table to return 82 * @return the specified table for the given font 83 * @throws UnsupportedOperationException 84 * if the font does not contain the table with the specified tag 85 * @see Tag 86 */ getTable(Font font, int tag)87 public static Table getTable(Font font, int tag) { 88 Table table = font.getTable(tag); 89 if (table == null) { 90 throw new RuntimeException("Font has no " + Tag.stringValue(tag) + " table"); 91 } 92 return table; 93 } 94 95 /** 96 * Gets the cmap table for the given font 97 * 98 * @param font 99 * the source font 100 * @return the cmap table for the given font 101 * @throws UnsupportedOperationException 102 * if the font does not contain a valid cmap table 103 */ getCMapTable(Font font)104 public static CMapTable getCMapTable(Font font) { 105 return (CMapTable) getTable(font, Tag.cmap); 106 } 107 108 /** 109 * Gets either a UCS4 or UCS2 cmap, if available 110 * 111 * @param font 112 * the source font 113 * @return the UCS4 or UCS2 cmap 114 * @throws UnsupportedOperationException 115 * if font does not contain a UCS-4 or UCS-2 cmap 116 */ getUCSCMap(Font font)117 public static CMap getUCSCMap(Font font) { 118 CMapTable cmapTable = getCMapTable(font); 119 120 // Obtain the UCS-4 cmap. If it doesn't exist, then obtain the UCS-2 cmap 121 CMap cmap = null; 122 cmap = cmapTable.cmap( 123 Font.PlatformId.Windows.value(), Font.WindowsEncodingId.UnicodeUCS4.value()); 124 if (cmap == null) { 125 cmap = cmapTable.cmap( 126 Font.PlatformId.Windows.value(), Font.WindowsEncodingId.UnicodeUCS2.value()); 127 } 128 if (cmap == null) { 129 throw new UnsupportedOperationException("Font has no UCS-4 or UCS-2 cmap"); 130 } 131 132 return cmap; 133 } 134 135 /** 136 * Gets the loca table for the given font 137 * 138 * @param font 139 * the source font 140 * @return the loca table for the given font 141 * @throws UnsupportedOperationException 142 * if the font does not contain a valid loca table 143 */ getLocaTable(Font font)144 public static LocaTable getLocaTable(Font font) { 145 return (LocaTable) getTable(font, Tag.loca); 146 } 147 148 /** 149 * Gets the glyph table for the given font 150 * 151 * @param font 152 * the source font 153 * @return the glyph table for the given font 154 * @throws UnsupportedOperationException 155 * if the font does not contain a valid glyph table 156 */ getGlyphTable(Font font)157 public static GlyphTable getGlyphTable(Font font) { 158 return (GlyphTable) getTable(font, Tag.glyf); 159 } 160 161 /** 162 * Gets a string version of the code point formatted as "U+hhhh" or "U+hhhhhh" 163 * 164 * @param codePoint 165 * the code point to format 166 * @return a formatted version of the code point as a string 167 */ getFormattedCodePointString(int codePoint)168 public static String getFormattedCodePointString(int codePoint) { 169 if (UCharacter.isValidCodePoint(codePoint)) { 170 if (UCharacter.isBMP(codePoint)) { 171 return String.format("U+%04X", codePoint); 172 } 173 return String.format("U+%06X", codePoint); 174 } 175 throw new IllegalArgumentException("Invalid code point " + codePoint); 176 } 177 }