001package armyc2.c5isr.JavaLineArray;
002
003import android.graphics.Bitmap;
004import android.graphics.Canvas;
005import android.graphics.Rect;
006import android.graphics.RectF;
007import android.util.LruCache;
008
009import armyc2.c5isr.renderer.utilities.Color;
010import armyc2.c5isr.renderer.utilities.MilStdAttributes;
011import armyc2.c5isr.renderer.utilities.RectUtilities;
012import armyc2.c5isr.renderer.utilities.RendererSettings;
013import armyc2.c5isr.renderer.utilities.RendererUtilities;
014import armyc2.c5isr.renderer.utilities.SymbolID;
015import armyc2.c5isr.renderer.utilities.SymbolUtilities;
016
017import java.util.Locale;
018import java.util.Map;
019
020public class LinePattern {
021
022    private static final LruCache<String,LinePattern> _patternCache = new LruCache<>(128);
023    Bitmap pattern = null;
024    String svg = null;
025    String dataUrl = null;
026    double vOffset = 0;
027    String _sybmolCode = null;
028    Color _fillColor = null;
029    Color _lineColor = null;
030    float _lineWidth = 0;
031
032
033    private LinePattern(Bitmap linePattern, String svgPattern, double verticalOffset)
034    {
035        pattern = linePattern;
036        svg = svgPattern;
037        vOffset = verticalOffset;
038    }
039
040    LinePattern(Bitmap linePattern, String svgPattern, double verticalOffset, String symbolCode, Color lineColor, Color fillColor, float lineWidth)
041    {
042        pattern = linePattern;
043        svg = svgPattern;
044        vOffset = verticalOffset;
045
046        _sybmolCode = symbolCode;
047        _lineColor = lineColor;
048        _fillColor = fillColor;
049        _lineWidth = lineWidth;
050    }
051
052    public Bitmap getLinePatternImage()
053    {
054        return pattern;
055    }
056
057    public String getLinePatternDataUrl()
058    {
059        if(dataUrl == null)
060            dataUrl = RendererUtilities.imgToBase64String(this.pattern);
061        return dataUrl;
062    }
063
064    public String getLinePatternSVG()
065    {
066        return svg;
067    }
068
069    public double getLinePatternVerticalOffset(){return vOffset;}
070
071    public static boolean supportsLinePattern(String symbolCode) {
072        int ec = SymbolID.getEntityCode(symbolCode);
073        if (SymbolID.getSymbolSet(symbolCode) == SymbolID.SymbolSet_ControlMeasure)
074        {
075            switch (ec)
076            {
077                // FLOT
078                case 140100:
079                    // Line of Contact
080                case 140200:
081                    // Strong Point
082                case 151203:
083                    // Encirclement
084                case 151800:
085                    // Obstacle Belt || Obstacle Zone || Obstacle Line
086                case 270100:
087                case 270200:
088                case 290100:
089                    // Obstacle Free Zone OR Obstacle Restricted Zone OR Antitank Wall
090                case 270300:
091                case 270400:
092                case 290204:
093                    // Ditch - Under Construction || Ditch - Completed
094                case 290201:
095                case 290202:
096                    // Ditch Reinforced, with Antitank Mines
097                case 290203:
098                    // Unspecified (Obstacle)
099                case 290301:
100                    // Single Fence
101                case 290302:
102                    // Double Fence
103                case 290303:
104                    // Double Apron Fence
105                case 290304:
106                    // Low Wire Fence
107                case 290305:
108                    // High Wire Fence
109                case 290306:
110                    // Single Concertina
111                case 290307:
112                    // Double Strand Concertina
113                case 290308:
114                    // Triple Strand Concertina
115                case 290309:
116                    // Fortified Line OR Fortified Area
117                case 290900:
118                case 151000:
119                    return true;
120                default:
121                    return false;
122            }
123        }
124        else
125            return false;
126    }
127
128    private static String makeCacheKey(String symbolID, String lineColor, String fillColor, float lineWidth, int dpi, float patternScale)
129    {
130        //String key = symbolID.substring(0, 20) + String.valueOf(lineColor) + String.valueOf(fillColor) + String.valueOf(size) + String.valueOf(keepUnitRatio);
131        String key = symbolID.substring(0, 7) + symbolID.substring(10, 20) + SymbolID.getFrameShape(symbolID) + lineColor + fillColor + lineWidth + dpi + patternScale;
132        return key;
133    }
134
135    public static LinePattern getLinePattern(String symbolCode, Map<String,String> attributes)
136    {
137        Color lineColor = SymbolUtilities.getLineColorOfAffiliation(symbolCode);
138        Color fillColor = null;
139        int lineWidth = 3;
140        float patternScale = 1f;
141        if(attributes != null)
142        {
143            if(attributes.containsKey(MilStdAttributes.LineColor))
144                lineColor = RendererUtilities.getColorFromHexString(attributes.get(MilStdAttributes.LineColor));
145            if(attributes.containsKey(MilStdAttributes.FillColor))
146                fillColor = RendererUtilities.getColorFromHexString(attributes.get(MilStdAttributes.FillColor));
147            if(attributes.containsKey(MilStdAttributes.LineWidth))
148                lineWidth = Integer.parseInt(attributes.get(MilStdAttributes.LineWidth));
149        }
150        return getLinePattern(symbolCode, lineColor, fillColor, lineWidth,patternScale);
151    }
152
153    public static LinePattern scaleLinePattern(LinePattern lp, float scale)
154    {
155        return getLinePattern(lp._sybmolCode, lp._lineColor,lp._fillColor,lp._lineWidth,scale);
156    }
157    public static LinePattern getLinePattern(String symbolCode, Color lineColor, Color fillColor, float lineWidth)
158    {
159        return getLinePattern(symbolCode,lineColor,fillColor,lineWidth,1f);
160    }
161
162    public static LinePattern getLinePattern(String symbolCode, Color lineColor, Color fillColor, float lineWidth, float patternScale)
163    {
164        LinePattern lp = null;
165        StringBuilder svgPath = null;
166        StringBuilder svgEllipse = null;
167        StringBuilder svgText = null;
168        RectF svgBounds = null;
169        double vOffset = 0;
170        boolean grouped = false;
171        String key = null;
172
173
174        if(symbolCode != null && symbolCode.length() >= 20)
175        {
176            int symbolSet = SymbolID.getSymbolSet(symbolCode);
177            int ec = SymbolID.getEntityCode(symbolCode);
178            int dpi = RendererSettings.getInstance().getDeviceDPI();
179
180            float offset = lineWidth/2f;// + 0.5f;
181            float multiplier = dpi > 96 ? dpi/96f : 1;
182            multiplier = multiplier * patternScale;
183
184            key = makeCacheKey(symbolCode,RendererUtilities.colorToHexString(lineColor,true),RendererUtilities.colorToHexString(fillColor,true),lineWidth,dpi, patternScale);
185            //see if it's in the cache
186            if(_patternCache != null)
187            {
188                lp = (LinePattern)_patternCache.get(key);
189                if(lp != null)
190                    return lp;
191            }
192
193            if(ec == 140100)//FLOT
194            {
195                fillColor=null;
196                double lwOffset = lineWidth/2f;
197                double x = 0;
198                double y = 0;
199                double eRadH = ((10 + lineWidth) * multiplier);
200                double eRadW = eRadH;// * 0.5f;
201                double patternHeight = eRadH + lineWidth;
202                double patternWidth = lwOffset + (eRadW * 2) + lineWidth + lwOffset;
203
204                //build line pattern
205                svgPath = new StringBuilder("<path d=\"");
206
207                //build 'uuuu'
208                y += eRadH + lineWidth;
209                svgPath.append("M ").append(lwOffset).append(" ").append(y).append(" ");
210                svgPath.append("a ").append(eRadW+lwOffset).append(",").append(eRadW+lwOffset).append(" 0 0,1 ").append((eRadW*2) + lineWidth).append(",0\" ");
211
212
213                //calculate bounds of line pattern
214                double left = 0;
215                double width = patternWidth;
216                double top = 0;
217                double bottom = y;
218
219                vOffset = lwOffset + eRadH;
220                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
221            }
222            if(ec == 140200)//Line of Contact
223            {
224                fillColor=null;
225                double lwOffset = lineWidth/2f;
226                double x = 0;
227                double y = 0;
228                double eRadH = ((10 + lineWidth) * multiplier);
229                double eRadW = eRadH;// * 0.5f;
230                double patternHeight = (eRadH + lineWidth)*2;
231                double patternWidth = lwOffset + (eRadW * 2) + lineWidth + lwOffset;
232
233                //build line pattern
234                svgPath = new StringBuilder("<path d=\"");
235
236                //build 'uuuu'
237                y += patternHeight;
238                //red squiggle
239                svgPath.append("M ").append(lwOffset).append(" ").append(y).append(" ");
240                svgPath.append("a ").append(eRadW+lwOffset).append(",").append(eRadW+lwOffset).append(" 0 0,1 ").append((eRadW*2) + lineWidth).append(",0\" ");
241                svgPath.append("style=\"fill:none;stroke:red;");
242                if(lineColor.getAlpha() < 255)
243                    svgPath.append("stroke-opacity:").append(lineColor.getAlpha()/255).append(";");
244                svgPath.append("stroke-width:").append(lineWidth).append("\" />");
245                //affiliation color squiggle
246                svgPath.append("<path d=\"");
247                svgPath.append("M ").append(lwOffset).append(" ").append(0).append(" ");
248                svgPath.append("a ").append(eRadW+lwOffset).append(",").append(eRadW+lwOffset).append(" 0 0,0 ").append((eRadW*2) + lineWidth).append(",0\" ");
249
250                //calculate bounds of line pattern
251                double left = 0;
252                double width = patternWidth;
253                double top = 0;
254                double bottom = y;
255
256                vOffset = patternHeight;
257                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
258            }
259            else if(ec == 151203)//Strong Point
260            {
261                fillColor=null;
262                double lwOffset = lineWidth/2f;
263                double x = 0;
264                double y = 0;
265                double eRadH = ((10 + lineWidth) * multiplier);
266                double eRadW = eRadH * 0.5f;
267                double patternHeight = eRadH + lineWidth;
268                double patternWidth = (eRadW * 2) + lineWidth;
269
270                //build line pattern
271                svgPath = new StringBuilder("<path d=\"");
272
273                //build '_|_' which will repeat
274                y += eRadH + lwOffset;
275                svgPath.append("M ").append(0).append(" ").append(y).append(" ");
276                svgPath.append("l ").append(patternWidth/2).append(" ").append(0).append(" ");
277                svgPath.append("l ").append(0).append(" ").append(-eRadH - lineWidth).append(" ");
278                svgPath.append("m ").append(0).append(" ").append(eRadH + lineWidth).append(" ");
279                svgPath.append("l ").append(patternWidth/2).append(" ").append(0).append("\" ");
280
281                //calculate bounds of line pattern
282                double left = 0;
283                double width = patternWidth;
284                double top = 0;
285                double bottom = y + lwOffset;
286
287                vOffset = lwOffset + eRadH;
288                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
289            }
290            else if(ec == 151800)//Encirclement
291            {
292                double lwOffset = lineWidth/2f;
293                double x = 0;
294                double y = lwOffset;
295                double eRadH = (10 + (lineWidth*2)) * multiplier;
296                double eRadW = eRadH * 0.6f;
297                double patternWidth = (eRadW * 3) + lineWidth;
298
299                //build line pattern
300                svgPath = new StringBuilder("<path d=\"");
301
302                //build '/\'
303                y += lineWidth + (eRadH) + lwOffset;
304                svgPath.append("M ").append(0).append(" ").append(y).append(" ");
305                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append(" ");
306                svgPath.append("l ").append(eRadW).append(" ").append(-eRadH - lineWidth).append(" ");
307                svgPath.append("l ").append(eRadW).append(" ").append(eRadH + lineWidth).append(" ");
308                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append(" Z\" ");
309
310                //calculate bounds of line pattern
311                double left = 0;
312                double width = patternWidth;
313                double top = 0;
314                double bottom = y + lwOffset;
315
316                vOffset = y;
317                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
318            }
319            else if(ec == 270100 || ec == 270200 || ec == 290100)//Obstacle Belt || Obstacle Zone || Obstacle Line
320            {
321                // _/\_/\_/\_
322
323                fillColor = null;
324                double lwOffset = lineWidth/2f;
325                double x = 0;
326                double y = lwOffset;
327                double eRadH = (10 + (lineWidth)) * multiplier;
328                double eRadW = eRadH * 0.6f;
329                double patternWidth = (eRadW * 3) + lineWidth;
330
331                //build line pattern
332                svgPath = new StringBuilder("<path d=\"");
333
334                //build '_/\_'
335                y += lwOffset + (eRadH) + lwOffset;
336                svgPath.append("M ").append(0).append(" ").append(y).append(" ");
337                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append(" ");
338                svgPath.append("l ").append(eRadW).append(" ").append(-eRadH).append(" ");
339                svgPath.append("l ").append(eRadW).append(" ").append(eRadH).append(" ");
340                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append("\" ");
341
342                //calculate bounds of line pattern
343                double left = 0;
344                double width = patternWidth;
345                double top = 0;
346                double bottom = y + lwOffset;
347
348                vOffset = y;
349                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
350
351                /*if(ec == 290100)//flip image because obstacle line points are reversed by renderer
352                {
353                    svgPath.insert(0,"<g transform=\"translate(0, " + bottom + ") scale(1, -1)\">");
354                    vOffset=lwOffset;
355                    grouped=true;
356                }//*/
357            }
358            else if(ec == 270300 || ec == 270400 || ec == 290204)
359            {
360                //Obstacle Free Zone OR
361                //Obstacle Restricted Zone (has fill, might be a problem) OR
362                // Antitank Wall
363                // _  _  _
364                //  \/ \/
365                fillColor = null;
366                double lwOffset = lineWidth/2f;
367                double x = 0;
368                double y = lwOffset;
369                double eRadH = (10 + (lineWidth)) * multiplier;
370                double eRadW = eRadH * 0.6f;
371                double patternWidth = (eRadW * 3) + lineWidth;
372
373                //build line pattern
374                svgPath = new StringBuilder("<path d=\"");
375
376                //build
377                // _  _  _
378                //  \/ \/
379                y += lwOffset + (eRadH) + lwOffset;
380                svgPath.append("M ").append(0).append(" ").append(lwOffset).append(" ");
381                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append(" ");
382                svgPath.append("l ").append(eRadW).append(" ").append(eRadH).append(" ");
383                svgPath.append("l ").append(eRadW).append(" ").append(-eRadH).append(" ");
384                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append("\" ");
385
386                //calculate bounds of line pattern
387                double left = 0;
388                double width = patternWidth;
389                double top = 0;
390                double bottom = y + lwOffset;
391
392                vOffset = lwOffset;
393                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
394            }
395            else if(ec == 290201 || ec == 290202)//Ditch - Under Construction || Ditch - Completed
396            {
397                fillColor = null;
398                double lwOffset = lineWidth/2f;
399                double x = 0;
400                double y = lwOffset;
401                double eRadH = (10 + (lineWidth)) * multiplier;
402                double eRadW = eRadH * 0.6f;
403                double patternWidth = (eRadW * 2) + lineWidth;
404
405                //build line pattern
406                svgPath = new StringBuilder("<path d=\"");
407
408                //build '/\'
409                y += lwOffset + (eRadH) + lwOffset;
410                svgPath.append("M ").append(lwOffset).append(" ").append(y).append(" ");
411                svgPath.append("l ").append(eRadW).append(" ").append(-eRadH).append(" ");
412                svgPath.append("l ").append(eRadW).append(" ").append(eRadH).append(" Z").append("\" ");
413
414                if(ec == 290202)
415                    fillColor = SymbolUtilities.getLineColorOfAffiliation(symbolCode);
416
417
418                //calculate bounds of line pattern
419                double left = 0;
420                double width = patternWidth;
421                double top = 0;
422                double bottom = y + lwOffset;
423
424                vOffset = y;
425                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
426            }
427            else if(ec == 290203)//Ditch Reinforced, with Antitank Mines
428            {
429
430                fillColor = SymbolUtilities.getLineColorOfAffiliation(symbolCode);
431                double lwOffset = lineWidth/2f;
432                double x = 0;
433                double y = lwOffset;
434                double eRadH = (10 + (lineWidth)) * multiplier;
435                double eRadW = eRadH * 0.6f;
436                double patternWidth = (eRadW * 3) + lineWidth*2 + ((eRadW*0.6));
437
438                //build line pattern
439                svgPath = new StringBuilder("<path d=\"");
440                svgEllipse = new StringBuilder("<circle ");
441
442                //build
443                // ______
444                //  \/ 0
445                y += lwOffset + (eRadH) + lwOffset;
446                svgPath.append("M ").append(lwOffset).append(" ").append(lwOffset).append(" ");
447                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append(" ");
448                svgPath.append("l ").append(eRadW).append(" ").append(eRadH).append(" ");
449                svgPath.append("l ").append(eRadW).append(" ").append(-eRadH).append(" Z ");
450                svgPath.append("M ").append(0).append(" ").append(lwOffset).append(" ");
451                svgPath.append("l ").append(patternWidth).append(" ").append(0).append("\" ");
452
453                svgEllipse.append("r=\"").append((eRadW*0.6)-lwOffset).append("\" cx=\"").append(lineWidth + (eRadW*3) + lineWidth).append("\" cy=\"").append((y + lwOffset)*0.6).append("\" ");
454
455                //calculate bounds of line pattern
456                double left = 0;
457                double width = patternWidth;
458                double top = 0;
459                double bottom = y + lwOffset;
460
461                vOffset = lwOffset;
462                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
463            }
464            else if(ec == 290301)//Unspecified
465            {
466                fillColor = null;
467                double lwOffset = lineWidth/2f;
468                double x = 0;
469                double y = lwOffset;
470                double eRadH = ((6 + lineWidth) * multiplier);
471                double eRadW = eRadH;
472                double patternWidth = (eRadH + lineWidth) * 2.5f;
473
474                //build line pattern
475                svgPath = new StringBuilder("<path d=\"");
476
477                //build 'X'
478                double eCenterX = lwOffset + (patternWidth/2);
479                double eCenterY = lwOffset + (eRadH);
480                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY - eRadH).append(" ");
481                svgPath.append("l ").append(eRadW*2).append(" ").append(eRadH*2).append(" ");
482                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY + eRadH).append(" ");
483                svgPath.append("l ").append(eRadW*2).append(" ").append(-(eRadH*2)).append(" ").append("\" ");
484
485                //calculate bounds of line pattern
486                y += (eRadH*2) + lwOffset;
487                double left = 0;
488                double width = patternWidth + lineWidth;
489                double top = 0;
490                double bottom = eRadH*2 + lineWidth;
491
492                vOffset = lwOffset;
493                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
494
495                //flip image because unspecified wire points are reversed by renderer
496                /*{
497                    vOffset=bottom-lwOffset;
498                }//*/
499            }
500            else if(ec == 290302)//Single Fence
501            {
502                fillColor = null;
503                double lwOffset = lineWidth/2f;
504                double x = 0;
505                double y = lwOffset;
506                double eRadH = ((6 + lineWidth) * multiplier);
507                double eRadW = eRadH;
508                double patternWidth = (eRadH + lineWidth) * 3.5f;
509
510                //build line pattern
511                svgPath = new StringBuilder("<path d=\"");
512
513                //build 'X'
514                double eCenterX = lwOffset + (patternWidth/2);
515                double eCenterY = lwOffset + (eRadH);
516                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY - eRadH).append(" ");
517                svgPath.append("l ").append(eRadW*2).append(" ").append(eRadH*2).append(" ");
518                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY + eRadH).append(" ");
519                svgPath.append("l ").append(eRadW*2).append(" ").append(-(eRadH*2)).append(" ");
520
521                //center line
522                y += lineWidth + (eRadH * 2) + lineWidth;
523                svgPath.append("M ").append(x).append(" ").append(eCenterY).append(" ");
524                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
525
526                //calculate bounds of line pattern
527                y += (eRadH*2) + lwOffset;
528                double left = 0;
529                double width = patternWidth + lineWidth;
530                double top = 0;
531                double bottom = eRadH*2 + lineWidth;
532
533                vOffset = eCenterY;
534                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
535            }
536            else if(ec == 290303)//Double Fence
537            {
538                fillColor = null;
539                double lwOffset = lineWidth/2f;
540                double x = 0;
541                double y = lwOffset;
542                double eRadH = ((6 + lineWidth) * multiplier);
543                double eRadW = eRadH;
544                double patternWidth = (eRadH + lineWidth) * 5.5f;
545
546                //build line pattern
547                svgPath = new StringBuilder("<path d=\"");
548
549                //build 'X1'
550                double eCenterX = (patternWidth/2) - eRadW - lwOffset;
551                double eCenterY = lwOffset + (eRadH);
552                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY - eRadH).append(" ");
553                svgPath.append("l ").append(eRadW*2).append(" ").append(eRadH*2).append(" ");
554                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY + eRadH).append(" ");
555                svgPath.append("l ").append(eRadW*2).append(" ").append(-(eRadH*2)).append(" ");
556
557                //build 'X2'
558                eCenterX = (patternWidth/2) + eRadW + lwOffset;
559                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY - eRadH).append(" ");
560                svgPath.append("l ").append(eRadW*2).append(" ").append(eRadH*2).append(" ");
561                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY + eRadH).append(" ");
562                svgPath.append("l ").append(eRadW*2).append(" ").append(-(eRadH*2)).append(" ");
563
564                //center line
565                y += lineWidth + (eRadH * 2) + lineWidth;
566                svgPath.append("M ").append(x).append(" ").append(eCenterY).append(" ");
567                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
568
569                //calculate bounds of line pattern
570                y += (eRadH*2) + lwOffset;
571                double left = 0;
572                double width = patternWidth + lineWidth;
573                double top = 0;
574                double bottom = eRadH*2 + lineWidth;
575
576                vOffset = eCenterY;
577                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
578            }
579            else if(ec == 290304)//Double Apron Fence
580            {
581                fillColor = null;
582                double lwOffset = lineWidth/2f;
583                double x = 0;
584                double y = lwOffset;
585                double eRadH = ((6 + lineWidth) * multiplier);
586                double eRadW = eRadH;
587                double patternWidth = (eRadH + lineWidth) * 2.5f;
588
589                //build line pattern
590                svgPath = new StringBuilder("<path d=\"");
591
592                //build 'X'
593                double eCenterX = lwOffset + (patternWidth/2);
594                double eCenterY = lwOffset + (eRadH);
595                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY - eRadH).append(" ");
596                svgPath.append("l ").append(eRadW*2).append(" ").append(eRadH*2).append(" ");
597                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY + eRadH).append(" ");
598                svgPath.append("l ").append(eRadW*2).append(" ").append(-(eRadH*2)).append(" ");
599
600                //center line
601                y += lineWidth + (eRadH * 2) + lineWidth;
602                svgPath.append("M ").append(x).append(" ").append(eCenterY).append(" ");
603                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
604
605                //calculate bounds of line pattern
606                y += (eRadH*2) + lwOffset;
607                double left = 0;
608                double width = patternWidth + lineWidth;
609                double top = 0;
610                double bottom = eRadH*2 + lineWidth;
611
612                vOffset = eCenterY;
613                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
614            }
615            else if(ec == 290305)//Low Wire Fence
616            {
617                fillColor = null;
618                double lwOffset = lineWidth/2f;
619                double x = 0;
620                double y = lwOffset;
621                double eRadH = ((6 + lineWidth) * multiplier);
622                double eRadW = eRadH;
623                double patternWidth = (eRadH + lineWidth) * 2.5f;
624
625                //build line pattern
626                svgPath = new StringBuilder("<path d=\"");
627
628                //build 'X'
629                double eCenterX = lwOffset + (patternWidth/2);
630                double eCenterY = lwOffset + (eRadH);
631                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY - eRadH).append(" ");
632                svgPath.append("l ").append(eRadW*2).append(" ").append(eRadH*2).append(" ");
633                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY + eRadH).append(" ");
634                svgPath.append("l ").append(eRadW*2).append(" ").append(-(eRadH*2)).append(" ");
635
636                //top line
637                svgPath.append("M ").append(x).append(" ").append(lwOffset).append(" ");
638                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
639
640                //calculate bounds of line pattern
641                y += (eRadH*2) + lwOffset;
642                double left = 0;
643                double width = patternWidth + lineWidth;
644                double top = 0;
645                double bottom = eRadH*2 + lineWidth;
646
647                vOffset = lwOffset;
648                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
649
650            }
651            else if(ec == 290306)//High Wire Fence
652            {
653                fillColor = null;
654                double lwOffset = lineWidth/2f;
655                double x = 0;
656                double y = lwOffset;
657                double eRadH = ((6 + lineWidth) * multiplier);
658                double eRadW = eRadH;
659                double patternWidth = (eRadH + lineWidth) * 2.5f;
660
661                //build line pattern
662                svgPath = new StringBuilder("<path d=\"");
663
664                //build 'X'
665                double eCenterX = lwOffset + (patternWidth/2);
666                double eCenterY = lwOffset + (eRadH);
667                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY - eRadH).append(" ");
668                svgPath.append("l ").append(eRadW*2).append(" ").append(eRadH*2).append(" ");
669                svgPath.append("M ").append(eCenterX-eRadW).append(" ").append(eCenterY + eRadH).append(" ");
670                svgPath.append("l ").append(eRadW*2).append(" ").append(-(eRadH*2)).append(" ");
671
672                //top line
673                svgPath.append("M ").append(x).append(" ").append(lwOffset).append(" ");
674                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0");
675
676                //calculate bounds of line pattern
677                y += (eRadH*2) + lwOffset;
678                double left = 0;
679                double width = patternWidth + lineWidth;
680                double top = 0;
681                double bottom = eRadH*2 + lineWidth;
682
683                //bottom line
684                svgPath.append("M ").append(x).append(" ").append(bottom - lwOffset).append(" ");
685                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
686
687                vOffset = lwOffset;
688                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
689            }
690            else if(ec == 290307)//Single Concertina
691            {
692                fillColor = null;
693                double lwOffset = lineWidth/2f;
694                double x = 0;
695                double y = lwOffset;
696                double eRadH = (5 + (lineWidth)) * multiplier;
697                double eRadW = eRadH * 0.6f;
698                double patternWidth = eRadH * 2.5f;
699
700
701                //build ellipse
702                svgEllipse = new StringBuilder("<ellipse ");
703                double eCenterX = lwOffset + (patternWidth/2);
704                double eCenterY = lineWidth + lwOffset + eRadH;
705                svgEllipse.append("cx=\"").append(eCenterX).append("\" cy=\"").append(eCenterY).append("\" ");
706                svgEllipse.append("rx=\"").append(eRadW).append("\" ry=\"").append(eRadH).append("\" ");
707
708                //build line pattern
709                svgPath = new StringBuilder("<path d=\"");
710
711                //top line
712                svgPath.append("M ").append(x).append(" ").append(lwOffset).append(" ");
713                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
714
715                //calculate bounds of line pattern
716                y += lineWidth + (eRadH * 2) + lineWidth;
717                double left = 0;
718                double width = patternWidth + lineWidth;
719                double top = 0;
720                double bottom = y + lwOffset;
721
722                vOffset = lwOffset;
723                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
724
725                /*svgPath.insert(0,"<g transform=\"translate(0, " + bottom + ") scale(1, -1)\">");
726                vOffset=lwOffset;
727                grouped=true;//*/
728
729            }else if(ec == 290308)//Double Strand Concertina
730            {
731                fillColor = null;
732                double lwOffset = lineWidth/2f;
733                double x = 0;
734                double y = lwOffset;
735                double eRadH = (5 + (lineWidth)) * multiplier;
736                double eRadW = eRadH * 0.6f;
737                double patternWidth = eRadH * 2.5f;
738
739
740                //build ellipse
741                svgEllipse = new StringBuilder("<ellipse ");
742                double eCenterX = lwOffset + (patternWidth/2);
743                double eCenterY = lineWidth + lwOffset + eRadH;
744                svgEllipse.append("cx=\"").append(eCenterX).append("\" cy=\"").append(eCenterY).append("\" ");
745                svgEllipse.append("rx=\"").append(eRadW).append("\" ry=\"").append(eRadH).append("\" ");
746
747                //build line pattern
748                svgPath = new StringBuilder("<path d=\"");
749
750                //middle line
751                svgPath.append("M ").append(x).append(" ").append(eCenterY).append(" ");
752                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0 ");
753
754                //top line
755                svgPath.append("M ").append(x).append(" ").append(lwOffset).append(" ");
756                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
757
758
759                //calculate bounds of line pattern
760                y += lineWidth + (eRadH * 2) + lineWidth;
761                double left = 0;
762                double width = patternWidth + lineWidth;
763                double top = 0;
764                double bottom = y + lwOffset;
765
766                vOffset = lwOffset;
767                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
768
769                /*svgPath.insert(0,"<g transform=\"translate(0, " + bottom + ") scale(1, -1)\">");
770                vOffset=bottom - lwOffset;
771                //grouped=true;//*/
772            }
773            else if(ec == 290309)//Triple Strand Concertina
774            {
775                fillColor = null;
776                double lwOffset = lineWidth/2f;
777                double x = 0;
778                double y = lwOffset;
779                double eRadH = (5 + (lineWidth)) * multiplier;
780                double eRadW = eRadH * 0.6f;
781                double patternWidth = eRadH * 2.5f;
782
783
784                //build ellipse
785                svgEllipse = new StringBuilder("<ellipse ");
786                double eCenterX = lwOffset + (patternWidth/2);
787                double eCenterY = lineWidth + lwOffset + eRadH;
788                svgEllipse.append("cx=\"").append(eCenterX).append("\" cy=\"").append(eCenterY).append("\" ");
789                svgEllipse.append("rx=\"").append(eRadW).append("\" ry=\"").append(eRadH).append("\" ");
790
791                //build line pattern
792                svgPath = new StringBuilder("<path d=\"");
793
794                //top line
795                svgPath.append("M ").append(x).append(" ").append(y).append(" ");
796                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0 ");
797
798                //bottom line
799                y += lineWidth + (eRadH * 2) + lineWidth;
800                svgPath.append("M ").append(x).append(" ").append(y).append(" ");
801                svgPath.append("l ").append(patternWidth + lineWidth).append(" 0").append("\" ");
802
803
804                //calculate bounds of line pattern
805                double left = 0;
806                double width = patternWidth + lineWidth;
807                double top = 0;
808                double bottom = y + lwOffset;
809
810                vOffset = lwOffset;
811                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
812
813                /*//svgPath.insert(0,"<g transform=\"translate(0, " + bottom + ") scale(1, -1)\">");
814                vOffset=bottom - lwOffset;
815                //grouped=true;//*/
816            }
817            else if(ec == 290900 || ec == 151000)//Fortified Line OR Fortified Area
818            {
819                fillColor = null;
820                double lwOffset = lineWidth/2f;
821                double x = 0;
822                double y = 0;
823                double eRadH = (10 + lineWidth) * multiplier;
824                double eRadW = eRadH;
825                double patternWidth = (eRadW * 2) + (lineWidth*2);
826
827                //build line pattern
828                svgPath = new StringBuilder("<path d=\"");
829                //         _
830                //build '_| |_'
831                y += lwOffset + (eRadH) + lwOffset;
832                svgPath.append("M ").append(0).append(" ").append(y).append(" ");
833                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append(" ");
834                svgPath.append("l ").append(0).append(" ").append(-eRadH - lwOffset).append(" ");
835                svgPath.append("l ").append(eRadW + lineWidth).append(" ").append(0).append(" ");
836                svgPath.append("l ").append(0).append(" ").append(eRadH + lwOffset).append(" ");
837                svgPath.append("l ").append((eRadW/2) + lwOffset).append(" ").append(0).append("\" ");
838
839                //calculate bounds of line pattern
840                double left = 0;
841                double width = patternWidth;
842                double top = 0;
843                double bottom = y + lwOffset;
844
845                vOffset = y;
846                svgBounds = RectUtilities.makeRectF((float)left,(float)top,(float)width,(float)(bottom - top));
847            }
848
849            if(svgPath != null)
850            {
851                //build style string
852                StringBuilder sbStyle = new StringBuilder("style=\"");
853                if(fillColor != null) {
854                    sbStyle.append("fill:").append(RendererUtilities.colorToHexString(fillColor, false)).append(";");
855                    if(fillColor.getAlpha() < 255)
856                        sbStyle.append("fill-opacity:").append(fillColor.getAlpha()/255).append(";");
857                }
858                else
859                    sbStyle.append("fill:none;");
860                sbStyle.append("stroke:").append(RendererUtilities.colorToHexString(lineColor, false)).append(";");
861                if(lineColor.getAlpha() < 255)
862                    sbStyle.append("stroke-opacity:").append(lineColor.getAlpha()/255).append(";");
863                sbStyle.append("stroke-width:").append(lineWidth).append("\"");
864
865                //add style to SVGPath
866                svgPath.append(sbStyle);
867                svgPath.append(" />");//close path tag
868
869                //add style to Ellipse if present
870                if(svgEllipse != null ){
871                    svgEllipse.append(sbStyle);
872                    svgEllipse.append(" />");//close path tag
873                }
874
875                //build svg tag and insert path
876                StringBuilder sbSVG = new StringBuilder("<svg xmlns=\"http://www.w3.org/2000/svg\" ");
877                sbSVG.append("width=\"").append(svgBounds.width()).append("\" height=\"").append(svgBounds.height()).append("\" ");
878                sbSVG.append("viewBox=\"").append(svgBounds.left).append(" ").append(svgBounds.top).append(" ").append(svgBounds.width()).append(" ").append(svgBounds.height()).append("\" ");
879                sbSVG.append("fill=\"none\">");
880
881                sbSVG.append(svgPath);
882                if(svgEllipse != null)
883                    sbSVG.append(svgEllipse);
884                if(grouped)
885                    sbSVG.append("</g>");
886                sbSVG.append("</svg>");
887
888                //convert to image
889                Rect biBounds = RectUtilities.makeRect(svgBounds.left,svgBounds.top,svgBounds.width(),svgBounds.height());
890                Bitmap bmp = RendererUtilities.renderSVG(biBounds, sbSVG.toString());
891
892                if(bmp != null)
893                    lp = new LinePattern(bmp,sbSVG.toString(), vOffset, symbolCode, lineColor, fillColor, lineWidth);
894            }
895        }
896        if(_patternCache != null && _patternCache.get(key) == null && lp != null)
897            _patternCache.put(key, lp);
898        return lp;
899    }
900
901    /**
902     * Calculates a vertically expanded SVG viewBox that places verticalAnchorY
903     * at the exact vertical center of the resulting SVG coordinate space.
904     *
905     * The original graphic remains unchanged; the extra area is transparent
906     * whitespace introduced by expanding the viewBox.
907     */
908    private static String createVerticallyCenteredViewBox(
909            float minX,
910            float minY,
911            float maxX,
912            float maxY,
913            float verticalAnchorY) {
914
915        // Preserve the original horizontal range.
916        float width = maxX - minX;
917
918        // Find the farthest existing vertical edge from the desired anchor.
919        float distanceToTop = verticalAnchorY - minY;
920        float distanceToBottom = maxY - verticalAnchorY;
921        float halfHeight = Math.max(distanceToTop, distanceToBottom);
922
923        // Make the anchor equidistant from the top and bottom viewBox edges.
924        float centeredMinY = verticalAnchorY - halfHeight;
925        float centeredHeight = halfHeight * 2.0f;
926
927        // Returned in standard SVG viewBox order:
928        // min-x, min-y, width, height.
929        float[] viewBox = new float[] {
930                minX,
931                centeredMinY,
932                width,
933                centeredHeight
934        };
935
936        String svgViewBox = String.format(
937                Locale.US,
938                "viewBox=\"%f %f %f %f\"",
939                viewBox[0],
940                viewBox[1],
941                viewBox[2],
942                viewBox[3]
943        );
944        return svgViewBox;
945    }
946
947    public static LinePattern centerLinePattern(LinePattern linePattern)
948    {
949        Bitmap original = linePattern.getLinePatternImage();
950        int y = (int)Math.round(linePattern.getLinePatternVerticalOffset());
951        // Validate the source image.
952        if (original == null) {
953            throw new IllegalArgumentException("Original image cannot be null.");
954        }
955
956        int width = original.getWidth();
957        int height = original.getHeight();
958
959        /*
960         * y represents a vertical position in the image:
961         * 0      = top edge
962         * height = bottom edge
963         */
964        if (y < 0 || y > height) {
965            throw new IllegalArgumentException(
966                    "y must be between 0 and " + height + "."
967            );
968        }
969
970        /*
971         * Determine the larger distance from y to either image edge.
972         *
973         * y                 = distance from y to the top
974         * height - y        = distance from y to the bottom
975         */
976        int halfHeight = Math.max(y, height - y);
977
978        /*
979         * Make a canvas whose center is exactly halfHeight.
980         * This guarantees the full original image fits without clipping.
981         */
982        int newHeight = halfHeight * 2;
983
984        // Position the source image such that its y-coordinate is at newHeight / 2.
985        int drawY = halfHeight - y;
986
987        // ARGB provides transparent padding above or below the source image.
988        Bitmap result = Bitmap.createBitmap(
989                width,
990                newHeight,
991                Bitmap.Config.ARGB_8888);
992
993        Canvas ctx = new Canvas(result);
994
995        ctx.drawBitmap(original,0,drawY,null);
996
997
998        return new LinePattern(result,"",result.getHeight()/2f, linePattern._sybmolCode, linePattern._lineColor, linePattern._fillColor, linePattern._lineWidth);
999    }
1000}
1001
1002