/*
 * 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;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.io.ScratchFile;
import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDAppearanceHandler;
import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDFreeTextAppearanceHandler;
import org.apache.pdfbox.pdmodel.interactive.form.PDVariableText;

/**
 *
 * @author Paul King
 */
public class PDAnnotationFreeText extends PDAnnotationMarkup
{
    /**
     * The type of annotation.
     */
    public static final String SUB_TYPE = "FreeText";

    private PDAppearanceHandler freeTextAppearanceHandler;

    public PDAnnotationFreeText()
    {
        getCOSObject().setName(COSName.SUBTYPE, SUB_TYPE);
    }

    /**
     * Creates a FreeText annotation from a COSDictionary, expected to be a correct object definition.
     *
     * @param field the PDF object to represent as a field.
     */
    public PDAnnotationFreeText(COSDictionary field)
    {
        super(field);
    }

    /**
     * Get the default appearance.
     * 
     * @return a string describing the default appearance.
     */
    public String getDefaultAppearance()
    {
        return getCOSObject().getString(COSName.DA);
    }

    /**
     * Set the default appearance.
     *
     * @param daValue a string describing the default appearance.
     */
    public void setDefaultAppearance(String daValue)
    {
        getCOSObject().setString(COSName.DA, daValue);
    }

    /**
     * Get the default style string.
     *
     * The default style string defines the default style for rich text fields.
     *
     * @return the DS element of the dictionary object
     */
    public String getDefaultStyleString()
    {
        return getCOSObject().getString(COSName.DS);
    }

    /**
     * Set the default style string.
     *
     * Providing null as the value will remove the default style string.
     *
     * @param defaultStyleString a string describing the default style.
     */
    public void setDefaultStyleString(String defaultStyleString)
    {
        getCOSObject().setString(COSName.DS, defaultStyleString);
    }

    /**
     * This will get the 'quadding' or justification of the text to be displayed.
     * <br>
     * 0 - Left (default)<br>
     * 1 - Centered<br>
     * 2 - Right<br>
     * Please see the QUADDING_CONSTANTS in {@link PDVariableText }.
     *
     * @return The justification of the text strings.
     */
    public int getQ()
    {
        return getCOSObject().getInt(COSName.Q, 0);
    }

    /**
     * This will set the quadding/justification of the text. Please see the QUADDING_CONSTANTS
     * in {@link PDVariableText }.
     *
     * @param q The new text justification.
     */
    public void setQ(int q)
    {
        getCOSObject().setInt(COSName.Q, q);
    }

    /**
     * Set a custom appearance handler for generating the annotations appearance streams.
     * 
     * @param freeTextAppearanceHandler
     */
    public void setCustomFreeTextAppearanceHandler(PDAppearanceHandler freeTextAppearanceHandler)
    {
        this.freeTextAppearanceHandler = freeTextAppearanceHandler;
    }

    @Override
    public void constructAppearances(ScratchFile scratchFile)
    {
        if (freeTextAppearanceHandler == null)
        {
            PDFreeTextAppearanceHandler appearanceHandler = new PDFreeTextAppearanceHandler(this);
            appearanceHandler.generateAppearanceStreams();
        }
        else
        {
            freeTextAppearanceHandler.generateAppearanceStreams();
        }
    }
}

@@ -700,26 +700,142 @@
 .cos.COS
-Dictionary
+Array;%0Aimport org.apache.pdfbox.cos.COSBase;%0Aimport org.apache.pdfbox.cos.COSDictionary;%0Aimport org.apache.pdfbox.cos.COSFloat
 ;%0Aimport
@@ -3604,32 +3604,1356 @@
 e.Q, q);%0A    %7D%0A%0A
+    /**%0A     * This will set the margin between the annotations %22outer%22 rectangle defined by%0A     * /Rect and the border.%0A     * %0A     * @param margin%0A     */%0A    public void setMargins(float margin) %7B%0A        setMargins(margin, margin, margin, margin);%0A    %7D%0A    %0A    /**%0A     * This will set the margin between the annotations %22outer%22 rectangle defined by%0A     * /Rect and the border.%0A     * %0A     * @param margin%0A     */%0A    public void setMargins(float marginLeft, float marginTop, float marginRight, float marginBottom)%0A    %7B%0A        COSArray margins = new COSArray();%0A        margins.add(new COSFloat(marginLeft));%0A        margins.add(new COSFloat(marginTop));%0A        margins.add(new COSFloat(marginRight));%0A        margins.add(new COSFloat(marginBottom));%0A        getCOSObject().setItem(COSName.RD, margins);    %0A    %7D%0A    %0A    /**%0A     * This will get the margin between the annotations %22outer%22 rectangle defined by%0A     * /Rect and the border.%0A     * %0A     * @return the margins. If the entry hasn't been set defaults to 0 on all sides.%0A     */%0A    public float%5B%5D getMargins()%0A    %7B%0A        COSBase margin = getCOSObject().getItem(COSName.RD);%0A        if (margin instanceof COSArray)%0A        %7B%0A            return ((COSArray) margin).toFloatArray();%0A        %7D%0A        return new float%5B%5D%7B0f, 0f, 0f, 0f%7D;%0A    %7D%0A    %0A
     /**%0A     * S

support setting, getting the /RD value (margin) for FreeText annotation