func main() { var cx, cy, cw, ch, midy int message := "Now is the time for all good men to come to the aid of the party" w, h := openvg.Init() var speed openvg.VGfloat = 0.5 var x openvg.VGfloat = 0 midy = (h / 2) fontsize := w / 50 cx = 0 ch = fontsize * 2 cw = w cy = midy - (ch / 2) rx, ry, rw, rh := openvg.VGfloat(cx), openvg.VGfloat(cy), openvg.VGfloat(cw), openvg.VGfloat(ch) // scroll the text, only in the clipping rectangle for x = 0; x < rw+speed; x += speed { openvg.Start(w, h) openvg.Background(255, 255, 255) openvg.FillRGB(0, 0, 0, .2) openvg.Rect(rx, ry, rw, rh) openvg.ClipRect(cx, cy, cw, ch) openvg.Translate(x, ry+openvg.VGfloat(fontsize/2)) openvg.FillRGB(0, 0, 0, 1) openvg.Text(0, 0, message, "sans", fontsize) openvg.ClipEnd() openvg.End() } bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() os.Exit(0) }
// rshapes draws shapes with random colors, openvg.Strokes, and sizes. func rshapes(width, height, n int) { var sx, sy, cx, cy, px, py, ex, ey, pox, poy openvg.VGfloat np := 10 polyx := make([]openvg.VGfloat, np) polyy := make([]openvg.VGfloat, np) openvg.Start(width, height) for i := 0; i < n; i++ { openvg.FillRGB(randcolor(), randcolor(), randcolor(), openvg.VGfloat(rand.Float32())) openvg.Ellipse(randf(width), randf(height), randf(200), randf(100)) openvg.Circle(randf(width), randf(height), randf(100)) openvg.Rect(randf(width), randf(height), randf(200), randf(100)) openvg.Arc(randf(width), randf(height), randf(200), randf(200), randf(360), randf(360)) sx = randf(width) sy = randf(height) openvg.StrokeRGB(randcolor(), randcolor(), randcolor(), 1) openvg.StrokeWidth(randf(5)) openvg.Line(sx, sy, sx+randf(200), sy+randf(100)) openvg.StrokeWidth(0) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) openvg.Qbezier(sx, sy, cx, cy, ex, ey) sx = randf(width) sy = randf(height) ex = sx + randf(200) ey = sy cx = sx + ((ex - sx) / 2.0) cy = sy + randf(100) px = cx py = sy - randf(100) openvg.Cbezier(sx, sy, cx, cy, px, py, ex, ey) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polygon(polyx, polyy) // , np) pox = randf(width) poy = randf(height) for j := 0; j < np; j++ { polyx[j] = pox + randf(200) polyy[j] = poy + randf(100) } openvg.Polyline(polyx, polyy) // , np) } openvg.FillRGB(128, 0, 0, 1) openvg.Text(20, 20, "OpenVG on the Raspberry Pi", "sans", 32) openvg.End() }
func paintBG(w, h openvg.VGfloat) { if !showTrails { openvg.Background(0, 0, 0) return } openvg.FillRGB(0, 0, 0, 0.3) openvg.Rect(0, 0, w, h) }
// testpattern shows a test pattern func testpattern(w, h int, s string) { var midx, midy1, midy2, midy3 openvg.VGfloat fontsize := 256 h2 := openvg.VGfloat(h / 2) by := openvg.VGfloat(h - 100) bx := openvg.VGfloat(w - 100) tw1 := &FW{"mono", 0, fontsize} tw2 := &FW{"sans", 0, fontsize} tw3 := &FW{"serif", 0, fontsize} openvg.Start(w, h) // colored squares in the corners openvg.FillRGB(255, 0, 0, 1) openvg.Rect(0, 0, 100, 100) openvg.FillRGB(0, 255, 0, 1) openvg.Rect(0, by, 100, 100) openvg.FillRGB(0, 0, 255, 1) openvg.Rect(bx, 0, 100, 100) openvg.FillRGB(128, 128, 128, 1) openvg.Rect(bx, by, 100, 100) // for each font, (Sans, Serif, Mono), adjust the string to the w tw1.fitwidth(w, 20, s) tw2.fitwidth(w, 20, s) tw3.fitwidth(w, 20, s) midx = openvg.VGfloat(w / 2) // Adjust the baselines to be medial midy1 = h2 + 20 + openvg.VGfloat((tw1.fontsize)/2) midy2 = h2 - openvg.VGfloat((tw2.fontsize)/2) midy3 = h2 - 20 - openvg.VGfloat(tw2.fontsize) - openvg.VGfloat((tw3.fontsize)/2) openvg.FillRGB(128, 128, 128, 1) openvg.TextMid(midx, midy1, s, tw1.font, tw1.fontsize) openvg.FillRGB(128, 0, 0, 1) openvg.TextMid(midx, midy2, s, tw2.font, tw2.fontsize) openvg.FillRGB(0, 0, 128, 1) openvg.TextMid(midx, midy3, s, tw3.font, tw3.fontsize) openvg.End() }
// loadimage loads all the images of the deck into a map for later display func loadimage(d deck.Deck, m map[string]image.Image) { firstrun++ w, h := d.Canvas.Width, d.Canvas.Height cw := openvg.VGfloat(w) ch := openvg.VGfloat(h) msize := int(cw * .01) mx := cw / 2 my := ch * 0.05 mbg := "white" mfg := "black" for ns, s := range d.Slide { for ni, i := range s.Image { if !modfile(i.Name, StartTime) { continue } openvg.Start(w, h) if i.Link != "" { fmt.Fprintf(os.Stderr, "Found altimg %s\n", i.Link) } f, err := os.Open(i.Name) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) continue } img, _, err := image.Decode(f) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) continue } openvg.FillColor(mbg) openvg.Rect(0, 0, cw, ch) openvg.FillColor(mfg) openvg.TextMid(mx, my, fmt.Sprintf("Loading image %s %d from slide %d", i.Name, ni, ns), "sans", msize) bounds := img.Bounds() iw := i.Width ih := i.Height if i.Scale > 0 { iw = int(float64(iw) * (i.Scale / 100)) ih = int(float64(ih) * (i.Scale / 100)) } // if the specified dimensions are native use those, otherwise resize if iw == (bounds.Max.X-bounds.Min.X) && ih == (bounds.Max.Y-bounds.Min.Y) { m[i.Name] = img } else { g := gift.New(gift.Resize(iw, ih, gift.BoxResampling)) resized := image.NewRGBA(g.Bounds(img.Bounds())) g.Draw(resized, img) m[i.Name] = resized } f.Close() openvg.End() } } }
// showgrid xrays a slide func showgrid(d deck.Deck, n int, p float64) { w := openvg.VGfloat(d.Canvas.Width) h := openvg.VGfloat(d.Canvas.Height) percent := openvg.VGfloat(p) fs := (w / 100) // labels are 1% of the width xpct := (percent / 100.0) * w ypct := (percent / 100.0) * h openvg.StrokeColor("lightgray", 0.5) openvg.StrokeWidth(3) // horizontal gridlines xl := percent for x := xpct; x <= w; x += xpct { openvg.Line(x, 0, x, h) openvg.Text(x, percent, fmt.Sprintf("%.0f%%", xl), "sans", int(fs)) xl += percent } // vertical gridlines yl := percent for y := ypct; y <= h; y += ypct { openvg.Line(0, y, w, y) openvg.Text(percent, y, fmt.Sprintf("%.0f%%", yl), "sans", int(fs)) yl += percent } // show boundary and location of images if n < 0 || n > len(d.Slide) { return } for _, im := range d.Slide[n].Image { x := pct(im.Xp, w) y := pct(im.Yp, h) iw := openvg.VGfloat(im.Width) ih := openvg.VGfloat(im.Height) if im.Scale > 0 { iw *= openvg.VGfloat(im.Scale / 100) ih *= openvg.VGfloat(im.Scale / 100) } openvg.FillRGB(127, 0, 0, 0.3) openvg.Circle(x, y, fs) openvg.FillRGB(255, 0, 0, 0.1) openvg.Rect(x-iw/2, y-ih/2, iw, ih) } openvg.End() }
// dodeck sets up the graphics environment and kicks off the interaction func dodeck(filename, searchterm string, pausetime time.Duration, slidenum, cw, ch int, gp float64) { firstrun = 0 w, h := openvg.Init() openvg.FillRGB(200, 200, 200, 1) openvg.Rect(0, 0, openvg.VGfloat(w), openvg.VGfloat(h)) if cw > 0 { w = cw } if ch > 0 { h = ch } if pausetime == 0 { interact(filename, searchterm, w, h, slidenum, gp) } else { loop(filename, w, h, slidenum, pausetime) } openvg.Finish() }
func gradient(width, height int) { w := openvg.VGfloat(width) h := openvg.VGfloat(height) stops := []openvg.Offcolor{ {0.0, openvg.RGB{255, 255, 255}, 1.0}, {0.5, openvg.RGB{128, 128, 128}, 1.0}, {1.0, openvg.RGB{0, 0, 0}, 1.0}, } x1 := w / 8 x2 := (w * 3) / 8 y1 := h / 3 y2 := (h * 2) / 3 cx := (w * 3) / 4 cy := (h / 2) r := (x2 - x1) fx := cx + (r / 4) fy := cy + (r / 4) openvg.Start(width, height) openvg.BackgroundRGB(128, 128, 128, 1) openvg.FillLinearGradient(x1, y1, x2, y2, stops) openvg.Rect(x1, y1, x2-x1, y2-y1) openvg.FillRadialGradient(cx, cy, fx, fy, r, stops) openvg.Circle(cx, cy, r) openvg.FillRGB(0, 0, 0, 0.3) openvg.Circle(x1, y1, 10) openvg.Circle(x2, y2, 10) openvg.Circle(cx, cy, 10) openvg.Circle(cx+r/2, cy, 10) openvg.Circle(fx, fy, 10) openvg.FillColor("black") SansTypeface := "sans" openvg.TextMid(x1, y1-20, "(x1, y1)", SansTypeface, 18) openvg.TextMid(x2, y2+10, "(x2, y2)", SansTypeface, 18) openvg.TextMid(cx, cy, "(cx, cy)", SansTypeface, 18) openvg.TextMid(fx, fy, "(fx, fy)", SansTypeface, 18) openvg.TextEnd(cx+(r/2)+20, cy, "r", SansTypeface, 18) openvg.TextMid(x1+((x2-x1)/2), h/6, "Linear Gradient", SansTypeface, 36) openvg.TextMid(cx, h/6, "Radial Gradient", SansTypeface, 36) openvg.End() }
// main plots data from specified files or standard input in a // grid where plotc specifies the number of columns. func main() { w, h := openvg.Init() i := 1 filenames := flag.Args() for i <= 1000 { openvg.Start(w, h) openvg.FillColor("white") openvg.Rect(0, 0, gwidth, gheight) if len(filenames) == 0 { doplot(beginx, beginy, "") } else { plotgrid(beginx, beginy, filenames) } openvg.End() i = i + 1 } bufio.NewReader(os.Stdin).ReadByte() openvg.Finish() }
func imagetable(w, h int) { imgw, imgh := 422, 238 itable := []it{ {"desert0.jpg", imgw, imgh}, {"desert1.jpg", imgw, imgh}, {"desert2.jpg", imgw, imgh}, {"desert3.jpg", imgw, imgh}, {"desert4.jpg", imgw, imgh}, {"desert5.jpg", imgw, imgh}, {"desert6.jpg", imgw, imgh}, {"desert7.jpg", imgw, imgh}, //{"http://farm4.static.flickr.com/3546/3338566612_9c56bfb53e_m.jpg", 240, 164}, //{"http://farm4.static.flickr.com/3642/3337734413_e36baba755_m.jpg", 240, 164}, } offset := openvg.VGfloat(50) left := offset bot := openvg.VGfloat(h-imgh) - offset gutter := offset x := left y := bot openvg.Start(w, h) openvg.BackgroundColor("black") for _, iname := range itable { openvg.Image(x, y, iname.width, iname.height, iname.name) openvg.FillRGB(255, 255, 255, 0.3) openvg.Rect(x, y, openvg.VGfloat(imgw), 32) openvg.FillRGB(0, 0, 0, 1) openvg.TextMid(x+openvg.VGfloat(imgw/2), y+10, iname.name, "sans", 16) x += openvg.VGfloat(iname.width) + gutter if x > openvg.VGfloat(w) { x = left y -= openvg.VGfloat(iname.height) + gutter } } y = openvg.VGfloat(h) * 0.1 openvg.FillRGB(128, 128, 128, 1) openvg.TextMid(openvg.VGfloat(w/2), 100, "Joshua Tree National Park", "sans", 48) openvg.End() }
func main() { var ( filename = flag.String("f", "svgcolors.txt", "input file") fontname = flag.String("font", "sans", "fontname") neg = flag.Bool("neg", false, "negative") showrgb = flag.Bool("rgb", false, "show RGB") showcode = flag.Bool("showcode", true, "show colors and codes") circsw = flag.Bool("circle", true, "circle swatch") outline = flag.Bool("outline", false, "outline swatch") fontsize = flag.Int("fs", 12, "fontsize") rowsize = flag.Int("r", 32, "rowsize") colw = flag.Float64("c", 340, "column size") swatch = flag.Float64("s", 16, "swatch size") gutter = flag.Float64("g", 12, "gutter") err error tcolor, line string ) flag.Parse() f, oerr := os.Open(*filename) if oerr != nil { fmt.Fprintf(os.Stderr, "%v\n", oerr) return } width, height := openvg.Init() openvg.Start(width, height) fw := openvg.VGfloat(width) fh := openvg.VGfloat(height) if *neg { openvg.FillColor("black") openvg.Rect(0, 0, fw, fh) tcolor = "white" } else { openvg.FillColor("white") openvg.Rect(0, 0, fw, fh) tcolor = "black" } top := fh - 32.0 left := openvg.VGfloat(32.0) cw := openvg.VGfloat(*colw) sw := openvg.VGfloat(*swatch) g := openvg.VGfloat(*gutter) in := bufio.NewReader(f) for x, y, nr := left, top, 0; err == nil; nr++ { line, err = in.ReadString('\n') fields := strings.Split(strings.TrimSpace(line), "\t") if nr%*rowsize == 0 && nr > 0 { x += cw y = top } if len(fields) == 3 { var red, green, blue uint8 fmt.Sscanf(fields[2], "%d,%d,%d", &red, &green, &blue) openvg.FillRGB(red, green, blue, 1) if *outline { openvg.StrokeColor("black") openvg.StrokeWidth(1) } if *circsw { openvg.Circle(x+sw/2, y+sw/2, sw) } else { openvg.Rect(x, y, sw, sw) } openvg.StrokeWidth(0) openvg.FillColor(tcolor) openvg.Text(x+sw+openvg.VGfloat(*fontsize/2), y, fields[0], *fontname, *fontsize) var label string if *showcode { if *showrgb { label = fields[1] } else { label = fields[2] } openvg.FillColor("gray") openvg.TextEnd(x+cw-(sw+g), y, label, *fontname, *fontsize) } } y -= (sw + g) } openvg.End() bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() }
// plot places a plot at the specified location with the specified dimemsions // using the specified settings, using the specified data func plot(x, y, w, h openvg.VGfloat, settings plotset, d []rawdata) { nd := len(d) if nd < 2 { fmt.Fprintf(os.Stderr, "%d is not enough points to plot\n", len(d)) return } // Compute the minima and maxima of the data maxx, minx := d[0].x, d[0].x maxy, miny := d[0].y, d[0].y for _, v := range d { if v.x > maxx { maxx = v.x } if v.y > maxy { maxy = v.y } if v.x < minx { minx = v.x } if v.y < miny { miny = v.y } } // Prepare for a area or line chart by allocating // polygon coordinates; for the horizon plot, you need two extra coordinates // for the extrema. needpoly := settings.opt["area"] || settings.opt["connect"] var xpoly, ypoly []openvg.VGfloat if needpoly { xpoly = make([]openvg.VGfloat, nd+2) ypoly = make([]openvg.VGfloat, nd+2) // preload the extrema of the polygon, // the bottom left and bottom right of the plot's rectangle xpoly[0] = x ypoly[0] = y xpoly[nd+1] = x + w ypoly[nd+1] = y } // Draw the plot's bounding rectangle if settings.opt["showbg"] && !settings.opt["sameplot"] { openvg.FillColor(settings.attr["bgcolor"]) openvg.Rect(x, y, w, h) } // Loop through the data, drawing items as specified spacer := openvg.VGfloat(10.0) for i, v := range d { xp := fmap(v.x, minx, maxx, x, x+w) yp := fmap(v.y, miny, maxy, y, y+h) if needpoly { xpoly[i+1] = xp ypoly[i+1] = yp } if settings.opt["showbar"] { openvg.StrokeColor(settings.attr["barcolor"]) openvg.StrokeWidth(settings.size["barsize"]) openvg.Line(xp, yp, xp, y) } if settings.opt["showdot"] { openvg.FillColor(settings.attr["dotcolor"]) openvg.StrokeWidth(0) openvg.Circle(xp, yp, settings.size["dotsize"]) } if settings.opt["showx"] { if i%int(settings.size["xinterval"]) == 0 { openvg.FillColor("black") openvg.TextMid(xp, y-(spacer*2), fmt.Sprintf("%d", int(v.x)), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(xp, y, xp, y-spacer) } openvg.StrokeWidth(0) } } // Done constructing the points for the area or line plots, display them in one shot if settings.opt["area"] { openvg.FillColor(settings.attr["areacolor"]) openvg.Polygon(xpoly, ypoly) } if settings.opt["connect"] { openvg.StrokeColor(settings.attr["linecolor"]) openvg.StrokeWidth(settings.size["linesize"]) openvg.Polyline(xpoly[1:nd+1], ypoly[1:nd+1]) } // Put on the y axis labels, if specified if settings.opt["showy"] { bot := openvg.VGfloat(math.Floor(float64(miny))) top := openvg.VGfloat(math.Ceil(float64(maxy))) yrange := top - bot interval := yrange / openvg.VGfloat(settings.size["yinterval"]) for yax := bot; yax <= top; yax += interval { yaxp := fmap(yax, bot, top, openvg.VGfloat(y), openvg.VGfloat(y+h)) openvg.FillColor("black") openvg.TextEnd(x-spacer, yaxp, fmt.Sprintf("%.1f", yax), settings.attr["font"], int(settings.size["fontsize"])) openvg.StrokeColor("silver") openvg.StrokeWidth(1) openvg.Line(x-spacer, yaxp, x, yaxp) } openvg.StrokeWidth(0) } // Finally, tack on the label, if specified if len(settings.attr["label"]) > 0 { openvg.FillColor(settings.attr["labelcolor"], 0.3) openvg.TextMid(x+(w/2), y+(h/2), settings.attr["label"], settings.attr["font"], int(w/8)) // int(settings.size["fontsize"])) } openvg.StrokeWidth(0) }
func main() { var sreenHeight, cx, cy, cw, ch, midy int message := "Scrolling texts could be useful if the information does not fit on screen" w, h := openvg.Init() sreenHeight = h var speed openvg.VGfloat = 0.5 var x openvg.VGfloat = 0 midy = (h / 2) fontsize := w / 50 cx = 0 ch = fontsize * 2 cw = w cy = midy - (ch / 2) var jsonData SLData for { response, err := http.Get("http://localhost:8000") if err == nil { defer response.Body.Close() contents, err := ioutil.ReadAll(response.Body) if err != nil { fmt.Printf("Error reading http data, %s", err) } else { fmt.Printf("Got: %s\n", string(contents)) if err := json.Unmarshal(contents, &jsonData); err != nil { panic(err) } fmt.Println(jsonData) } } openvg.Start(w, h) imgw, imgh := 0, 0 openvg.Background(255, 255, 255) //SLHeight = 60 var imgPosY = openvg.VGfloat(sreenHeight - 70) openvg.Image(4, imgPosY, imgw, imgh, "SL.jpg") var TAB1 = openvg.VGfloat(4 * w / 20) var TAB2 = openvg.VGfloat(8 * w / 20) rx1, rw1, rh1 := openvg.VGfloat(cx), openvg.VGfloat(cw), openvg.VGfloat(ch) ty := 0 rix := 0 // Buses for ty = sreenHeight - (80 + int(rh1)); ty > 0 && rix < len(jsonData.ResponseData.Buses); ty -= ch { tempy := openvg.VGfloat(ty) //ry := openvg.VGfloat(ty) if rix%2 == 0 { openvg.FillRGB(0, 0, 0, .2) //openvg.Rect(rx1, tempy, rw1, rh1) openvg.FillRGB(0, 0, 0, 1) tempy = tempy + 6.0 } else { openvg.FillRGB(0, 0, 0, .4) openvg.Rect(rx1, tempy, rw1, rh1) tempy = tempy + 6.0 openvg.FillRGB(0, 0, 0, 1) } openvg.Text(rx1, tempy, jsonData.ResponseData.Buses[rix].LineNumber, "sans", fontsize) drawTypeOfTransport(rx1+60, tempy+2, rw1, ch-20, jsonData.ResponseData.Buses[rix].TransportMode) openvg.Text(rx1+100, tempy, jsonData.ResponseData.Buses[rix].StopPointDesignation, "sans", fontsize) openvg.Text(rx1+TAB1, tempy, jsonData.ResponseData.Buses[rix].DisplayTime, "sans", fontsize) var dest = jsonData.ResponseData.Buses[rix].Destination dest = replaceAO(dest) openvg.Text(rx1+TAB2, tempy, dest, "sans", fontsize) //openvg.Translate(x, ry+openvg.VGfloat(fontsize/2)) //openvg.Background(255,255,0) rix = rix + 1 } var trainIx = 0 for ty = ty - 20; ty > 0 && trainIx < len(jsonData.ResponseData.Trains); ty -= ch { tempy := openvg.VGfloat(ty) //ry := openvg.VGfloat(ty) if rix%2 == 0 { openvg.FillRGB(0, 0, 0, .2) //openvg.Rect(rx1, tempy, rw1, rh1) openvg.FillRGB(0, 0, 0, 1) tempy = tempy + 6.0 } else { openvg.FillRGB(0, 0, 0, .4) openvg.Rect(rx1, tempy, rw1, rh1) tempy = tempy + 6.0 openvg.FillRGB(0, 0, 0, 1) } openvg.Text(rx1, tempy, jsonData.ResponseData.Trains[trainIx].LineNumber, "sans", fontsize) drawTypeOfTransport(rx1+55, tempy+4, rw1, ch-20, jsonData.ResponseData.Trains[trainIx].TransportMode) openvg.Text(rx1+TAB1, tempy, jsonData.ResponseData.Trains[trainIx].DisplayTime, "sans", fontsize) var dest = jsonData.ResponseData.Trains[trainIx].Destination dest = replaceAO(dest) openvg.Text(rx1+TAB2, tempy, dest, "sans", fontsize) //openvg.Translate(x, ry+openvg.VGfloat(fontsize/2)) //openvg.Background(255,255,0) trainIx = trainIx + 1 rix = rix + 1 } //openvg.End() openvg.SaveEnd("dump.raw") time.Sleep(60 * time.Second) } bufio.NewReader(os.Stdin).ReadBytes('\n') rx, ry, rw, rh := openvg.VGfloat(cx), openvg.VGfloat(cy), openvg.VGfloat(cw), openvg.VGfloat(ch) // scroll the text, only in the clipping rectangle for x = 0; x < rw+speed; x += speed { openvg.Start(w, h) openvg.Background(255, 255, 255) openvg.FillRGB(0, 0, 0, .2) openvg.Rect(rx, ry, rw, rh) openvg.ClipRect(cx, cy, cw, ch) openvg.Translate(x, ry+openvg.VGfloat(fontsize/2)) openvg.FillRGB(0, 0, 0, 1) openvg.Text(0, 0, message, "sans", fontsize) openvg.ClipEnd() openvg.End() } bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() os.Exit(0) }
func main() { var sreenHeight, cx, cy, cw, ch int message := "Hej Sandra tack forden mysiga kramen ..... <3 <3 Puss puss" w, h := openvg.Init() sreenHeight = h var speed openvg.VGfloat = 4.0 var x openvg.VGfloat = 0 //midy = (h / 2) fontsize := w / 50 cx = 0 ch = fontsize * 2 cw = w //cy = midy - (ch / 2) cy = h - 80 var jsonData SLData var lastmill = makeTimestamp() response, err := http.Get("http://localhost:8000") if err == nil { defer response.Body.Close() contents, err := ioutil.ReadAll(response.Body) if err != nil { fmt.Printf("Error reading http data, %s", err) } else { fmt.Printf("Got: %s\n", string(contents)) if err := json.Unmarshal(contents, &jsonData); err != nil { panic(err) } fmt.Println(jsonData) } } openvg.Start(w, h) openvg.End() go PlayVideo(w, h) for { openvg.Start(w, h) //fmt.Println("W,H",w,h) rx, ry, rw, rh := openvg.VGfloat(cx), openvg.VGfloat(cy), openvg.VGfloat(cw), openvg.VGfloat(ch) // scroll the text, only in the clipping rectangle for x = 0; x < rw+speed; x += speed { var mill = makeTimestamp() var delta = (mill - lastmill) / 5 //fmt.Println("delta ", delta) //speed x = openvg.VGfloat(delta) imgw, imgh := 0, 0 openvg.Background(0, 0, 0) //SLHeight = 60 var imgPosY = openvg.VGfloat(sreenHeight - 120) openvg.Image(8, imgPosY, imgw, imgh, "DA4FID.png") var TAB1 = openvg.VGfloat(4 * w / 20) //var TAB2 = openvg.VGfloat(8*w/20) rx1, rw1, rh1 := openvg.VGfloat(cx), openvg.VGfloat(cw), openvg.VGfloat(ch) ty := 0 rix := 0 // - (120 + int(rh1)) ty = sreenHeight - 140 var trainIx = 0 for ty = ty - 20; ty > 0 && trainIx < len(jsonData.ResponseData.Trains); ty -= ch { tempy := openvg.VGfloat(ty) //ry := openvg.VGfloat(ty) if rix%2 == 0 { openvg.FillRGB(255, 255, 255, .2) //openvg.Rect(rx1, tempy, rw1, rh1) tempy = tempy + 6.0 } else { openvg.FillRGB(255, 255, 255, .4) openvg.Rect(rx1, tempy, rw1/3, rh1) tempy = tempy + 6.0 } openvg.FillRGB(255, 255, 255, 1) //openvg.Text(rx1, tempy, jsonData.ResponseData.Trains[trainIx].LineNumber , "sans", fontsize) //drawTypeOfTransport(rx1+55,tempy+4,rw1,ch-20,jsonData.ResponseData.Trains[trainIx].TransportMode) openvg.Text(rx1, tempy, jsonData.ResponseData.Trains[trainIx].DisplayTime, "sans", fontsize) var dest = jsonData.ResponseData.Trains[trainIx].Destination dest = replaceAO(dest) openvg.Text(rx1+TAB1, tempy, dest, "sans", fontsize) //openvg.Translate(x, ry+openvg.VGfloat(fontsize/2)) //openvg.Background(255,255,0) trainIx = trainIx + 1 rix = rix + 1 } imgw = 20 imgh = 20 //openvg.Image(8, 100 , imgw, imgh, "no_smoking.png") // openvg.Start(w, h) //openvg.Background(255, 255, 255) openvg.FillRGB(100, 0, 0, 1) openvg.Rect(rx, ry, rw, rh) //openvg.ClipRect(cx, cy, cw, ch) //openvg.Translate(x, ry+openvg.VGfloat(fontsize/2)) openvg.FillRGB(255, 255, 255, 1) var pxp openvg.VGfloat = openvg.VGfloat(w) - openvg.VGfloat(x) var pyp openvg.VGfloat = openvg.VGfloat(h) - openvg.VGfloat(58) openvg.Text(pxp, pyp, message, "sans", fontsize) //openvg.ClipEnd() openvg.End() } //openvg.SaveEnd("dump.raw") //time.Sleep(60*time.Second); } bufio.NewReader(os.Stdin).ReadBytes('\n') bufio.NewReader(os.Stdin).ReadBytes('\n') openvg.Finish() os.Exit(0) }
// refcard shows a reference card of shapes func refcard(width, height int) { shapenames := []string{ "Circle", "Ellipse", "Rectangle", "Rounded Rectangle", "Line", "Polyline", "Polygon", "Arc", "Quadratic Bezier", "Cubic Bezier", "Image", } top := openvg.VGfloat(height) * .95 sx := openvg.VGfloat(width) * 0.10 sy := top sw := openvg.VGfloat(width) * .05 sh := openvg.VGfloat(height) * .045 dotsize := openvg.VGfloat(7.0) spacing := openvg.VGfloat(2.0) fontsize := int(openvg.VGfloat(height) * .033) shapecolor := Color{202, 225, 255, 1.0} openvg.Start(width, height) openvg.FillRGB(128, 0, 0, 1) openvg.TextEnd(openvg.VGfloat(width-20), openvg.VGfloat(height/2), "OpenVG on the Raspberry Pi", "sans", fontsize+(fontsize/2)) openvg.FillRGB(0, 0, 0, 1) for _, s := range shapenames { openvg.Text(sx+sw+sw/2, sy, s, "sans", fontsize) sy -= sh * spacing } sy = top cx := sx + (sw / 2) ex := sx + sw openvg.FillRGB(shapecolor.red, shapecolor.green, shapecolor.blue, shapecolor.alpha) openvg.Circle(cx, sy, sw) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Ellipse(cx, sy, sw, sh) coordpoint(cx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Rect(sx, sy, sw, sh) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.Roundrect(sx, sy, sw, sh, 20, 20) coordpoint(sx, sy, dotsize, shapecolor) sy -= sh * spacing openvg.StrokeWidth(1) openvg.StrokeRGB(204, 204, 204, 1) openvg.Line(sx, sy, ex, sy) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(ex, sy, dotsize, shapecolor) sy -= sh px := []openvg.VGfloat{sx, sx + (sw / 4), sx + (sw / 2), sx + ((sw * 3) / 4), sx + sw} py := []openvg.VGfloat{sy, sy - sh, sy, sy - sh, sy} openvg.Polyline(px, py) // , 5) coordpoint(px[0], py[0], dotsize, shapecolor) coordpoint(px[1], py[1], dotsize, shapecolor) coordpoint(px[2], py[2], dotsize, shapecolor) coordpoint(px[3], py[3], dotsize, shapecolor) coordpoint(px[4], py[4], dotsize, shapecolor) sy -= sh * spacing py[0] = sy py[1] = sy - sh py[2] = sy - (sh / 2) py[3] = py[1] - (sh / 4) py[4] = sy openvg.Polygon(px, py) // , 5) sy -= (sh * spacing) + sh openvg.Arc(sx+(sw/2), sy, sw, sh, 0, 180) coordpoint(sx+(sw/2), sy, dotsize, shapecolor) sy -= sh * spacing var cy, ey openvg.VGfloat cy = sy + (sh / 2) ey = sy openvg.Qbezier(sx, sy, cx, cy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= sh * spacing ey = sy cy = sy + sh openvg.Cbezier(sx, sy, cx, cy, cx, sy, ex, ey) coordpoint(sx, sy, dotsize, shapecolor) coordpoint(cx, cy, dotsize, shapecolor) coordpoint(cx, sy, dotsize, shapecolor) coordpoint(ex, ey, dotsize, shapecolor) sy -= (sh * spacing * 1.5) openvg.Image(sx, sy, 110, 110, "starx.jpg") openvg.End() }
// showlide displays slides func showslide(d deck.Deck, imap map[string]image.Image, n int) { var video videoType if n < 0 || n > len(d.Slide)-1 { return } slide := d.Slide[n] if slide.Bg == "" { slide.Bg = "white" } if slide.Fg == "" { slide.Fg = "black" } openvg.Start(d.Canvas.Width, d.Canvas.Height) cw := openvg.VGfloat(d.Canvas.Width) ch := openvg.VGfloat(d.Canvas.Height) if slide.Gradcolor1 != "" && slide.Gradcolor2 != "" { oc := []openvg.Offcolor{ {0, openvg.Colorlookup(slide.Gradcolor1), 1}, {1, openvg.Colorlookup(slide.Gradcolor2), 1}, } openvg.FillLinearGradient(0, ch, 0, 0, oc) } else { openvg.FillColor(slide.Bg) } openvg.Rect(0, 0, cw, ch) var x, y, fs openvg.VGfloat // every image in the slide for _, im := range slide.Image { x = pct(im.Xp, cw) y = pct(im.Yp, ch) imw := openvg.VGfloat(im.Width) imh := openvg.VGfloat(im.Height) if im.Scale > 0 { imw *= openvg.VGfloat(im.Scale / 100) imh *= openvg.VGfloat(im.Scale / 100) } midx := openvg.VGfloat(imw / 2) midy := openvg.VGfloat(imh / 2) if im.Type == "video" { // Only one video per slide supported video.name = im.Name video.x = x - midx video.y = y - midy video.w = imw video.h = imh video.altimg = im.Link } else { img, ok := imap[im.Name] if ok { openvg.Img(x-midx, y-midy, img) } if len(im.Caption) > 0 { capfs := pctwidth(im.Sp, cw, cw/100) if im.Font == "" { im.Font = "sans" } if im.Color == "" { openvg.FillColor(slide.Fg) } else { openvg.FillColor(im.Color) } if im.Align == "" { im.Align = "center" } switch im.Align { case "left", "start": x -= midx case "right", "end": x += midx } showtext(x, y-((midy)+(capfs*2.0)), im.Caption, im.Align, im.Font, capfs) } } } // every graphic on the slide const defaultColor = "rgb(127,127,127)" const defaultSw = 1.5 var strokeopacity float64 // line for _, line := range slide.Line { if line.Color == "" { line.Color = slide.Fg // defaultColor } if line.Opacity == 0 { strokeopacity = 1 } else { strokeopacity = line.Opacity / 100.0 } x1, y1, sw := dimen(d, line.Xp1, line.Yp1, line.Sp) x2, y2, _ := dimen(d, line.Xp2, line.Yp2, 0) openvg.StrokeColor(line.Color, openvg.VGfloat(strokeopacity)) if sw == 0 { sw = defaultSw } openvg.StrokeWidth(openvg.VGfloat(sw)) openvg.StrokeColor(line.Color) openvg.Line(x1, y1, x2, y2) openvg.StrokeWidth(0) } // ellipse for _, ellipse := range slide.Ellipse { x, y, _ = dimen(d, ellipse.Xp, ellipse.Yp, 0) var w, h openvg.VGfloat w = pct(ellipse.Wp, cw) if ellipse.Hr == 0 { // if relative height not specified, base height on overall height h = pct(ellipse.Hp, ch) } else { h = pct(ellipse.Hr, w) } if ellipse.Color == "" { ellipse.Color = defaultColor } if ellipse.Opacity == 0 { ellipse.Opacity = 1 } else { ellipse.Opacity /= 100 } openvg.FillColor(ellipse.Color, openvg.VGfloat(ellipse.Opacity)) openvg.Ellipse(x, y, w, h) } // rect for _, rect := range slide.Rect { x, y, _ = dimen(d, rect.Xp, rect.Yp, 0) var w, h openvg.VGfloat w = pct(rect.Wp, cw) if rect.Hr == 0 { // if relative height not specified, base height on overall height h = pct(rect.Hp, ch) } else { h = pct(rect.Hr, w) } if rect.Color == "" { rect.Color = defaultColor } if rect.Opacity == 0 { rect.Opacity = 1 } else { rect.Opacity /= 100 } openvg.FillColor(rect.Color, openvg.VGfloat(rect.Opacity)) openvg.Rect(x-(w/2), y-(h/2), w, h) } // curve for _, curve := range slide.Curve { if curve.Color == "" { curve.Color = defaultColor } if curve.Opacity == 0 { strokeopacity = 1 } else { strokeopacity = curve.Opacity / 100.0 } x1, y1, sw := dimen(d, curve.Xp1, curve.Yp1, curve.Sp) x2, y2, _ := dimen(d, curve.Xp2, curve.Yp2, 0) x3, y3, _ := dimen(d, curve.Xp3, curve.Yp3, 0) openvg.StrokeColor(curve.Color, openvg.VGfloat(strokeopacity)) openvg.FillColor(slide.Bg, openvg.VGfloat(curve.Opacity)) if sw == 0 { sw = defaultSw } openvg.StrokeWidth(sw) openvg.Qbezier(x1, y1, x2, y2, x3, y3) openvg.StrokeWidth(0) } // arc for _, arc := range slide.Arc { if arc.Color == "" { arc.Color = defaultColor } if arc.Opacity == 0 { strokeopacity = 1 } else { strokeopacity = arc.Opacity / 100.0 } ax, ay, sw := dimen(d, arc.Xp, arc.Yp, arc.Sp) w := pct(arc.Wp, cw) h := pct(arc.Hp, cw) openvg.StrokeColor(arc.Color, openvg.VGfloat(strokeopacity)) openvg.FillColor(slide.Bg, openvg.VGfloat(arc.Opacity)) if sw == 0 { sw = defaultSw } openvg.StrokeWidth(sw) openvg.Arc(ax, ay, w, h, openvg.VGfloat(arc.A1), openvg.VGfloat(arc.A2)) openvg.StrokeWidth(0) } // polygon for _, poly := range slide.Polygon { if poly.Color == "" { poly.Color = defaultColor } if poly.Opacity == 0 { poly.Opacity = 1 } else { poly.Opacity /= 100 } xs := strings.Split(poly.XC, " ") ys := strings.Split(poly.YC, " ") if len(xs) != len(ys) { continue } if len(xs) < 3 || len(ys) < 3 { continue } px := make([]openvg.VGfloat, len(xs)) py := make([]openvg.VGfloat, len(ys)) for i := 0; i < len(xs); i++ { x, err := strconv.ParseFloat(xs[i], 32) if err != nil { px[i] = 0 } else { px[i] = pct(x, cw) } y, err := strconv.ParseFloat(ys[i], 32) if err != nil { py[i] = 0 } else { py[i] = pct(y, ch) } } openvg.FillColor(poly.Color, openvg.VGfloat(poly.Opacity)) openvg.Polygon(px, py) } openvg.FillColor(slide.Fg) // every list in the slide var offset, textopacity openvg.VGfloat const blinespacing = 2.4 for _, l := range slide.List { if l.Font == "" { l.Font = "sans" } x, y, fs = dimen(d, l.Xp, l.Yp, l.Sp) if l.Type == "bullet" { offset = 1.2 * fs } else { offset = 0 } if l.Lp == 0 { l.Lp = blinespacing } if l.Opacity == 0 { textopacity = 1 } else { textopacity = openvg.VGfloat(l.Opacity / 100) } // every list item var li, lifont string for ln, tl := range l.Li { if len(l.Color) > 0 { openvg.FillColor(l.Color, textopacity) } else { openvg.FillColor(slide.Fg) } if l.Type == "bullet" { boffset := fs / 2 openvg.Ellipse(x, y+boffset, boffset, boffset) //openvg.Rect(x, y+boffset/2, boffset, boffset) } if l.Type == "number" { li = fmt.Sprintf("%d. ", ln+1) + tl.ListText } else { li = tl.ListText } if len(tl.Color) > 0 { openvg.FillColor(tl.Color, textopacity) } if len(tl.Font) > 0 { lifont = tl.Font } else { lifont = l.Font } showtext(x+offset, y, li, l.Align, lifont, fs) y -= fs * openvg.VGfloat(l.Lp) } } openvg.FillColor(slide.Fg) // every text in the slide const linespacing = 1.8 var tdata string for _, t := range slide.Text { if t.File != "" { tdata = includefile(t.File) } else { tdata = t.Tdata } if t.Font == "" { t.Font = "sans" } if t.Opacity == 0 { textopacity = 1 } else { textopacity = openvg.VGfloat(t.Opacity / 100) } if t.Lp == 0 { t.Lp = linespacing } x, y, fs = dimen(d, t.Xp, t.Yp, t.Sp) td := strings.Split(tdata, "\n") if t.Type == "code" { ls := fs * openvg.VGfloat(t.Lp) t.Font = "mono" tdepth := (ls * openvg.VGfloat(len(td))) + fs openvg.FillColor("rgb(240,240,240)") openvg.Rect(x-20, y-tdepth+(ls), pctwidth(t.Wp, cw, cw-x-20), tdepth) } if t.Color == "" { openvg.FillColor(slide.Fg, textopacity) } else { openvg.FillColor(t.Color, textopacity) } if t.Type == "block" { textwrap(x, y, pctwidth(t.Wp, cw, cw/2), tdata, t.Font, fs, fs*openvg.VGfloat(t.Lp), 0.3) } else { // every text line ls := fs * openvg.VGfloat(t.Lp) for _, txt := range td { showtext(x, y, txt, t.Align, t.Font, fs) y -= ls } } } openvg.FillColor(slide.Fg) openvg.End() if video.altimg != "" { img, ok := imap[video.altimg] if ok { openvg.Img(video.x, video.y, img) } } if video.name != "" { openvg.Video(video.x, video.y, video.w, video.h, video.name) } }
// makepi draws the Raspberry Pi func makepi(x, y, w, h openvg.VGfloat) { // dimensions socw := h / 5 compw := h / 5 cjw := h / 10 cjh := h / 8 audw := h / 5 aujw := h / 10 aujh := cjh / 2 hdw := w / 6 hdh := w / 10 gpw := w / 3 gph := h / 8 pw := h / 10 usw := w / 5 ush := h / 5 etw := w / 5 eth := h / 5 sdw := w / 6 sdh := w / 4 offset := (w / 2) / 10 w34 := (w * 3) / 4 w2 := w / 2 h2 := h / 2 h40 := (h * 2) / 5 openvg.FillRGB(0, 128, 0, 1) openvg.Rect(x, y, w, h) // board openvg.FillRGB(255, 255, 0, 1) openvg.Rect(x+w2, (y+h)-compw, compw, compw) // composite openvg.FillRGB(192, 192, 192, 1) openvg.Rect(x+w2+(cjw/2), y+h, cjw, cjh) // composite jack openvg.FillRGB(0, 0, 0, 1) openvg.Rect(x+w34, y+h-audw, audw, audw) // audio openvg.Rect(x+w34+(aujw/2), y+h, aujw, aujh) // audio jack openvg.FillRGB(192, 192, 192, 1) openvg.Rect(x+w2, y, hdw, hdh) // HDMI openvg.Rect((x+w)-etw, y, etw, eth) // Ethernet openvg.Rect((x+w+offset)-usw, y+h40, usw, ush) // USB openvg.Rect(x, y, pw, pw) // Power openvg.FillRGB(0, 0, 0, 1) openvg.Rect(x+(w*2)/5, y+h40, socw, socw) // SoC openvg.Rect(x, (y+h)-gph, gpw, gph) // GPIO openvg.FillRGB(0, 0, 255, 1) openvg.Rect(x-sdw, (y+h2)-(sdh/2), sdw, sdh) // SD card }
// interact controls the display of the deck func interact(filename, searchterm string, w, h, slidenum int, gp float64) { openvg.SaveTerm() defer openvg.RestoreTerm() var d deck.Deck var err error d, err = deck.Read(filename, w, h) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } openvg.RawTerm() r := bufio.NewReader(os.Stdin) lastslide := len(d.Slide) - 1 if slidenum > lastslide { slidenum = lastslide } if slidenum < 0 { slidenum = 0 } if len(searchterm) > 0 { sr := deck.Search(d, searchterm) if sr >= 0 { slidenum = sr } } n := slidenum xray := 1 initial := 0 imap := make(map[string]image.Image) // respond to keyboard commands, 'q' to exit for cmd := byte('0'); cmd != 'q'; cmd = readcmd(r) { switch cmd { // read/reload case 'r', 18: // r, Ctrl-R d, err = deck.Read(filename, w, h) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } loadimage(d, imap) openvg.Background(0, 0, 0) xray = 1 showslide(d, imap, n) // save slide case 's', 19: // s, Ctrl-S openvg.SaveEnd(fmt.Sprintf("%s-slide-%04d", filename, n)) // first slide case '0', '1', 1, '^': // 0,1,Ctrl-A,^ initial++ if initial == 1 { loadimage(d, imap) n = slidenum } else { n = 0 } showslide(d, imap, n) // last slide case '*', 5, '$': // *, Crtl-E, $ n = lastslide showslide(d, imap, n) // next slide case '+', 'n', '\n', ' ', '\t', '=', 14: // +,n,newline,space,tab,equal,Crtl-N n++ if n > lastslide { n = 0 } showslide(d, imap, n) // previous slide case '-', 'p', 8, 16, 127: // -,p,Backspace,Ctrl-P,Del n-- if n < 0 { n = lastslide } showslide(d, imap, n) // x-ray case 'x', 24: // x, Ctrl-X xray++ showslide(d, imap, n) if xray%2 == 0 { showgrid(d, n, gp) } // Escape sequence from remotes case 27: remote, rerr := r.ReadString('~') if len(remote) > 2 && rerr == nil { switch remote[1] { case '3': // blank screen openvg.Start(d.Canvas.Width, d.Canvas.Height) openvg.FillColor("black") openvg.Rect(0, 0, openvg.VGfloat(d.Canvas.Width), openvg.VGfloat(d.Canvas.Height)) openvg.End() case '5': // back n-- if n < 0 { n = lastslide } showslide(d, imap, n) case '6': // forward n++ if n > lastslide { n = 0 } showslide(d, imap, n) } } // search case '/', 6: // slash, Ctrl-F openvg.RestoreTerm() searchterm, serr := r.ReadString('\n') openvg.RawTerm() if serr != nil { continue } if len(searchterm) > 2 { ns := deck.Search(d, searchterm[0:len(searchterm)-1]) if ns >= 0 { showslide(d, imap, ns) n = ns } } } } }