dcsvg.cpp 44 KB
Newer Older
1 2 3 4 5 6 7 8 9
/////////////////////////////////////////////////////////////////////////////
// Name:        svg.cpp
// Purpose:     SVG plot
// Author:      Chris Elliott
// Modified by: JP Charras (dec 2006)
// Licence:     wxWindows license
/////////////////////////////////////////////////////////////////////////////


10

11
// For compilers that support precompilation, includes "wx/wx.h".
12
#include <wx/wxprec.h>
13 14 15 16 17 18

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
19
#include <wx/wx.h>
20 21
#endif

22
#include <dcsvg.h>
23

24
#include <wx/filename.h>
25 26
#include <wx/image.h>
#include <macros.h>
Dick Hollenbeck's avatar
Dick Hollenbeck committed
27
#include <common.h>
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#if wxCHECK_VERSION( 2, 9, 0 )

// We could do nothing, because wxWidgets 3 supports the SVG format
// (previously, it was a contribution library, not included in wxWidgets)
// However arcs are drawn as pies, and we must change it.
// Unfortunately most of functions are private, and we cannot derive
// our KicadSVGFileDCImpl from wxSVGFileDCImpl
// and just override the 2 incorrect functions
// Just wxWidget dcsvg is copied here and 2 functions are modified:
// KicadSVGFileDCImpl::DoDrawArc() and KicadSVGFileDCImpl::DoDrawEllipticArc()

namespace
{

inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }

// This function returns a string representation of a floating point number in
// C locale (i.e. always using "." for the decimal separator) and with the
// fixed precision (which is 2 for some unknown reason but this is what it was
// in this code originally).
inline wxString NumStr(double f)
{
    return wxString::FromCDouble(f, 2);
}

// Return the colour representation as HTML-like "#rrggbb" string and also
// returns its alpha as opacity number in 0..1 range.
wxString Col2SVG(wxColour c, float *opacity)
{
    if ( c.Alpha() != wxALPHA_OPAQUE )
    {
        *opacity = c.Alpha()/255.;

        // Remove the alpha before using GetAsString(wxC2S_HTML_SYNTAX) as it
        // doesn't support colours with alpha channel.
        c = wxColour(c.GetRGB());
    }
    else // No alpha.
    {
        *opacity = 1.;
    }

    return c.GetAsString(wxC2S_HTML_SYNTAX);
}

wxString wxPenString(wxColour c, int style = wxPENSTYLE_SOLID)
{
    float opacity;
    wxString s = wxT("stroke:") + Col2SVG(c, &opacity)  + wxT("; ");

    switch ( style )
    {
        case wxPENSTYLE_SOLID:
            s += wxString::Format(wxT("stroke-opacity:%s; "), NumStr(opacity));
            break;
        case wxPENSTYLE_TRANSPARENT:
            s += wxT("stroke-opacity:0.0; ");
            break;
        default :
            wxASSERT_MSG(false, wxT("wxSVGFileDC::Requested Pen Style not available"));
    }

    return s;
}

wxString wxBrushString(wxColour c, int style = wxBRUSHSTYLE_SOLID)
{
    float opacity;
    wxString s = wxT("fill:") + Col2SVG(c, &opacity)  + wxT("; ");

    switch ( style )
    {
        case wxBRUSHSTYLE_SOLID:
            s += wxString::Format(wxT("fill-opacity:%s; "), NumStr(opacity));
            break;
        case wxBRUSHSTYLE_TRANSPARENT:
            s += wxT("fill-opacity:0.0; ");
            break;
        default :
            wxASSERT_MSG(false, wxT("wxSVGFileDC::Requested Brush Style not available"));
    }

    return s;
}

} // anonymous namespace

// ----------------------------------------------------------
// KicadSVGFileDCImpl
// ----------------------------------------------------------

IMPLEMENT_ABSTRACT_CLASS(KicadSVGFileDCImpl, wxDC)

KicadSVGFileDCImpl::KicadSVGFileDCImpl( KicadSVGFileDC *owner, const wxString &filename,
                    int width, int height, double dpi ) :
        wxDCImpl( owner )
    {
        Init( filename, width, height, dpi );
    }

