001package armyc2.c2sd.renderer.utilities;
002import armyc2.c2sd.renderer.R;
003
004
005import java.io.BufferedOutputStream;
006import java.io.File;
007import java.io.FileOutputStream;
008import java.io.IOException;
009import java.io.InputStream;
010import java.util.UUID;
011
012import android.content.Context;
013import android.graphics.Paint;
014import android.graphics.Rect;
015import android.graphics.Typeface;
016import android.graphics.Paint.Align;
017import android.util.Log;
018
019
020
021public class FontManager {
022        
023        public static String FONT_UNIT = "UF";
024        public static String FONT_SPTG = "SF";
025        public static String FONT_MPTG = "MF";
026        
027        private static FontManager _instance = null;
028        private static Boolean _initSuccess = false;
029        
030        private Typeface _tfUnits = null;
031        private Typeface _tfSP = null;
032        private Typeface _tfTG = null;
033        private String _cacheDir = "";
034        
035        private FontManager()
036        {
037                
038        }
039        
040        public static synchronized FontManager getInstance()
041    {
042      if(_instance == null)
043          _instance = new FontManager();
044
045      return _instance;
046    }
047        
048        public synchronized void init(Context context, String cacheDir)
049        {
050                if(!_initSuccess)
051                {
052                        _cacheDir = cacheDir;
053                        _tfUnits = loadFont(context, R.raw.unitfont);
054                        _tfSP = loadFont(context, R.raw.singlepointfont);
055                        _tfTG = loadFont(context, R.raw.tacticalgraphicsfont);
056                        if( _tfUnits != null && _tfSP != null && _tfTG != null)
057                                _initSuccess = true;
058                        else
059                                throw new Error("FontManager:  failed to load font files using " + cacheDir + " as the cache directory.");
060                }
061        }
062        
063        public Typeface getTypeface(String fontName)
064        {
065                if(_initSuccess)
066                {
067                        if(fontName == FONT_UNIT)
068                        {
069                                return _tfUnits;
070                        }
071                        else if(fontName == FONT_SPTG)
072                        {
073                                return _tfSP;
074                        }
075                        else if(fontName == FONT_MPTG)
076                        {
077                                return _tfTG;
078                        }
079                        else
080                                return null;
081                }
082                throw new Error("FontManager:  Must call \".init(String cacheDir)\" before using");
083        }
084        
085        private Typeface loadFont(Context context, int fontName)
086        {
087                Typeface tf = null;
088                InputStream is = null;
089                try
090                {
091                        
092                        //InputStream fontStream = this.getClass().getClassLoader().getResourceAsStream("assets/fonts/unitfonts.ttf");
093                        is = context.getResources().openRawResource(fontName);
094                        
095                        if(is != null)
096                        {
097                                //Log.wtf("SPR.getFont", "we have input stream");
098                        }
099                        else
100                        {
101                                //Log.wtf(TAG, "Fail to load font file at: " + fontFolder + fontName);
102                                return null;
103                        }
104                                        
105                        /////////////////////
106                        
107                        //String sdState = Environment.getExternalStorageState();
108                        //String cacheDir = Environment.getDownloadCacheDirectory().getAbsolutePath();
109                        String cacheDir = _cacheDir;
110                        //Log.wtf("SPR.getFont", "Cache Directory: " + cacheDir);
111
112
113                        String path = cacheDir + "/secrenderer/" + UUID.randomUUID().toString();
114                        //Log.wtf("SPR.getFont", "Cache Directory: " + path);
115                        File f = new File(path);
116                        
117                        if(f.exists()==false)
118                        {
119                                //make directory
120                                if(f.mkdirs()==false)
121                                {
122                                        Log.wtf("SPR.getFont", "make temp SD dir \"" + path + "\" fail");
123                                }
124                                else
125                                {
126                                        //Log.wtf("SPR.getFont", "make temp SD dir success");
127                                }
128                        }
129                        else
130                        {
131                                //Log.wtf("SPR.getFont", "temp SD dir exists");
132                        }
133                        
134                        String outPath = path + "/secraw.dat";
135                        try
136                        {
137                                byte[] buffer = new byte[is.available()];
138                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath));
139                                
140                                int l = 0;
141                                while((l=is.read(buffer))>0)
142                                {
143                                        bos.write(buffer,0,l);
144                                }
145                                bos.close();
146                                //Log.wtf("SPR.getFont", "saved temp file");
147                                bos = null;
148                                tf = Typeface.createFromFile(outPath);
149                                if(tf!=null)
150                                {
151                                        //Log.wtf("SPR.getFont", "created TF from temp file");
152                                }
153                                File f2 = new File(outPath);
154                                if(f2.delete())
155                                {
156                                        //Log.wtf("SPR.getFont", "deleted temp file");
157                                }
158                                File f3 = new File(path);
159                                if(f3.delete())
160                                {
161                                        //Log.wtf("SPR.getFont", "deleted temp folder");
162                                }
163                        }
164                        catch(IOException ioe)
165                        {
166                                return null;
167                        }
168                        catch(Exception exc)
169                        {
170                                return null;
171                        }
172                
173                        /*
174                        //tf = Typeface.createFromFile("assets/fonts/unitfonts.ttf");
175                        tf = Typeface.createFromFile("res/raw/unitfont.ttf");
176                        //AssetManager am = _context.getAssets();
177                        //Typeface tf = Typeface.createFromAsset(am, "fonts/unitfonts.ttf");
178                        */
179                        
180                        if(tf != null)
181                        {
182                                //Log.wtf("SPR.getFont", "we have typeface");
183                        }
184                        else
185                                Log.wtf("SPR.getFont", "NO TYPEFACE");
186                        //*/
187                        
188                        return tf;
189                }
190                catch(Exception exc)
191                {
192                        Log.wtf("SPR.getFont", exc.getMessage(),exc);
193                        if(exc != null)
194                                ErrorLogger.LogException("SPR", "getFont", exc);
195                }
196                finally
197                {
198                        silentClose(is);
199                }
200                return tf;
201        }//*/
202
203        private void silentClose(InputStream is){
204                if(is == null)
205                {
206                        return;
207                }
208                try
209                {
210                        is.close();
211                }
212                catch (IOException ignore)
213                {
214                        // Ignore
215                }
216        }
217
218        public void testFontFiles()
219        {
220                Paint fillPaint = new Paint();
221        fillPaint.setStyle(Paint.Style.FILL);
222        fillPaint.setColor(Color.black.toARGB());
223        fillPaint.setTextSize(48);
224        fillPaint.setAntiAlias(true);
225        fillPaint.setTextAlign(Align.CENTER);
226        fillPaint.setTypeface(_tfUnits);
227        
228        //for(int i = 999; i < 9132; i++)
229        for(int i = 57999; i < 63132; i++)
230        {
231                Rect bounds = new Rect();
232                String strFill = String.valueOf((char) i);
233            fillPaint.getTextBounds(strFill, 0, 1, bounds);
234            
235            if(bounds.left < 0)
236            {
237                //System.err.println("Char: " + String.valueOf(i) + " has tiny width: " + String.valueOf(bounds.toString()) + " W: " + String.valueOf(bounds.width()) + " H: " + String.valueOf(bounds.width()) + ", valid");
238            }
239            else
240            {
241                System.err.println("Char: " + String.valueOf(i) + " has normal width: " + String.valueOf(bounds.toString()) + " W: " + String.valueOf(bounds.width()) + " H: " + String.valueOf(bounds.width()) + ", invalid");
242            }
243            
244            /*switch(i)
245            {
246                case 2166:
247                        i=3000;
248                        break;
249                case 3084:
250                        i=3999;
251                        break;
252                case 4085:
253                        i=4500;
254                        break;
255                case 4533:
256                        i=5000;
257                        break;
258                case 5261:
259                        i=9000;
260                        break;
261                case 9131:
262                        i=9500;
263                        break;
264                default:
265                        break;
266            }//*/
267            
268            switch(i)
269            {
270                case 59166:
271                        i=60000;
272                        break;
273                case 60084:
274                        i=60999;
275                        break;
276                case 61085:
277                        i=61500;
278                        break;
279                case 61533:
280                        i=62000;
281                        break;
282                case 62261:
283                        i=63000;
284                        break;
285                case 63131:
286                        i=63500;
287                        break;
288                default:
289                        break;
290            }
291        }
292        
293        
294        
295        }
296
297}