Changeset 3831 for print

Show
Ignore:
Timestamp:
07/08/11 15:57:05 (11 months ago)
Author:
sbrunner
Message:

Legends improvement r=jeichar (closes #643), fix null value introduce in [3827], merge with [3826]

Location:
print/trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • print/trunk/docs/configuration.txt

    r3750 r3831  
    515515 
    516516          - !legends 
    517   ?         maxIconWidth: 8 
     517  ?         maxIconWidth: 8  
    518518  ?         maxIconHeight: 8 
     519  ?         defaultScale: 0 
     520  ?         inline: true 
    519521  ?         classIndentation: 20 
    520522  ?         layerSpace: 5 
     
    527529  ?         fontEncoding: Cp1252 
    528530 
     531**maxIconWidth**, **maxIconHeight**, **defaultScale** with value of 0 indicates that the value will be ignore. 
     532 
     533if **defaultScale** is non null it means that the legend image will be scaled so it doesn't take the full space. 
     534 
     535Set **inline** to false mean that only tne image will be display per line.  
     536 
    529537**layerSpace** and **classSpace** is to specify the line space to add before layers and classes. 
    530538 
    531 For this to work, you need to set the **layerTree** config option on MF print widgets. 
     539For this to work, you need to set the **layerTree** config option on MF print widgets, 
     540more precisely the legends should be present in the print.pdf JSON request. 
    532541 
    533542Table configuration 
  • print/trunk/docs/protocol.txt

    r3677 r3831  
    9292            } 
    9393        ] 
     94        legends: [ 
     95            { 
     96                classes: [ 
     97                    { 
     98                        icons: [ 
     99                            'full url to the image' 
     100                        ] 
     101                        name: 'an icon name' 
     102                    } 
     103                ] 
     104                name: 'a class name' 
     105            }         
     106        ] 
    94107    } 
    95108 
  • print/trunk/src/main/java/org/mapfish/print/PDFUtils.java

    r3827 r3831  
    6868     */ 
    6969    public static Image getImage(RenderingContext context, URI uri, float w, float h) throws IOException, DocumentException { 
     70        return getImage(context, uri, w, h, 0f); 
     71    } 
     72     
     73    /** 
     74     * Gets an iText image with a cache that uses PdfTemplates to re-use the same 
     75     * bitmap content multiple times in order to reduce the file size. 
     76     */ 
     77    public static Image getImage(RenderingContext context, URI uri, float w, float h, float scale) throws IOException, DocumentException { 
    7078        //Check the image is not already used in the PDF file. 
    7179        // 
     
    8997        if (w == 0.0f) { 
    9098            if (h == 0.0f) { 
    91                 w = template.getWidth(); 
    92                 h = template.getHeight(); 
     99                scale = scale == 0f ? 1f : scale; 
     100                w = template.getWidth() * scale; 
     101                h = template.getHeight() * scale; 
    93102            } else { 
    94                 w = h / template.getHeight() * template.getWidth(); 
     103                if (scale == 0f) { 
     104                    w = h / template.getHeight() * template.getWidth(); 
     105                } 
     106                else { 
     107                    float maxh = h; 
     108                    w = template.getWidth() * scale; 
     109                    h = template.getWidth() * scale; 
     110                    float scaleh = h / maxh; 
     111                    if (scaleh > 1f) { 
     112                        w /=  scaleh; 
     113                        h /=  scaleh; 
     114                    } 
     115                } 
    95116            } 
    96117        } else { 
    97118            if (h == 0.0f) { 
    98                 h = w / template.getWidth() * template.getHeight(); 
     119                if (scale == 0f) { 
     120                    h = w / template.getWidth() * template.getHeight(); 
     121                } 
     122                else { 
     123                    float maxw = w; 
     124                    w = template.getWidth() * scale; 
     125                    h = template.getWidth() * scale; 
     126                    float scalew = w / maxw; 
     127                    if (scalew > 1f) { 
     128                        w /=  scalew; 
     129                        h /=  scalew; 
     130                    } 
     131                } 
     132            } 
     133            else { 
     134                float maxw = w; 
     135                float maxh = h; 
     136                w = template.getWidth() * scale; 
     137                h = template.getWidth() * scale; 
     138                float scalew = w / maxw; 
     139                float scaleh = h / maxh; 
     140                float maxscale = Math.max(scalew, scaleh); 
     141                if (maxscale > 1f) { 
     142                    w /=  maxscale; 
     143                    h /=  maxscale; 
     144                } 
    99145            } 
    100146        } 
     
    437483 
    438484    public static Chunk createImageChunk(RenderingContext context, double maxWidth, double maxHeight, URI url, float rotation) throws DocumentException { 
    439         final Image image = createImage(context, maxWidth, maxHeight, url, rotation); 
     485        return createImageChunk(context, maxWidth, maxHeight, 0f, url, rotation); 
     486    } 
     487    public static Chunk createImageChunk(RenderingContext context, double maxWidth, double maxHeight, float scale, URI url, float rotation) throws DocumentException { 
     488        final Image image = createImage(context, maxWidth, maxHeight, scale, url, rotation); 
    440489        return new Chunk(image, 0f, 0f, true); 
    441490    } 
    442491 
    443492    public static Image createImage(RenderingContext context, double maxWidth, double maxHeight, URI url, float rotation) throws DocumentException { 
     493        return createImage(context, maxWidth, maxHeight, 0f, url, rotation); 
     494    } 
     495    public static Image createImage(RenderingContext context, double maxWidth, double maxHeight, float scale, URI url, float rotation) throws DocumentException { 
    444496        final Image image; 
    445497        try { 
    446             image = getImage(context, url, (float) maxWidth, (float) maxHeight); 
     498            image = getImage(context, url, (float) maxWidth, (float) maxHeight, scale); 
    447499        } catch (IOException e) { 
    448500            throw new InvalidValueException("url", url.toString(), e); 
  • print/trunk/src/main/java/org/mapfish/print/config/Config.java

    r3827 r3831  
    396396        String name = null; 
    397397        if(layout != null) { 
    398             name = PDFUtils.getValueFromString(layout.getOutputFilename()); // get the string if it has ${now} or ${now DATEFORMAT} in it 
     398            name = layout.getOutputFilename(); 
     399            if (name != null) { 
     400                name = PDFUtils.getValueFromString(name); // get the string if it has ${now} or ${now DATEFORMAT} in it 
     401            } 
    399402        } 
    400403        return name == null ? outputFilename : name; 
  • print/trunk/src/main/java/org/mapfish/print/config/layout/LegendsBlock.java

    r3826 r3831  
    4646    public static final Logger LOGGER = Logger.getLogger(LegendsBlock.class); 
    4747 
    48     private double maxIconWidth = 0; 
    49     private double maxIconHeight = 8; 
     48    private double maxIconWidth = 0; // 0 mean disable 
     49    private double maxIconHeight = 8; // 0 mean disable 
     50    private float scale = 0; // 0 mean disable 
     51    private boolean inline = true; 
    5052    private double classIndentation = 20; 
    51     private double layerSpace = 5; 
    52     private double classSpace = 2; 
     53    private float layerSpace = 5; 
     54    private float classSpace = 2; 
    5355 
    5456    private String layerFont = "Helvetica"; 
     
    6971            for (int i = 0; i < legends.size(); ++i) { 
    7072                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); 
     73                createLine(context, 0.0, layer, layerPdfFont, params, table, i == 0 ? layerSpace : 0); 
    7674 
    7775                PJsonArray classes = layer.getJSONArray("classes"); 
    7876                for (int j = 0; j < classes.size(); ++j) { 
    7977                    PJsonObject clazz = classes.getJSONObject(j); 
    80                     final PdfPCell classCell = createLine(context, classIndentation, clazz, classPdfFont, params); 
    81                     classCell.setPaddingTop((float) classSpace); 
    82                     table.addCell(classCell); 
     78                    createLine(context, classIndentation, clazz, classPdfFont, params, table, classSpace); 
    8379                } 
    8480            } 
     
    8884    } 
    8985 
    90     private PdfPCell createLine(RenderingContext context, double indent, PJsonObject node, Font pdfFont, PJsonObject params) throws DocumentException { 
     86    private void createLine(RenderingContext context, double indent, PJsonObject node, Font pdfFont, PJsonObject params, 
     87            PdfPTable table, float lineSpace) throws DocumentException { 
    9188        final String name = node.getString("name"); 
    9289        final String icon = node.optString("icon"); 
    9390        final PJsonArray icons = node.optJSONArray("icons"); 
    9491 
    95         final Paragraph result = new Paragraph(); 
    96         result.setFont(pdfFont); 
     92        Paragraph result = new Paragraph(); 
    9793        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) { 
     94            try { 
     95                if (icon.indexOf("image%2Fsvg%2Bxml") != -1) { // TODO: make this cleaner 
     96                    result.add(PDFUtils.createImageChunkFromSVG(context, icon, maxIconWidth, maxIconHeight)); 
     97                } else { 
     98                    result.add(PDFUtils.createImageChunk(context, maxIconWidth, maxIconHeight, URI.create(icon), 0f)); 
     99                } 
     100                if (!inline) { 
     101                    addCell(context, indent, params, table, lineSpace, result); 
     102                    result = new Paragraph(); 
     103                    result.setFont(pdfFont); 
     104                } 
     105                else { 
     106                    result.add(" "); 
     107                } 
     108            } catch (IOException ioe) { 
    106109                LOGGER.warn("Failed to load " + icon + " with " + ioe.getMessage()); 
    107110            } catch (InvalidValueException e) { 
     
    113116                String iconItem = icons.getString(i); 
    114117                try { 
    115                     if (iconItem.indexOf("image%2Fsvg%2Bxml") != -1) { // TaODO: make this cleaner 
    116                         result.add(PDFUtils.createImageChunkFromSVG(context, iconItem, maxIconWidth, maxIconHeight)); 
     118                    if (iconItem.indexOf("image%2Fsvg%2Bxml") != -1) { // TODO: make this cleaner 
     119                        result.add(PDFUtils.createImageChunkFromSVG(context, iconItem, maxIconWidth, maxIconHeight)); 
    117120                    } else { 
    118                         result.add(PDFUtils.createImageChunk(context, maxIconWidth, maxIconHeight, URI.create(iconItem), 0.0f)); 
     121                        result.add(PDFUtils.createImageChunk(context, maxIconWidth, maxIconHeight, scale, URI.create(iconItem), 0f)); 
    119122                    } 
    120                     result.add(" "); 
     123                    if (!inline) { 
     124                        addCell(context, indent, params, table, 0, result); 
     125                        result = new Paragraph(); 
     126                        result.setFont(pdfFont); 
     127                    } 
     128                    else { 
     129                        result.add(" "); 
     130                    } 
    121131                } catch (IOException ioe) { 
    122132                    LOGGER.warn("Failed to load " + iconItem + " with " + ioe.getMessage()); 
     
    126136            } 
    127137        } 
    128  
    129138        final PdfPCell cell = new PdfPCell(result); 
    130139        cell.setBorder(PdfPCell.NO_BORDER); 
     
    132141        cell.setPaddingLeft((float) indent); 
    133142 
     143        result.setFont(pdfFont); 
    134144        result.add(name); 
    135145 
     
    137147            cell.setBackgroundColor(getBackgroundColorVal(context, params)); 
    138148        } 
    139  
    140         return cell; 
    141     } 
    142  
     149        table.addCell(cell); 
     150    } 
     151 
     152    private void addCell(RenderingContext context, double indent, PJsonObject params, PdfPTable table, 
     153            float lineSpace, final Paragraph result) { 
     154        final PdfPCell cell = new PdfPCell(result); 
     155        cell.setBorder(PdfPCell.NO_BORDER); 
     156        cell.setPadding(0f); 
     157        cell.setPaddingLeft((float) indent); 
     158 
     159        if (getBackgroundColorVal(context, params) != null) { 
     160            cell.setBackgroundColor(getBackgroundColorVal(context, params)); 
     161        } 
     162 
     163        cell.setPaddingTop(lineSpace); 
     164        table.addCell(cell); 
     165    } 
     166 
     167    public void setDefaultScale(double scale) { 
     168        this.scale = (float)scale; 
     169        if (scale < 0.0) throw new InvalidValueException("scale", scale); 
     170    } 
     171 
     172    public void setInline(String inline) { 
     173        this.inline = "true".equalsIgnoreCase(inline); 
     174        if (!inline.equalsIgnoreCase("true") || !inline.equalsIgnoreCase("false")) throw new InvalidValueException("inline", inline); 
     175    } 
     176     
    143177    public void setMaxIconWidth(double maxIconWidth) { 
    144178        this.maxIconWidth = maxIconWidth; 
     
    178212 
    179213    public void setLayerSpace(double layerSpace) { 
    180         this.layerSpace = layerSpace; 
     214        this.layerSpace = (float)layerSpace; 
    181215        if (layerSpace < 0.0) throw new InvalidValueException("layerSpace", layerSpace); 
    182216    } 
    183217 
    184218    public void setClassSpace(double classSpace) { 
    185         this.classSpace = classSpace; 
     219        this.classSpace = (float)classSpace; 
    186220        if (classSpace < 0.0) throw new InvalidValueException("classSpace", classSpace); 
    187221    }