<commit_before>/*
 * 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()<commit_msg>PDFBOX-3353: refactor ink annotation handler<commit_after>/*
 * 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();
        // PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available
        AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(ink, ink.getBorderStyle());
        if (ab.width == 0 || ab.color.getComponents().length == 0)
        {
            return;
        }

        try
        {
            try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream())
            {
                cs.setStrokingColor(ab.color);
                if (ab.dashArray != null)
                {
                    cs.setLineDashPattern(ab.dashArray, 0);
                }
                cs.setLineWidth(ab.width);

                for (float[] pathArray : ink.getInkList())
                {
                    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()
{'fraction_non_alphanumeric': 0.07979910714285714, 'fraction_numerical': 0.005301339285714286, 'mean_word_length': 1.6434365781710913, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2149981', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1717, 'n_words': 494}