001package armyc2.c5isr.renderer.utilities;
002
003import android.graphics.Paint;
004import android.graphics.Path;
005import android.graphics.Point;
006import android.graphics.PointF;
007import android.graphics.Rect;
008import android.graphics.RectF;
009
010import java.util.Base64;
011
012public class Shape2SVG {
013
014    /**
015     *
016     * @param shape like {@link Object}
017     * @param stroke like "#000000
018     * @param fill like "#0000FF" or "none"
019     * @param strokeWidth "#"
020     * @param strokeOpacity "1.0"
021     * @param fillOpacity "1.0"
022     * @param dashArray "4 1 2 3"
023     * @return
024     */
025    public static String Convert(Object shape,String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray, String lineCap)
026    {
027        if(shape instanceof SVGPath)
028            return convertSVGPath((SVGPath)shape, stroke, fill, strokeWidth, strokeOpacity, fillOpacity, dashArray, lineCap);
029        else if(shape instanceof Rect)
030            return convertRect((Rect)shape, stroke, fill, strokeWidth, strokeOpacity, fillOpacity, dashArray);
031        else if(shape instanceof RectF)
032            return convertRectF((RectF)shape, stroke, fill, strokeWidth, strokeOpacity, fillOpacity, dashArray);
033        /*else if(shape instanceof Point[])
034            return convertPoints((Rect)shape, stroke, fill, strokeWidth, strokeOpacity, fillOpacity, dashArray);
035        else if(shape instanceof PointF[])
036            return convertPoints((Rect)shape, stroke, fill, strokeWidth, strokeOpacity, fillOpacity, dashArray);//*/
037        else
038            return null;
039    }
040
041    public static String Convert(String text, int x, int y, Paint font, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray, String fontName)
042    {
043        //(String text, int x, int y, Font font, FontRenderContext frc)
044        TextInfo textInfo = new TextInfo(text, x, y, font, fontName);
045        return Convert(textInfo, stroke, fill, strokeWidth, strokeOpacity, fillOpacity, dashArray);
046    }
047
048    public static String Convert(TextInfo textInfo, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
049    {
050        String svg = null;
051        StringBuilder sb = new StringBuilder();
052        if(textInfo != null)
053        {
054            String style = null;
055            String name = textInfo.getFontName();
056            if(!name.endsWith("serif"))
057                name += ", sans-serif";
058            String size = String.valueOf(textInfo.getFontSize());
059            String weight = null;
060            String anchor = null;//"start";
061            String text = textInfo.getText();
062
063            text = text.replaceAll("&","&");
064            text = text.replaceAll("<","&lt;");
065            text = text.replaceAll(">","&gt;");
066
067            Point location = new Point(textInfo.getLocation().x,textInfo.getLocation().y);
068
069            if(textInfo.getLocation().x < 0)
070            {
071                if(textInfo.getLocation().x + textInfo.getTextBounds().width() > 0)
072                {
073                    anchor = "middle";
074                    location.set(textInfo.getTextBounds().centerX(), location.y);
075                }
076                else
077                {
078                    anchor = "end";
079                    location.set(textInfo.getTextBounds().right, location.y);
080                }
081            }
082
083            if(RendererSettings.getInstance().getModiferFont().getTypeface().isBold())
084                weight = "bold";
085
086            sb.append("<text x=\"").append(location.x).append("\" y=\"").append(location.y).append("\"");
087
088            if(anchor != null)
089                sb.append(" text-anchor=\"").append(anchor).append("\"");
090            sb.append(" font-family=\"").append(name).append('"');
091            sb.append(" font-size=\"").append(size).append("px\"");
092            if(weight != null)
093                sb.append(" font-weight=\"").append(weight).append("\"");
094            sb.append(" alignment-baseline=\"alphabetic\"");//
095            sb.append(" stroke-miterlimit=\"3\"");
096
097            //sb.append(" text-anchor=\"" + anchor + "\"");//always start for single points and default SVG behavior
098
099            /*if(this._angle)
100            {
101                se += ' transform="rotate(' + this._angle + ' ' + this._anchor.getX() + ' ' + this._anchor.getY() + ')"';
102            }*/
103
104            String seStroke = "",
105                    seFill = "";
106
107
108
109            if(stroke != null)
110            {
111                seStroke = sb.toString();
112
113                seStroke += " stroke=\"" + stroke + "\"";
114                /*else
115                    seStroke = se + ' stroke="' + stroke.replace(/#/g,"&#35;") + '"';*/
116
117                if(strokeWidth != null)
118                    seStroke += " stroke-width=\"" + strokeWidth + "\"";
119                seStroke += " fill=\"none\"";
120                seStroke += ">";
121                seStroke += text;
122                seStroke += "</text>";
123            }
124
125            if(fill != null)
126            {
127                seFill = sb.toString();
128
129                seFill += " fill=\"" + fill + "\"";
130                seFill += ">";
131                seFill += text;
132                seFill += "</text>";
133            }
134
135            sb = new StringBuilder();
136            if(stroke != null && fill != null)
137                sb.append(seStroke).append("\n").append(seFill).append("\n");
138            else if(fill != null)
139                sb.append(seFill);
140            else
141                return null;
142            return sb.toString();
143        }
144        return null;
145    }
146
147    /**
148     * Assumes common font properties will be defined in the group.
149     * @param textInfo
150     * @param stroke
151     * @param fill
152     * @param strokeWidth
153     * @param strokeOpacity
154     * @param fillOpacity
155     * @param dashArray
156     * @return
157     */
158    public static String ConvertForGroup(TextInfo textInfo, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
159    {
160        String svg = null;
161        StringBuilder sb = new StringBuilder();
162        if(textInfo != null)
163        {
164            String anchor = null;//"start";
165            String text = textInfo.getText();
166
167            text = text.replaceAll("&","&amp;");
168            text = text.replaceAll("<","&lt;");
169            text = text.replaceAll(">","&gt;");
170
171            Point location = new Point(textInfo.getLocation().x,textInfo.getLocation().y);
172
173            if(textInfo.getLocation().x < 0)
174            {
175                if(textInfo.getLocation().x + textInfo.getTextBounds().width() > 0)
176                {
177                    anchor = "middle";
178                    location.set(textInfo.getTextBounds().centerX(), location.y);
179                }
180                else
181                {
182                    anchor = "end";
183                    location.set(textInfo.getTextBounds().right, location.y);
184                }
185            }
186
187
188
189            sb.append("<text x=\"").append(location.x).append("\" y=\"").append(location.y).append("\"");
190
191            if(anchor != null)
192                sb.append(" text-anchor=\"").append(anchor).append("\"");
193
194            //sb.append(" text-anchor=\"" + anchor + "\"");//always start for single points and default SVG behavior
195
196            /*if(this._angle)
197            {
198                se += ' transform="rotate(' + this._angle + ' ' + this._anchor.getX() + ' ' + this._anchor.getY() + ')"';
199            }*/
200
201            String seStroke = "",
202                    seFill = "";
203
204            StringBuilder sbStroke = null;
205            StringBuilder sbFill = null;
206
207            if(stroke != null)
208            {
209                /*seStroke = sb.toString();
210
211                seStroke += " stroke=\"" + stroke + "\"";
212
213                if(strokeWidth != null)
214                    seStroke += " stroke-width=\"" + strokeWidth + "\"";
215                seStroke += " fill=\"none\"";
216                seStroke += ">";
217                seStroke += text;
218                seStroke += "</text>";//*/
219
220                sbStroke = new StringBuilder(sb.toString());
221                sbStroke.append(" stroke=\"").append(stroke).append("\"");
222                if(strokeWidth != null)
223                    sbStroke.append(" stroke-width=\"").append(strokeWidth).append("\"");
224                sbStroke.append(" fill=\"none\"");
225                sbStroke.append(">");
226                sbStroke.append(text);
227                sbStroke.append("</text>");
228
229            }
230
231            if(fill != null)
232            {
233                /*seFill = sb.toString();
234                seFill += " fill=\"" + fill + "\"";
235                seFill += ">";
236                seFill += text;
237                seFill += "</text>";//*/
238
239                sbFill = new StringBuilder(sb.toString());
240                sbFill.append(" fill=\"").append(fill).append("\"");
241                sbFill.append(">");
242                sbFill.append(text);
243                sbFill.append("</text>");
244            }
245
246            /*sb = new StringBuilder();
247            if(stroke != null && fill != null)
248                sb.append(seStroke).append("\n").append(seFill).append("\n");
249            else if(fill != null)
250                sb.append(seFill);
251            else
252                return null;//*/
253
254            if(sbStroke != null && sbFill != null)
255                sb.append(sbStroke).append("\n").append(sbFill).append("\n");
256            else if(fill != null)
257                sb.append(sbFill);
258            else
259                return null;
260
261            return sb.toString();
262        }
263        return null;
264    }
265
266    public static String makeBase64Safe(String svg)
267    {
268        if(svg != null)
269        {
270            //Base64 encoding
271            //return new String(Base64.getEncoder().encode(svg.getBytes()));
272            //URL-safe Base64 encoding
273            return new String(android.util.Base64.encode(svg.getBytes(),0));
274        }
275        else
276            return null;
277    }
278
279    private static String convertArc(Object arc)
280    {
281        return null;
282    }
283
284    /**
285     *
286     * @param svgPath like {@link Path}
287     * @param stroke like "#000000
288     * @param fill like "#0000FF" or "none"
289     * @param strokeWidth "#"
290     * @param strokeOpacity "1.0"
291     * @param fillOpacity "1.0"
292     * @param dashArray "4 1 2 3", will override dashArray currently specified in the SVGPath object
293     * @return
294     */
295    private static String convertSVGPath(SVGPath svgPath, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray, String lineCap)
296    {
297        if(dashArray != null)
298            svgPath.setLineDash(dashArray);
299        return svgPath.toSVGElement(stroke,Float.parseFloat(strokeWidth),fill,Float.parseFloat(strokeOpacity),Float.parseFloat(fillOpacity),lineCap);
300    }
301
302    private static String convertRect(Rect rect, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
303    {
304        StringBuilder sb = new StringBuilder();
305        if(rect != null && rect.isEmpty() != true)
306        {
307            sb.append("<rect x=\"").append(rect.left).append("\" y=\"").append(rect.top);
308            sb.append("\" width=\"").append(rect.width()).append("\" height=\"").append(rect.height()).append("\"");
309
310            if(stroke != null)
311            {
312                sb.append(" stroke=\"").append(stroke).append("\"");
313
314                if(strokeWidth != null)
315                    sb.append(" stroke-width=\"").append(strokeWidth).append("\"");
316                else
317                    sb.append(" stroke-width=\"2\"");
318            }
319
320            if(fill != null)
321                sb.append(" fill=\"").append(fill).append("\"");
322            else
323                sb.append(" fill=\"none\"");
324
325            sb.append("/>");
326
327            return sb.toString();
328        }
329        else
330            return null;
331    }
332
333    private static String convertRectF(RectF rect, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
334    {
335        StringBuilder sb = new StringBuilder();
336        if(rect != null && rect.isEmpty() != true)
337        {
338            sb.append("<rect x=\"").append(rect.left).append("\" y=\"").append(rect.top);
339            sb.append("\" width=\"").append(rect.width()).append("\" height=\"").append(rect.height()).append("\"");
340
341            if(stroke != null)
342            {
343                sb.append(" stroke=\"").append(stroke).append("\"");
344
345                if(strokeWidth != null)
346                    sb.append(" stroke-width=\"").append(strokeWidth).append("\"");
347                else
348                    sb.append(" stroke-width=\"2\"");
349            }
350
351            if(fill != null)
352                sb.append(" fill=\"").append(fill).append("\"");
353            else
354                sb.append(" fill=\"none\"");
355
356            sb.append("/>");
357
358            return sb.toString();
359        }
360        else
361            return null;
362    }
363
364    public static String ConvertCircle(PointF point, float radius, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
365    {
366        StringBuilder sb = new StringBuilder();
367        if(point != null)
368        {
369            sb.append("<ellipse cx=\"").append(point.x).append("\" cy=\"").append(point.y);
370            sb.append("\" rx=\"").append(radius).append("\" ry=\"").append(radius).append("\"");
371
372            if(stroke != null)
373            {
374                sb.append(" stroke=\"").append(stroke).append("\"");
375
376                if(strokeWidth != null)
377                    sb.append(" stroke-width=\"").append(strokeWidth).append("\"");
378                else
379                    sb.append(" stroke-width=\"2\"");
380            }
381
382            if(fill != null)
383                sb.append(" fill=\"").append(fill).append("\"");
384            else
385                sb.append(" fill=\"none\"");
386
387            sb.append("/>");
388
389            return sb.toString();
390        }
391        else
392            return null;
393    }
394}