File: text.cc

package info (click to toggle)
xplanet 0.43-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 904 kB
  • ctags: 321
  • sloc: cpp: 3,041; sh: 1,977; makefile: 141
file content (319 lines) | stat: -rw-r--r-- 8,758 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
190
191
192
193
194
195
196
197
198
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
/******************************************************************************
    Xplanet 0.43 - render an image of the earth into an X window
    Copyright (C) 1999 Hari Nair <hari@alumni.caltech.edu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

******************************************************************************/

/*
  text.cc - code for drawing labels and markers.  Most of this is adapted 
  from xearth by Kirk Johnson.
*/

#include <fstream.h>
#include <iostream.h>
#include <math.h>
#include <stdio.h>

#include "image.h"
#include "location.h"
#include "options.h"
#include "planet.h"
#include "text.h"
#include "util.h"
#include "xplanet.h"

static XFontStruct *font;

static Pixmap p;
static GC gc;
static Pixel fg, bg;

int
init_text (Pixmap pixmap)
{
  p = pixmap;
  gc = XCreateGC (disp, window, 0, NULL);

  bg = BlackPixel(disp, screen_num );
  Colormap cmap = DefaultColormap(disp, screen_num);
  XColor xc;      
  
  char *defaultcolor = "red";
  if (opts.color == NULL) opts.color = defaultcolor;

  if (XAllocNamedColor(disp, cmap, opts.color, &xc, &xc) != 0) 
    fg = xc.pixel;

  char *defaultfont = "variable";
  if (opts.font == NULL) opts.font = defaultfont;

  font = XLoadQueryFont (disp, opts.font);
  if (font == NULL)
    {
      cerr << "Can't load font " << opts.font 
	   << ", using default font\n";
      font = XQueryFont(disp, XGContextFromGC(gc));
      if (font == NULL)
	    {
	      cerr << "Can't load any fonts at all\n";
	      return (0);
	    }
    }

  XSetFont (disp, gc, font->fid);
  return (1);
}

void 
draw_outlined_string (int x, int y, char *text)
{
  int len = strlen (text);
  XSetForeground (disp, gc, bg);
  XDrawString(disp, p, gc, x+1, y, text, len);
  XDrawString(disp, p, gc, x-1, y, text, len);
  XDrawString(disp, p, gc, x, y+1, text, len);
  XDrawString(disp, p, gc, x, y-1, text, len);
  XSetForeground(disp, gc, fg);
  XDrawString(disp, p, gc, x, y, text, len);
}

void 
draw_label ()
{
  float sunlatdeg, sunlondeg;
  float obslatdeg, obslondeg;

  if (flipped) 
    {
      sunlon *= -1;
      opts.obslon *= -1;
    }

  sunlondeg = sunlon / deg_to_rad;
  if (sunlondeg > 180) sunlondeg -= 360;
  else if (sunlondeg < -180) sunlondeg += 360;
  sunlatdeg = sunlat / deg_to_rad;
  obslondeg = opts.obslon / deg_to_rad;
  if (obslondeg > 180) obslondeg -= 360;
  else if (obslondeg < -180) obslondeg += 360;
  obslatdeg = opts.obslat / deg_to_rad;

  int x, y;
  int dy = font->ascent + font->descent + 1;

  if (opts.label_mask & XNegative) opts.label_x += window_width;

  if (opts.label_mask & YNegative)
    {
      opts.label_y += (window_height - font->descent);
      if (opts.mode == MERCATOR) 
	opts.label_y -= dy;      // Move up 2 text heights
      else
	opts.label_y -= 3 * dy;  // Move up 4 text heights
    }
  else
    opts.label_y += font->ascent;

  y = opts.label_y;

  char label[64];
  int         direction;
  int         ascent;
  int         descent;
  XCharStruct extents;

  // Write the local time
  strftime (label, sizeof(label), "%a %d %b %Y %H:%M %Z", 
	    localtime (&opts.time.tv_sec));
  XTextExtents(font, label, strlen(label), &direction, &ascent, 
	       &descent, &extents);
  if (opts.label_mask & XNegative) x = opts.label_x - extents.rbearing;
  else x = opts.label_x - extents.lbearing;
  draw_outlined_string (x, y, label);
  y += dy;

  // Write the sun's position
  if (opts.body == 0 || opts.body == 3)
    sprintf (label, "sun %.1f %c %.1f %c",
	     fabs(sunlatdeg), ((sunlatdeg < 0) ? 'S' : 'N'),
	     fabs(sunlondeg), ((sunlondeg < 0) ? 'W' : 'E'));
  else
    {
      if (sunlondeg < 0) sunlondeg += 360;
      sprintf (label, "sun %.1f %c %.1f",
	       fabs(sunlatdeg), ((sunlatdeg < 0) ? 'S' : 'N'),
	       sunlondeg);
    }

  XTextExtents(font, label, strlen(label), &direction, &ascent, 
	       &descent, &extents);
  if (opts.label_mask & XNegative) x = opts.label_x - extents.rbearing;
  else x = opts.label_x - extents.lbearing;
  draw_outlined_string (x, y, label);
  y += dy;

  if (opts.mode == ORTHOGRAPHIC)
    {
      // Write the observer's position
      if (opts.body == 0 || opts.body == 3)
	sprintf (label, "obs %.1f %c %.1f %c",
		 fabs(obslatdeg), ((obslatdeg < 0) ? 'S' : 'N'),
		 fabs(obslondeg), ((obslondeg < 0) ? 'W' : 'E'));
      else
	{
	  if (obslondeg < 0) obslondeg += 360;
	  sprintf (label, "obs %.1f %c %.1f",
		   fabs(obslatdeg), ((obslatdeg < 0) ? 'S' : 'N'),
		   obslondeg);
	}
      
      XTextExtents(font, label, strlen(label), &direction, &ascent, 
		   &descent, &extents);
      if (opts.label_mask & XNegative) x = opts.label_x - extents.rbearing;
      else x = opts.label_x - extents.lbearing;
      draw_outlined_string (x, y, label);
      y += dy;

      // Write percent illumination
      location l1 (sunlatdeg, sunlondeg, 1, NULL);
      location l2 (obslatdeg, obslondeg, 1, NULL);
      l1.spher_to_rect ();
      l2.spher_to_rect ();
      // Find the angle between the two vectors
      float phase = l1.dot(l2);
      phase = 50 * (1 + phase);

      sprintf (label, "illumination %.1f %%", phase);
      XTextExtents(font, label, strlen(label), &direction, &ascent, 
		   &descent, &extents);
      if (opts.label_mask & XNegative) x = opts.label_x - extents.rbearing;
      else x = opts.label_x - extents.lbearing;
      draw_outlined_string (x, y, label);
    }
}

