Ticket #561: printLabel.patch
| File printLabel.patch, 14.1 kB (added by cmoullet, 2 years ago) |
|---|
-
print-lib/src/main/java/org/mapfish/print/PDFUtils.java
20 20 package org.mapfish.print; 21 21 22 22 import com.lowagie.text.*; 23 import com.lowagie.text.pdf.BaseFont; 23 24 import com.lowagie.text.pdf.PdfContentByte; 24 25 import com.lowagie.text.pdf.PdfPCell; 25 26 import com.lowagie.text.pdf.PdfPTable; … … 383 384 } 384 385 return image; 385 386 } 387 388 public static BaseFont getBaseFont(String fontFamily, String fontSize, 389 String fontWeight) { 390 int myFontValue; 391 float myFontSize; 392 int myFontWeight; 393 if (fontFamily.toUpperCase().contains("COURIER")) { 394 myFontValue = Font.COURIER; 395 } else if (fontFamily.toUpperCase().contains("HELVETICA")) { 396 myFontValue = Font.HELVETICA; 397 } else if (fontFamily.toUpperCase().contains("ROMAN")) { 398 myFontValue = Font.TIMES_ROMAN; 399 } else { 400 myFontValue = Font.HELVETICA; 401 } 402 myFontSize = (float) Double.parseDouble(fontSize.toLowerCase() 403 .replaceAll("px", "")); 404 if (fontWeight.toUpperCase().contains("NORMAL")) { 405 myFontWeight = Font.NORMAL; 406 } else if (fontWeight.toUpperCase().contains("BOLD")) { 407 myFontWeight = Font.BOLD; 408 } else if (fontWeight.toUpperCase().contains("ITALIC")) { 409 myFontWeight = Font.ITALIC; 410 } else { 411 myFontWeight = Font.NORMAL; 412 } 413 Font pdfFont = new Font(myFontValue, myFontSize, myFontWeight); 414 BaseFont bf = pdfFont.getCalculatedBaseFont(false); 415 return bf; 416 } 417 418 public static int getHorizontalAlignment(String labelAlign) { 419 /* Valid values for horizontal alignment: "l"=left, "c"=center, "r"=right. */ 420 int myAlignment = PdfContentByte.ALIGN_LEFT; 421 if (labelAlign.toUpperCase().contains("L")) { 422 myAlignment = PdfContentByte.ALIGN_LEFT; 423 } 424 if (labelAlign.toUpperCase().contains("C")) { 425 myAlignment = PdfContentByte.ALIGN_CENTER; 426 } 427 if (labelAlign.toUpperCase().contains("R")) { 428 myAlignment = PdfContentByte.ALIGN_RIGHT; 429 } 430 return myAlignment; 431 } 432 433 public static float getVerticalOffset(String labelAlign, float fontHeight) { 434 /* Valid values for vertical alignment: "t"=top, "m"=middle, "b"=bottom. */ 435 float myOffset = (float) 0.0; 436 if (labelAlign.toUpperCase().contains("T")) { 437 myOffset = fontHeight; 438 } 439 if (labelAlign.toUpperCase().contains("M")) { 440 myOffset = fontHeight/2; 441 } 442 if (labelAlign.toUpperCase().contains("B")) { 443 myOffset = (float) 0.0; 444 } 445 return myOffset; 446 } 386 447 } -
print-lib/src/main/java/org/mapfish/print/map/renderers/vector/PointRenderer.java
21 21 22 22 import com.vividsolutions.jts.geom.Point; 23 23 import com.vividsolutions.jts.geom.Coordinate; 24 import com.lowagie.text.pdf.BaseFont; 24 25 import com.lowagie.text.pdf.PdfContentByte; 25 26 import com.lowagie.text.pdf.PdfGState; 26 27 import com.lowagie.text.BadElementException; … … 29 30 import org.mapfish.print.RenderingContext; 30 31 import org.mapfish.print.PDFUtils; 31 32 import org.mapfish.print.InvalidValueException; 33 import org.mapfish.print.config.ColorWrapper; 32 34 import org.mapfish.print.utils.PJsonObject; 33 35 34 36 import java.net.URI; … … 89 91 float height = style.optFloat("graphicHeight", pointRadius * 2.0f); 90 92 float offsetX = style.optFloat("graphicXOffset", -width / 2.0f); 91 93 float offsetY = style.optFloat("graphicYOffset", -height / 2.0f); 94 // See Feature/Vector.js for more information about labels 95 String label = style.optString("label"); 96 String labelAlign = style.optString("labelAlign", "lb"); 97 /* 98 * Valid values for horizontal alignment: "l"=left, "c"=center, "r"=right. 99 * Valid values for vertical alignment: "t"=top, "m"=middle, "b"=bottom. 100 */ 101 float labelXOffset = style.optFloat("labelXOffset", (float) 0.0); 102 float labelYOffset = style.optFloat("labelYOffset", (float) 0.0); 103 String fontColor = style.optString("fontColor", "#000000"); 104 /* Supported itext fonts: COURIER, HELVETICA, TIMES_ROMAN */ 105 String fontFamily = style.optString("fontFamily", "HELVETICA"); 106 String fontSize = style.optString("fontSize", "12"); 107 String fontWeight = style.optString("fontWeight", "normal"); 92 108 93 109 if (style.optString("externalGraphic") != null) { 94 110 float opacity = style.optFloat("graphicOpacity", style.optFloat("fillOpacity", 1.0f)); … … 123 139 dc.closePath(); 124 140 dc.fillStroke(); 125 141 126 } else { 142 } else if (label != null && label.length() > 0) { 143 BaseFont bf = PDFUtils.getBaseFont(fontFamily, fontSize, fontWeight); 144 float fontHeight = (float) Double.parseDouble(fontSize.toLowerCase().replaceAll("px", "")) * f; 145 dc.setFontAndSize(bf, fontHeight); 146 dc.setColorFill(ColorWrapper.convertColor(fontColor)); 147 state.setFillOpacity((float) 1.0); 148 dc.setGState(state); 149 dc.beginText(); 150 dc.setTextMatrix((float) coordinate.x + labelXOffset * f, (float) coordinate.y + labelYOffset * f); 151 dc.setGState(state); 152 dc.showTextAligned(PDFUtils.getHorizontalAlignment(labelAlign), label, (float) coordinate.x + labelXOffset * f, (float) coordinate.y + labelYOffset * f - PDFUtils.getVerticalOffset(labelAlign, fontHeight), 0); 153 dc.endText(); 154 } else { 127 155 PolygonRenderer.applyStyle(context, dc, style, state); 128 156 dc.setGState(state); 129 157 -
print-standalone/samples/specVector.json
1 {"pages":[ 2 { 3 "center":[80999.999999998,125750], 4 "langfr":"true", 5 "dataOwner":"ch.swisstopo", 6 "scale":500000, 7 "comment": "This is the first page selected by the user.", 8 mapTitle: "First map" 9 } 10 ],"dpi":"127","units":"m","srs":"EPSG:2169","layers":[ 11 { 12 "opacity":1, 13 "customParams":{}, 14 "type":"Vector", 15 "styles":{ 16 "1":{"label":"","labelSelect":true,"pointRadius":6,"fillColor":"#ee9900","fillOpacity":0.4,"hoverFillColor":"white","hoverFillOpacity":0.8,"strokeColor":"#ee9900","strokeOpacity":1,"strokeWidth":1,"strokeLinecap":"round","strokeDashstyle":"solid","hoverStrokeColor":"red","hoverStrokeOpacity":1,"hoverStrokeWidth":0.2,"hoverPointRadius":1,"hoverPointUnit":"%","pointerEvents":"visiblePainted","cursor":"inherit"}, 17 "2":{"label":"Vérité","labelSelect":true,fontFamily:"Courier",fontColor:"#FF0000",fontSize:28,fontWeight:"Bold",labelAlign:"rt",labelXOffset:-25,labelYOffset:-25,"pointRadius":0,"fillColor":"#ee9900","fillOpacity":0.4,"hoverFillColor":"white","hoverFillOpacity":0.8,"strokeColor":"#ee9900","strokeOpacity":1,"strokeWidth":1,"strokeLinecap":"round","strokeDashstyle":"solid","hoverStrokeColor":"red","hoverStrokeOpacity":1,"hoverStrokeWidth":0.2,"hoverPointRadius":1,"hoverPointUnit":"%","pointerEvents":"visiblePainted","cursor":"inherit"}, 18 "3":{"label":"","labelSelect":true,"pointRadius":6,"fillColor":"#ee9900","fillOpacity":0.4,"hoverFillColor":"white","hoverFillOpacity":0.8,"strokeColor":"#ee9900","strokeOpacity":1,"strokeWidth":1,"strokeLinecap":"round","strokeDashstyle":"solid","hoverStrokeColor":"red","hoverStrokeOpacity":1,"hoverStrokeWidth":0.2,"hoverPointRadius":1,"hoverPointUnit":"%","pointerEvents":"visiblePainted","cursor":"inherit"}, 19 "4":{"label":"","labelSelect":true,"pointRadius":6,"fillColor":"#ee9900","fillOpacity":0.4,"hoverFillColor":"white","hoverFillOpacity":0.8,"strokeColor":"#ee9900","strokeOpacity":1,"strokeWidth":1,"strokeLinecap":"round","strokeDashstyle":"solid","hoverStrokeColor":"red","hoverStrokeOpacity":1,"hoverStrokeWidth":0.2,"hoverPointRadius":1,"hoverPointUnit":"%","pointerEvents":"visiblePainted","cursor":"inherit"}}, 20 "styleProperty":"_style", 21 "geoJson":{"type":"FeatureCollection","features":[ 22 { 23 "type":"Feature", 24 "id":"OpenLayers.Feature.Vector_202", 25 "properties":{"_style":1,"name":"","description":""}, 26 "geometry":{"type":"LineString","coordinates":[ 27 [77500,133125], 28 [68500,122375], 29 [74250,108625], 30 [97250,135125], 31 [89500,152625], 32 [89250,152625] 33 ]} 34 }, 35 { 36 "type":"Feature", 37 "id":"OpenLayers.Feature.Vector_226", 38 "properties":{"_style":2,"name":"Hello","description":""}, 39 "geometry":{"type":"Point","coordinates":[77250,101875]} 40 }, 41 { 42 "type":"Feature", 43 "id":"OpenLayers.Feature.Vector_235", 44 "properties":{"_style":3,"name":"","description":""}, 45 "geometry":{"type":"Point","coordinates":[102750,116875]} 46 }, 47 { 48 "type":"Feature", 49 "id":"OpenLayers.Feature.Vector_277", 50 "properties":{"_style":4,"name":"","description":""}, 51 "geometry":{"type":"Polygon","coordinates":[ 52 [ 53 [72000,150375], 54 [61250,141625], 55 [65000,134625], 56 [84500,146875], 57 [83250,169125], 58 [67250,171625], 59 [63500,157625], 60 [62500,157125], 61 [72000,150375] 62 ] 63 ]} 64 } 65 ]}, 66 "name":"Cosmetic" 67 }, 68 { 69 "opacity":1, 70 "customParams":{}, 71 "type":"Vector", 72 "styles":{}, 73 "styleProperty":"_style", 74 "geoJson":{"type":"FeatureCollection","features":[]}, 75 "name":"OpenLayers.Handler.Polygon" 76 } 77 ],"overviewLayers":[ 78 { 79 "type":"Image", 80 "baseURL":"http://geoportail-luxembourg.demo-camptocamp.com//gfx/keymap.png", 81 "opacity":1, 82 "extent":[40000,44000,114000,149000], 83 "pixelSize":[130,150], 84 "name":"overview" 85 } 86 ],"layout":"A4 portrait", 87 title: 'A simple example'} 88 No newline at end of file -
print-standalone/samples/configVector.yaml
1 #=========================================================================== 2 # allowed DPIs 3 #=========================================================================== 4 dpis: [254, 190, 127, 56] 5 6 #=========================================================================== 7 # the allowed scales 8 #=========================================================================== 9 scales: 10 - 25000 11 - 50000 12 - 100000 13 - 200000 14 - 500000 15 - 1000000 16 - 2000000 17 - 4000000 18 19 #=========================================================================== 20 # the list of allowed hosts 21 #=========================================================================== 22 hosts: 23 - !localMatch 24 dummy: true 25 - !ipMatch 26 ip: www.camptocamp.org 27 - !dnsMatch 28 host: labs.metacarta.com 29 port: 80 30 - !dnsMatch 31 host: terraservice.net 32 port: 80 33 - !dnsMatch 34 host: tile.openstreetmap.org 35 port: 80 36 37 layouts: 38 #=========================================================================== 39 A4 portrait: 40 #=========================================================================== 41 metaData: 42 title: '${title}' 43 author: 'MapFish print module' 44 subject: 'Simple layout' 45 keywords: 'map,print' 46 creator: 'MapFish' 47 48 #------------------------------------------------------------------------- 49 mainPage: 50 pageSize: A4 51 rotation: true 52 header: 53 height: 50 54 items: 55 - !text 56 font: Helvetica 57 fontSize: 30 58 align: right 59 text: '${mapTitle}' 60 items: 61 - !map 62 spacingAfter: 30 63 width: 440 64 height: 483 65 - !columns 66 # columns can have an absolute position. In that case, they need the 3 following fields: 67 absoluteX: 410 68 absoluteY: 310 69 width: 100 70 items: 71 - !scalebar 72 type: bar 73 maxSize: 100 74 barBgColor: white 75 fontSize: 8 76 align: right 77 - !text 78 text: '${comment}' 79 spacingAfter: 30 80 - !attributes 81 source: data 82 spacingAfter: 30 83 columnDefs: 84 id: 85 columnWeight: 2 86 header: !text 87 text: ID 88 backgroundColor: #A0A0A0 89 cell: !text 90 text: '${id}' 91 name: 92 columnWeight: 5 93 header: !text 94 text: Name 95 backgroundColor: #A0A0A0 96 cell: !columns 97 config: 98 cells: 99 - backgroundColor: '${nameBackgroundColor}' 100 borderWidth: 1 101 borderColor: '${nameBorderColor}' 102 items: 103 - !text 104 text: '${name}' 105 - !text 106 font: Helvetica 107 fontSize: 9 108 align: right 109 text: '1:${scale} ${now MM.dd.yyyy}' 110 footer: &commonFooter 111 height: 30 112 items: 113 - !columns 114 config: 115 cells: 116 - paddingBottom: 5 117 items: 118 - !text 119 backgroundColor: #FF0000 120 align: left 121 text: ©Camptocamp SA 122 - !text 123 align: right 124 text: 'Page ${pageNum}'
