// adjust the font to fit within a width func (f *FW) fitwidth(width, adj int, s string) { f.tw = openvg.TextWidth(s, f.font, f.fontsize) for f.tw > float64(width) { f.fontsize -= adj f.tw = openvg.TextWidth(s, f.font, f.fontsize) } }
// textwrap draws text at location, wrapping at the specified width func textwrap(x, y, w float64, s string, font string, size int, leading, factor float64, color string) { openvg.FillColor(color) wordspacing := openvg.TextWidth("M", font, size) words := strings.Split(s, " ") xp := x yp := y edge := x + w for i := 0; i < len(words); i++ { tw := openvg.TextWidth(words[i], font, size) openvg.Text(xp, yp, words[i], font, size) xp += tw + (wordspacing * factor) if xp > edge { xp = x yp -= leading } } }
// weather retrieves data from the forecast.io API, decodes and displays it. func (d *display) weather(latlong string) { wdim := dimen{x: 0, y: d.height / 2, width: d.width / 2, height: d.height / 2} r, err := netread(fmt.Sprintf(weatherfmt, weatherAPIkey, latlong)) if err != nil { fmt.Fprintf(os.Stderr, "Weather read error: %v\n", err) wdim.gerror(d.bgcolor, d.textcolor, "no weather") return } defer r.Close() var data Forecast err = json.NewDecoder(r).Decode(&data) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) wdim.gerror(d.bgcolor, d.textcolor, "no weather") return } x := d.width * 0.05 y := d.height * 0.70 wsize := d.width / 20 spacing := wsize * 2.0 w1 := int(wsize) w2 := w1 / 2 w3 := w1 / 4 c := data.Currently temp := fmt.Sprintf("%0.f°", c.Temperature) tw := openvg.TextWidth(temp, "sans", w1) wdim.regionFill(d.bgcolor, d.textcolor) openvg.Text(x, y, temp, "sans", w1) if c.Temperature-c.FeelsLike > 1 { openvg.Text(x, y-(spacing/3), fmt.Sprintf("(feels like %0.f°)", c.FeelsLike), "sans", w3) } openvg.Text(x, y+spacing, c.Summary, "sans", w2) if c.PrecipProb > 0 { openvg.Text(x, y-(spacing*.6), fmt.Sprintf("%0.f%% Chance of precipitation", c.PrecipProb*100), "sans", w3) } ic := dimen{ x: x + tw + d.width*0.01, y: d.height * 0.67, width: d.width / 10, height: d.width / 10, } switch c.Icon { case "clear-day": ic.sun("orange") case "clear-night": ic.moon(d.bgcolor, d.textcolor) case "rain": ic.rain("skyblue") case "snow": ic.snow(d.textcolor) case "wind": ic.wind(d.bgcolor, d.textcolor) case "fog": ic.fog(d.textcolor) case "cloudy": ic.cloud(d.textcolor) case "partly-cloudy-day": ic.pcloud(d.textcolor) case "partly-cloudy-night": ic.npcloud("darkgray", d.textcolor) } openvg.End() }