/*
 * Copyright 2018 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.pdfbox.pdmodel.interactive.annotation.handlers;

import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationInk;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream;

/**
 * Handler to generate the ink annotations appearance.
 *
 */
public class PDInkAppearanceHandler extends PDAbstractAppearanceHandler
{
    private static final Log LOG = LogFactory.getLog(PDInkAppearanceHandler.class);

    public PDInkAppearanceHandler(PDAnnotation annotation)
    {
        super(annotation);
    }

    @Override
    public void generateAppearanceStreams()
    {
        generateNormalAppearance();
        generateRolloverAppearance();
        generateDownAppearance();
    }

    @Override
    public void generateNormalAppearance()
    {
        PDAnnotationInk ink = (PDAnnotationInk) getAnnotation();
        try
        {
            try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream())
            {
                AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(ink, ink.getBorderStyle());
                if (ab.width == 0 || ab.color.getComponents().length == 0)
                {
                    ;
                }
                else
                {
                    cs.setStrokingColor(ab.color);
                    if (ab.dashArray != null)
                    {
                        cs.setLineDashPattern(ab.dashArray, 0);
                    }
                    cs.setLineWidth(ab.width);

                    float[][] inkList = ink.getInkList();
                    // PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available
                    for (float[] pathArray : inkList)
                    {
                        int nPoints = pathArray.length / 2;

                        // "When drawn, the points shall be connected by straight lines or curves 
                        // in an implementation-dependent way" - we do lines.
                        for (int i = 0; i < nPoints; ++i)
                        {
                            float x = pathArray[i * 2];
                            float y = pathArray[i * 2 + 1];

                            if (i == 0)
                            {
                                cs.moveTo(x, y);
                            }
                            else
                            {
                                cs.lineTo(x, y);
                            }
                        }
                        cs.stroke();
                    }
                }
            }
        }
        catch (IOException ex)
        {
            LOG.error(ex);
        }
    }

    @Override
    public void generateRolloverAppearance()
    {
        // No rollover appearance generated
    }

    @Override
    public void generateDownAppearance()
    {
        // No down appearance generated
    }
}
@@ -1661,130 +1661,105 @@
     
-try%0A        %7B%0A            try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream())%0A            %7B%0A        
+// PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available%0A
     
@@ -1853,32 +1853,24 @@
 ));%0A        
-    
-    
 if (ab.width
@@ -1912,32 +1912,24 @@
 ength == 0)%0A
-        
         %7B%0A  
@@ -1938,24 +1938,32 @@
         
+return;%0A
         
 ;%0A      
@@ -1954,17 +1954,18 @@
         
-;
+%7D%0A
 %0A       
@@ -1961,32 +1961,36 @@
  %7D%0A%0A        
+try%0A
         
 %7D%0A          
@@ -1969,33 +1969,33 @@
     try%0A        
-%7D
+%7B
 %0A               
@@ -1987,37 +1987,98 @@
             
-    else%0A    
+try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream())%0A
             
@@ -2071,36 +2071,32 @@
 )%0A            %7B%0A
-    
                 
@@ -2126,20 +2126,16 @@
 color);%0A
-    
         
@@ -2180,34 +2180,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -2258,34 +2258,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2312,187 +2312,8 @@
 );%0A%0A
-                    float%5B%5D%5B%5D inkList = ink.getInkList();%0A                    // PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available%0A    
     
@@ -2356,18 +2356,23 @@
  ink
-List)%0A    
+.getInkList())%0A
     
@@ -2405,20 +2405,16 @@
         
-   
- 
 int nPoi
@@ -2442,20 +2442,16 @@
 h / 2;%0A%0A
-    
         
@@ -2537,20 +2537,16 @@
 curves %0A
-    
         
@@ -2627,28 +2627,24 @@
             
-    
 for (int i =
@@ -2665,20 +2665,16 @@
 s; ++i)%0A
-    
         
@@ -2707,28 +2707,24 @@
             
-    
 float x = pa
@@ -2739,20 +2739,16 @@
 i * 2%5D;%0A
-    
         
@@ -2816,28 +2816,24 @@
             
- 
-   
 if (i == 0)%0A
@@ -2852,32 +2852,26 @@
             
-    %7B%0A  
+%7B%0A
             
@@ -2886,18 +2886,16 @@
         
-  
 cs.moveT
@@ -2923,33 +2923,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -2953,17 +2953,16 @@
         
- 
 else%0A   
@@ -2978,33 +2978,26 @@
             
-    %7B%0A   
+%7B%0A
             
@@ -3012,17 +3012,16 @@
         
- 
 cs.lineT
@@ -3049,34 +3049,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -3093,28 +3093,24 @@
             
-    
 cs.stroke();
@@ -3110,38 +3110,16 @@
 roke();%0A
-                    %7D%0A
         

refactor ink annotation handler