📄 defaultrasterlegendproducer.java
字号:
this.legendGraphic = mergeLegends(legendsStack, applicableRules, request);
}
/**
* Recieves a list of <code>BufferedImages</code> and produces a new one
* which holds all the images in <code>imageStack</code> one above the
* other.
*
* @param imageStack the list of BufferedImages, one for each applicable
* Rule
* @param rules The applicable rules, one for each image in the stack
* @param request The request.
*
* @return the stack image with all the images on the argument list.
*
* @throws IllegalArgumentException if the list is empty
*/
private static BufferedImage mergeLegends(List imageStack, Rule[] rules,
GetLegendGraphicRequest req) {
Font labelFont = getLabelFont(req);
boolean useAA = false;
if (req.getLegendOptions().get("fontAntiAliasing") instanceof String) {
String aaVal = (String)req.getLegendOptions().get("fontAntiAliasing");
if (aaVal.equalsIgnoreCase("on") || aaVal.equalsIgnoreCase("true") ||
aaVal.equalsIgnoreCase("yes") || aaVal.equalsIgnoreCase("1")) {
useAA = true;
}
}
boolean forceLabelsOn = false;
boolean forceLabelsOff = false;
if (req.getLegendOptions().get("forceLabels") instanceof String) {
String forceLabelsOpt = (String)req.getLegendOptions().get("forceLabels");
if (forceLabelsOpt.equalsIgnoreCase("on")) {
forceLabelsOn = true;
} else if (forceLabelsOpt.equalsIgnoreCase("off")) {
forceLabelsOff = true;
}
}
if (imageStack.size() == 0) {
throw new IllegalArgumentException("No legend graphics passed");
}
final BufferedImage finalLegend;
if (imageStack.size() == 1 && !forceLabelsOn) {
finalLegend = (BufferedImage) imageStack.get(0);
} else {
final int imgCount = imageStack.size();
final String[] labels = new String[imgCount];
BufferedImage img = ((BufferedImage) imageStack.get(0));
int totalHeight = 0;
int totalWidth = 0;
int[] rowHeights = new int[imgCount];
for (int i = 0; i < imgCount; i++) {
img = (BufferedImage) imageStack.get(i);
if (forceLabelsOff) {
totalWidth = (int) Math.ceil(Math.max(img.getWidth(), totalWidth));
rowHeights[i] = img.getHeight();
totalHeight += img.getHeight();
} else {
Rule rule = rules[i];
//What's the label on this rule? We prefer to use
//the 'title' if it's available, but fall-back to 'name'
labels[i] = rule.getTitle();
if (labels[i] == null) labels[i] = rule.getName();
if (labels[i] == null) labels[i] = "";
Graphics2D g = img.createGraphics();
g.setFont(labelFont);
if (useAA) {
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
} else {
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
}
if(labels[i] != null && labels[i].length() > 0) {
final BufferedImage renderedLabel = renderLabel(labels[i], g, req);
final Rectangle2D bounds = new Rectangle2D.Double(0, 0, renderedLabel.getWidth(),
renderedLabel.getHeight());
totalWidth = (int) Math.ceil(Math.max(img.getWidth() + bounds.getWidth(), totalWidth));
rowHeights[i] = (int) Math.ceil(Math.max(img.getHeight(), bounds.getHeight()));
} else {
totalWidth = (int) Math.ceil(Math.max(img.getWidth(), totalWidth));
rowHeights[i] = (int) Math.ceil(img.getHeight());
}
totalHeight += rowHeights[i];
}
}
//buffer the width a bit
totalWidth += 2;
final boolean transparent = req.isTransparent();
final Color backgroundColor = getBackgroundColor(req);
final Map hintsMap = new HashMap();
//create the final image
finalLegend = ImageUtils.createImage(totalWidth, totalHeight, (IndexColorModel)null, transparent);
Graphics2D finalGraphics = ImageUtils.prepareTransparency(transparent, backgroundColor, finalLegend, hintsMap);
int topOfRow = 0;
for (int i = 0; i < imgCount; i++) {
img = (BufferedImage) imageStack.get(i);
//draw the image
int y = topOfRow;
if (img.getHeight() < rowHeights[i]) {
//move the image to the center of the row
y += (int) ((rowHeights[i] - img.getHeight()) / 2d);
}
finalGraphics.drawImage(img, 0, y, imgObs);
if (forceLabelsOff) {
topOfRow += rowHeights[i];
continue;
}
finalGraphics.setFont(labelFont);
if (useAA) {
finalGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
} else {
finalGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
}
//draw the label
if (labels[i] != null && labels[i].length() > 0) {
//first create the actual overall label image.
final BufferedImage renderedLabel = renderLabel(labels[i], finalGraphics, req);
y = topOfRow;
if (renderedLabel.getHeight() < rowHeights[i]) {
y += (int) ((rowHeights[i] - renderedLabel.getHeight()) / 2d);
}
finalGraphics.drawImage(renderedLabel, img.getWidth(), y, imgObs);
}
topOfRow += rowHeights[i];
}
}
return finalLegend;
}
private static Font getLabelFont(GetLegendGraphicRequest req) {
String legendFontName = "Sans-Serif";
String legendFontFamily = "plain";
int legendFontSize = 12;
Map legendOptions = req.getLegendOptions();
if (legendOptions.get("fontName") != null) {
legendFontName = (String) legendOptions.get("fontName");
}
if (legendOptions.get("fontStyle") != null) {
legendFontFamily = (String)legendOptions.get("fontStyle");
}
if (legendOptions.get("fontSize") != null) {
try {
legendFontSize = Integer.parseInt((String)legendOptions.get("fontSize"));
} catch (Exception e) {
LOGGER.warning("Error trying to interpret legendOption 'fontSize': " + legendOptions.get("fontSize"));
}
}
Font legendFont;
if (legendFontFamily.equalsIgnoreCase("italic")) {
legendFont = new Font(legendFontName, Font.ITALIC, legendFontSize);
} else if (legendFontFamily.equalsIgnoreCase("bold")) {
legendFont = new Font(legendFontName, Font.BOLD, legendFontSize);
} else {
legendFont = new Font(legendFontName, Font.PLAIN, legendFontSize);
}
return legendFont;
}
private static Color getLabelFontColor(GetLegendGraphicRequest req) {
Map legendOptions = req.getLegendOptions();
String color = (String) legendOptions.get("fontColor");
if ( color == null ) {
//return the default
return FONT_COLOR;
}
try {
return color(color);
}
catch(NumberFormatException e) {
LOGGER.warning("Could not decode label color: " + color + ", default to " + FONT_COLOR.toString() );
return FONT_COLOR;
}
}
/**
* Returns the image background color for the given
* {@link GetLegendGraphicRequest}.
*
* @param req
* @return the Color for the hexadecimal value passed as the
* <code>BGCOLOR</code>
* {@link GetLegendGraphicRequest#getLegendOptions() legend option},
* or the default background color if no bgcolor were passed.
*/
private static Color getBackgroundColor( GetLegendGraphicRequest req ) {
Map legendOptions = req.getLegendOptions();
String color = (String) legendOptions.get("bgColor");
if ( color == null ) {
//return the default
return BG_COLOR;
}
try {
return color(color);
}
catch( NumberFormatException e ) {
LOGGER.warning("Could not decode background color: " + color + ", default to " + BG_COLOR.toString() );
return BG_COLOR;
}
}
private static Color color(String hex) throws NumberFormatException {
if (!hex.startsWith("#")) {
hex = "#" + hex;
}
return Color.decode(hex);
}
/**
* Return a {@link BufferedImage} representing this label.
* The characters '\n' '\r' and '\f' are interpreted as linebreaks,
* as is the characater combination "\n" (as opposed to the actual '\n' character).
* This allows people to force line breaks in their labels by
* including the character "\" followed by "n" in their
* label.
*
* @param label - the label to render
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -