Changeset 5610 for trunk/Ohana/src/kapa/graph/DrawRotString.c
- Timestamp:
- Nov 27, 2005, 4:14:38 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/kapa/graph/DrawRotString.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/kapa/graph/DrawRotString.c
r5482 r5610 2 2 # include "alphabet.h" 3 3 4 # define NROT 2565 4 # define XPROC(x,y) (scale*(cs*((x) - x0) - sn*((y) - y0)) + X0) 6 5 # define YPROC(x,y) (scale*(cs*((y) - y0) + sn*((x) - x0)) + Y0) … … 213 212 return (TRUE); 214 213 } 215 216 void InitRotFonts () {217 218 int i, Nhardwired;219 220 Nhardwired = sizeof (HardwiredFonts) / sizeof (FontSet);221 222 Nallfonts = Nhardwired;223 ALLOCATE (AllFonts, FontSet, Nallfonts);224 225 for (i = 0; i < Nhardwired; i++) {226 AllFonts[i] = HardwiredFonts[i];227 }228 229 activefont = AllFonts[DEFFONT].font;230 activescale = 1.0;231 strcpy (currentname, AllFonts[DEFFONT].name);232 currentsize = AllFonts[DEFFONT].size;233 }234 235 int SetRotFont (char *name, int size) {236 237 int i, nsize, msize, bsize, bigger, dsize, match, good;238 239 bigger = good = match = -1;240 dsize = 10000;241 bsize = 10000;242 for (i = 0; i < Nallfonts; i++) {243 if (!strcasecmp (AllFonts[i].name, name)) {244 good = i;245 nsize = abs (AllFonts[i].size - size);246 if (nsize < dsize) {247 match = i;248 dsize = nsize;249 }250 msize = AllFonts[i].size - size;251 if ((msize < bsize) && (msize >= 0)) {252 bigger = i;253 bsize = msize;254 }255 }256 }257 258 if ((match == -1) && (good != -1)) match = good;259 if (bigger != -1) match = bigger;260 if (match != -1) {261 activefont = AllFonts[match].font;262 activescale = (double) size / AllFonts[match].size;263 currentsize = size;264 strcpy (currentname, name);265 return (TRUE);266 } else {267 fprintf (stderr, "no matching font\n");268 return (FALSE);269 }270 271 }272 273 char *GetRotFont (int *size) {274 275 *size = currentsize;276 return (currentname);277 278 }279 280 int RotStrlen (char *c) {281 282 int i, N, dX, code;283 double scale;284 285 scale = activescale;286 287 /* find string length */288 dX = 0;289 290 code = FALSE;291 for (i = 0; i < strlen (c); i++) {292 N = (int)(c[i]);293 /* skip non-printing characters */294 if ((N < 0) || (N >= NROT)) continue;295 296 /* check for special characters */297 if (!code) {298 if (N == 94) { /* super-script */299 scale *= 0.8;300 continue;301 }302 if (N == 95) { /* sub-script */303 scale *= 0.8;304 continue;305 }306 if (N == 124) { /* normal-script */307 scale = activescale;308 continue;309 }310 if (N == 92) { /* backslash */311 code = TRUE;312 continue;313 }314 if (N == 38) { /* font-code */315 i++;316 continue;317 }318 }319 code = FALSE;320 dX += scale*activefont[N].dx + 1;321 }322 return (dX);323 }324 325 /* writes commands to print string at location and angle using326 currently set font and size */327 void PSRotText (FILE *f, int x, int y, char *string, int pos, double angle) {328 329 char *c, *segment, basefont[64];330 int i, N, code;331 int dX, dY, Xoff, Yoff, X, Y, Nseg, NSEG, YoffBase;332 double cs, sn, fscale;333 int basesize;334 335 /* strip off leading whitespace */336 for (c = string; (*c == ' ') || (*c == '\t'); c++);337 if (*c == 0) return;338 339 /* compute string length */340 /* PS fonts are somewhat different scales from bitmap font equivalents */341 fscale = 1.0;342 if (!strcmp (currentname, "times")) fscale = 1.07;343 if (!strcmp (currentname, "courier")) fscale = 1.5;344 if (!strcmp (currentname, "helvetica")) fscale = 0.9;345 if (!strcmp (currentname, "symbol")) fscale = 1.2;346 dX = fscale*RotStrlen (c);347 dY = activefont[65].ascent;348 349 /* apply appropriate offset */350 Xoff = Yoff = 0;351 switch (pos) {352 case 0: Xoff = -dX; Yoff = -dY; break;353 case 1: Xoff = -0.5*dX; Yoff = -dY; break;354 case 2: Xoff = 0; Yoff = -dY; break;355 case 3: Xoff = -dX; Yoff = -0.5*dY; break;356 case 4: Xoff = -0.5*dX; Yoff = -0.5*dY; break;357 case 5: Xoff = 0; Yoff = -0.5*dY; break;358 case 6: Xoff = -dX; Yoff = 0; break;359 case 7: Xoff = -0.5*dX; Yoff = 0; break;360 case 8: Xoff = 0; Yoff = 0; break;361 }362 cs = cos(angle*RAD_DEG);363 sn = sin(angle*RAD_DEG);364 X = x + Xoff*cs + Yoff*sn;365 Y = y - Xoff*sn + Yoff*cs;366 367 basesize = currentsize;368 strcpy (basefont, currentname);369 370 PSSetFont (f, currentsize);371 fprintf (f, "gsave\n");372 fprintf (f, " %d %d moveto %f rotate\n", X, Y, -angle);373 374 Nseg = 0;375 NSEG = strlen(c) + 2;376 ALLOCATE (segment, char, NSEG);377 bzero (segment, NSEG);378 379 code = FALSE;380 YoffBase = 0;381 /* accumulate string segments with common state */382 for (i = 0; i < strlen (c); i++) {383 N = (int)(c[i]);384 if ((N < 0) || (N >= NROT)) continue;385 386 /* check for special characters */387 if (!code) {388 /* superscript character (^) */389 if (N == 94) {390 PSDumpRotSegment (f, segment, &Nseg);391 SetRotFont (currentname, (int)(0.8*currentsize));392 Yoff = 0.75*activescale*dY;393 fprintf (f, "0 %d rmoveto\n", Yoff);394 YoffBase += Yoff;395 PSSetFont (f, currentsize);396 continue;397 }398 /* subscript character (_) */399 if (N == 95) {400 PSDumpRotSegment (f, segment, &Nseg);401 SetRotFont (currentname, (int)(0.8*currentsize));402 Yoff = -0.5*activescale*dY;403 fprintf (f, "0 %d rmoveto\n", Yoff);404 YoffBase += Yoff;405 PSSetFont (f, currentsize);406 continue;407 }408 /* end super/sub script (|) */409 if (N == 124) {410 PSDumpRotSegment (f, segment, &Nseg);411 SetRotFont (currentname, basesize);412 fprintf (f, "0 %d rmoveto\n", -YoffBase);413 YoffBase = 0;414 PSSetFont (f, currentsize);415 continue;416 }417 /* escape char (\) */418 if (N == 92) {419 code = TRUE;420 continue;421 }422 /* begin paren (insert \) */423 if (N == 40) {424 code = FALSE;425 segment[Nseg] = 92;426 Nseg ++;427 CHECK_REALLOCATE (segment, char, NSEG, Nseg, 64);428 }429 /* end paren (insert \) */430 if (N == 41) {431 code = FALSE;432 segment[Nseg] = 92;433 Nseg ++;434 CHECK_REALLOCATE (segment, char, NSEG, Nseg, 64);435 }436 /* font change character (&) */437 if (N == 38) {438 PSDumpRotSegment (f, segment, &Nseg);439 if (c[i+1] == 'h') {440 SetRotFont ("helvetica", currentsize);441 PSSetFont (f, currentsize);442 }443 if (c[i+1] == 't') {444 SetRotFont ("times", currentsize);445 PSSetFont (f, currentsize);446 }447 if (c[i+1] == 'c') {448 SetRotFont ("courier", currentsize);449 PSSetFont (f, currentsize);450 }451 if (c[i+1] == 's') {452 SetRotFont ("symbol", currentsize);453 PSSetFont (f, currentsize);454 }455 i++;456 continue;457 }458 }459 code = FALSE;460 segment[Nseg] = N;461 Nseg ++;462 CHECK_REALLOCATE (segment, char, NSEG, Nseg, 64);463 }464 PSDumpRotSegment (f, segment, &Nseg);465 fprintf (f, "stroke grestore\n");466 free (segment);467 SetRotFont (basefont, basesize);468 }469 470 void PSDumpRotSegment (FILE *f, char *segment, int *Nseg) {471 segment[*Nseg] = 0;472 fprintf (f, "(%s) show\n", segment);473 bzero (segment, *Nseg);474 *Nseg = 0;475 }476 477 void PSSetFont (FILE *f, int size) {478 if (!strcmp (currentname, "times"))479 fprintf (f, "/Times-Roman findfont %d scalefont setfont\n", size);480 if (!strcmp (currentname, "helvetica"))481 fprintf (f, "/Helvetica findfont %d scalefont setfont\n", size);482 if (!strcmp (currentname, "courier"))483 fprintf (f, "/Courier findfont %d scalefont setfont\n", size);484 if (!strcmp (currentname, "symbol"))485 fprintf (f, "/Symbol findfont %d scalefont setfont\n", size);486 }487 488 489 490 /* test cross hair491 fprintf (f, "%d %d %d %d L\n", x-10, y, x+10, y);492 fprintf (f, "%d %d %d %d L\n", x, y-10, x, y+10);493 */
Note:
See TracChangeset
for help on using the changeset viewer.
