/*
 * Copyright (c) 1997-2024 IDRsolutions (https://www.idrsolutions.com)
 */
package org.jpedal.examples;

import org.jpedal.color.PdfPaint;
import org.jpedal.external.JPedalHelper;
import org.jpedal.fonts.PdfFont;
import org.jpedal.fonts.glyph.PdfJavaGlyphs;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class ExampleHelper implements JPedalHelper {

    /**
     * allow user to alter font mapping for substitution - return null if not used
     *
     @param pdfJavaGlyphs an object containing data of glyphs
     @param name the name of the font
     @param size font size
     @return font with altered mapping
     */
    @Override
    public Font setFont(final PdfJavaGlyphs pdfJavaGlyphs, final String name, final int size) {
        return null;
    }

    /**
     * allow user to alter font mapping for substitution - return null if not use
     *
     @param pdfFont an object containing data of fonts
     @param size font size
     @return the java font
     */
    @Override
    public Font getJavaFontX(final PdfFont pdfFont, final int size) {
        return null;
    }

    /**
     * allow user to alter colour (ie to convert to bw
     *
     @param g2 the graphics containing the colour to be altered
     @param col the color to be altered
     @param pageNumber the page number being modified
     @param isPrinting true if set to printing
     */
    @Override
    public void setPaint(final Graphics2D g2, final PdfPaint col, final int pageNumber, final boolean isPrinting) {

        //example here converts to bw for printing
        if (isPrinting) { //only on printout

            final int rgb = col.getRGB();

            //black and white conversion
            if (rgb > -16777216 2) { //less than 50% is white
                g2.setPaint(Color.WHITE);
            else {
                g2.setPaint(Color.BLACK);
            }
        else {
            g2.setPaint(col);
        }
    }

    /**
     * allow user to alter colour (ie to convert to bw
     *
     @param image the image to be altered
     @param isPrinting true if set to printing
     @param pageNumber the page number being modified
     @return an image with the altered colour
     */
    @Override
    public BufferedImage processImage(final BufferedImage image, final int pageNumber, final boolean isPrinting) {

        BufferedImage newImage = null;

        if (isPrinting) { //only on printout

            //black and white conversion
            newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
            final Graphics2D newG2bw = newImage.createGraphics();
            newG2bw.setPaint(Color.WHITE);
            newG2bw.fillRect(00, image.getWidth(), image.getHeight());
            newG2bw.drawImage(image, 00null);
        }
        return newImage;
    }
}