Logo Search packages:      
Sourcecode: inkscape version File versions  Download package

void Inkscape::UI::Dialog::SVGPreview::showImage ( Glib::ustring &  fileName  ) 

Show image embedded in SVG

Definition at line 192 of file filedialogimpl-gtkmm.cpp.

Referenced by Inkscape::UI::Dialog::FileListViewText::on_cursor_changed().

{
    Glib::ustring fileName = theFileName;


    /*#####################################
    # LET'S HAVE SOME FUN WITH SVG!
    # Instead of just loading an image, why
    # don't we make a lovely little svg and
    # display it nicely?
    #####################################*/

    //Arbitrary size of svg doc -- rather 'portrait' shaped
    gint previewWidth  = 400;
    gint previewHeight = 600;

    //Get some image info. Smart pointer does not need to be deleted
    Glib::RefPtr<Gdk::Pixbuf> img(NULL);
    try {
        img = Gdk::Pixbuf::create_from_file(fileName);
    }
    catch (const Glib::FileError & e)
    {
        g_message("caught Glib::FileError in SVGPreview::showImage");
        return;
    }
    catch (const Gdk::PixbufError & e)
    {
        g_message("Gdk::PixbufError in SVGPreview::showImage");
        return;
    }
    catch (...)
    {
        g_message("Caught ... in SVGPreview::showImage");
        return;
    }

    gint imgWidth  = img->get_width();
    gint imgHeight = img->get_height();

    //Find the minimum scale to fit the image inside the preview area
    double scaleFactorX = (0.9 *(double)previewWidth)  / ((double)imgWidth);
    double scaleFactorY = (0.9 *(double)previewHeight) / ((double)imgHeight);
    double scaleFactor = scaleFactorX;
    if (scaleFactorX > scaleFactorY)
        scaleFactor = scaleFactorY;

    //Now get the resized values
    gint scaledImgWidth  = (int) (scaleFactor * (double)imgWidth);
    gint scaledImgHeight = (int) (scaleFactor * (double)imgHeight);

    //center the image on the area
    gint imgX = (previewWidth  - scaledImgWidth)  / 2;
    gint imgY = (previewHeight - scaledImgHeight) / 2;

    //wrap a rectangle around the image
    gint rectX      = imgX-1;
    gint rectY      = imgY-1;
    gint rectWidth  = scaledImgWidth +2;
    gint rectHeight = scaledImgHeight+2;

    //Our template.  Modify to taste
    gchar const *xformat =
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
          "<svg\n"
          "xmlns=\"http://www.w3.org/2000/svg\"\n"
          "xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
          "width=\"%d\" height=\"%d\">\n"  //# VALUES HERE
          "<rect\n"
          "  style=\"fill:#eeeeee;stroke:none\"\n"
          "  x=\"-100\" y=\"-100\" width=\"4000\" height=\"4000\"/>\n"
          "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"\n"
          "xlink:href=\"%s\"/>\n"
          "<rect\n"
          "  style=\"fill:none;"
          "    stroke:#000000;stroke-width:1.0;"
          "    stroke-linejoin:miter;stroke-opacity:1.0000000;"
          "    stroke-miterlimit:4.0000000;stroke-dasharray:none\"\n"
          "  x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\n"
          "<text\n"
          "  style=\"font-size:24.000000;font-style:normal;font-weight:normal;"
          "    fill:#000000;fill-opacity:1.0000000;stroke:none;"
          "    font-family:Bitstream Vera Sans\"\n"
          "  x=\"10\" y=\"26\">%d x %d</text>\n" //# VALUES HERE
          "</svg>\n\n";

    //if (!Glib::get_charset()) //If we are not utf8
    fileName = Glib::filename_to_utf8(fileName);

    //Fill in the template
    /* FIXME: Do proper XML quoting for fileName. */
    gchar *xmlBuffer = g_strdup_printf(xformat,
           previewWidth, previewHeight,
           imgX, imgY, scaledImgWidth, scaledImgHeight,
           fileName.c_str(),
           rectX, rectY, rectWidth, rectHeight,
           imgWidth, imgHeight);

    //g_message("%s\n", xmlBuffer);

    //now show it!
    setFromMem(xmlBuffer);
    g_free(xmlBuffer);
}


Generated by  Doxygen 1.6.0   Back to index