Changeset 23746 in osm for applications/editors


Ignore:
Timestamp:
2010-10-22T15:51:17+02:00 (14 years ago)
Author:
extropy
Message:

fixes

Location:
applications/editors/josm/plugins/pdfimport/src/pdfimport
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/LoadPdfDialog.java

    r23703 r23746  
    1919import java.awt.event.WindowEvent;
    2020import java.io.FileNotFoundException;
     21import java.io.IOException;
    2122import java.util.Collection;
    2223
     
    4142import org.openstreetmap.josm.data.projection.Projection;
    4243import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     44import org.openstreetmap.josm.io.OsmExporter;
    4345
    4446public class LoadPdfDialog extends JFrame {
     
    6769        private JButton loadFileButton;
    6870        private JButton showButton;
     71        private JButton saveButton;
    6972
    7073        public LoadPdfDialog() {
     
    8891                        public void actionPerformed(ActionEvent e) {
    8992                                okPressed();
     93                        }
     94                });
     95
     96                this.saveButton.addActionListener(new ActionListener() {
     97                        public void actionPerformed(ActionEvent e) {
     98                                savePressed();
    9099                        }
    91100                });
     
    137146                this.loadFileButton = new JButton(tr("Load file..."));
    138147                this.okButton = new JButton(tr("Place"));
     148                this.saveButton = new JButton(tr("Save"));
    139149                this.showButton = new JButton(tr("Show target"));
    140150                this.cancelButton = new JButton(tr("Discard"));
     
    206216                okCancelPanel.add(this.showButton);
    207217                okCancelPanel.add(this.okButton);
    208 
     218                okCancelPanel.add(this.saveButton);
    209219
    210220                JPanel panel = new JPanel(new BorderLayout());
     
    261271        }
    262272
     273        private void savePressed() {
     274                boolean ok = this.loadTransformation();
     275                if (!ok){
     276                        return;
     277                }
     278
     279                java.io.File file = this.chooseSaveFile();
     280
     281                if (file == null){
     282                        return;
     283                }
     284
     285                //rebuild layer with latest projection
     286                this.removeLayer();
     287                this.saveLayer(file);
     288                this.setVisible(false);
     289        }
     290
     291
    263292        private void showPressed() {
    264293
     
    325354                        public String getDescription() {
    326355                                return tr("PDF files");
     356                        }
     357                });
     358                int result = fc.showOpenDialog(Main.parent);
     359
     360                if (result != JFileChooser.APPROVE_OPTION) {
     361                        return null;
     362                }
     363                else
     364                {
     365                        return fc.getSelectedFile();
     366                }
     367        }
     368
     369        private java.io.File chooseSaveFile() {
     370                //get file name
     371                JFileChooser fc = new JFileChooser();
     372                fc.setAcceptAllFileFilterUsed(true);
     373                fc.setMultiSelectionEnabled(false);
     374                fc.setFileFilter(new FileFilter(){
     375                        @Override
     376                        public boolean accept(java.io.File pathname) {
     377                                return pathname.isDirectory() || pathname.getName().endsWith(".osm");
     378                        }
     379                        @Override
     380                        public String getDescription() {
     381                                return tr("OSM files");
    327382                        }
    328383                });
     
    449504                this.showButton.setEnabled(false);
    450505        }
     506
     507        private void saveLayer(java.io.File file) {
     508                DataSet data = builder.build(this.data, true);
     509                OsmDataLayer layer = new OsmDataLayer(data, file.getName(), file);
     510
     511                OsmExporter exporter = new OsmExporter();
     512
     513                try {
     514                        exporter.exportData(file, layer);
     515                }
     516                catch(IOException e){
     517                        //TODO:
     518                }
     519        }
     520
    451521}
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/OsmBuilder.java

    r23702 r23746  
    8383
    8484                for (PdfPath path: layer.paths){
    85                         Way w = this.insertWay(path, point2Node, -1, full);
     85                        Way w = this.insertWay(path, point2Node, -1, full, false);
    8686                        target.addPrimitive(w);
    8787                        path2Way.put(path, w);
     
    9191                for (PdfMultiPath mpath: layer.multiPaths) {
    9292                        for (PdfPath path: mpath.paths){
    93                                 Way w = this.insertWay(path, point2Node, pathId, full);
     93                                Way w = this.insertWay(path, point2Node, pathId, full, true);
    9494                                target.addPrimitive(w);
    9595                                path2Way.put(path, w);
     
    118118        }
    119119
    120         private Way insertWay(PdfPath path, Map<Point2D, Node> point2Node, int multipathId, boolean full) {
     120        private Way insertWay(PdfPath path, Map<Point2D, Node> point2Node, int multipathId, boolean full, boolean multipolygon) {
    121121
    122122                List<Node> nodes = new ArrayList<Node>(path.points.size());
     
    149149                                keys.put("PDF_multipath", ""+ multipathId);
    150150                        }
    151                         else if (path.layer.info.fill) {
     151                        else if (path.layer.info.fill && !multipolygon) {
    152152                                keys.put("area", "yes");
    153153                        }
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/PDFStreamProcessor.java

    r23702 r23746  
    33import it.stefanochizzolini.clown.documents.Document;
    44import it.stefanochizzolini.clown.documents.contents.ContentScanner;
    5 import it.stefanochizzolini.clown.documents.contents.Contents;
    65import it.stefanochizzolini.clown.documents.contents.ContentScanner.GraphicsState;
    76import it.stefanochizzolini.clown.documents.contents.colorSpaces.ColorSpace;
     
    1918import it.stefanochizzolini.clown.documents.contents.objects.FillStrokeEvenOdd;
    2019import it.stefanochizzolini.clown.documents.contents.objects.GenericOperation;
    21 import it.stefanochizzolini.clown.documents.contents.objects.LocalGraphicsState;
    22 import it.stefanochizzolini.clown.documents.contents.objects.ModifyCTM;
    2320import it.stefanochizzolini.clown.documents.contents.objects.Path;
    24 import it.stefanochizzolini.clown.documents.contents.objects.SetLineCap;
    25 import it.stefanochizzolini.clown.documents.contents.objects.SetLineWidth;
    2621import it.stefanochizzolini.clown.documents.contents.objects.Stroke;
    2722import it.stefanochizzolini.clown.documents.contents.objects.Text;
     
    3833import java.util.Map;
    3934
    40 import javax.management.RuntimeErrorException;
    41 
    4235public class PDFStreamProcessor {
    4336
    44         private LayerInfo info;
     37        private final LayerInfo info;
    4538        public Rectangle2D bounds;
    4639        int pathNo = 0;
     
    5346
    5447        public PDFStreamProcessor(Document doc) {
    55                
     48
    5649                this.rgbSpace = new DeviceRGBColorSpace(doc);
    5750                this.graySpace = new DeviceGrayColorSpace(doc);
    58                
     51
    5952                this.info = new LayerInfo();
    6053        }
    61        
     54
    6255        public void finish() {
    6356                this.rgbSpace = null;
    6457                this.graySpace = null;
    65                 this.state = null;             
     58                this.state = null;
    6659                this.optimizer.optimize();
    6760        }
    68        
     61
    6962        public List<LayerContents> getResult() {
    7063                return this.optimizer.getLayers();
     
    7568                        return;
    7669
    77             while(level.moveNext()) {           
    78                         ContentObject object = level.getCurrent();                     
     70                while(level.moveNext()) {
     71                        ContentObject object = level.getCurrent();
    7972                        if(object instanceof ContainerObject) {
    8073                                // Scan the inner level!
    8174                                process(level.getChildLevel());
    82                         } 
     75                        }
    8376                        else {
    8477                                addObject(level);
    8578                        }
    86             }
    87         }       
    88        
     79                }
     80        }
     81
    8982        public void addObject(ContentScanner level){
    90                
     83
    9184                ContentObject obj = level.getCurrent();
    92                
     85
    9386                if (obj instanceof Path)
    9487                {
     
    9891                else if (obj instanceof Text){
    9992                        //maybe something here
    100                 }                       
     93                }
    10194                else if (obj instanceof EndPathNoOp){
    10295                        //nothing here
    10396                        this.info.divider ++;
    104                 }       
     97                }
    10598                else if (obj instanceof GenericOperation) {
    10699                        this.state = level.getState();
     
    110103                else {
    111104                        int a = 10;
    112                         a++;   
     105                        a++;
    113106                }
    114107        }
     
    117110                String op = go.getOperator();
    118111                boolean parsed = true;
    119                
    120                 if (op.equals("RG")) {
     112                //FIXME - currently mapping ICC colors (SCN) to device RGB.
     113
     114                if (op.equals("RG") || op.equals("SCN")) {
    121115                        this.state.strokeColorSpace = this.rgbSpace;
    122116                        this.state.strokeColor = this.rgbSpace.getColor(go.getOperands().toArray(new PdfDirectObject[3]));
     
    126120                        this.state.strokeColor = this.graySpace.getColor(go.getOperands().toArray(new PdfDirectObject[3]));
    127121                }
    128                 else if (op.equals("rg")) {
     122                else if (op.equals("rg") || op.equals("scn")) {
    129123                        this.state.fillColorSpace = this.rgbSpace;
    130124                        this.state.fillColor = this.rgbSpace.getColor(go.getOperands().toArray(new PdfDirectObject[3]));
     
    138132                        //nothing here
    139133                        int a = 10;
    140                         a++;   
    141134                        a++;
    142                 }
    143                
     135                        a++;
     136                }
     137
    144138                if (parsed && setDivider) {
    145139                        this.info.divider ++;
     
    148142
    149143        private void parsePath(Path path) {
    150                
     144
    151145                List<PdfPath> paths = this.getPathNodes(path);
    152146                this.updateInfoFromState();
    153                
     147
    154148                for (PdfPath p: paths){
    155149                        p.nr = pathNo;
    156150                }
    157                
    158                 pathNo ++;             
    159                
    160                 if (paths.size() > 1) {                 
     151
     152                pathNo ++;
     153
     154                if (paths.size() > 1) {
    161155                        this.optimizer.addMultiPath(this.info, paths);
    162156                }
     
    165159                }
    166160        }
    167        
     161
    168162        private List<PdfPath> getPathNodes(Path path) {
    169                 List<PdfPath> result = new ArrayList<PdfPath>(2); 
     163                List<PdfPath> result = new ArrayList<PdfPath>(2);
    170164                List<Point2D> points = new ArrayList<Point2D>(2);
    171165                this.info.fill = false;
    172166                this.info.stroke = true;
    173        
     167
    174168                for (ContentObject obj:path.getObjects()) {
    175169                        Point2D point = null;
    176                        
     170
    177171                        if (obj instanceof BeginSubpath) {
    178172                                if (points.size() >= 2) {
     
    180174                                        points = new ArrayList<Point2D>(2);
    181175                                }
    182                                
     176
    183177                                BeginSubpath b = (BeginSubpath)obj;
    184178                                point = b.getPoint();
     
    188182                        }
    189183                        else if (obj instanceof DrawCurve) {
    190                                
    191                                 DrawCurve c = (DrawCurve) obj;                         
    192                                 point = c.getPoint();                   
     184
     185                                DrawCurve c = (DrawCurve) obj;
     186                                point = c.getPoint();
    193187                        }
    194188                        else if (obj instanceof Stroke) {
     
    223217                                        result.add(new PdfPath(points));
    224218                                        points = new ArrayList<Point2D>(2);
    225                                 }                               
    226                                        
     219                                }
     220
    227221                                DrawRectangle r = (DrawRectangle) obj;
    228                                
     222
    229223                                points.add(this.parsePoint(new Point2D.Double(r.getX(), r.getY())));
    230224                                points.add(this.parsePoint(new Point2D.Double(r.getX()+r.getWidth(), r.getY())));
     
    232226                                points.add(this.parsePoint(new Point2D.Double(r.getX(), r.getY()+r.getHeight())));
    233227                                points.add(points.get(0));
    234                                 result.add(new PdfPath(points));                               
     228                                result.add(new PdfPath(points));
    235229                                points = new ArrayList<Point2D>(2);
    236230                        }
    237                         else {                         
     231                        else {
    238232                                int a = 10;
    239233                                a++;
    240234                        }
    241                        
     235
    242236                        //add point
    243237                        if (point != null)
    244                         {       
    245                                 boolean sameAsPrevPoint = (points.size() > 0) &&points.get(points.size() - 1).equals(point);                           
     238                        {
     239                                boolean sameAsPrevPoint = (points.size() > 0) &&points.get(points.size() - 1).equals(point);
    246240                                if (!sameAsPrevPoint) {
    247241                                        points.add(this.parsePoint(point));
    248242                                }
    249                         }               
    250                 }                       
    251                
     243                        }
     244                }
     245
    252246                if (points.size() >= 2)
    253247                {
    254                         result.add(new PdfPath(points));       
    255                 }
    256        
     248                        result.add(new PdfPath(points));
     249                }
     250
    257251                return result;
    258         }       
     252        }
    259253
    260254        private Point2D parsePoint(Point2D point) {
     
    264258
    265259
    266        
     260
    267261        private void updateInfoFromState() {
    268262                this.info.color = getColor(this.state.strokeColor);
    269263                this.info.fillColor = getColor(this.state.fillColor);
    270                 this.info.width = this.state.lineWidth;         
    271         }
    272                
     264                this.info.width = this.state.lineWidth;
     265        }
     266
    273267        private Color getColor(
    274268                        it.stefanochizzolini.clown.documents.contents.colorSpaces.Color col) {
     269                if (col == null) {
     270                        return Color.BLACK;
     271                }
     272
    275273                ColorSpace space = col.getColorSpace();
    276                
     274
    277275                if (space instanceof DeviceRGBColorSpace) {
    278276                        return new Color(
     
    289287                else {
    290288                        throw new RuntimeException("Unexpected colour space: "+space.toString());
    291                 }               
     289                }
    292290        }
    293291
    294292        private Color addColor(GenericOperation go) {
    295                 List<PdfDirectObject> operands = go.getOperands();             
     293                List<PdfDirectObject> operands = go.getOperands();
    296294                PdfDirectObject o1 = operands.get(0);
    297295                PdfDirectObject o2 = operands.get(1);
     
    300298                return c;
    301299        }
    302        
    303        
     300
     301
    304302        private Color addGrayColor(GenericOperation go) {
    305                 List<PdfDirectObject> operands = go.getOperands();             
     303                List<PdfDirectObject> operands = go.getOperands();
    306304                PdfDirectObject o1 = operands.get(0);
    307305                Color c =new Color(parseFloat(o1), parseFloat(o1), parseFloat(o1));
    308306                return c;
    309307        }
    310                
    311        
     308
     309
    312310        private float parseFloat(PdfDirectObject obj) {
    313311                if (obj instanceof PdfReal) {
     
    320318                        return 0.0f;
    321319                }
    322         }       
     320        }
    323321
    324322}
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java

    r23702 r23746  
    1010
    1111public class PathOptimizer {
    12        
     12
    1313        public Map<Point2D, Point2D> uniquePointMap;
    14         private Map<LayerInfo, LayerContents> layerMap;
     14        private final Map<LayerInfo, LayerContents> layerMap;
    1515        private List<LayerContents> layers;
    16        
     16
    1717        public PathOptimizer()
    1818        {
     
    3030                        this.uniquePointMap.put(point, point);
    3131                        return point;
    32                 }               
    33         }
    34        
     32                }
     33        }
     34
    3535        public void addPath(LayerInfo info, PdfPath path)
    3636        {
     
    3838                layer.paths.add(path);
    3939        }
    40                
     40
    4141        public void addMultiPath(LayerInfo info, List<PdfPath> paths) {
    4242                LayerContents layer = this.getLayer(info);
    4343                PdfMultiPath p = new PdfMultiPath(paths);
    44                 layer.multiPaths.add(p);                                       
    45         }       
    46        
     44                layer.multiPaths.add(p);
     45        }
     46
    4747        private LayerContents getLayer(LayerInfo info) {
    4848                LayerContents layer;
    49                
     49
    5050                if (this.layerMap.containsKey(info))
    5151                {
     
    6363                return layer;
    6464        }
    65        
     65
    6666        public void optimize()
    6767        {
    6868                for(LayerContents layer: this.layers) {
    69                         this.optimizeLayer(layer);                     
    70                 }
    71                
     69                        this.concatenataPaths(layer);
     70                }
     71
    7272                List<LayerContents> newLayers = new ArrayList<LayerContents>();
    7373                int nr = 0;
    74                
     74
    7575                for(LayerContents l: this.layers) {
    7676                        List<LayerContents> splitResult = splitBySegmentKind(l);
    77                        
     77
    7878                        for(LayerContents ll: splitResult) {
    7979                                ll.info.nr = nr;
     
    8282                        }
    8383                }
    84                
     84
    8585                this.layers = newLayers;
    86                
     86
    8787                for(LayerContents layer: this.layers) {
    8888                        finalizeLayer(layer);
     
    9393                Set<Point2D> points = new HashSet<Point2D>();
    9494                layer.points = new ArrayList<Point2D>();
    95                
     95
    9696                for(PdfPath pp: layer.paths){
    9797                        pp.layer = layer;
    98                        
     98
    9999                        for(Point2D point: pp.points){
    100100                                if (!points.contains(point)) {
     
    102102                                        points.add(point);
    103103                                }
    104                         }       
    105                 }
    106                
     104                        }
     105                }
     106
    107107                for (PdfMultiPath multipath: layer.multiPaths) {
    108108                        multipath.layer = layer;
    109109                        for(PdfPath pp: multipath.paths){
    110110                                pp.layer = layer;
    111                                
     111
    112112                                for(Point2D point: pp.points){
    113113                                        if (!points.contains(point)) {
     
    119119                }
    120120        }
    121        
     121
    122122        /**
    123          * This method merges together paths with common end nodes. 
     123         * This method merges together paths with common end nodes.
    124124         * @param layer the layer to process.
    125125         */
    126         private void optimizeLayer(LayerContents layer) {
     126        private void concatenataPaths(LayerContents layer) {
    127127                Map<Point2D, PdfPath> pathEndpoints = new HashMap<Point2D, PdfPath>();
    128                 Set<PdfPath> mergedPaths = new HashSet<PdfPath>();             
    129        
    130                 /*
    131                 //sort paths, longest first
    132                 List<PdfPath> sortedPaths = new ArrayList<PdfPath>();
    133                
    134                 for(PdfPath path: layer.paths) {
    135                         path.calculateLength();
    136                         sortedPaths.add(path);
    137                 }
    138                
    139                 Collections.sort(sortedPaths, new Comparator<PdfPath>(){
    140                         public int compare(PdfPath o1, PdfPath o2) {
    141 
    142                                 if (o1.length > o2.length) {
    143                                         return -1;
    144                                 } else if (o1.length < o2.length) {
    145                                         return 1;
    146                                 } else {
    147                                         return 0;
    148                                 }
    149                         }
    150                        
    151                 });
    152                 */
    153        
     128                Set<PdfPath> mergedPaths = new HashSet<PdfPath>();
     129
    154130                for(PdfPath pp: layer.paths){
    155                                                
     131
    156132                        PdfPath path = pp;
    157133                        boolean changed = true;
    158                        
     134
    159135                        while (changed && !path.isClosed()) {
    160136                                changed  = false;
    161                                
    162                                 if (pathEndpoints.containsKey(path.firstPoint())){                                     
     137
     138                                if (pathEndpoints.containsKey(path.firstPoint())) {
    163139                                        PdfPath p1 = pathEndpoints.get(path.firstPoint());
    164140                                        pathEndpoints.remove(p1.firstPoint());
    165141                                        pathEndpoints.remove(p1.lastPoint());
    166                                                                        
     142
    167143                                        List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points);
    168                                         path.points = newNodes;                                                                 
     144                                        path.points = newNodes;
    169145                                        mergedPaths.add(p1);
    170146                                        changed = true;
    171147                                }
    172                                
    173                                 if (pathEndpoints.containsKey(path.lastPoint())){                                       
     148
     149                                if (pathEndpoints.containsKey(path.lastPoint())) {
    174150                                        PdfPath p1 = pathEndpoints.get(path.lastPoint());
    175151                                        pathEndpoints.remove(p1.firstPoint());
    176152                                        pathEndpoints.remove(p1.lastPoint());
    177                                                                        
     153
    178154                                        List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points);
    179                                         path.points = newNodes;                                                                 
     155                                        path.points = newNodes;
    180156                                        mergedPaths.add(p1);
    181157                                        changed = true;
    182                                 }                               
    183                         }       
    184                        
     158                                }
     159                        }
     160
    185161                        if (!path.isClosed()){
    186162                                pathEndpoints.put(path.firstPoint(), path);
     
    188164                        }
    189165                }
    190                
     166
    191167                List<PdfPath> resultPaths = new ArrayList<PdfPath>();
    192                
     168
    193169                for(PdfPath path: layer.paths) {
    194170                        if (!mergedPaths.contains(path)){
     
    196172                        }
    197173                }
    198                                
     174
    199175                layer.paths = resultPaths;
    200176        }
    201        
     177
    202178        private List<LayerContents> splitBySegmentKind(LayerContents layer)
    203179        {
     
    205181                List<PdfPath> multiSegmentPaths = new ArrayList<PdfPath>();
    206182                List<PdfPath> closedPaths = new ArrayList<PdfPath>();
    207                
     183
    208184                for(PdfPath path: layer.paths) {
    209185                        if (path.points.size() <= 3) {
     
    217193                        }
    218194                }
    219                
     195
    220196                List<LayerContents> layers = new ArrayList<LayerContents>();
    221                
     197
    222198                if (multiSegmentPaths.size() > 0) {
    223199                        LayerContents l = new LayerContents();
    224200                        l.paths = multiSegmentPaths;
    225201                        l.info = layer.info.copy();
    226                        
     202
    227203                        layers.add(l);
    228204                }
    229                
     205
    230206                if (singleSegmentPaths.size() > 0) {
    231207                        LayerContents l = new LayerContents();
     
    242218                        layers.add(l);
    243219                }
    244                
     220
    245221                return layers;
    246222        }
    247        
    248        
     223
     224
    249225
    250226        private List<Point2D> tryMergeNodeLists(List<Point2D> nodes1, List<Point2D> nodes2) {
    251                
     227
    252228                boolean nodes1Closed = (nodes1.get(0) == nodes1.get(nodes1.size() - 1));
    253229                boolean nodes2Closed = (nodes2.get(0) == nodes2.get(nodes2.size() - 1));
    254                
     230
    255231                if (nodes1Closed || nodes2Closed) {
    256232                        return null;
    257233                }
    258                
     234
    259235                if (nodes1.get(nodes1.size() - 1) == nodes2.get(0)) {
    260236                        nodes1.remove(nodes1.size() -1);
     
    267243                                nodes1.add(nodes2.get(pos));
    268244                        }
    269                        
    270                         return nodes1;
    271                 } 
     245
     246                        return nodes1;
     247                }
    272248                else if (nodes1.get(0) == nodes2.get(nodes2.size() - 1)) {
    273249                        nodes1.remove(0);
    274250                        nodes1.addAll(0, nodes2);
    275251                        return nodes1;
    276                 } 
     252                }
    277253                else if (nodes1.get(0) == nodes2.get(0)) {
    278                         nodes1.remove(0);                       
     254                        nodes1.remove(0);
    279255                        for (int pos = 0; pos < nodes2.size(); pos ++) {
    280256                                nodes1.add(0, nodes2.get(pos));
    281257                        }
    282                        
     258
    283259                        return nodes1;
    284260                } else {
Note: See TracChangeset for help on using the changeset viewer.