Hello again!

Appearently my included "patches" in my previous mail on this thread were malformed
by my mailclient.  So, I saved them to files and ancluded them as attachments
instead.  Note, it's still "context patches", I don't attach the entire modified
files.

Sorry for any inconvenience.

Best regards

/IlvJa

    /**
     * To print the current document.
     */
    public class PrintAction extends AbstractAction {
        public PrintAction() {}
        public void actionPerformed(ActionEvent e) {
            if (svgDocument != null) {
                final SVGDocument doc = svgDocument;
                new Thread() {
                    public void run(){
                        String uri = doc.getURL();
                        String fragment = svgCanvas.getFragmentIdentifier();
                        if (fragment != null) {
                            uri += "#"+fragment;
                        }

                        //
                        // Build a PrintTranscoder to handle printing
                        // of the svgDocument object
                        //
                        PrintTranscoder pt = new PrintTranscoder();

                        //
                        // Set transcoding hints
                        //
                        pt.addTranscodingHint(pt.KEY_XML_PARSER_CLASSNAME,
                                              application.getXMLParserClassName());

                        pt.addTranscodingHint(pt.KEY_SHOW_PAGE_DIALOG,
                                              Boolean.TRUE);


                        pt.addTranscodingHint(pt.KEY_SHOW_PRINTER_DIALOG,
                                              Boolean.TRUE);
                                              
                        //
                        // Jakob Ilves hardcoded (and therefore ugly) hack.
                        //
                        // As I want to print UNSCALED graphics I want the
                        // transcoder hint KEY_SCALE_TO_PAGE to be set to
                        // false.  How to best do that I have to discuss with
                        // people who develop batik.
                        //
                        // Also, it appears that the graphics on screen is
                        // assumed to be 96dpi while the printer is assumed
                        // to be 72dpi and somewhere a mistake occurs so the
                        // printed graphics becomes 4/3 times larger than
                        // wanted.  So, I also (ab-)use the deprecated
                        // KEY_PIXEL_TO_MM to adjust things.
                        //

                        pt.addTranscodingHint(pt.KEY_SCALE_TO_PAGE,
                                             Boolean.FALSE);

                        pt.addTranscodingHint(pt.KEY_PIXEL_UNIT_TO_MILLIMETER,
                                              new Float(0.1984375));
                                              
                        pt.addTranscodingHint(pt.KEY_PIXEL_TO_MM,
                                              new Float(0.1984375));
                        //
                        // Do transcoding now
                        //
                        pt.transcode(new TranscoderInput(uri), null);

                        //
                        // Print
                        //
                        try {
                            pt.print();
                        } catch (PrinterException ex) {
                            userAgent.displayError(ex);
                        }
                    }
                }.start();
            }
        }
    }

    /**
     * Printable implementation
     */
    public int print(Graphics _g, PageFormat pageFormat, int pageIndex){
        //
        // On the first page, take a snapshot of the vector of
        // TranscodeInputs.
        //
        if(pageIndex == 0){
            printedInputs = (Vector)inputs.clone();
        }

        //
        // If we have already printed each page, return
        //
        if(pageIndex >= printedInputs.size()){
            curIndex = -1;
            System.out.println("Done");
            return NO_SUCH_PAGE;
        }

        //
        // Load a new document now if we are printing a new page
        //
        if(curIndex != pageIndex){
            // The following call will invoke this class' transcode
            // method which takes a document as an input. That method
            // builds the GVT root tree.{
            try{
                super.transcode((TranscoderInput)printedInputs.elementAt(pageIndex),
                                null);
                curIndex = pageIndex;
            }catch(TranscoderException e){
                drawError(_g, e);
                return PAGE_EXISTS;
            }
        }

        // Cast to Graphics2D to access Java 2D features
        Graphics2D g = (Graphics2D)_g;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                           RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                           RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING,
                           RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);

        //
        // Compute transform so that the SVG document fits on one page
        //
        AffineTransform t = g.getTransform();
        Shape clip = g.getClip();

        Rectangle2D bounds = curAOI;

        double scaleX = pageFormat.getImageableWidth() / bounds.getWidth();
        double scaleY = pageFormat.getImageableHeight() / bounds.getHeight();
        double scale = scaleX < scaleY ? scaleX : scaleY;

        // Check hint to know if scaling is really needed
        Boolean scaleToPage = (Boolean)hints.get(KEY_SCALE_TO_PAGE);
        if(scaleToPage != null && !scaleToPage.booleanValue()){
            //
            // Jakob Ilves ugly addition:
            // If KEY_SCALE_TO_PAGE is set to FALSE but still
            // KEY_PIXEL_UNIT_TO_MILLIMETER or the deprecated
            // KEY_PIXEL_TO_MM is set honor that value.  Note, the default
            // appears to be 72dpi (for some reason), so if you set to
            // the value to 0.264583333... (72dpi), the scale should be 1.
            // If you set it to 0.1984375 (96dpi) the scaling becomes 0.75.
            //
            // This hack should be brought up with the Batik developers.
            //
            
            Float px2mm;

            px2mm = (Float)hints.get(KEY_PIXEL_UNIT_TO_MILLIMETER);

            if(px2mm == null)
            {
              px2mm = (Float)hints.get(KEY_PIXEL_TO_MM);
            }

            if(px2mm != null)
            {
              scale = px2mm.floatValue()/0.2645833333333333333333333333333f;
            }
            else
            {
              scale = 1;
            }
        }

        double xMargin = (pageFormat.getImageableWidth() - bounds.getWidth()*scale)/2;
        double yMargin = (pageFormat.getImageableHeight() - 
bounds.getHeight()*scale)/2;
        g.translate(pageFormat.getImageableX() + xMargin,
                    pageFormat.getImageableY() + yMargin);
        g.scale(scale, scale);


        //
        // Append transform to selected area
        //
        g.transform(curTxf);

        g.clip(curAOI);

        //
        // Delegate rendering to painter
        //
        try{
            root.paint(g);
        }catch(Exception e){
            g.setTransform(t);
            g.setClip(clip);
            drawError(_g, e);
        }

        //
        // Restore transform and clip
        //
        g.setTransform(t);
        g.setClip(clip);

        // g.setPaint(Color.black);
        // g.drawString(uris[pageIndex], 30, 30);


        //
        // Return status indicated that we did paint a page
        //
        return PAGE_EXISTS;
    }

begin:vcard 
n:;Jakob
x-mozilla-html:FALSE
adr:;;;;;;
version:2.1
email;internet:[EMAIL PROTECTED]
fn:Jakob Ilves
end:vcard

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to