<commit_before>/*
 * This file is part of Geomajas, a component framework for building
 * rich Internet applications (RIA) with sophisticated capabilities for the
 * display, analysis and management of geographic information.
 * It is a building block that allows developers to add maps
 * and other geographic data capabilities to their web applications.
 *
 * Copyright 2008-2010 Geosparc, http://www.geosparc.com, Belgium
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.geomajas.plugin.printing.component.impl;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import org.geomajas.configuration.client.ClientMapInfo;
import org.geomajas.plugin.printing.component.LayoutConstraint;
import org.geomajas.plugin.printing.component.PdfContext;
import org.geomajas.plugin.printing.component.PrintComponentVisitor;
import org.geomajas.plugin.printing.component.ScaleBarComponent;
import org.geomajas.plugin.printing.component.dto.ScaleBarComponentInfo;
import org.geomajas.plugin.printing.component.service.PrintConfigurationService;
import org.geomajas.plugin.printing.component.service.PrintDtoConverterService;
import org.geomajas.plugin.printing.parser.FontConverter;
import org.geomajas.service.GeoService;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.lowagie.text.Rectangle;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

/**
 * Scale bar component for inclusion in printed documents.
 * 
 * @author <NAME>
 */
@Component("ScaleBarComponentPrototype")
@Scope(value = "prototype")
public class ScaleBarComponentImpl extends PrintComponentImpl<ScaleBarComponentInfo> implements ScaleBarComponent {

	@XStreamOmitField
	private final Logger log = LoggerFactory.getLogger(ScaleBarComponentImpl.class);

	/**
	 * The unit (meter, mile, degree)
	 */
	private String unit = "units";

	/**
	 * The number of tics for the scalebar
	 */
	private int ticNumber;

	/**
	 * The labels on top of the ticks
	 */
	private List<String> tickLabels = new ArrayList<String>();

	/**
	 * The calculated sizes of the ticks
	 */
	private List<Rectangle> tickSizes = new ArrayList<Rectangle>();

	/**
	 * The label font
	 */
	@XStreamConverter(FontConverter.class)
	private Font font = new Font("Dialog", Font.PLAIN, 12);

	/**
	 * Width a tic (double checkerboard style).
	 */
	private float ticWidth;

	/**
	 * Height of a tic (double checkerboard style).
	 */
	private float ticHeight;

	@Autowired
	@XStreamOmitField
	private PrintConfigurationService configurationService;
	
	@Autowired
	@XStreamOmitField
	private GeoService geoService;

	@Autowired
	@XStreamOmitField
	private PrintDtoConverterService converterService;

	private static final float[] ALLOW_TICK_NUMBERS = new float[] { 1, 2, 5, 10, 25, 50, 100, 200, 250, 500, 750 };

	private static final String[] UNIT_PREFIXES = new String[] { "n", "m", "", "k", "M" };

	public ScaleBarComponentImpl() {
		getConstraint().setAlignmentX(LayoutConstraint.LEFT);
		getConstraint().setAlignmentY(LayoutConstraint.BOTTOM);
		getConstraint().setMarginX(20);
		getConstraint().setMarginY(20);
		getConstraint().setWidth(200);
	}	
	
	public Font getFont() {
		return font;
	}



	
	public void setFont(Font font) {
		this.font = font;
	}



	/**
	 * Call back visitor.
	 * 
	 * @param visitor
	 */
	public void accept(PrintComponentVisitor visitor) {
		visitor.visit(this);
	}

