001package armyc2.c2sd.renderer;
002
003import android.content.Context;
004import android.os.Build;
005import android.util.DisplayMetrics;
006import android.util.Log;
007import android.util.SparseArray;
008import android.view.WindowManager;
009
010import java.util.concurrent.atomic.AtomicBoolean;
011import java.util.logging.Level;
012
013import armyc2.c2sd.renderer.utilities.*;
014
015public class MilStdIconRenderer
016/* implements IIconRenderer */ {
017
018    private String TAG = "MilStdIconRenderer";
019    private String _cacheDir = null;
020
021    private static MilStdIconRenderer _instance = null;
022    private AtomicBoolean _initSucces = new AtomicBoolean(false);
023    private SinglePointRenderer _SPR = null;
024    private SinglePointSVGRenderer _SPSVGR = null;
025
026    public static synchronized MilStdIconRenderer getInstance()
027    {
028        if (_instance == null) {
029            _instance = new MilStdIconRenderer();
030        }
031        return _instance;
032    }
033
034    /**
035     *
036     * @param cacheDir
037     */
038    public synchronized void init(Context context, String cacheDir)// List<Typeface> fonts, List<String> xml
039    {
040        try {
041            if (!_initSucces.get()) {
042
043                _cacheDir = cacheDir;
044
045                // setup fonts
046                FontManager.getInstance().init(context, cacheDir);
047
048                UnitDefTable.getInstance().init(context);
049                UnitFontLookup.getInstance().init(context);
050                SymbolDefTable.getInstance().init(context);
051                SinglePointLookup.getInstance().init(context);
052                TacticalGraphicLookup.getInstance().init(context);
053
054                // PROTOTYPE SVG////////////////////////////////////////
055                // works, but half speed
056                /*
057                 * String USVG = getXML("unitfontsvg.svg"); UnitSVGTable.getInstance().init(USVG); String
058                 * SPSVG = getXML("singlepointsvg.svg"); SymbolSVGTable.getInstance().init(SPSVG);//
059                 */
060                //////////////////////////////////////////////////////
061
062                /*//works but may setup past users who were setting it manually or weren't setting DPI at all
063                DisplayMetrics dm = new DisplayMetrics();
064                WindowManager manager;
065                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
066                    manager = context.getSystemService(WindowManager.class);//.getDefaultDisplay().getRealMetrics(dm);
067                }
068                else
069                {
070                    manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
071                }
072                manager.getDefaultDisplay().getRealMetrics(dm);
073
074                RendererSettings.getInstance().setDeviceDPI(dm.densityDpi);
075                //RendererSettings.getInstance().setDeviceHeight(dm.heightPixels);
076                //RendererSettings.getInstance().setDeviceWidth(dm.widthPixels);//*/
077
078                // setup single point renderer
079                _SPR = SinglePointRenderer.getInstance();
080                // _SPSVGR = SinglePointSVGRenderer.getInstance();
081
082                _initSucces.set(true);
083            }
084
085        } catch (Exception exc) {
086            Log.e(TAG, exc.getMessage(), exc);
087        }
088    }
089
090    public synchronized boolean isReady()
091    {
092        return _initSucces.get();
093    }
094
095    // @Override
096    public Boolean CanRender(String symbolID, SparseArray<String> modifiers, SparseArray<String> attributes)
097    {
098
099        String message = null;
100        String basicSymbolID = symbolID;
101        basicSymbolID = SymbolUtilities.getBasicSymbolID(basicSymbolID);
102        // ErrorLogger.LogMessage("TEST");
103
104        int symStd = -1;
105        String temp;
106        if (attributes.indexOfKey(MilStdAttributes.SymbologyStandard) >= 0) {
107            temp = attributes.get(MilStdAttributes.SymbologyStandard);
108            if (SymbolUtilities.isNumber(temp)) {
109                symStd = Integer.parseInt(temp);
110            }
111        }
112
113        if (symStd < 0 || symStd > RendererSettings.Symbology_2525C) {
114            symStd = RendererSettings.getInstance().getSymbologyStandard();
115        }
116
117        try {
118            // message = "Cannot draw: " + symbolCode + " (" + basicSymbolID + ")";
119            SymbolDefTable sdt = SymbolDefTable.getInstance();
120            if (SymbolUtilities.isTacticalGraphic(basicSymbolID)) {
121
122                SymbolDef sd = sdt.getSymbolDef(basicSymbolID, symStd);
123                if (sd != null) {
124
125                    if (sd.getDrawCategory() == 8)// make sure we can find the character in the font.
126                    {
127                        int index = -1;
128                        index = SinglePointLookup.getInstance().getCharCodeFromSymbol(symbolID, symStd);
129                        if (index > 0) {
130                            return true;
131                        } else {
132                            message = "Bad font lookup for: " + symbolID + " (" + basicSymbolID + ")";
133                        }
134                    } else // check with icon renderer for multipoints
135                    {
136                        message = "Cannot draw: " + symbolID + " (" + basicSymbolID + ")";
137                    }
138
139                } else {
140                    message = "Cannot draw symbolID: " + symbolID + " (" + basicSymbolID + ")";
141                }
142            } else {
143                UnitDef ud = UnitDefTable.getInstance().getUnitDef(basicSymbolID, symStd);
144                // UnitFontLookupInfo ufli = UnitFontLookup.getInstance().getLookupInfo(basicSymbolID,symStd);
145                if (ud != null) {
146                    return true;
147                } else {
148                    message = "JavaRenderer.CanRender() - Cannot draw symbolID: " + symbolID + " ("
149                            + basicSymbolID + ")";
150                }
151            }
152
153            if (message != null && !message.equals("")) {
154                ErrorLogger.LogMessage(this.getClass().getName(), "CanRender()", message, Level.FINE);
155                // System.err.println(message);
156                // System.out.println("");
157                // System.out.println("INFO: CanRender - " + message);
158                // Exception foo = new Exception("Stack?");
159                // foo.printStackTrace();
160            }
161        } catch (Exception exc) {
162            ErrorLogger.LogException("MilStdIconRenderer", "CanRender", exc);
163        }
164        return false;
165    }
166
167    private ImageInfo renderTacticalMultipointIcon(String symbolID, SparseArray<String> attributes)
168    {
169        Color lineColor = SymbolUtilities.getLineColorOfAffiliation(symbolID);
170        if (attributes.indexOfKey(MilStdAttributes.LineColor) >= 0) {
171            lineColor = new Color(attributes.get(MilStdAttributes.LineColor));
172        }
173        int size = RendererSettings.getInstance().getDefaultPixelSize();// 35;
174        if (attributes.indexOfKey(MilStdAttributes.PixelSize) >= 0) {
175            size = Integer.parseInt(attributes.get(MilStdAttributes.PixelSize));
176        }
177
178        int symStd = RendererSettings.getInstance().getSymbologyStandard();
179        if (attributes.indexOfKey(MilStdAttributes.SymbologyStandard) >= 0) {
180            symStd = Integer.parseInt(attributes.get(MilStdAttributes.SymbologyStandard));
181        }
182
183        int outlineWidth = RendererSettings.getInstance().getSinglePointSymbolOutlineWidth();
184        boolean asIcon = false;
185        Color outlineColor = null;
186        if (attributes.indexOfKey(MilStdAttributes.DrawAsIcon) >= 0) {
187            asIcon = Boolean.getBoolean(attributes.get(MilStdAttributes.SymbologyStandard));
188            if(asIcon)
189                outlineWidth = 0;
190        }
191        outlineColor = RendererUtilities.getIdealOutlineColor(lineColor);
192        ImageInfo ii = TacticalGraphicIconRenderer.getIcon(symbolID, size, lineColor, symStd, outlineWidth, outlineColor);
193        return ii;
194    }
195
196    // @Override
197    public ImageInfo RenderIcon(String symbolID, SparseArray<String> modifiers,
198            SparseArray<String> attributes)
199    {
200
201        int symStd = 1;
202
203        if (attributes != null && attributes.indexOfKey(MilStdAttributes.SymbologyStandard) >= 0) {
204            symStd = Integer.parseInt(attributes.get(MilStdAttributes.SymbologyStandard));
205        }
206
207        ImageInfo temp = null;
208        if (SymbolUtilities.isTacticalGraphic(symbolID)) {
209            String basicSymbolID = SymbolUtilities.getBasicSymbolIDStrict(symbolID);
210            SymbolDef sd = SymbolDefTable.getInstance().getSymbolDef(basicSymbolID, symStd);
211            if (sd == null) {
212                symbolID = SymbolUtilities.reconcileSymbolID(symbolID);
213                basicSymbolID = SymbolUtilities.getBasicSymbolIDStrict(symbolID);
214                sd = SymbolDefTable.getInstance().getSymbolDef(basicSymbolID, symStd);
215            }
216
217            if (sd != null) {
218                if (sd.getDrawCategory() == SymbolDef.DRAW_CATEGORY_POINT) {
219                    temp = _SPR.RenderSP(symbolID, modifiers, attributes);
220                } else {
221                    return renderTacticalMultipointIcon(symbolID, attributes);
222                }
223            } else {
224                temp = _SPR.RenderUnit(symbolID, modifiers, attributes);
225            }
226
227        } else {
228            temp = _SPR.RenderUnit(symbolID, modifiers, attributes);
229            /*
230             * if(RendererSettings.getInstance().getIconEngine() == RendererSettings.IconEngine_FONT) { temp =
231             * _SPR.RenderUnit(symbolID, modifiers, attributes); } else { temp = _SPSVGR.RenderUnit(symbolID,
232             * modifiers,attributes); }//
233             */
234
235        }
236
237        return temp;
238    }
239
240    // @Override
241    public String getRendererID()
242    {
243
244        return "milstd2525";
245    }
246
247    private SparseArray<String> getDefaultAttributes(String symbolID)
248    {
249        SparseArray<String> map = new SparseArray<String>();
250        try {
251            if (symbolID == null || symbolID.length() != 15) {
252                if (symbolID == null) {
253                    symbolID = "null";
254                }
255                ErrorLogger.LogMessage("MilStdIconRenderer", "getDefaultAttributes",
256                        "getDefaultAttributes passed bad symbolID: " + symbolID);
257                return null;
258            }
259
260            map.put(MilStdAttributes.Alpha, "1.0");
261            if (SymbolUtilities.hasDefaultFill(symbolID)) {
262                map.put(MilStdAttributes.FillColor,
263                        SymbolUtilities.getFillColorOfAffiliation(symbolID).toHexString());
264            }
265
266            map.put(MilStdAttributes.LineColor,
267                    SymbolUtilities.getLineColorOfAffiliation(symbolID).toHexString());
268
269            map.put(MilStdAttributes.OutlineSymbol, "false");
270            // attribute[MilStdAttributes.SymbolOutlineColor] = null;
271            // map.put(MilStdAttributes.OutlineWidth,"1");
272
273            map.put(MilStdAttributes.DrawAsIcon, "false");
274
275            RendererSettings rs = RendererSettings.getInstance();
276
277            if (SymbolUtilities.isWarfighting(symbolID)) {
278                if (rs != null) {
279                    map.put(MilStdAttributes.FontSize, String.valueOf(rs.getUnitFontSize()));// 50;
280                } else {
281                    map.put(MilStdAttributes.FontSize, "50");// 50;
282                }
283                map.put(MilStdAttributes.PixelSize, "-1");
284            } else {
285                if (rs != null) {
286                    map.put(MilStdAttributes.FontSize, String.valueOf(rs.getSPFontSize()));// 60;
287                } else {
288                    map.put(MilStdAttributes.FontSize, "60");
289                }
290
291                map.put(MilStdAttributes.PixelSize, "-1");
292            }
293            map.put(MilStdAttributes.KeepUnitRatio, "true");
294            return map;
295        } catch (Exception exc) {
296            ErrorLogger.LogException("MilStdIconRenderer", "getDefaultAttributes", exc);
297        }
298        return map;
299    }
300
301}