home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / gmt_os2.zip / src / pstext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-01  |  10.3 KB  |  305 lines

  1. /*--------------------------------------------------------------------
  2.  *    The GMT-system:    @(#)pstext.c    2.28  5/1/95
  3.  *
  4.  *    Copyright (c) 1991-1995 by P. Wessel and W. H. F. Smith
  5.  *    See README file for copying and redistribution conditions.
  6.  *--------------------------------------------------------------------*/
  7. /*
  8.  * pstext will read (x, y, angle, size, fontno, justify, text) from stdin or file and plot
  9.  * the textstrings at (x,y) on a map using the size/font/justification selected by
  10.  * the user. 
  11.  *
  12.  * Author:    Paul Wessel
  13.  * Date:    21-JAN-1991-1995
  14.  * Version:    2.0 based on old v1.x
  15.  *
  16.  */
  17.  
  18. #include "gmt.h"
  19.  
  20. main(argc, argv)
  21. int argc;
  22. char *argv[]; {
  23.     int i, justify, font, last_font, n_labels = 0, fno, n_files = 0, n_args;
  24.     int text_size, ix, iy, key, use_font[N_FONTS];
  25.     
  26.     double xy[2], west = 0., east = 0.0, south = 0.0, north = 0.0, plot_x, plot_y;
  27.     double angle, dx = 0.05, dy = 0.05, new_z_level = 0.0, z_level = 0.0;
  28.     
  29.     BOOLEAN error = FALSE, nofile = TRUE, box_outline = FALSE, outline = FALSE, set_z = FALSE;
  30.     BOOLEAN solid = TRUE, draw_box = FALSE, list = FALSE, done = FALSE, clip = TRUE;
  31.     
  32.     char text[512], line[512], this_font[80];
  33.     
  34.     FILE *fp = NULL;
  35.     
  36.     struct PEN pen;
  37.     struct FILL txt, box;
  38.     
  39.     pen.r = txt.r = gmtdefs.basemap_frame_rgb[0];
  40.     pen.g = txt.g = gmtdefs.basemap_frame_rgb[1];
  41.     pen.b = txt.b = gmtdefs.basemap_frame_rgb[2];
  42.     box.r = 255 - gmtdefs.basemap_frame_rgb[0];
  43.     box.g = 255 - gmtdefs.basemap_frame_rgb[1];
  44.     box.b = 255 - gmtdefs.basemap_frame_rgb[2];
  45.     pen.width = 1;
  46.  
  47.     argc = gmt_begin (argc, argv);
  48.     
  49.     /* Check and interpret the command line arguments */
  50.     
  51.     for (i = 1; i < argc; i++) {
  52.         if (argv[i][0] == '-') {
  53.             switch(argv[i][1]) {
  54.         
  55.                 /* Common parameters */
  56.             
  57.                 case 'B':
  58.                 case 'H':
  59.                 case 'J':
  60.                 case 'K':
  61.                 case 'O':
  62.                 case 'P':
  63.                 case 'R':
  64.                 case 'U':
  65.                 case 'V':
  66.                 case 'X':
  67.                 case 'x':
  68.                 case 'Y':
  69.                 case 'y':
  70.                 case 'c':
  71.                 case ':':
  72.                 case '\0':
  73.                     error += get_common_args (argv[i], &west, &east, &south, &north);
  74.                     break;
  75.                 
  76.                 /* Supplemental parameters */
  77.             
  78.                 case 'C':
  79.                     sscanf (&argv[i][2], "%lf/%lf", &dx, &dy);
  80.                     break;
  81.                 case 'E':
  82.                     sscanf (&argv[i][2], "%lf/%lf", &z_project.view_azimuth, &z_project.view_elevation);
  83.                     break;
  84.                 case 'F':
  85.                     if (gmt_getrgb (&argv[i][2], &gmtdefs.basemap_frame_rgb[0], &gmtdefs.basemap_frame_rgb[1], &gmtdefs.basemap_frame_rgb[2])) {
  86.                         gmt_pen_syntax ('F');
  87.                         error++;
  88.                     }
  89.                     break;
  90.                 case 'G':
  91.                     if (gmt_getfill (&argv[i][2], &txt)) {
  92.                         gmt_fill_syntax ('G');
  93.                         error++;
  94.                     }
  95.                     solid = TRUE;
  96.                     break;
  97.                 case 'L':
  98.                     list = gmt_quick = TRUE;
  99.                     break;
  100.                 case 'N':    /* Do not clip at border */
  101.                     clip = FALSE;
  102.                     break;
  103.                 case 'S':
  104.                     outline = TRUE;
  105.                     if (gmt_getpen (&argv[i][2], &pen)) {
  106.                         gmt_pen_syntax ('S');
  107.                         error++;
  108.                     }
  109.                     break;
  110.                 case 'W':
  111.                     draw_box = TRUE;
  112.                     if (argv[i][2] && argv[i][2] != 'o' && gmt_getfill (&argv[i][2], &box)) {
  113.                         gmt_fill_syntax ('W');
  114.                         error++;
  115.                     }
  116.                     if (argv[i][strlen(argv[i])-1] == 'o') box_outline = TRUE;
  117.                     break;
  118.                 case 'Z':
  119.                     new_z_level = atof (&argv[i][2]);
  120.                     set_z = TRUE;
  121.                     break;
  122.                     
  123.                 /* Options not recognized */
  124.                         
  125.                 default:
  126.                     error = TRUE;
  127.                     gmt_default_error (argv[i][1]);
  128.                     break;
  129.             }
  130.         }
  131.         else
  132.             n_files++;
  133.     }
  134.     
  135.     if (argc == 1 || gmt_quick) {
  136.         fprintf (stderr,"pstext %s - To plot textstrings on maps\n\n", GMT_VERSION);
  137.         fprintf(stderr,"usage: pstext <txtfile> -J<params> -R<west>/<east>/<south>/<north>\n");
  138.         fprintf (stderr, "    [-B<tickinfo>] [-C<dx>/<dy>] [-Eaz/el] [-F<r/g/b>] [-G<color>] [-H] [-K] [-L] [-N] [-O] [-P] [-S<pen>]\n");
  139.         fprintf (stderr, "    [-U] [-V] [-W<fill>[o]] [-X<x_shift>] [-Y<y_shift>] [-Z<zlevel>] [-c<ncopies>] [-:]\n\n");
  140.         fprintf(stderr,"    Reads (x,y,size,angle,fontno,justify,text) from <txtfile> [or stdin]\n");
  141.         fprintf(stderr,"    Built-in escape sequences:\n");
  142.         fprintf(stderr,"        @~ toggles between current font and Symbol font\n");
  143.         fprintf(stderr,"        @%%<no>%% switches to font number <no>; @%%%% resets font\n");
  144.         fprintf(stderr,"        @+ toggles between normal and superscript mode\n");
  145.         fprintf(stderr,"        @- toggles between normal and subscript mode\n");
  146.         fprintf(stderr,"        @# toggles between normal and Small Caps mode\n");
  147.         fprintf(stderr,"        @!<char1><char2> makes one composite character\n");
  148.         fprintf(stderr,"        @@ prints the @ sign itself\n");
  149.         fprintf(stderr,"        @e, @o, @a, @E, @O, @A give the accented Scandinavian characters\n");
  150.         fprintf(stderr,"       (See manual page for more information)\n");
  151.         
  152.         if (list) {    /* List fonts */
  153.             fprintf (stderr, "\n\tFont #    Font Name\n");
  154.             fprintf (stderr, "\t------------------------------------\n");
  155.             for (i = 0; i < N_FONTS; i++)
  156.                 fprintf (stderr, "\t%3d\t%s\n", i, font_name[i]);
  157.         }
  158.             
  159.         if (gmt_quick) exit(-1);
  160.         
  161.         explain_option ('j');
  162.         explain_option ('R');
  163.         fprintf(stderr,"\n\tOPTIONS:\n");
  164.         explain_option ('b');
  165.         fprintf(stderr,"    -C sets the clearance between characters and surrounding box [0.05/0.05]\n");
  166.         fprintf(stderr,"       only used if -W has been set\n");
  167.         fprintf (stderr, "    -E set azimuth and elevation of viewpoint for 3-D perspective [180/90]\n");
  168.         fprintf (stderr, "    -F Set color used for Frame and anotation [%d/%d/%d]\n",
  169.             gmtdefs.basemap_frame_rgb[0], gmtdefs.basemap_frame_rgb[1], gmtdefs.basemap_frame_rgb[2]);
  170.         fprintf (stderr, "    -G set the color (red/green/blue (0-255)) for solid text [0/0/0]\n");
  171.         explain_option ('H');
  172.         explain_option ('K');
  173.         fprintf (stderr, "    -N Do Not clip text that exceeds the map boundaries [Default will clip]\n");
  174.         explain_option ('O');
  175.         explain_option ('P');
  176.         fprintf (stderr, "    -S draw outline of characters.  Append pen attributes\n");
  177.         explain_option ('U');
  178.         explain_option ('V');
  179.         fprintf (stderr, "    -W paints a colored rectangle underneath the text.  [Default is no rectangle]\n");
  180.         fprintf (stderr, "       Append colors [255/255/255].   To draw outline, append o [No outline]\n");
  181.         explain_option ('X');
  182.         fprintf (stderr, "      -Z For 3-D plots: Set the z-level of map [0]\n");
  183.         explain_option ('c');
  184.         explain_option (':');
  185.         explain_option ('.');
  186.         exit(-1);
  187.     }
  188.  
  189.     /* Check that the options selected are mutually consistant */
  190.     
  191.     if (dx < 0.0 || dy < 0.0) {
  192.         fprintf (stderr, "%s: GMT SYNTAX ERROR -C option:  clearances cannot be negative!\n", gmt_program);
  193.         error++;
  194.     }
  195.     if (z_project.view_azimuth > 360.0 || z_project.view_elevation <= 0.0 || z_project.view_elevation > 90.0) {
  196.         fprintf (stderr, "%s: GMT SYNTAX ERROR -E option:  Enter azimuth in 0-360 range, elevation in 0-90 range\n", gmt_program);
  197.         error++;
  198.     }
  199.     if (!project_info.region_supplied) {
  200.         fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify -R option\n", gmt_program);
  201.         error++;
  202.     }
  203.     
  204.     if (error) exit (-1);
  205.  
  206.     if (n_files > 0)
  207.         nofile = FALSE;
  208.     else
  209.         n_files = 1;
  210.     n_args = (argc > 1) ? argc : 2;
  211.     
  212.     map_setup (west, east, south, north);
  213.     
  214.     ps_plotinit (CNULL, gmtdefs.overlay, gmtdefs.page_orientation, gmtdefs.x_origin, gmtdefs.y_origin,
  215.         gmtdefs.global_x_scale, gmtdefs.global_y_scale, gmtdefs.n_copies,
  216.         gmtdefs.dpi, gmtdefs.measure_unit, gmtdefs.paper_width, gmtdefs.page_rgb, gmt_epsinfo (argv[0]));
  217.         
  218.     echo_command (argc, argv);
  219.     if (gmtdefs.unix_time) timestamp (argc, argv);
  220.     if (project_info.three_D) ps_transrotate (-z_project.xmin, -z_project.ymin, 0.0);
  221.     
  222.     if (set_z) {
  223.         project_info.z_level = new_z_level;
  224.         z_to_zz (new_z_level, &z_level);
  225.     }
  226.     ps_setline (pen.width);
  227.  
  228.     if (clip) map_clip_on (-1, -1, -1, 3);
  229.     
  230.     last_font = 0;
  231.     ix = (gmtdefs.xy_toggle);    iy = 1 - ix;
  232.     
  233.     /* if (draw_box && project_info.three_D) draw_box = FALSE; */    /* Not implemented yet */
  234.     
  235.     /* Mark used fonts */
  236.     
  237.     if (gmtdefs.want_euro_font) {
  238.         memset ((char *)use_font, 0, N_FONTS * sizeof (int));
  239.         if (gmtdefs.unix_time) use_font[0] = use_font[1] = TRUE;
  240.         use_font[gmtdefs.anot_font] = TRUE;
  241.         if (frame_info.header[0]) use_font[gmtdefs.header_font] = TRUE;
  242.         if (frame_info.label[0][0] || frame_info.label[1][0] || frame_info.label[2][0]) use_font[gmtdefs.label_font] = TRUE;
  243.     }
  244.     
  245.     for (fno = 1; !done && fno < n_args; fno++) {    /* Loop over all input files */
  246.         if (!nofile && argv[fno][0] == '-') continue;
  247.         if (nofile) {
  248.             fp = stdin;
  249.             done = TRUE;
  250.         }
  251.         else if ((fp = fopen (argv[fno], "r")) == NULL) {
  252.             fprintf (stderr, "psxy: Cannot open file %s\n", argv[fno]);
  253.             continue;
  254.         }
  255.  
  256.         if (!nofile && gmtdefs.verbose) fprintf (stderr, "pstext: Working on file %s\n", argv[fno]);
  257.         if (gmtdefs.io_header) for (i = 0; i < gmtdefs.n_header_recs; i++) fgets (line, 512, fp);
  258.         while (fgets (line, 512, fp)) {
  259.             sscanf (line, "%lf %lf %d %lf %s %d %[^\n]\n", &xy[ix], &xy[iy], &text_size, &angle, this_font, &justify, text);
  260.             n_labels++;
  261.             geo_to_xy (xy[0], xy[1], &plot_x, &plot_y);
  262.             
  263.             if (this_font[0] == '-') {
  264.                 key = get_entry (&this_font[1], font_name, N_FONTS);
  265.                 if (key < N_FONTS) font = key;
  266.             }
  267.             else
  268.                 font = atoi (this_font);
  269.                 
  270.             if (font != last_font) {
  271.                 ps_setfont (font);
  272.                 last_font = font;
  273.             }
  274.             if (!use_font[font] && gmtdefs.want_euro_font) {    /* Must reencode this font */
  275.                 ps_def_euro_font (font_name[font]);
  276.                 use_font[font] = TRUE;
  277.             }
  278.             if (draw_box) gmt_textbox3d (plot_x, plot_y, z_level, text_size, font, text, angle, justify, box_outline, dx, dy, box.r, box.g, box.b);
  279.             if (solid) {
  280.                 ps_setpaint (txt.r, txt.g, txt.b);
  281.                 gmt_text3d (plot_x, plot_y, z_level, text_size, font, text, angle, justify, FALSE);
  282.             }
  283.             if (outline) {
  284.                 ps_setpaint (pen.r, pen.g, pen.b);
  285.                 gmt_text3d (plot_x, plot_y, z_level, text_size, font, text, angle, justify, TRUE);
  286.             }
  287.         }
  288.         if (fp != stdin) fclose (fp);
  289.     }
  290.     
  291.         if (clip) map_clip_off (); 
  292.  
  293.     if (frame_info.plot) {
  294.         ps_setpaint (gmtdefs.basemap_frame_rgb[0], gmtdefs.basemap_frame_rgb[1], gmtdefs.basemap_frame_rgb[2]);
  295.         map_basemap ();
  296.     }
  297.     ps_setpaint (gmtdefs.background_rgb[0], gmtdefs.background_rgb[1], gmtdefs.background_rgb[2]);
  298.         
  299.     if (project_info.three_D) ps_rotatetrans (z_project.xmin, z_project.ymin, 0.0);
  300.     ps_plotend (gmtdefs.last_page);
  301.     if (gmtdefs.verbose) fprintf (stderr, "pstext: Plotted %d text-strings\n", n_labels);
  302.     
  303.     gmt_end (argc, argv);
  304. }
  305.