| 74 | | Renderer renderer = new Renderer(params, context); |
| 75 | | renderer.render(target); |
| 76 | | } |
| 77 | | |
| 78 | | /** |
| 79 | | * A renderer to render the legend block |
| 80 | | * @author Stéphane Brunner |
| 81 | | */ |
| 82 | | private class Renderer { |
| 83 | | private PJsonObject params; |
| 84 | | private RenderingContext context; |
| 85 | | |
| 86 | | // all the pdf columns |
| 87 | | private ArrayList<PdfPTable> columns = new ArrayList<PdfPTable>(); |
| 88 | | // all the columns width |
| 89 | | private ArrayList<Float> columnsWidth = new ArrayList<Float>(); |
| 90 | | // the current cell width |
| 91 | | private float cellWidth = 0; |
| 92 | | // the current column |
| 93 | | private PdfPTable column = new PdfPTable(1); |
| 94 | | // the curent title |
| 95 | | private PdfPCell title; |
| 96 | | // the current cell height |
| 97 | | private double currentCellHeight = 0; |
| 98 | | // the current column height |
| 99 | | private double currentColumnHeight = 0; |
| 100 | | |
| 101 | | /** |
| 102 | | * Construct |
| 103 | | * @param params the params |
| 104 | | * @param context the context |
| 105 | | */ |
| 106 | | public Renderer(PJsonObject params, RenderingContext context) { |
| 107 | | column.setWidthPercentage(100f); |
| 108 | | columns.add(column); |
| 109 | | currentCellHeight = 0; |
| 110 | | columnsWidth.add(0f); |
| 111 | | this.params = params; |
| 112 | | this.context = context; |
| 113 | | } |
| 114 | | |
| 115 | | /** |
| 116 | | * Render |
| 117 | | * @param target the target element |
| 118 | | * @throws DocumentException |
| 119 | | */ |
| 120 | | public void render(PdfElement target) throws DocumentException { |
| 121 | | |
| 122 | | Font layerPdfFont = getLayerPdfFont(); |
| 123 | | Font classPdfFont = getClassPdfFont(); |
| 124 | | |
| 125 | | // create the legend |
| 126 | | PJsonArray legends = context.getGlobalParams().optJSONArray("legends"); |
| 127 | | if (legends != null && legends.size() > 0) { |
| 128 | | for (int i = 0; i < legends.size(); ++i) { |
| 129 | | PJsonObject layer = legends.getJSONObject(i); |
| 130 | | createLine(0.0, layer, layerPdfFont, i == 0 ? 0 : layerSpace, true); |
| 131 | | |
| 132 | | PJsonArray classes = layer.getJSONArray("classes"); |
| 133 | | for (int j = 0; j < classes.size(); ++j) { |
| 134 | | PJsonObject clazz = classes.getJSONObject(j); |
| 135 | | createLine(classIndentation, clazz, classPdfFont, classSpace, false); |
| 136 | | } |
| | 61 | PdfPTable table = new PdfPTable(1); |
| | 62 | table.setWidthPercentage(100f); |
| | 63 | |
| | 64 | Font layerPdfFont = getLayerPdfFont(); |
| | 65 | Font classPdfFont = getClassPdfFont(); |
| | 66 | |
| | 67 | PJsonArray legends = context.getGlobalParams().optJSONArray("legends"); |
| | 68 | if (legends != null && legends.size() > 0) { |
| | 69 | for (int i = 0; i < legends.size(); ++i) { |
| | 70 | PJsonObject layer = legends.getJSONObject(i); |
| | 71 | final PdfPCell cell = createLine(context, 0.0, layer, layerPdfFont, params); |
| | 72 | if (i > 0) { |
| | 73 | cell.setPaddingTop((float) layerSpace); |
| | 74 | } |
| | 75 | table.addCell(cell); |
| | 76 | |
| | 77 | PJsonArray classes = layer.getJSONArray("classes"); |
| | 78 | for (int j = 0; j < classes.size(); ++j) { |
| | 79 | PJsonObject clazz = classes.getJSONObject(j); |
| | 80 | final PdfPCell classCell = createLine(context, classIndentation, clazz, classPdfFont, params); |
| | 81 | classCell.setPaddingTop((float) classSpace); |
| | 82 | table.addCell(classCell); |
| 139 | | |
| 140 | | // complete the width of the last column |
| 141 | | { |
| 142 | | int index = columnsWidth.size() - 1; |
| 143 | | columnsWidth.set(index, Math.max(columnsWidth.get(index), cellWidth)); |
| 144 | | } |
| 145 | | if (title != null) { |
| 146 | | column.addCell(title); |
| 147 | | } |
| 148 | | |
| 149 | | // calculate the fullWidth (sum of width of visible column with margin) |
| 150 | | int len = columns.size(); |
| 151 | | float fullWidth = 0; |
| 152 | | for (int i = 0 ; i < len ; i++) { |
| 153 | | float width = columnsWidth.get(i); |
| 154 | | if (fullWidth + width < maxWidth) { |
| 155 | | fullWidth += width + columnMargin; |
| 156 | | } |
| 157 | | else { |
| 158 | | len = i + 1; |
| 159 | | } |
| 160 | | } |
| 161 | | |
| 162 | | // create the column with array for the table |
| 163 | | float[] pdfWidths = new float[len + 1]; |
| 164 | | for (int i = 0 ; i < len ; i++) { |
| 165 | | pdfWidths[i] = columnsWidth.get(i); |
| 166 | | } |
| 167 | | // for empty column |
| 168 | | pdfWidths[len] = Math.max(0f, (float)maxWidth - fullWidth); |
| 169 | | |
| 170 | | // table used for column |
| 171 | | PdfPTable table = new PdfPTable(pdfWidths); |
| 172 | | table.setWidthPercentage(100f); |
| 173 | | table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); |
| 174 | | table.getDefaultCell().setPadding(0f); |
| 175 | | table.setSpacingAfter((float) spacingAfter); |
| 176 | | |
| 177 | | // add columns |
| 178 | | for (int i = 0 ; i < len ; i++) { |
| 179 | | table.addCell(columns.get(i)); |
| 180 | | } |
| 181 | | // create empty column |
| 182 | | table.addCell(""); |
| 183 | | |
| 184 | | // add to result |
| 185 | | target.add(table); |
| 186 | | } |
| 187 | | |
| 188 | | /** |
| 189 | | * Display a legend or class block (name and icon(s) ). |
| 190 | | * In not inline mode it's a line. |
| 191 | | * @param indent left indentation |
| 192 | | * @param node the json node |
| 193 | | * @param pdfFont the font used for the title |
| 194 | | * @param lineSpace the top indent for the icons |
| 195 | | * @param escapeOrphanTitle don't have orphan title |
| 196 | | * @throws DocumentException |
| 197 | | */ |
| 198 | | private void createLine(double indent, PJsonObject node, Font pdfFont, |
| 199 | | float lineSpace, boolean escapeOrphanTitle) throws DocumentException { |
| 200 | | final String name = node.getString("name"); |
| 201 | | final String icon = node.optString("icon"); |
| 202 | | final PJsonArray icons = node.optJSONArray("icons"); |
| 203 | | |
| 204 | | Paragraph result = new Paragraph(); |
| 205 | | if (icon != null) { |
| 206 | | result = createIcon(indent, lineSpace, icon, result); |
| 207 | | } |
| 208 | | if (icons != null) { |
| 209 | | for (int i = 0; i < icons.size(); ++i) { |
| 210 | | String iconItem = icons.getString(i); |
| 211 | | result = createIcon(indent, i == 0 ? lineSpace : 0, |
| 212 | | iconItem, result); |
| 213 | | } |
| 214 | | } |
| 215 | | |
| 216 | | if (title != null) { |
| 217 | | column.addCell(title); |
| 218 | | } |
| 219 | | title = new PdfPCell(result); |
| 220 | | title.setBorder(PdfPCell.NO_BORDER); |
| 221 | | title.setPadding(0f); |
| 222 | | title.setPaddingLeft((float)indent); |
| 223 | | if (inline) { |
| 224 | | title.setPaddingTop(lineSpace); |
| 225 | | } |
| 226 | | |
| 227 | | result.setFont(pdfFont); |
| 228 | | result.add(name); |
| 229 | | if (name.trim().length() > 0) { |
| 230 | | float width = pdfFont.getBaseFont().getWidthPoint(name, pdfFont.getSize()); |
| 231 | | if (escapeOrphanTitle) { |
| 232 | | currentCellHeight += pdfFont.getSize(); |
| 233 | | cellWidth = Math.max(cellWidth, width); |
| 234 | | } |
| 235 | | else { |
| 236 | | currentColumnHeight += pdfFont.getSize(); |
| 237 | | int index = columnsWidth.size() - 1; |
| 238 | | columnsWidth.set(index, Math.max(columnsWidth.get(index), width)); |
| 239 | | } |
| 240 | | } |
| 241 | | |
| 242 | | if (getBackgroundColorVal(context, params) != null) { |
| 243 | | title.setBackgroundColor(getBackgroundColorVal(context, params)); |
| 244 | | } |
| 245 | | if (!escapeOrphanTitle) { |
| 246 | | column.addCell(title); |
| 247 | | title = null; |
| 248 | | } |
| 249 | | } |
| 250 | | |
| 251 | | /** |
| 252 | | * Creates a legend icon |
| 253 | | * @param indent left indentation |
| 254 | | * @param lineSpace the top indent for the icons |
| 255 | | * @param icon the icon to display |
| 256 | | * @param result the element to add to |
| 257 | | * @return the result for the next elements |
| 258 | | * @throws DocumentException |
| 259 | | */ |
| 260 | | private Paragraph createIcon(double indent, float lineSpace, |
| 261 | | final String icon, Paragraph result) throws DocumentException { |
| 262 | | try { |
| 263 | | Chunk iconChunk = null; |
| 264 | | if (icon.indexOf("image%2Fsvg%2Bxml") != -1) { // TODO: make this cleaner |
| 265 | | iconChunk = PDFUtils.createImageChunkFromSVG(context, icon, maxIconWidth, maxIconHeight); |
| 266 | | } else { |
| 267 | | iconChunk = PDFUtils.createImageChunk(context, maxIconWidth, maxIconHeight, scale, |
| 268 | | URI.create(icon), 0f); |
| 269 | | } |
| 270 | | result.add(iconChunk); |
| 271 | | if (!inline) { |
| 272 | | currentCellHeight += iconChunk.getImage().getPlainHeight() + lineSpace; |
| 273 | | cellWidth = Math.max(cellWidth, iconChunk.getImage().getPlainWidth()); |
| 274 | | addCell(indent, lineSpace, result); |
| 275 | | result = new Paragraph(); |
| 276 | | } |
| 277 | | else { |
| 278 | | result.add(" "); |
| 279 | | } |
| 280 | | } catch (IOException ioe) { |
| | 85 | } |
| | 86 | table.setSpacingAfter((float) spacingAfter); |
| | 87 | target.add(table); |
| | 88 | } |
| | 89 | |
| | 90 | private PdfPCell createLine(RenderingContext context, double indent, PJsonObject node, Font pdfFont, PJsonObject params) throws DocumentException { |
| | 91 | final String name = node.getString("name"); |
| | 92 | final String icon = node.optString("icon"); |
| | 93 | final PJsonArray icons = node.optJSONArray("icons"); |
| | 94 | |
| | 95 | final Paragraph result = new Paragraph(); |
| | 96 | result.setFont(pdfFont); |
| | 97 | if (icon != null) { |
| | 98 | try { |
| | 99 | if (icon.indexOf("image%2Fsvg%2Bxml") != -1) { // TaODO: make this cleaner |
| | 100 | result.add(PDFUtils.createImageChunkFromSVG(context, icon, maxIconWidth, maxIconHeight)); |
| | 101 | } else { |
| | 102 | result.add(PDFUtils.createImageChunk(context, maxIconWidth, maxIconHeight, URI.create(icon), 0.0f)); |
| | 103 | } |
| | 104 | result.add(" "); |
| | 105 | } catch (IOException ioe) { |
| 285 | | return result; |
| 286 | | } |
| 287 | | |
| 288 | | /** |
| 289 | | * Add an icon in the column as a cell. |
| 290 | | * @param indent left indentation |
| 291 | | * @param lineSpace the top indent for the icons |
| 292 | | * @param icon the icon element to add |
| 293 | | */ |
| 294 | | private void addCell(double indent, float lineSpace, final Paragraph icon) { |
| 295 | | final PdfPCell cell = new PdfPCell(icon); |
| 296 | | cell.setBorder(PdfPCell.NO_BORDER); |
| 297 | | cell.setPadding(0f); |
| 298 | | cell.setPaddingLeft((float) indent); |
| 299 | | |
| 300 | | if (getBackgroundColorVal(context, params) != null) { |
| 301 | | cell.setBackgroundColor(getBackgroundColorVal(context, params)); |
| | 110 | } |
| | 111 | if (icons != null) { |
| | 112 | for (int i = 0; i < icons.size(); ++i) { |
| | 113 | String iconItem = icons.getString(i); |
| | 114 | try { |
| | 115 | if (iconItem.indexOf("image%2Fsvg%2Bxml") != -1) { // TaODO: make this cleaner |
| | 116 | result.add(PDFUtils.createImageChunkFromSVG(context, iconItem, maxIconWidth, maxIconHeight)); |
| | 117 | } else { |
| | 118 | result.add(PDFUtils.createImageChunk(context, maxIconWidth, maxIconHeight, URI.create(iconItem), 0.0f)); |
| | 119 | } |
| | 120 | result.add(" "); |
| | 121 | } catch (IOException ioe) { |
| | 122 | LOGGER.warn("Failed to load " + iconItem + " with " + ioe.getMessage()); |
| | 123 | } catch (InvalidValueException e) { |
| | 124 | LOGGER.warn("Failed to create image chunk: " + e.getMessage()); |
| | 125 | } |
| 303 | | |
| 304 | | cell.setPaddingTop(lineSpace); |
| 305 | | currentCellHeight += lineSpace; |
| 306 | | if (!inline && maxHeight != 0 && (currentColumnHeight + currentCellHeight > maxHeight)) { |
| 307 | | column = new PdfPTable(1); |
| 308 | | column.setWidthPercentage(100f); |
| 309 | | columns.add(column); |
| 310 | | currentColumnHeight = 0; |
| 311 | | columnsWidth.add(cellWidth); |
| 312 | | } |
| 313 | | else { |
| 314 | | int index = columnsWidth.size() - 1; |
| 315 | | columnsWidth.set(index, Math.max(columnsWidth.get(index), cellWidth)); |
| 316 | | } |
| 317 | | cellWidth = 0; |
| 318 | | currentColumnHeight += currentCellHeight; |
| 319 | | currentCellHeight = 0; |
| 320 | | if (title != null) { |
| 321 | | column.addCell(title); |
| 322 | | title = null; |
| 323 | | } |
| 324 | | column.addCell(cell); |
| 325 | | } |
| 326 | | } |
| 327 | | |
| 328 | | public void setMaxWidth(double maxWidth) { |
| 329 | | this.maxWidth = maxWidth; |
| 330 | | if (maxWidth < 0.0) throw new InvalidValueException("maxWidth", maxWidth); |
| 331 | | } |
| 332 | | |
| 333 | | public void setMaxHeight(double maxHeight) { |
| 334 | | this.maxHeight = maxHeight; |
| 335 | | if (maxHeight < 0.0) throw new InvalidValueException("maxHeight", maxHeight); |
| 336 | | } |
| 337 | | |
| 338 | | public void setDefaultScale(double scale) { |
| 339 | | this.scale = (float)scale; |
| 340 | | if (scale < 0.0) throw new InvalidValueException("scale", scale); |
| 341 | | } |
| 342 | | |
| 343 | | public void setInline(String inline) { |
| 344 | | this.inline = "true".equalsIgnoreCase(inline); |
| 345 | | if (!(inline.equalsIgnoreCase("true") || inline.equalsIgnoreCase("false"))) throw new InvalidValueException("inline", inline); |
| 346 | | } |
| 347 | | |
| | 127 | } |
| | 128 | |
| | 129 | final PdfPCell cell = new PdfPCell(result); |
| | 130 | cell.setBorder(PdfPCell.NO_BORDER); |
| | 131 | cell.setPadding(0f); |
| | 132 | cell.setPaddingLeft((float) indent); |
| | 133 | |
| | 134 | result.add(name); |
| | 135 | |
| | 136 | if (getBackgroundColorVal(context, params) != null) { |
| | 137 | cell.setBackgroundColor(getBackgroundColorVal(context, params)); |
| | 138 | } |
| | 139 | |
| | 140 | return cell; |
| | 141 | } |
| | 142 | |