📄 gpcanvas.c
字号:
PIXrect_t pr; pr = rdrawtopix (widget, gr); setgattr (widget, ap); if (WPU->gattr.fill) fprintf ( FP, "DO %d %d %d %d BOX FI\n", (int) pr.o.x, (int) pr.o.y, (int) pr.c.x, (int) pr.c.y ); else fprintf ( FP, "DO %d %d %d %d BOX DO\n", (int) pr.o.x, (int) pr.o.y, (int) pr.c.x, (int) pr.c.y ); return 0;}int GPpolygon (Gwidget_t *widget, int gpn, Gpoint_t *gpp, Ggattr_t *ap) { PIXpoint_t pp; int i; if (gpn == 0) return 0; pp = pdrawtopix (widget, gpp[0]); setgattr (widget, ap); fprintf (FP, "DO %d %d moveto\n", (int) pp.x, (int) pp.y); for (i = 1; i < gpn; i++) { pp = pdrawtopix (widget, gpp[i]); fprintf (FP, "%d %d lineto\n", (int) pp.x, (int) pp.y); } if (WPU->gattr.fill) fprintf (FP, "FI\n"); else fprintf (FP, "DO\n"); return 0;}int GPsplinegon (Gwidget_t *widget, int gpn, Gpoint_t *gpp, Ggattr_t *ap) { PIXpoint_t p0, p1, p2, p3; int i; if (gpn == 0) return 0; p0 = pdrawtopix (widget, gpp[0]); setgattr (widget, ap); fprintf (FP, "DO %d %d moveto\n", (int) p0.x, (int) p0.y); for (i = 1; i < gpn; i += 3) { p1 = pdrawtopix (widget, gpp[i]); p2 = pdrawtopix (widget, gpp[i + 1]); p3 = pdrawtopix (widget, gpp[i + 2]); fprintf ( FP, "%d %d %d %d %d %d CT\n", (int) p1.x, (int) p1.y, (int) p2.x, (int) p2.y, (int) p3.x, (int) p3.y ); } if (WPU->gattr.fill) fprintf (FP, "FI\n"); else fprintf (FP, "DO\n"); return 0;}int GParc ( Gwidget_t *widget, Gpoint_t gc, Gsize_t gs, double ang1, double ang2, Ggattr_t *ap) { PIXpoint_t pc; PIXsize_t ps; pc = pdrawtopix (widget, gc), ps = sdrawtopix (widget, gs); setgattr (widget, ap); if (WPU->gattr.fill) fprintf ( FP, "DO %d %d %f %d %f %f ARF\n", pc.x, pc.y, (double) ps.y / (double) ps.x, ps.x, ang1, ang2 ); else fprintf ( FP, "DO %d %d %f %d %f %f AR\n", pc.x, pc.y, (double) ps.y / (double) ps.x, ps.x, ang1, ang2 ); return 0;}int GPtext ( Gwidget_t *widget, Gtextline_t *tlp, int n, Gpoint_t go, char *fn, double fs, char *justs, Ggattr_t *ap) { Gsize_t gs; PIXpoint_t po; PIXsize_t ps; char *font; char c, *p; int i; po = pdrawtopix (widget, go); gs.x = 0, gs.y = fs; ps = sdrawtopix (widget, gs); font = findfont (fn); setgattr (widget, ap); fprintf ( FP, "DO %d (%c) %d (%c) [", (int) po.x, justs[0], (int) po.y, justs[1] ); for (i = 0; i < n; i++) { c = tlp[i].p[tlp[i].n], tlp[i].p[tlp[i].n] = '\000'; fprintf (FP, "[ ("); for (p = tlp[i].p; *p; p++) { /* generate PS escapes as needed */ if ((*p == '(') || (*p == ')') || (*p == '\\')) fputc('\\',FP); fputc(*p,FP); } fprintf (FP,") (%c) ] ", tlp[i].j); tlp[i].p[tlp[i].n] = c; } fprintf (FP, "] %d /%s %d TXT\n", n, font, (int) ps.y); return 0;}static char *findfont (char *name) { char *font; if (name[0] == '\000' || strcmp (name, "default") == 0) font = "Times-Roman"; else font = name; return font;}int GPcreatebitmap (Gwidget_t *widget, Gbitmap_t *bitmap, Gsize_t s) { if (!widget) { Gerr (POS, G_ERRNOPARENTWIDGET); return -1; } if (!bitmap) { Gerr (POS, G_ERRNOBITMAP); return -1; } bitmap->u.bits = Marrayalloc ((long) ((int) s.x * (int) s.y * 3)); bitmap->ctype = widget->type; bitmap->canvas = widget - &Gwidgets[0]; bitmap->size = s; return 0;}int GPdestroybitmap (Gbitmap_t *bitmap) { if (!bitmap) { Gerr (POS, G_ERRNOBITMAP); return -1; } Marrayfree (bitmap->u.bits); return 0;}int GPreadbitmap (Gwidget_t *widget, Gbitmap_t *bitmap, FILE *fp) { Gsize_t s; char bufp[2048]; char *s1, *s2; char c; int bufn, bufi, step, x, y, k; s.x = s.y = 0; if (!widget) { Gerr (POS, G_ERRNOPARENTWIDGET); return -1; } if (!bitmap) { Gerr (POS, G_ERRNOBITMAP); return -1; } step = 0; while (step < 3) {l1: if (!fgets (bufp, 2048, fp)) { Gerr (POS, G_ERRCANNOTREADBITMAP); return -1; } s1 = &bufp[0];l2: for (; *s1 && isspace (*s1); s1++) ; if (!*s1 || *s1 == '#') goto l1; switch (step) { case 0: if (strncmp (s1, "P6", 2) != 0) { Gerr (POS, G_ERRCANNOTREADBITMAP); return -1; } step++, s1 += 2; goto l2; case 1: for (s2 = s1; *s2 && *s2 >= '0' && *s2 <= '9'; s2++) ; c = *s2, *s2 = 0; if (s2 == s1 || (s.x = atoi (s1)) <= 0) { *s2 = c, Gerr (POS, G_ERRCANNOTREADBITMAP); return -1; } *s2 = c, step++, s1 = s2; goto l2; case 2: for (s2 = s1; *s2 && *s2 >= '0' && *s2 <= '9'; s2++) ; c = *s2, *s2 = 0; if (s2 == s1 || (s.y = atoi (s1)) <= 0) { *s2 = c, Gerr (POS, G_ERRCANNOTREADBITMAP); return -1; } *s2 = c, step++, s1 = s2; goto l2; } } bitmap->u.bits = Marrayalloc ((long) ((int) s.x * (int) s.y * 3)); bitmap->ctype = widget->type; bitmap->canvas = widget - &Gwidgets[0]; bitmap->size = s; bufi = bufn = 0; bufp[bufi] = 0; s1 = (char *) bitmap->u.bits; for (y = 0; y < s.y; y++) { for (x = 0; x < s.x; x++) { for (k = 0; k < 3; k++) { if (bufi == bufn) { if ((bufn = fread (bufp, 1, 2047, fp)) == 0) { Marrayfree (bitmap->u.bits); Gerr (POS, G_ERRCANNOTCREATEBITMAP); return -1; } bufi = 0; } *s1++ = bufp[bufi++]; } } } return 0;}int GPwritebitmap (Gbitmap_t *bitmap, FILE *fp) { return -1;}int GPbitblt ( Gwidget_t *widget, Gpoint_t gp, Grect_t gr, Gbitmap_t *bitmap, char *mode, Ggattr_t *ap) { PIXrect_t pr, br; PIXpoint_t pp; PIXsize_t bs; Gsize_t scale; Gxy_t p; int x, y, hi, lo; double tvx, tvy, twx, twy; char *s; if (gr.o.x > gr.c.x) p.x = gr.o.x, gr.o.x = gr.c.x, gr.c.x = p.x; if (gr.o.y > gr.c.y) p.y = gr.o.y, gr.o.y = gr.c.y, gr.c.y = p.y; tvx = WPU->vsize.x, tvy = WPU->vsize.y; twx = WPU->wrect.c.x - WPU->wrect.o.x; twy = WPU->wrect.c.y - WPU->wrect.o.y; scale.x = tvx / twx, scale.y = tvy / twy; pr = rdrawtopix (widget, gr); pp = pdrawtobpix (bitmap, gp); bs.x = (pr.c.x - pr.o.x + 1) / scale.x; bs.y = (pr.c.y - pr.o.y + 1) / scale.y; br.o.x = pp.x, br.o.y = pp.y - bs.y + 1; br.c.x = br.o.x + bs.x - 1, br.c.y = br.o.y + bs.y - 1; if (br.o.x < 0) pr.o.x -= br.o.x * scale.x, br.o.x = 0; if (br.o.y < 0) pr.o.y -= br.o.y * scale.y, br.o.y = 0; if (br.c.x >= bitmap->size.x) { pr.c.x -= (br.c.x + 1 - bitmap->size.x) * scale.x; br.c.x = bitmap->size.x - 1; } if (br.c.y >= bitmap->size.y) { pr.c.y -= (br.c.y + 1 - bitmap->size.y) * scale.y; br.c.y = bitmap->size.y - 1; } if (pr.o.x < 0) br.o.x -= pr.o.x / scale.x, pr.o.x = 0; if (pr.o.y < 0) br.o.y -= pr.o.y / scale.y, pr.o.y = 0; if (pr.c.x >= tvx) br.c.x -= (pr.c.x + 1 - tvx) / scale.x, pr.c.x = tvx - 1; if (pr.c.y >= tvy) br.c.y -= (pr.c.y + 1 - tvy) / scale.y, pr.c.y = tvy - 1; bs.x = (pr.c.x - pr.o.x + 1) / scale.x; bs.y = (pr.c.y - pr.o.y + 1) / scale.y; setgattr (widget, ap); fprintf (FP, "DO gsave\n"); fprintf (FP, "%d %d translate\n", pr.o.x, pr.o.y); fprintf (FP, "%f %f scale\n", scale.x * bs.x, scale.y * bs.y); fprintf (FP, "/mystr %d string def\n", 3 * bs.x); fprintf (FP, "%d %d 8\n", bs.x, bs.y); fprintf (FP, "[%d 0 0 %d 0 %d]\n", bs.x, -bs.y, bs.y); fprintf (FP, "{currentfile mystr readhexstring pop} false 3 colorimage\n"); for (y = 0; y < bs.y; y++) { s = (char *) ( bitmap->u.bits + 3 * ((int) bitmap->size.x * (br.o.y + y) + br.o.x) ); for (x = 0; x < bs.x; x++) { hi = (*s >> 4) & 15, lo = *s++ && 15; fprintf (FP, "%x%x", hi, lo); hi = (*s >> 4) & 15, lo = *s++ && 15; fprintf (FP, "%x%x", hi, lo); hi = (*s >> 4) & 15, lo = *s++ && 15; fprintf (FP, "%x%x", hi, lo); } fprintf (FP, "\n"); } fprintf (FP, "grestore\nNP\n"); return 0;}static void setgattr (Gwidget_t *widget, Ggattr_t *ap) { int color, width, style; if (!(ap->flags & G_GATTRCOLOR)) ap->color = WPU->defgattr.color; if (!(ap->flags & G_GATTRWIDTH)) ap->width = WPU->defgattr.width; if (!(ap->flags & G_GATTRFILL)) ap->fill = WPU->defgattr.fill; if (!(ap->flags & G_GATTRSTYLE)) ap->style = WPU->defgattr.style; color = ap->color; if (color >= G_MAXCOLORS || !(WPU->colors[color].inuse)) color = 1; if (color != WPU->gattr.color) { WPU->gattr.color = color; fprintf (FP, "%f %f %f CL\n", RED, GREEN, BLUE); } width = ap->width; if (width != WPU->gattr.width) { WPU->gattr.width = width; fprintf (FP, "DO %d setlinewidth NP\n", width); } WPU->gattr.fill = ap->fill; style = ap->style; if (style != WPU->gattr.style) { WPU->gattr.style = style; fprintf (FP, "DO [ %s ] 0 setdash NP\n", gstyles[style]); }}static PIXrect_t rdrawtopix (Gwidget_t *widget, Grect_t gr) { PIXrect_t pr; double tvx, tvy, twx, twy; tvx = WPU->vsize.x - 1, tvy = WPU->vsize.y - 1; twx = WPU->wrect.c.x - WPU->wrect.o.x; twy = WPU->wrect.c.y - WPU->wrect.o.y; pr.o.x = tvx * (gr.o.x - WPU->wrect.o.x) / twx + 0.5; pr.o.y = tvy * (gr.o.y - WPU->wrect.o.y) / twy + 0.5; pr.c.x = tvx * (gr.c.x - WPU->wrect.o.x) / twx + 0.5; pr.c.y = tvy * (gr.c.y - WPU->wrect.o.y) / twy + 0.5; return pr;}static PIXpoint_t pdrawtopix (Gwidget_t *widget, Gpoint_t gp) { PIXpoint_t pp; double tvx, tvy, twx, twy; tvx = WPU->vsize.x - 1, tvy = WPU->vsize.y - 1; twx = WPU->wrect.c.x - WPU->wrect.o.x; twy = WPU->wrect.c.y - WPU->wrect.o.y; pp.x = tvx * (gp.x - WPU->wrect.o.x) / twx + 0.5; pp.y = tvy * (gp.y - WPU->wrect.o.y) / twy + 0.5; return pp;}static PIXsize_t sdrawtopix (Gwidget_t *widget, Gsize_t gs) { PIXsize_t ps; double tvx, tvy, twx, twy; tvx = WPU->vsize.x - 1, tvy = WPU->vsize.y - 1; twx = WPU->wrect.c.x - WPU->wrect.o.x; twy = WPU->wrect.c.y - WPU->wrect.o.y; ps.x = tvx * (gs.x - 1) / twx + 1.5; ps.y = tvy * (gs.y - 1) / twy + 1.5; return ps;}static PIXpoint_t pdrawtobpix (Gbitmap_t *bitmap, Gpoint_t gp) { PIXpoint_t pp; double tvy; tvy = bitmap->size.y - 1; pp.x = gp.x + 0.5; pp.y = tvy - gp.y + 0.5; return pp;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -