FAUST compiler  0.9.9.6b8
SVGDev.cpp
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003     FAUST compiler
00004     Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005     ---------------------------------------------------------------------
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021 
00022 
00023 
00024 // SVGDev.cpp
00025 
00026 #include "SVGDev.h"
00027 #include "stdio.h"
00028 #include <iostream>
00029 using namespace std;
00030 
00031 extern bool gShadowBlur;
00032 
00033 static char* xmlcode(const char* name, char* name2)
00034 {
00035     int i,j;
00036 
00037     // SUBSTITUTION DES CARACTeRES INTERDITS EN XML
00038 
00039     for (i=0, j=0; (name[i] != 0) && (j < 250); i++) {
00040         switch (name[i]) {
00041             case '<'    : name2[j++] = '&'; name2[j++] = 'l'; name2[j++] = 't'; name2[j++] = ';';   break;
00042             case '>'    : name2[j++] = '&'; name2[j++] = 'g'; name2[j++] = 't'; name2[j++] = ';';   break;
00043             case '\''   : name2[j++] = '&'; name2[j++] = 'a'; name2[j++] = 'p'; name2[j++] = 'o'; name2[j++] = 's'; name2[j++] = ';';   break;
00044             case '"'    : name2[j++] = '&'; name2[j++] = 'q'; name2[j++] = 'u'; name2[j++] = 'o'; name2[j++] = 't'; name2[j++] = ';';   break;
00045             case '&'    : name2[j++] = '&'; name2[j++] = 'a'; name2[j++] = 'm'; name2[j++] = 'p'; name2[j++] = ';';     break;
00046             default     : name2[j++] = name[i];
00047         }
00048     }
00049     name2[j] = 0;
00050 
00051     return name2;
00052 }
00053 
00054 SVGDev::SVGDev(const char* ficName,double largeur, double hauteur)
00055 {
00056     double gScale = 0.5;
00057     if ((fic_repr = fopen(ficName,"w+")) == NULL) {
00058         cout<<"Impossible de creer ou d'ouvrir "<<ficName<<endl;
00059     }
00060 
00061     // representation file:
00062     fprintf(fic_repr,"<?xml version=\"1.0\"?>\n");
00063     // + DTD ...
00064     // viewBox:
00065     fprintf(fic_repr,"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 %f %f\" width=\"%fmm\" height=\"%fmm\" version=\"1.1\">\n", largeur, hauteur, largeur*gScale, hauteur*gScale);
00066 
00067     if (gShadowBlur) {
00068          fprintf(fic_repr,
00069             "<defs>\n"
00070             "   <filter id=\"filter\" filterRes=\"18\" x=\"0\" y=\"0\">\n"
00071             "     <feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"1.55\" result=\"blur\"/>\n"
00072             "     <feOffset in=\"blur\" dx=\"3\" dy=\"3\"/>\n"
00073             "   </filter>\n"
00074             "</defs>\n"
00075             );
00076     }
00077 
00078 }
00079 
00080 SVGDev::~SVGDev()
00081 {
00082     fprintf(fic_repr,"</svg>\n");
00083     fclose(fic_repr);
00084 }
00085 
00086 void SVGDev::rect(double x,double y,double l,double h, const char* color, const char* link)
00087 {
00088     char buf[512];
00089     if (link != 0 && link[0]!=0) {
00090         // open the optional link tag
00091         fprintf(fic_repr,"<a xlink:href=\"%s\">\n", xmlcode(link, buf));
00092     }
00093     // draw the shadow
00094     if (gShadowBlur) {
00095         fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" rx=\"0.1\" ry=\"0.1\" style=\"stroke:none;fill:#aaaaaa;;filter:url(#filter);\"/>\n",x+1,y+1,l,h);
00096     } else {
00097         fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" rx=\"0\" ry=\"0\" style=\"stroke:none;fill:#cccccc;\"/>\n",x+1,y+1,l,h);
00098     }
00099 
00100     // draw the rectangle
00101     fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" rx=\"0\" ry=\"0\" style=\"stroke:none;fill:%s;\"/>\n", x, y, l, h, color);
00102     if (link != 0 && link[0]!=0) {
00103         // close the optional link tag
00104         fprintf(fic_repr,"</a>\n");
00105     }
00106 
00107 }
00108 
00109 //<polygon fill="lightsteelblue" stroke="midnightblue" stroke-width="5"
00110 //    points="350,180 380,180 380,160 410,160 410,180 440,180 440,140 470,140 470,180
00111 //    500,180 500,120 530,120 530,180" />
00112 
00113 void SVGDev::triangle(double x,double y,double l,double h, const char* color, const char* link, bool leftright)
00114 {
00115     char buf[512];
00116     if (link != 0 && link[0]!=0) {
00117         // open the optional link tag
00118         fprintf(fic_repr,"<a xlink:href=\"%s\">\n", xmlcode(link, buf));
00119     }
00120     // draw triangle+circle
00121     float r = 1.5; // circle radius
00122     float x0, x1, x2;
00123     if (leftright) {
00124         x0 = x;
00125         x1 = x+l-2*r;
00126         x2 = x+l-r;
00127     } else {
00128         x0 = x+l;
00129         x1 = x+2*r;
00130         x2 = x+r;
00131     }
00132     fprintf(fic_repr,"<polygon fill=\"%s\" stroke=\"black\" stroke-width=\".25\" points=\"%f,%f %f,%f %f,%f\"/>\n", color, x0,y, x1,y+h/2.0, x0,y+h);
00133     fprintf(fic_repr,"<circle  fill=\"%s\" stroke=\"black\" stroke-width=\".25\" cx=\"%f\" cy=\"%f\" r=\"%f\"/>\n", color, x2, y+h/2.0, r);
00134 }
00135 
00136 void SVGDev::rond(double x,double y,double rayon)
00137 {
00138     fprintf(fic_repr,"<circle cx=\"%f\" cy=\"%f\" r=\"%f\"/>\n",x,y,rayon);
00139 }
00140 
00141 void SVGDev::fleche(double x,double y,double rotation,int sens)
00142 {
00143     double dx = 3;
00144     double dy = 1;
00145 
00146     if(sens == 1)
00147     {
00148         fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"  transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x-dx,y-dy,x,y,rotation,x,y);
00149         fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"  transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x-dx,y+dy,x,y,rotation,x,y);
00150     }
00151     else //for recursion
00152     {
00153         fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"  transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x+dx,y-dy,x,y,rotation,x,y);
00154         fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"  transform=\"rotate(%f,%f,%f)\" style=\"stroke: black; stroke-width:0.25;\"/>\n",x+dx,y+dy,x,y,rotation,x,y);
00155     }
00156 }
00157 
00158 void SVGDev::carre(double x,double y,double cote)
00159 {
00160     fprintf(fic_repr,"<rect x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\" style=\"stroke: black;stroke-width:0.5;fill:none;\"/>\n",x-0.5*cote,y-cote,cote,cote);
00161 }
00162 
00163 void SVGDev::trait(double x1,double y1,double x2,double y2)
00164 {
00165     fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"  style=\"stroke:black; stroke-linecap:round; stroke-width:0.25;\"/>\n",x1,y1,x2,y2);
00166 }
00167 
00168 void SVGDev::dasharray(double x1,double y1,double x2,double y2)
00169 {
00170     fprintf(fic_repr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\"  style=\"stroke: black; stroke-linecap:round; stroke-width:0.25; stroke-dasharray:3,3;\"/>\n",x1,y1,x2,y2);
00171 }
00172 
00173 void SVGDev::text(double x,double y,const char* name, const char* link)
00174 {
00175     char buf[512];
00176     if (link != 0 && link[0]!=0) {
00177         // open the optional link tag
00178         fprintf(fic_repr,"<a xlink:href=\"%s\">\n", xmlcode(link, buf));
00179     }
00180     char name2[256];
00181     fprintf(fic_repr,"<text x=\"%f\" y=\"%f\" font-family=\"Arial\" font-size=\"7\" text-anchor=\"middle\" fill=\"#FFFFFF\">%s</text>\n",x,y+2,xmlcode(name,name2));
00182     if (link != 0 && link[0]!=0) {
00183         // close the optional link tag
00184         fprintf(fic_repr,"</a>\n");
00185     }
00186 }
00187 
00188 void SVGDev::label(double x,double y,const char* name)
00189 {
00190     char name2[256];
00191     fprintf(fic_repr,"<text x=\"%f\" y=\"%f\" font-family=\"Arial\" font-size=\"7\">%s</text>\n",x,y+2,xmlcode(name,name2));
00192 }
00193 
00194 void SVGDev::markSens(double x,double y,int sens)
00195 {
00196     int offset = (sens == 1) ? 2 : -2;
00197     fprintf(fic_repr,"<circle cx=\"%f\" cy=\"%f\" r=\"1\"/>\n", x+offset, y+offset);
00198 }
00199 
00200 void SVGDev::Error(const char* message, const char* reason,int nb_error,double x,double y,double largeur)
00201 {
00202     fprintf(fic_repr,"<text x=\"%f\" y=\"%f\"  textLength=\"%f\" lengthAdjust=\"spacingAndGlyphs\" style=\"stroke: red; stroke-width:0.3; fill:red; text-anchor:middle;\">%d : %s</text>\n",x,y-7,largeur,nb_error,message);
00203     fprintf(fic_repr,"<text x=\"%f\" y=\"%f\"  textLength=\"%f\" lengthAdjust=\"spacingAndGlyphs\" style=\"stroke: red; stroke-width:0.3; fill:none; text-anchor:middle;\">%s</text>\n",x,y+7,largeur,reason);
00204 }
00205 
00206 
00207