void KicadSVGFileDCImpl::Init (const wxString &filename, int Width, int Height, double dpi)
{
    m_width = Width;
    m_height = Height;

    m_dpi = dpi;

    m_OK = true;

    m_mm_to_pix_x = dpi/25.4;
    m_mm_to_pix_y = dpi/25.4;

    m_backgroundBrush = *wxTRANSPARENT_BRUSH;
    m_textForegroundColour = *wxBLACK;
    m_textBackgroundColour = *wxWHITE;
    m_colour = wxColourDisplay();

    m_pen   = *wxBLACK_PEN;
    m_font  = *wxNORMAL_FONT;
    m_brush = *wxWHITE_BRUSH;

    m_graphics_changed = true;

    ////////////////////code here

    m_outfile = new wxFileOutputStream(filename);
    m_OK = m_outfile->IsOk();
    if (m_OK)
    {
        m_filename = filename;
        m_sub_images = 0;
        wxString s;
        s = wxT("<?xml version=\"1.0\" standalone=\"no\"?>") + wxString(wxT("\n"));
        write(s);
        s = wxT("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" ") + wxString(wxT("\n"));
        write(s);
        s = wxT("\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\"> ") + wxString(wxT("\n"));
        write(s);
        s = wxT("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" ") + wxString(wxT("\n"));
        write(s);
        s.Printf( wxT("    width=\"%scm\" height=\"%scm\" viewBox=\"0 0 %d %d \"> \n"), NumStr(float(Width)/dpi*2.54), NumStr(float(Height)/dpi*2.54), Width, Height );
        write(s);
        s = wxT("<title>SVG Picture created as ") + wxFileName(filename).GetFullName() + wxT(" </title>") + wxT("\n");
        write(s);
        s = wxString (wxT("<desc>Picture generated by wxSVG ")) + wxSVGVersion + wxT(" </desc>")+ wxT("\n");
        write(s);
        s =  wxT("<g style=\"fill:black; stroke:black; stroke-width:1\">") + wxString(wxT("\n"));
        write(s);
    }
}

KicadSVGFileDCImpl::~KicadSVGFileDCImpl()
{
    wxString s = wxT("</g> \n</svg> \n");
    write(s);
    delete m_outfile;
}

void KicadSVGFileDCImpl::DoGetSizeMM( int *width, int *height ) const
{
    if (width)
190
        *width = KiROUND( (double)m_width / m_mm_to_pix_x );
191 192

    if (height)
193
        *height = KiROUND( (double)m_height / m_mm_to_pix_y );
194 195 196 197
}

wxSize KicadSVGFileDCImpl::GetPPI() const
{
198
    return wxSize( KiROUND(m_dpi), KiROUND(m_dpi) );
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
}

void KicadSVGFileDCImpl::DoDrawLine (wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
{
    if (m_graphics_changed) NewGraphics();
    wxString s;
    s.Printf ( wxT("<path d=\"M%d %d L%d %d\" /> \n"), x1,y1,x2,y2 );
    if (m_OK)
    {
        write(s);
    }
    CalcBoundingBox(x1, y1);
    CalcBoundingBox(x2, y2);
}

void KicadSVGFileDCImpl::DoDrawLines(int n, wxPoint points[], wxCoord xoffset , wxCoord yoffset )
{
    for ( int i = 1; i < n; i++ )
    {
        DoDrawLine ( points [i-1].x + xoffset, points [i-1].y + yoffset,
            points [ i ].x + xoffset, points [ i ].y + yoffset );
    }
}

void KicadSVGFileDCImpl::DoDrawPoint (wxCoord x1, wxCoord y1)
{
    wxString s;
    if (m_graphics_changed) NewGraphics();
    s = wxT("<g style = \"stroke-linecap:round;\" > ") + wxString(wxT("\n"));
    write(s);
    DoDrawLine ( x1,y1,x1,y1 );
    s = wxT("</g>");
    write(s);
}

void KicadSVGFileDCImpl::DoDrawCheckMark(wxCoord x1, wxCoord y1, wxCoord width, wxCoord height)
{
    wxDCImpl::DoDrawCheckMark (x1,y1,width,height);
}

void KicadSVGFileDCImpl::DoDrawText(const wxString& text, wxCoord x1, wxCoord y1)
{
    DoDrawRotatedText(text, x1,y1,0.0);
}

void KicadSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoord y, double angle)
{
    //known bug; if the font is drawn in a scaled DC, it will not behave exactly as wxMSW
    if (m_graphics_changed) NewGraphics();
    wxString s, sTmp;

    // calculate bounding box
    wxCoord w, h, desc;
    DoGetTextExtent(sText, &w, &h, &desc);

    double rad = DegToRad(angle);

    // wxT("upper left") and wxT("upper right")
    CalcBoundingBox(x, y);
    CalcBoundingBox((wxCoord)(x + w*cos(rad)), (wxCoord)(y - h*sin(rad)));

    // wxT("bottom left") and wxT("bottom right")
    x += (wxCoord)(h*sin(rad));
    y += (wxCoord)(h*cos(rad));
    CalcBoundingBox(x, y);
    CalcBoundingBox((wxCoord)(x + h*sin(rad)), (wxCoord)(y + h*cos(rad)));

    if (m_backgroundMode == wxBRUSHSTYLE_SOLID)
    {
        // draw background first
        // just like DoDrawRectangle except we pass the text color to it and set the border to a 1 pixel wide text background

        sTmp.Printf ( wxT(" <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "), x,y+desc-h, w, h );
        s = sTmp + wxT("style=\"") + wxBrushString(m_textBackgroundColour);
        s += wxT("stroke-width:1; ") + wxPenString(m_textBackgroundColour);
        sTmp.Printf ( wxT("\" transform=\"rotate( %s %d %d )  \" />"), NumStr(-angle), x,y );
        s += sTmp + wxT("\n");
        write(s);
    }
    //now do the text itself
    s.Printf (wxT(" <text x=\"%d\" y=\"%d\" "),x,y );

    sTmp = m_font.GetFaceName();
    if (sTmp.Len() > 0)  s += wxT("style=\"font-family:") + sTmp + wxT("; ");
    else s += wxT("style=\" ");

    wxString fontweights [3] = { wxT("normal"), wxT("lighter"), wxT("bold") };
    s += wxT("font-weight:") + fontweights[m_font.GetWeight() - wxNORMAL] + wxT("; ");

    wxString fontstyles [5] = { wxT("normal"), wxT("style error"), wxT("style error"), wxT("italic"), wxT("oblique") };
    s += wxT("font-style:") + fontstyles[m_font.GetStyle() - wxNORMAL] + wxT("; ");

    sTmp.Printf (wxT("font-size:%dpt; "), m_font.GetPointSize() );
    s += sTmp;
    //text will be solid, unless alpha value isn't opaque in the foreground colour
    s += wxBrushString(m_textForegroundColour) + wxPenString(m_textForegroundColour);
    sTmp.Printf ( wxT("stroke-width:0;\"  transform=\"rotate( %s %d %d )  \" >"),  NumStr(-angle), x,y );
    s += sTmp + sText + wxT("</text> ") + wxT("\n");
    if (m_OK)
    {
        write(s);
    }
}

void KicadSVGFileDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
{
    DoDrawRoundedRectangle(x, y, width, height, 0);
}

void KicadSVGFileDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius )

{
    if (m_graphics_changed) NewGraphics();
    wxString s;

    s.Printf ( wxT(" <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" rx=\"%s\" "),
            x, y, width, height, NumStr(radius) );

    s += wxT(" /> \n");
    write(s);

    CalcBoundingBox(x, y);
    CalcBoundingBox(x + width, y + height);
}

void KicadSVGFileDCImpl::DoDrawPolygon(int n, wxPoint points[],
                                    wxCoord xoffset, wxCoord yoffset,
                                    wxPolygonFillMode fillStyle)
{
    if (m_graphics_changed) NewGraphics();
    wxString s, sTmp;
    s = wxT("<polygon style=\"");
    if ( fillStyle == wxODDEVEN_RULE )
        s += wxT("fill-rule:evenodd; ");
    else
        s += wxT("fill-rule:nonzero; ");

    s += wxT("\" \npoints=\"");

    for (int i = 0; i < n;  i++)
    {
        sTmp.Printf ( wxT("%d,%d"), points [i].x+xoffset, points[i].y+yoffset );
        s += sTmp + wxT("\n");
        CalcBoundingBox ( points [i].x+xoffset, points[i].y+yoffset);
    }
    s += wxT("\" /> \n");
    write(s);
}

void KicadSVGFileDCImpl::DoDrawEllipse (wxCoord x, wxCoord y, wxCoord width, wxCoord height)

{
    if (m_graphics_changed) NewGraphics();

    int rh = height /2;
    int rw = width  /2;

    wxString s;
    s.Printf ( wxT("<ellipse cx=\"%d\" cy=\"%d\" rx=\"%d\" ry=\"%d\" "), x+rw,y+rh, rw, rh );
    s += wxT(" /> \n");

    write(s);

    CalcBoundingBox(x, y);
    CalcBoundingBox(x + width, y + height);
}

void KicadSVGFileDCImpl::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc)
{
    /* Draws an arc of a circle, centred on (xc, yc), with starting point
    (x1, y1) and ending at (x2, y2). The current pen is used for the outline
    and the current brush for filling the shape.

    The arc is drawn in an anticlockwise direction from the start point to
    the end point
    */

    if (m_graphics_changed) NewGraphics();
    wxString s;

    // we need the radius of the circle which has two estimates
    double r1 = sqrt ( double( (x1-xc)*(x1-xc) ) + double( (y1-yc)*(y1-yc) ) );
    double r2 = sqrt ( double( (x2-xc)*(x2-xc) ) + double( (y2-yc)*(y2-yc) ) );

    wxASSERT_MSG( (fabs ( r2-r1 ) <= 3), wxT("wxSVGFileDC::DoDrawArc Error in getting radii of circle"));
    if ( fabs ( r2-r1 ) > 3 )    //pixels
    {
        s = wxT("<!--- wxSVGFileDC::DoDrawArc Error in getting radii of circle --> \n");
        write(s);
    }

    double theta1 = atan2((double)(yc-y1),(double)(x1-xc));
    if ( theta1 < 0 ) theta1 = theta1 + M_PI * 2;
    double theta2 = atan2((double)(yc-y2), (double)(x2-xc));
    if ( theta2 < 0 ) theta2 = theta2 + M_PI * 2;
    if ( theta2 < theta1 ) theta2 = theta2 + M_PI *2;

    int fArc;   // flag for large or small arc 0 means less than 180 degrees
    if ( fabs(theta2 - theta1) > M_PI ) fArc = 1; else fArc = 0;

    int fSweep = 0;             // flag for sweep always 0

    // Draw a pie:
    // the z means close the path and fill
    // s.Printf ( wxT("<path d=\"M%d %d A%s %s 0.0 %d %d %d %d L%d %d z "),
    //    x1,y1, NumStr(r1), NumStr(r2), fArc, fSweep, x2, y2, xc, yc );

    // Draw a single arc:
    s.Printf( wxT("<path d=\"M%d %d A%s %s 0.0 %d %d %d %d" ),
        x1,y1, NumStr(r1), NumStr(r2), fArc, fSweep, x2, y2 );

    s += wxT(" \" /> \n");

    if (m_OK)
    {
        write(s);
    }
}

void KicadSVGFileDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
{
    /*
    Draws an arc of an ellipse. The current pen is used for drawing the arc
    and the current brush is used for drawing the pie. This function is
    currently only available for X window and PostScript device contexts.

    x and y specify the x and y coordinates of the upper-left corner of the
    rectangle that contains the ellipse.

    width and height specify the width and height of the rectangle that
    contains the ellipse.

    start and end specify the start and end of the arc relative to the
    three-o'clock position from the center of the rectangle. Angles are
    specified in degrees (360 is a complete circle). Positive values mean
    counter-clockwise motion. If start is equal to end, a complete ellipse
    will be drawn. */

    //known bug: SVG draws with the current pen along the radii, but this does not happen in wxMSW

    if (m_graphics_changed) NewGraphics();

    wxString s;
    //radius
    double rx = w / 2;
    double ry = h / 2;
    // center
    double xc = x + rx;
    double yc = y + ry;

    double xs, ys, xe, ye;
    xs = xc + rx * cos (DegToRad(sa));
    xe = xc + rx * cos (DegToRad(ea));
    ys = yc - ry * sin (DegToRad(sa));
    ye = yc - ry * sin (DegToRad(ea));

    ///now same as circle arc...

    double theta1 = atan2(ys-yc, xs-xc);
    double theta2 = atan2(ye-yc, xe-xc);

    int fArc;                  // flag for large or small arc 0 means less than 180 degrees
    if ( (theta2 - theta1) > 0 ) fArc = 1; else fArc = 0;

    int fSweep;
    if ( fabs(theta2 - theta1) > M_PI) fSweep = 1; else fSweep = 0;

    // Draw a pie:
    // s.Printf ( wxT("<path d=\"M%d %d A%d %d 0.0 %d %d  %d %d L %d %d z "),
    //    int(xs), int(ys), int(rx), int(ry),
     //   fArc, fSweep, int(xe), int(ye), int(xc), int(yc)  );

    // Draw an arc:
    s.Printf ( wxT("<path d=\"M%d %d A%d %d 0.0 %d %d  %d %d"),
        int(xs), int(ys), int(rx), int(ry),
        fArc, fSweep, int(xe), int(ye)  );

    s += wxT(" \" /> \n");

    if (m_OK)
    {
        write(s);
    }
}

void KicadSVGFileDCImpl::DoGetTextExtent(const wxString& string, wxCoord *w, wxCoord *h, wxCoord *descent , wxCoord *externalLeading , const wxFont *font) const

{
    wxScreenDC sDC;

    sDC.SetFont (m_font);
    if ( font != NULL ) sDC.SetFont ( *font );
    sDC.GetTextExtent(string, w,  h, descent, externalLeading );
}

wxCoord KicadSVGFileDCImpl::GetCharHeight() const
{
    wxScreenDC sDC;
    sDC.SetFont (m_font);

    return sDC.GetCharHeight();

}

wxCoord KicadSVGFileDCImpl::GetCharWidth() const
{
    wxScreenDC sDC;
    sDC.SetFont (m_font);

    return sDC.GetCharWidth();
}


// ----------------------------------------------------------
// wxSVGFileDCImpl - set functions
// ----------------------------------------------------------

void KicadSVGFileDCImpl::SetBackground( const wxBrush &brush )
{
    m_backgroundBrush = brush;
}


void KicadSVGFileDCImpl::SetBackgroundMode( int mode )
{
    m_backgroundMode = mode;
}


void KicadSVGFileDCImpl::SetBrush(const wxBrush& brush)

{
    m_brush = brush;

    m_graphics_changed = true;
}


void KicadSVGFileDCImpl::SetPen(const wxPen& pen)
{
    // width, color, ends, joins : currently implemented
    // dashes, stipple :  not implemented
    m_pen = pen;

    m_graphics_changed = true;
}

void KicadSVGFileDCImpl::NewGraphics()
{
    wxString s, sBrush, sPenCap, sPenJoin, sPenStyle, sLast, sWarn;

    sBrush = wxT("</g>\n<g style=\"") + wxBrushString ( m_brush.GetColour(), m_brush.GetStyle() )
            + wxPenString(m_pen.GetColour(), m_pen.GetStyle());

    switch ( m_pen.GetCap() )
    {
        case  wxCAP_PROJECTING :
            sPenCap = wxT("stroke-linecap:square; ");
            break;
        case  wxCAP_BUTT :
            sPenCap = wxT("stroke-linecap:butt; ");
            break;
        case    wxCAP_ROUND :
        default :
            sPenCap = wxT("stroke-linecap:round; ");
    }

    switch ( m_pen.GetJoin() )
    {
        case  wxJOIN_BEVEL :
            sPenJoin = wxT("stroke-linejoin:bevel; ");
            break;
        case  wxJOIN_MITER :
            sPenJoin = wxT("stroke-linejoin:miter; ");
            break;
        case    wxJOIN_ROUND :
        default :
            sPenJoin = wxT("stroke-linejoin:round; ");
    }

    sLast.Printf( wxT("stroke-width:%d\" \n   transform=\"translate(%s %s) scale(%s %s)\">"),
                m_pen.GetWidth(), NumStr(m_logicalOriginX), NumStr(m_logicalOriginY), NumStr(m_scaleX), NumStr(m_scaleY)  );

    s = sBrush + sPenCap + sPenJoin + sPenStyle + sLast + wxT("\n") + sWarn;
    write(s);
    m_graphics_changed = false;
}


void KicadSVGFileDCImpl::SetFont(const wxFont& font)

{
    m_font = font;
}

// export a bitmap as a raster image in png
bool KicadSVGFileDCImpl::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
                        wxDC* source, wxCoord xsrc, wxCoord ysrc,
                        wxRasterOperationMode logicalFunc /*= wxCOPY*/, bool useMask /*= false*/,
                        wxCoord /*xsrcMask = -1*/, wxCoord /*ysrcMask = -1*/)
{
    if (logicalFunc != wxCOPY)
    {
        wxASSERT_MSG(false, wxT("wxSVGFileDC::DoBlit Call requested nonCopy mode; this is not possible"));
        return false;
    }
    if (useMask != false)
    {
        wxASSERT_MSG(false, wxT("wxSVGFileDC::DoBlit Call requested false mask; this is not possible"));
        return false;
    }
    wxBitmap myBitmap (width, height);
    wxMemoryDC memDC;
    memDC.SelectObject( myBitmap );
    memDC.Blit(0, 0, width, height, source, xsrc, ysrc);
    memDC.SelectObject( wxNullBitmap );
    DoDrawBitmap(myBitmap, xdest, ydest);
    return false;
}