void 
draw_marker (location m)
{
  int ilon, ilat;

  int         direction;
  int         ascent;
  int         descent;
  XCharStruct extents;

  if (opts.mode == MERCATOR) 
    {
      ilon = int ((m.lon + M_PI)/(TWO_PI) * (image_width - 1));
      ilat = int ((M_PI_2 - m.lat)/(M_PI) * (image_height - 1));
    }
  else
    {
      // Check if we're on the far side of the globe
      if (m.x < 0) return;    

      ilon = opts.centx + int (m.y * opts.radius);
      ilat = opts.centy - int (m.z * opts.radius);
    }

  XSetForeground(disp, gc, bg);
  XDrawArc(disp, p, gc, ilon-3, ilat-3, 6, 6, 0, 360*64);
  XDrawArc(disp, p, gc, ilon-1, ilat-1, 2, 2, 0, 360*64);
  XSetForeground(disp, gc, fg);
  XDrawArc(disp, p, gc, ilon-2, ilat-2, 4, 4, 0, 360*64);

  XTextExtents(font, m.name, strlen(m.name), 
	       &direction, &ascent, &descent, &extents);

  ilon += (extents.lbearing + 3);
  ilat += (font->ascent + font->descent) / 3;

  draw_outlined_string (ilon, ilat, m.name);
}

int 
delimiter (char c)
{
  return (c == ' ' || c == '\t' || c == ',' || c == '/');
}

void 
read_markers ()
{
  if (opts.markerfile == NULL) 
    {
      opts.markerfile = new char [300];
      strcpy (opts.markerfile, prefix);
      strcat (opts.markerfile, current_body);
      strcat (opts.markerfile, defaultmarkerfile);
    }

  ifstream infile;
  infile.open (opts.markerfile);

  if (infile.bad()) 
    {
      cerr << "Can't open marker file " << opts.markerfile << endl;
      return;
    }
  
  double rot[3][3];
  if (flipped) opts.obslon *= -1;
  rotate_zyx (rot, -opts.rotate, 
	      -opts.obslat, opts.obslon);

  int len=200;
  char *line = new char[len];
  while (infile.getline (line, len, '\n') != NULL)
    {
      for(int i = 0; i < len; i++)
	{
	  while (delimiter (line[i])) i++;
	  if (line[i] == '#' || line[i] == '\0') break;
	  double lat, lon;
	  int istart = i;
	  sscanf (line + i, "%lf", &lat);
	  while (!delimiter (line[i])) i++;
	  while (delimiter (line[i])) i++;
	  sscanf (line + i, "%lf", &lon);
	  while (line[i] != '"') i++;
	  istart = ++i;
	  while (line[i] != '"') i++;
	  char *name = new char[i - istart + 1];
	  strncpy (name, (line + istart), i - istart);
	  name[i-istart] = '\0';

	  if (flipped) lon *= -1;

	  double radius = 1;
	  if (opts.dem != NULL)
	    {
	      int ilon = int ((lon + 180)/360 * (image_width - 1));
	      int ilat = int ((90 - lat)/180 * (image_height - 1));
	      radius += (opts.demscale * 
			 elevation [ilat * image_width + ilon]/255.);
	    }
	  location m (lat, lon, radius, name);
	  m.spher_to_rect ();
	  if (opts.obslon || opts.obslat || opts.rotate) m.rotate (rot);
	  m.rect_to_spher ();
	  draw_marker (m);  
	  break;
	}
    }
}