gview.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <draw.h>
  5. #include <event.h>
  6. #include <cursor.h>
  7. #include <stdio.h>
  8. #define Never 0xffffffff /* Maximum ulong */
  9. #define LOG2 0.301029995664
  10. #define Button_bit(b) (1 << ((b)-1))
  11. enum {
  12. But1 = Button_bit(1),/* mouse buttons for events */
  13. But2 = Button_bit(2),
  14. But3 = Button_bit(3),
  15. };
  16. int cantmv = 1; /* disallow rotate and move? 0..1 */
  17. int plotdots; /* plot dots instead of lines */
  18. int top_border, bot_border, lft_border, rt_border;
  19. int lft_border0; /* lft_border for y-axis labels >0 */
  20. int top_left, top_right; /* edges of top line free space */
  21. int Mv_delay = 400; /* msec for button click vs. button hold down */
  22. int Dotrad = 2; /* dot radius in pixels */
  23. int framewd=1; /* line thickness for frame (pixels) */
  24. int framesep=1; /* distance between frame and surrounding text */
  25. int outersep=1; /* distance: surrounding text to screen edge */
  26. Point sdigit; /* size of a digit in the font */
  27. Point smaxch; /* assume any character in font fits in this */
  28. double underscan = .05; /* fraction of frame initially unused per side */
  29. double fuzz = 6; /* selection tolerance in pixels */
  30. int tick_len = 15; /* length of axis label tick mark in pixels */
  31. FILE* logfil = 0; /* dump selected points here if nonzero */
  32. #define labdigs 3 /* allow this many sig digits in axis labels */
  33. #define digs10pow 1000 /* pow(10,labdigs) */
  34. #define axis_color clr_im(DLtblue)
  35. /********************************* Utilities *********************************/
  36. /* Prepend string s to null-terminated string in n-byte buffer buf[], truncating if
  37. necessary and using a space to separate s from the rest of buf[].
  38. */
  39. char* str_insert(char* buf, char* s, int n)
  40. {
  41. int blen, slen = strlen(s) + 1;
  42. if (slen >= n)
  43. {strncpy(buf,s,n); buf[n-1]='\0'; return buf;}
  44. blen = strlen(buf);
  45. if (blen >= n-slen)
  46. buf[blen=n-slen-1] = '\0';
  47. memmove(buf+slen, buf, slen+blen+1);
  48. memcpy(buf, s, slen-1);
  49. buf[slen-1] = ' ';
  50. return buf;
  51. }
  52. /* Alter string smain (without lengthening it) so as to remove the first occurrence of
  53. ssub, assuming ssub is ASCII. Return nonzero (true) if string smain had to be changed.
  54. In spite of the ASCII-centric appearance, I think this can handle UTF in smain.
  55. */
  56. int remove_substr(char* smain, char* ssub)
  57. {
  58. char *ss, *s = strstr(smain, ssub);
  59. int n = strlen(ssub);
  60. if (s==0)
  61. return 0;
  62. if (islower(s[n]))
  63. s[0] ^= 32; /* probably tolower(s[0]) or toupper(s[0]) */
  64. else {
  65. for (ss=s+n; *ss!=0; s++, ss++)
  66. *s = *ss;
  67. *s = '\0';
  68. }
  69. return 1;
  70. }
  71. void adjust_border(Font* f)
  72. {
  73. int sep = framesep + outersep;
  74. sdigit = stringsize(f, "8");
  75. smaxch = stringsize(f, "MMMg");
  76. smaxch.x = (smaxch.x + 3)/4;
  77. lft_border0 = (1+labdigs)*sdigit.x + framewd + sep;
  78. rt_border = (lft_border0 - sep)/2 + outersep;
  79. bot_border = sdigit.y + framewd + sep;
  80. top_border = smaxch.y + framewd + sep;
  81. lft_border = lft_border0; /* this gets reset later */
  82. }
  83. int is_off_screen(Point p)
  84. {
  85. const Rectangle* r = &(screen->r);
  86. return p.x-r->min.x<lft_border || r->max.x-p.x<rt_border
  87. || p.y-r->min.y<=top_border || r->max.y-p.y<=bot_border;
  88. }
  89. Cursor bullseye =
  90. {
  91. {-7, -7},
  92. {
  93. 0x1F, 0xF8, 0x3F, 0xFC, 0x7F, 0xFE, 0xFB, 0xDF,
  94. 0xF3, 0xCF, 0xE3, 0xC7, 0xFF, 0xFF, 0xFF, 0xFF,
  95. 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0xC7, 0xF3, 0xCF,
  96. 0x7B, 0xDF, 0x7F, 0xFE, 0x3F, 0xFC, 0x1F, 0xF8,
  97. },
  98. {
  99. 0x00, 0x00, 0x0F, 0xF0, 0x31, 0x8C, 0x21, 0x84,
  100. 0x41, 0x82, 0x41, 0x82, 0x41, 0x82, 0x7F, 0xFE,
  101. 0x7F, 0xFE, 0x41, 0x82, 0x41, 0x82, 0x41, 0x82,
  102. 0x21, 0x84, 0x31, 0x8C, 0x0F, 0xF0, 0x00, 0x00,
  103. }
  104. };
  105. /* Wait for a mouse click and return 0 for failue if not button but (curs can be 0) */
  106. int get_1click(int but, Mouse* m, Cursor* curs)
  107. {
  108. if (curs)
  109. esetcursor(curs);
  110. while (m->buttons==0)
  111. *m = emouse();
  112. if (curs)
  113. esetcursor(0);
  114. return (m->buttons==Button_bit(but));
  115. }
  116. /* Wait for a mouse click or keyboard event from the string of expected characters. Return
  117. the character code or -1 for a button-but mouse event or 0 for wrong button.
  118. */
  119. int get_click_or_kbd(int but, Mouse* m, const char* expected)
  120. {
  121. Event ev;
  122. ulong expbits[4], ty;
  123. expbits[0] = expbits[1] = expbits[2] = expbits[3];
  124. for (; *expected!=0; expected++)
  125. expbits[((*expected)>>5)&3] |= 1 << (*expected&31);
  126. do ty = eread(Emouse|Ekeyboard, &ev);
  127. while ((ty&Emouse) ? ev.mouse.buttons==0
  128. : (ev.kbdc&~127) || !(expbits[(ev.kbdc>>5)&3] & (1<<(ev.kbdc&31))) );
  129. if (ty&Ekeyboard)
  130. return ev.kbdc;
  131. *m = ev.mouse;
  132. return (ev.mouse.buttons==Button_bit(but)) ? -1 : 0;
  133. }
  134. /* Wait until but goes up or until a mouse event's msec passes tlimit.
  135. Return a boolean result that tells whether the button went up.
  136. */
  137. int lift_button(int but, Mouse* m, int tlimit)
  138. {
  139. do { *m = emouse();
  140. if (m->msec >= tlimit)
  141. return 0;
  142. } while (m->buttons & Button_bit(but));
  143. return 1;
  144. }
  145. /* Set *m to the last pending mouse event, or the first one where but is up.
  146. If no mouse events are pending, wait for the next one.
  147. */
  148. void latest_mouse(int but, Mouse* m)
  149. {
  150. int bbit = Button_bit(but);
  151. do { *m = emouse();
  152. } while ((m->buttons & bbit) && ecanmouse());
  153. }
  154. /*********************************** Colors ***********************************/
  155. enum { DOrange=0xffaa00FF, Dgray=0xbbbbbbFF, DDkgreen=0x009900FF,
  156. DDkred=0xcc0000FF, DViolet=0x990099FF, DDkyellow=0xaaaa00FF,
  157. DLtblue=0xaaaaffFF, DPink=0xffaaaaFF,
  158. /* ndraw.h sets DBlack, DBlue, DRed, DYellow, DGreen,
  159. DCyan, DMagenta, DWhite */
  160. };
  161. typedef struct thick_color {
  162. int thick; /* use 1+2*thick pixel wide lines */
  163. Image* clr; /* Color to use when drawing this */
  164. } thick_color;
  165. typedef struct color_ref {
  166. ulong c; /* RGBA pixel color */
  167. char* nam; /* ASCII name (matched to input, used in output)*/
  168. int nam1; /* single-letter version of color name */
  169. Image* im; /* replicated solid-color image */
  170. } color_ref;
  171. color_ref clrtab[] = {
  172. DRed, "Red", 'R', 0,
  173. DPink, "Pink", 'P', 0,
  174. DDkred, "Dkred", 'r', 0,
  175. DOrange, "Orange", 'O', 0,
  176. DYellow, "Yellow", 'Y', 0,
  177. DDkyellow, "Dkyellow", 'y', 0,
  178. DGreen, "Green", 'G', 0,
  179. DDkgreen, "Dkgreen", 'g', 0,
  180. DCyan, "Cyan", 'C', 0,
  181. DBlue, "Blue", 'B', 0,
  182. DLtblue, "Ltblue", 'b', 0,
  183. DMagenta, "Magenta", 'M', 0,
  184. DViolet, "Violet", 'V', 0,
  185. Dgray, "Gray", 'A', 0,
  186. DBlack, "Black", 'K', 0,
  187. DWhite, "White", 'W', 0,
  188. DNofill, 0, 0, 0 /* DNofill means "end of data" */
  189. };
  190. short nam1_idx[128]; /* the clrtab[] index for each nam1, else -1 */
  191. void init_clrtab(void)
  192. {
  193. int i;
  194. Rectangle r = Rect(0,0,1,1);
  195. memset(&nam1_idx[0], -1, sizeof(nam1_idx));
  196. for (i=0; clrtab[i].c!=DNofill; i++) {
  197. clrtab[i].im = allocimage(display, r, CMAP8, 1, clrtab[i].c);
  198. /* should check for 0 result? */
  199. nam1_idx[clrtab[i].nam1] = i;
  200. }
  201. }
  202. int clrim_id(Image* clr)
  203. {
  204. int i;
  205. for (i=0; clrtab[i].im!=clr; i++)
  206. if (clrtab[i].c==DNofill)
  207. exits("bad image color");
  208. return i;
  209. }
  210. int clr_id(int clr)
  211. {
  212. int i;
  213. for (i=0; clrtab[i].c!=clr; i++)
  214. if (clrtab[i].c==DNofill)
  215. exits("bad color");
  216. return i;
  217. }
  218. #define clr_im(clr) clrtab[clr_id(clr)].im
  219. #define is_Multi -2 /* dummy clrtab[] less than -1 */
  220. thick_color* tc_default(thick_color *buf)
  221. {
  222. buf[0].thick = 1;
  223. buf[1].clr = clr_im(DBlack);
  224. buf[1].thick = 0;
  225. return buf;
  226. }
  227. /* Return an allocated array that describes the color letters (clrtab[].nam1 values,
  228. optionally prefixed by 'T') in the string that starts at c0 and ends just before
  229. fin. The first entry is a dummy whose thick field tells how many data entries follow.
  230. If buf!=0, it should point to an array of length 2 that is to hold the output
  231. (truncated to a dummy and one data entry). The error indication is 1 data entry
  232. of default color and thickness; e.g., "Multi(xxbadxx)" in a label prevents gview
  233. from recognizing anything that follows.
  234. */
  235. thick_color* parse_color_chars(const char* c0, const char* fin, thick_color *buf)
  236. {
  237. thick_color* tc; /* Pending return value */
  238. int i, j, n=fin-c0; /* n is an upper bound on how many data members */
  239. const char* c;
  240. for (c=c0; c<fin-1; c++)
  241. if (*c=='T')
  242. n--;
  243. if (buf==0)
  244. tc = (thick_color*) malloc((n+1)*sizeof(thick_color));
  245. else {tc=buf; n=1;}
  246. i = 0;
  247. for (c=c0; c<fin && i<n; c++) {
  248. tc[++i].thick = 0;
  249. if (*c=='T')
  250. if (++c==fin)
  251. return tc_default(tc);
  252. else tc[i].thick=1;
  253. j = (*c&~127) ? -1 : nam1_idx[*c];
  254. if (j < 0)
  255. return tc_default(tc);
  256. tc[i].clr = clrtab[j].im;
  257. }
  258. tc[0].thick = i;
  259. return tc;
  260. }
  261. /* Decide what color and thickness to use for a polyline based on the label it has in the
  262. input file. The winner is whichever color name comes first, or otherwise black; and
  263. thickness is determined by the presence of "Thick" in the string. Place the result
  264. in *r1 and return 0 unless a Multi(...) spec is found, in which case the result is
  265. an allocated array of alternative color and thickness values. A nonzero idxdest
  266. requests the clrtab[] index in *idxdest and no allocated array.
  267. */
  268. thick_color* nam2thclr(const char* nam, thick_color *r1, int *idxdest)
  269. {
  270. char *c, *cbest=0, *rp=0;
  271. int i, ibest=-1;
  272. thick_color* tc = 0;
  273. thick_color buf[2];
  274. if (*nam!=0) {
  275. c = strstr(nam, "Multi(");
  276. if (c!=0 && (rp=strchr(c+6,')'))!=0)
  277. {ibest=is_Multi; cbest=c;}
  278. for (i=0; clrtab[i].nam!=0; i++) {
  279. c = strstr(nam,clrtab[i].nam);
  280. if (c!=0 && (ibest==-1 || c<cbest))
  281. {ibest=i; cbest=c;}
  282. }
  283. }
  284. if (ibest==is_Multi) {
  285. tc = parse_color_chars(cbest+6, rp, (idxdest==0 ? 0 : &buf[0]));
  286. ibest = clrim_id(tc[1].clr);
  287. }
  288. if (idxdest!=0)
  289. *idxdest = (ibest<0) ? clr_id(DBlack) : ibest;
  290. r1->clr = (ibest<0) ? clr_im(DBlack) : clrtab[ibest].im;
  291. r1->thick = (tc!=0) ? tc[1].thick : (strstr(nam,"Thick")==0 ? 0 : 1);
  292. return tc;
  293. }
  294. /* Alter string nam so that nam2thick() and nam2clr() agree with *tc, using
  295. buf[] (a buffer of length bufn) to store the result if it differs from nam.
  296. We go to great pains to perform this alteration in a manner that will seem natural
  297. to the user, i.e., we try removing a suitably isolated color name before inserting
  298. a new one.
  299. */
  300. char* nam_with_thclr(char* nam, const thick_color *tc, char* buf, int bufn)
  301. {
  302. thick_color c0;
  303. int clr0i;
  304. nam2thclr(nam, &c0, &clr0i);
  305. char *clr0s;
  306. if (c0.thick==tc->thick && c0.clr==tc->clr)
  307. return nam;
  308. clr0s = clrtab[clr0i].nam;
  309. if (strlen(nam)<bufn) strcpy(buf,nam);
  310. else {strncpy(buf,nam,bufn); buf[bufn-1]='\0';}
  311. if (c0.clr != tc->clr)
  312. remove_substr(buf, clr0s);
  313. if (c0.thick > tc->thick)
  314. while (remove_substr(buf, "Thick"))
  315. /* do nothing */;
  316. nam2thclr(nam, &c0, &clr0i);
  317. if (c0.clr != tc->clr)
  318. str_insert(buf, clrtab[clrim_id(tc->clr)].nam, bufn);
  319. if (c0.thick < tc->thick)
  320. str_insert(buf, "Thick", bufn);
  321. return buf;
  322. }
  323. /****************************** Data structures ******************************/
  324. Image* mv_bkgd; /* Background image (usually 0) */
  325. typedef struct fpoint {
  326. double x, y;
  327. } fpoint;
  328. typedef struct frectangle {
  329. fpoint min, max;
  330. } frectangle;
  331. frectangle empty_frect = {1e30, 1e30, -1e30, -1e30};
  332. /* When *r2 is transformed by y=y-x*slant, might it intersect *r1 ?
  333. */
  334. int fintersects(const frectangle* r1, const frectangle* r2, double slant)
  335. {
  336. double x2min=r2->min.x, x2max=r2->max.x;
  337. if (r1->max.x <= x2min || x2max <= r1->min.x)
  338. return 0;
  339. if (slant >=0)
  340. {x2min*=slant; x2max*=slant;}
  341. else {double t=x2min*slant; x2min=x2max*slant; x2max=t;}
  342. return r1->max.y > r2->min.y-x2max && r2->max.y-x2min > r1->min.y;
  343. }
  344. int fcontains(const frectangle* r, fpoint p)
  345. {
  346. return r->min.x <=p.x && p.x<= r->max.x && r->min.y <=p.y && p.y<= r->max.y;
  347. }
  348. void grow_bb(frectangle* dest, const frectangle* r)
  349. {
  350. if (r->min.x < dest->min.x) dest->min.x=r->min.x;
  351. if (r->min.y < dest->min.y) dest->min.y=r->min.y;
  352. if (r->max.x > dest->max.x) dest->max.x=r->max.x;
  353. if (r->max.y > dest->max.y) dest->max.y=r->max.y;
  354. }
  355. void slant_frect(frectangle *r, double sl)
  356. {
  357. r->min.y += sl*r->min.x;
  358. r->max.y += sl*r->max.x;
  359. }
  360. fpoint fcenter(const frectangle* r)
  361. {
  362. fpoint c;
  363. c.x = .5*(r->max.x + r->min.x);
  364. c.y = .5*(r->max.y + r->min.y);
  365. return c;
  366. }
  367. typedef struct fpolygon {
  368. fpoint* p; /* a malloc'ed array */
  369. int n; /* p[] has n elements: p[0..n] */
  370. frectangle bb; /* bounding box */
  371. char* nam; /* name of this polygon (malloc'ed) */
  372. thick_color c; /* the current color and line thickness */
  373. thick_color* ct; /* 0 or malloc'ed color schemes, ct[1..ct->thick] */
  374. struct fpolygon* link;
  375. } fpolygon;
  376. typedef struct fpolygons {
  377. fpolygon* p; /* the head of a linked list */
  378. frectangle bb; /* overall bounding box */
  379. frectangle disp; /* part being mapped onto screen->r */
  380. double slant_ht; /* controls how disp is slanted */
  381. } fpolygons;
  382. fpolygons univ = { /* everything there is to display */
  383. 0,
  384. 1e30, 1e30, -1e30, -1e30,
  385. 0, 0, 0, 0,
  386. 2*1e30
  387. };
  388. void free_fp_etc(fpolygon* fp)
  389. {
  390. if (fp->ct != 0)
  391. free(fp->ct);
  392. free(fp->p);
  393. free(fp);
  394. }
  395. void set_default_clrs(fpolygons* fps, fpolygon* fpstop)
  396. {
  397. fpolygon* fp;
  398. for (fp=fps->p; fp!=0 && fp!=fpstop; fp=fp->link)
  399. fp->ct = nam2thclr(fp->nam, &fp->c, 0);
  400. }
  401. void fps_invert(fpolygons* fps)
  402. {
  403. fpolygon *p, *r=0;
  404. for (p=fps->p; p!=0;) {
  405. fpolygon* q = p;
  406. p = p->link;
  407. q->link = r;
  408. r = q;
  409. }
  410. fps->p = r;
  411. }
  412. void fp_remove(fpolygons* fps, fpolygon* fp)
  413. {
  414. fpolygon *q, **p = &fps->p;
  415. while (*p!=fp)
  416. if (*p==0)
  417. return;
  418. else p = &(*p)->link;
  419. *p = fp->link;
  420. fps->bb = empty_frect;
  421. for (q=fps->p; q!=0; q=q->link)
  422. grow_bb(&fps->bb, &q->bb);
  423. }
  424. /* The transform maps abstract fpoint coordinates (the ones used in the input)
  425. to the current screen coordinates. The do_untransform() macros reverses this.
  426. If univ.slant_ht is not the height of univ.disp, the actual region in the
  427. abstract coordinates is a parallelogram inscribed in univ.disp with two
  428. vertical edges and two slanted slanted edges: slant_ht>0 means that the
  429. vertical edges have height slant_ht and the parallelogram touches the lower
  430. left and upper right corners of univ.disp; slant_ht<0 refers to a parallelogram
  431. of height -slant_ht that touches the other two corners of univ.disp.
  432. NOTE: the ytransform macro assumes that tr->sl times the x coordinate has
  433. already been subtracted from yy.
  434. */
  435. typedef struct transform {
  436. double sl;
  437. fpoint o, sc; /* (x,y):->(o.x+sc.x*x, o.y+sc.y*y+sl*x) */
  438. } transform;
  439. #define do_transform(d,tr,s) ((d)->x = (tr)->o.x + (tr)->sc.x*(s)->x, \
  440. (d)->y = (tr)->o.y + (tr)->sc.y*(s)->y \
  441. + (tr)->sl*(s)->x)
  442. #define do_untransform(d,tr,s) ((d)->x = (.5+(s)->x-(tr)->o.x)/(tr)->sc.x, \
  443. (d)->y = (.5+(s)->y-(tr)->sl*(d)->x-(tr)->o.y) \
  444. /(tr)->sc.y)
  445. #define xtransform(tr,xx) ((tr)->o.x + (tr)->sc.x*(xx))
  446. #define ytransform(tr,yy) ((tr)->o.y + (tr)->sc.y*(yy))
  447. #define dxuntransform(tr,xx) ((xx)/(tr)->sc.x)
  448. #define dyuntransform(tr,yy) ((yy)/(tr)->sc.y)
  449. transform cur_trans(void)
  450. {
  451. transform t;
  452. Rectangle d = screen->r;
  453. const frectangle* s = &univ.disp;
  454. double sh = univ.slant_ht;
  455. d.min.x += lft_border;
  456. d.min.y += top_border;
  457. d.max.x -= rt_border;
  458. d.max.y -= bot_border;
  459. t.sc.x = (d.max.x - d.min.x)/(s->max.x - s->min.x);
  460. t.sc.y = -(d.max.y - d.min.y)/fabs(sh);
  461. if (sh > 0) {
  462. t.sl = -t.sc.y*(s->max.y-s->min.y-sh)/(s->max.x - s->min.x);
  463. t.o.y = d.min.y - t.sc.y*s->max.y - t.sl*s->max.x;
  464. } else {
  465. t.sl = t.sc.y*(s->max.y-s->min.y+sh)/(s->max.x - s->min.x);
  466. t.o.y = d.min.y - t.sc.y*s->max.y - t.sl*s->min.x;
  467. }
  468. t.o.x = d.min.x - t.sc.x*s->min.x;
  469. return t;
  470. }
  471. double u_slant_amt(fpolygons *u)
  472. {
  473. double sh=u->slant_ht, dy=u->disp.max.y - u->disp.min.y;
  474. double dx = u->disp.max.x - u->disp.min.x;
  475. return (sh>0) ? (dy-sh)/dx : -(dy+sh)/dx;
  476. }
  477. /* Set *y0 and *y1 to the lower and upper bounds of the set of y-sl*x values that
  478. *u says to display, where sl is the amount of slant.
  479. */
  480. double set_unslanted_y(fpolygons *u, double *y0, double *y1)
  481. {
  482. double yy1, sl=u_slant_amt(u);
  483. if (u->slant_ht > 0) {
  484. *y0 = u->disp.min.y - sl*u->disp.min.x;
  485. yy1 = *y0 + u->slant_ht;
  486. } else {
  487. yy1 = u->disp.max.y - sl*u->disp.min.x;
  488. *y0 = yy1 + u->slant_ht;
  489. }
  490. if (y1 != 0)
  491. *y1 = yy1;
  492. return sl;
  493. }
  494. /*************************** The region to display ****************************/
  495. void nontrivial_interval(double *lo, double *hi)
  496. {
  497. if (*lo >= *hi) {
  498. double mid = .5*(*lo + *hi);
  499. double tweak = 1e-6 + 1e-6*fabs(mid);
  500. *lo = mid - tweak;
  501. *hi = mid + tweak;
  502. }
  503. }
  504. void init_disp(void)
  505. {
  506. double dw = (univ.bb.max.x - univ.bb.min.x)*underscan;
  507. double dh = (univ.bb.max.y - univ.bb.min.y)*underscan;
  508. univ.disp.min.x = univ.bb.min.x - dw;
  509. univ.disp.min.y = univ.bb.min.y - dh;
  510. univ.disp.max.x = univ.bb.max.x + dw;
  511. univ.disp.max.y = univ.bb.max.y + dh;
  512. nontrivial_interval(&univ.disp.min.x, &univ.disp.max.x);
  513. nontrivial_interval(&univ.disp.min.y, &univ.disp.max.y);
  514. univ.slant_ht = univ.disp.max.y - univ.disp.min.y; /* means no slant */
  515. }
  516. void recenter_disp(Point c)
  517. {
  518. transform tr = cur_trans();
  519. fpoint cc, off;
  520. do_untransform(&cc, &tr, &c);
  521. off.x = cc.x - .5*(univ.disp.min.x + univ.disp.max.x);
  522. off.y = cc.y - .5*(univ.disp.min.y + univ.disp.max.y);
  523. univ.disp.min.x += off.x;
  524. univ.disp.min.y += off.y;
  525. univ.disp.max.x += off.x;
  526. univ.disp.max.y += off.y;
  527. }
  528. /* Find the upper-left and lower-right corners of the bounding box of the
  529. parallelogram formed by untransforming the rectangle rminx, rminy, ... (given
  530. in screen coordinates), and return the height of the parallelogram (negated
  531. if it slopes downward).
  532. */
  533. double untransform_corners(double rminx, double rminy, double rmaxx, double rmaxy,
  534. fpoint *ul, fpoint *lr)
  535. {
  536. fpoint r_ur, r_ul, r_ll, r_lr; /* corners of the given recangle */
  537. fpoint ur, ll; /* untransformed versions of r_ur, r_ll */
  538. transform tr = cur_trans();
  539. double ht;
  540. r_ur.x=rmaxx; r_ur.y=rminy;
  541. r_ul.x=rminx; r_ul.y=rminy;
  542. r_ll.x=rminx; r_ll.y=rmaxy;
  543. r_lr.x=rmaxx; r_lr.y=rmaxy;
  544. do_untransform(ul, &tr, &r_ul);
  545. do_untransform(lr, &tr, &r_lr);
  546. do_untransform(&ur, &tr, &r_ur);
  547. do_untransform(&ll, &tr, &r_ll);
  548. ht = ur.y - lr->y;
  549. if (ll.x < ul->x)
  550. ul->x = ll.x;
  551. if (ur.y > ul->y)
  552. ul->y = ur.y;
  553. else ht = -ht;
  554. if (ur.x > lr->x)
  555. lr->x = ur.x;
  556. if (ll.y < lr->y)
  557. lr->y = ll.y;
  558. return ht;
  559. }
  560. void disp_dozoom(double rminx, double rminy, double rmaxx, double rmaxy)
  561. {
  562. fpoint ul, lr;
  563. double sh = untransform_corners(rminx, rminy, rmaxx, rmaxy, &ul, &lr);
  564. if (ul.x==lr.x || ul.y==lr.y)
  565. return;
  566. univ.slant_ht = sh;
  567. univ.disp.min.x = ul.x;
  568. univ.disp.max.y = ul.y;
  569. univ.disp.max.x = lr.x;
  570. univ.disp.min.y = lr.y;
  571. nontrivial_interval(&univ.disp.min.x, &univ.disp.max.x);
  572. nontrivial_interval(&univ.disp.min.y, &univ.disp.max.y);
  573. }
  574. void disp_zoomin(Rectangle r)
  575. {
  576. disp_dozoom(r.min.x, r.min.y, r.max.x, r.max.y);
  577. }
  578. void disp_zoomout(Rectangle r)
  579. {
  580. double qminx, qminy, qmaxx, qmaxy;
  581. double scx, scy;
  582. Rectangle s = screen->r;
  583. if (r.min.x==r.max.x || r.min.y==r.max.y)
  584. return;
  585. s.min.x += lft_border;
  586. s.min.y += top_border;
  587. s.max.x -= rt_border;
  588. s.max.y -= bot_border;
  589. scx = (s.max.x - s.min.x)/(r.max.x - r.min.x);
  590. scy = (s.max.y - s.min.y)/(r.max.y - r.min.y);
  591. qminx = s.min.x + scx*(s.min.x - r.min.x);
  592. qmaxx = s.max.x + scx*(s.max.x - r.max.x);
  593. qminy = s.min.y + scy*(s.min.y - r.min.y);
  594. qmaxy = s.max.y + scy*(s.max.y - r.max.y);
  595. disp_dozoom(qminx, qminy, qmaxx, qmaxy);
  596. }
  597. void expand2(double* a, double* b, double f)
  598. {
  599. double mid = .5*(*a + *b);
  600. *a = mid + f*(*a - mid);
  601. *b = mid + f*(*b - mid);
  602. }
  603. void disp_squareup(void)
  604. {
  605. double dx = univ.disp.max.x - univ.disp.min.x;
  606. double dy = univ.disp.max.y - univ.disp.min.y;
  607. dx /= screen->r.max.x - lft_border - screen->r.min.x - rt_border;
  608. dy /= screen->r.max.y - bot_border - screen->r.min.y - top_border;
  609. if (dx > dy)
  610. expand2(&univ.disp.min.y, &univ.disp.max.y, dx/dy);
  611. else expand2(&univ.disp.min.x, &univ.disp.max.x, dy/dx);
  612. univ.slant_ht = univ.disp.max.y - univ.disp.min.y;
  613. }
  614. /* Slant so that p and q appear at the same height on the screen and the
  615. screen contains the smallest possible superset of what its previous contents.
  616. */
  617. void slant_disp(fpoint p, fpoint q)
  618. {
  619. double yll, ylr, yul, yur; /* corner y coords of displayed parallelogram */
  620. double sh, dy;
  621. if (p.x == q.x)
  622. return;
  623. sh = univ.slant_ht;
  624. if (sh > 0) {
  625. yll=yul=univ.disp.min.y; yul+=sh;
  626. ylr=yur=univ.disp.max.y; ylr-=sh;
  627. } else {
  628. yll=yul=univ.disp.max.y; yll+=sh;
  629. ylr=yur=univ.disp.min.y; yur-=sh;
  630. }
  631. dy = (univ.disp.max.x-univ.disp.min.x)*(q.y - p.y)/(q.x - p.x);
  632. dy -= ylr - yll;
  633. if (dy > 0)
  634. {yll-=dy; yur+=dy;}
  635. else {yul-=dy; ylr+=dy;}
  636. if (ylr > yll) {
  637. univ.disp.min.y = yll;
  638. univ.disp.max.y = yur;
  639. univ.slant_ht = yur - ylr;
  640. } else {
  641. univ.disp.max.y = yul;
  642. univ.disp.min.y = ylr;
  643. univ.slant_ht = ylr - yur;
  644. }
  645. }
  646. /******************************** Ascii input ********************************/
  647. void set_fbb(fpolygon* fp)
  648. {
  649. fpoint lo=fp->p[0], hi=fp->p[0];
  650. const fpoint *q, *qtop;
  651. for (qtop=(q=fp->p)+fp->n; ++q<=qtop;) {
  652. if (q->x < lo.x) lo.x=q->x;
  653. if (q->y < lo.y) lo.y=q->y;
  654. if (q->x > hi.x) hi.x=q->x;
  655. if (q->y > hi.y) hi.y=q->y;
  656. }
  657. fp->bb.min = lo;
  658. fp->bb.max = hi;
  659. }
  660. char* mystrdup(char* s)
  661. {
  662. char *r, *t = strrchr(s,'"');
  663. if (t==0) {
  664. t = s + strlen(s);
  665. while (t>s && (t[-1]=='\n' || t[-1]=='\r'))
  666. t--;
  667. }
  668. r = malloc(1+(t-s));
  669. memcpy(r, s, t-s);
  670. r[t-s] = 0;
  671. return r;
  672. }
  673. int is_valid_label(char* lab)
  674. {
  675. char* t;
  676. if (lab[0]=='"')
  677. return (t=strrchr(lab,'"'))!=0 && t!=lab && strspn(t+1," \t\r\n")==strlen(t+1);
  678. return strcspn(lab," \t")==strlen(lab);
  679. }
  680. /* Read a polyline and update the number of lines read. A zero result indicates bad
  681. syntax if *lineno increases; otherwise it indicates end of file.
  682. */
  683. fpolygon* rd_fpoly(FILE* fin, int *lineno)
  684. {
  685. char buf[4096], junk[2];
  686. fpoint q;
  687. fpolygon* fp;
  688. int allocn;
  689. if (!fgets(buf,4096,fin))
  690. return 0;
  691. (*lineno)++;
  692. if (sscanf(buf,"%lg%lg%1s",&q.x,&q.y,junk) != 2)
  693. return 0;
  694. fp = malloc(sizeof(fpolygon));
  695. allocn = 4;
  696. fp->p = malloc(allocn*sizeof(fpoint));
  697. fp->p[0] = q;
  698. fp->n = 0;
  699. fp->nam = "";
  700. fp->c.thick = 0;
  701. fp->c.clr = clr_im(DBlack);
  702. fp->ct = 0;
  703. while (fgets(buf,4096,fin)) {
  704. (*lineno)++;
  705. if (sscanf(buf,"%lg%lg%1s",&q.x,&q.y,junk) != 2) {
  706. if (!is_valid_label(buf))
  707. {free_fp_etc(fp); return 0;}
  708. fp->nam = (buf[0]=='"') ? buf+1 : buf;
  709. break;
  710. }
  711. if (++(fp->n) == allocn)
  712. fp->p = realloc(fp->p, (allocn<<=1)*sizeof(fpoint));
  713. fp->p[fp->n] = q;
  714. }
  715. fp->nam = mystrdup(fp->nam);
  716. set_fbb(fp);
  717. fp->link = 0;
  718. return fp;
  719. }
  720. /* Read input into *fps and return 0 or a line number where there's a syntax error */
  721. int rd_fpolys(FILE* fin, fpolygons* fps)
  722. {
  723. fpolygon *fp, *fp0=fps->p;
  724. int lineno=0, ok_upto=0;
  725. while ((fp=rd_fpoly(fin,&lineno)) != 0) {
  726. ok_upto = lineno;
  727. fp->link = fps->p;
  728. fps->p = fp;
  729. grow_bb(&fps->bb, &fp->bb);
  730. }
  731. set_default_clrs(fps, fp0);
  732. return (ok_upto==lineno) ? 0 : lineno;
  733. }
  734. /* Read input from file fnam and return an error line no., -1 for "can't open"
  735. or 0 for success.
  736. */
  737. int doinput(char* fnam)
  738. {
  739. FILE* fin = strcmp(fnam,"-")==0 ? stdin : fopen(fnam, "r");
  740. int errline_or0;
  741. if (fin==0)
  742. return -1;
  743. errline_or0 = rd_fpolys(fin, &univ);
  744. fclose(fin);
  745. return errline_or0;
  746. }
  747. /******************************** Ascii output ********************************/
  748. fpolygon* fp_reverse(fpolygon* fp)
  749. {
  750. fpolygon* r = 0;
  751. while (fp!=0) {
  752. fpolygon* q = fp->link;
  753. fp->link = r;
  754. r = fp;
  755. fp = q;
  756. }
  757. return r;
  758. }
  759. void wr_fpoly(FILE* fout, const fpolygon* fp)
  760. {
  761. char buf[256];
  762. int i;
  763. for (i=0; i<=fp->n; i++)
  764. fprintf(fout,"%.12g\t%.12g\n", fp->p[i].x, fp->p[i].y);
  765. fprintf(fout,"\"%s\"\n", nam_with_thclr(fp->nam, &fp->c, buf, 256));
  766. }
  767. void wr_fpolys(FILE* fout, fpolygons* fps)
  768. {
  769. fpolygon* fp;
  770. fps->p = fp_reverse(fps->p);
  771. for (fp=fps->p; fp!=0; fp=fp->link)
  772. wr_fpoly(fout, fp);
  773. fps->p = fp_reverse(fps->p);
  774. }
  775. int dooutput(char* fnam)
  776. {
  777. FILE* fout = fopen(fnam, "w");
  778. if (fout==0)
  779. return 0;
  780. wr_fpolys(fout, &univ);
  781. fclose(fout);
  782. return 1;
  783. }
  784. /************************ Clipping to screen rectangle ************************/
  785. /* Find the t values, 0<=t<=1 for which x0+t*(x1-x0) is between xlo and xhi,
  786. or return 0 to indicate no such t values exist. If returning 1, set *t0 and
  787. *t1 to delimit the t interval.
  788. */
  789. int do_xory(double x0, double x1, double xlo, double xhi, double* t0, double* t1)
  790. {
  791. *t1 = 1.0;
  792. if (x0<xlo) {
  793. if (x1<xlo) return 0;
  794. *t0 = (xlo-x0)/(x1-x0);
  795. if (x1>xhi) *t1 = (xhi-x0)/(x1-x0);
  796. } else if (x0>xhi) {
  797. if (x1>xhi) return 0;
  798. *t0 = (xhi-x0)/(x1-x0);
  799. if (x1<xlo) *t1 = (xlo-x0)/(x1-x0);
  800. } else {
  801. *t0 = 0.0;
  802. if (x1>xhi) *t1 = (xhi-x0)/(x1-x0);
  803. else if (x1<xlo) *t1 = (xlo-x0)/(x1-x0);
  804. else *t1 = 1.0;
  805. }
  806. return 1;
  807. }
  808. /* After mapping y to y-slope*x, what initial fraction of the *p to *q edge is
  809. outside of *r? Note that the edge could start outside *r, pass through *r,
  810. and wind up outside again.
  811. */
  812. double frac_outside(const fpoint* p, const fpoint* q, const frectangle* r,
  813. double slope)
  814. {
  815. double t0, t1, tt0, tt1;
  816. double px=p->x, qx=q->x;
  817. if (!do_xory(px, qx, r->min.x, r->max.x, &t0, &t1))
  818. return 1;
  819. if (!do_xory(p->y-slope*px, q->y-slope*qx, r->min.y, r->max.y, &tt0, &tt1))
  820. return 1;
  821. if (tt0 > t0)
  822. t0 = tt0;
  823. if (t1<=t0 || tt1<=t0)
  824. return 1;
  825. return t0;
  826. }
  827. /* Think of p0..pn as piecewise-linear function F(t) for t=0..pn-p0, and find
  828. the maximum tt such that F(0..tt) is all inside of r, assuming p0 is inside.
  829. Coordinates are transformed by y=y-x*slope before testing against r.
  830. */
  831. double in_length(const fpoint* p0, const fpoint* pn, frectangle r, double slope)
  832. {
  833. const fpoint* p = p0;
  834. double px, py;
  835. do if (++p > pn)
  836. return pn - p0;
  837. while (r.min.x<=(px=p->x) && px<=r.max.x
  838. && r.min.y<=(py=p->y-slope*px) && py<=r.max.y);
  839. return (p - p0) - frac_outside(p, p-1, &r, slope);
  840. }
  841. /* Think of p0..pn as piecewise-linear function F(t) for t=0..pn-p0, and find
  842. the maximum tt such that F(0..tt) is all outside of *r. Coordinates are
  843. transformed by y=y-x*slope before testing against r.
  844. */
  845. double out_length(const fpoint* p0, const fpoint* pn, frectangle r, double slope)
  846. {
  847. const fpoint* p = p0;
  848. double fr;
  849. do { if (p->x < r.min.x)
  850. do if (++p>pn) return pn-p0;
  851. while (p->x <= r.min.x);
  852. else if (p->x > r.max.x)
  853. do if (++p>pn) return pn-p0;
  854. while (p->x >= r.max.x);
  855. else if (p->y-slope*p->x < r.min.y)
  856. do if (++p>pn) return pn-p0;
  857. while (p->y-slope*p->x <= r.min.y);
  858. else if (p->y-slope*p->x > r.max.y)
  859. do if (++p>pn) return pn-p0;
  860. while (p->y-slope*p->x >= r.max.y);
  861. else return p - p0;
  862. } while ((fr=frac_outside(p-1,p,&r,slope)) == 1);
  863. return (p - p0) + fr-1;
  864. }
  865. /*********************** Drawing frame and axis labels ***********************/
  866. #define Nthous 7
  867. #define Len_thous 30 /* bound on strlen(thous_nam[i]) */
  868. char* thous_nam[Nthous] = {
  869. "one", "thousand", "million", "billion",
  870. "trillion", "quadrillion", "quintillion",
  871. };
  872. typedef struct lab_interval {
  873. double sep; /* separation between tick marks */
  874. double unit; /* power of 1000 divisor */
  875. int logunit; /* log base 1000 of of this divisor */
  876. double off; /* offset to subtract before dividing */
  877. } lab_interval;
  878. char* abbrev_num(double x, const lab_interval* iv)
  879. {
  880. static char buf[16];
  881. double dx = x - iv->off;
  882. dx = iv->sep * floor(dx/iv->sep + .5);
  883. sprintf(buf,"%g", dx/iv->unit);
  884. return buf;
  885. }
  886. double lead_digits(double n, double r) /* n truncated to power of 10 above r */
  887. {
  888. double rr = pow(10, ceil(log10(r)));
  889. double nn = (n<rr) ? 0.0 : rr*floor(n/rr);
  890. if (n+r-nn >= digs10pow) {
  891. rr /= 10;
  892. nn = (n<rr) ? 0.0 : rr*floor(n/rr);
  893. }
  894. return nn;
  895. }
  896. lab_interval next_larger(double s0, double xlo, double xhi)
  897. {
  898. double nlo, nhi;
  899. lab_interval r;
  900. r.logunit = (int) floor(log10(s0) + LOG2);
  901. r.unit = pow(10, r.logunit);
  902. nlo = xlo/r.unit;
  903. nhi = xhi/r.unit;
  904. if (nhi >= digs10pow)
  905. r.off = r.unit*lead_digits(nlo, nhi-nlo);
  906. else if (nlo <= -digs10pow)
  907. r.off = -r.unit*lead_digits(-nhi, nhi-nlo);
  908. else r.off = 0;
  909. r.sep = (s0<=r.unit) ? r.unit : (s0<2*r.unit ? 2*r.unit : 5*r.unit);
  910. switch (r.logunit%3) {
  911. case 1: r.unit*=.1; r.logunit--;
  912. break;
  913. case -1: case 2:
  914. r.unit*=10; r.logunit++;
  915. break;
  916. case -2: r.unit*=100; r.logunit+=2;
  917. }
  918. r.logunit /= 3;
  919. return r;
  920. }
  921. double min_hsep(const transform* tr)
  922. {
  923. double s = (2+labdigs)*sdigit.x;
  924. double ss = (univ.disp.min.x<0) ? s+sdigit.x : s;
  925. return dxuntransform(tr, ss);
  926. }
  927. lab_interval mark_x_axis(const transform* tr)
  928. {
  929. fpoint p = univ.disp.min;
  930. Point q, qtop, qbot, tmp;
  931. double x0=univ.disp.min.x, x1=univ.disp.max.x;
  932. double seps0, nseps, seps;
  933. lab_interval iv = next_larger(min_hsep(tr), x0, x1);
  934. set_unslanted_y(&univ, &p.y, 0);
  935. q.y = ytransform(tr, p.y) + .5;
  936. qtop.y = q.y - tick_len;
  937. qbot.y = q.y + framewd + framesep;
  938. seps0 = ceil(x0/iv.sep);
  939. for (seps=0, nseps=floor(x1/iv.sep)-seps0; seps<=nseps; seps+=1) {
  940. char* num = abbrev_num((p.x=iv.sep*(seps0+seps)), &iv);
  941. Font* f = display->defaultfont;
  942. q.x = qtop.x = qbot.x = xtransform(tr, p.x);
  943. line(screen, qtop, q, Enddisc, Enddisc, 0, axis_color, q);
  944. tmp = stringsize(f, num);
  945. qbot.x -= tmp.x/2;
  946. string(screen, qbot, display->black, qbot, f, num);
  947. }
  948. return iv;
  949. }
  950. lab_interval mark_y_axis(const transform* tr)
  951. {
  952. Font* f = display->defaultfont;
  953. fpoint p = univ.disp.min;
  954. Point q, qrt, qlft;
  955. double y0, y1, seps0, nseps, seps;
  956. lab_interval iv;
  957. set_unslanted_y(&univ, &y0, &y1);
  958. iv = next_larger(dyuntransform(tr,-f->height), y0, y1);
  959. q.x = xtransform(tr, p.x) - .5;
  960. qrt.x = q.x + tick_len;
  961. qlft.x = q.x - (framewd + framesep);
  962. seps0 = ceil(y0/iv.sep);
  963. for (seps=0, nseps=floor(y1/iv.sep)-seps0; seps<=nseps; seps+=1) {
  964. char* num = abbrev_num((p.y=iv.sep*(seps0+seps)), &iv);
  965. Point qq = stringsize(f, num);
  966. q.y = qrt.y = qlft.y = ytransform(tr, p.y);
  967. line(screen, qrt, q, Enddisc, Enddisc, 0, axis_color, q);
  968. qq.x = qlft.x - qq.x;
  969. qq.y = qlft.y - qq.y/2;
  970. string(screen, qq, display->black, qq, f, num);
  971. }
  972. return iv;
  973. }
  974. void lab_iv_info(const lab_interval *iv, double slant, char* buf, int *n)
  975. {
  976. if (iv->off > 0)
  977. (*n) += sprintf(buf+*n,"-%.12g",iv->off);
  978. else if (iv->off < 0)
  979. (*n) += sprintf(buf+*n,"+%.12g",-iv->off);
  980. if (slant>0)
  981. (*n) += sprintf(buf+*n,"-%.6gx", slant);
  982. else if (slant<0)
  983. (*n) += sprintf(buf+*n,"+%.6gx", -slant);
  984. if (abs(iv->logunit) >= Nthous)
  985. (*n) += sprintf(buf+*n," in 1e%d units", 3*iv->logunit);
  986. else if (iv->logunit > 0)
  987. (*n) += sprintf(buf+*n," in %ss", thous_nam[iv->logunit]);
  988. else if (iv->logunit < 0)
  989. (*n) += sprintf(buf+*n," in %sths", thous_nam[-iv->logunit]);
  990. }
  991. void draw_xy_ranges(const lab_interval *xiv, const lab_interval *yiv)
  992. {
  993. Point p;
  994. char buf[2*(19+Len_thous+8)+50];
  995. int bufn = 0;
  996. buf[bufn++] = 'x';
  997. lab_iv_info(xiv, 0, buf, &bufn);
  998. bufn += sprintf(buf+bufn, "; y");
  999. lab_iv_info(yiv, u_slant_amt(&univ), buf, &bufn);
  1000. buf[bufn] = '\0';
  1001. p = stringsize(display->defaultfont, buf);
  1002. top_left = screen->r.min.x + lft_border;
  1003. p.x = top_right = screen->r.max.x - rt_border - p.x;
  1004. p.y = screen->r.min.y + outersep;
  1005. string(screen, p, display->black, p, display->defaultfont, buf);
  1006. }
  1007. transform draw_frame(void)
  1008. {
  1009. lab_interval x_iv, y_iv;
  1010. transform tr;
  1011. Rectangle r = screen->r;
  1012. lft_border = (univ.disp.min.y<0) ? lft_border0+sdigit.x : lft_border0;
  1013. tr = cur_trans();
  1014. r.min.x += lft_border;
  1015. r.min.y += top_border;
  1016. r.max.x -= rt_border;
  1017. r.max.y -= bot_border;
  1018. border(screen, r, -framewd, axis_color, r.min);
  1019. x_iv = mark_x_axis(&tr);
  1020. y_iv = mark_y_axis(&tr);
  1021. draw_xy_ranges(&x_iv, &y_iv);
  1022. return tr;
  1023. }
  1024. /*************************** Finding the selection ***************************/
  1025. typedef struct pt_on_fpoly {
  1026. fpoint p; /* the point */
  1027. fpolygon* fp; /* the fpolygon it lies on */
  1028. double t; /* how many knots from the beginning */
  1029. } pt_on_fpoly;
  1030. static double myx, myy;
  1031. #define mydist(p,o,sl,xwt,ywt) (myx=(p).x-(o).x, myy=(p).y-sl*(p).x-(o).y, \
  1032. xwt*myx*myx + ywt*myy*myy)
  1033. /* At what fraction of the way from p0[0] to p0[1] is mydist(p,ctr,slant,xwt,ywt)
  1034. minimized?
  1035. */
  1036. double closest_time(const fpoint* p0, const fpoint* ctr, double slant,
  1037. double xwt, double ywt)
  1038. {
  1039. double p00y=p0[0].y-slant*p0[0].x, p01y=p0[1].y-slant*p0[1].x;
  1040. double dx=p0[1].x-p0[0].x, dy=p01y-p00y;
  1041. double x0=p0[0].x-ctr->x, y0=p00y-ctr->y;
  1042. double bot = xwt*dx*dx + ywt*dy*dy;
  1043. if (bot==0)
  1044. return 0;
  1045. return -(xwt*x0*dx + ywt*y0*dy)/bot;
  1046. }
  1047. /* Scan the polygonal path of length len knots starting at p0, and find the
  1048. point that the transformation y=y-x*slant makes closest to the center of *r,
  1049. where *r itself defines the distance metric. Knots get higher priority than
  1050. points between knots. If psel->t is negative, always update *psel; otherwise
  1051. update *psel only if the scan can improve it. Return a boolean that says
  1052. whether *psel was updated.
  1053. Note that *r is a very tiny rectangle (tiny when converted screen pixels)
  1054. such that anything in *r is considered close enough to match the mouse click.
  1055. The purpose of this routine is to be careful in case there is a lot of hidden
  1056. detail in the tiny rectangle *r.
  1057. */
  1058. int improve_pt(fpoint* p0, double len, const frectangle* r, double slant,
  1059. pt_on_fpoly* psel)
  1060. {
  1061. fpoint ctr = fcenter(r);
  1062. double x_wt=2/(r->max.x-r->min.x), y_wt=2/(r->max.y-r->min.y);
  1063. double xwt=x_wt*x_wt, ywt=y_wt*y_wt;
  1064. double d, dbest = (psel->t <0) ? 1e30 : mydist(psel->p,ctr,slant,xwt,ywt);
  1065. double tt, dbest0 = dbest;
  1066. fpoint pp;
  1067. int ilen = (int) len;
  1068. if (len==0 || ilen>0) {
  1069. int i;
  1070. for (i=(len==0 ? 0 : 1); i<=ilen; i++) {
  1071. d = mydist(p0[i], ctr, slant, xwt, ywt);
  1072. if (d < dbest)
  1073. {psel->p=p0[i]; psel->t=i; dbest=d;}
  1074. }
  1075. return (dbest < dbest0);
  1076. }
  1077. tt = closest_time(p0, &ctr, slant, xwt, ywt);
  1078. if (tt > len)
  1079. tt = len;
  1080. pp.x = p0[0].x + tt*(p0[1].x - p0[0].x);
  1081. pp.y = p0[0].y + tt*(p0[1].y - p0[0].y);
  1082. if (mydist(pp, ctr, slant, xwt, ywt) < dbest) {
  1083. psel->p = pp;
  1084. psel->t = tt;
  1085. return 1;
  1086. }
  1087. return 0;
  1088. }
  1089. /* Test *fp against *r after transforming by y=y-x*slope, and set *psel accordingly.
  1090. */
  1091. void select_in_fpoly(fpolygon* fp, const frectangle* r, double slant,
  1092. pt_on_fpoly* psel)
  1093. {
  1094. fpoint *p0=fp->p, *pn=fp->p+fp->n;
  1095. double l1, l2;
  1096. if (p0==pn)
  1097. {improve_pt(p0, 0, r, slant, psel); psel->fp=fp; return;}
  1098. while ((l1=out_length(p0,pn,*r,slant)) < pn-p0) {
  1099. fpoint p0sav;
  1100. int i1 = (int) l1;
  1101. p0+=i1; l1-=i1;
  1102. p0sav = *p0;
  1103. p0[0].x += l1*(p0[1].x - p0[0].x);
  1104. p0[0].y += l1*(p0[1].y - p0[0].y);
  1105. l2 = in_length(p0, pn, *r, slant);
  1106. if (improve_pt(p0, l2, r, slant, psel)) {
  1107. if (l1==0 && psel->t!=((int) psel->t)) {
  1108. psel->t = 0;
  1109. psel->p = *p0;
  1110. } else if (psel->t < 1)
  1111. psel->t += l1*(1 - psel->t);
  1112. psel->t += p0 - fp->p;
  1113. psel->fp = fp;
  1114. }
  1115. *p0 = p0sav;
  1116. p0 += (l2>0) ? ((int) ceil(l2)) : 1;
  1117. }
  1118. }
  1119. /* Test all the fpolygons against *r after transforming by y=y-x*slope, and return
  1120. the resulting selection, if any.
  1121. */
  1122. pt_on_fpoly* select_in_univ(const frectangle* r, double slant)
  1123. {
  1124. static pt_on_fpoly answ;
  1125. fpolygon* fp;
  1126. answ.t = -1;
  1127. for (fp=univ.p; fp!=0; fp=fp->link)
  1128. if (fintersects(r, &fp->bb, slant))
  1129. select_in_fpoly(fp, r, slant, &answ);
  1130. if (answ.t < 0)
  1131. return 0;
  1132. return &answ;
  1133. }
  1134. /**************************** Using the selection ****************************/
  1135. pt_on_fpoly cur_sel; /* current selection if cur_sel.t>=0 */
  1136. pt_on_fpoly prev_sel; /* previous selection if prev_sel.t>=0 (for slant) */
  1137. Image* sel_bkg = 0; /* what's behind the red dot */
  1138. void clear_txt(void)
  1139. {
  1140. Rectangle r;
  1141. r.min = screen->r.min;
  1142. r.min.x += lft_border;
  1143. r.min.y += outersep;
  1144. r.max.x = top_left;
  1145. r.max.y = r.min.y + smaxch.y;
  1146. draw(screen, r, display->white, display->opaque, r.min);
  1147. top_left = r.min.x;
  1148. }
  1149. Rectangle sel_dot_box(const transform* tr)
  1150. {
  1151. Point ctr;
  1152. Rectangle r;
  1153. if (tr==0)
  1154. ctr.x = ctr.y = Dotrad;
  1155. else do_transform(&ctr, tr, &cur_sel.p);
  1156. r.min.x=ctr.x-Dotrad; r.max.x=ctr.x+Dotrad+1;
  1157. r.min.y=ctr.y-Dotrad; r.max.y=ctr.y+Dotrad+1;
  1158. return r;
  1159. }
  1160. void unselect(const transform* tr)
  1161. {
  1162. transform tra;
  1163. if (sel_bkg==0)
  1164. sel_bkg = allocimage(display, sel_dot_box(0), CMAP8, 0, DWhite);
  1165. clear_txt();
  1166. if (cur_sel.t < 0)
  1167. return;
  1168. prev_sel = cur_sel;
  1169. if (tr==0)
  1170. {tra=cur_trans(); tr=&tra;}
  1171. draw(screen, sel_dot_box(tr), sel_bkg, display->opaque, ZP);
  1172. cur_sel.t = -1;
  1173. }
  1174. /* Text at top right is written first and this low-level routine clobbers it if
  1175. the new top-left text would overwrite it. However, users of this routine should
  1176. try to keep the new text short enough to avoid this.
  1177. */
  1178. void show_mytext(char* msg)
  1179. {
  1180. Point tmp, pt = screen->r.min;
  1181. int siz;
  1182. tmp = stringsize(display->defaultfont, msg);
  1183. siz = tmp.x;
  1184. pt.x=top_left; pt.y+=outersep;
  1185. if (top_left+siz > top_right) {
  1186. Rectangle r;
  1187. r.min.y = pt.y;
  1188. r.min.x = top_right;
  1189. r.max.y = r.min.y + smaxch.y;
  1190. r.max.x = top_left+siz;
  1191. draw(screen, r, display->white, display->opaque, r.min);
  1192. top_right = top_left+siz;
  1193. }
  1194. string(screen, pt, display->black, ZP, display->defaultfont, msg);
  1195. top_left += siz;
  1196. }
  1197. double rnd(double x, double tol) /* round to enough digits for accuracy tol */
  1198. {
  1199. double t = pow(10, floor(log10(tol)));
  1200. return t * floor(x/t + .5);
  1201. }
  1202. double t_tol(double xtol, double ytol)
  1203. {
  1204. int t = (int) floor(cur_sel.t);
  1205. fpoint* p = cur_sel.fp->p;
  1206. double dx, dy;
  1207. if (t==cur_sel.t)
  1208. return 1;
  1209. dx = fabs(p[t+1].x - p[t].x);
  1210. dy = fabs(p[t+1].y - p[t].y);
  1211. xtol /= (xtol>dx) ? xtol : dx;
  1212. ytol /= (ytol>dy) ? ytol : dy;
  1213. return (xtol<ytol) ? xtol : ytol;
  1214. }
  1215. void say_where(const transform* tr)
  1216. {
  1217. double xtol=dxuntransform(tr,1), ytol=dyuntransform(tr,-1);
  1218. char buf[100];
  1219. int n, nmax = (top_right - top_left)/smaxch.x;
  1220. if (nmax >= 100)
  1221. nmax = 100-1;
  1222. n = sprintf(buf,"(%.14g,%.14g) at t=%.14g",
  1223. rnd(cur_sel.p.x,xtol), rnd(cur_sel.p.y,ytol),
  1224. rnd(cur_sel.t, t_tol(xtol,ytol)));
  1225. if (cur_sel.fp->nam[0] != 0)
  1226. sprintf(buf+n," %.*s", nmax-n-1, cur_sel.fp->nam);
  1227. show_mytext(buf);
  1228. }
  1229. void reselect(const transform* tr) /* uselect(); set cur_sel; call this */
  1230. {
  1231. Point pt2, pt3;
  1232. fpoint p2;
  1233. transform tra;
  1234. if (cur_sel.t < 0)
  1235. return;
  1236. if (tr==0)
  1237. {tra=cur_trans(); tr=&tra;}
  1238. do_transform(&p2, tr, &cur_sel.p);
  1239. if (fabs(p2.x)+fabs(p2.y)>1e8 || (pt2.x=p2.x, pt2.y=p2.y, is_off_screen(pt2)))
  1240. {cur_sel.t= -1; return;}
  1241. pt3.x=pt2.x-Dotrad; pt3.y=pt2.y-Dotrad;
  1242. draw(sel_bkg, sel_dot_box(0), screen, display->opaque, pt3);
  1243. fillellipse(screen, pt2, Dotrad, Dotrad, clr_im(DRed), pt2);
  1244. say_where(tr);
  1245. }
  1246. void do_select(Point pt)
  1247. {
  1248. transform tr = cur_trans();
  1249. fpoint pt1, pt2, ctr;
  1250. frectangle r;
  1251. double slant;
  1252. pt_on_fpoly* psel;
  1253. unselect(&tr);
  1254. do_untransform(&ctr, &tr, &pt);
  1255. pt1.x=pt.x-fuzz; pt1.y=pt.y+fuzz;
  1256. pt2.x=pt.x+fuzz; pt2.y=pt.y-fuzz;
  1257. do_untransform(&r.min, &tr, &pt1);
  1258. do_untransform(&r.max, &tr, &pt2);
  1259. slant = u_slant_amt(&univ);
  1260. slant_frect(&r, -slant);
  1261. psel = select_in_univ(&r, slant);
  1262. if (psel==0)
  1263. return;
  1264. if (logfil!=0) {
  1265. fprintf(logfil,"%.14g\t%.14g\n", psel->p.x, psel->p.y);
  1266. fflush(logfil);
  1267. }
  1268. cur_sel = *psel;
  1269. reselect(&tr);
  1270. }
  1271. /***************************** Prompting for text *****************************/
  1272. void unshow_mytext(char* msg)
  1273. {
  1274. Rectangle r;
  1275. Point siz = stringsize(display->defaultfont, msg);
  1276. top_left -= siz.x;
  1277. r.min.y = screen->r.min.y + outersep;
  1278. r.min.x = top_left;
  1279. r.max.y = r.min.y + siz.y;
  1280. r.max.x = r.min.x + siz.x;
  1281. draw(screen, r, display->white, display->opaque, r.min);
  1282. }
  1283. /* Show the given prompt and read a line of user input. The text appears at the
  1284. top left. If it runs into the top right text, we stop echoing but let the user
  1285. continue typing blind if he wants to.
  1286. */
  1287. char* prompt_text(char* prompt)
  1288. {
  1289. static char buf[200];
  1290. int n0, n=0, nshown=0;
  1291. Rune c;
  1292. unselect(0);
  1293. show_mytext(prompt);
  1294. while (n<200-1-UTFmax && (c=ekbd())!='\n') {
  1295. if (c=='\b') {
  1296. buf[n] = 0;
  1297. if (n > 0)
  1298. do n--;
  1299. while (n>0 && (buf[n-1]&0xc0)==0x80);
  1300. if (n < nshown)
  1301. {unshow_mytext(buf+n); nshown=n;}
  1302. } else {
  1303. n0 = n;
  1304. n += runetochar(buf+n, &c);
  1305. buf[n] = 0;
  1306. if (nshown==n0 && top_right-top_left >= smaxch.x)
  1307. {show_mytext(buf+n0); nshown=n;}
  1308. }
  1309. }
  1310. buf[n] = 0;
  1311. while (ecanmouse())
  1312. emouse();
  1313. return buf;
  1314. }
  1315. /**************************** Redrawing the screen ****************************/
  1316. /* Let p0 and its successors define a piecewise-linear function of a paramter t,
  1317. and draw the 0<=t<=n1 portion using transform *tr.
  1318. */
  1319. void draw_fpts(const fpoint* p0, double n1, const transform* tr, int thick,
  1320. Image* clr)
  1321. {
  1322. int n = (int) n1;
  1323. const fpoint* p = p0 + n;
  1324. fpoint pp;
  1325. Point qq, q;
  1326. if (n1 > n) {
  1327. pp.x = p[0].x + (n1-n)*(p[1].x - p[0].x);
  1328. pp.y = p[0].y + (n1-n)*(p[1].y - p[0].y);
  1329. } else pp = *p--;
  1330. do_transform(&qq, tr, &pp);
  1331. if (n1==0)
  1332. fillellipse(screen, qq, 1+thick, 1+thick, clr, qq);
  1333. for (; p>=p0; p--) {
  1334. do_transform(&q, tr, p);
  1335. if(plotdots)
  1336. fillellipse(screen, q, Dotrad, Dotrad, clr, q);
  1337. else
  1338. line(screen, qq, q, Enddisc, Enddisc, thick, clr, qq);
  1339. qq = q;
  1340. }
  1341. }
  1342. void draw_1fpoly(const fpolygon* fp, const transform* tr, Image* clr,
  1343. const frectangle *udisp, double slant)
  1344. {
  1345. fpoint *p0=fp->p, *pn=fp->p+fp->n;
  1346. double l1, l2;
  1347. if (p0==pn && fcontains(udisp,*p0))
  1348. {draw_fpts(p0, 0, tr, fp->c.thick, clr); return;}
  1349. while ((l1=out_length(p0,pn,*udisp,slant)) < pn-p0) {
  1350. fpoint p0sav;
  1351. int i1 = (int) l1;
  1352. p0+=i1; l1-=i1;
  1353. p0sav = *p0;
  1354. p0[0].x += l1*(p0[1].x - p0[0].x);
  1355. p0[0].y += l1*(p0[1].y - p0[0].y);
  1356. l2 = in_length(p0, pn, *udisp, slant);
  1357. draw_fpts(p0, l2, tr, fp->c.thick, clr);
  1358. *p0 = p0sav;
  1359. p0 += (l2>0) ? ((int) ceil(l2)) : 1;
  1360. }
  1361. }
  1362. double get_clip_data(const fpolygons *u, frectangle *r)
  1363. {
  1364. double slant = set_unslanted_y(u, &r->min.y, &r->max.y);
  1365. r->min.x = u->disp.min.x;
  1366. r->max.x = u->disp.max.x;
  1367. return slant;
  1368. }
  1369. void draw_fpoly(const fpolygon* fp, const transform* tr, Image* clr)
  1370. {
  1371. frectangle r;
  1372. double slant = get_clip_data(&univ, &r);
  1373. draw_1fpoly(fp, tr, clr, &r, slant);
  1374. }
  1375. void eresized(int new)
  1376. {
  1377. transform tr;
  1378. fpolygon* fp;
  1379. frectangle clipr;
  1380. double slant;
  1381. if(new && getwindow(display, Refmesg) < 0) {
  1382. fprintf(stderr,"can't reattach to window\n");
  1383. exits("reshap");
  1384. }
  1385. draw(screen, screen->r, display->white, display->opaque, screen->r.min);
  1386. tr = draw_frame();
  1387. slant = get_clip_data(&univ, &clipr);
  1388. for (fp=univ.p; fp!=0; fp=fp->link)
  1389. if (fintersects(&clipr, &fp->bb, slant))
  1390. draw_1fpoly(fp, &tr, fp->c.clr, &clipr, slant);
  1391. reselect(0);
  1392. if (mv_bkgd!=0 && mv_bkgd->repl==0) {
  1393. freeimage(mv_bkgd);
  1394. mv_bkgd = display->white;
  1395. }
  1396. flushimage(display, 1);
  1397. }
  1398. /********************************* Recoloring *********************************/
  1399. int draw_palette(int n) /* n is number of colors; returns patch dy */
  1400. {
  1401. int y0 = screen->r.min.y + top_border;
  1402. int dy = (screen->r.max.y - bot_border - y0)/n;
  1403. Rectangle r;
  1404. int i;
  1405. r.min.y = y0;
  1406. r.min.x = screen->r.max.x - rt_border + framewd;
  1407. r.max.y = y0 + dy;
  1408. r.max.x = screen->r.max.x;
  1409. for (i=0; i<n; i++) {
  1410. draw(screen, r, clrtab[i].im, display->opaque, r.min);
  1411. r.min.y = r.max.y;
  1412. r.max.y += dy;
  1413. }
  1414. return dy;
  1415. }
  1416. Image* palette_color(Point pt, int dy, int n)
  1417. { /* mouse at pt, patch size dy, n colors */
  1418. int yy;
  1419. if (screen->r.max.x - pt.x > rt_border - framewd)
  1420. return 0;
  1421. yy = pt.y - (screen->r.min.y + top_border);
  1422. if (yy<0 || yy>=n*dy)
  1423. return 0;
  1424. return clrtab[yy/dy].im;
  1425. }
  1426. void all_set_clr(fpolygons* fps, Image* clr)
  1427. {
  1428. fpolygon* p;
  1429. for (p=fps->p; p!=0; p=p->link)
  1430. p->c.clr = clr;
  1431. }
  1432. void all_set_scheme(fpolygons* fps, int scheme)
  1433. {
  1434. fpolygon* p;
  1435. for (p=fps->p; p!=0; p=p->link)
  1436. if (p->ct!=0 && scheme <= p->ct[0].thick)
  1437. p->c = p->ct[scheme];
  1438. }
  1439. void do_recolor(int but, Mouse* m, int alluniv)
  1440. {
  1441. int sel, clkk, nclr = clr_id(DWhite);
  1442. int dy = draw_palette(nclr);
  1443. Image* clr;
  1444. clkk = get_click_or_kbd(but, m, "123456789abcdefghijklmnopqrstuvwxyz");
  1445. if (clkk < 0) {
  1446. clr = palette_color(m->xy, dy, nclr);
  1447. if (clr != 0) {
  1448. if (alluniv)
  1449. all_set_clr(&univ, clr);
  1450. else cur_sel.fp->c.clr = clr;
  1451. }
  1452. eresized(0);
  1453. lift_button(but, m, Never);
  1454. } else if (clkk > 0) {
  1455. sel = ('0'<clkk&&clkk<='9') ? 0 : 10+(clkk-'a')*10;
  1456. while (!('0'<=clkk&&clkk<='9'))
  1457. clkk = ekbd();
  1458. sel += clkk-'0';
  1459. if (alluniv)
  1460. all_set_scheme(&univ, sel);
  1461. else if (sel <= cur_sel.fp->ct[0].thick)
  1462. cur_sel.fp->c = cur_sel.fp->ct[sel];
  1463. }
  1464. eresized(0);
  1465. }
  1466. /****************************** Move and rotate ******************************/
  1467. void prepare_mv(const fpolygon* fp)
  1468. {
  1469. Rectangle r = screen->r;
  1470. Image* scr0;
  1471. int dt = 1 + fp->c.thick;
  1472. r.min.x+=lft_border-dt; r.min.y+=top_border-dt;
  1473. r.max.x-=rt_border-dt; r.max.y-=bot_border-dt;
  1474. if (mv_bkgd!=0 && mv_bkgd->repl==0)
  1475. freeimage(mv_bkgd);
  1476. mv_bkgd = allocimage(display, r, CMAP8, 0, DNofill);
  1477. if (mv_bkgd==0)
  1478. mv_bkgd = display->white;
  1479. else { transform tr = cur_trans();
  1480. draw(mv_bkgd, r, screen, display->opaque, r.min);
  1481. draw(mv_bkgd, sel_dot_box(&tr), sel_bkg, display->opaque, ZP);
  1482. scr0 = screen;
  1483. screen = mv_bkgd;
  1484. draw_fpoly(fp, &tr, display->white);
  1485. screen = scr0;
  1486. }
  1487. }
  1488. void move_fp(fpolygon* fp, double dx, double dy)
  1489. {
  1490. fpoint *p, *pn=fp->p+fp->n;
  1491. for (p=fp->p; p<=pn; p++) {
  1492. (p->x) += dx;
  1493. (p->y) += dy;
  1494. }
  1495. (fp->bb.min.x)+=dx; (fp->bb.min.y)+=dy;
  1496. (fp->bb.max.x)+=dx; (fp->bb.max.y)+=dy;
  1497. }
  1498. void rotate_fp(fpolygon* fp, fpoint o, double theta)
  1499. {
  1500. double s=sin(theta), c=cos(theta);
  1501. fpoint *p, *pn=fp->p+fp->n;
  1502. for (p=fp->p; p<=pn; p++) {
  1503. double x=p->x-o.x, y=p->y-o.y;
  1504. (p->x) = o.x + c*x - s*y;
  1505. (p->y) = o.y + s*x + c*y;
  1506. }
  1507. set_fbb(fp);
  1508. }
  1509. /* Move the selected fpolygon so the selected point tracks the mouse, and return
  1510. the total amount of movement. Button but has already been held down for at
  1511. least Mv_delay milliseconds and the mouse might have moved some distance.
  1512. */
  1513. fpoint do_move(int but, Mouse* m)
  1514. {
  1515. transform tr = cur_trans();
  1516. int bbit = Button_bit(but);
  1517. fpolygon* fp = cur_sel.fp;
  1518. fpoint loc, loc0=cur_sel.p;
  1519. double tsav = cur_sel.t;
  1520. unselect(&tr);
  1521. do { latest_mouse(but, m);
  1522. (fp->c.thick)++; /* line() DISAGREES WITH ITSELF */
  1523. draw_fpoly(fp, &tr, mv_bkgd);
  1524. (fp->c.thick)--;
  1525. do_untransform(&loc, &tr, &m->xy);
  1526. move_fp(fp, loc.x-cur_sel.p.x, loc.y-cur_sel.p.y);
  1527. cur_sel.p = loc;
  1528. draw_fpoly(fp, &tr, fp->c.clr);
  1529. } while (m->buttons & bbit);
  1530. cur_sel.t = tsav;
  1531. reselect(&tr);
  1532. loc.x -= loc0.x;
  1533. loc.y -= loc0.y;
  1534. return loc;
  1535. }
  1536. double dir_angle(const Point* pt, const transform* tr)
  1537. {
  1538. fpoint p;
  1539. double dy, dx;
  1540. do_untransform(&p, tr, pt);
  1541. dy=p.y-cur_sel.p.y; dx=p.x-cur_sel.p.x;
  1542. return (dx==0 && dy==0) ? 0.0 : atan2(dy, dx);
  1543. }
  1544. /* Rotate the selected fpolygon around the selection point so as to track the
  1545. direction angle from the selected point to m->xy. Stop when button but goes
  1546. up and return the total amount of rotation in radians.
  1547. */
  1548. double do_rotate(int but, Mouse* m)
  1549. {
  1550. transform tr = cur_trans();
  1551. int bbit = Button_bit(but);
  1552. fpolygon* fp = cur_sel.fp;
  1553. double theta0 = dir_angle(&m->xy, &tr);
  1554. double th, theta = theta0;
  1555. do { latest_mouse(but, m);
  1556. (fp->c.thick)++; /* line() DISAGREES WITH ITSELF */
  1557. draw_fpoly(fp, &tr, mv_bkgd);
  1558. (fp->c.thick)--;
  1559. th = dir_angle(&m->xy, &tr);
  1560. rotate_fp(fp, cur_sel.p, th-theta);
  1561. theta = th;
  1562. draw_fpoly(fp, &tr, fp->c.clr);
  1563. } while (m->buttons & bbit);
  1564. unselect(&tr);
  1565. cur_sel = prev_sel;
  1566. reselect(&tr);
  1567. return theta - theta0;
  1568. }
  1569. /********************************* Edit menu *********************************/
  1570. typedef enum e_index {
  1571. Erecolor, Ethick, Edelete, Eundo, Erotate, Eoptions,
  1572. Emove
  1573. } e_index;
  1574. char* e_items[Eoptions+1];
  1575. Menu e_menu = {e_items, 0, 0};
  1576. typedef struct e_action {
  1577. e_index typ; /* What type of action */
  1578. fpolygon* fp; /* fpolygon the action applies to */
  1579. Image* clr; /* color to use if typ==Erecolor */
  1580. double amt; /* rotation angle or line thickness */
  1581. fpoint pt; /* movement vector or rotation center */
  1582. struct e_action* link; /* next in a stack */
  1583. } e_action;
  1584. e_action* unact = 0; /* heads a linked list of actions */
  1585. e_action* do_undo(e_action*); /* pop off an e_action and (un)do it */
  1586. e_action* save_act(e_action*,e_index); /* append new e_action for status quo */
  1587. void save_mv(fpoint movement)
  1588. {
  1589. unact = save_act(unact, Emove);
  1590. unact->pt = movement;
  1591. }
  1592. void init_e_menu(void)
  1593. {
  1594. char* u = "can't undo";
  1595. e_items[Erecolor] = "recolor";
  1596. e_items[Edelete] = "delete";
  1597. e_items[Erotate] = "rotate";
  1598. e_items[Eoptions-cantmv] = 0;
  1599. e_items[Ethick] = (cur_sel.fp->c.thick >0) ? "thin" : "thick";
  1600. if (unact!=0)
  1601. switch (unact->typ) {
  1602. case Erecolor: u="uncolor"; break;
  1603. case Ethick: u=(unact->fp->c.thick==0) ? "unthin" : "unthicken";
  1604. break;
  1605. case Edelete: u="undelete"; break;
  1606. case Emove: u="unmove"; break;
  1607. case Erotate: u="unrotate"; break;
  1608. }
  1609. e_items[Eundo] = u;
  1610. }
  1611. void do_emenu(int but, Mouse* m)
  1612. {
  1613. int h;
  1614. if (cur_sel.t < 0)
  1615. return;
  1616. init_e_menu();
  1617. h = emenuhit(but, m, &e_menu);
  1618. switch(h) {
  1619. case Ethick: unact = save_act(unact, h);
  1620. cur_sel.fp->c.thick ^= 1;
  1621. eresized(0);
  1622. break;
  1623. case Edelete: unact = save_act(unact, h);
  1624. fp_remove(&univ, cur_sel.fp);
  1625. unselect(0);
  1626. eresized(0);
  1627. break;
  1628. case Erecolor: unact = save_act(unact, h);
  1629. do_recolor(but, m, 0);
  1630. break;
  1631. case Erotate: unact = save_act(unact, h);
  1632. prepare_mv(cur_sel.fp);
  1633. if (get_1click(but, m, 0)) {
  1634. unact->pt = cur_sel.p;
  1635. unact->amt = do_rotate(but, m);
  1636. }
  1637. break;
  1638. case Eundo: unact = do_undo(unact);
  1639. break;
  1640. }
  1641. }
  1642. /******************************* Undoing edits *******************************/
  1643. e_action* save_act(e_action* a0, e_index typ)
  1644. { /* append new e_action for status quo */
  1645. e_action* a = malloc(sizeof(e_action));
  1646. a->link = a0;
  1647. a->pt.x = a->pt.y = 0.0;
  1648. a->amt = cur_sel.fp->c.thick;
  1649. a->clr = cur_sel.fp->c.clr;
  1650. a->fp = cur_sel.fp;
  1651. a->typ = typ;
  1652. return a;
  1653. }
  1654. /* This would be trivial except it's nice to preserve the selection in order to make
  1655. it easy to undo a series of moves. (There's no do_unrotate() because it's harder
  1656. and less important to preserve the selection in that case.)
  1657. */
  1658. void do_unmove(e_action* a)
  1659. {
  1660. double tsav = cur_sel.t;
  1661. unselect(0);
  1662. move_fp(a->fp, -a->pt.x, -a->pt.y);
  1663. if (a->fp == cur_sel.fp) {
  1664. cur_sel.p.x -= a->pt.x;
  1665. cur_sel.p.y -= a->pt.y;
  1666. }
  1667. cur_sel.t = tsav;
  1668. reselect(0);
  1669. }
  1670. e_action* do_undo(e_action* a0) /* pop off an e_action and (un)do it */
  1671. {
  1672. e_action* a = a0;
  1673. if (a==0)
  1674. return 0;
  1675. switch(a->typ) {
  1676. case Ethick: a->fp->c.thick = a->amt;
  1677. eresized(0);
  1678. break;
  1679. case Erecolor: a->fp->c.clr = a->clr;
  1680. eresized(0);
  1681. break;
  1682. case Edelete:
  1683. a->fp->link = univ.p;
  1684. univ.p = a->fp;
  1685. grow_bb(&univ.bb, &a->fp->bb);
  1686. eresized(0);
  1687. break;
  1688. case Emove:
  1689. do_unmove(a);
  1690. eresized(0);
  1691. break;
  1692. case Erotate:
  1693. unselect(0);
  1694. rotate_fp(a->fp, a->pt, -a->amt);
  1695. eresized(0);
  1696. break;
  1697. }
  1698. a0 = a->link;
  1699. free(a);
  1700. return a0;
  1701. }
  1702. /********************************* Main menu *********************************/
  1703. enum m_index { Mzoom_in, Mzoom_out, Munzoom, Mslant, Munslant,
  1704. Msquare_up, Mrecenter, Mrecolor, Mrestack, Mread,
  1705. Mwrite, Mexit};
  1706. char* m_items[] = {"zoom in", "zoom out", "unzoom", "slant", "unslant",
  1707. "square up", "recenter", "recolor", "restack", "read",
  1708. "write", "exit", 0};
  1709. Menu m_menu = {m_items, 0, 0};
  1710. void do_mmenu(int but, Mouse* m)
  1711. {
  1712. int e, h = emenuhit(but, m, &m_menu);
  1713. switch (h) {
  1714. case Mzoom_in:
  1715. disp_zoomin(egetrect(but,m));
  1716. eresized(0);
  1717. break;
  1718. case Mzoom_out:
  1719. disp_zoomout(egetrect(but,m));
  1720. eresized(0);
  1721. break;
  1722. case Msquare_up:
  1723. disp_squareup();
  1724. eresized(0);
  1725. break;
  1726. case Munzoom:
  1727. init_disp();
  1728. eresized(0);
  1729. break;
  1730. case Mrecenter:
  1731. if (get_1click(but, m, &bullseye)) {
  1732. recenter_disp(m->xy);
  1733. eresized(0);
  1734. lift_button(but, m, Never);
  1735. }
  1736. break;
  1737. case Mslant:
  1738. if (cur_sel.t>=0 && prev_sel.t>=0) {
  1739. slant_disp(prev_sel.p, cur_sel.p);
  1740. eresized(0);
  1741. }
  1742. break;
  1743. case Munslant:
  1744. univ.slant_ht = univ.disp.max.y - univ.disp.min.y;
  1745. eresized(0);
  1746. break;
  1747. case Mrecolor:
  1748. do_recolor(but, m, 1);
  1749. break;
  1750. case Mrestack:
  1751. fps_invert(&univ);
  1752. eresized(0);
  1753. break;
  1754. case Mread:
  1755. e = doinput(prompt_text("File:"));
  1756. if (e==0)
  1757. eresized(0);
  1758. else if (e<0)
  1759. show_mytext(" - can't read");
  1760. else {
  1761. char ebuf[80];
  1762. snprintf(ebuf, 80, " - error line %d", e);
  1763. show_mytext(ebuf);
  1764. }
  1765. break;
  1766. case Mwrite:
  1767. if (!dooutput(prompt_text("File:")))
  1768. show_mytext(" - can't write");
  1769. break;
  1770. case Mexit:
  1771. exits("");
  1772. }
  1773. }
  1774. /****************************** Handling events ******************************/
  1775. void doevent(void)
  1776. {
  1777. ulong etype;
  1778. int mobile;
  1779. ulong mvtime;
  1780. Event ev;
  1781. etype = eread(Emouse|Ekeyboard, &ev);
  1782. if(etype & Emouse) {
  1783. if (ev.mouse.buttons & But1) {
  1784. do_select(ev.mouse.xy);
  1785. mvtime = Never;
  1786. mobile = !cantmv && cur_sel.t>=0;
  1787. if (mobile) {
  1788. mvtime = ev.mouse.msec + Mv_delay;
  1789. prepare_mv(cur_sel.fp);
  1790. }
  1791. if (!lift_button(1, &ev.mouse, mvtime) && mobile)
  1792. save_mv(do_move(1, &ev.mouse));
  1793. } else if (ev.mouse.buttons & But2)
  1794. do_emenu(2, &ev.mouse);
  1795. else if (ev.mouse.buttons & But3)
  1796. do_mmenu(3, &ev.mouse);
  1797. } else if (etype & Ekeyboard) {
  1798. if (ev.kbdc=='\n' && cur_sel.t>=0 && logfil!=0) {
  1799. fprintf(logfil,"%s\n", cur_sel.fp->nam);
  1800. fflush(logfil);
  1801. }
  1802. }
  1803. }
  1804. /******************************** Main program ********************************/
  1805. extern char* argv0;
  1806. void usage(void)
  1807. {
  1808. int i;
  1809. fprintf(stderr,"Usage %s [options] [infile]\n", argv0);
  1810. fprintf(stderr,
  1811. "option ::= -l logfile | -m | -p\n"
  1812. "\n"
  1813. "Read a polygonal line graph in an ASCII format (one x y pair per line, delimited\n"
  1814. "by spaces with a label after each polyline), and view it interactively. Use\n"
  1815. "standard input if no infile is specified.\n"
  1816. "Option -l specifies a file in which to log the coordinates of each point selected.\n"
  1817. "(Clicking a point with button one selects it and displays its coordinates and\n"
  1818. "the label of its polylone.) Option -m allows polylines to be moved and rotated.\n"
  1819. "The -p option plots only the vertices of the polygons.\n"
  1820. "The polyline labels can use the following color names:"
  1821. );
  1822. for (i=0; clrtab[i].c!=DNofill; i++)
  1823. fprintf(stderr,"%s%8s", (i%8==0 ? "\n" : " "), clrtab[i].nam);
  1824. fputc('\n', stderr);
  1825. exits("usage");
  1826. }
  1827. void main(int argc, char *argv[])
  1828. {
  1829. int e;
  1830. ARGBEGIN {
  1831. case 'm':
  1832. cantmv=0;
  1833. break;
  1834. case 'l':
  1835. logfil = fopen(ARGF(),"w");
  1836. break;
  1837. case 'p':
  1838. plotdots++;
  1839. break;
  1840. default:
  1841. usage();
  1842. } ARGEND;
  1843. if(initdraw(0, 0, "gview") < 0)
  1844. exits("initdraw");
  1845. einit(Emouse|Ekeyboard);
  1846. do {
  1847. e = doinput(*argv ? *argv : "-");
  1848. if (e < 0) {
  1849. fprintf(stderr,"Cannot read input file %s\n", *argv);
  1850. exits("no valid input file");
  1851. } else if (e > 0) {
  1852. fprintf(stderr,"Bad syntax at line %d of file %s\n", e, *argv ? *argv : "-");
  1853. exits("bad syntax in input");
  1854. }
  1855. } while (*argv && *++argv);
  1856. init_disp();
  1857. init_clrtab();
  1858. set_default_clrs(&univ, 0);
  1859. adjust_border(display->defaultfont);
  1860. cur_sel.t = prev_sel.t = -1;
  1861. eresized(0);
  1862. for(;;)
  1863. doevent();
  1864. }