@SuppressWarnings("unchecked")
public RenderedImage paintLegend(MapContext mapContext) {
LegendBuilder builder = new LegendBuilder();
MapRasterizingInfo mapRasterizingInfo = (MapRasterizingInfo) mapContext.getUserData().get(
LayerFactory.USERDATA_RASTERIZING_INFO);
LegendRasterizingInfo legendRasterizingInfo = mapRasterizingInfo.getLegendRasterizingInfo();
Font font = textService.getFont(legendRasterizingInfo.getFont());
builder.setTitle(legendRasterizingInfo.getTitle(), font);
if (legendRasterizingInfo.getWidth() > 0) {
builder.setSize(legendRasterizingInfo.getWidth(), legendRasterizingInfo.getHeight());
}
for (Layer layer : mapContext.layers()) {
if (layer instanceof RasterDirectLayer) {
RasterDirectLayer rasterLayer = (RasterDirectLayer) layer;
builder.addRasterLayer(rasterLayer.getTitle(), font);