void KicadSVGFileDCImpl::DoDrawIcon(const class wxIcon & myIcon, wxCoord x, wxCoord y)
{
    wxBitmap myBitmap (myIcon.GetWidth(), myIcon.GetHeight() );
    wxMemoryDC memDC;
    memDC.SelectObject( myBitmap );
    memDC.DrawIcon(myIcon,0,0);
    memDC.SelectObject( wxNullBitmap );
    DoDrawBitmap(myBitmap, x, y);
}

void KicadSVGFileDCImpl::DoDrawBitmap(const class wxBitmap & bmp, wxCoord x, wxCoord y , bool  WXUNUSED(bTransparent) /*=0*/ )
{
    if (m_graphics_changed) NewGraphics();

    wxString sTmp, s, sPNG;
    if ( wxImage::FindHandler(wxBITMAP_TYPE_PNG) == NULL )
        wxImage::AddHandler(new wxPNGHandler);

// create suitable file name
    sTmp.Printf ( wxT("_image%d.png"), m_sub_images);
    sPNG = m_filename.BeforeLast(wxT('.')) + sTmp;
    while (wxFile::Exists(sPNG) )
    {
        m_sub_images ++;
        sTmp.Printf ( wxT("_image%d.png"), m_sub_images);
        sPNG = m_filename.BeforeLast(wxT('.')) + sTmp;
    }

//create copy of bitmap (wxGTK doesn't like saving a constant bitmap)
    wxBitmap myBitmap = bmp;
//save it
    bool bPNG_OK = myBitmap.SaveFile(sPNG,wxBITMAP_TYPE_PNG);

// reference the bitmap from the SVG doc
// only use filename & ext
    sPNG = sPNG.AfterLast(wxFileName::GetPathSeparator());

// reference the bitmap from the SVG doc
    int w = myBitmap.GetWidth();
    int h = myBitmap.GetHeight();
    sTmp.Printf ( wxT(" <image x=\"%d\" y=\"%d\" width=\"%dpx\" height=\"%dpx\" "), x,y,w,h );
    s += sTmp;
    sTmp.Printf ( wxT(" xlink:href=\"%s\"> \n"), sPNG.c_str() );
    s += sTmp + wxT("<title>Image from wxSVG</title>  </image>") + wxT("\n");

    if (m_OK && bPNG_OK)
    {
        write(s);
    }
    m_OK = m_outfile->IsOk() && bPNG_OK;
}

void KicadSVGFileDCImpl::write(const wxString &s)
{
    const wxCharBuffer buf = s.utf8_str();
    m_outfile->Write(buf, strlen((const char *)buf));
    m_OK = m_outfile->IsOk();
}

#else
679 680 681 682

#define newline    wxString( wxT( "\n" ) )
#define space      wxString( wxT( " " ) )
#define semicolon  wxString( wxT( ";" ) )
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

#ifdef __BORLANDC__
#pragma warn -rch
#pragma warn -ccc
#endif


/* some define not included in early wxWidget versions: */
#ifndef twips2mm
#define twips2mm         0.0176388888889
#endif
#ifndef pt2mm
#define pt2mm            0.352777777778
#endif

698 699 700
static inline double DegToRad( double deg )
{
    return (deg * M_PI) / 180.0;
jean-pierre charras's avatar
jean-pierre charras committed
701
}
702

703
wxString wxColStr( wxColour c )
704
{
705 706 707 708 709
    unsigned char r, g, b;

    r = c.Red();
    g = c.Green();
    b = c.Blue();
710 711

    // possible Unicode bug here
712 713
    wxString s = wxDecToHex( r ) + wxDecToHex( g ) + wxDecToHex( b );
    return s;
714 715 716
}


717
wxString wxBrushString( wxColour c, int style )
718
{
719 720 721
    wxString s = wxT( "fill:#" ) + wxColStr( c ) + semicolon + space;

    switch( style )
722
    {
723 724 725
    case wxSOLID:
        s = s + wxT( "fill-opacity:1.0; " );
        break;
726

727 728 729
    case wxTRANSPARENT:
        s = s + wxT( "fill-opacity:0.0; " );
        break;
730

731
    default:
732
        wxASSERT_MSG( false, wxT( "wxSVGFileDC::Requested Brush Style not available" ) );
733
    }
734 735 736

    s = s + newline;
    return s;
737 738 739 740
}


/***********************************************************************/
741
void wxSVGFileDC::Init( wxString f, int Width, int Height, float dpi )
742
/***********************************************************************/
743

744
/* set up things first  wxDCBase does all this?
745
 */
