All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mapfish.print.config.layout.ScalebarBlock Maven / Gradle / Ivy

/*
 * Copyright (C) 2013  Camptocamp
 *
 * This file is part of MapFish Print
 *
 * MapFish Print is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MapFish Print 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MapFish Print.  If not, see .
 */

package org.mapfish.print.config.layout;

import com.itextpdf.text.BaseColor;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import org.mapfish.print.ChunkDrawer;
import org.mapfish.print.InvalidJsonValueException;
import org.mapfish.print.InvalidValueException;
import org.mapfish.print.PDFUtils;
import org.mapfish.print.RenderingContext;
import org.mapfish.print.config.ColorWrapper;
import org.mapfish.print.scalebar.Direction;
import org.mapfish.print.scalebar.Label;
import org.mapfish.print.scalebar.ScalebarDrawer;
import org.mapfish.print.scalebar.Type;
import org.mapfish.print.utils.DistanceUnit;
import org.mapfish.print.utils.Maps;
import org.mapfish.print.utils.PJsonObject;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;

/**
 * Block for drawing a !scalebar block.
 * 

* See http://trac.mapfish.org/trac/mapfish/wiki/PrintModuleServer#Scalebarblock */ public class ScalebarBlock extends FontBlock { private int maxSize = 150; private Type type = Type.LINE; private int intervals = 3; private boolean subIntervals = false; private DistanceUnit units = null; private boolean lockUnits = false; private Integer barSize = null; private Direction barDirection = Direction.UP; private Direction textDirection = Direction.UP; private Integer labelDistance = null; private String color = "black"; /** * The background color of the odd intervals (only for the scalebar of type "bar") */ private String barBgColor = null; private Double lineWidth = null; private String name = null; public void render(PJsonObject params, PdfElement target, RenderingContext context) throws DocumentException { final PJsonObject globalParams = context.getGlobalParams(); final DistanceUnit mapUnits = DistanceUnit.fromString(globalParams.getString("units")); if (mapUnits == null) { throw new InvalidJsonValueException(globalParams, "units", globalParams.getString("units")); } DistanceUnit scaleUnit = (units != null ? units : mapUnits); final double scale = context.getLayout().getMainPage().getMap(name).createTransformer(context, params).getScale(); final double maxWidthIntervaleDistance = DistanceUnit.PT.convertTo(maxSize, scaleUnit) * scale / intervals; final double intervalDistance = getNearestNiceValue(maxWidthIntervaleDistance, scaleUnit); final Font pdfFont = getPdfFont(); tryLayout(context, target, pdfFont, scaleUnit, scale, intervalDistance, 0); } /** * Try recursively to find the correct layout. */ private void tryLayout(RenderingContext context, PdfElement target, Font pdfFont, DistanceUnit scaleUnit, double scale, double intervalDistance, int tryNumber) throws DocumentException { if (tryNumber > 3) { // no inspection ThrowableInstanceNeverThrown context.addError(new InvalidValueException("maxSize too small", maxSize)); return; } DistanceUnit intervalUnit = bestUnit(scaleUnit, intervalDistance); final float intervalPaperWidth = (float) scaleUnit.convertTo(intervalDistance / scale, DistanceUnit.PT); //compute the label positions final List

* Creates the drawer and schedule it for drawing when the position of the block is known. */ private void doLayout(RenderingContext context, PdfElement target, Font pdfFont, List





© 2015 - 2025 Weber Informatics LLC | Privacy Policy