Changeset bef66af in git


Ignore:
Timestamp:
28/09/04 03:48:38 (15 years ago)
Author:
Olly Betts <olly@…>
Branches:
add-show-splays, line_contents, master, stereo, svn/1.0, svn/github/master, svn/origin/master, svn/tags/1.0.40, svn/tags/1.1.14, svn/tags/1.1.15, svn/tags/1.1.16, svn/tags/1.2.0, svn/tags/1.2.1, svn/tags/1.2.2, svn/tags/1.2.3, svn/tags/1.2.4, svn/tags/1.2.5, svn/tags/v1_0_32, svn/tags/v1_0_33, svn/tags/v1_0_34, svn/tags/v1_0_35, svn/tags/v1_0_36, svn/tags/v1_0_37, svn/tags/v1_0_38, svn/tags/v1_0_39, svn/trunk, travis-osx
Children:
d8a725c
Parents:
0be7a88
Message:

Added SVG support from John Pybus

git-svn-id: file:///home/survex-svn/survex/trunk@2711 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/cad3d.c

    r0be7a88 rbef66af  
    55
    66/* Copyright (C) 1994-2004 Olly Betts
     7 * Copyright (C) 2004 John Pybus (SVG Output code)
    78 *
    89 * This program is free software; you can redistribute it and/or modify
     
    5051#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
    5152
     53#define SQRT_2          1.41421356237309504880168872420969
     54
     55#define LEGS 1
     56#define STNS 2
     57#define LABELS 4
     58
    5259/* default to DXF */
    5360#define FMT_DEFAULT FMT_DXF
     
    6370static double scale = 500.0;
    6471static double factor;
     72static const char *unit = "mm";
    6573
    6674static img *pimg;
     
    334342
    335343static void
     344set_name(const img_point *p, const char *s)
     345{
     346   int hash;
     347   point *pt;
     348   union {
     349      char data[sizeof(int) * 3];
     350      int x[3];
     351   } u;
     352
     353   u.x[0] = (int)(p->x * 100);
     354   u.x[1] = (int)(p->y * 100);
     355   u.x[2] = (int)(p->z * 100);
     356   hash = (hash_data(u.data, sizeof(int) * 3) & (HTAB_SIZE - 1));
     357   for (pt = htab[hash]; pt; pt = pt->next) {
     358      if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z) {
     359         /* already got name for these coordinates */
     360         /* FIXME: what about multiple names for the same station? */
     361         return;
     362      }
     363   }
     364
     365   pt = osnew(point);
     366   pt->label = osstrdup(s);
     367   pt->p = *p;
     368   pt->next = htab[hash];
     369   htab[hash] = pt;
     370
     371   return;
     372}
     373
     374static const char *
     375find_name(const img_point *p)
     376{
     377   int hash;
     378   point *pt;
     379   union {
     380      char data[sizeof(int) * 3];
     381      int x[3];
     382   } u;
     383   SVX_ASSERT(p);
     384
     385   u.x[0] = (int)(p->x * 100);
     386   u.x[1] = (int)(p->y * 100);
     387   u.x[2] = (int)(p->z * 100);
     388   hash = (hash_data(u.data, sizeof(int) * 3) & (HTAB_SIZE - 1));
     389   for (pt = htab[hash]; pt; pt = pt->next) {
     390      if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z)
     391         return pt->label;
     392   }
     393   return "?";
     394}
     395
     396static void
     397svg_header(void)
     398{
     399   size_t i;
     400   htab = osmalloc(HTAB_SIZE * ossizeof(point *));
     401   for (i = 0; i < HTAB_SIZE; ++i) htab[i] = NULL;
     402   fprintf(fh, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
     403   fprintf(fh, "<svg width=\"%.3f%s\" height=\"%.3f%s\""
     404               " viewBox=\"0 0 %0.3f %0.3f\">\n",
     405           (max_x - min_x) * factor, unit, (max_y - min_y) * factor, unit,
     406           (max_x - min_x) * factor, (max_y - min_y) * factor );
     407   fprintf(fh, "<g transform=\"translate(%.3f %.3f)\">\n",
     408           min_x * -factor, max_y * factor);
     409}
     410
     411static bool to_close = 0;
     412static bool close_g = 0;
     413
     414static void
     415svg_start_pass(int layer)
     416{
     417   if (to_close) {
     418      fprintf(fh, "\"/>\n");
     419      to_close = 0;
     420   }
     421   if (close_g) {
     422      fprintf(fh, "</g>\n");
     423   }
     424   close_g = 1;
     425
     426   fprintf(fh, "<g id=\"%s\"", layer_names[layer]);
     427   if (layer & LEGS)
     428      fprintf(fh, " style=\"stroke:black;fill:none;stroke-width:0.4\"");
     429   else if (layer & STNS)
     430      fprintf(fh, " style=\"stroke:black;fill:none;stroke-width:0.05\"");
     431   else if (layer & LABELS)
     432      fprintf(fh, " style=\"font-size:%.3f\"", text_height);
     433   fprintf(fh, ">\n");
     434}
     435
     436static void
     437svg_move(const img_point *p)
     438{
     439   if (to_close) {
     440      fprintf(fh, "\"/>\n");
     441   }
     442   fprintf(fh, "<path d=\"M %.3f %.3f", p->x * factor, p->y * -factor);
     443   to_close = 1;
     444}
     445
     446static void
     447svg_line(const img_point *p1, const img_point *p, bool fSurface)
     448{
     449   fSurface = fSurface; /* unused */
     450   p1 = p1; /* unused */
     451   fprintf(fh, "L%.3f %.3f", p->x * factor, p->y * -factor);
     452   to_close = 1;
     453}
     454
     455static void
     456svg_label(const img_point *p, const char *s, bool fSurface)
     457{
     458   fSurface = fSurface; /* unused */
     459   fprintf(fh, "<text transform=\"translate(%.3f %.3f)\">",
     460           p->x * factor, p->y * -factor);
     461   fprintf(fh, s);
     462   fprintf(fh, "</text>\n");
     463   set_name(p, s);
     464}
     465
     466static void
     467svg_cross(const img_point *p, bool fSurface)
     468{
     469   fSurface = fSurface; /* unused */
     470   fprintf(fh, "<circle id=\"%s\" cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\"/>\n",
     471           find_name(p), p->x * factor, p->y * -factor, MARKER_SIZE * SQRT_2);
     472   fprintf(fh, "<path d=\"M%.3f %.3fL%.3f %.3fM%.3f %.3fL%.3f %.3f\"/>\n",
     473           p->x * factor - MARKER_SIZE, p->y * -factor - MARKER_SIZE,
     474           p->x * factor + MARKER_SIZE, p->y * -factor + MARKER_SIZE,
     475           p->x * factor + MARKER_SIZE, p->y * -factor - MARKER_SIZE,
     476           p->x * factor - MARKER_SIZE, p->y * -factor + MARKER_SIZE );
     477}
     478
     479static void
     480svg_footer(void) ////
     481{
     482   if (to_close) {
     483      fprintf(fh, "\"/>\n");
     484      to_close = 0;
     485   }
     486   fprintf(fh, "</g>\n");
     487   fprintf(fh, "</g>\n</svg>");
     488}
     489
     490static void
    336491plt_header(void)
    337492{
     
    354509
    355510static void
    356 set_name(const img_point *p, const char *s)
    357 {
    358    int hash;
    359    point *pt;
    360    union {
    361       char data[sizeof(int) * 3];
    362       int x[3];
    363    } u;
    364 
    365    u.x[0] = (int)(p->x * 100);
    366    u.x[1] = (int)(p->y * 100);
    367    u.x[2] = (int)(p->z * 100);
    368    hash = (hash_data(u.data, sizeof(int) * 3) & (HTAB_SIZE - 1));
    369    for (pt = htab[hash]; pt; pt = pt->next) {
    370       if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z) {
    371          /* already got name for these coordinates */
    372          /* FIXME: what about multiple names for the same station? */
    373          return;
    374       }
    375    }
    376 
    377    pt = osnew(point);
    378    pt->label = osstrdup(s);
    379    pt->p = *p;
    380    pt->next = htab[hash];
    381    htab[hash] = pt;
    382 
    383    return;
    384 }
    385 
    386 static const char *
    387 find_name(const img_point *p)
    388 {
    389    int hash;
    390    point *pt;
    391    union {
    392       char data[sizeof(int) * 3];
    393       int x[3];
    394    } u;
    395    SVX_ASSERT(p);
    396 
    397    u.x[0] = (int)(p->x * 100);
    398    u.x[1] = (int)(p->y * 100);
    399    u.x[2] = (int)(p->z * 100);
    400    hash = (hash_data(u.data, sizeof(int) * 3) & (HTAB_SIZE - 1));
    401    for (pt = htab[hash]; pt; pt = pt->next) {
    402       if (pt->p.x == p->x && pt->p.y == p->y && pt->p.z == p->z)
    403          return pt->label;
    404    }
    405    return "?";
    406 }
    407 
    408 static void
    409511plt_move(const img_point *p)
    410512{
     
    459561}
    460562
    461 #define LEGS 1
    462 #define STNS 2
    463 #define LABELS 4
    464563static int dxf_passes[] = { LEGS|STNS|LABELS, 0 };
    465564static int sketch_passes[] = { LEGS, STNS, LABELS, 0 };
    466565static int plt_passes[] = { LABELS, LEGS, 0 };
     566static int svg_passes[] = { LEGS, LABELS, STNS, 0 };
    467567
    468568int
     
    477577   double elev_angle = 0;
    478578   double s = 0, c = 0;
    479    enum { FMT_DXF = 0, FMT_SKETCH, FMT_PLT, FMT_AUTO } format;
    480    static const char *extensions[] = { "dxf", "sk", "plt" };
     579   enum { FMT_DXF = 0, FMT_SKETCH, FMT_PLT, FMT_SVG, FMT_AUTO } format;
     580   static const char *extensions[] = { "dxf", "sk", "plt", "svg" };
    481581   int *pass;
    482582
     
    505605        {"sketch", no_argument, 0, 'S'},
    506606        {"plt", no_argument, 0, 'P'},
     607        {"svg", no_argument, 0, 'V'},
    507608        {"help", no_argument, 0, HLP_HELP},
    508609        {"version", no_argument, 0, HLP_VERSION},
     
    510611   };
    511612
    512 #define short_opts "s:cnlg::t:m:e:r:DSP"
     613#define short_opts "s:cnlg::t:m:e:r:DSPV"
    513614
    514615   /* TRANSLATE */
     
    526627        {HLP_ENCODELONG(10), "produce Sketch output"},
    527628        {HLP_ENCODELONG(11), "produce Compass PLT output for Carto"},
     629        {HLP_ENCODELONG(12), "produce SVG output"},
    528630        {0,0}
    529631   };
     
    589691         format = FMT_PLT;
    590692         break;
     693       case 'V':
     694         format = FMT_SVG;
     695         break;
    591696       case 's':
    592697         survey = optarg;
     
    655760      footer = plt_footer;
    656761      pass = plt_passes;
     762      mode = "wb"; /* Binary file output */
     763      break;
     764    case FMT_SVG:
     765      header = svg_header;
     766      start_pass = svg_start_pass;
     767      move = svg_move;
     768      line = svg_line;
     769      label = svg_label;
     770      cross = svg_cross;
     771      footer = svg_footer;
     772      pass = svg_passes;
     773      factor = 1000.0 / scale;
    657774      mode = "wb"; /* Binary file output */
    658775      break;
Note: See TracChangeset for help on using the changeset viewer.