<commit_before>package net.databinder.components;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import wicket.Component;
import wicket.Resource;
import wicket.WicketRuntimeException;
import wicket.markup.ComponentTag;
import wicket.markup.html.image.Image;
import wicket.markup.html.image.resource.RenderedDynamicImageResource;
import wicket.model.ICompoundModel;
import wicket.model.IModel;

/**
 * Renders its model text into a PNG, using any typeface available to the JVM. The size of 
 * the image is determined by the model text and the characteristics of font selected. The
 * default font is 14pt sans, plain black on a white background. The image's alt attribute
 * will be set to the model text. <p>This class is inspired by, and draws code from, Wicket's 
 * DefaultButtonImageResource. </p>
 * @author <NAME>
 * @see wicket.markup.html.image.resource.DefaultButtonImageResource
 */
public class RenderedLabel extends Image {

	private Color backgroundColor = Color.WHITE;
	private Color color = Color.BLACK;
	private Font font = new Font("sans", Font.PLAIN, 14);
	private Integer maxWidth;
	
	private RenderedTextImageResource resource;
	
	/**
	 * Constructor to be used if model is derived from a compound property model. The 
	 * model object <b>must</b> be a string.
	 * @param id
	 */
	public RenderedLabel(String id) {
		super(id);
		setImageResource(resource = new RenderedTextImageResource());
	}
	
	/**
	 * Constructor with explicit model. The model object <b>must</b> be a string.
	 * @param id Wicket id
	 * @param model model for 
	 */
	public RenderedLabel(String id, IModel model) {
		super(id, model);
		setImageResource(resource = new RenderedTextImageResource());
	}
	
	/** Adds alt attribute for accessibility. */
	@Override
	protected void onComponentTag(ComponentTag tag) {
		super.onComponentTag(tag);
		tag.put("alt", getText());
	}
	
	/** Restores  compound model resolution that is disabled in  the Image superclass. */
	@Override
	protected IModel initModel() {
		// c&p'd from Component 
		for (Component current = getParent(); current != null; current = current.getParent())
		{
			final IModel model = current.getModel();
			if (model instanceof ICompoundModel)
			{
				setVersioned(false);
				return model;
			}
		}
		return null;
	}
	
	/**
	 * Inner class that renders the model text into an image  resource.
	 * @see wicket.markup.html.image.resource.DefaultButtonImageResource
	 */
	protected class RenderedTextImageResource extends RenderedDynamicImageResource
	{
		public RenderedTextImageResource()
		{
			super(10, 10,"png");	// tiny default that will resize to fit text
		}
		
		/** Renders text into image. */
		protected boolean render(final Graphics2D graphics)
		{
			String text = getText(); // get text from outer class model
			final int width = getWidth(), height = getHeight();

			// Fill background
			graphics.setColor(backgroundColor);
			graphics.fillRect(0, 0, width, height);

			if (text == null)
				return true;	// no text? we're done here
			
			// Get size of text
			graphics.setFont(font);
			final FontMetrics metrics = graphics.getFontMetrics();
			
			List<String> lines = new LinkedList<String>();
			
			int dxText = breakLines(text, metrics, lines),
				lineHeight = metrics.getHeight(),
				dyText = lineHeight * lines.size();
			
			// grow and redraw if we need to
			if (dxText > width || dyText > height)
			{
				setWidth(dxText);
				setHeight(dyText);
				return false;
			}
			
			// Turn on anti-aliasing
			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			<commit_msg>add alpha support<commit_after>package net.databinder.components;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import wicket.Component;
import wicket.Resource;
import wicket.WicketRuntimeException;
import wicket.markup.ComponentTag;
import wicket.markup.html.image.Image;
import wicket.markup.html.image.resource.RenderedDynamicImageResource;
import wicket.model.ICompoundModel;
import wicket.model.IModel;

/**
 * Renders its model text into a PNG, using any typeface available to the JVM. The size of 
 * the image is determined by the model text and the characteristics of font selected. The
 * default font is 14pt sans, plain black on a white background. The image's alt attribute
 * will be set to the model text. <p>This class is inspired by, and draws code from, Wicket's 
 * DefaultButtonImageResource. </p>
 * @author <NAME>
 * @see wicket.markup.html.image.resource.DefaultButtonImageResource
 */
public class RenderedLabel extends Image {

	private Color backgroundColor = Color.WHITE;
	private Color color = Color.BLACK;
	private Font font = new Font("sans", Font.PLAIN, 14);
	private Integer maxWidth;
	
	private RenderedTextImageResource resource;
	
	/**
	 * Constructor to be used if model is derived from a compound property model. The 
	 * model object <b>must</b> be a string.
	 * @param id
	 */
	public RenderedLabel(String id) {
		super(id);
		setImageResource(resource = new RenderedTextImageResource());
	}
	
	/**
	 * Constructor with explicit model. The model object <b>must</b> be a string.
	 * @param id Wicket id
	 * @param model model for 
	 */
	public RenderedLabel(String id, IModel model) {
		super(id, model);
		setImageResource(resource = new RenderedTextImageResource());
	}
	
	/** Adds alt attribute for accessibility. */
	@Override
	protected void onComponentTag(ComponentTag tag) {
		super.onComponentTag(tag);
		tag.put("alt", getText());
	}
	
	/** Restores  compound model resolution that is disabled in  the Image superclass. */
	@Override
	protected IModel initModel() {
		// c&p'd from Component 
		for (Component current = getParent(); current != null; current = current.getParent())
		{
			final IModel model = current.getModel();
			if (model instanceof ICompoundModel)
			{
				setVersioned(false);
				return model;
			}
		}
		return null;
	}
	
	/**
	 * Inner class that renders the model text into an image  resource.
	 * @see wicket.markup.html.image.resource.DefaultButtonImageResource
	 */
	protected class RenderedTextImageResource extends RenderedDynamicImageResource
	{
		public RenderedTextImageResource()
		{
			super(10, 10,"png");	// tiny default that will resize to fit text
			setType(BufferedImage.TYPE_INT_ARGB); // allow alpha transparency
		}
		
		/** Renders text into image. */
		protected boolean render(final Graphics2D graphics)
		{
			String text = getText(); // get text from outer class model
			final int width = getWidth(), height = getHeight();

			// draw background if not null
			if (backgroundColor != null) {
				graphics.setColor(backgroundColor);
				graphics.fillRect(0, 0, width, height);
			} else
				// otherwise leave transparent; DstOver seems to render more nicely
				graphics.setComposite(AlphaComposite.DstOver);

			if (text == null)
				return true;	// no text? we're done here
			
			// Get size of text
			graphics.setFont(font);
			final FontMetrics metrics = graphics.getFontMetrics();
			
			List<String> lines = new LinkedList<String>();
			
			int dxText = breakLines(text, metrics, lines),
				lineHeight = metrics.getHeight(),
				dyText = lineHeight * lines.size();
			
			// grow and redraw if we need to
			if (dxText > width || dyText > height)
			{
				setWidth(dxText);
				setHeight(dyText);
				return false;
			}
			
			// Turn on anti-aliasing
			graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			
{'fraction_non_alphanumeric': 0.09879518072289156, 'fraction_numerical': 0.0030437539632213063, 'mean_word_length': 3.838036809815951, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '414653', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2287, 'n_words': 737}