746
{
747 748
    m_width  = Width;
    m_height = Height;
749

750 751
    m_clipping = false;
    m_OK = true;
752

753 754
    m_mm_to_pix_x = dpi / 25.4;
    m_mm_to_pix_y = dpi / 25.4;
755 756 757 758

    m_signX = m_signY = 1;

    m_userScaleX = m_userScaleY =
759
                       m_deviceOriginX = m_deviceOriginY = 0;
760 761 762

    m_OriginX = m_OriginY = 0;
    m_logicalOriginX = m_logicalOriginY = 0;
763 764
    m_logicalScaleX  = m_logicalScaleY = 0;
    m_scaleX = m_scaleY = 1.0;
765 766

    m_logicalFunction = wxCOPY;
767
    m_backgroundMode  = wxTRANSPARENT;
768 769
    m_mappingMode = wxMM_TEXT;

770
    m_backgroundBrush      = *wxTRANSPARENT_BRUSH;
771 772 773 774 775 776 777 778
    m_textForegroundColour = *wxBLACK;
    m_textBackgroundColour = *wxWHITE;
    m_colour = wxColourDisplay();

    m_pen   = *wxBLACK_PEN;
    m_font  = *wxNORMAL_FONT;
    m_brush = *wxWHITE_BRUSH;

779
    m_graphics_changed = true;
780 781 782

    ////////////////////code here

783 784 785
    m_outfile = new wxFileOutputStream( f );
    m_OK = m_outfile->Ok();
    if( m_OK )
786
    {
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
        m_filename   = f;
        m_sub_images = 0;
        wxString s;
        s = wxT( "<?xml version=\"1.0\" standalone=\"no\"?>" ); s = s + newline;
        write( s );
        s = wxT( "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" " ) + newline;
        write( s );
        s = wxT( "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> " ) + newline;
        write( s );

        s.Printf( wxT( "<svg\n" ) );
        write( s );
        s.Printf( wxT( "  xmlns=\"http://www.w3.org/2000/svg\"\n" ) );
        write( s );
        s.Printf( wxT( "  version=\"1.1\"\n" ) );
        write( s );
jp's avatar
jp committed
803 804
        s.Printf( wxT( "  width=\"%gin\" height=\"%gin\" viewBox=\"0 0 %d %d \"\n" ),
                  double (Width) / dpi, double (Height) / dpi, Width, Height );
805 806 807 808 809 810 811 812 813 814 815 816
        write( s );
        s.Printf( wxT( ">\n" ) );
        write( s );

        s = wxT( "  <title>SVG Picture created as " ) + wxFileNameFromPath( f ) +
            wxT( " </title>" ) + newline;
        write( s );
        s = wxString( wxT( "  <desc>Picture generated by wxSVG " ) ) + wxSVGVersion + wxT(
            " </desc>" ) + newline;
        write( s );
        s = wxT( "  <g style=\"fill:black; stroke:black; stroke-width:1\">" ) + newline;
        write( s );
817 818 819 820 821
    }
}


// constructors
822
wxSVGFileDC::wxSVGFileDC( wxString f )
823 824
{
    // quarter 640x480 screen display at 72 dpi
825
    Init( f, 320, 240, 72.0 );
jean-pierre charras's avatar
jean-pierre charras committed
826
}
827

828
wxSVGFileDC::wxSVGFileDC( wxString f, int Width, int Height )
829
{
830
    Init( f, Width, Height, 72.0 );
jean-pierre charras's avatar
jean-pierre charras committed
831
}
832

833
wxSVGFileDC::wxSVGFileDC( wxString f, int Width, int Height, float dpi )
834
{
835
    Init( f, Width, Height, dpi );
jean-pierre charras's avatar
jean-pierre charras committed
836
}
837 838 839

wxSVGFileDC::~wxSVGFileDC()
{
840 841 842 843
    wxString s = wxT( "</g> \n</svg> \n" );

    write( s );
    delete m_outfile;
844 845 846 847 848
}


//////////////////////////////////////////////////////////////////////////////////////////

849
void wxSVGFileDC::DoDrawLine( wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2 )
850
{
851 852 853 854 855
    if( m_graphics_changed )
        NewGraphics();
    wxString s;
    s.Printf( wxT( "<path d=\"M%d %d L%d %d\" /> \n" ), x1, y1, x2, y2 );
    if( m_OK )
856
    {
857
        write( s );
858
    }
859 860
    CalcBoundingBox( x1, y1 );
    CalcBoundingBox( x2, y2 );
861
    return;
jean-pierre charras's avatar
jean-pierre charras committed
862
}
863

864
void wxSVGFileDC::DoDrawLines( int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset )
865
{
866
    for( int i = 1; i < n; i++ )
867
    {
868 869
        DoDrawLine( points[i - 1].x + xoffset, points[i - 1].y + yoffset,
                    points[ i ].x + xoffset, points[ i ].y + yoffset );
870 871 872 873
    }
}


874
void wxSVGFileDC::DoDrawPoint( wxCoord x1, wxCoord y1 )
875 876
{
    wxString s;
877 878 879 880 881 882 883 884

    if( m_graphics_changed )
        NewGraphics();
    s = wxT( "<g style = \"stroke-linecap:round;\" > " ) + newline;
    write( s );
    DrawLine( x1, y1, x1, y1 );
    s = wxT( "</g>" );
    write( s );
885 886 887
}


888
void wxSVGFileDC::DoDrawCheckMark( wxCoord x1, wxCoord y1, wxCoord width, wxCoord height )
889
{
890
    wxDCBase::DoDrawCheckMark( x1, y1, width, height );
891 892 893
}


894
void wxSVGFileDC::DoDrawText( const wxString& text, wxCoord x1, wxCoord y1 )
895
{
896
    DoDrawRotatedText( text, x1, y1, 0.0 );
897 898 899
}


900
void wxSVGFileDC::DoDrawRotatedText( const wxString& sText, wxCoord x, wxCoord y, double angle )
901 902
{
    //known bug; if the font is drawn in a scaled DC, it will not behave exactly as wxMSW
903 904
    if( m_graphics_changed )
        NewGraphics();
905 906 907
    wxString s, sTmp;

    // calculate bounding box
908 909
    wxCoord  w, h, desc;
    DoGetTextExtent( sText, &w, &h, &desc );
910

911
    double   rad = DegToRad( angle );
912 913

    // wxT("upper left") and wxT("upper right")
914 915
    CalcBoundingBox( x, y );
    CalcBoundingBox( (wxCoord) ( x + w * cos( rad ) ), (wxCoord) ( y - h * sin( rad ) ) );
916 917

    // wxT("bottom left") and wxT("bottom right")
918 919 920 921
    x += (wxCoord) ( h * sin( rad ) );
    y += (wxCoord) ( h * cos( rad ) );
    CalcBoundingBox( x, y );
    CalcBoundingBox( (wxCoord) ( x + h * sin( rad ) ), (wxCoord) ( y + h * cos( rad ) ) );
922

923
    if( m_backgroundMode == wxSOLID )
924 925 926 927
    {
        // draw background first
        // just like DoDrawRectangle except we pass the text color to it and set the border to a 1 pixel wide text background

928 929 930 931 932 933 934 935
        sTmp.Printf( wxT(
                         " <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"  " ), x, y + desc -
                     h, w, h );
        s = sTmp + wxT( "style=\"fill:#" ) + wxColStr( m_textBackgroundColour ) + wxT( "; " );
        s = s + wxT( "stroke-width:1; stroke:#" ) + wxColStr( m_textBackgroundColour ) + wxT( "; " );
        sTmp.Printf( wxT( "\" transform=\"rotate( %.2g %d %d )  \">" ), -angle, x, y );
        s = s + sTmp + newline;
        write( s );
936 937
    }

938 939
    //now do the text itself
    s.Printf( wxT( " <text x=\"%d\" y=\"%d\" " ), x, y );
940

941 942 943 944 945
    sTmp = m_font.GetFaceName();
    if( sTmp.Len() > 0 )
        s = s + wxT( "style=\"font-family:" ) + sTmp + wxT( "; " );
    else
        s = s + wxT( "style=\" " );
946

947 948
    wxString fontweights[3] = { wxT( "normal" ), wxT( "lighter" ), wxT( "bold" ) };
    s = s + wxT( "font-weight:" ) + fontweights[m_font.GetWeight() - wxNORMAL] + semicolon + space;
949

950 951 952 953 954 955 956 957 958 959 960 961 962
    wxString fontstyles[5] = {
        wxT( "normal" ), wxT( "style error" ), wxT( "style error" ), wxT(
            "italic" ),  wxT( "oblique" )
    };
    s = s + wxT( "font-style:" ) + fontstyles[m_font.GetStyle() - wxNORMAL] + semicolon + space;

    sTmp.Printf( wxT( "font-size:%dpt; fill:#" ), m_font.GetPointSize() );
    s = s + sTmp;
    s = s + wxColStr( m_textForegroundColour ) + wxT( "; stroke:#" ) + wxColStr(
        m_textForegroundColour ) + wxT( "; " );
    sTmp.Printf( wxT( "stroke-width:0;\"  transform=\"rotate( %.2g %d %d )  \" >" ), -angle, x, y );
    s = s + sTmp + sText + wxT( "</text> " ) + newline;
    if( m_OK )
963
    {
964
        write( s );
965 966 967 968
    }
}


969
void wxSVGFileDC::DoDrawRectangle( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
970
{
971
    DoDrawRoundedRectangle( x, y, width, height, 0 );
972 973 974
}


975 976 977 978 979
void wxSVGFileDC::DoDrawRoundedRectangle( wxCoord x,
                                          wxCoord y,
                                          wxCoord width,
                                          wxCoord height,
                                          double  radius )
980 981

{
982 983 984
    if( m_graphics_changed )
        NewGraphics();
    wxString s;
985

986 987
    s.Printf( wxT( " <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" rx=\"%.2g\" " ),
              x, y, width, height, radius );
988

989 990
    s = s + wxT( " /> " ) + newline;
    write( s );
991

992 993
    CalcBoundingBox( x, y );
    CalcBoundingBox( x + width, y + height );
994 995 996
}


997 998 999 1000 1001
void wxSVGFileDC::DoDrawPolygon( int     n,
                                 wxPoint points[],
                                 wxCoord xoffset,
                                 wxCoord yoffset,
                                 int     fillStyle )
1002
{
1003 1004 1005 1006 1007 1008
    if( m_graphics_changed )
        NewGraphics();
    wxString s, sTmp;
    s = wxT( "<polygon style=\"" );
    if( fillStyle == wxODDEVEN_RULE )
        s = s + wxT( "fill-rule:evenodd; " );
1009
    else
1010
        s = s + wxT( "fill-rule:nonzero; " );
1011

1012
    s = s + wxT( "\" \npoints=\"" );
1013

1014
    for( int i = 0; i < n;  i++ )
1015
    {
1016 1017 1018
        sTmp.Printf( wxT( "%d,%d" ), points[i].x + xoffset, points[i].y + yoffset );
        s = s + sTmp + newline;
        CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset );
1019 1020
    }

1021 1022 1023
    s = s + wxT( "\" /> " );
    s = s + newline;
    write( s );
1024 1025 1026
}


1027
void wxSVGFileDC::DoDrawEllipse( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
1028 1029

{
1030 1031
    if( m_graphics_changed )
        NewGraphics();
1032

1033 1034
    int      rh = height / 2;
    int      rw = width / 2;
1035 1036

    wxString s;
1037 1038
    s.Printf( wxT( "<ellipse cx=\"%d\" cy=\"%d\" rx=\"%d\" ry=\"%d\" " ), x + rw, y + rh, rw, rh );
    s = s + wxT( " /> " ) + newline;
1039

1040
    write( s );
1041

1042 1043
    CalcBoundingBox( x, y );
    CalcBoundingBox( x + width, y + height );
1044 1045 1046
}


1047 1048 1049 1050 1051 1052
void wxSVGFileDC::DoDrawArc( wxCoord x1,
                             wxCoord y1,
                             wxCoord x2,
                             wxCoord y2,
                             wxCoord xc,
                             wxCoord yc )
1053 1054
{
    /* Draws an arc of a circle, centred on (xc, yc), with starting point
1055 1056 1057 1058 1059 1060 1061 1062 1063
     *  (x1, y1) and ending at (x2, y2). The current pen is used for the outline
     *  and the current brush for filling the shape.
     *
     *  The arc is drawn in an anticlockwise direction from the start point to
     *  the end point. */

    if( m_graphics_changed )
        NewGraphics();
    wxString s;
1064 1065

    // we need the radius of the circle which has two estimates
1066 1067
    double   r1 = sqrt( double ( (x1 - xc) * (x1 - xc) ) + double ( (y1 - yc) * (y1 - yc) ) );
    double   r2 = sqrt( double ( (x2 - xc) * (x2 - xc) ) + double ( (y2 - yc) * (y2 - yc) ) );
1068

1069 1070 1071
    wxASSERT_MSG( (fabs( r2 - r1 ) <= 3),
                 wxT( "wxSVGFileDC::DoDrawArc Error in getting radii of circle" ) );
    if( fabs( r2 - r1 ) > 3 )    //pixels
1072
    {
1073 1074
        s = wxT( "<!--- wxSVGFileDC::DoDrawArc Error in getting radii of circle --> \n" );
        write( s );
1075 1076
    }

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    double theta1 = atan2( (double) (yc - y1), (double) (x1 - xc) );
    if( theta1 < 0 )
        theta1 = theta1 + M_PI * 2;
    double theta2 = atan2( (double) (yc - y2), (double) (x2 - xc) );
    if( theta2 < 0 )
        theta2 = theta2 + M_PI * 2;
    if( theta2 < theta1 )
        theta2 = theta2 + M_PI * 2;

    int fArc;                  // flag for large or small arc 0 means less than 180 degrees
    if( fabs( theta2 - theta1 ) > M_PI )
        fArc = 1;else
        fArc = 0;

    int fSweep;
    if( (theta2 - theta1) > 0 )
        fSweep = 0;else
        fSweep = 1;
    float Axis_rotation = 0.0;

// Draw arc as pie:
1098 1099
//	s.Printf ( wxT("<path d=\"M%d %d A%.2g %.2g 0.0 %d %d %d %d L%d %d z "),
//	the z means close the path and fill (usefull to draw a pie)
1100 1101 1102 1103 1104 1105 1106
//      x1, y1, r1, r2, fArc, fSweep, x2, y2, xc ,yc );

    // Draw a single arc:
    s.Printf( wxT( "<path d=\"M%d %d A%.2g %.2g %g %d %d %d %d" ),
              x1, y1, r1, r2,
              Axis_rotation,
              fArc, fSweep, x2, y2 );
1107

1108
    s = s + wxT( " \" /> " ) + newline;
1109 1110


1111
    if( m_OK )
1112
    {
1113
        write( s );
1114 1115 1116 1117
    }
}


1118 1119 1120 1121 1122 1123
void wxSVGFileDC::DoDrawEllipticArc( wxCoord x,
                                     wxCoord y,
                                     wxCoord w,
                                     wxCoord h,
                                     double  sa,
                                     double  ea )
1124 1125
{
    /*
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
     *  Draws an arc of an ellipse. The current pen is used for drawing the arc
     *  and the current brush is used for drawing the pie. This function is
     *  currently only available for X window and PostScript device contexts.
     *
     *  x and y specify the x and y coordinates of the upper-left corner of the
     *  rectangle that contains the ellipse.
     *
     *  width and height specify the width and height of the rectangle that
     *  contains the ellipse.
     *
     *  start and end specify the start and end of the arc relative to the
     *  three-o'clock position from the center of the rectangle. Angles are
     *  specified in degrees (360 is a complete circle). Positive values mean
     *  counter-clockwise motion. If start is equal to end, a complete ellipse
     *  will be drawn. */
1141 1142 1143

    //known bug: SVG draws with the current pen along the radii, but this does not happen in wxMSW

1144 1145 1146 1147
    if( m_graphics_changed )
        NewGraphics();

    wxString s;
1148 1149

    //radius
1150 1151 1152
    double   rx = w / 2;
    double   ry = h / 2;

1153
    // center
1154 1155
    double   xc = x + rx;
    double   yc = y + ry;
1156

1157 1158 1159 1160 1161 1162 1163 1164
    double   xs, ys, xe, ye;
    xs = xc + rx*   cos( DegToRad (sa) );

    xe = xc + rx*   cos( DegToRad (ea) );

    ys = yc - ry*   sin( DegToRad (sa) );

    ye = yc - ry*   sin( DegToRad (ea) );
1165 1166 1167

    ///now same as circle arc...

1168 1169
    double theta1 = atan2( ys - yc, xs - xc );
    double theta2 = atan2( ye - yc, xe - xc );
1170

1171 1172 1173 1174
    int    fArc;    // flag for large or small arc 0 means less than 180 degrees
    if( fabs( theta2 - theta1 ) > M_PI )
        fArc = 1;else
        fArc = 0;
1175

1176 1177 1178 1179 1180
    int fSweep;
    if( (theta2 - theta1) > 0 )
        fSweep = 0;else
        fSweep = 1;
    float Axis_rotation = 0.0;
1181

1182 1183 1184 1185 1186 1187
    // Draw a single arc:
    s.Printf( wxT( "<path d=\"M%d,%d A%d,%d %g %d %d %d,%d" ),
             int (xs), int (ys),
             int (rx), int (ry),
             Axis_rotation,
             fArc, fSweep, int (xe), int (ye) );
1188 1189


1190
    s = s + wxT( " \" /> " ) + newline;
1191

1192
    if( m_OK )
1193
    {
1194
        write( s );
1195 1196 1197 1198
    }
}


1199 1200 1201 1202 1203 1204
void wxSVGFileDC::DoGetTextExtent( const wxString& string,
                                   wxCoord*        w,
                                   wxCoord*        h,
                                   wxCoord*        descent,
                                   wxCoord*        externalLeading,
                                   wxFont*         font ) const
1205 1206

{
1207
    wxScreenDC sDC;
1208

1209 1210 1211 1212
    sDC.SetFont( m_font );
    if( font != NULL )
        sDC.SetFont( *font );
    sDC.GetTextExtent( string, w, h, descent, externalLeading );
1213 1214 1215 1216 1217 1218
}


wxCoord wxSVGFileDC::GetCharHeight() const

{
1219
    wxScreenDC sDC;
1220

1221
    sDC.SetFont( m_font );
1222

1223
    return sDC.GetCharHeight();
1224 1225 1226 1227 1228
}


wxCoord wxSVGFileDC::GetCharWidth() const
{
1229
    wxScreenDC sDC;
1230

1231
    sDC.SetFont( m_font );
1232

1233
    return sDC.GetCharWidth();
1234 1235 1236 1237
}


/// Set Functions /////////////////////////////////////////////////////////////////
1238
void wxSVGFileDC::SetBackground( const wxBrush& brush )
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
{
    m_backgroundBrush = brush;
    return;
}


void wxSVGFileDC::SetBackgroundMode( int mode )
{
    m_backgroundMode = mode;
    return;
}


1252
void wxSVGFileDC::SetBrush( const wxBrush& brush )
1253 1254

{
1255
    m_brush = brush;
1256

1257
    m_graphics_changed = true;
1258 1259 1260
}


1261
void wxSVGFileDC::SetPen( const wxPen& pen )
1262 1263 1264
{
    // width, color, ends, joins : currently implemented
    // dashes, stipple :  not implemented
1265
    m_pen = pen;
1266

1267
    m_graphics_changed = true;
1268 1269 1270
}


1271 1272 1273 1274
void wxSVGFileDC::NewGraphics()
{
    int      w = m_pen.GetWidth();
    wxColour c = m_pen.GetColour();
1275 1276 1277

    wxString s, sBrush, sPenCap, sPenJoin, sPenStyle, sLast, sWarn;

1278 1279
    sBrush = wxT( "</g>\n<g style=\"" ) + wxBrushString( m_brush.GetColour(), m_brush.GetStyle() )
             + wxT( "  stroke:#" ) + wxColStr( c ) + wxT( "; " );
1280

1281
    switch( m_pen.GetCap() )
1282
    {
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
    case  wxCAP_PROJECTING:
        sPenCap = wxT( "stroke-linecap:square; " );
        break;

    case  wxCAP_BUTT:
        sPenCap = wxT( "stroke-linecap:butt; " );
        break;

    case    wxCAP_ROUND:
    default:
        sPenCap = wxT( "stroke-linecap:round; " );
    }

    ;

    switch( m_pen.GetJoin() )
1299
    {
1300 1301 1302 1303 1304 1305 1306
    case  wxJOIN_BEVEL:
        sPenJoin = wxT( "stroke-linejoin:bevel; " );
        break;

    case  wxJOIN_MITER:
        sPenJoin = wxT( "stroke-linejoin:miter; " );
        break;
1307

1308 1309 1310 1311 1312 1313 1314 1315
    case    wxJOIN_ROUND:
    default:
        sPenJoin = wxT( "stroke-linejoin:round; " );
    }

    ;

    switch( m_pen.GetStyle() )
1316
    {
1317 1318 1319 1320 1321 1322 1323 1324 1325
    case  wxSOLID:
        sPenStyle = wxT( "stroke-opacity:1.0; stroke-opacity:1.0; " );
        break;

    case  wxTRANSPARENT:
        sPenStyle = wxT( "stroke-opacity:0.0; stroke-opacity:0.0; " );
        break;

    default:
1326
        wxASSERT_MSG( false,
1327 1328 1329 1330
                      wxT( "wxSVGFileDC::SetPen Call called to set a Style which is not available" )
                      );
        sWarn = sWarn + wxT(
            "<!--- wxSVGFileDC::SetPen Call called to set a Style which is not available --> \n" );
1331 1332
    }

1333 1334 1335
    sLast.Printf(   wxT(
                        "stroke-width:%d\" \n   transform=\"translate(%.2g %.2g) scale(%.2g %.2g)\">" ),
                    w, m_OriginX, m_OriginY, m_scaleX, m_scaleY  );
1336 1337

    s = sBrush + sPenCap + sPenJoin + sPenStyle + sLast + newline + sWarn;
1338
    write( s );
1339
    m_graphics_changed = false;
1340 1341 1342
}


1343
void wxSVGFileDC::SetFont( const wxFont& font )
1344 1345

{
1346
    m_font = font;
1347 1348 1349 1350 1351
}


void wxSVGFileDC::ComputeScaleAndOrigin()
{
1352 1353 1354 1355
    m_scaleX  = m_logicalScaleX * m_userScaleX;
    m_scaleY  = m_logicalScaleY * m_userScaleY;
    m_OriginX = m_logicalOriginX * m_logicalScaleX + m_deviceOriginX;
    m_OriginY = m_logicalOriginY * m_logicalScaleY + m_deviceOriginY;
1356
    m_graphics_changed = true;
1357 1358 1359 1360 1361
}


int wxSVGFileDC::GetMapMode()
{
1362
    return m_mappingMode;
1363 1364 1365 1366 1367
}


void wxSVGFileDC::SetMapMode( int mode )
{
1368
    switch( mode )
1369
    {
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    case wxMM_TWIPS:
        SetLogicalScale( twips2mm * m_mm_to_pix_x, twips2mm * m_mm_to_pix_y );
        break;

    case wxMM_POINTS:
        SetLogicalScale( pt2mm * m_mm_to_pix_x, pt2mm * m_mm_to_pix_y );
        break;

    case wxMM_METRIC:
        SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
        break;

    case wxMM_LOMETRIC:
        SetLogicalScale( m_mm_to_pix_x / 10.0, m_mm_to_pix_y / 10.0 );
        break;

    default:
    case wxMM_TEXT:
        SetLogicalScale( 1.0, 1.0 );
        break;
1390
    }
1391

1392 1393 1394
    m_mappingMode = mode;

    /*  we don't do this mega optimisation
1395 1396
     *  if (mode != wxMM_TEXT)
     *  {
1397 1398
     *      m_needComputeScaleX = true;
     *      m_needComputeScaleY = true;
1399 1400
     *  }
     */
1401 1402 1403
}


1404
void wxSVGFileDC::GetUserScale( double* x, double* y ) const
1405
{
1406 1407
    *x = m_userScaleX;
    *y = m_userScaleY;
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
}


void wxSVGFileDC::SetUserScale( double x, double y )
{
    // allow negative ? -> no
    m_userScaleX = x;
    m_userScaleY = y;
    ComputeScaleAndOrigin();
}


void wxSVGFileDC::SetLogicalScale( double x, double y )
{
    // allow negative ?
    m_logicalScaleX = x;
    m_logicalScaleY = y;
    ComputeScaleAndOrigin();
}


void wxSVGFileDC::SetLogicalOrigin( wxCoord x, wxCoord y )
{
    // is this still correct ?
    m_logicalOriginX = x * m_signX;
    m_logicalOriginY = y * m_signY;
    ComputeScaleAndOrigin();
}


void wxSVGFileDC::SetDeviceOrigin( wxCoord x, wxCoord y )
{
    // only wxPostScripDC has m_signX = -1,
    m_deviceOriginX = x;
    m_deviceOriginY = y;
    ComputeScaleAndOrigin();
}


void wxSVGFileDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
{
    // only wxPostScripDC has m_signX = -1,
    m_signX = (xLeftRight ?  1 : -1);
    m_signY = (yBottomUp  ? -1 :  1);
    ComputeScaleAndOrigin();
}


// export a bitmap as a raster image in png
1457 1458
bool wxSVGFileDC::DoBlit( wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
                          wxDC* source, wxCoord xsrc, wxCoord ysrc,
1459
                          int logicalFunc /*= wxCOPY*/, bool useMask /*= false*/,
1460
                          wxCoord /*xsrcMask = -1*/, wxCoord /*ysrcMask = -1*/ )
1461
{
1462
    if( logicalFunc != wxCOPY )
1463
    {
1464
        wxASSERT_MSG( false,
1465 1466
                      wxT( "wxSVGFileDC::DoBlit Call requested nonCopy mode; this is not possible" )
                      );
1467
        return false;
1468
    }
1469
    if( useMask != false )
1470
    {
1471
        wxASSERT_MSG( false,
1472 1473
                      wxT( "wxSVGFileDC::DoBlit Call requested False mask ; this is not possible" )
                      );
1474
        return false;
1475
    }
1476
    wxBitmap   myBitmap( width, height );
1477 1478
    wxMemoryDC memDC;
    memDC.SelectObject( myBitmap );
1479
    memDC.Blit( 0, 0, width, height, source, xsrc, ysrc );
1480
    memDC.SelectObject( wxNullBitmap );
1481
    DoDrawBitmap( myBitmap, xdest, ydest );
1482
    return false;
1483 1484 1485
}


1486
void wxSVGFileDC::DoDrawIcon( const class wxIcon& myIcon, wxCoord x, wxCoord y )
1487
{
1488
    wxBitmap   myBitmap( myIcon.GetWidth(), myIcon.GetHeight() );
1489
    wxMemoryDC memDC;
1490

1491
    memDC.SelectObject( myBitmap );
1492
    memDC.DrawIcon( myIcon, 0, 0 );
1493
    memDC.SelectObject( wxNullBitmap );
1494 1495
    DoDrawBitmap( myBitmap, x, y );
    return;
1496 1497 1498
}


1499 1500 1501
void wxSVGFileDC::DoDrawBitmap( const class wxBitmap& bmp,
                                wxCoord               x,
                                wxCoord               y,
1502
                                bool                  bTransparent /*=0*/ )
1503
{
1504 1505
    if( m_graphics_changed )
        NewGraphics();
1506

1507 1508
    wxString sTmp, s, sPNG;
    wxImage::AddHandler( new wxPNGHandler );
1509 1510

// create suitable file name
1511 1512 1513
    sTmp.Printf( wxT( "_image%d.png" ), m_sub_images );
    sPNG = m_filename.BeforeLast( wxT( '.' ) ) + sTmp;
    while( wxFile::Exists( sPNG ) )
1514
    {
1515 1516 1517
        m_sub_images++;
        sTmp.Printf( wxT( "_image%d.png" ), m_sub_images );
        sPNG = m_filename.BeforeLast( wxT( '.' ) ) + sTmp;
1518 1519 1520
    }

//create copy of bitmap (wxGTK doesn't like saving a constant bitmap)
1521 1522
    wxBitmap myBitmap = bmp;

1523
//save it
1524
    bool     bPNG_OK = myBitmap.SaveFile( sPNG, wxBITMAP_TYPE_PNG );
1525 1526

// refrence the bitmap from the SVG doc
1527 1528 1529 1530
    int      w = myBitmap.GetWidth();
    int      h = myBitmap.GetHeight();
    sTmp.Printf( wxT( " <image x=\"%d\" y=\"%d\" width=\"%dpx\" height=\"%dpx\" " ), x, y, w, h );
    s = s + sTmp;
1531
    sTmp.Printf( wxT( " xlink:href=\"%s\"> \n" ), GetChars( sPNG ) );
1532 1533 1534
    s = s + sTmp + wxT( "<title>Image from wxSVG</title>  </image>" ) + newline;

    if( m_OK && bPNG_OK )
1535
    {
1536
        write( s );
1537
    }
1538
    m_OK = m_outfile->Ok() && bPNG_OK;
1539

1540
    return;
1541 1542 1543 1544 1545 1546 1547
}


// ---------------------------------------------------------------------------
// coordinates transformations
// ---------------------------------------------------------------------------

1548
wxCoord wxSVGFileDC::DeviceToLogicalX( wxCoord x ) const
1549
{
1550
    return XDEV2LOG( x );
1551 1552 1553
}


1554
wxCoord wxSVGFileDC::DeviceToLogicalY( wxCoord y ) const
1555
{
1556
    return YDEV2LOG( y );
1557 1558 1559
}


1560
wxCoord wxSVGFileDC::DeviceToLogicalXRel( wxCoord x ) const
1561
{
1562
    return XDEV2LOGREL( x );
1563 1564 1565
}


1566
wxCoord wxSVGFileDC::DeviceToLogicalYRel( wxCoord y ) const
1567
{
1568
    return YDEV2LOGREL( y );
1569 1570 1571
}


1572
wxCoord wxSVGFileDC::LogicalToDeviceX( wxCoord x ) const
1573
{
1574
    return XLOG2DEV( x );
1575 1576 1577
}


1578
wxCoord wxSVGFileDC::LogicalToDeviceY( wxCoord y ) const
1579
{
1580
    return YLOG2DEV( y );
1581 1582 1583
}


1584
wxCoord wxSVGFileDC::LogicalToDeviceXRel( wxCoord x ) const
1585
{
1586
    return XLOG2DEVREL( x );
1587 1588 1589
}


1590
wxCoord wxSVGFileDC::LogicalToDeviceYRel( wxCoord y ) const
1591
{
1592
    return YLOG2DEVREL( y );
1593 1594
}

1595 1596

void wxSVGFileDC::write( const wxString& s )
1597
{
1598 1599 1600
    const wxWX2MBbuf buf = s.mb_str( wxConvUTF8 );

    m_outfile->Write( buf, strlen( (const char*) buf ) );
1601 1602 1603
    m_OK = m_outfile->Ok();
}

1604
#endif // wxCHECK_VERSION