	@Override
	public void calculateSize(PdfContext context) {
		// the width must be set !!
		float width = getConstraint().getWidth();

		// Calculate width in map units and round.
		float widthInUnits = width / (float) getMap().getPpUnit();

		// Calculate ideal tic width in units
		float ticWidthInUnits = widthInUnits / getTicNumber();

		// Calculate minimal tic width in units
		int ticLog = (int) Math.floor(Math.log10(ticWidthInUnits) / 3);
		float minTicWidthInUnits = (float) Math.pow(10, ticLog * 3);

		// Find the highest nice number
		float nicestNumber = 0;
		for (float allowTickNumber : ALLOW_TICK_NUMBERS) {
			if (minTicWidthInUnits * allowTickNumber > ticWidthInUnits) {
				break;
			} else {
				nicestNumber = allowTickNumber;
			}
		}
		ticWidthInUnits = minTicWidthInUnits * nicestNumber;
		ticWidth = ticWidthInUnits * getMap().getPpUnit();
		ticHeight = ticWidth * 0.3f;
		int ticCount = getTicNumber();<commit_msg>GBE-94 apply improvements in GeoService to prevent TransformException<commit_after>/*
 * This file is part of Geomajas, a component framework for building
 * rich Internet applications (RIA) with sophisticated capabilities for the
 * display, analysis and management of geographic information.
 * It is a building block that allows developers to add maps
 * and other geographic data capabilities to their web applications.
 *
 * Copyright 2008-2010 Geosparc, http://www.geosparc.com, Belgium
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.geomajas.plugin.printing.component.impl;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import org.geomajas.configuration.client.ClientMapInfo;
import org.geomajas.geometry.Crs;
import org.geomajas.plugin.printing.component.LayoutConstraint;
import org.geomajas.plugin.printing.component.PdfContext;
import org.geomajas.plugin.printing.component.PrintComponentVisitor;
import org.geomajas.plugin.printing.component.ScaleBarComponent;
import org.geomajas.plugin.printing.component.dto.ScaleBarComponentInfo;
import org.geomajas.plugin.printing.component.service.PrintConfigurationService;
import org.geomajas.plugin.printing.component.service.PrintDtoConverterService;
import org.geomajas.plugin.printing.parser.FontConverter;
import org.geomajas.service.GeoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.lowagie.text.Rectangle;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamOmitField;

/**
 * Scale bar component for inclusion in printed documents.
 * 
 * @author <NAME>
 */
@Component("ScaleBarComponentPrototype")
@Scope(value = "prototype")
public class ScaleBarComponentImpl extends PrintComponentImpl<ScaleBarComponentInfo> implements ScaleBarComponent {

	@XStreamOmitField
	private final Logger log = LoggerFactory.getLogger(ScaleBarComponentImpl.class);

	/**
	 * The unit (meter, mile, degree)
	 */
	private String unit = "units";

	/**
	 * The number of tics for the scalebar
	 */
	private int ticNumber;

	/**
	 * The labels on top of the ticks
	 */
	private List<String> tickLabels = new ArrayList<String>();

	/**
	 * The calculated sizes of the ticks
	 */
	private List<Rectangle> tickSizes = new ArrayList<Rectangle>();

	/**
	 * The label font
	 */
	@XStreamConverter(FontConverter.class)
	private Font font = new Font("Dialog", Font.PLAIN, 12);

	/**
	 * Width a tic (double checkerboard style).
	 */
	private float ticWidth;

	/**
	 * Height of a tic (double checkerboard style).
	 */
	private float ticHeight;

	@Autowired
	@XStreamOmitField
	private PrintConfigurationService configurationService;
	
	@Autowired
	@XStreamOmitField
	private GeoService geoService;

	@Autowired
	@XStreamOmitField
	private PrintDtoConverterService converterService;

	private static final float[] ALLOW_TICK_NUMBERS = new float[] { 1, 2, 5, 10, 25, 50, 100, 200, 250, 500, 750 };

	private static final String[] UNIT_PREFIXES = new String[] { "n", "m", "", "k", "M" };

	public ScaleBarComponentImpl() {
		getConstraint().setAlignmentX(LayoutConstraint.LEFT);
		getConstraint().setAlignmentY(LayoutConstraint.BOTTOM);
		getConstraint().setMarginX(20);
		getConstraint().setMarginY(20);
		getConstraint().setWidth(200);
	}	
	
	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	/**
	 * Call back visitor.
	 * 
	 * @param visitor visitor
	 */
	public void accept(PrintComponentVisitor visitor) {
		visitor.visit(this);
	}

	@Override
	public void calculateSize(PdfContext context) {
		// the width must be set !!
		float width = getConstraint().getWidth();

		// Calculate width in map units and round.
		float widthInUnits = width / getMap().getPpUnit();

		// Calculate ideal tic width in units
		float ticWidthInUnits = widthInUnits / getTicNumber();

		// Calculate minimal tic width in units
		int ticLog = (int) Math.floor(Math.log10(ticWidthInUnits) / 3);
		float minTicWidthInUnits = (float) Math.pow(10, ticLog * 3);

		// Find the highest nice number
		float nicestNumber = 0;
		for (float allowTickNumber : ALLOW_TICK_NUMBERS) {
			if (minTicWidthInUnits * allowTickNumber > ticWidthInUnits) {
				break;
			} else {
				nicestNumber = allowTickNumber;
			}
		}
		ticWidthInUnits = minTicWidthInUnits * nicestNumber;
		ticWidth = ticWidthInUnits * getMap().getPpUnit();
		ticHeight = ticWidth * 0.3f;
		int ticCount = getTicNumber();
{'fraction_non_alphanumeric': 0.09723275208491282, 'fraction_numerical': 0.01023502653525398, 'mean_word_length': 4.595440084835631, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2776490', 'n_tokens_mistral': 3373, 'n_tokens_neox': 3090, 'n_words': 920}