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)
042    {
043        //(String text, int x, int y, Font font, FontRenderContext frc)
044        TextInfo textInfo = new TextInfo(text, x, y, font);
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 = RendererSettings.getInstance().getModiferFontProps()[0] + ", sans-serif";//"SansSerif";
056            String size = RendererSettings.getInstance().getModiferFontProps()[2];
057            String weight = null;
058            String anchor = null;//"start";
059            String text = textInfo.getText();
060
061            text = text.replaceAll("&","&");
062            text = text.replaceAll("<","&lt;");
063            text = text.replaceAll(">","&gt;");
064
065            Point location = new Point(textInfo.getLocation().x,textInfo.getLocation().y);
066
067            if(textInfo.getLocation().x < 0)
068            {
069                if(textInfo.getLocation().x + textInfo.getTextBounds().width() > 0)
070                {
071                    anchor = "middle";
072                    location.set(textInfo.getTextBounds().centerX(), location.y);
073                }
074                else
075                {
076                    anchor = "end";
077                    location.set(textInfo.getTextBounds().right, location.y);
078                }
079            }
080
081            if(RendererSettings.getInstance().getModiferFont().getTypeface().isBold())
082                weight = "bold";
083
084            sb.append("<text x=\"" + location.x + "\" y=\"" + location.y + "\"");
085
086            if(anchor != null)
087                sb.append(" text-anchor=\"" + anchor + "\"");
088            sb.append(" font-family=\"" + name + '"');
089            sb.append(" font-size=\"" + size + "px\"");
090            if(weight != null)
091                sb.append(" font-weight=\"" + weight + "\"");
092            sb.append(" alignment-baseline=\"alphabetic\"");//
093            sb.append(" stroke-miterlimit=\"3\"");
094
095            //sb.append(" text-anchor=\"" + anchor + "\"");//always start for single points and default SVG behavior
096
097            /*if(this._angle)
098            {
099                se += ' transform="rotate(' + this._angle + ' ' + this._anchor.getX() + ' ' + this._anchor.getY() + ')"';
100            }*/
101
102            String seStroke = "",
103                    seFill = "";
104
105
106
107            if(stroke != null)
108            {
109                seStroke = sb.toString();
110
111                seStroke += " stroke=\"" + stroke + "\"";
112                /*else
113                    seStroke = se + ' stroke="' + stroke.replace(/#/g,"&#35;") + '"';*/
114
115                if(strokeWidth != null)
116                    seStroke += " stroke-width=\"" + strokeWidth + "\"";
117                seStroke += " fill=\"none\"";
118                seStroke += ">";
119                seStroke += text;
120                seStroke += "</text>";
121            }
122
123            if(fill != null)
124            {
125                seFill = sb.toString();
126
127                seFill += " fill=\"" + fill + "\"";
128                seFill += ">";
129                seFill += text;
130                seFill += "</text>";
131            }
132
133            sb = new StringBuilder();
134            if(stroke != null && fill != null)
135                sb.append(seStroke + "\n" + seFill).append("\n");
136            else if(fill != null)
137                sb.append(seFill);
138            else
139                return null;
140            return sb.toString();
141        }
142        return null;
143    }
144
145    /**
146     * Assumes common font properties will be defined in the group.
147     * @param textInfo
148     * @param stroke
149     * @param fill
150     * @param strokeWidth
151     * @param strokeOpacity
152     * @param fillOpacity
153     * @param dashArray
154     * @return
155     */
156    public static String ConvertForGroup(TextInfo textInfo, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
157    {
158        String svg = null;
159        StringBuilder sb = new StringBuilder();
160        if(textInfo != null)
161        {
162            String anchor = null;//"start";
163            String text = textInfo.getText();
164
165            text = text.replaceAll("&","&amp;");
166            text = text.replaceAll("<","&lt;");
167            text = text.replaceAll(">","&gt;");
168
169            Point location = new Point(textInfo.getLocation().x,textInfo.getLocation().y);
170
171            if(textInfo.getLocation().x < 0)
172            {
173                if(textInfo.getLocation().x + textInfo.getTextBounds().width() > 0)
174                {
175                    anchor = "middle";
176                    location.set(textInfo.getTextBounds().centerX(), location.y);
177                }
178                else
179                {
180                    anchor = "end";
181                    location.set(textInfo.getTextBounds().right, location.y);
182                }
183            }
184
185
186
187            sb.append("<text x=\"" + location.x + "\" y=\"" + location.y + "\"");
188
189            if(anchor != null)
190                sb.append(" text-anchor=\"" + anchor + "\"");
191
192            //sb.append(" text-anchor=\"" + anchor + "\"");//always start for single points and default SVG behavior
193
194            /*if(this._angle)
195            {
196                se += ' transform="rotate(' + this._angle + ' ' + this._anchor.getX() + ' ' + this._anchor.getY() + ')"';
197            }*/
198
199            String seStroke = "",
200                    seFill = "";
201
202
203
204            if(stroke != null)
205            {
206                seStroke = sb.toString();
207
208                seStroke += " stroke=\"" + stroke + "\"";
209                /*else
210                    seStroke = se + ' stroke="' + stroke.replace(/#/g,"&#35;") + '"';*/
211
212                if(strokeWidth != null)
213                    seStroke += " stroke-width=\"" + strokeWidth + "\"";
214                seStroke += " fill=\"none\"";
215                seStroke += ">";
216                seStroke += text;
217                seStroke += "</text>";
218            }
219
220            if(fill != null)
221            {
222                seFill = sb.toString();
223
224
225                seFill += " fill=\"" + fill + "\"";
226                seFill += ">";
227                seFill += text;
228                seFill += "</text>";
229            }
230
231            sb = new StringBuilder();
232            if(stroke != null && fill != null)
233                sb.append(seStroke + "\n" + seFill).append("\n");
234            else if(fill != null)
235                sb.append(seFill);
236            else
237                return null;
238            return sb.toString();
239        }
240        return null;
241    }
242
243    public static String makeBase64Safe(String svg)
244    {
245        if(svg != null)
246        {
247            //Base64 encoding
248            //return new String(Base64.getEncoder().encode(svg.getBytes()));
249            //URL-safe Base64 encoding
250            return new String(android.util.Base64.encode(svg.getBytes(),0));
251        }
252        else
253            return null;
254    }
255
256    private static String convertArc(Object arc)
257    {
258        return null;
259    }
260
261    /**
262     *
263     * @param svgPath like {@link Path}
264     * @param stroke like "#000000
265     * @param fill like "#0000FF" or "none"
266     * @param strokeWidth "#"
267     * @param strokeOpacity "1.0"
268     * @param fillOpacity "1.0"
269     * @param dashArray "4 1 2 3", will override dashArray currently specified in the SVGPath object
270     * @return
271     */
272    private static String convertSVGPath(SVGPath svgPath, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray, String lineCap)
273    {
274        if(dashArray != null)
275            svgPath.setLineDash(dashArray);
276        return svgPath.toSVGElement(stroke,Float.parseFloat(strokeWidth),fill,Float.parseFloat(strokeOpacity),Float.parseFloat(fillOpacity),lineCap);
277    }
278
279    private static String convertRect(Rect rect, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
280    {
281        StringBuilder sb = new StringBuilder();
282        if(rect != null && rect.isEmpty() != true)
283        {
284            sb.append("<rect x=\"" + rect.left + "\" y=\"" + rect.top);
285            sb.append("\" width=\"" + rect.width() + "\" height=\"" + rect.height() + "\"");
286
287            if(stroke != null)
288            {
289                sb.append(" stroke=\"" + stroke + "\"");
290
291                if(strokeWidth != null)
292                    sb.append(" stroke-width=\"" + strokeWidth + "\"");
293                else
294                    sb.append(" stroke-width=\"2\"");
295            }
296
297            if(fill != null)
298                sb.append(" fill=\"" + fill + "\"");
299            else
300                sb.append(" fill=\"none\"");
301
302            sb.append("/>");
303
304            return sb.toString();
305        }
306        else
307            return null;
308    }
309
310    private static String convertRectF(RectF rect, String stroke, String fill, String strokeWidth, String strokeOpacity, String fillOpacity, String dashArray)
311    {
312        StringBuilder sb = new StringBuilder();
313        if(rect != null && rect.isEmpty() != true)
314        {
315            sb.append("<rect x=\"" + rect.left + "\" y=\"" + rect.top);
316            sb.append("\" width=\"" + rect.width() + "\" height=\"" + rect.height() + "\"");
317
318            if(stroke != null)
319            {
320                sb.append(" stroke=\"" + stroke + "\"");
321
322                if(strokeWidth != null)
323                    sb.append(" stroke-width=\"" + strokeWidth + "\"");
324                else
325                    sb.append(" stroke-width=\"2\"");
326            }
327
328            if(fill != null)
329                sb.append(" fill=\"" + fill + "\"");
330            else
331                sb.append(" fill=\"none\"");
332
333            sb.append("/>");
334
335            return sb.toString();
336        }
337        else
338            return null;
339    }
340}