initial gui base

This commit is contained in:
Liam Galvin 2018-06-28 12:26:48 +01:00
parent 6e12fe74a1
commit d5e398ac22
162 changed files with 41235 additions and 7 deletions

74
Gopkg.lock generated
View File

@ -1,9 +1,81 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/4ydx/gltext"
packages = [
".",
"v4.1"
]
revision = "0cb49edd9bd9c90f54449abdf2efc7d2efdb97b3"
[[projects]]
branch = "master"
name = "github.com/go-gl/gl"
packages = ["v4.1-core/gl"]
revision = "68e2537930806bfcee1b92a0e14b4e9b8bb3a3e3"
[[projects]]
branch = "master"
name = "github.com/go-gl/glfw"
packages = ["v3.2/glfw"]
revision = "46a8d530c3268b31eeb1310f30fe42be1cae98f2"
[[projects]]
branch = "master"
name = "github.com/go-gl/mathgl"
packages = ["mgl32"]
revision = "5ab0e04e1f55a10f3a71b1fccc60ca2ee9a0bc02"
[[projects]]
branch = "master"
name = "github.com/golang/freetype"
packages = [
".",
"raster",
"truetype"
]
revision = "e2365dfdc4a05e4b8299a783240d4a7d5a65d4e4"
[[projects]]
name = "go.uber.org/atomic"
packages = ["."]
revision = "1ea20fb1cbb1cc08cbd0d913a96dead89aa18289"
version = "v1.3.2"
[[projects]]
name = "go.uber.org/multierr"
packages = ["."]
revision = "3c4937480c32f4c13a875a1829af76c98ca3d40a"
version = "v1.1.0"
[[projects]]
name = "go.uber.org/zap"
packages = [
".",
"buffer",
"internal/bufferpool",
"internal/color",
"internal/exit",
"zapcore"
]
revision = "eeedf312bc6c57391d84767a4cd413f02a917974"
version = "v1.8.0"
[[projects]]
branch = "master"
name = "golang.org/x/image"
packages = [
"font",
"math/f32",
"math/fixed"
]
revision = "cc896f830cedae125428bc9fe1b0362aa91b3fb1"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "ab4fef131ee828e96ba67d31a7d690bd5f2f42040c6766b1b12fe856f87e0ff7"
inputs-digest = "e66cc362b8904acc9c4fd94bc590e57630d71a2f12874668f55092d181855b7c"
solver-name = "gps-cdcl"
solver-version = 1

5
config/config.go Normal file
View File

@ -0,0 +1,5 @@
package config
type Config struct {
DebugMode bool
}

Binary file not shown.

View File

@ -1,3 +1,185 @@
package gui
import (
"fmt"
"os"
"runtime"
"github.com/4ydx/gltext"
v41 "github.com/4ydx/gltext/v4.1"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/go-gl/mathgl/mgl32"
"gitlab.com/liamg/terminal/config"
"go.uber.org/zap"
"golang.org/x/image/math/fixed"
)
type GUI struct {
window *glfw.Window
logger *zap.SugaredLogger
config config.Config
font *v41.Font
width int
height int
}
func New(config config.Config, logger *zap.SugaredLogger) *GUI {
//logger.
return &GUI{
config: config,
logger: logger,
width: 600,
height: 300,
}
}
// inspired by https://kylewbanks.com/blog/tutorial-opengl-with-golang-part-1-hello-opengl
func (gui *GUI) SetSize(w int, h int) {
gui.window.SetSize(w, h)
gui.resize(gui.window, w, h)
}
func (gui *GUI) resize(w *glfw.Window, width int, height int) {
gui.width = width
gui.height = height
if gui.font != nil {
gui.font.ResizeWindow(float32(width), float32(height))
}
gl.Viewport(0, 0, int32(width), int32(height))
}
func (gui *GUI) getTermSize() (int, int) {
scaleMin, scaleMax := float32(1.0), float32(1.1)
text := v41.NewText(gui.font, scaleMin, scaleMax)
text.SetString("A")
return gui.width / int(text.Width()), gui.height / int(text.Height())
}
func (gui *GUI) Render() error {
gui.logger.Debugf("Locking OS thread...")
runtime.LockOSThread()
gui.logger.Debugf("Creating window...")
var err error
gui.window, err = gui.createWindow(gui.width, gui.height)
if err != nil {
return fmt.Errorf("Failed to create window: %s", err)
}
defer glfw.Terminate()
gui.logger.Debugf("Initialising OpenGL and creating program...")
program, err := gui.createProgram()
if err != nil {
return fmt.Errorf("Failed to initialise OpenGL: %s", err)
}
gui.logger.Debugf("Loading font...")
//gui.font, err = gui.loadFont("/usr/share/fonts/nerd-fonts-complete/ttf/Roboto Mono Nerd Font Complete.ttf", 12)
if err := gui.loadFont("./fonts/CamingoCode-Regular.ttf", 12); err != nil {
return fmt.Errorf("Failed to load font: %s", err)
}
gui.resize(gui.window, gui.width, gui.height)
gui.window.SetFramebufferSizeCallback(gui.resize)
w, h := gui.window.GetFramebufferSize()
gl.Viewport(0, 0, int32(w), int32(h))
scaleMin, scaleMax := float32(1.0), float32(1.1)
text := v41.NewText(gui.font, scaleMin, scaleMax)
text.SetString("hello")
text.SetColor(mgl32.Vec3{1, 1, 1})
text.SetPosition(mgl32.Vec2{-float32(gui.width) / 2, -float32(gui.height) / 2})
text.SetPosition(mgl32.Vec2{0, 0})
//gl.Disable(gl.MULTISAMPLE)
// stop smoothing fonts
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gui.logger.Debugf("Starting render...")
for !gui.window.ShouldClose() {
gl.UseProgram(program)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// Render the string.
text.Draw()
glfw.PollEvents()
gui.window.SwapBuffers()
}
gui.logger.Debugf("Stopping render...")
return nil
}
func (gui *GUI) loadFont(path string, scale int32) error {
fd, err := os.Open(path)
if err != nil {
return err
}
defer fd.Close()
runeRanges := make(gltext.RuneRanges, 0)
runeRanges = append(runeRanges, gltext.RuneRange{Low: 32, High: 127})
/*runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x3000, High: 0x3030})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x3040, High: 0x309f})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x30a0, High: 0x30ff})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0x4e00, High: 0x9faf})
runeRanges = append(runeRanges, gltext.RuneRange{Low: 0xff00, High: 0xffef})
*/
runesPerRow := fixed.Int26_6(128)
conf, err := gltext.NewTruetypeFontConfig(fd, fixed.Int26_6(scale), runeRanges, runesPerRow)
if err != nil {
return err
}
font, err := v41.NewFont(conf)
if err != nil {
return err
}
font.ResizeWindow(float32(gui.width), float32(gui.height))
gui.font = font
return nil
}
func (gui *GUI) createWindow(width int, height int) (*glfw.Window, error) {
if err := glfw.Init(); err != nil {
return nil, err
}
glfw.WindowHint(glfw.Resizable, glfw.True)
glfw.WindowHint(glfw.ContextVersionMajor, 4) // OR 2
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
window, err := glfw.CreateWindow(width, height, "Terminal", nil, nil)
if err != nil {
return nil, err
}
window.MakeContextCurrent()
return window, nil
}
// initOpenGL initializes OpenGL and returns an intiialized program.
func (gui *GUI) createProgram() (uint32, error) {
if err := gl.Init(); err != nil {
return 0, fmt.Errorf("Failed to initialise OpenGL: %s", err)
}
gui.logger.Infof("OpenGL version %s", gl.GoStr(gl.GetString(gl.VERSION)))
prog := gl.CreateProgram()
gl.LinkProgram(prog)
return prog, nil
}

39
main.go
View File

@ -1,21 +1,48 @@
package main
import (
"io"
"fmt"
"os"
"gitlab.com/liamg/terminal/pty"
"gitlab.com/liamg/terminal/config"
"gitlab.com/liamg/terminal/gui"
"go.uber.org/zap"
)
func main() {
pty, err := pty.NewPtyWithShell()
// parse this
conf := config.Config{DebugMode: true}
var logger *zap.Logger
var err error
if conf.DebugMode {
logger, err = zap.NewDevelopment()
} else {
logger, err = zap.NewProduction()
}
if err != nil {
panic(err)
fmt.Printf("Failed to create logger: %s", err)
os.Exit(1)
}
sugaredLogger := logger.Sugar()
defer sugaredLogger.Sync()
/*
sugaredLogger.Infof("Allocationg pty...")
pty, err := pty.NewPtyWithShell()
if err != nil {
panic(err)
}
*/
g := gui.New(conf, sugaredLogger)
if err := g.Render(); err != nil {
sugaredLogger.Fatalf("Render error: %s", err)
}
go io.Copy(pty, os.Stdin)
io.Copy(os.Stdout, pty)
//go io.Copy(pty, os.Stdin)
//io.Copy(os.Stdout, pty)
// return pty, err
}

2
vendor/github.com/4ydx/gltext/.gitignore generated vendored Normal file
View File

@ -0,0 +1,2 @@
tags
fontconfigs

13
vendor/github.com/4ydx/gltext/CONTRIBUTORS generated vendored Normal file
View File

@ -0,0 +1,13 @@
# This is the list of people who can contribute (or have contributed) to this
# project. This includes code, documentation, testing, content creation and
# bugfixes.
#
# Names should be added to this file like so:
# Name [<email address>]
#
# Please keep the list sorted.
André Moraes <andre@amoraes.info / andrebq@gmail.com>
Jim Teeuwen <jimteeuwen@gmail.com>
Nathan Findley <nathan@4ydx.com>
Tim Howard <plash@komiga.com>

27
vendor/github.com/4ydx/gltext/LICENSE generated vendored Normal file
View File

@ -0,0 +1,27 @@
Copyright (c) 2012 The go-gl Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of go-gl nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

35
vendor/github.com/4ydx/gltext/README.md generated vendored Normal file
View File

@ -0,0 +1,35 @@
## Modern opengl text rendering
A simple package for rendering a string using modern opengl. Based on the bounding
box of a string, positioning of the string on screen prior to rendering is possible.
There do seem to be issues with the dimensions reported by freetype-go unfortunately.
- Unicode support.
- Dynamic text zooming along the z-axis.
- Dynamic text positioning within the orthographic projection space.
- Dynamic color changes.
Unicode support is based on the underlying truetype font being used (or bitmap).
![Alt text](/example/example.png?raw=true "Simple Screenshot")
### Install
* go get github.com/4ydx/gltext
### Example
* Provided using Japanese text.
### Dependencies
This packages uses [freetype-go](https://github.com/golang/freetype) which is licensed
under GPLv2 and FTL licenses. You can choose which one is a better fit for your
use case but FTL requires you to give some form of credit to Freetype.org
### License
Copyright 2012 The go-gl Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.

90
vendor/github.com/4ydx/gltext/charset.go generated vendored Normal file
View File

@ -0,0 +1,90 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gltext
// A Glyph describes metrics for a single font glyph.
// These indicate which area of a given image contains the
// glyph data and how the glyph should be spaced in a rendered string.
type Point struct {
X float32
Y float32
}
type Glyph struct {
X int `json:"x"` // The x location of the glyph on a sprite sheet.
Y int `json:"y"` // The y location of the glyph on a sprite sheet.
Width int `json:"width"` // The width of the glyph on a sprite sheet.
Height int `json:"height"` // The height of the glyph on a sprite sheet.
// Advance determines the distance to the next glyph.
// This is used to properly align non-monospaced fonts.
Advance int `json:"advance"`
}
func (g *Glyph) GetTexturePositions(font FontLike) (tP1, tP2 Point) {
// Quad width/height
// Originally the ttf width value was being used. This, however, differs from the Advance value.
// This has been changed to advance so that the resulting quads that are generated for text to not
// overlap one another.
vw := float32(g.Advance)
vh := float32(g.Height)
// Unfortunately with the current font, if I don't add a small offset to the Y axis location
// the bottom edge of the character above might appear.
//
// EG:
// Wrapping 16 characters per line:
// runesPerRow := fixed.Int26_6(16)
// runeRanges := make(gltext.RuneRanges, 0)
// runeRange := gltext.RuneRange{Low: 1, High: 128}
// runeRanges = append(runeRanges, runeRange)
//
// The resulting image file will place "g" above "w". The very bottom edge of "g" will show up
// when using the "w" character in a line of text. So the dirty hack is to remove just a bit of
// the original top as per below. This is not ideal. Either I am not understanding something
// about the glyph layout or this will have to be tweaked based on the font being used.
// See the file example_image.png.
// texture point 1
tP1 = Point{X: float32(g.X) / font.GetTextureWidth(), Y: float32(g.Y) / font.GetTextureHeight()}
// texture point 2
tP2 = Point{X: (float32(g.X) + vw) / font.GetTextureWidth(), Y: (float32(g.Y) + vh) / font.GetTextureHeight()}
return
}
// A Charset represents a set of glyph descriptors for a font.
// Each glyph descriptor holds glyph metrics which are used to
// properly align the given glyph in the resulting rendered string.
type Charset []Glyph
// Scale scales all glyphs by the given factor and repositions them
// appropriately. A scale of 1 retains the original size. A scale of 2
// doubles the size of each glyph, etc.
//
// This is useful when the accompanying sprite sheet is scaled by the
// same factor. In this case, we want the glyph data to match up with the
// new image.
func (c Charset) Scale(factor int) {
if factor <= 1 {
// A factor of zero results in zero-sized glyphs and
// is therefore not valid. A factor of 1 does not change
// the glyphs, so we can ignore it.
return
}
// Multiply each glyph field by the given factor
// to scale them up to the new size.
for i := range c {
c[i].X *= factor
c[i].Y *= factor
c[i].Width *= factor
c[i].Height *= factor
c[i].Advance *= factor
}
}

60
vendor/github.com/4ydx/gltext/debug.go generated vendored Normal file
View File

@ -0,0 +1,60 @@
package gltext
import (
"fmt"
"runtime"
)
var IsDebug = false
func DebugPrefix() string {
_, fn, line, _ := runtime.Caller(1)
return fmt.Sprintf("DB: [%s:%d]", fn, line)
}
func TextDebug(message string) {
if IsDebug {
pc, fn, line, _ := runtime.Caller(1)
fmt.Printf("[error] in %s[%s:%d] %s", runtime.FuncForPC(pc).Name(), fn, line, message)
}
}
// PrintVBO prints the individual index locations as well as the texture locations
//
// (0,0) (x1,y1): This shows the layout of the runes. There relative locations to one another can be seen here.
// - If called just after makeBufferData, the left-most x value will start at 0.
// - If called after centerTheData, all indices will have been shifted so that the entire text value is
// centered around the screen's origin of (0,0).
//
// (U,V) (u1,v1) -> (x,y): The (x,y) values refer to pixel locations within the texture
// - Open the texture in an image editor and, using the upper left hand corner as (0,0)
// move to the location (x,y). This is where opengl will pinpoint your rune within the image.
func PrintVBO(vbo []float32, w, h float32) {
if len(vbo)%4 != 0 {
fmt.Println("VBO appears to have an incorrect size. Should be a multiple of 4.")
}
// drawing a quad take 16 floats: (2 x,y + 2 u,v) * 4 indices
for i := 0; i < len(vbo); i += 16 {
fmt.Println("Quad")
at := i
fmt.Printf(
"(0,0) (%.2f,%.2f); (U,V) (%f,%f) -> (%f,%f)\n",
vbo[at], vbo[at+1], vbo[at+2], vbo[at+3], vbo[at+2]*w, vbo[at+3]*h,
)
at += 4
fmt.Printf(
"(1,0) (%.2f,%.2f); (U,V) (%f,%f) -> (%f,%f)\n",
vbo[at], vbo[at+1], vbo[at+2], vbo[at+3], vbo[at+2]*w, vbo[at+3]*h,
)
at += 4
fmt.Printf(
"(1,1) (%.2f,%.2f); (U,V) (%f,%f) -> (%f,%f)\n",
vbo[at], vbo[at+1], vbo[at+2], vbo[at+3], vbo[at+2]*w, vbo[at+3]*h,
)
at += 4
fmt.Printf(
"(0,1) (%.2f,%.2f); (U,V) (%f,%f) -> (%f,%f)\n",
vbo[at], vbo[at+1], vbo[at+2], vbo[at+3], vbo[at+2]*w, vbo[at+3]*h,
)
}
}

42
vendor/github.com/4ydx/gltext/example/font/COPYING generated vendored Normal file
View File

@ -0,0 +1,42 @@
Luxi fonts copyright (c) 2001 by Bigelow & Holmes Inc. Luxi font
instruction code copyright (c) 2001 by URW++ GmbH. All Rights
Reserved. Luxi is a registered trademark of Bigelow & Holmes Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of these Fonts and associated documentation files (the "Font
Software"), to deal in the Font Software, including without
limitation the rights to use, copy, merge, publish, distribute,
sublicense, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to
the following conditions:
The above copyright and trademark notices and this permission notice
shall be included in all copies of one or more of the Font Software.
The Font Software may not be modified, altered, or added to, and in
particular the designs of glyphs or characters in the Fonts may not
be modified nor may additional glyphs or characters be added to the
Fonts. This License becomes null and void when the Fonts or Font
Software have been modified.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL
BIGELOW & HOLMES INC. OR URW++ GMBH. BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR
INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT
SOFTWARE.
Except as contained in this notice, the names of Bigelow & Holmes
Inc. and URW++ GmbH. shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Font Software without
prior written authorization from Bigelow & Holmes Inc. and URW++ GmbH.
For further information, contact:
info@urwpp.de
or
design@bigelowandholmes.com

109
vendor/github.com/4ydx/gltext/fontconfig.go generated vendored Normal file
View File

@ -0,0 +1,109 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gltext
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"image"
"image/png"
"io/ioutil"
"os"
"time"
)
// Direction represents the direction in which strings should be rendered.
type Direction uint8
// FontConfig describes raster font metadata.
//
// It can be loaded from, or saved to a JSON encoded file,
// which should come with any bitmap font image.
type FontConfig struct {
// The range of glyphs covered by this fontconfig
// An array of Low, High values allowing the user to select disjoint subsets of the ttf
RuneRanges RuneRanges
// Glyphs holds a set of glyph descriptors, defining the location,
// size and advance of each glyph in the sprite sheet.
Glyphs Charset
Image *image.NRGBA `json:"-"`
Name string
}
// Load reads font configuration data from the given JSON encoded stream.
func (fc *FontConfig) Load(rootPath string) (err error) {
file := fmt.Sprintf("%s/%s.config", rootPath, fc.Name)
data, err := ioutil.ReadFile(file)
if err != nil {
return
}
err = json.Unmarshal(data, fc)
if err != nil {
return err
}
fmt.Printf("%+v\n", time.Now())
fc.Image, err = LoadFontImage(rootPath, fc.Name)
if err != nil {
return err
}
fmt.Printf("%+v\n", time.Now())
fc.Glyphs.Scale(1)
return nil
}
// Save writes font configuration data to the given stream as JSON data.
func (fc *FontConfig) Save(rootPath string) error {
if _, err := os.Stat(rootPath); err != nil {
if os.IsNotExist(err) {
os.MkdirAll(rootPath, os.ModeDir|os.ModePerm)
} else {
return err
}
}
data, err := json.Marshal(fc)
if err != nil {
return err
}
file := fmt.Sprintf("%s/%s.config", rootPath, fc.Name)
err = ioutil.WriteFile(file, data, os.ModePerm)
if err != nil {
return err
}
if fc.Image == nil {
return errors.New("Should not be nil.")
}
err = SaveImage(rootPath, fc.Name, fc.Image)
if err != nil {
return err
}
err = ioutil.WriteFile(file, data, os.ModePerm)
return err
}
func LoadFontImage(rootPath, name string) (*image.NRGBA, error) {
file := fmt.Sprintf("%s/%s.png", rootPath, name)
return LoadImage(file)
}
func SaveImage(rootPath, name string, img *image.NRGBA) error {
file := fmt.Sprintf("%s/%s.png", rootPath, name)
image, err := os.Create(file)
if err != nil {
return err
}
defer image.Close()
b := bufio.NewWriter(image)
err = png.Encode(b, img)
if err != nil {
return err
}
return b.Flush()
}

10
vendor/github.com/4ydx/gltext/fontlike.go generated vendored Normal file
View File

@ -0,0 +1,10 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gltext
type FontLike interface {
GetTextureWidth() float32
GetTextureHeight() float32
}

112
vendor/github.com/4ydx/gltext/misc.go generated vendored Normal file
View File

@ -0,0 +1,112 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gltext
import (
"errors"
"fmt"
"image"
"image/color"
"os"
)
// Pow2 returns the first power-of-two value >= to n.
// This can be used to create suitable texture dimensions.
func Pow2(x uint32) uint32 {
x--
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
return x + 1
}
// IsPow2 returns true if the given value is a power-of-two.
func IsPow2(x uint32) bool { return (x & (x - 1)) == 0 }
// Pow2Image returns the given image, scaled to the smallest power-of-two
// dimensions larger or equal to the input dimensions.
// It preserves the image format and contents.
//
// This is useful if an image is to be used as an OpenGL texture.
// These often require image data to have power-of-two dimensions.
func Pow2Image(src image.Image) image.Image {
sb := src.Bounds()
w, h := uint32(sb.Dx()), uint32(sb.Dy())
if IsPow2(w) && IsPow2(h) {
return src // Nothing to do.
}
rect := image.Rect(0, 0, int(Pow2(w)), int(Pow2(h)))
switch src := src.(type) {
case *image.Alpha:
return copyImg(src, image.NewAlpha(rect))
case *image.Alpha16:
return copyImg(src, image.NewAlpha16(rect))
case *image.Gray:
return copyImg(src, image.NewGray(rect))
case *image.Gray16:
return copyImg(src, image.NewGray16(rect))
case *image.NRGBA:
return copyImg(src, image.NewNRGBA(rect))
case *image.NRGBA64:
return copyImg(src, image.NewNRGBA64(rect))
case *image.Paletted:
return copyImg(src, image.NewPaletted(rect, src.Palette))
case *image.RGBA:
return copyImg(src, image.NewRGBA(rect))
case *image.RGBA64:
return copyImg(src, image.NewRGBA64(rect))
}
panic(fmt.Sprintf("Unsupported image format: %T", src))
}
// Why the image.Image interface does not support this,
// I can never understand.
type copyable interface {
image.Image
Set(x, y int, clr color.Color)
}
func copyImg(src, dst copyable) image.Image {
var x, y int
sb := src.Bounds()
for y = 0; y < sb.Dy(); y++ {
for x = 0; x < sb.Dx(); x++ {
dst.Set(x, y, src.At(x, y))
}
}
return dst
}
func LoadImage(path string) (*image.NRGBA, error) {
img, err := os.Open(path)
if err != nil {
return nil, err
}
pix, _, err := image.Decode(img)
if err != nil {
return nil, err
}
p, ok := pix.(*image.NRGBA)
if ok {
return p, nil
}
return nil, errors.New("Not a NRGBA image.")
}

163
vendor/github.com/4ydx/gltext/truetype.go generated vendored Normal file
View File

@ -0,0 +1,163 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gltext
import (
"errors"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/math/fixed"
"image"
"image/draw"
"io"
"io/ioutil"
"sort"
)
// RuneRanges specify the rune ranges for ordered disjoint subsets of the ttf
// EG 32 - 127, 5000 - 6000 will created a more compact bitmap that holds the
// specified ranges of runes.
type RuneRange struct {
Low, High rune
}
type RuneRanges []RuneRange
func (rr RuneRanges) Len() int { return len(rr) }
func (rr RuneRanges) Swap(i, j int) { rr[i], rr[j] = rr[j], rr[i] }
func (rr RuneRanges) Less(i, j int) bool { return rr[i].Low < rr[j].Low }
func (rr RuneRanges) Validate() bool {
sort.Sort(rr)
previousMax := rune(0)
for _, r := range rr {
if r.Low <= previousMax {
return false
}
if r.Low > r.High {
return false
}
previousMax = r.High
}
return true
}
// GetGlyphIndex returns the location of the glyph data within
// the compressed rune ranges covered by the font
// EG if runes 0-25, 100-110 are supported by the font then
// the actual location of 100 will be in position 26 in the png image
func (rr RuneRanges) GetGlyphIndex(char rune) rune {
var index, offset rune
index = -1
for _, runes := range rr {
if char >= runes.Low && char <= runes.High {
index = char - runes.Low + offset
}
offset += runes.High - runes.Low + 1
}
return index
}
// http://www.freetype.org/freetype2/docs/tutorial/step2.html
// LoadTruetype loads a truetype font from the given stream and
// applies the given font scale in points.
//
// The low and high values determine the lower and upper rune limits
// we should load for this font. For standard ASCII this would be: 32, 127.
func NewTruetypeFontConfig(r io.Reader, scale fixed.Int26_6, runeRanges RuneRanges, runesPerRow fixed.Int26_6) (*FontConfig, error) {
if !runeRanges.Validate() {
return nil, errors.New("Invalid rune ranges supplied.")
}
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
// Read the truetype font.
ttf, err := truetype.Parse(data)
if err != nil {
return nil, err
}
// Create our FontConfig type.
fc := &FontConfig{}
length := rune(0)
for _, r := range runeRanges {
length += r.High - r.Low + 1
}
fc.RuneRanges = runeRanges
fc.Glyphs = make(Charset, int(length))
// Create an image, large enough to store all requested glyphs.
// The resulting image is set to power of 2 dimensions so it might be wise to adjust the runesPerRow
// parameter to ensure that unnecessary space isn't created based on the character set being used
gc := fixed.Int26_6(len(fc.Glyphs))
runesPerCol := (gc / runesPerRow) + 1
gb := ttf.Bounds(scale)
gw := (gb.Max.X - gb.Min.X)
gh := (gb.Max.Y - gb.Min.Y)
iw := Pow2(uint32(gw * runesPerRow))
ih := Pow2(uint32(gh * runesPerCol))
fg, bg := image.White, image.Transparent
rect := image.Rect(0, 0, int(iw), int(ih))
fc.Image = image.NewNRGBA(rect)
draw.Draw(fc.Image, fc.Image.Bounds(), bg, image.ZP, draw.Src)
// Use a freetype context to do the drawing.
c := freetype.NewContext()
c.SetDPI(72) // Do not change this. It is required in order to have a properly aligned bounding box!!!
c.SetFont(ttf)
c.SetFontSize(float64(scale))
c.SetClip(fc.Image.Bounds())
c.SetDst(fc.Image)
c.SetSrc(fg)
// Iterate over all relevant glyphs in the truetype font and draw them all to the image buffer
// Add Glyph objects to track various glyph values
var gi fixed.Int26_6
var gx, gy fixed.Int26_6
for _, runeRange := range fc.RuneRanges {
for ch := runeRange.Low; ch <= runeRange.High; ch++ {
index := ttf.Index(ch)
metric := ttf.HMetric(scale, index)
if gi%runesPerRow == 0 {
gx = 0
if gi > 0 {
gy += gh
}
} else {
gx += gw
}
fc.Glyphs[gi].Advance = int(metric.AdvanceWidth)
fc.Glyphs[gi].X = int(gx)
fc.Glyphs[gi].Y = int(gy)
fc.Glyphs[gi].Width = int(gw)
fc.Glyphs[gi].Height = int(gh)
pt := freetype.Pt(int(gx), int(gy)+int(c.PointToFixed(float64(scale))>>6))
c.DrawString(string(ch), pt)
gi++
}
}
return fc, nil
}
func LoadTruetypeFontConfig(rootPath, name string) (*FontConfig, error) {
fc := &FontConfig{}
fc.Name = name
err := fc.Load(rootPath)
if err != nil {
return nil, err
}
return fc, nil
}

182
vendor/github.com/4ydx/gltext/v4.1/bounding_box.go generated vendored Normal file
View File

@ -0,0 +1,182 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package v41
import (
"fmt"
"github.com/4ydx/gltext"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
var boxVertexShaderSource string = `
#version 330
uniform mat4 orthographic_matrix;
uniform vec2 final_position;
in vec4 centered_position;
void main() {
vec4 center = orthographic_matrix * centered_position;
gl_Position = vec4(center.x + final_position.x, center.y + final_position.y, center.z, center.w);
}
` + "\x00"
var boxFragmentShaderSource string = `
#version 330
out vec4 fragment_color;
void main() {
fragment_color = vec4(0.3,0.3,0.3,1);
}
` + "\x00"
type BoundingBox struct {
program uint32 // program compiled from shaders
// font holds our orthographic matrix
font *Font
// attributes
centeredPosition uint32 // vertex position
// the final screen position post-scaling
finalPositionUniform int32
finalPosition mgl32.Vec2
// transform to orthographic projection
orthographicMatrixUniform int32
vao uint32
vbo uint32
ebo uint32
windowWidth float32
windowHeight float32
vboData []float32
vboIndexCount int
eboData []int32
eboIndexCount int
// X1, X2: the lower left and upper right points of a box that bounds the text
X1 gltext.Point
X2 gltext.Point
}
func loadBoundingBox(f *Font, X1 gltext.Point, X2 gltext.Point) (b *BoundingBox, err error) {
b = new(BoundingBox)
b.font = f
// create shader program and define attributes and uniforms
b.program, err = NewProgram(boxVertexShaderSource, boxFragmentShaderSource)
if err != nil {
return b, err
}
// ebo, vbo data
b.vboIndexCount = 4 * 2 // 4 indexes per bounding box (containing 2 position)
b.eboIndexCount = 6 // each rune requires 6 triangle indices for a quad
b.vboData = make([]float32, b.vboIndexCount, b.vboIndexCount)
b.eboData = make([]int32, b.eboIndexCount, b.eboIndexCount)
b.makeBufferData(X1, X2)
if gltext.IsDebug {
prefix := gltext.DebugPrefix()
fmt.Printf("%s bounding %v %v\n", prefix, X1, X2)
fmt.Printf("%s bounding vbo data\n%v\n", prefix, b.vboData)
fmt.Printf("%s bounding ebo data\n%v\n", prefix, b.eboData)
}
// attributes
b.centeredPosition = uint32(gl.GetAttribLocation(b.program, gl.Str("centered_position\x00")))
// uniforms
b.finalPositionUniform = gl.GetUniformLocation(b.program, gl.Str("final_position\x00"))
b.orthographicMatrixUniform = gl.GetUniformLocation(b.program, gl.Str("orthographic_matrix\x00"))
// size of glfloat
glfloatSize := int32(4)
gl.GenVertexArrays(1, &b.vao)
gl.GenBuffers(1, &b.vbo)
gl.GenBuffers(1, &b.ebo)
// vao
gl.BindVertexArray(b.vao)
// vbo
// specify the buffer for which the VertexAttribPointer calls apply
gl.BindBuffer(gl.ARRAY_BUFFER, b.vbo)
gl.EnableVertexAttribArray(b.centeredPosition)
gl.VertexAttribPointer(
b.centeredPosition,
2,
gl.FLOAT,
false,
0,
gl.PtrOffset(0),
)
gl.BufferData(gl.ARRAY_BUFFER, int(glfloatSize)*b.vboIndexCount, gl.Ptr(b.vboData), gl.DYNAMIC_DRAW)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b.ebo)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int(glfloatSize)*b.eboIndexCount, gl.Ptr(b.eboData), gl.DYNAMIC_DRAW)
gl.BindVertexArray(0)
// not necesssary, but i just want to better understand using vertex arrays
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
return b, nil
}
func (b *BoundingBox) Release() {
gl.DeleteBuffers(1, &b.vbo)
gl.DeleteBuffers(1, &b.ebo)
gl.DeleteBuffers(1, &b.vao)
}
func (b *BoundingBox) Draw() {
gl.UseProgram(b.program)
// uniforms
gl.Uniform2fv(b.finalPositionUniform, 1, &b.finalPosition[0])
gl.UniformMatrix4fv(b.orthographicMatrixUniform, 1, false, &b.font.OrthographicMatrix[0])
// draw
gl.BindVertexArray(b.vao)
gl.DrawElements(gl.TRIANGLES, int32(b.eboIndexCount), gl.UNSIGNED_INT, nil)
gl.BindVertexArray(0)
}
func (b *BoundingBox) makeBufferData(X1, X2 gltext.Point) {
// counter-clockwise quad
// index (0,0)
b.vboData[0] = X1.X // position
b.vboData[1] = X1.Y
// index (1,0)
b.vboData[2] = X2.X
b.vboData[3] = X1.Y
// index (1,1)
b.vboData[4] = X2.X
b.vboData[5] = X2.Y
// index (0,1)
b.vboData[6] = X1.X
b.vboData[7] = X2.Y
// ebo data
b.eboData[0] = 0
b.eboData[1] = 1
b.eboData[2] = 2
b.eboData[3] = 0
b.eboData[4] = 2
b.eboData[5] = 3
return
}

177
vendor/github.com/4ydx/gltext/v4.1/font.go generated vendored Normal file
View File

@ -0,0 +1,177 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package v41
import (
"github.com/4ydx/gltext"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"
"image"
)
var fontVertexShaderSource string = `
#version 330
uniform mat4 scale_matrix;
uniform mat4 orthographic_matrix;
uniform vec2 final_position;
in vec4 centered_position;
in vec2 uv;
out vec2 fragment_uv;
// The orthographic projection uses a lower left-hand point of (0,0)
// 1) We center the text on screen.
// 2) We perform othographic transformation and then scaling.
// 3) We move the text to its final resting place.
// This is all pretty standard I would imagine, but it took me a bit to sort out what has to happen :P
void main() {
fragment_uv = uv;
vec4 scaled = scale_matrix * orthographic_matrix * centered_position;
gl_Position = vec4(scaled.x + final_position.x, scaled.y + final_position.y, scaled.z, scaled.w);
}
` + "\x00"
var fontFragmentShaderSource string = `
#version 330
uniform sampler2D fragment_texture;
uniform float fadeout;
uniform vec4 fragment_color_adjustment;
in vec2 fragment_uv;
out vec4 fragment_color;
void main() {
vec4 color = texture(fragment_texture, fragment_uv);
color.xyz = fragment_color_adjustment.xyz;
color.w = color.w - fadeout;
fragment_color = color;
}
` + "\x00"
type Font struct {
Config *gltext.FontConfig // Character set for this font.
textureID uint32 // Holds the glyph texture id.
maxGlyphWidth int // Largest glyph width.
maxGlyphHeight int // Largest glyph height.
program uint32 // program compiled from shaders
// attributes
centeredPositionAttribute uint32 // vertex centered_position required for scaling around the orthographic projections center
uvAttribute uint32 // texture position
// The final screen position post-scaling
finalPositionUniform int32
// Position of the shaders fragment texture variable
fragmentTextureUniform int32
// The desired color of the text
colorUniform int32
fadeoutUniform int32
// View matrix
orthographicMatrixUniform int32
OrthographicMatrix mgl32.Mat4
// Scale the resulting text
scaleMatrixUniform int32
textureWidth float32
textureHeight float32
WindowWidth float32
WindowHeight float32
}
func (f *Font) GetTextureWidth() float32 {
return f.textureWidth
}
func (f *Font) GetTextureHeight() float32 {
return f.textureHeight
}
func NewFont(config *gltext.FontConfig) (f *Font, err error) {
if config == nil {
panic("Nil config")
}
f = &Font{}
f.Config = config
// Resize image to next power-of-two.
config.Image = gltext.Pow2Image(config.Image).(*image.NRGBA)
ib := config.Image.Bounds()
f.textureWidth = float32(ib.Dx())
f.textureHeight = float32(ib.Dy())
for _, glyph := range config.Glyphs {
if glyph.Width > f.maxGlyphWidth {
f.maxGlyphWidth = glyph.Width
}
if glyph.Height > f.maxGlyphHeight {
f.maxGlyphHeight = glyph.Height
}
}
// save to disk for testing
if gltext.IsDebug {
err = gltext.SaveImage(".", "Debug", config.Image)
if err != nil {
return f, err
}
}
// generate texture
gl.GenTextures(1, &f.textureID)
gl.BindTexture(gl.TEXTURE_2D, f.textureID)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(ib.Dx()),
int32(ib.Dy()),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(config.Image.Pix),
)
gl.BindTexture(gl.TEXTURE_2D, 0)
// create shader program and define attributes and uniforms
f.program, err = NewProgram(fontVertexShaderSource, fontFragmentShaderSource)
if err != nil {
return f, err
}
// attributes
f.centeredPositionAttribute = uint32(gl.GetAttribLocation(f.program, gl.Str("centered_position\x00")))
f.uvAttribute = uint32(gl.GetAttribLocation(f.program, gl.Str("uv\x00")))
// uniforms
f.finalPositionUniform = gl.GetUniformLocation(f.program, gl.Str("final_position\x00"))
f.orthographicMatrixUniform = gl.GetUniformLocation(f.program, gl.Str("orthographic_matrix\x00"))
f.scaleMatrixUniform = gl.GetUniformLocation(f.program, gl.Str("scale_matrix\x00"))
f.fragmentTextureUniform = gl.GetUniformLocation(f.program, gl.Str("fragment_texture\x00"))
f.colorUniform = gl.GetUniformLocation(f.program, gl.Str("fragment_color_adjustment\x00"))
f.fadeoutUniform = gl.GetUniformLocation(f.program, gl.Str("fadeout\x00"))
return f, nil
}
func (f *Font) ResizeWindow(width float32, height float32) {
f.WindowWidth = width
f.WindowHeight = height
f.OrthographicMatrix = mgl32.Ortho2D(-f.WindowWidth/2, f.WindowWidth/2, -f.WindowHeight/2, f.WindowHeight/2)
}
func (f *Font) Release() {
gl.DeleteTextures(1, &f.textureID)
}

70
vendor/github.com/4ydx/gltext/v4.1/shaders.go generated vendored Normal file
View File

@ -0,0 +1,70 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package v41
import (
"errors"
"fmt"
"github.com/go-gl/gl/v4.1-core/gl"
"strings"
)
func NewProgram(vertexShaderSource, fragmentShaderSource string) (uint32, error) {
vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
if err != nil {
return 0, err
}
fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
if err != nil {
return 0, err
}
program := gl.CreateProgram()
gl.AttachShader(program, vertexShader)
gl.AttachShader(program, fragmentShader)
gl.LinkProgram(program)
var status int32
gl.GetProgramiv(program, gl.LINK_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))
gl.DeleteProgram(program)
gl.DeleteShader(vertexShader)
gl.DeleteShader(fragmentShader)
return 0, errors.New(fmt.Sprintf("failed to link program: %v", log))
}
gl.DetachShader(program, vertexShader)
gl.DetachShader(program, fragmentShader)
gl.DeleteShader(vertexShader)
gl.DeleteShader(fragmentShader)
return program, nil
}
func compileShader(source string, shaderType uint32) (uint32, error) {
shader := gl.CreateShader(shaderType)
csource := gl.Str(source)
gl.ShaderSource(shader, 1, &csource, nil)
gl.CompileShader(shader)
var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
return 0, fmt.Errorf("failed to compile %v: %v", source, log)
}
return shader, nil
}

538
vendor/github.com/4ydx/gltext/v4.1/text.go generated vendored Normal file
View File

@ -0,0 +1,538 @@
// Copyright 2012 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package v41
import (
"fmt"
"github.com/4ydx/gltext"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
// CharacterSide shows which side of a character is
// clicked
type CharacterSide int
const (
CSLeft CharacterSide = iota
CSRight
CSUnknown
)
// Text is not designed to be accessed concurrently
type Text struct {
Font *Font
// final position on screen
finalPosition mgl32.Vec2
// text color
color mgl32.Vec3
// scaling the text
Scale float32
ScaleMin float32
ScaleMax float32
scaleMatrix mgl32.Mat4
// Fadeout reduces alpha
FadeOutBegun bool
FadeOutFrameCount float32 // number of frames since drawing began
FadeOutPerFrame float32 // smaller value takes more time
// bounding box of text
BoundingBox *BoundingBox
// general opengl values
vao uint32
vbo uint32
ebo uint32
vboData []float32
vboIndexCount int
eboData []int32
eboIndexCount int
// determines how many prefix characters are drawn on screen
RuneCount int
// no longer than this string
MaxRuneCount int
// X1, X2: the lower left and upper right points of a box that bounds the text with a center point (0,0)
// lower left
X1 gltext.Point
// upper right
X2 gltext.Point
// Screen position away from center
Position mgl32.Vec2
String string
CharSpacing []float32
}
func (t *Text) GetLength() int {
return t.eboIndexCount / 6
}
// NewText creates a new text object with scaling boundaries
// the rest state of the text when not being interacted with
// is scaleMin. most likely one wants to use 1.0.
func NewText(f *Font, scaleMin, scaleMax float32) (t *Text) {
t = &Text{}
t.Font = f
// text hover values
// "resting state" of a text object is the min scale
t.ScaleMin, t.ScaleMax = scaleMin, scaleMax
t.SetScale(1)
glfloat_size := int32(4)
// stride of the buffered data
xy_count := int32(2)
stride := xy_count + int32(2)
gl.GenVertexArrays(1, &t.vao)
gl.GenBuffers(1, &t.vbo)
gl.GenBuffers(1, &t.ebo)
// vao
gl.BindVertexArray(t.vao)
// i think this call isnt necessary here
// gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, t.Font.textureID)
// vbo
// specify the buffer for which the VertexAttribPointer calls apply
gl.BindBuffer(gl.ARRAY_BUFFER, t.vbo)
gl.EnableVertexAttribArray(t.Font.centeredPositionAttribute)
gl.VertexAttribPointer(
t.Font.centeredPositionAttribute,
2,
gl.FLOAT,
false,
glfloat_size*stride,
gl.PtrOffset(0),
)
gl.EnableVertexAttribArray(t.Font.uvAttribute)
gl.VertexAttribPointer(
t.Font.uvAttribute,
2,
gl.FLOAT,
false,
glfloat_size*stride,
gl.PtrOffset(int(glfloat_size*xy_count)),
)
// ebo
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, t.ebo)
// i am guessing that order is important here
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
return t
}
// Release releases text resources.
func (t *Text) Release() {
gl.DeleteBuffers(1, &t.vbo)
gl.DeleteBuffers(1, &t.ebo)
gl.DeleteVertexArrays(1, &t.vao)
}
// SetScale returns true when a change occured
func (t *Text) SetScale(s float32) bool {
if s > t.ScaleMax || s < t.ScaleMin {
return false
}
t.Scale = s
t.scaleMatrix = mgl32.Scale3D(s, s, s)
return true
}
// AddScale returns true when a change occured
func (t *Text) AddScale(s float32) bool {
if s < 0 && t.Scale <= t.ScaleMin {
return false
}
if s > 0 && t.Scale >= t.ScaleMax {
return false
}
t.Scale += s
t.scaleMatrix = mgl32.Scale3D(t.Scale, t.Scale, t.Scale)
return true
}
func (t *Text) SetColor(color mgl32.Vec3) {
t.color = color
}
// SetString performs creates new vbo and ebo objects as well as to perform all
// binding required for displaying text to screen
func (t *Text) SetString(fs string, argv ...interface{}) {
indices := []rune(fmt.Sprintf(fs, argv...))
if t.MaxRuneCount > 0 && len(indices) > t.MaxRuneCount+1 {
indices = indices[0:t.MaxRuneCount]
}
t.String = string(indices)
// ebo, vbo data
glfloat_size := int32(4)
t.vboIndexCount = len(indices) * 4 * 2 * 2 // 4 indexes per rune (containing 2 position + 2 texture)
t.eboIndexCount = len(indices) * 6 // each rune requires 6 triangle indices for a quad
t.RuneCount = len(indices)
t.vboData = make([]float32, t.vboIndexCount, t.vboIndexCount)
t.eboData = make([]int32, t.eboIndexCount, t.eboIndexCount)
// generate the basic vbo data and bounding box
// center the vbo data around the orthographic (0,0) point
t.X1 = gltext.Point{0, 0}
t.X2 = gltext.Point{0, 0}
t.makeBufferData(indices)
t.centerTheData(t.getLowerLeft())
if gltext.IsDebug {
prefix := gltext.DebugPrefix()
fmt.Printf("%s bounding box %v %v\n", prefix, t.X1, t.X2)
fmt.Printf("%s text vbo data\n%v\n", prefix, t.vboData)
fmt.Printf("%s text ebo data\n%v\n", prefix, t.eboData)
}
if len(indices) > 0 {
// in the event that we have no data to draw dont bother here
gl.BindVertexArray(t.vao)
gl.BindBuffer(gl.ARRAY_BUFFER, t.vbo)
gl.BufferData(
gl.ARRAY_BUFFER, int(glfloat_size)*t.vboIndexCount, gl.Ptr(t.vboData), gl.DYNAMIC_DRAW)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, t.ebo)
gl.BufferData(
gl.ELEMENT_ARRAY_BUFFER, int(glfloat_size)*t.eboIndexCount, gl.Ptr(t.eboData), gl.DYNAMIC_DRAW)
gl.BindVertexArray(0)
// possibly not necesssary?
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0)
}
// SetString can be called at anytime. we want to make sure that if the user is updating the text,
// the previous position will be maintained
t.SetPosition(t.Position)
}
// The block of text is positioned around the center of the screen, which in this case must
// be considered (0,0). This is necessary for orthographic projection and scaling to work
// well together. If the text is *not* at (0,0), then scaling doesnt produce a direct zoom effect.
func (t *Text) getLowerLeft() (lowerLeft gltext.Point) {
lineWidthHalf := (t.X2.X - t.X1.X) / 2
lineHeightHalf := (t.X2.Y - t.X1.Y) / 2
lowerLeft.X = -lineWidthHalf
lowerLeft.Y = -lineHeightHalf
return
}
// SetPosition prepares variables passed to the shader as well as values
// used for bounding box calculations when clicking or hovering above text
func (t *Text) SetPosition(v mgl32.Vec2) {
// transform to orthographic coordinates ranged -1 to 1 for the shader
t.finalPosition[0] = v.X() / (t.Font.WindowWidth / 2)
t.finalPosition[1] = v.Y() / (t.Font.WindowHeight / 2)
if gltext.IsDebug {
t.BoundingBox.finalPosition[0] = v.X() / (t.Font.WindowWidth / 2)
t.BoundingBox.finalPosition[1] = v.Y() / (t.Font.WindowHeight / 2)
}
t.Position = v
}
func (t *Text) GetBoundingBox() (X1, X2 gltext.Point) {
x, y := t.Position.X(), t.Position.Y()
X1.X = t.X1.X + x
X1.Y = t.X1.Y + y
X2.X = t.X2.X + x
X2.Y = t.X2.Y + y
return
}
func (t *Text) Draw() {
if gltext.IsDebug {
t.BoundingBox.Draw()
}
if t.FadeOutBegun {
t.FadeOutFrameCount++
if t.FadeOutPerFrame*t.FadeOutFrameCount > 1 {
// prevent overflow
t.FadeOutFrameCount--
}
}
gl.UseProgram(t.Font.program)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, t.Font.textureID)
// uniforms
gl.Uniform1i(t.Font.fragmentTextureUniform, 0)
gl.Uniform1f(t.Font.fadeoutUniform, t.FadeOutPerFrame*t.FadeOutFrameCount)
gl.Uniform4fv(t.Font.colorUniform, 1, &t.color[0])
gl.Uniform2fv(t.Font.finalPositionUniform, 1, &t.finalPosition[0])
gl.UniformMatrix4fv(t.Font.orthographicMatrixUniform, 1, false, &t.Font.OrthographicMatrix[0])
gl.UniformMatrix4fv(t.Font.scaleMatrixUniform, 1, false, &t.scaleMatrix[0])
// draw
drawCount := int32(t.RuneCount * 6)
if drawCount > int32(t.eboIndexCount) {
drawCount = int32(t.eboIndexCount)
}
if drawCount <= 0 {
return
}
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.BindVertexArray(t.vao)
gl.DrawElements(gl.TRIANGLES, drawCount, gl.UNSIGNED_INT, nil)
gl.BindVertexArray(0)
gl.Disable(gl.BLEND)
}
func (t *Text) BeginFadeOut() {
if t.FadeOutBegun == false {
t.FadeOutBegun = true
t.FadeOutFrameCount = 0
}
}
func (t *Text) Show() {
t.FadeOutBegun = false
t.FadeOutFrameCount = 0
}
func (t *Text) Hide() {
t.FadeOutBegun = false
t.FadeOutFrameCount = 1.0 / t.FadeOutPerFrame
}
// centerTheData prepares the value "centered_position" found in the font shader
// as named, the function centers the text around the orthographic center of the screen
// expected to only be called within SetString
func (t *Text) centerTheData(lowerLeft gltext.Point) (err error) {
length := len(t.vboData)
for index := 0; index < length; {
// index (0,0)
t.vboData[index] += lowerLeft.X
index++
t.vboData[index] += lowerLeft.Y
index += 3 // skip texture data
// index (1,0)
t.vboData[index] += lowerLeft.X
index++
t.vboData[index] += lowerLeft.Y
index += 3
// index (1,1)
t.vboData[index] += lowerLeft.X
index++
t.vboData[index] += lowerLeft.Y
index += 3
// index (0,1)
t.vboData[index] += lowerLeft.X
index++
t.vboData[index] += lowerLeft.Y
index += 3
}
// update bounding box so that it is centered around (0,0)
t.X1.X += lowerLeft.X
t.X2.X += lowerLeft.X
t.X1.Y += lowerLeft.Y
t.X2.Y += lowerLeft.Y
// prepare objects for drawing the bounding box
if gltext.IsDebug {
t.BoundingBox, err = loadBoundingBox(t.Font, t.X1, t.X2)
}
return
}
func (t *Text) Width() float32 {
return t.X2.X - t.X1.X
}
func (t *Text) Height() float32 {
return t.X2.Y - t.X1.Y
}
// PrintCharSpacing is used for debugging
func (t *Text) PrintCharSpacing() {
fmt.Printf("\n%s:\n", t.String)
at := t.X1.X
for i, cs := range t.CharSpacing {
at = cs + at
fmt.Printf("'%c': %.2f ", t.String[i], at)
}
}
// ClickedCharacter should only be called after a bounding box hit is confirmed because
// it does not check y-axis values at all. Returns the index and side of the char clicked.
func (t *Text) ClickedCharacter(xPos, offset float64) (index int, side CharacterSide) {
// transform from screen coordinates to... window coordinates?
xPos = xPos - float64(t.Font.WindowWidth/2) - offset
// could do a binary search...
at := float64(t.X1.X)
for i, cs := range t.CharSpacing {
at = float64(cs) + at
if i == 0 && xPos <= at-float64(cs) {
return i, CSLeft
}
if i == len(t.CharSpacing)-1 && xPos > at {
return i, CSRight
}
if xPos <= at && xPos > at-float64(cs) {
if xPos-(at-float64(cs)) > float64(cs)/2 {
return i, CSRight
} else {
return i, CSLeft
}
}
}
return -1, CSUnknown
}
func (t *Text) CharPosition(index int) float64 {
at := float64(t.X1.X)
for i, cs := range t.CharSpacing {
if i == index {
break
}
at = float64(cs) + at
}
return at
}
func (t *Text) HasRune(r rune) bool {
for _, runes := range t.Font.Config.RuneRanges {
if r >= runes.Low && r <= runes.High {
return true
}
}
return false
}
// makeBufferData positions quads for drawing the text in the indices parameter using glyph dimensions
// it also generates the bounding box (which needs to later be centered around (0,0))
// expected to only be called by SetString
func (t *Text) makeBufferData(indices []rune) {
glyphs := t.Font.Config.Glyphs
vboIndex := 0
eboIndex := 0
lineX := float32(0)
eboOffset := int32(0)
t.CharSpacing = make([]float32, 0)
for i, r := range indices {
glyphIndex := t.Font.Config.RuneRanges.GetGlyphIndex(r)
if glyphIndex >= 0 {
if gltext.IsDebug {
prefix := gltext.DebugPrefix()
fmt.Printf("%s png index %3d: %s rune %+v line at %f", prefix, glyphIndex, string(r), glyphs[glyphIndex], lineX)
}
advance := float32(glyphs[glyphIndex].Advance)
// Originally the glyph Width was used, but that results in quads that overlap one another.
vw := float32(glyphs[glyphIndex].Advance)
vh := float32(glyphs[glyphIndex].Height)
// used to determine which character inside of the text was clicked
t.CharSpacing = append(t.CharSpacing, advance)
// variable width characters will produce a bounding box that is just
// a bit too long on the right-hand side unless we trim off the excess
// when processing the right-most character
trim := float32(0)
if i == len(indices)-1 {
trim = vw - advance
}
tP1, tP2 := glyphs[glyphIndex].GetTexturePositions(t.Font)
// counter-clockwise quad
// the bounding box value X2 is being expanded as characters are added
// index (0,0)
t.vboData[vboIndex] = lineX // position
vboIndex++
t.vboData[vboIndex] = 0
vboIndex++
t.vboData[vboIndex] = tP1.X // texture uv
vboIndex++
t.vboData[vboIndex] = tP2.Y
vboIndex++
// index (1,0) - expanding X2
t.vboData[vboIndex], t.X2.X = lineX+vw, lineX+vw-trim
vboIndex++
t.vboData[vboIndex] = 0
vboIndex++
t.vboData[vboIndex] = tP2.X
vboIndex++
t.vboData[vboIndex] = tP2.Y
vboIndex++
// index (1,1) - expanding X2
t.vboData[vboIndex] = lineX + vw
vboIndex++
t.vboData[vboIndex], t.X2.Y = vh, vh
vboIndex++
t.vboData[vboIndex] = tP2.X
vboIndex++
t.vboData[vboIndex] = tP1.Y
vboIndex++
// index (0,1)
t.vboData[vboIndex] = lineX
vboIndex++
t.vboData[vboIndex] = vh
vboIndex++
t.vboData[vboIndex] = tP1.X
vboIndex++
t.vboData[vboIndex] = tP1.Y
vboIndex++
// ebo data
t.eboData[eboIndex] = 0 + eboOffset
eboIndex++
t.eboData[eboIndex] = 1 + eboOffset
eboIndex++
t.eboData[eboIndex] = 2 + eboOffset
eboIndex++
t.eboData[eboIndex] = 0 + eboOffset
eboIndex++
t.eboData[eboIndex] = 2 + eboOffset
eboIndex++
t.eboData[eboIndex] = 3 + eboOffset
eboIndex++
eboOffset += 4
// shift to the right
lineX += advance
if gltext.IsDebug {
fmt.Printf("-> %f\n", lineX)
}
}
}
if gltext.IsDebug {
gltext.PrintVBO(t.vboData, t.Font.GetTextureHeight(), t.Font.GetTextureWidth())
}
return
}

21
vendor/github.com/go-gl/gl/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Eric Woroshow
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

109
vendor/github.com/go-gl/gl/v4.1-core/gl/conversions.go generated vendored Normal file
View File

@ -0,0 +1,109 @@
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
package gl
import (
"fmt"
"reflect"
"strings"
"unsafe"
)
// #include <stdlib.h>
import "C"
// Ptr takes a slice or pointer (to a singular scalar value or the first
// element of an array or slice) and returns its GL-compatible address.
//
// For example:
//
// var data []uint8
// ...
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
func Ptr(data interface{}) unsafe.Pointer {
if data == nil {
return unsafe.Pointer(nil)
}
var addr unsafe.Pointer
v := reflect.ValueOf(data)
switch v.Type().Kind() {
case reflect.Ptr:
e := v.Elem()
switch e.Kind() {
case
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
addr = unsafe.Pointer(e.UnsafeAddr())
default:
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
}
case reflect.Uintptr:
addr = unsafe.Pointer(v.Pointer())
case reflect.Slice:
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
default:
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
}
return addr
}
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
// Useful for functions such as glVertexAttribPointer that take pointer
// parameters indicating an offset rather than an absolute memory address.
func PtrOffset(offset int) unsafe.Pointer {
return unsafe.Pointer(uintptr(offset))
}
// Str takes a null-terminated Go string and returns its GL-compatible address.
// This function reaches into Go string storage in an unsafe way so the caller
// must ensure the string is not garbage collected.
func Str(str string) *uint8 {
if !strings.HasSuffix(str, "\x00") {
panic("str argument missing null terminator: " + str)
}
header := (*reflect.StringHeader)(unsafe.Pointer(&str))
return (*uint8)(unsafe.Pointer(header.Data))
}
// GoStr takes a null-terminated string returned by OpenGL and constructs a
// corresponding Go string.
func GoStr(cstr *uint8) string {
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
}
// Strs takes a list of Go strings (with or without null-termination) and
// returns their C counterpart.
//
// The returned free function must be called once you are done using the strings
// in order to free the memory.
//
// If no strings are provided as a parameter this function will panic.
func Strs(strs ...string) (cstrs **uint8, free func()) {
if len(strs) == 0 {
panic("Strs: expected at least 1 string")
}
// Allocate a contiguous array large enough to hold all the strings' contents.
n := 0
for i := range strs {
n += len(strs[i])
}
data := C.malloc(C.size_t(n))
// Copy all the strings into data.
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(data),
Len: n,
Cap: n,
}))
css := make([]*uint8, len(strs)) // Populated with pointers to each string.
offset := 0
for i := range strs {
copy(dataSlice[offset:offset+len(strs[i])], strs[i][:]) // Copy strs[i] into proper data location.
css[i] = (*uint8)(unsafe.Pointer(&dataSlice[offset])) // Set a pointer to it.
offset += len(strs[i])
}
return (**uint8)(&css[0]), func() { C.free(data) }
}

31
vendor/github.com/go-gl/gl/v4.1-core/gl/debug.go generated vendored Normal file
View File

@ -0,0 +1,31 @@
// Glow automatically generated OpenGL binding: http://github.com/go-gl/glow
package gl
import "C"
import "unsafe"
type DebugProc func(
source uint32,
gltype uint32,
id uint32,
severity uint32,
length int32,
message string,
userParam unsafe.Pointer)
var userDebugCallback DebugProc
//export glowDebugCallback_glcore41
func glowDebugCallback_glcore41(
source uint32,
gltype uint32,
id uint32,
severity uint32,
length int32,
message *uint8,
userParam unsafe.Pointer) {
if userDebugCallback != nil {
userDebugCallback(source, gltype, id, severity, length, GoStr(message), userParam)
}
}

15893
vendor/github.com/go-gl/gl/v4.1-core/gl/package.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

65
vendor/github.com/go-gl/gl/v4.1-core/gl/procaddr.go generated vendored Normal file
View File

@ -0,0 +1,65 @@
// This file implements GlowGetProcAddress for every supported platform. The
// correct version is chosen automatically based on build tags:
// windows: WGL
// darwin: CGL
// linux freebsd: GLX
// Use of EGL instead of the platform's default (listed above) is made possible
// via the "egl" build tag.
// It is also possible to install your own function outside this package for
// retrieving OpenGL function pointers, to do this see InitWithProcAddrFunc.
package gl
/*
#cgo windows CFLAGS: -DTAG_WINDOWS
#cgo windows LDFLAGS: -lopengl32
#cgo darwin CFLAGS: -DTAG_DARWIN
#cgo darwin LDFLAGS: -framework OpenGL
#cgo linux freebsd CFLAGS: -DTAG_POSIX
#cgo linux freebsd LDFLAGS: -lGL
#cgo egl CFLAGS: -DTAG_EGL
#cgo egl LDFLAGS: -lEGL
// Check the EGL tag first as it takes priority over the platform's default
// configuration of WGL/GLX/CGL.
#if defined(TAG_EGL)
#include <stdlib.h>
#include <EGL/egl.h>
void* GlowGetProcAddress_glcore41(const char* name) {
return eglGetProcAddress(name);
}
#elif defined(TAG_WINDOWS)
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <stdlib.h>
static HMODULE ogl32dll = NULL;
void* GlowGetProcAddress_glcore41(const char* name) {
void* pf = wglGetProcAddress((LPCSTR) name);
if (pf) {
return pf;
}
if (ogl32dll == NULL) {
ogl32dll = LoadLibraryA("opengl32.dll");
}
return GetProcAddress(ogl32dll, (LPCSTR) name);
}
#elif defined(TAG_DARWIN)
#include <stdlib.h>
#include <dlfcn.h>
void* GlowGetProcAddress_glcore41(const char* name) {
return dlsym(RTLD_DEFAULT, name);
}
#elif defined(TAG_POSIX)
#include <stdlib.h>
#include <GL/glx.h>
void* GlowGetProcAddress_glcore41(const char* name) {
return glXGetProcAddress((const GLubyte *) name);
}
#endif
*/
import "C"
import "unsafe"
func getProcAddress(namea string) unsafe.Pointer {
cname := C.CString(namea)
defer C.free(unsafe.Pointer(cname))
return C.GlowGetProcAddress_glcore41(cname)
}

10
vendor/github.com/go-gl/glfw/AUTHORS generated vendored Normal file
View File

@ -0,0 +1,10 @@
# This is the official list of glfw3-go authors for copyright purposes.
# Please keep the list sorted.
Coşku Baş
Dmitri Shuralyov
James Gray
Peter Waller <p@pwaller.net> (github:pwaller)
Robin Eklind
Stephen Gutekanst

27
vendor/github.com/go-gl/glfw/LICENSE generated vendored Normal file
View File

@ -0,0 +1,27 @@
Copyright (c) 2012 The glfw3-go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -0,0 +1,22 @@
Copyright (c) 2002-2006 Marcus Geelnard
Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would
be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.

View File

@ -0,0 +1 @@
999f3556fdd80983b10051746264489f2cb1ef16

43
vendor/github.com/go-gl/glfw/v3.2/glfw/build.go generated vendored Normal file
View File

@ -0,0 +1,43 @@
package glfw
/*
// Windows Build Tags
// ----------------
// GLFW Options:
#cgo windows CFLAGS: -D_GLFW_WIN32 -Iglfw/deps/mingw
// Linker Options:
#cgo windows LDFLAGS: -lopengl32 -lgdi32
// Darwin Build Tags
// ----------------
// GLFW Options:
#cgo darwin CFLAGS: -D_GLFW_COCOA -D_GLFW_USE_CHDIR -D_GLFW_USE_MENUBAR -D_GLFW_USE_RETINA -Wno-deprecated-declarations
// Linker Options:
#cgo darwin LDFLAGS: -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo
// Linux Build Tags
// ----------------
// GLFW Options:
#cgo linux,!wayland CFLAGS: -D_GLFW_X11
#cgo linux,wayland CFLAGS: -D_GLFW_WAYLAND -D_GNU_SOURCE
// Linker Options:
#cgo linux,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama -ldl -lrt
#cgo linux,wayland LDFLAGS: -lGL -lwayland-client -lwayland-cursor -lwayland-egl -lxkbcommon -lm -ldl -lrt
// FreeBSD Build Tags
// ----------------
// GLFW Options:
#cgo freebsd,!wayland CFLAGS: -D_GLFW_X11 -D_GLFW_HAS_GLXGETPROCADDRESSARB -D_GLFW_HAS_DLOPEN
#cgo freebsd,wayland CFLAGS: -D_GLFW_WAYLAND -D_GLFW_HAS_DLOPEN
// Linker Options:
#cgo freebsd,!wayland LDFLAGS: -lGL -lX11 -lXrandr -lXxf86vm -lXi -lXcursor -lm -lXinerama
#cgo freebsd,wayland LDFLAGS: -lGL -lwayland-client -lwayland-cursor -lwayland-egl -lxkbcommon -lm
*/
import "C"

11
vendor/github.com/go-gl/glfw/v3.2/glfw/c_glfw.go generated vendored Normal file
View File

@ -0,0 +1,11 @@
package glfw
/*
#include "glfw/src/context.c"
#include "glfw/src/init.c"
#include "glfw/src/input.c"
#include "glfw/src/monitor.c"
#include "glfw/src/vulkan.c"
#include "glfw/src/window.c"
*/
import "C"

View File

@ -0,0 +1,13 @@
package glfw
/*
#cgo CFLAGS: -x objective-c
#include "glfw/src/cocoa_init.m"
#include "glfw/src/cocoa_joystick.m"
#include "glfw/src/cocoa_monitor.m"
#include "glfw/src/cocoa_window.m"
#include "glfw/src/cocoa_time.c"
#include "glfw/src/posix_tls.c"
#include "glfw/src/nsgl_context.m"
*/
import "C"

View File

@ -0,0 +1,30 @@
// +build linux freebsd
package glfw
/*
#ifdef _GLFW_MIR
#include "glfw/src/mir_init.c"
#include "glfw/src/mir_monitor.c"
#include "glfw/src/mir_window.c"
#endif
#ifdef _GLFW_WAYLAND
#include "glfw/src/wl_init.c"
#include "glfw/src/wl_monitor.c"
#include "glfw/src/wl_window.c"
#include "glfw/src/wayland-pointer-constraints-unstable-v1-client-protocol.c"
#include "glfw/src/wayland-relative-pointer-unstable-v1-client-protocol.c"
#endif
#ifdef _GLFW_X11
#include "glfw/src/x11_init.c"
#include "glfw/src/x11_monitor.c"
#include "glfw/src/x11_window.c"
#include "glfw/src/glx_context.c"
#endif
#include "glfw/src/linux_joystick.c"
#include "glfw/src/posix_time.c"
#include "glfw/src/posix_tls.c"
#include "glfw/src/xkb_unicode.c"
#include "glfw/src/egl_context.c"
*/
import "C"

View File

@ -0,0 +1,13 @@
package glfw
/*
#include "glfw/src/win32_init.c"
#include "glfw/src/win32_joystick.c"
#include "glfw/src/win32_monitor.c"
#include "glfw/src/win32_time.c"
#include "glfw/src/win32_tls.c"
#include "glfw/src/win32_window.c"
#include "glfw/src/wgl_context.c"
#include "glfw/src/egl_context.c"
*/
import "C"

77
vendor/github.com/go-gl/glfw/v3.2/glfw/context.go generated vendored Normal file
View File

@ -0,0 +1,77 @@
package glfw
//#include <stdlib.h>
//#include "glfw/include/GLFW/glfw3.h"
import "C"
import (
"unsafe"
)
// MakeContextCurrent makes the context of the window current.
// Originally GLFW 3 passes a null pointer to detach the context.
// But since we're using receievers, DetachCurrentContext should
// be used instead.
func (w *Window) MakeContextCurrent() {
C.glfwMakeContextCurrent(w.data)
panicError()
}
// DetachCurrentContext detaches the current context.
func DetachCurrentContext() {
C.glfwMakeContextCurrent(nil)
panicError()
}
// GetCurrentContext returns the window whose context is current.
func GetCurrentContext() *Window {
w := C.glfwGetCurrentContext()
panicError()
if w == nil {
return nil
}
return windows.get(w)
}
// SwapBuffers swaps the front and back buffers of the window. If the
// swap interval is greater than zero, the GPU driver waits the specified number
// of screen updates before swapping the buffers.
func (w *Window) SwapBuffers() {
C.glfwSwapBuffers(w.data)
panicError()
}
// SwapInterval sets the swap interval for the current context, i.e. the number
// of screen updates to wait before swapping the buffers of a window and
// returning from SwapBuffers. This is sometimes called
// 'vertical synchronization', 'vertical retrace synchronization' or 'vsync'.
//
// Contexts that support either of the WGL_EXT_swap_control_tear and
// GLX_EXT_swap_control_tear extensions also accept negative swap intervals,
// which allow the driver to swap even if a frame arrives a little bit late.
// You can check for the presence of these extensions using
// ExtensionSupported. For more information about swap tearing,
// see the extension specifications.
//
// Some GPU drivers do not honor the requested swap interval, either because of
// user settings that override the request or due to bugs in the driver.
func SwapInterval(interval int) {
C.glfwSwapInterval(C.int(interval))
panicError()
}
// ExtensionSupported reports whether the specified OpenGL or context creation
// API extension is supported by the current context. For example, on Windows
// both the OpenGL and WGL extension strings are checked.
//
// As this functions searches one or more extension strings on each call, it is
// recommended that you cache its results if it's going to be used frequently.
// The extension strings will not change during the lifetime of a context, so
// there is no danger in doing this.
func ExtensionSupported(extension string) bool {
e := C.CString(extension)
defer C.free(unsafe.Pointer(e))
ret := glfwbool(C.glfwExtensionSupported(e))
panicError()
return ret
}

9
vendor/github.com/go-gl/glfw/v3.2/glfw/error.c generated vendored Normal file
View File

@ -0,0 +1,9 @@
#include "_cgo_export.h"
void glfwErrorCB(int code, const char *desc) {
goErrorCB(code, (char*)desc);
}
void glfwSetErrorCallbackCB() {
glfwSetErrorCallback(glfwErrorCB);
}

199
vendor/github.com/go-gl/glfw/v3.2/glfw/error.go generated vendored Normal file
View File

@ -0,0 +1,199 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
//void glfwSetErrorCallbackCB();
import "C"
import (
"fmt"
"log"
)
// ErrorCode corresponds to an error code.
type ErrorCode int
// Error codes that are translated to panics and the programmer should not
// expect to handle.
const (
notInitialized ErrorCode = C.GLFW_NOT_INITIALIZED // GLFW has not been initialized.
noCurrentContext ErrorCode = C.GLFW_NO_CURRENT_CONTEXT // No context is current.
invalidEnum ErrorCode = C.GLFW_INVALID_ENUM // One of the enum parameters for the function was given an invalid enum.
invalidValue ErrorCode = C.GLFW_INVALID_VALUE // One of the parameters for the function was given an invalid value.
outOfMemory ErrorCode = C.GLFW_OUT_OF_MEMORY // A memory allocation failed.
platformError ErrorCode = C.GLFW_PLATFORM_ERROR // A platform-specific error occurred that does not match any of the more specific categories.
)
const (
// APIUnavailable is the error code used when GLFW could not find support
// for the requested client API on the system.
//
// The installed graphics driver does not support the requested client API,
// or does not support it via the chosen context creation backend. Below
// are a few examples.
//
// Some pre-installed Windows graphics drivers do not support OpenGL. AMD
// only supports OpenGL ES via EGL, while Nvidia and Intel only supports it
// via a WGL or GLX extension. OS X does not provide OpenGL ES at all. The
// Mesa EGL, OpenGL and OpenGL ES libraries do not interface with the
// Nvidia binary driver.
APIUnavailable ErrorCode = C.GLFW_API_UNAVAILABLE
// VersionUnavailable is the error code used when the requested OpenGL or
// OpenGL ES (including any requested profile or context option) is not
// available on this machine.
//
// The machine does not support your requirements. If your application is
// sufficiently flexible, downgrade your requirements and try again.
// Otherwise, inform the user that their machine does not match your
// requirements.
//
// Future invalid OpenGL and OpenGL ES versions, for example OpenGL 4.8 if
// 5.0 comes out before the 4.x series gets that far, also fail with this
// error and not GLFW_INVALID_VALUE, because GLFW cannot know what future
// versions will exist.
VersionUnavailable ErrorCode = C.GLFW_VERSION_UNAVAILABLE
// FormatUnavailable is the error code used for both window creation and
// clipboard querying format errors.
//
// If emitted during window creation, the requested pixel format is not
// supported. This means one or more hard constraints did not match any of
// the available pixel formats. If your application is sufficiently
// flexible, downgrade your requirements and try again. Otherwise, inform
// the user that their machine does not match your requirements.
//
// If emitted when querying the clipboard, the contents of the clipboard
// could not be converted to the requested format. You should ignore the
// error or report it to the user, as appropriate.
FormatUnavailable ErrorCode = C.GLFW_FORMAT_UNAVAILABLE
)
func (e ErrorCode) String() string {
switch e {
case notInitialized:
return "NotInitialized"
case noCurrentContext:
return "NoCurrentContext"
case invalidEnum:
return "InvalidEnum"
case invalidValue:
return "InvalidValue"
case outOfMemory:
return "OutOfMemory"
case platformError:
return "PlatformError"
case APIUnavailable:
return "APIUnavailable"
case VersionUnavailable:
return "VersionUnavailable"
case FormatUnavailable:
return "FormatUnavailable"
default:
return fmt.Sprintf("ErrorCode(%d)", e)
}
}
// Error holds error code and description.
type Error struct {
Code ErrorCode
Desc string
}
// Error prints the error code and description in a readable format.
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.Code.String(), e.Desc)
}
// Note: There are many cryptic caveats to proper error handling here.
// See: https://github.com/go-gl/glfw3/pull/86
// Holds the value of the last error.
var lastError = make(chan *Error, 1)
//export goErrorCB
func goErrorCB(code C.int, desc *C.char) {
flushErrors()
err := &Error{ErrorCode(code), C.GoString(desc)}
select {
case lastError <- err:
default:
fmt.Println("GLFW: An uncaught error has occurred:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
}
}
// Set the glfw callback internally
func init() {
C.glfwSetErrorCallbackCB()
}
// flushErrors is called by Terminate before it actually calls C.glfwTerminate,
// this ensures that any uncaught errors buffered in lastError are printed
// before the program exits.
func flushErrors() {
err := fetchError()
if err != nil {
fmt.Println("GLFW: An uncaught error has occurred:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
}
}
// acceptError fetches the next error from the error channel, it accepts only
// errors with one of the given error codes. If any other error is encountered,
// a panic will occur.
//
// Platform errors are always printed, for information why please see:
//
// https://github.com/go-gl/glfw/issues/127
//
func acceptError(codes ...ErrorCode) error {
// Grab the next error, if there is one.
err := fetchError()
if err == nil {
return nil
}
// Only if the error has the specific error code accepted by the caller, do
// we return the error.
for _, code := range codes {
if err.Code == code {
return err
}
}
// The error isn't accepted by the caller. If the error code is not a code
// defined in the GLFW C documentation as a programmer error, then the
// caller should have accepted it. This is effectively a bug in this
// package.
switch err.Code {
case platformError:
log.Println(err)
return nil
case notInitialized, noCurrentContext, invalidEnum, invalidValue, outOfMemory:
panic(err)
default:
fmt.Println("GLFW: An invalid error was not accepted by the caller:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
panic(err)
}
}
// panicError is a helper used by functions which expect no errors (except
// programmer errors) to occur. It will panic if it finds any such error.
func panicError() {
err := acceptError()
if err != nil {
panic(err)
}
}
// fetchError fetches the next error from the error channel, it does not block
// and returns nil if there is no error present.
func fetchError() *Error {
select {
case err := <-lastError:
return err
default:
return nil
}
}

76
vendor/github.com/go-gl/glfw/v3.2/glfw/glfw.go generated vendored Normal file
View File

@ -0,0 +1,76 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
import "C"
// Version constants.
const (
VersionMajor = C.GLFW_VERSION_MAJOR // This is incremented when the API is changed in non-compatible ways.
VersionMinor = C.GLFW_VERSION_MINOR // This is incremented when features are added to the API but it remains backward-compatible.
VersionRevision = C.GLFW_VERSION_REVISION // This is incremented when a bug fix release is made that does not contain any API changes.
)
// Init initializes the GLFW library. Before most GLFW functions can be used,
// GLFW must be initialized, and before a program terminates GLFW should be
// terminated in order to free any resources allocated during or after
// initialization.
//
// If this function fails, it calls Terminate before returning. If it succeeds,
// you should call Terminate before the program exits.
//
// Additional calls to this function after successful initialization but before
// termination will succeed but will do nothing.
//
// This function may take several seconds to complete on some systems, while on
// other systems it may take only a fraction of a second to complete.
//
// On Mac OS X, this function will change the current directory of the
// application to the Contents/Resources subdirectory of the application's
// bundle, if present.
//
// This function may only be called from the main thread.
func Init() error {
C.glfwInit()
return acceptError(APIUnavailable)
}
// Terminate destroys all remaining windows, frees any allocated resources and
// sets the library to an uninitialized state. Once this is called, you must
// again call Init successfully before you will be able to use most GLFW
// functions.
//
// If GLFW has been successfully initialized, this function should be called
// before the program exits. If initialization fails, there is no need to call
// this function, as it is called by Init before it returns failure.
//
// This function may only be called from the main thread.
func Terminate() {
flushErrors()
C.glfwTerminate()
}
// GetVersion retrieves the major, minor and revision numbers of the GLFW
// library. It is intended for when you are using GLFW as a shared library and
// want to ensure that you are using the minimum required version.
//
// This function may be called before Init.
func GetVersion() (major, minor, revision int) {
var (
maj C.int
min C.int
rev C.int
)
C.glfwGetVersion(&maj, &min, &rev)
return int(maj), int(min), int(rev)
}
// GetVersionString returns a static string generated at compile-time according
// to which configuration macros were defined. This is intended for use when
// submitting bug reports, to allow developers to see which code paths are
// enabled in a binary.
//
// This function may be called before Init.
func GetVersionString() string {
return C.GoString(C.glfwGetVersionString())
}

View File

@ -0,0 +1,22 @@
Copyright (c) 2002-2006 Marcus Geelnard
Copyright (c) 2006-2016 Camilla Berglund <elmindreda@glfw.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would
be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.

81
vendor/github.com/go-gl/glfw/v3.2/glfw/input.c generated vendored Normal file
View File

@ -0,0 +1,81 @@
#include "_cgo_export.h"
void glfwJoystickCB(int joy, int event) {
goJoystickCB(joy, event);
}
void glfwMouseButtonCB(GLFWwindow* window, int button, int action, int mods) {
goMouseButtonCB(window, button, action, mods);
}
void glfwCursorPosCB(GLFWwindow* window, double xpos, double ypos) {
goCursorPosCB(window, xpos, ypos);
}
void glfwCursorEnterCB(GLFWwindow* window, int entered) {
goCursorEnterCB(window, entered);
}
void glfwScrollCB(GLFWwindow* window, double xoff, double yoff) {
goScrollCB(window, xoff, yoff);
}
void glfwKeyCB(GLFWwindow* window, int key, int scancode, int action, int mods) {
goKeyCB(window, key, scancode, action, mods);
}
void glfwCharCB(GLFWwindow* window, unsigned int character) {
goCharCB(window, character);
}
void glfwCharModsCB(GLFWwindow* window, unsigned int character, int mods) {
goCharModsCB(window, character, mods);
}
void glfwDropCB(GLFWwindow* window, int count, const char **names) {
goDropCB(window, count, (char**)names);
}
void glfwSetJoystickCallbackCB() {
glfwSetJoystickCallback(glfwJoystickCB);
}
void glfwSetKeyCallbackCB(GLFWwindow *window) {
glfwSetKeyCallback(window, glfwKeyCB);
}
void glfwSetCharCallbackCB(GLFWwindow *window) {
glfwSetCharCallback(window, glfwCharCB);
}
void glfwSetCharModsCallbackCB(GLFWwindow *window) {
glfwSetCharModsCallback(window, glfwCharModsCB);
}
void glfwSetMouseButtonCallbackCB(GLFWwindow *window) {
glfwSetMouseButtonCallback(window, glfwMouseButtonCB);
}
void glfwSetCursorPosCallbackCB(GLFWwindow *window) {
glfwSetCursorPosCallback(window, glfwCursorPosCB);
}
void glfwSetCursorEnterCallbackCB(GLFWwindow *window) {
glfwSetCursorEnterCallback(window, glfwCursorEnterCB);
}
void glfwSetScrollCallbackCB(GLFWwindow *window) {
glfwSetScrollCallback(window, glfwScrollCB);
}
void glfwSetDropCallbackCB(GLFWwindow *window) {
glfwSetDropCallback(window, glfwDropCB);
}
float GetAxisAtIndex(float *axis, int i) {
return axis[i];
}
unsigned char GetButtonsAtIndex(unsigned char *buttons, int i) {
return buttons[i];
}

696
vendor/github.com/go-gl/glfw/v3.2/glfw/input.go generated vendored Normal file
View File

@ -0,0 +1,696 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
//void glfwSetJoystickCallbackCB();
//void glfwSetKeyCallbackCB(GLFWwindow *window);
//void glfwSetCharCallbackCB(GLFWwindow *window);
//void glfwSetCharModsCallbackCB(GLFWwindow *window);
//void glfwSetMouseButtonCallbackCB(GLFWwindow *window);
//void glfwSetCursorPosCallbackCB(GLFWwindow *window);
//void glfwSetCursorEnterCallbackCB(GLFWwindow *window);
//void glfwSetScrollCallbackCB(GLFWwindow *window);
//void glfwSetDropCallbackCB(GLFWwindow *window);
//float GetAxisAtIndex(float *axis, int i);
//unsigned char GetButtonsAtIndex(unsigned char *buttons, int i);
import "C"
import (
"image"
"image/draw"
"unsafe"
)
var fJoystickHolder func(joy, event int)
// Joystick corresponds to a joystick.
type Joystick int
// Joystick IDs.
const (
Joystick1 Joystick = C.GLFW_JOYSTICK_1
Joystick2 Joystick = C.GLFW_JOYSTICK_2
Joystick3 Joystick = C.GLFW_JOYSTICK_3
Joystick4 Joystick = C.GLFW_JOYSTICK_4
Joystick5 Joystick = C.GLFW_JOYSTICK_5
Joystick6 Joystick = C.GLFW_JOYSTICK_6
Joystick7 Joystick = C.GLFW_JOYSTICK_7
Joystick8 Joystick = C.GLFW_JOYSTICK_8
Joystick9 Joystick = C.GLFW_JOYSTICK_9
Joystick10 Joystick = C.GLFW_JOYSTICK_10
Joystick11 Joystick = C.GLFW_JOYSTICK_11
Joystick12 Joystick = C.GLFW_JOYSTICK_12
Joystick13 Joystick = C.GLFW_JOYSTICK_13
Joystick14 Joystick = C.GLFW_JOYSTICK_14
Joystick15 Joystick = C.GLFW_JOYSTICK_15
Joystick16 Joystick = C.GLFW_JOYSTICK_16
JoystickLast Joystick = C.GLFW_JOYSTICK_LAST
)
// Key corresponds to a keyboard key.
type Key int
// These key codes are inspired by the USB HID Usage Tables v1.12 (p. 53-60),
// but re-arranged to map to 7-bit ASCII for printable keys (function keys are
// put in the 256+ range).
const (
KeyUnknown Key = C.GLFW_KEY_UNKNOWN
KeySpace Key = C.GLFW_KEY_SPACE
KeyApostrophe Key = C.GLFW_KEY_APOSTROPHE
KeyComma Key = C.GLFW_KEY_COMMA
KeyMinus Key = C.GLFW_KEY_MINUS
KeyPeriod Key = C.GLFW_KEY_PERIOD
KeySlash Key = C.GLFW_KEY_SLASH
Key0 Key = C.GLFW_KEY_0
Key1 Key = C.GLFW_KEY_1
Key2 Key = C.GLFW_KEY_2
Key3 Key = C.GLFW_KEY_3
Key4 Key = C.GLFW_KEY_4
Key5 Key = C.GLFW_KEY_5
Key6 Key = C.GLFW_KEY_6
Key7 Key = C.GLFW_KEY_7
Key8 Key = C.GLFW_KEY_8
Key9 Key = C.GLFW_KEY_9
KeySemicolon Key = C.GLFW_KEY_SEMICOLON
KeyEqual Key = C.GLFW_KEY_EQUAL
KeyA Key = C.GLFW_KEY_A
KeyB Key = C.GLFW_KEY_B
KeyC Key = C.GLFW_KEY_C
KeyD Key = C.GLFW_KEY_D
KeyE Key = C.GLFW_KEY_E
KeyF Key = C.GLFW_KEY_F
KeyG Key = C.GLFW_KEY_G
KeyH Key = C.GLFW_KEY_H
KeyI Key = C.GLFW_KEY_I
KeyJ Key = C.GLFW_KEY_J
KeyK Key = C.GLFW_KEY_K
KeyL Key = C.GLFW_KEY_L
KeyM Key = C.GLFW_KEY_M
KeyN Key = C.GLFW_KEY_N
KeyO Key = C.GLFW_KEY_O
KeyP Key = C.GLFW_KEY_P
KeyQ Key = C.GLFW_KEY_Q
KeyR Key = C.GLFW_KEY_R
KeyS Key = C.GLFW_KEY_S
KeyT Key = C.GLFW_KEY_T
KeyU Key = C.GLFW_KEY_U
KeyV Key = C.GLFW_KEY_V
KeyW Key = C.GLFW_KEY_W
KeyX Key = C.GLFW_KEY_X
KeyY Key = C.GLFW_KEY_Y
KeyZ Key = C.GLFW_KEY_Z
KeyLeftBracket Key = C.GLFW_KEY_LEFT_BRACKET
KeyBackslash Key = C.GLFW_KEY_BACKSLASH
KeyRightBracket Key = C.GLFW_KEY_RIGHT_BRACKET
KeyGraveAccent Key = C.GLFW_KEY_GRAVE_ACCENT
KeyWorld1 Key = C.GLFW_KEY_WORLD_1
KeyWorld2 Key = C.GLFW_KEY_WORLD_2
KeyEscape Key = C.GLFW_KEY_ESCAPE
KeyEnter Key = C.GLFW_KEY_ENTER
KeyTab Key = C.GLFW_KEY_TAB
KeyBackspace Key = C.GLFW_KEY_BACKSPACE
KeyInsert Key = C.GLFW_KEY_INSERT
KeyDelete Key = C.GLFW_KEY_DELETE
KeyRight Key = C.GLFW_KEY_RIGHT
KeyLeft Key = C.GLFW_KEY_LEFT
KeyDown Key = C.GLFW_KEY_DOWN
KeyUp Key = C.GLFW_KEY_UP
KeyPageUp Key = C.GLFW_KEY_PAGE_UP
KeyPageDown Key = C.GLFW_KEY_PAGE_DOWN
KeyHome Key = C.GLFW_KEY_HOME
KeyEnd Key = C.GLFW_KEY_END
KeyCapsLock Key = C.GLFW_KEY_CAPS_LOCK
KeyScrollLock Key = C.GLFW_KEY_SCROLL_LOCK
KeyNumLock Key = C.GLFW_KEY_NUM_LOCK
KeyPrintScreen Key = C.GLFW_KEY_PRINT_SCREEN
KeyPause Key = C.GLFW_KEY_PAUSE
KeyF1 Key = C.GLFW_KEY_F1
KeyF2 Key = C.GLFW_KEY_F2
KeyF3 Key = C.GLFW_KEY_F3
KeyF4 Key = C.GLFW_KEY_F4
KeyF5 Key = C.GLFW_KEY_F5
KeyF6 Key = C.GLFW_KEY_F6
KeyF7 Key = C.GLFW_KEY_F7
KeyF8 Key = C.GLFW_KEY_F8
KeyF9 Key = C.GLFW_KEY_F9
KeyF10 Key = C.GLFW_KEY_F10
KeyF11 Key = C.GLFW_KEY_F11
KeyF12 Key = C.GLFW_KEY_F12
KeyF13 Key = C.GLFW_KEY_F13
KeyF14 Key = C.GLFW_KEY_F14
KeyF15 Key = C.GLFW_KEY_F15
KeyF16 Key = C.GLFW_KEY_F16
KeyF17 Key = C.GLFW_KEY_F17
KeyF18 Key = C.GLFW_KEY_F18
KeyF19 Key = C.GLFW_KEY_F19
KeyF20 Key = C.GLFW_KEY_F20
KeyF21 Key = C.GLFW_KEY_F21
KeyF22 Key = C.GLFW_KEY_F22
KeyF23 Key = C.GLFW_KEY_F23
KeyF24 Key = C.GLFW_KEY_F24
KeyF25 Key = C.GLFW_KEY_F25
KeyKP0 Key = C.GLFW_KEY_KP_0
KeyKP1 Key = C.GLFW_KEY_KP_1
KeyKP2 Key = C.GLFW_KEY_KP_2
KeyKP3 Key = C.GLFW_KEY_KP_3
KeyKP4 Key = C.GLFW_KEY_KP_4
KeyKP5 Key = C.GLFW_KEY_KP_5
KeyKP6 Key = C.GLFW_KEY_KP_6
KeyKP7 Key = C.GLFW_KEY_KP_7
KeyKP8 Key = C.GLFW_KEY_KP_8
KeyKP9 Key = C.GLFW_KEY_KP_9
KeyKPDecimal Key = C.GLFW_KEY_KP_DECIMAL
KeyKPDivide Key = C.GLFW_KEY_KP_DIVIDE
KeyKPMultiply Key = C.GLFW_KEY_KP_MULTIPLY
KeyKPSubtract Key = C.GLFW_KEY_KP_SUBTRACT
KeyKPAdd Key = C.GLFW_KEY_KP_ADD
KeyKPEnter Key = C.GLFW_KEY_KP_ENTER
KeyKPEqual Key = C.GLFW_KEY_KP_EQUAL
KeyLeftShift Key = C.GLFW_KEY_LEFT_SHIFT
KeyLeftControl Key = C.GLFW_KEY_LEFT_CONTROL
KeyLeftAlt Key = C.GLFW_KEY_LEFT_ALT
KeyLeftSuper Key = C.GLFW_KEY_LEFT_SUPER
KeyRightShift Key = C.GLFW_KEY_RIGHT_SHIFT
KeyRightControl Key = C.GLFW_KEY_RIGHT_CONTROL
KeyRightAlt Key = C.GLFW_KEY_RIGHT_ALT
KeyRightSuper Key = C.GLFW_KEY_RIGHT_SUPER
KeyMenu Key = C.GLFW_KEY_MENU
KeyLast Key = C.GLFW_KEY_LAST
)
// ModifierKey corresponds to a modifier key.
type ModifierKey int
// Modifier keys.
const (
ModShift ModifierKey = C.GLFW_MOD_SHIFT
ModControl ModifierKey = C.GLFW_MOD_CONTROL
ModAlt ModifierKey = C.GLFW_MOD_ALT
ModSuper ModifierKey = C.GLFW_MOD_SUPER
)
// MouseButton corresponds to a mouse button.
type MouseButton int
// Mouse buttons.
const (
MouseButton1 MouseButton = C.GLFW_MOUSE_BUTTON_1
MouseButton2 MouseButton = C.GLFW_MOUSE_BUTTON_2
MouseButton3 MouseButton = C.GLFW_MOUSE_BUTTON_3
MouseButton4 MouseButton = C.GLFW_MOUSE_BUTTON_4
MouseButton5 MouseButton = C.GLFW_MOUSE_BUTTON_5
MouseButton6 MouseButton = C.GLFW_MOUSE_BUTTON_6
MouseButton7 MouseButton = C.GLFW_MOUSE_BUTTON_7
MouseButton8 MouseButton = C.GLFW_MOUSE_BUTTON_8
MouseButtonLast MouseButton = C.GLFW_MOUSE_BUTTON_LAST
MouseButtonLeft MouseButton = C.GLFW_MOUSE_BUTTON_LEFT
MouseButtonRight MouseButton = C.GLFW_MOUSE_BUTTON_RIGHT
MouseButtonMiddle MouseButton = C.GLFW_MOUSE_BUTTON_MIDDLE
)
// StandardCursor corresponds to a standard cursor icon.
type StandardCursor int
// Standard cursors
const (
ArrowCursor StandardCursor = C.GLFW_ARROW_CURSOR
IBeamCursor StandardCursor = C.GLFW_IBEAM_CURSOR
CrosshairCursor StandardCursor = C.GLFW_CROSSHAIR_CURSOR
HandCursor StandardCursor = C.GLFW_HAND_CURSOR
HResizeCursor StandardCursor = C.GLFW_HRESIZE_CURSOR
VResizeCursor StandardCursor = C.GLFW_VRESIZE_CURSOR
)
// Action corresponds to a key or button action.
type Action int
// Action types.
const (
Release Action = C.GLFW_RELEASE // The key or button was released.
Press Action = C.GLFW_PRESS // The key or button was pressed.
Repeat Action = C.GLFW_REPEAT // The key was held down until it repeated.
)
// InputMode corresponds to an input mode.
type InputMode int
// Input modes.
const (
CursorMode InputMode = C.GLFW_CURSOR // See Cursor mode values
StickyKeysMode InputMode = C.GLFW_STICKY_KEYS // Value can be either 1 or 0
StickyMouseButtonsMode InputMode = C.GLFW_STICKY_MOUSE_BUTTONS // Value can be either 1 or 0
)
// Cursor mode values.
const (
CursorNormal int = C.GLFW_CURSOR_NORMAL
CursorHidden int = C.GLFW_CURSOR_HIDDEN
CursorDisabled int = C.GLFW_CURSOR_DISABLED
)
// Cursor represents a cursor.
type Cursor struct {
data *C.GLFWcursor
}
//export goJoystickCB
func goJoystickCB(joy, event C.int) {
fJoystickHolder(int(joy), int(event))
}
//export goMouseButtonCB
func goMouseButtonCB(window unsafe.Pointer, button, action, mods C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fMouseButtonHolder(w, MouseButton(button), Action(action), ModifierKey(mods))
}
//export goCursorPosCB
func goCursorPosCB(window unsafe.Pointer, xpos, ypos C.double) {
w := windows.get((*C.GLFWwindow)(window))
w.fCursorPosHolder(w, float64(xpos), float64(ypos))
}
//export goCursorEnterCB
func goCursorEnterCB(window unsafe.Pointer, entered C.int) {
w := windows.get((*C.GLFWwindow)(window))
hasEntered := glfwbool(entered)
w.fCursorEnterHolder(w, hasEntered)
}
//export goScrollCB
func goScrollCB(window unsafe.Pointer, xoff, yoff C.double) {
w := windows.get((*C.GLFWwindow)(window))
w.fScrollHolder(w, float64(xoff), float64(yoff))
}
//export goKeyCB
func goKeyCB(window unsafe.Pointer, key, scancode, action, mods C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fKeyHolder(w, Key(key), int(scancode), Action(action), ModifierKey(mods))
}
//export goCharCB
func goCharCB(window unsafe.Pointer, character C.uint) {
w := windows.get((*C.GLFWwindow)(window))
w.fCharHolder(w, rune(character))
}
//export goCharModsCB
func goCharModsCB(window unsafe.Pointer, character C.uint, mods C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fCharModsHolder(w, rune(character), ModifierKey(mods))
}
//export goDropCB
func goDropCB(window unsafe.Pointer, count C.int, names **C.char) { // TODO: The types of name can be `**C.char` or `unsafe.Pointer`, use whichever is better.
w := windows.get((*C.GLFWwindow)(window))
namesSlice := make([]string, int(count)) // TODO: Make this better. This part is unfinished, hacky, probably not correct, and not idiomatic.
for i := 0; i < int(count); i++ { // TODO: Make this better. It should be cleaned up and vetted.
var x *C.char // TODO: Make this better.
p := (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(names)) + uintptr(i)*unsafe.Sizeof(x))) // TODO: Make this better.
namesSlice[i] = C.GoString(*p) // TODO: Make this better.
}
w.fDropHolder(w, namesSlice)
}
// GetInputMode returns the value of an input option of the window.
func (w *Window) GetInputMode(mode InputMode) int {
ret := int(C.glfwGetInputMode(w.data, C.int(mode)))
panicError()
return ret
}
// SetInputMode sets an input option for the window.
func (w *Window) SetInputMode(mode InputMode, value int) {
C.glfwSetInputMode(w.data, C.int(mode), C.int(value))
panicError()
}
// GetKey returns the last reported state of a keyboard key. The returned state
// is one of Press or Release. The higher-level state Repeat is only reported to
// the key callback.
//
// If the StickyKeys input mode is enabled, this function returns Press the first
// time you call this function after a key has been pressed, even if the key has
// already been released.
//
// The key functions deal with physical keys, with key tokens named after their
// use on the standard US keyboard layout. If you want to input text, use the
// Unicode character callback instead.
func (w *Window) GetKey(key Key) Action {
ret := Action(C.glfwGetKey(w.data, C.int(key)))
panicError()
return ret
}
// GetKeyName returns the localized name of the specified printable key.
//
// If the key is glfw.KeyUnknown, the scancode is used, otherwise the scancode is ignored.
func GetKeyName(key Key, scancode int) string {
ret := C.glfwGetKeyName(C.int(key), C.int(scancode))
panicError()
return C.GoString(ret)
}
// GetMouseButton returns the last state reported for the specified mouse button.
//
// If the StickyMouseButtons input mode is enabled, this function returns Press
// the first time you call this function after a mouse button has been pressed,
// even if the mouse button has already been released.
func (w *Window) GetMouseButton(button MouseButton) Action {
ret := Action(C.glfwGetMouseButton(w.data, C.int(button)))
panicError()
return ret
}
// GetCursorPos returns the last reported position of the cursor.
//
// If the cursor is disabled (with CursorDisabled) then the cursor position is
// unbounded and limited only by the minimum and maximum values of a double.
//
// The coordinate can be converted to their integer equivalents with the floor
// function. Casting directly to an integer type works for positive coordinates,
// but fails for negative ones.
func (w *Window) GetCursorPos() (x, y float64) {
var xpos, ypos C.double
C.glfwGetCursorPos(w.data, &xpos, &ypos)
panicError()
return float64(xpos), float64(ypos)
}
// SetCursorPos sets the position of the cursor. The specified window must
// be focused. If the window does not have focus when this function is called,
// it fails silently.
//
// If the cursor is disabled (with CursorDisabled) then the cursor position is
// unbounded and limited only by the minimum and maximum values of a double.
func (w *Window) SetCursorPos(xpos, ypos float64) {
C.glfwSetCursorPos(w.data, C.double(xpos), C.double(ypos))
panicError()
}
// CreateCursor creates a new custom cursor image that can be set for a window with SetCursor.
// The cursor can be destroyed with Destroy. Any remaining cursors are destroyed by Terminate.
//
// The image is ideally provided in the form of *image.NRGBA.
// The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight
// bits per channel with the red channel first. They are arranged canonically
// as packed sequential rows, starting from the top-left corner. If the image
// type is not *image.NRGBA, it will be converted to it.
//
// The cursor hotspot is specified in pixels, relative to the upper-left corner of the cursor image.
// Like all other coordinate systems in GLFW, the X-axis points to the right and the Y-axis points down.
func CreateCursor(img image.Image, xhot, yhot int) *Cursor {
var imgC C.GLFWimage
var pixels []uint8
b := img.Bounds()
switch img := img.(type) {
case *image.NRGBA:
pixels = img.Pix
default:
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
pixels = m.Pix
}
pix, free := bytes(pixels)
imgC.width = C.int(b.Dx())
imgC.height = C.int(b.Dy())
imgC.pixels = (*C.uchar)(pix)
c := C.glfwCreateCursor(&imgC, C.int(xhot), C.int(yhot))
free()
panicError()
return &Cursor{c}
}
// CreateStandardCursor returns a cursor with a standard shape,
// that can be set for a window with SetCursor.
func CreateStandardCursor(shape StandardCursor) *Cursor {
c := C.glfwCreateStandardCursor(C.int(shape))
panicError()
return &Cursor{c}
}
// Destroy destroys a cursor previously created with CreateCursor.
// Any remaining cursors will be destroyed by Terminate.
func (c *Cursor) Destroy() {
C.glfwDestroyCursor(c.data)
panicError()
}
// SetCursor sets the cursor image to be used when the cursor is over the client area
// of the specified window. The set cursor will only be visible when the cursor mode of the
// window is CursorNormal.
//
// On some platforms, the set cursor may not be visible unless the window also has input focus.
func (w *Window) SetCursor(c *Cursor) {
if c == nil {
C.glfwSetCursor(w.data, nil)
} else {
C.glfwSetCursor(w.data, c.data)
}
panicError()
}
// JoystickCallback is the joystick configuration callback.
type JoystickCallback func(joy, event int)
// SetJoystickCallback sets the joystick configuration callback, or removes the
// currently set callback. This is called when a joystick is connected to or
// disconnected from the system.
func SetJoystickCallback(cbfun JoystickCallback) (previous JoystickCallback) {
previous = fJoystickHolder
fJoystickHolder = cbfun
if cbfun == nil {
C.glfwSetJoystickCallback(nil)
} else {
C.glfwSetJoystickCallbackCB()
}
panicError()
return previous
}
// KeyCallback is the key callback.
type KeyCallback func(w *Window, key Key, scancode int, action Action, mods ModifierKey)
// SetKeyCallback sets the key callback which is called when a key is pressed,
// repeated or released.
//
// The key functions deal with physical keys, with layout independent key tokens
// named after their values in the standard US keyboard layout. If you want to
// input text, use the SetCharCallback instead.
//
// When a window loses focus, it will generate synthetic key release events for
// all pressed keys. You can tell these events from user-generated events by the
// fact that the synthetic ones are generated after the window has lost focus,
// i.e. Focused will be false and the focus callback will have already been
// called.
func (w *Window) SetKeyCallback(cbfun KeyCallback) (previous KeyCallback) {
previous = w.fKeyHolder
w.fKeyHolder = cbfun
if cbfun == nil {
C.glfwSetKeyCallback(w.data, nil)
} else {
C.glfwSetKeyCallbackCB(w.data)
}
panicError()
return previous
}
// CharCallback is the character callback.
type CharCallback func(w *Window, char rune)
// SetCharCallback sets the character callback which is called when a
// Unicode character is input.
//
// The character callback is intended for Unicode text input. As it deals with
// characters, it is keyboard layout dependent, whereas the
// key callback is not. Characters do not map 1:1
// to physical keys, as a key may produce zero, one or more characters. If you
// want to know whether a specific physical key was pressed or released, see
// the key callback instead.
//
// The character callback behaves as system text input normally does and will
// not be called if modifier keys are held down that would prevent normal text
// input on that platform, for example a Super (Command) key on OS X or Alt key
// on Windows. There is a character with modifiers callback that receives these events.
func (w *Window) SetCharCallback(cbfun CharCallback) (previous CharCallback) {
previous = w.fCharHolder
w.fCharHolder = cbfun
if cbfun == nil {
C.glfwSetCharCallback(w.data, nil)
} else {
C.glfwSetCharCallbackCB(w.data)
}
panicError()
return previous
}
// CharModsCallback is the character with modifiers callback.
type CharModsCallback func(w *Window, char rune, mods ModifierKey)
// SetCharModsCallback sets the character with modifiers callback which is called when a
// Unicode character is input regardless of what modifier keys are used.
//
// The character with modifiers callback is intended for implementing custom
// Unicode character input. For regular Unicode text input, see the
// character callback. Like the character callback, the character with modifiers callback
// deals with characters and is keyboard layout dependent. Characters do not
// map 1:1 to physical keys, as a key may produce zero, one or more characters.
// If you want to know whether a specific physical key was pressed or released,
// see the key callback instead.
func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) {
previous = w.fCharModsHolder
w.fCharModsHolder = cbfun
if cbfun == nil {
C.glfwSetCharModsCallback(w.data, nil)
} else {
C.glfwSetCharModsCallbackCB(w.data)
}
panicError()
return previous
}
// MouseButtonCallback is the mouse button callback.
type MouseButtonCallback func(w *Window, button MouseButton, action Action, mod ModifierKey)
// SetMouseButtonCallback sets the mouse button callback which is called when a
// mouse button is pressed or released.
//
// When a window loses focus, it will generate synthetic mouse button release
// events for all pressed mouse buttons. You can tell these events from
// user-generated events by the fact that the synthetic ones are generated after
// the window has lost focus, i.e. Focused will be false and the focus
// callback will have already been called.
func (w *Window) SetMouseButtonCallback(cbfun MouseButtonCallback) (previous MouseButtonCallback) {
previous = w.fMouseButtonHolder
w.fMouseButtonHolder = cbfun
if cbfun == nil {
C.glfwSetMouseButtonCallback(w.data, nil)
} else {
C.glfwSetMouseButtonCallbackCB(w.data)
}
panicError()
return previous
}
// CursorPosCallback the cursor position callback.
type CursorPosCallback func(w *Window, xpos float64, ypos float64)
// SetCursorPosCallback sets the cursor position callback which is called
// when the cursor is moved. The callback is provided with the position relative
// to the upper-left corner of the client area of the window.
func (w *Window) SetCursorPosCallback(cbfun CursorPosCallback) (previous CursorPosCallback) {
previous = w.fCursorPosHolder
w.fCursorPosHolder = cbfun
if cbfun == nil {
C.glfwSetCursorPosCallback(w.data, nil)
} else {
C.glfwSetCursorPosCallbackCB(w.data)
}
panicError()
return previous
}
// CursorEnterCallback is the cursor boundary crossing callback.
type CursorEnterCallback func(w *Window, entered bool)
// SetCursorEnterCallback the cursor boundary crossing callback which is called
// when the cursor enters or leaves the client area of the window.
func (w *Window) SetCursorEnterCallback(cbfun CursorEnterCallback) (previous CursorEnterCallback) {
previous = w.fCursorEnterHolder
w.fCursorEnterHolder = cbfun
if cbfun == nil {
C.glfwSetCursorEnterCallback(w.data, nil)
} else {
C.glfwSetCursorEnterCallbackCB(w.data)
}
panicError()
return previous
}
// ScrollCallback is the scroll callback.
type ScrollCallback func(w *Window, xoff float64, yoff float64)
// SetScrollCallback sets the scroll callback which is called when a scrolling
// device is used, such as a mouse wheel or scrolling area of a touchpad.
func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback) {
previous = w.fScrollHolder
w.fScrollHolder = cbfun
if cbfun == nil {
C.glfwSetScrollCallback(w.data, nil)
} else {
C.glfwSetScrollCallbackCB(w.data)
}
panicError()
return previous
}
// DropCallback is the drop callback.
type DropCallback func(w *Window, names []string)
// SetDropCallback sets the drop callback which is called when an object
// is dropped over the window.
func (w *Window) SetDropCallback(cbfun DropCallback) (previous DropCallback) {
previous = w.fDropHolder
w.fDropHolder = cbfun
if cbfun == nil {
C.glfwSetDropCallback(w.data, nil)
} else {
C.glfwSetDropCallbackCB(w.data)
}
panicError()
return previous
}
// JoystickPresent reports whether the specified joystick is present.
func JoystickPresent(joy Joystick) bool {
ret := glfwbool(C.glfwJoystickPresent(C.int(joy)))
panicError()
return ret
}
// GetJoystickAxes returns a slice of axis values.
func GetJoystickAxes(joy Joystick) []float32 {
var length int
axis := C.glfwGetJoystickAxes(C.int(joy), (*C.int)(unsafe.Pointer(&length)))
panicError()
if axis == nil {
return nil
}
a := make([]float32, length)
for i := 0; i < length; i++ {
a[i] = float32(C.GetAxisAtIndex(axis, C.int(i)))
}
return a
}
// GetJoystickButtons returns a slice of button values.
func GetJoystickButtons(joy Joystick) []byte {
var length int
buttons := C.glfwGetJoystickButtons(C.int(joy), (*C.int)(unsafe.Pointer(&length)))
panicError()
if buttons == nil {
return nil
}
b := make([]byte, length)
for i := 0; i < length; i++ {
b[i] = byte(C.GetButtonsAtIndex(buttons, C.int(i)))
}
return b
}
// GetJoystickName returns the name, encoded as UTF-8, of the specified joystick.
func GetJoystickName(joy Joystick) string {
jn := C.glfwGetJoystickName(C.int(joy))
panicError()
return C.GoString(jn)
}

25
vendor/github.com/go-gl/glfw/v3.2/glfw/monitor.c generated vendored Normal file
View File

@ -0,0 +1,25 @@
#include "_cgo_export.h"
GLFWmonitor *GetMonitorAtIndex(GLFWmonitor **monitors, int index) {
return monitors[index];
}
GLFWvidmode GetVidmodeAtIndex(GLFWvidmode *vidmodes, int index) {
return vidmodes[index];
}
void glfwMonitorCB(GLFWmonitor* monitor, int event) {
goMonitorCB(monitor, event);
}
void glfwSetMonitorCallbackCB() {
glfwSetMonitorCallback(glfwMonitorCB);
}
unsigned int GetGammaAtIndex(unsigned short *color, int i) {
return color[i];
}
void SetGammaAtIndex(unsigned short *color, int i, unsigned short value) {
color[i] = value;
}

208
vendor/github.com/go-gl/glfw/v3.2/glfw/monitor.go generated vendored Normal file
View File

@ -0,0 +1,208 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
//GLFWmonitor* GetMonitorAtIndex(GLFWmonitor **monitors, int index);
//GLFWvidmode GetVidmodeAtIndex(GLFWvidmode *vidmodes, int index);
//void glfwSetMonitorCallbackCB();
//unsigned int GetGammaAtIndex(unsigned short *color, int i);
//void SetGammaAtIndex(unsigned short *color, int i, unsigned short value);
import "C"
import (
"unsafe"
)
// Monitor represents a monitor.
type Monitor struct {
data *C.GLFWmonitor
}
// MonitorEvent corresponds to a monitor configuration event.
type MonitorEvent int
// GammaRamp describes the gamma ramp for a monitor.
type GammaRamp struct {
Red []uint16 // A slice of value describing the response of the red channel.
Green []uint16 // A slice of value describing the response of the green channel.
Blue []uint16 // A slice of value describing the response of the blue channel.
}
// Monitor events.
const (
Connected MonitorEvent = C.GLFW_CONNECTED
Disconnected MonitorEvent = C.GLFW_DISCONNECTED
)
// VidMode describes a single video mode.
type VidMode struct {
Width int // The width, in pixels, of the video mode.
Height int // The height, in pixels, of the video mode.
RedBits int // The bit depth of the red channel of the video mode.
GreenBits int // The bit depth of the green channel of the video mode.
BlueBits int // The bit depth of the blue channel of the video mode.
RefreshRate int // The refresh rate, in Hz, of the video mode.
}
var fMonitorHolder func(monitor *Monitor, event MonitorEvent)
//export goMonitorCB
func goMonitorCB(monitor unsafe.Pointer, event C.int) {
fMonitorHolder(&Monitor{(*C.GLFWmonitor)(monitor)}, MonitorEvent(event))
}
// GetMonitors returns a slice of handles for all currently connected monitors.
func GetMonitors() []*Monitor {
var length int
mC := C.glfwGetMonitors((*C.int)(unsafe.Pointer(&length)))
panicError()
if mC == nil {
return nil
}
m := make([]*Monitor, length)
for i := 0; i < length; i++ {
m[i] = &Monitor{C.GetMonitorAtIndex(mC, C.int(i))}
}
return m
}
// GetPrimaryMonitor returns the primary monitor. This is usually the monitor
// where elements like the Windows task bar or the OS X menu bar is located.
func GetPrimaryMonitor() *Monitor {
m := C.glfwGetPrimaryMonitor()
panicError()
if m == nil {
return nil
}
return &Monitor{m}
}
// GetPos returns the position, in screen coordinates, of the upper-left
// corner of the monitor.
func (m *Monitor) GetPos() (x, y int) {
var xpos, ypos C.int
C.glfwGetMonitorPos(m.data, &xpos, &ypos)
panicError()
return int(xpos), int(ypos)
}
// GetPhysicalSize returns the size, in millimetres, of the display area of the
// monitor.
//
// Note: Some operating systems do not provide accurate information, either
// because the monitor's EDID data is incorrect, or because the driver does not
// report it accurately.
func (m *Monitor) GetPhysicalSize() (width, height int) {
var wi, h C.int
C.glfwGetMonitorPhysicalSize(m.data, &wi, &h)
panicError()
return int(wi), int(h)
}
// GetName returns a human-readable name of the monitor, encoded as UTF-8.
func (m *Monitor) GetName() string {
mn := C.glfwGetMonitorName(m.data)
panicError()
if mn == nil {
return ""
}
return C.GoString(mn)
}
// SetMonitorCallback sets the monitor configuration callback, or removes the
// currently set callback. This is called when a monitor is connected to or
// disconnected from the system.
func SetMonitorCallback(cbfun func(monitor *Monitor, event MonitorEvent)) {
if cbfun == nil {
C.glfwSetMonitorCallback(nil)
} else {
fMonitorHolder = cbfun
C.glfwSetMonitorCallbackCB()
}
panicError()
}
// GetVideoModes returns an array of all video modes supported by the monitor.
// The returned array is sorted in ascending order, first by color bit depth
// (the sum of all channel depths) and then by resolution area (the product of
// width and height).
func (m *Monitor) GetVideoModes() []*VidMode {
var length int
vC := C.glfwGetVideoModes(m.data, (*C.int)(unsafe.Pointer(&length)))
panicError()
if vC == nil {
return nil
}
v := make([]*VidMode, length)
for i := 0; i < length; i++ {
t := C.GetVidmodeAtIndex(vC, C.int(i))
v[i] = &VidMode{int(t.width), int(t.height), int(t.redBits), int(t.greenBits), int(t.blueBits), int(t.refreshRate)}
}
return v
}
// GetVideoMode returns the current video mode of the monitor. If you
// are using a full screen window, the return value will therefore depend on
// whether it is focused.
func (m *Monitor) GetVideoMode() *VidMode {
t := C.glfwGetVideoMode(m.data)
if t == nil {
return nil
}
panicError()
return &VidMode{int(t.width), int(t.height), int(t.redBits), int(t.greenBits), int(t.blueBits), int(t.refreshRate)}
}
// SetGamma generates a 256-element gamma ramp from the specified exponent and then calls
// SetGamma with it.
func (m *Monitor) SetGamma(gamma float32) {
C.glfwSetGamma(m.data, C.float(gamma))
panicError()
}
// GetGammaRamp retrieves the current gamma ramp of the monitor.
func (m *Monitor) GetGammaRamp() *GammaRamp {
var ramp GammaRamp
rampC := C.glfwGetGammaRamp(m.data)
panicError()
if rampC == nil {
return nil
}
length := int(rampC.size)
ramp.Red = make([]uint16, length)
ramp.Green = make([]uint16, length)
ramp.Blue = make([]uint16, length)
for i := 0; i < length; i++ {
ramp.Red[i] = uint16(C.GetGammaAtIndex(rampC.red, C.int(i)))
ramp.Green[i] = uint16(C.GetGammaAtIndex(rampC.green, C.int(i)))
ramp.Blue[i] = uint16(C.GetGammaAtIndex(rampC.blue, C.int(i)))
}
return &ramp
}
// SetGammaRamp sets the current gamma ramp for the monitor.
func (m *Monitor) SetGammaRamp(ramp *GammaRamp) {
var rampC C.GLFWgammaramp
length := len(ramp.Red)
for i := 0; i < length; i++ {
C.SetGammaAtIndex(rampC.red, C.int(i), C.ushort(ramp.Red[i]))
C.SetGammaAtIndex(rampC.green, C.int(i), C.ushort(ramp.Green[i]))
C.SetGammaAtIndex(rampC.blue, C.int(i), C.ushort(ramp.Blue[i]))
}
C.glfwSetGammaRamp(m.data, &rampC)
panicError()
}

View File

@ -0,0 +1,39 @@
package glfw
/*
#define GLFW_EXPOSE_NATIVE_COCOA
#define GLFW_EXPOSE_NATIVE_NSGL
#include "glfw/include/GLFW/glfw3.h"
#include "glfw/include/GLFW/glfw3native.h"
// workaround wrappers needed due to a cgo and/or LLVM bug.
// See: https://github.com/go-gl/glfw/issues/136
void *workaround_glfwGetCocoaWindow(GLFWwindow *w) {
return (void *)glfwGetCocoaWindow(w);
}
void *workaround_glfwGetNSGLContext(GLFWwindow *w) {
return (void *)glfwGetNSGLContext(w);
}
*/
import "C"
// GetCocoaMonitor returns the CGDirectDisplayID of the monitor.
func (m *Monitor) GetCocoaMonitor() uintptr {
ret := uintptr(C.glfwGetCocoaMonitor(m.data))
panicError()
return ret
}
// GetCocoaWindow returns the NSWindow of the window.
func (w *Window) GetCocoaWindow() uintptr {
ret := uintptr(C.workaround_glfwGetCocoaWindow(w.data))
panicError()
return ret
}
// GetNSGLContext returns the NSOpenGLContext of the window.
func (w *Window) GetNSGLContext() uintptr {
ret := uintptr(C.workaround_glfwGetNSGLContext(w.data))
panicError()
return ret
}

View File

@ -0,0 +1,50 @@
// +build linux,!wayland freebsd,!wayland
package glfw
//#define GLFW_EXPOSE_NATIVE_X11
//#define GLFW_EXPOSE_NATIVE_GLX
//#include "glfw/include/GLFW/glfw3.h"
//#include "glfw/include/GLFW/glfw3native.h"
import "C"
func GetX11Display() *C.Display {
ret := C.glfwGetX11Display()
panicError()
return ret
}
// GetX11Adapter returns the RRCrtc of the monitor.
func (m *Monitor) GetX11Adapter() C.RRCrtc {
ret := C.glfwGetX11Adapter(m.data)
panicError()
return ret
}
// GetX11Monitor returns the RROutput of the monitor.
func (m *Monitor) GetX11Monitor() C.RROutput {
ret := C.glfwGetX11Monitor(m.data)
panicError()
return ret
}
// GetX11Window returns the Window of the window.
func (w *Window) GetX11Window() C.Window {
ret := C.glfwGetX11Window(w.data)
panicError()
return ret
}
// GetGLXContext returns the GLXContext of the window.
func (w *Window) GetGLXContext() C.GLXContext {
ret := C.glfwGetGLXContext(w.data)
panicError()
return ret
}
// GetGLXWindow returns the GLXWindow of the window.
func (w *Window) GetGLXWindow() C.GLXWindow {
ret := C.glfwGetGLXWindow(w.data)
panicError()
return ret
}

View File

@ -0,0 +1,35 @@
package glfw
//#define GLFW_EXPOSE_NATIVE_WIN32
//#define GLFW_EXPOSE_NATIVE_WGL
//#include "glfw/include/GLFW/glfw3.h"
//#include "glfw/include/GLFW/glfw3native.h"
import "C"
// GetWin32Adapter returns the adapter device name of the monitor.
func (m *Monitor) GetWin32Adapter() string {
ret := C.glfwGetWin32Adapter(m.data)
panicError()
return C.GoString(ret)
}
// GetWin32Monitor returns the display device name of the monitor.
func (m *Monitor) GetWin32Monitor() string {
ret := C.glfwGetWin32Monitor(m.data)
panicError()
return C.GoString(ret)
}
// GetWin32Window returns the HWND of the window.
func (w *Window) GetWin32Window() C.HWND {
ret := C.glfwGetWin32Window(w.data)
panicError()
return ret
}
// GetWGLContext returns the HGLRC of the window.
func (w *Window) GetWGLContext() C.HGLRC {
ret := C.glfwGetWGLContext(w.data)
panicError()
return ret
}

41
vendor/github.com/go-gl/glfw/v3.2/glfw/time.go generated vendored Normal file
View File

@ -0,0 +1,41 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
import "C"
// GetTime returns the value of the GLFW timer. Unless the timer has been set
// using SetTime, the timer measures time elapsed since GLFW was initialized.
//
// The resolution of the timer is system dependent, but is usually on the order
// of a few micro- or nanoseconds. It uses the highest-resolution monotonic time
// source on each supported platform.
func GetTime() float64 {
ret := float64(C.glfwGetTime())
panicError()
return ret
}
// SetTime sets the value of the GLFW timer. It then continues to count up from
// that value.
//
// The resolution of the timer is system dependent, but is usually on the order
// of a few micro- or nanoseconds. It uses the highest-resolution monotonic time
// source on each supported platform.
func SetTime(time float64) {
C.glfwSetTime(C.double(time))
panicError()
}
// GetTimerFrequency returns frequency of the timer, in Hz, or zero if an error occurred.
func GetTimerFrequency() uint64 {
ret := uint64(C.glfwGetTimerFrequency())
panicError()
return ret
}
// GetTimerValue returns the current value of the raw timer, measured in 1 / frequency seconds.
func GetTimerValue() uint64 {
ret := uint64(C.glfwGetTimerValue())
panicError()
return ret
}

37
vendor/github.com/go-gl/glfw/v3.2/glfw/util.go generated vendored Normal file
View File

@ -0,0 +1,37 @@
package glfw
//#include <stdlib.h>
//#include "glfw/include/GLFW/glfw3.h"
import "C"
import (
"reflect"
"unsafe"
)
func glfwbool(b C.int) bool {
if b == C.GL_TRUE {
return true
}
return false
}
func bytes(origin []byte) (pointer *uint8, free func()) {
n := len(origin)
if n == 0 {
return nil, func() {}
}
data := C.malloc(C.size_t(n))
dataSlice := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(data),
Len: n,
Cap: n,
}))
copy(dataSlice, origin)
return &dataSlice[0], func() { C.free(data) }
}

14
vendor/github.com/go-gl/glfw/v3.2/glfw/vulkan.go generated vendored Normal file
View File

@ -0,0 +1,14 @@
package glfw
//#include "glfw/include/GLFW/glfw3.h"
import "C"
// VulkanSupported reports whether the Vulkan loader has been found. This check is performed by Init.
//
// The availability of a Vulkan loader does not by itself guarantee that window surface creation or
// even device creation is possible. Call GetRequiredInstanceExtensions to check whether the
// extensions necessary for Vulkan surface creation are available and GetPhysicalDevicePresentationSupport
// to check whether a queue family of a physical device supports image presentation.
func VulkanSupported() bool {
return glfwbool(C.glfwVulkanSupported())
}

57
vendor/github.com/go-gl/glfw/v3.2/glfw/window.c generated vendored Normal file
View File

@ -0,0 +1,57 @@
#include "_cgo_export.h"
void glfwWindowPosCB(GLFWwindow* window, int xpos, int ypos) {
goWindowPosCB(window, xpos, ypos);
}
void glfwWindowSizeCB(GLFWwindow* window, int width, int height) {
goWindowSizeCB(window, width, height);
}
void glfwFramebufferSizeCB(GLFWwindow* window, int width, int height) {
goFramebufferSizeCB(window, width, height);
}
void glfwWindowCloseCB(GLFWwindow* window) {
goWindowCloseCB(window);
}
void glfwWindowRefreshCB(GLFWwindow* window) {
goWindowRefreshCB(window);
}
void glfwWindowFocusCB(GLFWwindow* window, int focused) {
goWindowFocusCB(window, focused);
}
void glfwWindowIconifyCB(GLFWwindow* window, int iconified) {
goWindowIconifyCB(window, iconified);
}
void glfwSetWindowPosCallbackCB(GLFWwindow* window) {
glfwSetWindowPosCallback(window, glfwWindowPosCB);
}
void glfwSetWindowSizeCallbackCB(GLFWwindow* window) {
glfwSetWindowSizeCallback(window, glfwWindowSizeCB);
}
void glfwSetFramebufferSizeCallbackCB(GLFWwindow* window) {
glfwSetFramebufferSizeCallback(window, glfwFramebufferSizeCB);
}
void glfwSetWindowCloseCallbackCB(GLFWwindow* window) {
glfwSetWindowCloseCallback(window, glfwWindowCloseCB);
}
void glfwSetWindowRefreshCallbackCB(GLFWwindow* window) {
glfwSetWindowRefreshCallback(window, glfwWindowRefreshCB);
}
void glfwSetWindowFocusCallbackCB(GLFWwindow* window) {
glfwSetWindowFocusCallback(window, glfwWindowFocusCB);
}
void glfwSetWindowIconifyCallbackCB(GLFWwindow* window) {
glfwSetWindowIconifyCallback(window, glfwWindowIconifyCB);
}

830
vendor/github.com/go-gl/glfw/v3.2/glfw/window.go generated vendored Normal file
View File

@ -0,0 +1,830 @@
package glfw
//#include <stdlib.h>
//#include "glfw/include/GLFW/glfw3.h"
//void glfwSetWindowPosCallbackCB(GLFWwindow *window);
//void glfwSetWindowSizeCallbackCB(GLFWwindow *window);
//void glfwSetFramebufferSizeCallbackCB(GLFWwindow *window);
//void glfwSetWindowCloseCallbackCB(GLFWwindow *window);
//void glfwSetWindowRefreshCallbackCB(GLFWwindow *window);
//void glfwSetWindowFocusCallbackCB(GLFWwindow *window);
//void glfwSetWindowIconifyCallbackCB(GLFWwindow *window);
import "C"
import (
"image"
"image/draw"
"sync"
"unsafe"
)
// Internal window list stuff
type windowList struct {
l sync.Mutex
m map[*C.GLFWwindow]*Window
}
var windows = windowList{m: map[*C.GLFWwindow]*Window{}}
func (w *windowList) put(wnd *Window) {
w.l.Lock()
defer w.l.Unlock()
w.m[wnd.data] = wnd
}
func (w *windowList) remove(wnd *C.GLFWwindow) {
w.l.Lock()
defer w.l.Unlock()
delete(w.m, wnd)
}
func (w *windowList) get(wnd *C.GLFWwindow) *Window {
w.l.Lock()
defer w.l.Unlock()
return w.m[wnd]
}
// Hint corresponds to hints that can be set before creating a window.
//
// Hint also corresponds to the attributes of the window that can be get after
// its creation.
type Hint int
// Window related hints.
const (
Focused Hint = C.GLFW_FOCUSED // Specifies whether the window will be given input focus when created. This hint is ignored for full screen and initially hidden windows.
Iconified Hint = C.GLFW_ICONIFIED // Specifies whether the window will be minimized.
Maximized Hint = C.GLFW_MAXIMIZED // Specifies whether the window is maximized.
Visible Hint = C.GLFW_VISIBLE // Specifies whether the window will be initially visible.
Resizable Hint = C.GLFW_RESIZABLE // Specifies whether the window will be resizable by the user.
Decorated Hint = C.GLFW_DECORATED // Specifies whether the window will have window decorations such as a border, a close widget, etc.
Floating Hint = C.GLFW_FLOATING // Specifies whether the window will be always-on-top.
AutoIconify Hint = C.GLFW_AUTO_ICONIFY // Specifies whether fullscreen windows automatically iconify (and restore the previous video mode) on focus loss.
)
// Context related hints.
const (
ClientAPI Hint = C.GLFW_CLIENT_API // Specifies which client API to create the context for. Hard constraint.
ContextVersionMajor Hint = C.GLFW_CONTEXT_VERSION_MAJOR // Specifies the client API version that the created context must be compatible with.
ContextVersionMinor Hint = C.GLFW_CONTEXT_VERSION_MINOR // Specifies the client API version that the created context must be compatible with.
ContextRobustness Hint = C.GLFW_CONTEXT_ROBUSTNESS // Specifies the robustness strategy to be used by the context.
ContextReleaseBehavior Hint = C.GLFW_CONTEXT_RELEASE_BEHAVIOR // Specifies the release behavior to be used by the context.
OpenGLForwardCompatible Hint = C.GLFW_OPENGL_FORWARD_COMPAT // Specifies whether the OpenGL context should be forward-compatible. Hard constraint.
OpenGLDebugContext Hint = C.GLFW_OPENGL_DEBUG_CONTEXT // Specifies whether to create a debug OpenGL context, which may have additional error and performance issue reporting functionality. If OpenGL ES is requested, this hint is ignored.
OpenGLProfile Hint = C.GLFW_OPENGL_PROFILE // Specifies which OpenGL profile to create the context for. Hard constraint.
ContextCreationAPI Hint = C.GLFW_CONTEXT_CREATION_API // Specifies which context creation API to use to create the context.
)
// Framebuffer related hints.
const (
ContextRevision Hint = C.GLFW_CONTEXT_REVISION
RedBits Hint = C.GLFW_RED_BITS // Specifies the desired bit depth of the default framebuffer.
GreenBits Hint = C.GLFW_GREEN_BITS // Specifies the desired bit depth of the default framebuffer.
BlueBits Hint = C.GLFW_BLUE_BITS // Specifies the desired bit depth of the default framebuffer.
AlphaBits Hint = C.GLFW_ALPHA_BITS // Specifies the desired bit depth of the default framebuffer.
DepthBits Hint = C.GLFW_DEPTH_BITS // Specifies the desired bit depth of the default framebuffer.
StencilBits Hint = C.GLFW_STENCIL_BITS // Specifies the desired bit depth of the default framebuffer.
AccumRedBits Hint = C.GLFW_ACCUM_RED_BITS // Specifies the desired bit depth of the accumulation buffer.
AccumGreenBits Hint = C.GLFW_ACCUM_GREEN_BITS // Specifies the desired bit depth of the accumulation buffer.
AccumBlueBits Hint = C.GLFW_ACCUM_BLUE_BITS // Specifies the desired bit depth of the accumulation buffer.
AccumAlphaBits Hint = C.GLFW_ACCUM_ALPHA_BITS // Specifies the desired bit depth of the accumulation buffer.
AuxBuffers Hint = C.GLFW_AUX_BUFFERS // Specifies the desired number of auxiliary buffers.
Stereo Hint = C.GLFW_STEREO // Specifies whether to use stereoscopic rendering. Hard constraint.
Samples Hint = C.GLFW_SAMPLES // Specifies the desired number of samples to use for multisampling. Zero disables multisampling.
SRGBCapable Hint = C.GLFW_SRGB_CAPABLE // Specifies whether the framebuffer should be sRGB capable.
RefreshRate Hint = C.GLFW_REFRESH_RATE // Specifies the desired refresh rate for full screen windows. If set to zero, the highest available refresh rate will be used. This hint is ignored for windowed mode windows.
DoubleBuffer Hint = C.GLFW_DOUBLEBUFFER // Specifies whether the framebuffer should be double buffered. You nearly always want to use double buffering. This is a hard constraint.
)
// Values for the ClientAPI hint.
const (
OpenGLAPI int = C.GLFW_OPENGL_API
OpenGLESAPI int = C.GLFW_OPENGL_ES_API
NoAPI int = C.GLFW_NO_API
)
// Values for ContextCreationAPI hint.
const (
NativeContextAPI int = C.GLFW_NATIVE_CONTEXT_API
EGLContextAPI int = C.GLFW_EGL_CONTEXT_API
)
// Values for the ContextRobustness hint.
const (
NoRobustness int = C.GLFW_NO_ROBUSTNESS
NoResetNotification int = C.GLFW_NO_RESET_NOTIFICATION
LoseContextOnReset int = C.GLFW_LOSE_CONTEXT_ON_RESET
)
// Values for ContextReleaseBehavior hint.
const (
AnyReleaseBehavior int = C.GLFW_ANY_RELEASE_BEHAVIOR
ReleaseBehaviorFlush int = C.GLFW_RELEASE_BEHAVIOR_FLUSH
ReleaseBehaviorNone int = C.GLFW_RELEASE_BEHAVIOR_NONE
)
// Values for the OpenGLProfile hint.
const (
OpenGLAnyProfile int = C.GLFW_OPENGL_ANY_PROFILE
OpenGLCoreProfile int = C.GLFW_OPENGL_CORE_PROFILE
OpenGLCompatProfile int = C.GLFW_OPENGL_COMPAT_PROFILE
)
// Other values.
const (
True int = C.GL_TRUE
False int = C.GL_FALSE
DontCare int = C.GLFW_DONT_CARE
)
// Window represents a window.
type Window struct {
data *C.GLFWwindow
// Window.
fPosHolder func(w *Window, xpos int, ypos int)
fSizeHolder func(w *Window, width int, height int)
fFramebufferSizeHolder func(w *Window, width int, height int)
fCloseHolder func(w *Window)
fRefreshHolder func(w *Window)
fFocusHolder func(w *Window, focused bool)
fIconifyHolder func(w *Window, iconified bool)
// Input.
fMouseButtonHolder func(w *Window, button MouseButton, action Action, mod ModifierKey)
fCursorPosHolder func(w *Window, xpos float64, ypos float64)
fCursorEnterHolder func(w *Window, entered bool)
fScrollHolder func(w *Window, xoff float64, yoff float64)
fKeyHolder func(w *Window, key Key, scancode int, action Action, mods ModifierKey)
fCharHolder func(w *Window, char rune)
fCharModsHolder func(w *Window, char rune, mods ModifierKey)
fDropHolder func(w *Window, names []string)
}
// GLFWWindow returns a *C.GLFWwindow reference (i.e. the GLFW window itself). This can be used for
// passing the GLFW window handle to external C libraries.
func (w *Window) GLFWWindow() uintptr {
return uintptr(unsafe.Pointer(w.data))
}
//export goWindowPosCB
func goWindowPosCB(window unsafe.Pointer, xpos, ypos C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fPosHolder(w, int(xpos), int(ypos))
}
//export goWindowSizeCB
func goWindowSizeCB(window unsafe.Pointer, width, height C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fSizeHolder(w, int(width), int(height))
}
//export goFramebufferSizeCB
func goFramebufferSizeCB(window unsafe.Pointer, width, height C.int) {
w := windows.get((*C.GLFWwindow)(window))
w.fFramebufferSizeHolder(w, int(width), int(height))
}
//export goWindowCloseCB
func goWindowCloseCB(window unsafe.Pointer) {
w := windows.get((*C.GLFWwindow)(window))
w.fCloseHolder(w)
}
//export goWindowRefreshCB
func goWindowRefreshCB(window unsafe.Pointer) {
w := windows.get((*C.GLFWwindow)(window))
w.fRefreshHolder(w)
}
//export goWindowFocusCB
func goWindowFocusCB(window unsafe.Pointer, focused C.int) {
w := windows.get((*C.GLFWwindow)(window))
isFocused := glfwbool(focused)
w.fFocusHolder(w, isFocused)
}
//export goWindowIconifyCB
func goWindowIconifyCB(window unsafe.Pointer, iconified C.int) {
isIconified := glfwbool(iconified)
w := windows.get((*C.GLFWwindow)(window))
w.fIconifyHolder(w, isIconified)
}
// DefaultWindowHints resets all window hints to their default values.
//
// This function may only be called from the main thread.
func DefaultWindowHints() {
C.glfwDefaultWindowHints()
panicError()
}
// WindowHint sets hints for the next call to CreateWindow. The hints,
// once set, retain their values until changed by a call to WindowHint or
// DefaultWindowHints, or until the library is terminated with Terminate.
//
// This function may only be called from the main thread.
func WindowHint(target Hint, hint int) {
C.glfwWindowHint(C.int(target), C.int(hint))
panicError()
}
// CreateWindow creates a window and its associated context. Most of the options
// controlling how the window and its context should be created are specified
// through Hint.
//
// Successful creation does not change which context is current. Before you can
// use the newly created context, you need to make it current using
// MakeContextCurrent.
//
// Note that the created window and context may differ from what you requested,
// as not all parameters and hints are hard constraints. This includes the size
// of the window, especially for full screen windows. To retrieve the actual
// attributes of the created window and context, use queries like
// GetWindowAttrib and GetWindowSize.
//
// To create the window at a specific position, make it initially invisible using
// the Visible window hint, set its position and then show it.
//
// If a fullscreen window is active, the screensaver is prohibited from starting.
//
// Windows: If the executable has an icon resource named GLFW_ICON, it will be
// set as the icon for the window. If no such icon is present, the IDI_WINLOGO
// icon will be used instead.
//
// Mac OS X: The GLFW window has no icon, as it is not a document window, but the
// dock icon will be the same as the application bundle's icon. Also, the first
// time a window is opened the menu bar is populated with common commands like
// Hide, Quit and About. The (minimal) about dialog uses information from the
// application's bundle. For more information on bundles, see the Bundle
// Programming Guide provided by Apple.
//
// This function may only be called from the main thread.
func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) {
var (
m *C.GLFWmonitor
s *C.GLFWwindow
)
t := C.CString(title)
defer C.free(unsafe.Pointer(t))
if monitor != nil {
m = monitor.data
}
if share != nil {
s = share.data
}
w := C.glfwCreateWindow(C.int(width), C.int(height), t, m, s)
if w == nil {
return nil, acceptError(APIUnavailable, VersionUnavailable)
}
wnd := &Window{data: w}
windows.put(wnd)
return wnd, nil
}
// Destroy destroys the specified window and its context. On calling this
// function, no further callbacks will be called for that window.
//
// This function may only be called from the main thread.
func (w *Window) Destroy() {
windows.remove(w.data)
C.glfwDestroyWindow(w.data)
panicError()
}
// ShouldClose reports the value of the close flag of the specified window.
func (w *Window) ShouldClose() bool {
ret := glfwbool(C.glfwWindowShouldClose(w.data))
panicError()
return ret
}
// SetShouldClose sets the value of the close flag of the window. This can be
// used to override the user's attempt to close the window, or to signal that it
// should be closed.
func (w *Window) SetShouldClose(value bool) {
if !value {
C.glfwSetWindowShouldClose(w.data, C.GL_FALSE)
} else {
C.glfwSetWindowShouldClose(w.data, C.GL_TRUE)
}
panicError()
}
// SetTitle sets the window title, encoded as UTF-8, of the window.
//
// This function may only be called from the main thread.
func (w *Window) SetTitle(title string) {
t := C.CString(title)
defer C.free(unsafe.Pointer(t))
C.glfwSetWindowTitle(w.data, t)
panicError()
}
// SetIcon sets the icon of the specified window. If passed an array of candidate images,
// those of or closest to the sizes desired by the system are selected. If no images are
// specified, the window reverts to its default icon.
//
// The image is ideally provided in the form of *image.NRGBA.
// The pixels are 32-bit, little-endian, non-premultiplied RGBA, i.e. eight
// bits per channel with the red channel first. They are arranged canonically
// as packed sequential rows, starting from the top-left corner. If the image
// type is not *image.NRGBA, it will be converted to it.
//
// The desired image sizes varies depending on platform and system settings. The selected
// images will be rescaled as needed. Good sizes include 16x16, 32x32 and 48x48.
func (w *Window) SetIcon(images []image.Image) {
count := len(images)
cimages := make([]C.GLFWimage, count)
freePixels := make([]func(), count)
for i, img := range images {
var pixels []uint8
b := img.Bounds()
switch img := img.(type) {
case *image.NRGBA:
pixels = img.Pix
default:
m := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
pixels = m.Pix
}
pix, free := bytes(pixels)
freePixels[i] = free
cimages[i].width = C.int(b.Dx())
cimages[i].height = C.int(b.Dy())
cimages[i].pixels = (*C.uchar)(pix)
}
var p *C.GLFWimage
if count > 0 {
p = &cimages[0]
}
C.glfwSetWindowIcon(w.data, C.int(count), p)
for _, v := range freePixels {
v()
}
panicError()
}
// GetPos returns the position, in screen coordinates, of the upper-left
// corner of the client area of the window.
func (w *Window) GetPos() (x, y int) {
var xpos, ypos C.int
C.glfwGetWindowPos(w.data, &xpos, &ypos)
panicError()
return int(xpos), int(ypos)
}
// SetPos sets the position, in screen coordinates, of the upper-left corner
// of the client area of the window.
//
// If it is a full screen window, this function does nothing.
//
// If you wish to set an initial window position you should create a hidden
// window (using Hint and Visible), set its position and then show it.
//
// It is very rarely a good idea to move an already visible window, as it will
// confuse and annoy the user.
//
// The window manager may put limits on what positions are allowed.
//
// This function may only be called from the main thread.
func (w *Window) SetPos(xpos, ypos int) {
C.glfwSetWindowPos(w.data, C.int(xpos), C.int(ypos))
panicError()
}
// GetSize returns the size, in screen coordinates, of the client area of the
// specified window.
func (w *Window) GetSize() (width, height int) {
var wi, h C.int
C.glfwGetWindowSize(w.data, &wi, &h)
panicError()
return int(wi), int(h)
}
// SetSize sets the size, in screen coordinates, of the client area of the
// window.
//
// For full screen windows, this function selects and switches to the resolution
// closest to the specified size, without affecting the window's context. As the
// context is unaffected, the bit depths of the framebuffer remain unchanged.
//
// The window manager may put limits on what window sizes are allowed.
//
// This function may only be called from the main thread.
func (w *Window) SetSize(width, height int) {
C.glfwSetWindowSize(w.data, C.int(width), C.int(height))
panicError()
}
// SetSizeLimits sets the size limits of the client area of the specified window.
// If the window is full screen or not resizable, this function does nothing.
//
// The size limits are applied immediately and may cause the window to be resized.
func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
C.glfwSetWindowSizeLimits(w.data, C.int(minw), C.int(minh), C.int(maxw), C.int(maxh))
panicError()
}
// SetAspectRatio sets the required aspect ratio of the client area of the specified window.
// If the window is full screen or not resizable, this function does nothing.
//
// The aspect ratio is specified as a numerator and a denominator and both values must be greater
// than zero. For example, the common 16:9 aspect ratio is specified as 16 and 9, respectively.
//
// If the numerator and denominator is set to glfw.DontCare then the aspect ratio limit is disabled.
//
// The aspect ratio is applied immediately and may cause the window to be resized.
func (w *Window) SetAspectRatio(numer, denom int) {
C.glfwSetWindowAspectRatio(w.data, C.int(numer), C.int(denom))
panicError()
}
// GetFramebufferSize retrieves the size, in pixels, of the framebuffer of the
// specified window.
func (w *Window) GetFramebufferSize() (width, height int) {
var wi, h C.int
C.glfwGetFramebufferSize(w.data, &wi, &h)
panicError()
return int(wi), int(h)
}
// GetFrameSize retrieves the size, in screen coordinates, of each edge of the frame
// of the specified window. This size includes the title bar, if the window has one.
// The size of the frame may vary depending on the window-related hints used to create it.
//
// Because this function retrieves the size of each window frame edge and not the offset
// along a particular coordinate axis, the retrieved values will always be zero or positive.
func (w *Window) GetFrameSize() (left, top, right, bottom int) {
var l, t, r, b C.int
C.glfwGetWindowFrameSize(w.data, &l, &t, &r, &b)
panicError()
return int(l), int(t), int(r), int(b)
}
// Focus brings the specified window to front and sets input focus.
// The window should already be visible and not iconified.
//
// By default, both windowed and full screen mode windows are focused when initially created.
// Set the glfw.Focused to disable this behavior.
//
// Do not use this function to steal focus from other applications unless you are certain that
// is what the user wants. Focus stealing can be extremely disruptive.
func (w *Window) Focus() error {
C.glfwFocusWindow(w.data)
return acceptError(APIUnavailable)
}
// Iconify iconifies/minimizes the window, if it was previously restored. If it
// is a full screen window, the original monitor resolution is restored until the
// window is restored. If the window is already iconified, this function does
// nothing.
//
// This function may only be called from the main thread.
func (w *Window) Iconify() error {
C.glfwIconifyWindow(w.data)
return acceptError(APIUnavailable)
}
// Maximize maximizes the specified window if it was previously not maximized.
// If the window is already maximized, this function does nothing.
//
// If the specified window is a full screen window, this function does nothing.
func (w *Window) Maximize() error {
C.glfwMaximizeWindow(w.data)
return acceptError(APIUnavailable)
}
// Restore restores the window, if it was previously iconified/minimized. If it
// is a full screen window, the resolution chosen for the window is restored on
// the selected monitor. If the window is already restored, this function does
// nothing.
//
// This function may only be called from the main thread.
func (w *Window) Restore() error {
C.glfwRestoreWindow(w.data)
return acceptError(APIUnavailable)
}
// Show makes the window visible, if it was previously hidden. If the window is
// already visible or is in full screen mode, this function does nothing.
//
// This function may only be called from the main thread.
func (w *Window) Show() {
C.glfwShowWindow(w.data)
panicError()
}
// Hide hides the window, if it was previously visible. If the window is already
// hidden or is in full screen mode, this function does nothing.
//
// This function may only be called from the main thread.
func (w *Window) Hide() {
C.glfwHideWindow(w.data)
panicError()
}
// GetMonitor returns the handle of the monitor that the window is in
// fullscreen on.
//
// Returns nil if the window is in windowed mode.
func (w *Window) GetMonitor() *Monitor {
m := C.glfwGetWindowMonitor(w.data)
panicError()
if m == nil {
return nil
}
return &Monitor{m}
}
// SetMonitor sets the monitor that the window uses for full screen mode or,
// if the monitor is NULL, makes it windowed mode.
//
// When setting a monitor, this function updates the width, height and refresh
// rate of the desired video mode and switches to the video mode closest to it.
// The window position is ignored when setting a monitor.
//
// When the monitor is NULL, the position, width and height are used to place
// the window client area. The refresh rate is ignored when no monitor is specified.
// If you only wish to update the resolution of a full screen window or the size of
// a windowed mode window, see window.SetSize.
//
// When a window transitions from full screen to windowed mode, this function
// restores any previous window settings such as whether it is decorated, floating,
// resizable, has size or aspect ratio limits, etc..
func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
var m *C.GLFWmonitor
if monitor == nil {
m = nil
} else {
m = monitor.data
}
C.glfwSetWindowMonitor(w.data, m, C.int(xpos), C.int(ypos), C.int(width), C.int(height), C.int(refreshRate))
panicError()
}
// GetAttrib returns an attribute of the window. There are many attributes,
// some related to the window and others to its context.
func (w *Window) GetAttrib(attrib Hint) int {
ret := int(C.glfwGetWindowAttrib(w.data, C.int(attrib)))
panicError()
return ret
}
// SetUserPointer sets the user-defined pointer of the window. The current value
// is retained until the window is destroyed. The initial value is nil.
func (w *Window) SetUserPointer(pointer unsafe.Pointer) {
C.glfwSetWindowUserPointer(w.data, pointer)
panicError()
}
// GetUserPointer returns the current value of the user-defined pointer of the
// window. The initial value is nil.
func (w *Window) GetUserPointer() unsafe.Pointer {
ret := C.glfwGetWindowUserPointer(w.data)
panicError()
return ret
}
// PosCallback is the window position callback.
type PosCallback func(w *Window, xpos int, ypos int)
// SetPosCallback sets the position callback of the window, which is called
// when the window is moved. The callback is provided with the screen position
// of the upper-left corner of the client area of the window.
func (w *Window) SetPosCallback(cbfun PosCallback) (previous PosCallback) {
previous = w.fPosHolder
w.fPosHolder = cbfun
if cbfun == nil {
C.glfwSetWindowPosCallback(w.data, nil)
} else {
C.glfwSetWindowPosCallbackCB(w.data)
}
panicError()
return previous
}
// SizeCallback is the window size callback.
type SizeCallback func(w *Window, width int, height int)
// SetSizeCallback sets the size callback of the window, which is called when
// the window is resized. The callback is provided with the size, in screen
// coordinates, of the client area of the window.
func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
previous = w.fSizeHolder
w.fSizeHolder = cbfun
if cbfun == nil {
C.glfwSetWindowSizeCallback(w.data, nil)
} else {
C.glfwSetWindowSizeCallbackCB(w.data)
}
panicError()
return previous
}
// FramebufferSizeCallback is the framebuffer size callback.
type FramebufferSizeCallback func(w *Window, width int, height int)
// SetFramebufferSizeCallback sets the framebuffer resize callback of the specified
// window, which is called when the framebuffer of the specified window is resized.
func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
previous = w.fFramebufferSizeHolder
w.fFramebufferSizeHolder = cbfun
if cbfun == nil {
C.glfwSetFramebufferSizeCallback(w.data, nil)
} else {
C.glfwSetFramebufferSizeCallbackCB(w.data)
}
panicError()
return previous
}
// CloseCallback is the window close callback.
type CloseCallback func(w *Window)
// SetCloseCallback sets the close callback of the window, which is called when
// the user attempts to close the window, for example by clicking the close
// widget in the title bar.
//
// The close flag is set before this callback is called, but you can modify it at
// any time with SetShouldClose.
//
// Mac OS X: Selecting Quit from the application menu will trigger the close
// callback for all windows.
func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
previous = w.fCloseHolder
w.fCloseHolder = cbfun
if cbfun == nil {
C.glfwSetWindowCloseCallback(w.data, nil)
} else {
C.glfwSetWindowCloseCallbackCB(w.data)
}
panicError()
return previous
}
// RefreshCallback is the window refresh callback.
type RefreshCallback func(w *Window)
// SetRefreshCallback sets the refresh callback of the window, which
// is called when the client area of the window needs to be redrawn, for example
// if the window has been exposed after having been covered by another window.
//
// On compositing window systems such as Aero, Compiz or Aqua, where the window
// contents are saved off-screen, this callback may be called only very
// infrequently or never at all.
func (w *Window) SetRefreshCallback(cbfun RefreshCallback) (previous RefreshCallback) {
previous = w.fRefreshHolder
w.fRefreshHolder = cbfun
if cbfun == nil {
C.glfwSetWindowRefreshCallback(w.data, nil)
} else {
C.glfwSetWindowRefreshCallbackCB(w.data)
}
panicError()
return previous
}
// FocusCallback is the window focus callback.
type FocusCallback func(w *Window, focused bool)
// SetFocusCallback sets the focus callback of the window, which is called when
// the window gains or loses focus.
//
// After the focus callback is called for a window that lost focus, synthetic key
// and mouse button release events will be generated for all such that had been
// pressed. For more information, see SetKeyCallback and SetMouseButtonCallback.
func (w *Window) SetFocusCallback(cbfun FocusCallback) (previous FocusCallback) {
previous = w.fFocusHolder
w.fFocusHolder = cbfun
if cbfun == nil {
C.glfwSetWindowFocusCallback(w.data, nil)
} else {
C.glfwSetWindowFocusCallbackCB(w.data)
}
panicError()
return previous
}
// IconifyCallback is the window iconification callback.
type IconifyCallback func(w *Window, iconified bool)
// SetIconifyCallback sets the iconification callback of the window, which is
// called when the window is iconified or restored.
func (w *Window) SetIconifyCallback(cbfun IconifyCallback) (previous IconifyCallback) {
previous = w.fIconifyHolder
w.fIconifyHolder = cbfun
if cbfun == nil {
C.glfwSetWindowIconifyCallback(w.data, nil)
} else {
C.glfwSetWindowIconifyCallbackCB(w.data)
}
panicError()
return previous
}
// SetClipboardString sets the system clipboard to the specified UTF-8 encoded
// string.
//
// This function may only be called from the main thread.
func (w *Window) SetClipboardString(str string) {
cp := C.CString(str)
defer C.free(unsafe.Pointer(cp))
C.glfwSetClipboardString(w.data, cp)
panicError()
}
// GetClipboardString returns the contents of the system clipboard, if it
// contains or is convertible to a UTF-8 encoded string.
//
// This function may only be called from the main thread.
func (w *Window) GetClipboardString() (string, error) {
cs := C.glfwGetClipboardString(w.data)
if cs == nil {
return "", acceptError(FormatUnavailable)
}
return C.GoString(cs), nil
}
// PollEvents processes only those events that have already been received and
// then returns immediately. Processing events will cause the window and input
// callbacks associated with those events to be called.
//
// This function is not required for joystick input to work.
//
// This function may not be called from a callback.
//
// This function may only be called from the main thread.
func PollEvents() {
C.glfwPollEvents()
panicError()
}
// WaitEvents puts the calling thread to sleep until at least one event has been
// received. Once one or more events have been recevied, it behaves as if
// PollEvents was called, i.e. the events are processed and the function then
// returns immediately. Processing events will cause the window and input
// callbacks associated with those events to be called.
//
// Since not all events are associated with callbacks, this function may return
// without a callback having been called even if you are monitoring all
// callbacks.
//
// This function may not be called from a callback.
//
// This function may only be called from the main thread.
func WaitEvents() {
C.glfwWaitEvents()
panicError()
}
// WaitEventsTimeout puts the calling thread to sleep until at least one event is available in the
// event queue, or until the specified timeout is reached. If one or more events are available,
// it behaves exactly like PollEvents, i.e. the events in the queue are processed and the function
// then returns immediately. Processing events will cause the window and input callbacks associated
// with those events to be called.
//
// The timeout value must be a positive finite number.
//
// Since not all events are associated with callbacks, this function may return without a callback
// having been called even if you are monitoring all callbacks.
//
// On some platforms, a window move, resize or menu operation will cause event processing to block.
// This is due to how event processing is designed on those platforms. You can use the window
// refresh callback to redraw the contents of your window when necessary during such operations.
//
// On some platforms, certain callbacks may be called outside of a call to one of the event
// processing functions.
//
// If no windows exist, this function returns immediately. For synchronization of threads in
// applications that do not create windows, use native Go primitives.
//
// Event processing is not required for joystick input to work.
func WaitEventsTimeout(timeout float64) {
C.glfwWaitEventsTimeout(C.double(timeout))
panicError()
}
// PostEmptyEvent posts an empty event from the current thread to the main
// thread event queue, causing WaitEvents to return.
//
// If no windows exist, this function returns immediately. For synchronization of threads in
// applications that do not create windows, use native Go primitives.
//
// This function may be called from secondary threads.
func PostEmptyEvent() {
C.glfwPostEmptyEvent()
panicError()
}

14
vendor/github.com/go-gl/mathgl/AUTHORS generated vendored Normal file
View File

@ -0,0 +1,14 @@
# This is the official list of go-gl/mathgl authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS files.
# See the latter for an explanation.
# Names should be added to this file as
# Name or Organization <email address>
# The email address is not required for organizations.
# Please keep the list sorted.
Alex Vasi <eee@someuser.com>
Dmitri Shuralyov <shurcooL@gmail.com>
GlenKelley <???>
Zoe Juozapaitis <jragonmiris@gmail.com>

21
vendor/github.com/go-gl/mathgl/CONTRIBUTORS generated vendored Normal file
View File

@ -0,0 +1,21 @@
# This is the official list of people who can contribute
# (and typically have contributed) code to the mathgl
# repository.
#
# The AUTHORS file lists the copyright holders; this file
# lists people. For example, Google employees would be listed here
# but not in AUTHORS, because Google would hold the copyright.
#
# When adding J Random Contributor's name to this file,
# either J's name or J's organization's name should be
# added to the AUTHORS file.
#
# Names should be added to this file like so:
# Name <email address>
#
# Please keep the list sorted.
Alex Vasi <eee@someuser.com>
Dmitri Shuralyov <shurcooL@gmail.com>
GlenKelley <???>
Zoe Juozapaitis <jragonmiris@gmail.com>

23
vendor/github.com/go-gl/mathgl/LICENSE generated vendored Normal file
View File

@ -0,0 +1,23 @@
Copyright ©2013 The go-gl Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the gonum project nor the names of its authors and
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

276
vendor/github.com/go-gl/mathgl/mgl32/codegen.go generated vendored Normal file
View File

@ -0,0 +1,276 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
// codegen generates go code from templates. Intended to be
// used with go generate; Also makes mgl64 from mgl32.
// See the invocation in mgl32/util.go for details.
// To use it, just run "go generate github.com/go-gl/mathgl/mgl32"
// (or "go generate" in mgl32 directory).
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
type Context struct {
Comment string
TemplateName string
}
type MatrixIter struct {
M int // row
N int // column
index int
}
var mgl64RewriteRules = []string{
"mgl32 -> mgl64",
"float32 -> float64",
"f32 -> f64",
"a.Float32 -> a.Float64",
"math.MaxFloat32 -> math.MaxFloat64",
"math.SmallestNonzeroFloat32 -> math.SmallestNonzeroFloat64",
}
func main() {
flag.Usage = func() {
fmt.Println("Usage: codegen -template file.tmpl -output file.go")
fmt.Println("Usage: codegen -mgl64 [-dir ../mgl64]")
flag.PrintDefaults()
}
tmplPath := flag.String("template", "file.tmpl", "template path")
oPath := flag.String("output", "file.go", "output path")
mgl64 := flag.Bool("mgl64", false, "make mgl64")
mgl64Path := flag.String("dir", "../mgl64", "path to mgl64 location")
flag.Parse()
if flag.NArg() > 0 || flag.NFlag() == 0 {
flag.Usage()
os.Exit(2)
}
if *mgl64 {
genMgl64(*mgl64Path)
return
}
tmpl := template.New("").Delims("<<", ">>").Funcs(template.FuncMap{
"typename": typenameHelper,
"elementname": elementNameHelper,
"iter": iterHelper,
"matiter": matrixIterHelper,
"enum": enumHelper,
"sep": separatorHelper,
"repeat": repeatHelper,
"add": addHelper,
"mul": mulHelper,
})
tmpl = template.Must(tmpl.ParseFiles(*tmplPath))
tmplName := filepath.Base(*tmplPath)
oFile, err := os.Create(*oPath)
if err != nil {
panic(err)
}
context := Context{
Comment: "This file is generated by codegen.go; DO NOT EDIT",
TemplateName: tmplName,
}
if err = tmpl.ExecuteTemplate(oFile, tmplName, context); err != nil {
panic(err)
}
oFile.Close()
if err = rungofmt(*oPath, false, nil); err != nil {
panic(err)
}
}
func genMgl64(destPath string) {
HandleFile := func(source string, info os.FileInfo, err error) error {
if err != nil {
return err
}
dest := filepath.Join(destPath, source)
if info.IsDir() {
return os.MkdirAll(dest, info.Mode())
}
if !strings.HasSuffix(source, ".go") || info.Name() == "codegen.go" {
return nil
}
if !info.Mode().IsRegular() {
fmt.Println("Ignored, not a regular file:", source)
return nil
}
in, err := ioutil.ReadFile(source)
if err != nil {
return err
}
out, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC,
info.Mode())
if err != nil {
return err
}
defer out.Close()
comment := fmt.Sprintf(
"// This file is generated from mgl32/%s; DO NOT EDIT\n\n",
source)
if _, err = out.WriteString(comment); err != nil {
return err
}
r := strings.NewReplacer("//go:generate ", "//#go:generate ") // We don't want go generate directives in mgl64 package.
if _, err = r.WriteString(out, string(in)); err != nil {
return err
}
return rungofmt(dest, true, mgl64RewriteRules)
}
if err := filepath.Walk(".", HandleFile); err != nil {
panic(err)
}
}
func rungofmt(path string, fiximports bool, rewriteRules []string) error {
args := []string{"-w", path}
output, err := exec.Command("gofmt", args...).CombinedOutput()
for i := 0; err == nil && i < len(rewriteRules); i++ {
args = []string{"-w", "-r", rewriteRules[i], path}
output, err = exec.Command("gofmt", args...).CombinedOutput()
}
if fiximports && err == nil {
args = []string{"-w", path}
output, err = exec.Command("goimports", args...).CombinedOutput()
}
if err != nil {
fmt.Println("Error executing gofmt", strings.Join(args, " "))
os.Stdout.Write(output)
}
return err
}
func typenameHelper(m, n int) string {
if m == 1 {
return fmt.Sprintf("Vec%d", n)
}
if n == 1 {
return fmt.Sprintf("Vec%d", m)
}
if m == n {
return fmt.Sprintf("Mat%d", m)
}
return fmt.Sprintf("Mat%dx%d", m, n)
}
func elementNameHelper(m int) string {
switch m {
case 0:
return "X"
case 1:
return "Y"
case 2:
return "Z"
case 3:
return "W"
default:
panic("Can't generate element name")
}
}
func iterHelper(start, end int) []int {
iter := make([]int, end-start)
for i := start; i < end; i++ {
iter[i] = i
}
return iter
}
func matrixIterHelper(rows, cols int) []MatrixIter {
res := make([]MatrixIter, 0, rows*cols)
for n := 0; n < cols; n++ {
for m := 0; m < rows; m++ {
res = append(res, MatrixIter{
M: m,
N: n,
index: n*rows + m,
})
}
}
return res
}
// Template function that returns slice from its arguments. Indended to be used
// in range loops.
func enumHelper(args ...int) []int {
return args
}
// Template function to insert commas and '+' in range loops.
func separatorHelper(sep string, iterCond int) string {
if iterCond > 0 {
return sep
}
return ""
}
// Template function to repeat string 'count' times. Inserting 'sep' between
// repetitions. Also changes all occurrences of '%d' to repetition number.
// For example, repeatHelper(3, "col%d", ",") will output "col0, col1, col2"
func repeatHelper(count int, text string, sep string) string {
var res bytes.Buffer
for i := 0; i < count; i++ {
if i > 0 {
res.WriteString(sep)
}
res.WriteString(strings.Replace(text, "%d", fmt.Sprintf("%d", i), -1))
}
return res.String()
}
func addHelper(args ...int) int {
res := 0
for _, a := range args {
res += a
}
return res
}
func mulHelper(args ...int) int {
res := 1
for _, a := range args {
res *= a
}
return res
}
func (i MatrixIter) String() string {
return fmt.Sprintf("%d", i.index)
}

94
vendor/github.com/go-gl/mathgl/mgl32/conv.go generated vendored Normal file
View File

@ -0,0 +1,94 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// Converts 3-dimensional cartesian coordinates (x,y,z) to spherical
// coordinates with radius r, inclination theta, and azimuth phi.
//
// All angles are in radians.
func CartesianToSpherical(coord Vec3) (r, theta, phi float32) {
r = coord.Len()
theta = float32(math.Acos(float64(coord[2] / r)))
phi = float32(math.Atan2(float64(coord[1]), float64(coord[0])))
return
}
// Converts 3-dimensional cartesian coordinates (x,y,z) to cylindrical
// coordinates with radial distance r, azimuth phi, and height z.
//
// All angles are in radians.
func CartesianToCylindical(coord Vec3) (rho, phi, z float32) {
rho = float32(math.Hypot(float64(coord[0]), float64(coord[1])))
phi = float32(math.Atan2(float64(coord[1]), float64(coord[0])))
z = coord[2]
return
}
// Converts spherical coordinates with radius r, inclination theta,
// and azimuth phi to cartesian coordinates (x,y,z).
//
// Angles are in radians.
func SphericalToCartesian(r, theta, phi float32) Vec3 {
st, ct := math.Sincos(float64(theta))
sp, cp := math.Sincos(float64(phi))
return Vec3{r * float32(st*cp), r * float32(st*sp), r * float32(ct)}
}
// Converts spherical coordinates with radius r, inclination theta,
// and azimuth phi to cylindrical coordinates with radial distance r,
// azimuth phi, and height z.
//
// Angles are in radians
func SphericalToCylindrical(r, theta, phi float32) (rho, phi2, z float32) {
s, c := math.Sincos(float64(theta))
rho = r * float32(s)
z = r * float32(c)
phi2 = phi
return
}
// Converts cylindrical coordinates with radial distance r,
// azimuth phi, and height z to spherical coordinates with radius r,
// inclination theta, and azimuth phi.
//
// Angles are in radians
func CylindircalToSpherical(rho, phi, z float32) (r, theta, phi2 float32) {
r = float32(math.Hypot(float64(rho), float64(z)))
phi2 = phi
theta = float32(math.Atan2(float64(rho), float64(z)))
return
}
// Converts cylindrical coordinates with radial distance r,
// azimuth phi, and height z to cartesian coordinates (x,y,z)
//
// Angles are in radians.
func CylindricalToCartesian(rho, phi, z float32) Vec3 {
s, c := math.Sincos(float64(phi))
return Vec3{rho * float32(c), rho * float32(s), z}
}
// Converts degrees to radians
func DegToRad(angle float32) float32 {
return angle * float32(math.Pi) / 180
}
// Converts radians to degrees
func RadToDeg(angle float32) float32 {
return angle * 180 / float32(math.Pi)
}

22
vendor/github.com/go-gl/mathgl/mgl32/doc.go generated vendored Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package mgl[32|64] (an abbreviation of mathgl since the packages were split between 32 and 64-bit versions)
is a pure Go math package specialized for 3D math, with inspiration from GLM. It provides statically-sized vectors and matrices with
compile-time generated calculations for most basic math operations. It also provides several basic graphics utilities such as bezier curves and surfaces,
generation of basic primitives like circles, easy creation of common matrices such as perspective or rotation, and common operations like converting
to/from screen/OpenGL coordinates or Projecting/Unprojecting from an MVP matrix. Quaternions are also supported.
The basic vectors and matrices are written with code generation, so looking directly at the source will probably be a bit confusing. I recommend looking at the Godoc
instead, as all basic functions are documented.
This package is written in Column Major Order to make it easier with OpenGL. This means for uniform blocks you can use the default ordering, and when you call
pass-in functions you can leave the "transpose" argument as false.
The package now contains variable sized vectors and matrices. Using these is discouraged. They exist for corner cases where you need "small" matrices that are still
bigger than 4x4. An example may be a Jacobean used for inverse kinematics. Things like computer vision or general linear algebra are best left to packages
more directly suited for that task -- OpenCV, BLAS, LAPACK, numpy, gonum (if you want to stay in Go), and so on.
*/
package mgl32

494
vendor/github.com/go-gl/mathgl/mgl32/matmn.go generated vendored Normal file
View File

@ -0,0 +1,494 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// An arbitrary mxn matrix backed by a slice of floats.
//
// This is emphatically not recommended for hardcore n-dimensional
// linear algebra. For that purpose I recommend github.com/gonum/matrix or
// well-tested C libraries such as BLAS or LAPACK.
//
// This is meant to complement future algorithms that may require matrices larger than
// 4x4, but still relatively small (e.g. Jacobeans for inverse kinematics).
//
// It makes use of the same memory sync.Pool set that VecN does, with the same sizing rules.
//
// MatMN will always check if the receiver is nil on any method. Meaning MathMN(nil).Add(dst,m2)
// should always work. Except for the Reshape function, the semantics of this is to "propogate" nils
// forward, so if an invalid operation occurs in a long chain of matrix operations, the overall result will be nil.
type MatMxN struct {
m, n int
dat []float32
}
// Creates a matrix backed by a new slice of size m*n
func NewMatrix(m, n int) (mat *MatMxN) {
if shouldPool {
return &MatMxN{m: m, n: n, dat: grabFromPool(m * n)}
} else {
return &MatMxN{m: m, n: n, dat: make([]float32, m*n)}
}
}
// Returns a matrix with data specified by the data in src
//
// For instance, to create a 3x3 MatMN from a Mat3
//
// m1 := mgl32.Rotate3DX(3.14159)
// mat := mgl32.NewBackedMatrix(m1[:],3,3)
//
// will create an MN matrix matching the data in the original
// rotation matrix. This matrix is NOT backed by the initial slice;
// it's a copy of the data
//
// If m*n > cap(src), this function will panic.
func NewMatrixFromData(src []float32, m, n int) *MatMxN {
var internal []float32
if shouldPool {
internal = grabFromPool(m * n)
} else {
internal = make([]float32, m*n)
}
copy(internal, src[:m*n])
return &MatMxN{m: m, n: n, dat: internal}
}
// Copies src into dst. This Reshapes dst
// to the same size as src.
//
// If dst or src is nil, this is a no-op
func CopyMatMN(dst, src *MatMxN) {
if dst == nil || src == nil {
return
}
dst.Reshape(src.m, src.n)
copy(dst.dat, src.dat)
}
// Stores the NxN identity matrix in dst, reallocating as necessary.
func IdentN(dst *MatMxN, n int) *MatMxN {
dst = dst.Reshape(n, n)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i == j {
dst.Set(i, j, 1)
} else {
dst.Set(i, j, 0)
}
}
}
return dst
}
// Creates an NxN diagonal matrix seeded by the diagonal vector
// diag. Meaning: for all entries, where i==j, dst.At(i,j) = diag[i]. Otherwise
// dst.At(i,j) = 0
//
// This reshapes dst to the correct size, returning/grabbing from the memory pool as necessary.
func DiagN(dst *MatMxN, diag *VecN) *MatMxN {
dst = dst.Reshape(len(diag.vec), len(diag.vec))
n := len(diag.vec)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i == j {
dst.Set(i, j, diag.vec[i])
} else {
dst.Set(i, j, 0)
}
}
}
return dst
}
// Reshapes the matrix to m by n and zeroes out all
// elements.
func (mat *MatMxN) Zero(m, n int) {
if mat == nil {
return
}
mat.Reshape(m, n)
for i := range mat.dat {
mat.dat[i] = 0
}
}
// Returns the underlying matrix slice to the memory pool
func (mat *MatMxN) destroy() {
if mat == nil {
return
}
if shouldPool && mat.dat != nil {
returnToPool(mat.dat)
}
mat.m, mat.n = 0, 0
mat.dat = nil
}
// Reshapes the matrix to the desired dimensions.
// If the overall size of the new matrix (m*n) is bigger
// than the current size, the underlying slice will
// be grown, sending the current slice to the memory pool
// and grabbing a bigger one if necessary
//
// If the caller is a nil pointer, the return value will be a new
// matrix, as if NewMatrix(m,n) had been called. Otherwise it's
// simply the caller.
func (mat *MatMxN) Reshape(m, n int) *MatMxN {
if mat == nil {
return NewMatrix(m, n)
}
if m*n <= cap(mat.dat) {
if mat.dat != nil {
mat.dat = mat.dat[:m*n]
} else {
mat.dat = []float32{}
}
mat.m, mat.n = m, n
return mat
}
if shouldPool && mat.dat != nil {
returnToPool(mat.dat)
}
(*mat) = (*NewMatrix(m, n))
return mat
}
// Infers an MxN matrix from a constant matrix from this package. For instance,
// a Mat2x3 inferred with this function will work just like NewMatrixFromData(m[:],2,3)
// where m is the Mat2x3. This uses a type switch.
//
// I personally recommend using NewMatrixFromData, because it avoids a potentially costly type switch.
// However, this is also more robust and less error prone if you change the size of your matrix somewhere.
//
// If the value passed in is not recognized, it returns an InferMatrixError.
func (mat *MatMxN) InferMatrix(m interface{}) (*MatMxN, error) {
switch raw := m.(type) {
case Mat2:
return NewMatrixFromData(raw[:], 2, 2), nil
case Mat2x3:
return NewMatrixFromData(raw[:], 2, 3), nil
case Mat2x4:
return NewMatrixFromData(raw[:], 2, 4), nil
case Mat3:
return NewMatrixFromData(raw[:], 3, 3), nil
case Mat3x2:
return NewMatrixFromData(raw[:], 3, 2), nil
case Mat3x4:
return NewMatrixFromData(raw[:], 3, 4), nil
case Mat4:
return NewMatrixFromData(raw[:], 4, 4), nil
case Mat4x2:
return NewMatrixFromData(raw[:], 4, 2), nil
case Mat4x3:
return NewMatrixFromData(raw[:], 4, 3), nil
default:
return nil, InferMatrixError{}
}
}
// Returns the trace of a square matrix (sum of all diagonal elements). If the matrix
// is nil, or not square, the result will be NaN.
func (mat *MatMxN) Trace() float32 {
if mat == nil || mat.m != mat.n {
return float32(math.NaN())
}
var out float32
for i := 0; i < mat.m; i++ {
out += mat.At(i, i)
}
return out
}
// Takes the transpose of mat and puts it in dst.
//
// If dst is not of the correct dimensions, it will be Reshaped,
// if dst and mat are the same, a temporary matrix of the correct size will
// be allocated; these resources will be released via the memory pool.
//
// This should be improved in the future.
func (mat *MatMxN) Transpose(dst *MatMxN) (t *MatMxN) {
if mat == nil {
return nil
}
if dst == mat {
dst = NewMatrix(mat.n, mat.m)
// Copy data to correct matrix,
// delete temporary buffer,
// and set the return value to the
// correct one
defer func() {
copy(mat.dat, dst.dat)
mat.m, mat.n = mat.n, mat.m
dst.destroy()
t = mat
}()
return mat
} else {
dst = dst.Reshape(mat.n, mat.m)
}
for r := 0; r < mat.m; r++ {
for c := 0; c < mat.n; c++ {
dst.dat[r*dst.m+c] = mat.dat[c*mat.m+r]
}
}
return dst
}
// Returns the raw slice backing this matrix
func (mat *MatMxN) Raw() []float32 {
if mat == nil {
return nil
}
return mat.dat
}
// Returns the number of rows in this matrix
func (mat *MatMxN) NumRows() int {
return mat.m
}
// Returns the number of columns in this matrix
func (mat *MatMxN) NumCols() int {
return mat.n
}
// Returns the number of rows and columns in this matrix
// as a single operation
func (mat *MatMxN) NumRowCols() (rows, cols int) {
return mat.m, mat.n
}
// Returns the element at the given row and column.
// This is garbage in/garbage out and does no bounds
// checking. If the computation happens to lead to an invalid
// element, it will be returned; or it may panic.
func (mat *MatMxN) At(row, col int) float32 {
return mat.dat[col*mat.m+row]
}
// Sets the element at the given row and column.
// This is garbage in/garbage out and does no bounds
// checking. If the computation happens to lead to an invalid
// element, it will be set; or it may panic.
func (mat *MatMxN) Set(row, col int, val float32) {
mat.dat[col*mat.m+row] = val
}
func (mat *MatMxN) Add(dst *MatMxN, addend *MatMxN) *MatMxN {
if mat == nil || addend == nil || mat.m != addend.m || mat.n != addend.n {
return nil
}
dst = dst.Reshape(mat.m, mat.n)
// No need to care about rows and columns
// since it's element-wise anyway
for i, el := range mat.dat {
dst.dat[i] = el + addend.dat[i]
}
return dst
}
func (mat *MatMxN) Sub(dst *MatMxN, subtrahend *MatMxN) *MatMxN {
if mat == nil || subtrahend == nil || mat.m != subtrahend.m || mat.n != subtrahend.n {
return nil
}
dst = dst.Reshape(mat.m, mat.n)
// No need to care about rows and columns
// since it's element-wise anyway
for i, el := range mat.dat {
dst.dat[i] = el - subtrahend.dat[i]
}
return dst
}
// Performs matrix multiplication on MxN matrix mat and NxO matrix mul, storing the result in dst.
// This returns dst, or nil if the operation is not able to be performed.
//
// If mat == dst, or mul == dst a temporary matrix will be used.
//
// This uses the naive algorithm (though on smaller matrices,
// this can actually be faster; about len(mat)+len(mul) < ~100)
func (mat *MatMxN) MulMxN(dst *MatMxN, mul *MatMxN) *MatMxN {
if mat == nil || mul == nil || mat.n != mul.m {
return nil
}
if dst == mul {
mul = NewMatrix(mul.m, mul.n)
copy(mul.dat, dst.dat)
// If mat==dst==mul, we need to change
// mat too or we have a bug
if mat == dst {
mat = mul
}
defer mul.destroy()
} else if dst == mat {
mat = NewMatrix(mat.m, mat.n)
copy(mat.dat, dst.dat)
defer mat.destroy()
}
dst = dst.Reshape(mat.m, mul.n)
for r1 := 0; r1 < mat.m; r1++ {
for c2 := 0; c2 < mul.n; c2++ {
dst.dat[c2*mat.m+r1] = 0
for i := 0; i < mat.n; i++ {
dst.dat[c2*mat.m+r1] += mat.dat[i*mat.m+r1] * mul.dat[c2*mul.m+i]
}
}
}
return dst
}
// Performs a scalar multiplication between mat and some constant c,
// storing the result in dst. Mat and dst can be equal. If dst is not the
// correct size, a Reshape will occur.
func (mat *MatMxN) Mul(dst *MatMxN, c float32) *MatMxN {
if mat == nil {
return nil
}
dst = dst.Reshape(mat.m, mat.n)
for i, el := range mat.dat {
dst.dat[i] = el * c
}
return dst
}
// Multiplies the matrix by a vector of size n. If mat or v is
// nil, this returns nil. If the number of columns in mat does not match
// the Size of v, this also returns nil.
//
// Dst will be resized if it's not big enough. If dst == v; a temporary
// vector will be allocated and returned via the realloc callback when complete.
func (mat *MatMxN) MulNx1(dst, v *VecN) *VecN {
if mat == nil || v == nil || mat.n != len(v.vec) {
return nil
}
if dst == v {
v = NewVecN(len(v.vec))
copy(v.vec, dst.vec)
defer v.destroy()
}
dst = dst.Resize(mat.m)
for r := 0; r < mat.m; r++ {
dst.vec[r] = 0
for c := 0; c < mat.n; c++ {
dst.vec[r] += mat.At(r, c) * v.vec[c]
}
}
return dst
}
func (mat *MatMxN) ApproxEqual(m2 *MatMxN) bool {
if mat == m2 {
return true
}
if mat.m != m2.m || mat.n != m2.n {
return false
}
for i, el := range mat.dat {
if !FloatEqual(el, m2.dat[i]) {
return false
}
}
return true
}
func (mat *MatMxN) ApproxEqualThreshold(m2 *MatMxN, epsilon float32) bool {
if mat == m2 {
return true
}
if mat.m != m2.m || mat.n != m2.n {
return false
}
for i, el := range mat.dat {
if !FloatEqualThreshold(el, m2.dat[i], epsilon) {
return false
}
}
return true
}
func (mat *MatMxN) ApproxEqualFunc(m2 *MatMxN, comp func(float32, float32) bool) bool {
if mat == m2 {
return true
}
if mat.m != m2.m || mat.n != m2.n {
return false
}
for i, el := range mat.dat {
if !comp(el, m2.dat[i]) {
return false
}
}
return true
}
type InferMatrixError struct{}
func (me InferMatrixError) Error() string {
return "could not infer matrix. Make sure you're using a constant matrix such as Mat3 from within the same package (meaning: mgl32.MatMxN can't handle a mgl64.Mat2x3)."
}
type RectangularMatrixError struct{}
func (mse RectangularMatrixError) Error() string {
return "the matrix was the wrong shape, needed a square matrix."
}
type NilMatrixError struct{}
func (me NilMatrixError) Error() string {
return "the matrix is nil"
}

2341
vendor/github.com/go-gl/mathgl/mgl32/matrix.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

370
vendor/github.com/go-gl/mathgl/mgl32/matrix.tmpl generated vendored Normal file
View File

@ -0,0 +1,370 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// <<.Comment>>
// Edit <<.TemplateName>> and run "go generate" to make changes.
package mgl32
import (
"bytes"
"fmt"
"golang.org/x/image/math/f32"
"text/tabwriter"
)
type Mat2 [4]float32
type Mat2x3 [6]float32
type Mat2x4 [8]float32
type Mat3x2 [6]float32
type Mat3 f32.Mat3
type Mat3x4 [12]float32
type Mat4x2 [8]float32
type Mat4x3 [12]float32
type Mat4 f32.Mat4
func (m Mat2) Mat3() Mat3 {
col0, col1 := m.Cols()
return Mat3FromCols(
col0.Vec3(0),
col1.Vec3(0),
Vec3{0, 0, 1},
)
}
func (m Mat2) Mat4() Mat4 {
col0, col1 := m.Cols()
return Mat4FromCols(
col0.Vec4(0, 0),
col1.Vec4(0, 0),
Vec4{0, 0, 1, 0},
Vec4{0, 0, 0, 1},
)
}
func (m Mat3) Mat2() Mat2 {
col0, col1, _ := m.Cols()
return Mat2FromCols(
col0.Vec2(),
col1.Vec2(),
)
}
func (m Mat3) Mat4() Mat4 {
col0, col1, col2 := m.Cols()
return Mat4FromCols(
col0.Vec4(0),
col1.Vec4(0),
col2.Vec4(0),
Vec4{0, 0, 0, 1},
)
}
func (m Mat4) Mat2() Mat2 {
col0, col1, _, _ := m.Cols()
return Mat2FromCols(
col0.Vec2(),
col1.Vec2(),
)
}
func (m Mat4) Mat3() Mat3 {
col0, col1, col2, _ := m.Cols()
return Mat3FromCols(
col0.Vec3(),
col1.Vec3(),
col2.Vec3(),
)
}
<</* Common functions for all matrices */>>
<<range $m := enum 2 3 4>><<range $n := enum 2 3 4>>
<<$type := typename $m $n>>
// Sets a Column within the Matrix, so it mutates the calling matrix.
func (m *<<$type>>) SetCol(col int, v <<typename $m 1>>) {
<<range $i := iter 0 $m>><<sep "," $i>>m[col*<<$m>>+<<$i>>]<<end>> = <<repeat $m "v[%d]" ",">>
}
// Sets a Row within the Matrix, so it mutates the calling matrix.
func (m *<<$type>>) SetRow(row int, v <<typename $n 1>>) {
<<range $i := iter 0 $n>><<sep "," $i>>m[row+<<mul $m $i>>]<<end>> = <<repeat $n "v[%d]" ",">>
}
<<if eq $m $n>>
// Diag is a basic operation on a square matrix that simply
// returns main diagonal (meaning all elements such that row==col).
func (m <<$type>>) Diag() <<typename $m 1>> {
return <<typename $m 1>>{<<range $i := iter 0 $m>>m[<<mul $i $m | add $i>>],<<end>>}
}
<<end>>
<<if eq $m $n>>
// Ident<<$m>> returns the <<$m>>x<<$n>> identity matrix.
// The identity matrix is a square matrix with the value 1 on its
// diagonals. The characteristic property of the identity matrix is that
// any matrix multiplied by it is itself. (MI = M; IN = N)
func Ident<<$m>>() <<$type>> {
return <<$type>>{<<range $i := matiter $m $n>><<if eq $i.M $i.N>>1<<else>>0<<end>>,<<end>>}
}
<<end>>
<<if eq $m $n>>
// Diag<<$m>> creates a diagonal matrix from the entries of the input vector.
// That is, for each pointer for row==col, vector[row] is the entry. Otherwise it's 0.
//
// Another way to think about it is that the identity is this function where the every vector element is 1.
func Diag<<$m>>(v <<typename $m 1>>) <<$type>> {
var m <<$type>>
<<range $i := iter 0 $m>><<sep "," $i>>m[<<mul $i $m | add $i>>]<<end>> = <<repeat $m "v[%d]" ",">>
return m
}
<<end>>
// <<$type>>FromRows builds a new matrix from row vectors.
// The resulting matrix will still be in column major order, but this can be
// good for hand-building matrices.
func <<$type>>FromRows(<<range $i := iter 0 $m>><<sep "," $i>>row<<$i>><<end>> <<typename $n 1>>) <<$type>> {
return <<$type>>{<<range $i := matiter $m $n>>row<<$i.M>>[<<$i.N>>],<<end>>}
}
// <<$type>>FromCols builds a new matrix from column vectors.
func <<$type>>FromCols(<<repeat $n "col%d" ",">> <<typename $m 1>>) <<$type>> {
return <<$type>>{<<range $i := matiter $m $n>>col<<$i.N>>[<<$i.M>>], <<end>>}
}
// Add performs an element-wise addition of two matrices, this is
// equivalent to iterating over every element of m1 and adding the corresponding value of m2.
func (m1 <<$type>>) Add(m2 <<$type>>) <<$type>> {
return <<$type>>{<< range $i := matiter $m $n>>m1[<<$i>>] + m2[<<$i>>], <<end>>}
}
// Sub performs an element-wise subtraction of two matrices, this is
// equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.
func (m1 <<$type>>) Sub(m2 <<$type>>) <<$type>> {
return <<$type>>{<< range $i := matiter $m $n>>m1[<<$i>>] - m2[<<$i>>], <<end>>}
}
// Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating
// over every element of the matrix and multiply it by c.
func (m1 <<$type>>) Mul(c float32) <<$type>> {
return <<$type>>{<< range $i := matiter $m $n>>m1[<<$i>>] * c, <<end>>}
}
<<range $o := enum 1 2 3 4>>
// Mul<<$n>><<if ne $n $o>>x<<$o>><<end>> performs a "matrix product" between this matrix
// and another of the given dimension. For any two matrices of dimensionality
// MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using
// Mul4x2 will result in a Mat4x2.
func (m1 <<$type>>) Mul<<$n>><<if ne $n $o>>x<<$o>><<end>>(m2 <<typename $n $o>>) <<typename $m $o>> {
return <<typename $m $o>>{<<range $i := matiter $m $o>>
<<range $k := iter 0 $n>><<sep "+" $k>>m1[<<mul $k $m | add $i.M>>]*m2[<<mul $i.N $n| add $k>>]<<end>>,<<end>>
}
}
<<end>>
// Transpose produces the transpose of this matrix. For any MxN matrix
// the transpose is an NxM matrix with the rows swapped with the columns. For instance
// the transpose of the Mat3x2 is a Mat2x3 like so:
//
// [[a b]] [[a c e]]
// [[c d]] = [[b d f]]
// [[e f]]
func (m1 <<$type>>) Transpose() <<typename $n $m>> {
return <<typename $n $m>>{<<range $i := matiter $n $m>>m1[<<mul $m $i.M | add $i.N>>], <<end>>}
}
<<if eq $m $n>>
// The determinant of a matrix is a measure of a square matrix's
// singularity and invertability, among other things. In this library, the
// determinant is hard coded based on pre-computed cofactor expansion, and uses
// no loops. Of course, the addition and multiplication must still be done.
func (m <<$type>>) Det() float32 {
<<if eq $m 2 ->>
return m[0]*m[3] - m[1]*m[2]
<<else if eq $m 3 ->>
return m[0]*m[4]*m[8] + m[3]*m[7]*m[2] + m[6]*m[1]*m[5] - m[6]*m[4]*m[2] - m[3]*m[1]*m[8] - m[0]*m[7]*m[5]
<<else if eq $m 4 ->>
return m[0]*m[5]*m[10]*m[15] - m[0]*m[5]*m[11]*m[14] - m[0]*m[6]*m[9]*m[15] + m[0]*m[6]*m[11]*m[13] + m[0]*m[7]*m[9]*m[14] - m[0]*m[7]*m[10]*m[13] - m[1]*m[4]*m[10]*m[15] + m[1]*m[4]*m[11]*m[14] + m[1]*m[6]*m[8]*m[15] - m[1]*m[6]*m[11]*m[12] - m[1]*m[7]*m[8]*m[14] + m[1]*m[7]*m[10]*m[12] + m[2]*m[4]*m[9]*m[15] - m[2]*m[4]*m[11]*m[13] - m[2]*m[5]*m[8]*m[15] + m[2]*m[5]*m[11]*m[12] + m[2]*m[7]*m[8]*m[13] - m[2]*m[7]*m[9]*m[12] - m[3]*m[4]*m[9]*m[14] + m[3]*m[4]*m[10]*m[13] + m[3]*m[5]*m[8]*m[14] - m[3]*m[5]*m[10]*m[12] - m[3]*m[6]*m[8]*m[13] + m[3]*m[6]*m[9]*m[12]
<<end ->>
}
<<end>>
<<if eq $m $n>>
// Inv computes the inverse of a square matrix. An inverse is a square matrix such that when multiplied by the
// original, yields the identity.
//
// M_inv * M = M * M_inv = I
//
// In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling
// are still done. This can still be (relatively) expensive for a 4x4.
//
// This function checks the determinant to see if the matrix is invertible.
// If the determinant is 0.0, this function returns the zero matrix. However, due to floating point errors, it is
// entirely plausible to get a false positive or negative.
// In the future, an alternate function may be written which takes in a pre-computed determinant.
func (m <<$type>>) Inv() <<$type>> {
det := m.Det()
if FloatEqual(det, float32(0.0)) {
return <<$type>>{}
}
<<if eq $m 2>>
retMat := Mat2{m[3], -m[1], -m[2], m[0]}
<<else if eq $m 3>>
retMat := Mat3{
m[4]*m[8] - m[5]*m[7],
m[2]*m[7] - m[1]*m[8],
m[1]*m[5] - m[2]*m[4],
m[5]*m[6] - m[3]*m[8],
m[0]*m[8] - m[2]*m[6],
m[2]*m[3] - m[0]*m[5],
m[3]*m[7] - m[4]*m[6],
m[1]*m[6] - m[0]*m[7],
m[0]*m[4] - m[1]*m[3],
}
<<else if eq $m 4>>
retMat := Mat4{
-m[7]*m[10]*m[13] + m[6]*m[11]*m[13] + m[7]*m[9]*m[14] - m[5]*m[11]*m[14] - m[6]*m[9]*m[15] + m[5]*m[10]*m[15],
m[3]*m[10]*m[13] - m[2]*m[11]*m[13] - m[3]*m[9]*m[14] + m[1]*m[11]*m[14] + m[2]*m[9]*m[15] - m[1]*m[10]*m[15],
-m[3]*m[6]*m[13] + m[2]*m[7]*m[13] + m[3]*m[5]*m[14] - m[1]*m[7]*m[14] - m[2]*m[5]*m[15] + m[1]*m[6]*m[15],
m[3]*m[6]*m[9] - m[2]*m[7]*m[9] - m[3]*m[5]*m[10] + m[1]*m[7]*m[10] + m[2]*m[5]*m[11] - m[1]*m[6]*m[11],
m[7]*m[10]*m[12] - m[6]*m[11]*m[12] - m[7]*m[8]*m[14] + m[4]*m[11]*m[14] + m[6]*m[8]*m[15] - m[4]*m[10]*m[15],
-m[3]*m[10]*m[12] + m[2]*m[11]*m[12] + m[3]*m[8]*m[14] - m[0]*m[11]*m[14] - m[2]*m[8]*m[15] + m[0]*m[10]*m[15],
m[3]*m[6]*m[12] - m[2]*m[7]*m[12] - m[3]*m[4]*m[14] + m[0]*m[7]*m[14] + m[2]*m[4]*m[15] - m[0]*m[6]*m[15],
-m[3]*m[6]*m[8] + m[2]*m[7]*m[8] + m[3]*m[4]*m[10] - m[0]*m[7]*m[10] - m[2]*m[4]*m[11] + m[0]*m[6]*m[11],
-m[7]*m[9]*m[12] + m[5]*m[11]*m[12] + m[7]*m[8]*m[13] - m[4]*m[11]*m[13] - m[5]*m[8]*m[15] + m[4]*m[9]*m[15],
m[3]*m[9]*m[12] - m[1]*m[11]*m[12] - m[3]*m[8]*m[13] + m[0]*m[11]*m[13] + m[1]*m[8]*m[15] - m[0]*m[9]*m[15],
-m[3]*m[5]*m[12] + m[1]*m[7]*m[12] + m[3]*m[4]*m[13] - m[0]*m[7]*m[13] - m[1]*m[4]*m[15] + m[0]*m[5]*m[15],
m[3]*m[5]*m[8] - m[1]*m[7]*m[8] - m[3]*m[4]*m[9] + m[0]*m[7]*m[9] + m[1]*m[4]*m[11] - m[0]*m[5]*m[11],
m[6]*m[9]*m[12] - m[5]*m[10]*m[12] - m[6]*m[8]*m[13] + m[4]*m[10]*m[13] + m[5]*m[8]*m[14] - m[4]*m[9]*m[14],
-m[2]*m[9]*m[12] + m[1]*m[10]*m[12] + m[2]*m[8]*m[13] - m[0]*m[10]*m[13] - m[1]*m[8]*m[14] + m[0]*m[9]*m[14],
m[2]*m[5]*m[12] - m[1]*m[6]*m[12] - m[2]*m[4]*m[13] + m[0]*m[6]*m[13] + m[1]*m[4]*m[14] - m[0]*m[5]*m[14],
-m[2]*m[5]*m[8] + m[1]*m[6]*m[8] + m[2]*m[4]*m[9] - m[0]*m[6]*m[9] - m[1]*m[4]*m[10] + m[0]*m[5]*m[10],
}
<<end>>
return retMat.Mul(1 / det)
}
<<end>>
// ApproxEqual performs an element-wise approximate equality test between two matrices,
// as if FloatEqual had been used.
func (m1 <<$type>>) ApproxEqual(m2 <<$type>>) bool {
for i := range m1 {
if !FloatEqual(m1[i], m2[i]) {
return false
}
}
return true
}
// ApproxEqualThreshold performs an element-wise approximate equality test between two matrices
// with a given epsilon threshold, as if FloatEqualThreshold had been used.
func (m1 <<$type>>) ApproxEqualThreshold(m2 <<$type>>, threshold float32) bool {
for i := range m1 {
if !FloatEqualThreshold(m1[i], m2[i], threshold) {
return false
}
}
return true
}
// ApproxEqualFunc performs an element-wise approximate equality test between two matrices
// with a given equality functions, intended to be used with FloatEqualFunc; although and comparison
// function may be used in practice.
func (m1 <<$type>>) ApproxFuncEqual(m2 <<$type>>, eq func(float32, float32) bool) bool {
for i := range m1 {
if !eq(m1[i], m2[i]) {
return false
}
}
return true
}
// At returns the matrix element at the given row and column.
// This is equivalent to mat[col * numRow + row] where numRow is constant
// (E.G. for a Mat3x2 it's equal to 3)
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// At(5,0) will work just like At(1,1). Or it may panic if it's out of bounds.
func (m <<$type>>) At(row, col int) float32 {
return m[col*<<$m>>+row]
}
// Set sets the corresponding matrix element at the given row and column.
// This has a pointer receiver because it mutates the matrix.
//
// This method is garbage-in garbage-out. For instance, on a Mat4 asking for
// Set(5,0,val) will work just like Set(1,1,val). Or it may panic if it's out of bounds.
func (m *<<$type>>) Set(row, col int, value float32) {
m[col*<<$m>>+row] = value
}
// Index returns the index of the given row and column, to be used with direct
// access. E.G. Index(0,0) = 0.
//
// This is a garbage-in garbage-out method. For instance, on a Mat4 asking for the index of
// (5,0) will work the same as asking for (1,1). Or it may give you a value that will cause
// a panic if you try to access the array with it if it's truly out of bounds.
func (m <<$type>>) Index(row, col int) int {
return col*<<$m>> + row
}
// Row returns a vector representing the corresponding row (starting at row 0).
// This package makes no distinction between row and column vectors, so it
// will be a normal VecM for a MxN matrix.
func (m <<$type>>) Row(row int) <<typename $n 1>> {
return <<typename $n 1>>{<<range $i := iter 0 $n>>m[row+<<mul $m $i>>],<<end>>}
}
// Rows decomposes a matrix into its corresponding row vectors.
// This is equivalent to calling mat.Row for each row.
func (m <<$type>>) Rows() (<<repeat $m "row%d" ",">> <<typename $n 1>>) {
return <<repeat $m "m.Row(%d)" ",">>
}
// Col returns a vector representing the corresponding column (starting at col 0).
// This package makes no distinction between row and column vectors, so it
// will be a normal VecN for a MxN matrix.
func (m <<$type>>) Col(col int) <<typename $m 1>> {
return <<typename $m 1>>{<<range $i := iter 0 $m>>m[col*<<$m>>+<<$i>>],<<end>>}
}
// Cols decomposes a matrix into its corresponding column vectors.
// This is equivalent to calling mat.Col for each column.
func (m <<$type>>) Cols() (<<repeat $n "col%d" ",">> <<typename $m 1>>) {
return <<repeat $n "m.Col(%d)" ",">>
}
<<if eq $m $n>>
// Trace is a basic operation on a square matrix that simply
// sums up all elements on the main diagonal (meaning all elements such that row==col).
func (m <<$type>>) Trace() float32 {
return <<range $i := iter 0 $m>><<sep "+" $i>> m[<<mul $i $m | add $i>>]<<end>>
}
<<end>>
// Abs returns the element-wise absolute value of this matrix
func (m <<$type>>) Abs() <<$type>> {
return <<$type>>{<<repeat (mul $m $n) "Abs(m[%d])" ",">>}
}
// Pretty prints the matrix
func (m <<$type>>) String() string {
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 4, 4, 1, ' ', tabwriter.AlignRight)
for i := 0; i < <<$m>>; i++ {
for _, col := range m.Row(i) {
fmt.Fprintf(w, "%f\t", col)
}
fmt.Fprintln(w, "")
}
w.Flush()
return buf.String()
}
<<end>><<end>> <</* range $m range $n */>>

121
vendor/github.com/go-gl/mathgl/mgl32/mempool.go generated vendored Normal file
View File

@ -0,0 +1,121 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"sync"
)
var (
slicePools []*sync.Pool
listLock sync.RWMutex
)
var shouldPool = true
func DisableMemoryPooling() {
shouldPool = false
}
// Returns the given memory pool. If the pool doesn't exist, it will
// create all pools up to element i. The number "i" corresponds to "p"
// in most other comments. That is, it's Ceil(log_2(whatever)). So i=0
// means you'll get the pool for slices of size 1, i=1 for size 2, i=2 for size 4,
// and so on.
//
// This is concurrency safe and uses an RWMutex to protect the list expansion.
func getPool(i int) *sync.Pool {
listLock.RLock()
if i >= len(slicePools) {
// Promote to a write lock because we now
// need to mutate the pool
listLock.RUnlock()
listLock.Lock()
defer listLock.Unlock()
for n := i - len(slicePools); n >= 0; n-- {
newFunc := genPoolNew(1 << uint(len(slicePools)))
slicePools = append(slicePools, &sync.Pool{New: newFunc})
}
} else {
defer listLock.RUnlock()
}
return slicePools[i]
}
func genPoolNew(i int) func() interface{} {
return func() interface{} {
return make([]float32, 0, i)
}
}
// Grabs a slice from the memory pool, such that its cap
// is 2^p where p is Ceil(log_2(size)). It will be downsliced
// such that the len is size.
func grabFromPool(size int) []float32 {
pool, exact := binLog(size)
// Tried to grab something of size
// zero or less
if pool == -1 {
return nil
}
// If the log is not exact, we
// need to "overallocate" so we have
// log+1
if !exact {
pool++
}
slice := getPool(pool).Get().([]float32)
slice = slice[:size]
return slice
}
// Returns a slice to the appropriate pool. If the slice does not have a cap that's precisely
// a power of 2, this will panic.
func returnToPool(slice []float32) {
if cap(slice) == 0 {
return
}
pool, exact := binLog(cap(slice))
if !exact {
panic("attempt to pool slice with non-exact cap. If you're a user, please file an issue with github.com/go-gl/mathgl about this bug. This should never happen.")
}
getPool(pool).Put(slice)
}
// This returns the integer base 2 log of the value
// and whether the log is exact or rounded down.
//
// This is only for positive integers.
//
// There are faster ways to do this, I'm open to suggestions. Most rely on knowing system endianness
// which Go makes hard to do. I'm hesistant to use float conversions and the math package because of off-by-one errors.
func binLog(val int) (int, bool) {
if val <= 0 {
return -1, false
}
exact := true
l := 0
for ; val > 1; val = val >> 1 {
// If the current lsb is 1 and the number
// is not equal to 1, this is not an exact
// log, but rather a rounding of it
if val&1 != 0 {
exact = false
}
l++
}
return l, exact
}

98
vendor/github.com/go-gl/mathgl/mgl32/project.go generated vendored Normal file
View File

@ -0,0 +1,98 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"errors"
"math"
)
func Ortho(left, right, bottom, top, near, far float32) Mat4 {
rml, tmb, fmn := (right - left), (top - bottom), (far - near)
return Mat4{float32(2. / rml), 0, 0, 0, 0, float32(2. / tmb), 0, 0, 0, 0, float32(-2. / fmn), 0, float32(-(right + left) / rml), float32(-(top + bottom) / tmb), float32(-(far + near) / fmn), 1}
}
// Equivalent to Ortho with the near and far planes being -1 and 1, respectively
func Ortho2D(left, right, bottom, top float32) Mat4 {
return Ortho(left, right, bottom, top, -1, 1)
}
func Perspective(fovy, aspect, near, far float32) Mat4 {
// fovy = (fovy * math.Pi) / 180.0 // convert from degrees to radians
nmf, f := near-far, float32(1./math.Tan(float64(fovy)/2.0))
return Mat4{float32(f / aspect), 0, 0, 0, 0, float32(f), 0, 0, 0, 0, float32((near + far) / nmf), -1, 0, 0, float32((2. * far * near) / nmf), 0}
}
func Frustum(left, right, bottom, top, near, far float32) Mat4 {
rml, tmb, fmn := (right - left), (top - bottom), (far - near)
A, B, C, D := (right+left)/rml, (top+bottom)/tmb, -(far+near)/fmn, -(2*far*near)/fmn
return Mat4{float32((2. * near) / rml), 0, 0, 0, 0, float32((2. * near) / tmb), 0, 0, float32(A), float32(B), float32(C), -1, 0, 0, float32(D), 0}
}
func LookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ float32) Mat4 {
return LookAtV(Vec3{eyeX, eyeY, eyeZ}, Vec3{centerX, centerY, centerZ}, Vec3{upX, upY, upZ})
}
// LookAtV generates a transform matrix from world space into the specific eye space
func LookAtV(eye, center, up Vec3) Mat4 {
f := center.Sub(eye).Normalize()
s := f.Cross(up.Normalize()).Normalize()
u := s.Cross(f)
M := Mat4{
s[0], u[0], -f[0], 0,
s[1], u[1], -f[1], 0,
s[2], u[2], -f[2], 0,
0, 0, 0, 1,
}
return M.Mul4(Translate3D(float32(-eye[0]), float32(-eye[1]), float32(-eye[2])))
}
// Transform a set of coordinates from object space (in obj) to window coordinates (with depth)
//
// Window coordinates are continuous, not discrete (well, as continuous as an IEEE Floating Point can be), so you won't get exact pixel locations
// without rounding or similar
func Project(obj Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (win Vec3) {
obj4 := obj.Vec4(1)
vpp := projection.Mul4(modelview).Mul4x1(obj4)
vpp = vpp.Mul(1 / vpp.W())
win[0] = float32(initialX) + (float32(width)*(vpp[0]+1))/2
win[1] = float32(initialY) + (float32(height)*(vpp[1]+1))/2
win[2] = (vpp[2] + 1) / 2
return win
}
// Transform a set of window coordinates to object space. If your MVP (projection.Mul(modelview) matrix is not invertible, this will return an error
//
// Note that the projection may not be perfect if you use strict pixel locations rather than the exact values given by Projectf.
// (It's still unlikely to be perfect due to precision errors, but it will be closer)
func UnProject(win Vec3, modelview, projection Mat4, initialX, initialY, width, height int) (obj Vec3, err error) {
inv := projection.Mul4(modelview).Inv()
var blank Mat4
if inv == blank {
return Vec3{}, errors.New("Could not find matrix inverse (projection times modelview is probably non-singular)")
}
obj4 := inv.Mul4x1(Vec4{
(2 * (win[0] - float32(initialX)) / float32(width)) - 1,
(2 * (win[1] - float32(initialY)) / float32(height)) - 1,
2*win[2] - 1,
1.0,
})
obj = obj4.Vec3()
//if obj4[3] > MinValue {}
obj[0] /= obj4[3]
obj[1] /= obj4[3]
obj[2] /= obj4[3]
return obj, nil
}

458
vendor/github.com/go-gl/mathgl/mgl32/quat.go generated vendored Normal file
View File

@ -0,0 +1,458 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// A rotation order is the order in which
// rotations will be transformed for the purposes of AnglesToQuat
type RotationOrder int
const (
XYX RotationOrder = iota
XYZ
XZX
XZY
YXY
YXZ
YZY
YZX
ZYZ
ZYX
ZXZ
ZXY
)
// A Quaternion is an extension of the imaginary numbers; there's all sorts of
// interesting theory behind it. In 3D graphics we mostly use it as a cheap way of
// representing rotation since quaternions are cheaper to multiply by, and easier to
// interpolate than matrices.
//
// A Quaternion has two parts: W, the so-called scalar component,
// and "V", the vector component. The vector component is considered to
// be the part in 3D space, while W (loosely interpreted) is its 4D coordinate.
type Quat struct {
W float32
V Vec3
}
// The quaternion identity: W=1; V=(0,0,0).
//
// As with all identities, multiplying any quaternion by this will yield the same
// quaternion you started with.
func QuatIdent() Quat {
return Quat{1., Vec3{0, 0, 0}}
}
// Creates an angle from an axis and an angle relative to that axis.
//
// This is cheaper than HomogRotate3D.
func QuatRotate(angle float32, axis Vec3) Quat {
// angle = (float32(math.Pi) * angle) / 180.0
c, s := float32(math.Cos(float64(angle/2))), float32(math.Sin(float64(angle/2)))
return Quat{c, axis.Mul(s)}
}
// A convenient alias for q.V[0]
func (q Quat) X() float32 {
return q.V[0]
}
// A convenient alias for q.V[1]
func (q Quat) Y() float32 {
return q.V[1]
}
// A convenient alias for q.V[2]
func (q Quat) Z() float32 {
return q.V[2]
}
// Adds two quaternions. It's no more complicated than
// adding their W and V components.
func (q1 Quat) Add(q2 Quat) Quat {
return Quat{q1.W + q2.W, q1.V.Add(q2.V)}
}
// Subtracts two quaternions. It's no more complicated than
// subtracting their W and V components.
func (q1 Quat) Sub(q2 Quat) Quat {
return Quat{q1.W - q2.W, q1.V.Sub(q2.V)}
}
// Multiplies two quaternions. This can be seen as a rotation. Note that
// Multiplication is NOT commutative, meaning q1.Mul(q2) does not necessarily
// equal q2.Mul(q1).
func (q1 Quat) Mul(q2 Quat) Quat {
return Quat{q1.W*q2.W - q1.V.Dot(q2.V), q1.V.Cross(q2.V).Add(q2.V.Mul(q1.W)).Add(q1.V.Mul(q2.W))}
}
// Scales every element of the quaternion by some constant factor.
func (q1 Quat) Scale(c float32) Quat {
return Quat{q1.W * c, Vec3{q1.V[0] * c, q1.V[1] * c, q1.V[2] * c}}
}
// Returns the conjugate of a quaternion. Equivalent to
// Quat{q1.W, q1.V.Mul(-1)}
func (q1 Quat) Conjugate() Quat {
return Quat{q1.W, q1.V.Mul(-1)}
}
// Returns the Length of the quaternion, also known as its Norm. This is the same thing as
// the Len of a Vec4
func (q1 Quat) Len() float32 {
return float32(math.Sqrt(float64(q1.W*q1.W + q1.V[0]*q1.V[0] + q1.V[1]*q1.V[1] + q1.V[2]*q1.V[2])))
}
// Norm() is an alias for Len() since both are very common terms.
func (q1 Quat) Norm() float32 {
return q1.Len()
}
// Normalizes the quaternion, returning its versor (unit quaternion).
//
// This is the same as normalizing it as a Vec4.
func (q1 Quat) Normalize() Quat {
length := q1.Len()
if FloatEqual(1, length) {
return q1
}
if length == 0 {
return QuatIdent()
}
if length == InfPos {
length = MaxValue
}
return Quat{q1.W * 1 / length, q1.V.Mul(1 / length)}
}
// The inverse of a quaternion. The inverse is equivalent
// to the conjugate divided by the square of the length.
//
// This method computes the square norm by directly adding the sum
// of the squares of all terms instead of actually squaring q1.Len(),
// both for performance and precision.
func (q1 Quat) Inverse() Quat {
return q1.Conjugate().Scale(1 / q1.Dot(q1))
}
// Rotates a vector by the rotation this quaternion represents.
// This will result in a 3D vector. Strictly speaking, this is
// equivalent to q1.v.q* where the "."" is quaternion multiplication and v is interpreted
// as a quaternion with W 0 and V v. In code:
// q1.Mul(Quat{0,v}).Mul(q1.Conjugate()), and
// then retrieving the imaginary (vector) part.
//
// In practice, we hand-compute this in the general case and simplify
// to save a few operations.
func (q1 Quat) Rotate(v Vec3) Vec3 {
cross := q1.V.Cross(v)
// v + 2q_w * (q_v x v) + 2q_v x (q_v x v)
return v.Add(cross.Mul(2 * q1.W)).Add(q1.V.Mul(2).Cross(cross))
}
// Returns the homogeneous 3D rotation matrix corresponding to the quaternion.
func (q1 Quat) Mat4() Mat4 {
w, x, y, z := q1.W, q1.V[0], q1.V[1], q1.V[2]
return Mat4{
1 - 2*y*y - 2*z*z, 2*x*y + 2*w*z, 2*x*z - 2*w*y, 0,
2*x*y - 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z + 2*w*x, 0,
2*x*z + 2*w*y, 2*y*z - 2*w*x, 1 - 2*x*x - 2*y*y, 0,
0, 0, 0, 1,
}
}
// The dot product between two quaternions, equivalent to if this was a Vec4
func (q1 Quat) Dot(q2 Quat) float32 {
return q1.W*q2.W + q1.V[0]*q2.V[0] + q1.V[1]*q2.V[1] + q1.V[2]*q2.V[2]
}
// Returns whether the quaternions are approximately equal, as if
// FloatEqual was called on each matching element
func (q1 Quat) ApproxEqual(q2 Quat) bool {
return FloatEqual(q1.W, q2.W) && q1.V.ApproxEqual(q2.V)
}
// Returns whether the quaternions are approximately equal with a given tolerence, as if
// FloatEqualThreshold was called on each matching element with the given epsilon
func (q1 Quat) ApproxEqualThreshold(q2 Quat, epsilon float32) bool {
return FloatEqualThreshold(q1.W, q2.W, epsilon) && q1.V.ApproxEqualThreshold(q2.V, epsilon)
}
// Returns whether the quaternions are approximately equal using the given comparison function, as if
// the function had been called on each individual element
func (q1 Quat) ApproxEqualFunc(q2 Quat, f func(float32, float32) bool) bool {
return f(q1.W, q2.W) && q1.V.ApproxFuncEqual(q2.V, f)
}
// Returns whether the quaternions represents the same orientation
//
// Different values can represent the same orientation (q == -q) because quaternions avoid singularities
// and discontinuities involved with rotation in 3 dimensions by adding extra dimensions
func (q1 Quat) OrientationEqual(q2 Quat) bool {
return q1.OrientationEqualThreshold(q2, Epsilon)
}
// Returns whether the quaternions represents the same orientation with a given tolerence
func (q1 Quat) OrientationEqualThreshold(q2 Quat, epsilon float32) bool {
return Abs(q1.Normalize().Dot(q2.Normalize())) > 1-epsilon
}
// Slerp is *S*pherical *L*inear Int*erp*olation, a method of interpolating
// between two quaternions. This always takes the straightest path on the sphere between
// the two quaternions, and maintains constant velocity.
//
// However, it's expensive and QuatSlerp(q1,q2) is not the same as QuatSlerp(q2,q1)
func QuatSlerp(q1, q2 Quat, amount float32) Quat {
q1, q2 = q1.Normalize(), q2.Normalize()
dot := q1.Dot(q2)
// If the inputs are too close for comfort, linearly interpolate and normalize the result.
if dot > 0.9995 {
return QuatNlerp(q1, q2, amount)
}
// This is here for precision errors, I'm perfectly aware that *technically* the dot is bound [-1,1], but since Acos will freak out if it's not (even if it's just a liiiiitle bit over due to normal error) we need to clamp it
dot = Clamp(dot, -1, 1)
theta := float32(math.Acos(float64(dot))) * amount
c, s := float32(math.Cos(float64(theta))), float32(math.Sin(float64(theta)))
rel := q2.Sub(q1.Scale(dot)).Normalize()
return q1.Scale(c).Add(rel.Scale(s))
}
// *L*inear Int*erp*olation between two Quaternions, cheap and simple.
//
// Not excessively useful, but uses can be found.
func QuatLerp(q1, q2 Quat, amount float32) Quat {
return q1.Add(q2.Sub(q1).Scale(amount))
}
// *Normalized* *L*inear Int*erp*olation between two Quaternions. Cheaper than Slerp
// and usually just as good. This is literally Lerp with Normalize() called on it.
//
// Unlike Slerp, constant velocity isn't maintained, but it's much faster and
// Nlerp(q1,q2) and Nlerp(q2,q1) return the same path. You should probably
// use this more often unless you're suffering from choppiness due to the
// non-constant velocity problem.
func QuatNlerp(q1, q2 Quat, amount float32) Quat {
return QuatLerp(q1, q2, amount).Normalize()
}
// Performs a rotation in the specified order. If the order is not
// a valid RotationOrder, this function will panic
//
// The rotation "order" is more of an axis descriptor. For instance XZX would
// tell the function to interpret angle1 as a rotation about the X axis, angle2 about
// the Z axis, and angle3 about the X axis again.
//
// Based off the code for the Matlab function "angle2quat", though this implementation
// only supports 3 single angles as opposed to multiple angles.
func AnglesToQuat(angle1, angle2, angle3 float32, order RotationOrder) Quat {
var s [3]float64
var c [3]float64
s[0], c[0] = math.Sincos(float64(angle1 / 2))
s[1], c[1] = math.Sincos(float64(angle2 / 2))
s[2], c[2] = math.Sincos(float64(angle3 / 2))
ret := Quat{}
switch order {
case ZYX:
ret.W = float32(c[0]*c[1]*c[2] + s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*c[1]*s[2]),
float32(s[0]*c[1]*c[2] - c[0]*s[1]*s[2]),
}
case ZYZ:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*c[1]*c[2] + c[0]*c[1]*s[2]),
}
case ZXY:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] - s[0]*c[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*s[1]*c[2]),
float32(c[0]*s[1]*s[2] + s[0]*c[1]*c[2]),
}
case ZXZ:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*s[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
}
case YXZ:
ret.W = float32(c[0]*c[1]*c[2] + s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] + s[0]*c[1]*s[2]),
float32(s[0]*c[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] - s[0]*s[1]*c[2]),
}
case YXY:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*c[1]*c[2] + c[0]*c[1]*s[2]),
float32(c[0]*s[1]*s[2] - s[0]*s[1]*c[2]),
}
case YZX:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] + s[0]*s[1]*c[2]),
float32(c[0]*s[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] - s[0]*c[1]*s[2]),
}
case YZY:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(s[0]*s[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
}
case XYZ:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*s[1]*s[2])
ret.V = Vec3{float32(c[0]*s[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] - s[0]*c[1]*s[2]),
float32(c[0]*c[1]*s[2] + s[0]*s[1]*c[2]),
}
case XYX:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
float32(s[0]*s[1]*c[2] - c[0]*s[1]*s[2]),
}
case XZY:
ret.W = float32(c[0]*c[1]*c[2] + s[0]*s[1]*s[2])
ret.V = Vec3{float32(s[0]*c[1]*c[2] - c[0]*s[1]*s[2]),
float32(c[0]*c[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*c[1]*s[2]),
}
case XZX:
ret.W = float32(c[0]*c[1]*c[2] - s[0]*c[1]*s[2])
ret.V = Vec3{float32(c[0]*c[1]*s[2] + s[0]*c[1]*c[2]),
float32(c[0]*s[1]*s[2] - s[0]*s[1]*c[2]),
float32(c[0]*s[1]*c[2] + s[0]*s[1]*s[2]),
}
default:
panic("Unsupported rotation order")
}
return ret
}
// Mat4ToQuat converts a pure rotation matrix into a quaternion
func Mat4ToQuat(m Mat4) Quat {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
if tr := m[0] + m[5] + m[10]; tr > 0 {
s := float32(0.5 / math.Sqrt(float64(tr+1.0)))
return Quat{
0.25 / s,
Vec3{
(m[6] - m[9]) * s,
(m[8] - m[2]) * s,
(m[1] - m[4]) * s,
},
}
}
if (m[0] > m[5]) && (m[0] > m[10]) {
s := float32(2.0 * math.Sqrt(float64(1.0+m[0]-m[5]-m[10])))
return Quat{
(m[6] - m[9]) / s,
Vec3{
0.25 * s,
(m[4] + m[1]) / s,
(m[8] + m[2]) / s,
},
}
}
if m[5] > m[10] {
s := float32(2.0 * math.Sqrt(float64(1.0+m[5]-m[0]-m[10])))
return Quat{
(m[8] - m[2]) / s,
Vec3{
(m[4] + m[1]) / s,
0.25 * s,
(m[9] + m[6]) / s,
},
}
}
s := float32(2.0 * math.Sqrt(float64(1.0+m[10]-m[0]-m[5])))
return Quat{
(m[1] - m[4]) / s,
Vec3{
(m[8] + m[2]) / s,
(m[9] + m[6]) / s,
0.25 * s,
},
}
}
// QuatLookAtV creates a rotation from an eye vector to a center vector
//
// It assumes the front of the rotated object at Z- and up at Y+
func QuatLookAtV(eye, center, up Vec3) Quat {
// http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/#I_need_an_equivalent_of_gluLookAt__How_do_I_orient_an_object_towards_a_point__
// https://bitbucket.org/sinbad/ogre/src/d2ef494c4a2f5d6e2f0f17d3bfb9fd936d5423bb/OgreMain/src/OgreCamera.cpp?at=default#cl-161
direction := center.Sub(eye).Normalize()
// Find the rotation between the front of the object (that we assume towards Z-,
// but this depends on your model) and the desired direction
rotDir := QuatBetweenVectors(Vec3{0, 0, -1}, direction)
// Recompute up so that it's perpendicular to the direction
// You can skip that part if you really want to force up
//right := direction.Cross(up)
//up = right.Cross(direction)
// Because of the 1rst rotation, the up is probably completely screwed up.
// Find the rotation between the "up" of the rotated object, and the desired up
upCur := rotDir.Rotate(Vec3{0, 1, 0})
rotUp := QuatBetweenVectors(upCur, up)
rotTarget := rotUp.Mul(rotDir) // remember, in reverse order.
return rotTarget.Inverse() // camera rotation should be inversed!
}
// QuatBetweenVectors calculates the rotation between two vectors
func QuatBetweenVectors(start, dest Vec3) Quat {
// http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-17-quaternions/#I_need_an_equivalent_of_gluLookAt__How_do_I_orient_an_object_towards_a_point__
// https://github.com/g-truc/glm/blob/0.9.5/glm/gtx/quaternion.inl#L225
// https://bitbucket.org/sinbad/ogre/src/d2ef494c4a2f5d6e2f0f17d3bfb9fd936d5423bb/OgreMain/include/OgreVector3.h?at=default#cl-654
start = start.Normalize()
dest = dest.Normalize()
epsilon := float32(0.001)
cosTheta := start.Dot(dest)
if cosTheta < -1.0+epsilon {
// special case when vectors in opposite directions:
// there is no "ideal" rotation axis
// So guess one; any will do as long as it's perpendicular to start
axis := Vec3{1, 0, 0}.Cross(start)
if axis.Dot(axis) < epsilon {
// bad luck, they were parallel, try again!
axis = Vec3{0, 1, 0}.Cross(start)
}
return QuatRotate(math.Pi, axis.Normalize())
}
axis := start.Cross(dest)
s := float32(math.Sqrt(float64(1.0+cosTheta) * 2.0))
return Quat{
s * 0.5,
axis.Mul(1.0 / s),
}
}

306
vendor/github.com/go-gl/mathgl/mgl32/shapes.go generated vendored Normal file
View File

@ -0,0 +1,306 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"fmt"
"math"
)
// Generates a circle centered at (0,0) with a given radius.
// The radii are assumed to be in GL's coordinate sizing.
//
// Technically this draws an ellipse with two axes that match with the X and Y axes, the reason it has a radiusX and radiusY is because GL's coordinate system
// is proportional to screen width and screen height. So if you have a non-square viewport, a single radius will appear
// to "squash" the circle in one direction (usually the Y direction), so the X and Y radius allow for a circle to be made.
// A good way to get the correct radii is with mathgl.ScreenToGLCoords(radius, radius, screenWidth, screenHeight) which will get you the correct
// proportional GL coords.
//
// The numSlices argument specifies how many triangles you want your circle divided into, setting this
// number to too low a value may cause problem (and too high will cause it to take a lot of memory and time to compute
// without much gain in resolution).
//
// This uses discrete triangles, not a triangle fan
func Circle(radiusX, radiusY float32, numSlices int) []Vec2 {
twoPi := float32(2.0 * math.Pi)
circlePoints := make([]Vec2, 0, numSlices*3)
center := Vec2{0.0, 0.0}
previous := Vec2{radiusX, 0.0}
for theta := twoPi / float32(numSlices); !FloatEqual(theta, twoPi); theta = Clamp(theta+twoPi/float32(numSlices), 0.0, twoPi) {
sin, cos := math.Sincos(float64(theta))
curr := Vec2{float32(cos) * radiusX, float32(sin) * radiusY}
circlePoints = append(circlePoints, center, previous, curr)
previous = curr
}
// Now add the final point at theta=2pi
circlePoints = append(circlePoints, center, previous, Vec2{radiusX, 0.0})
return circlePoints
}
// Generates a 2-triangle rectangle for use with GL_TRIANGLES. The width and height should use GL's proportions (that is, where a width of 1.0
// is equivalent to half of the width of the render target); however, the y-coordinates grow downwards, not upwards. That is, it
// assumes you want the origin of the rectangle with the top-left corner at (0.0,0.0).
//
// Keep in mind that GL's coordinate system is proportional, so width=height will not result in a square unless your viewport is square.
// If you want to maintain proportionality regardless of screen size, use the results of w,h := ScreenToGLCoordsf(absoluteWidth, absoluteHeight, screenWidth, screenHeight);
// w,h=w+1,h-1 in the call to this function. (The w+1,h-1 step maps the coordinates to start at 0.0 rather than -1.0)
func Rect(width, height float32) []Vec2 {
return []Vec2{
{0.0, 0.0},
{0.0, -height},
{width, -height},
{0.0, 0.0},
{width, -height},
{width, 0.0},
}
}
func QuadraticBezierCurve2D(t float32, cPoint1, cPoint2, cPoint3 Vec2) Vec2 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1.0 - t) * (1.0 - t)).Add(cPoint2.Mul(2 * (1 - t) * t)).Add(cPoint3.Mul(t * t))
}
func QuadraticBezierCurve3D(t float32, cPoint1, cPoint2, cPoint3 Vec3) Vec3 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1.0 - t) * (1.0 - t)).Add(cPoint2.Mul(2 * (1 - t) * t)).Add(cPoint3.Mul(t * t))
}
func CubicBezierCurve2D(t float32, cPoint1, cPoint2, cPoint3, cPoint4 Vec2) Vec2 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1 - t) * (1 - t) * (1 - t)).Add(cPoint2.Mul(3 * (1 - t) * (1 - t) * t)).Add(cPoint3.Mul(3 * (1 - t) * t * t)).Add(cPoint4.Mul(t * t * t))
}
func CubicBezierCurve3D(t float32, cPoint1, cPoint2, cPoint3, cPoint4 Vec3) Vec3 {
if t < 0.0 || t > 1.0 {
panic("Can't interpolate on bezier curve with t out of range [0.0,1.0]")
}
return cPoint1.Mul((1 - t) * (1 - t) * (1 - t)).Add(cPoint2.Mul(3 * (1 - t) * (1 - t) * t)).Add(cPoint3.Mul(3 * (1 - t) * t * t)).Add(cPoint4.Mul(t * t * t))
}
// Returns the point at point t along an n-control point Bezier curve
//
// t must be in the range 0.0 and 1.0 or this function will panic. Consider [0.0,1.0] to be similar to a percentage,
// 0.0 is first control point, and the point at 1.0 is the last control point. Any point in between is how far along the path you are between 0 and 1.
//
// This function is not sensitive to the coordinate system of the control points. It will correctly interpolate regardless of whether they're in screen coords,
// gl coords, or something else entirely
func BezierCurve2D(t float32, cPoints []Vec2) Vec2 {
if t < 0.0 || t > 1.0 {
panic("Input to bezier has t not in range [0,1]. If you think this is a precision error, use mathgl.Clamp[f|d] before calling this function")
}
n := len(cPoints) - 1
point := cPoints[0].Mul(float32(math.Pow(float64(1.0-t), float64(n))))
for i := 1; i <= n; i++ {
point = point.Add(cPoints[i].Mul(float32(float64(choose(n, i)) * math.Pow(float64(1-t), float64(n-i)) * math.Pow(float64(t), float64(i))))) // P += P_i * nCi * (1-t)^(n-i) * t^i
}
return point
}
// Same as the 2D version, except the line is in 3D space
func BezierCurve3D(t float32, cPoints []Vec3) Vec3 {
if t < 0.0 || t > 1.0 {
panic("Input to bezier has t not in range [0,1]. If you think this is a precision error, use mathgl.Clamp[f|d] before calling this function")
}
n := len(cPoints) - 1
point := cPoints[0].Mul(float32(math.Pow(float64(1.0-t), float64(n))))
for i := 1; i <= n; i++ {
point = point.Add(cPoints[i].Mul(float32(float64(choose(n, i)) * math.Pow(float64(1-t), float64(n-i)) * math.Pow(float64(t), float64(i))))) // P += P_i * nCi * (1-t)^(n-i) * t^i
}
return point
}
// Generates a bezier curve with controlPoints cPoints. The numPoints argument
// determines how many "samples" it makes along the line. For instance, a
// call to this with numPoints 2 will have exactly two points: the start and end points
// For any points above 2 it will divide it into numPoints-1 chunks (which means it will generate numPoints-2 vertices other than the beginning and end).
// So for 3 points it will divide it in half, 4 points into thirds, and so on.
//
// This is likely to get rather expensive for anything over perhaps a cubic curve.
func MakeBezierCurve2D(numPoints int, cPoints []Vec2) (line []Vec2) {
line = make([]Vec2, numPoints)
if numPoints == 0 {
return
} else if numPoints == 1 {
line[0] = cPoints[0]
return
} else if numPoints == 2 {
line[0] = cPoints[0]
line[1] = cPoints[len(cPoints)-1]
return
}
line[0] = cPoints[0]
for i := 1; i < numPoints-1; i++ {
line[i] = BezierCurve2D(Clamp(float32(i)/float32(numPoints-1), 0.0, 1.0), cPoints)
}
line[numPoints-1] = cPoints[len(cPoints)-1]
return
}
// Same as the 2D version, except with the line in 3D space
func MakeBezierCurve3D(numPoints int, cPoints []Vec3) (line []Vec3) {
line = make([]Vec3, numPoints)
if numPoints == 0 {
return
} else if numPoints == 1 {
line[0] = cPoints[0]
return
} else if numPoints == 2 {
line[0] = cPoints[0]
line[1] = cPoints[len(cPoints)-1]
return
}
line[0] = cPoints[0]
for i := 1; i < numPoints-1; i++ {
line[i] = BezierCurve3D(Clamp(float32(i)/float32(numPoints-1), 0.0, 1.0), cPoints)
}
line[numPoints-1] = cPoints[len(cPoints)-1]
return
}
// Creates a 2-dimensional Bezier surface of arbitrary degree in 3D Space
// Like the curve functions, if u or v are not in the range [0.0,1.0] the function will panic, use Clamp[f|d]
// to ensure it is correct.
//
// The control point matrix must not be jagged, or this will end up panicking from an index out of bounds exception
func BezierSurface(u, v float32, cPoints [][]Vec3) Vec3 {
if u < 0.0 || u > 1.0 || v < 1.0 || v > 1.0 {
panic("u or v not in range [0.0,1.0] in BezierSurface")
}
n := len(cPoints) - 1
m := len(cPoints[0]) - 1
point := cPoints[0][0].Mul(float32(math.Pow(float64(1.0-u), float64(n)) * math.Pow(float64(1.0-v), float64(m))))
for i := 0; i <= n; i++ {
for j := 0; j <= m; j++ {
if i == 0 && j == 0 {
continue
}
point = point.Add(cPoints[i][j].Mul(float32(float64(choose(n, i)) * math.Pow(float64(u), float64(i)) * math.Pow(float64(1.0-u), float64(n-i)) * float64(choose(m, j)) * math.Pow(float64(v), float64(j)) * math.Pow(float64(1.0-v), float64(m-j)))))
}
}
return point
}
// Does interpolation over a spline of several bezier curves. Each bezier curve must have a finite range,
// though the spline may be disjoint. The bezier curves are not required to be in any particular order.
//
// If t is out of the range of all given curves, this function will panic
func BezierSplineInterpolate2D(t float32, ranges [][2]float32, cPoints [][]Vec2) Vec2 {
if len(ranges) != len(cPoints) {
panic("Each bezier curve needs a range")
}
for i, curveRange := range ranges {
if t >= curveRange[0] && t <= curveRange[1] {
return BezierCurve2D((t-curveRange[0])/(curveRange[1]-curveRange[0]), cPoints[i])
}
}
panic("t is out of the range of all bezier curves in this spline")
}
// Does interpolation over a spline of several bezier curves. Each bezier curve must have a finite range,
// though the spline may be disjoint. The bezier curves are not required to be in any particular order.
//
// If t is out of the range of all given curves, this function will panic
func BezierSplineInterpolate3D(t float32, ranges [][2]float32, cPoints [][]Vec3) Vec3 {
if len(ranges) != len(cPoints) {
panic("Each bezier curve needs a range")
}
for i, curveRange := range ranges {
if t >= curveRange[0] && t <= curveRange[1] {
return BezierCurve3D((t-curveRange[0])/(curveRange[1]-curveRange[0]), cPoints[i])
}
}
panic("t is out of the range of all bezier curves in this spline")
}
// Reticulates ALL the Splines
//
// For the overly serious: the function is just for fun. It does nothing except prints a Maxis reference. Technically you could "reticulate splines"
// by joining a bunch of splines together, but that ruins the joke.
func ReticulateSplines(ranges [][][2]float32, cPoints [][][]Vec2, withLlamas bool) {
if !withLlamas {
fmt.Println("You can't reticulate splines without llamas, silly.")
} else {
fmt.Println("Actually, you can't even reticulate splines WITH llamas")
}
}
// Transform from pixel coordinates to GL coordinates.
//
// This assumes that your pixel coordinate system considers its origin to be in the top left corner (GL's is in the bottom left).
// The coordinates x and y may be out of the range [0,screenWidth-1] and [0,screeneHeight-1].
//
// GL's coordinate system maps [screenWidth-1,0] to [1.0,1.0] and [0,screenHeight-1] to [-1.0,-1.0]. If x and y are out of the range, they'll still
// be mapped correctly, just off the screen. (e.g. if y = 2*(screenHeight-1) you'll get -3.0 for yOut)
//
// This is similar to Unproject, except for 2D cases and much simpler (especially since an inverse may always be found)
func ScreenToGLCoords(x, y int, screenWidth, screenHeight int) (xOut, yOut float32) {
xOut = 2.0*float32(x)/float32(screenWidth-1) - 1.0
yOut = -2.0*float32(y)/float32(screenHeight-1) + 1.0
return
}
// Transform from GL's proportional system to pixel coordinates.
//
// Assumes the pixel coordinate system has its origin in the top left corner. (GL's is in the bottom left)
//
// GL's coordinate system maps [screenWidth-1,0] to [1.0,1.0] and [0,screenHeight-1] to [-1.0,-1.0]. If x and y are out of the range, they'll still
// be mapped correctly, just off the screen. (e.g. if y=-3.0, you'll get 2*(screenHeight-1) for yOut)
//
// This is similar to Project, except for 2D cases and much simpler
func GLToScreenCoords(x, y float32, screenWidth, screenHeight int) (xOut, yOut int) {
xOut = int((x + 1.0) * float32(screenWidth-1) / 2.0)
yOut = int((1.0 - y) * float32(screenHeight-1) / 2.0)
return
}
func choose(n, k int) (result int) {
if k == 0 {
return 1
} else if n == 0 {
return 0
}
result = (n - (k - 1))
for i := 2; i <= k; i++ {
result *= (n - (k - i)) / i
}
return result
}

223
vendor/github.com/go-gl/mathgl/mgl32/transform.go generated vendored Normal file
View File

@ -0,0 +1,223 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import "math"
// Rotate2D returns a rotation Matrix about a angle in 2-D space. Specifically about the origin.
// It is a 2x2 matrix, if you need a 3x3 for Homogeneous math (e.g. composition with a Translation matrix)
// see HomogRotate2D
func Rotate2D(angle float32) Mat2 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat2{cos, sin, -sin, cos}
}
// Rotate3DX returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the X-axis
//
// Where c is cos(angle) and s is sin(angle)
// [1 0 0]
// [0 c -s]
// [0 s c ]
func Rotate3DX(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{1, 0, 0, 0, cos, sin, 0, -sin, cos}
}
// Rotate3DY returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the Y-axis
//
// Where c is cos(angle) and s is sin(angle)
// [c 0 s]
// [0 1 0]
// [s 0 c ]
func Rotate3DY(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{cos, 0, -sin, 0, 1, 0, sin, 0, cos}
}
// Rotate3DZ returns a 3x3 (non-homogeneous) Matrix that rotates by angle about the Z-axis
//
// Where c is cos(angle) and s is sin(angle)
// [c -s 0]
// [s c 0]
// [0 0 1 ]
func Rotate3DZ(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}
// Translate2D returns a homogeneous (3x3 for 2D-space) Translation matrix that moves a point by Tx units in the x-direction and Ty units in the y-direction
//
// [[1, 0, Tx]]
// [[0, 1, Ty]]
// [[0, 0, 1 ]]
func Translate2D(Tx, Ty float32) Mat3 {
return Mat3{1, 0, 0, 0, 1, 0, float32(Tx), float32(Ty), 1}
}
// Translate3D returns a homogeneous (4x4 for 3D-space) Translation matrix that moves a point by Tx units in the x-direction, Ty units in the y-direction,
// and Tz units in the z-direction
//
// [[1, 0, 0, Tx]]
// [[0, 1, 0, Ty]]
// [[0, 0, 1, Tz]]
// [[0, 0, 0, 1 ]]
func Translate3D(Tx, Ty, Tz float32) Mat4 {
return Mat4{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, float32(Tx), float32(Ty), float32(Tz), 1}
}
// Same as Rotate2D, except homogeneous (3x3 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate2D(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}
// Same as Rotate3DX, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DX(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat4{1, 0, 0, 0, 0, cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1}
}
// Same as Rotate3DY, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DY(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat4{cos, 0, -sin, 0, 0, 1, 0, 0, sin, 0, cos, 0, 0, 0, 0, 1}
}
// Same as Rotate3DZ, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DZ(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
return Mat4{cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
}
// Scale3D creates a homogeneous 3D scaling matrix
// [[ scaleX, 0 , 0 , 0 ]]
// [[ 0 , scaleY, 0 , 0 ]]
// [[ 0 , 0 , scaleZ, 0 ]]
// [[ 0 , 0 , 0 , 1 ]]
func Scale3D(scaleX, scaleY, scaleZ float32) Mat4 {
return Mat4{float32(scaleX), 0, 0, 0, 0, float32(scaleY), 0, 0, 0, 0, float32(scaleZ), 0, 0, 0, 0, 1}
}
// Scale2D creates a homogeneous 2D scaling matrix
// [[ scaleX, 0 , 0 ]]
// [[ 0 , scaleY, 0 ]]
// [[ 0 , 0 , 1 ]]
func Scale2D(scaleX, scaleY float32) Mat3 {
return Mat3{float32(scaleX), 0, 0, 0, float32(scaleY), 0, 0, 0, 1}
}
// ShearX2D creates a homogeneous 2D shear matrix along the X-axis
func ShearX2D(shear float32) Mat3 {
return Mat3{1, 0, 0, float32(shear), 1, 0, 0, 0, 1}
}
// ShearY2D creates a homogeneous 2D shear matrix along the Y-axis
func ShearY2D(shear float32) Mat3 {
return Mat3{1, float32(shear), 0, 0, 1, 0, 0, 0, 1}
}
// ShearX3D creates a homogeneous 3D shear matrix along the X-axis
func ShearX3D(shearY, shearZ float32) Mat4 {
return Mat4{1, float32(shearY), float32(shearZ), 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
}
// ShearY3D creates a homogeneous 3D shear matrix along the Y-axis
func ShearY3D(shearX, shearZ float32) Mat4 {
return Mat4{1, 0, 0, 0, float32(shearX), 1, float32(shearZ), 0, 0, 0, 1, 0, 0, 0, 0, 1}
}
// ShearZ3D creates a homogeneous 3D shear matrix along the Z-axis
func ShearZ3D(shearX, shearY float32) Mat4 {
return Mat4{1, 0, 0, 0, 0, 1, 0, 0, float32(shearX), float32(shearY), 1, 0, 0, 0, 0, 1}
}
// HomogRotate3D creates a 3D rotation Matrix that rotates by (radian) angle about some arbitrary axis given by a normalized Vector.
// It produces a homogeneous matrix (4x4)
//
// Where c is cos(angle) and s is sin(angle), and x, y, and z are the first, second, and third elements of the axis vector (respectively):
//
// [[ x^2(1-c)+c, xy(1-c)-zs, xz(1-c)+ys, 0 ]]
// [[ xy(1-c)+zs, y^2(1-c)+c, yz(1-c)-xs, 0 ]]
// [[ xz(1-c)-ys, yz(1-c)+xs, z^2(1-c)+c, 0 ]]
// [[ 0 , 0 , 0 , 1 ]]
func HomogRotate3D(angle float32, axis Vec3) Mat4 {
x, y, z := axis[0], axis[1], axis[2]
s, c := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
k := 1 - c
return Mat4{x*x*k + c, x*y*k + z*s, x*z*k - y*s, 0, x*y*k - z*s, y*y*k + c, y*z*k + x*s, 0, x*z*k + y*s, y*z*k - x*s, z*z*k + c, 0, 0, 0, 0, 1}
}
// Extracts the 3d scaling from a homogeneous matrix
func Extract3DScale(m Mat4) (x, y, z float32) {
return float32(math.Sqrt(float64(m[0]*m[0] + m[1]*m[1] + m[2]*m[2]))),
float32(math.Sqrt(float64(m[4]*m[4] + m[5]*m[5] + m[6]*m[6]))),
float32(math.Sqrt(float64(m[8]*m[8] + m[9]*m[9] + m[10]*m[10])))
}
// Extracts the maximum scaling from a homogeneous matrix
func ExtractMaxScale(m Mat4) float32 {
scaleX := float64(m[0]*m[0] + m[1]*m[1] + m[2]*m[2])
scaleY := float64(m[4]*m[4] + m[5]*m[5] + m[6]*m[6])
scaleZ := float64(m[8]*m[8] + m[9]*m[9] + m[10]*m[10])
return float32(math.Sqrt(math.Max(scaleX, math.Max(scaleY, scaleZ))))
}
// Calculates the Normal of the Matrix (aka the inverse transpose)
func Mat4Normal(m Mat4) Mat3 {
n := m.Inv().Transpose()
return Mat3{n[0], n[1], n[2], n[4], n[5], n[6], n[8], n[9], n[10]}
}
// Multiplies a 3D vector by a transformation given by
// the homogeneous 4D matrix m, applying any translation.
// If this transformation is non-affine, it will project this
// vector onto the plane w=1 before returning the result.
//
// This is similar to saying you're transforming and projecting a point.
//
// This is effectively equivalent to the GLSL code
// vec4 r = (m * vec4(v,1.));
// r = r/r.w;
// vec3 newV = r.xyz;
func TransformCoordinate(v Vec3, m Mat4) Vec3 {
t := v.Vec4(1)
t = m.Mul4x1(t)
t = t.Mul(1 / t[3])
return t.Vec3()
}
// Multiplies a 3D vector by a transformation given by
// the homogeneous 4D matrix m, NOT applying any translations.
//
// This is similar to saying you're applying a transformation
// to a direction or normal. Rotation still applies (as does scaling),
// but translating a direction or normal is meaningless.
//
// This is effectively equivalent to the GLSL code
// vec4 r = (m * vec4(v,0.));
// vec3 newV = r.xyz
func TransformNormal(v Vec3, m Mat4) Vec3 {
t := v.Vec4(0)
t = m.Mul4x1(t)
return t.Vec3()
}

142
vendor/github.com/go-gl/mathgl/mgl32/util.go generated vendored Normal file
View File

@ -0,0 +1,142 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run codegen.go -template vector.tmpl -output vector.go
//go:generate go run codegen.go -template matrix.tmpl -output matrix.go
//go:generate go run codegen.go -mgl64
package mgl32
import (
"math"
)
// Epsilon is some tiny value that determines how precisely equal we want our floats to be
// This is exported and left as a variable in case you want to change the default threshold for the
// purposes of certain methods (e.g. Unproject uses the default epsilon when determining
// if the determinant is "close enough" to zero to mean there's no inverse).
//
// This is, obviously, not mutex protected so be **absolutely sure** that no functions using Epsilon
// are being executed when you change this.
var Epsilon float32 = 1e-10
// A direct copy of the math package's Abs. This is here for the mgl32
// package, to prevent rampant type conversions during equality tests.
func Abs(a float32) float32 {
if a < 0 {
return -a
} else if a == 0 {
return 0
}
return a
}
// FloatEqual is a safe utility function to compare floats.
// It's Taken from http://floating-point-gui.de/errors/comparison/
//
// It is slightly altered to not call Abs when not needed.
func FloatEqual(a, b float32) bool {
return FloatEqualThreshold(a, b, Epsilon)
}
// FloatEqualFunc is a utility closure that will generate a function that
// always approximately compares floats like FloatEqualThreshold with a different
// threshold.
func FloatEqualFunc(epsilon float32) func(float32, float32) bool {
return func(a, b float32) bool {
return FloatEqualThreshold(a, b, epsilon)
}
}
var (
MinNormal = float32(1.1754943508222875e-38) // 1 / 2**(127 - 1)
MinValue = float32(math.SmallestNonzeroFloat32)
MaxValue = float32(math.MaxFloat32)
InfPos = float32(math.Inf(1))
InfNeg = float32(math.Inf(-1))
NaN = float32(math.NaN())
)
// FloatEqualThreshold is a utility function to compare floats.
// It's Taken from http://floating-point-gui.de/errors/comparison/
//
// It is slightly altered to not call Abs when not needed.
//
// This differs from FloatEqual in that it lets you pass in your comparison threshold, so that you can adjust the comparison value to your specific needs
func FloatEqualThreshold(a, b, epsilon float32) bool {
if a == b { // Handles the case of inf or shortcuts the loop when no significant error has accumulated
return true
}
diff := Abs(a - b)
if a*b == 0 || diff < MinNormal { // If a or b are 0 or both are extremely close to it
return diff < epsilon*epsilon
}
// Else compare difference
return diff/(Abs(a)+Abs(b)) < epsilon
}
// Clamp takes in a value and two thresholds. If the value is smaller than the low
// threshold, it returns the low threshold. If it's bigger than the high threshold
// it returns the high threshold. Otherwise it returns the value.
//
// Useful to prevent some functions from freaking out because a value was
// teeeeechnically out of range.
func Clamp(a, low, high float32) float32 {
if a < low {
return low
} else if a > high {
return high
}
return a
}
// ClampFunc generates a closure that returns its parameter
// clamped to the range [low,high].
func ClampFunc(low, high float32) func(float32) float32 {
return func(a float32) float32 {
return Clamp(a, low, high)
}
}
/* The IsClamped functions use strict equality (meaning: not the FloatEqual function)
there shouldn't be any major issues with this since clamp is often used to fix minor errors*/
// Checks if a is clamped between low and high as if
// Clamp(a, low, high) had been called.
//
// In most cases it's probably better to just call Clamp
// without checking this since it's relatively cheap.
func IsClamped(a, low, high float32) bool {
return a >= low && a <= high
}
// If a > b, then a will be set to the value of b.
func SetMin(a, b *float32) {
if *b < *a {
*a = *b
}
}
// If a < b, then a will be set to the value of b.
func SetMax(a, b *float32) {
if *a < *b {
*a = *b
}
}
// Round shortens a float32 value to a specified precision (number of digits after the decimal point)
// with "round half up" tie-braking rule. Half-way values (23.5) are always rounded up (24).
func Round(v float32, precision int) float32 {
p := float64(precision)
t := float64(v) * math.Pow(10, p)
if t > 0 {
return float32(math.Floor(t+0.5) / math.Pow(10, p))
}
return float32(math.Ceil(t-0.5) / math.Pow(10, p))
}

350
vendor/github.com/go-gl/mathgl/mgl32/vecn.go generated vendored Normal file
View File

@ -0,0 +1,350 @@
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl32
import (
"math"
)
// A vector of N elements backed by a slice
//
// As with MatMxN, this is not for hardcore linear algebra with large dimensions. Use github.com/gonum/matrix
// or something like BLAS/LAPACK for that. This is for corner cases in 3D math where you require
// something a little bigger that 4D, but still relatively small.
//
// This VecN uses several sync.Pool objects as a memory pool. The rule is that for any sized vector, the backing slice
// has CAPACITY (not length) of 2^p where p is Ceil(log_2(N)) -- or in other words, rounding up the base-2
// log of the size of the vector. E.G. a VecN of size 17 will have a backing slice of Cap 32.
type VecN struct {
vec []float32
}
// Creates a new vector with a backing slice filled with the contents
// of initial. It is NOT backed by initial, but rather a slice with cap
// 2^p where p is Ceil(log_2(len(initial))), with the data from initial copied into
// it.
func NewVecNFromData(initial []float32) *VecN {
if initial == nil {
return &VecN{}
}
var internal []float32
if shouldPool {
internal = grabFromPool(len(initial))
} else {
internal = make([]float32, len(initial))
}
copy(internal, initial)
return &VecN{vec: internal}
}
// Creates a new vector with a backing slice of
// 2^p where p = Ceil(log_2(n))
func NewVecN(n int) *VecN {
if shouldPool {
return &VecN{vec: grabFromPool(n)}
} else {
return &VecN{vec: make([]float32, n)}
}
}
// Returns the raw slice backing the VecN
//
// This may be sent back to the memory pool at any time
// and you aren't advised to rely on this value
func (vn VecN) Raw() []float32 {
return vn.vec
}
// Gets the element at index i from the vector.
// This does not bounds check, and will panic if i is
// out of range.
func (vn VecN) Get(i int) float32 {
return vn.vec[i]
}
func (vn *VecN) Set(i int, val float32) {
vn.vec[i] = val
}
// Sends the allocated memory through the callback if it exists
func (vn *VecN) destroy() {
if vn == nil || vn.vec == nil {
return
}
if shouldPool {
returnToPool(vn.vec)
}
vn.vec = nil
}
// Resizes the underlying slice to the desired amount, reallocating or retrieving from the pool
// if necessary. The values after a Resize cannot be expected to be related to the values before a Resize.
//
// If the caller is a nil pointer, this returns a value as if NewVecN(n) had been called,
// otherwise it simply returns the caller.
func (vn *VecN) Resize(n int) *VecN {
if vn == nil {
return NewVecN(n)
}
if n <= cap(vn.vec) {
if vn.vec != nil {
vn.vec = vn.vec[:n]
} else {
vn.vec = []float32{}
}
return vn
}
if shouldPool && vn.vec != nil {
returnToPool(vn.vec)
}
*vn = (*NewVecN(n))
return vn
}
// Sets the vector's backing slice to the given
// new one.
func (vn *VecN) SetBackingSlice(newSlice []float32) {
vn.vec = newSlice
}
// Return the len of the vector's underlying slice.
// This is not titled Len because it conflicts the package's
// convention of calling the Norm the Len.
func (vn *VecN) Size() int {
return len(vn.vec)
}
// Returns the cap of the vector's underlying slice.
func (vn *VecN) Cap() int {
return cap(vn.vec)
}
// Sets the vector's size to n and zeroes out the vector.
// If n is bigger than the vector's size, it will realloc.
func (vn *VecN) Zero(n int) {
vn.Resize(n)
for i := range vn.vec {
vn.vec[i] = 0
}
}
// Adds vn and addend, storing the result in dst.
// If dst does not have sufficient size it will be resized
// Dst may be one of the other arguments. If dst is nil, it will be allocated.
// The value returned is dst, for easier method chaining
//
// If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size())
// elements.
func (vn *VecN) Add(dst *VecN, subtrahend *VecN) *VecN {
if vn == nil || subtrahend == nil {
return nil
}
size := intMin(len(vn.vec), len(subtrahend.vec))
dst = dst.Resize(size)
for i := 0; i < size; i++ {
dst.vec[i] = vn.vec[i] + subtrahend.vec[i]
}
return dst
}
// Subtracts addend from vn, storing the result in dst.
// If dst does not have sufficient size it will be resized
// Dst may be one of the other arguments. If dst is nil, it will be allocated.
// The value returned is dst, for easier method chaining
//
// If vn and addend are not the same size, this function will add min(vn.Size(), addend.Size())
// elements.
func (vn *VecN) Sub(dst *VecN, addend *VecN) *VecN {
if vn == nil || addend == nil {
return nil
}
size := intMin(len(vn.vec), len(addend.vec))
dst = dst.Resize(size)
for i := 0; i < size; i++ {
dst.vec[i] = vn.vec[i] - addend.vec[i]
}
return dst
}
// Takes the binary cross product of vn and other, and stores it in dst.
// If either vn or other are not of size 3 this function will panic
//
// If dst is not of sufficient size, or is nil, a new slice is allocated.
// Dst is permitted to be one of the other arguments
func (vn *VecN) Cross(dst *VecN, other *VecN) *VecN {
if vn == nil || other == nil {
return nil
}
if len(vn.vec) != 3 || len(other.vec) != 3 {
panic("Cannot take binary cross product of non-3D elements (7D cross product not implemented)")
}
dst = dst.Resize(3)
dst.vec[0], dst.vec[1], dst.vec[2] = vn.vec[1]*other.vec[2]-vn.vec[2]*other.vec[1], vn.vec[2]*other.vec[0]-vn.vec[0]*other.vec[2], vn.vec[0]*other.vec[1]-vn.vec[1]*other.vec[0]
return dst
}
func intMin(a, b int) int {
if a < b {
return a
}
return b
}
// Computes the dot product of two VecNs, if
// the two vectors are not of the same length -- this
// will return NaN.
func (vn *VecN) Dot(other *VecN) float32 {
if vn == nil || other == nil || len(vn.vec) != len(other.vec) {
return float32(math.NaN())
}
var result float32 = 0.0
for i, el := range vn.vec {
result += el * other.vec[i]
}
return result
}
// Computes the vector length (also called the Norm) of the
// vector. Equivalent to math.Sqrt(vn.Dot(vn)) with the appropriate
// type conversions.
//
// If vn is nil, this returns NaN
func (vn *VecN) Len() float32 {
if vn == nil {
return float32(math.NaN())
}
if len(vn.vec) == 0 {
return 0
}
return float32(math.Sqrt(float64(vn.Dot(vn))))
}
// Normalizes the vector and stores the result in dst, which
// will be returned. Dst will be appropraitely resized to the
// size of vn.
//
// The destination can be vn itself and nothing will go wrong.
//
// This is equivalent to vn.Mul(dst, 1/vn.Len())
func (vn *VecN) Normalize(dst *VecN) *VecN {
if vn == nil {
return nil
}
return vn.Mul(dst, 1/vn.Len())
}
// Multiplied the vector by some scalar value and stores the result in dst, which
// will be returned. Dst will be appropraitely resized to the
// size of vn.
//
// The destination can be vn itself and nothing will go wrong.
func (vn *VecN) Mul(dst *VecN, c float32) *VecN {
if vn == nil {
return nil
}
dst = dst.Resize(len(vn.vec))
for i, el := range vn.vec {
dst.vec[i] = el * c
}
return dst
}
// Performs the vector outer product between vn and v2.
// The outer product is like a "reverse" dot product. Where the dot product
// aligns both vectors with the "sized" part facing "inward" (Vec3*Vec3=Mat1x3*Mat3x1=Mat1x1=Scalar).
// The outer product multiplied them with it facing "outward"
// (Vec3*Vec3=Mat3x1*Mat1x3=Mat3x3).
//
// The matrix dst will be Reshaped to the correct size, if vn or v2 are nil,
// this returns nil.
func (vn *VecN) OuterProd(dst *MatMxN, v2 *VecN) *MatMxN {
if vn == nil || v2 == nil {
return nil
}
dst = dst.Reshape(len(vn.vec), len(v2.vec))
for c, el1 := range v2.vec {
for r, el2 := range vn.vec {
dst.Set(r, c, el1*el2)
}
}
return dst
}
func (vn *VecN) ApproxEqual(vn2 *VecN) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !FloatEqual(el, vn2.vec[i]) {
return false
}
}
return true
}
func (vn *VecN) ApproxEqualThreshold(vn2 *VecN, epsilon float32) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !FloatEqualThreshold(el, vn2.vec[i], epsilon) {
return false
}
}
return true
}
func (vn *VecN) ApproxEqualFunc(vn2 *VecN, comp func(float32, float32) bool) bool {
if vn == nil || vn2 == nil || len(vn.vec) != len(vn2.vec) {
return false
}
for i, el := range vn.vec {
if !comp(el, vn2.vec[i]) {
return false
}
}
return true
}
func (vn *VecN) Vec2() Vec2 {
raw := vn.Raw()
return Vec2{raw[0], raw[1]}
}
func (vn *VecN) Vec3() Vec3 {
raw := vn.Raw()
return Vec3{raw[0], raw[1], raw[2]}
}
func (vn *VecN) Vec4() Vec4 {
raw := vn.Raw()
return Vec4{raw[0], raw[1], raw[2], raw[3]}
}

562
vendor/github.com/go-gl/mathgl/mgl32/vector.go generated vendored Normal file
View File

@ -0,0 +1,562 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file is generated by codegen.go; DO NOT EDIT
// Edit vector.tmpl and run "go generate" to make changes.
package mgl32
import (
"golang.org/x/image/math/f32"
"math"
)
type Vec2 f32.Vec2
type Vec3 f32.Vec3
type Vec4 f32.Vec4
func (v Vec2) Vec3(z float32) Vec3 {
return Vec3{v[0], v[1], z}
}
func (v Vec2) Vec4(z, w float32) Vec4 {
return Vec4{v[0], v[1], z, w}
}
func (v Vec3) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec3) Vec4(w float32) Vec4 {
return Vec4{v[0], v[1], v[2], w}
}
func (v Vec4) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec4) Vec3() Vec3 {
return Vec3{v[0], v[1], v[2]}
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec2) Elem() (x, y float32) {
return v[0], v[1]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec3) Elem() (x, y, z float32) {
return v[0], v[1], v[2]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec4) Elem() (x, y, z, w float32) {
return v[0], v[1], v[2], v[3]
}
// The vector cross product is an operation only defined on 3D vectors. It is equivalent to
// Vec3{v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}.
// Another interpretation is that it's the vector whose magnitude is |v1||v2|sin(theta)
// where theta is the angle between v1 and v2.
//
// The cross product is most often used for finding surface normals. The cross product of vectors
// will generate a vector that is perpendicular to the plane they form.
//
// Technically, a generalized cross product exists as an "(N-1)ary" operation
// (that is, the 4D cross product requires 3 4D vectors). But the binary
// 3D (and 7D) cross product is the most important. It can be considered
// the area of a parallelogram with sides v1 and v2.
//
// Like the dot product, the cross product is roughly a measure of directionality.
// Two normalized perpendicular vectors will return a vector with a magnitude of
// 1.0 or -1.0 and two parallel vectors will return a vector with magnitude 0.0.
// The cross product is "anticommutative" meaning v1.Cross(v2) = -v2.Cross(v1),
// this property can be useful to know when finding normals,
// as taking the wrong cross product can lead to the opposite normal of the one you want.
func (v1 Vec3) Cross(v2 Vec3) Vec3 {
return Vec3{v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}
}
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 Vec2) Add(v2 Vec2) Vec2 {
return Vec2{v1[0] + v2[0], v1[1] + v2[1]}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 Vec2) Sub(v2 Vec2) Vec2 {
return Vec2{v1[0] - v2[0], v1[1] - v2[1]}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 Vec2) Mul(c float32) Vec2 {
return Vec2{v1[0] * c, v1[1] * c}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 Vec2) Dot(v2 Vec2) float32 {
return v1[0]*v2[0] + v1[1]*v2[1]
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 Vec2) Len() float32 {
return float32(math.Hypot(float64(v1[0]), float64(v1[1])))
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 Vec2) Normalize() Vec2 {
l := 1.0 / v1.Len()
return Vec2{v1[0] * l, v1[1] * l}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 Vec2) ApproxEqual(v2 Vec2) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 Vec2) ApproxEqualThreshold(v2 Vec2, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 Vec2) ApproxFuncEqual(v2 Vec2, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec2) X() float32 {
return v[0]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec2) Y() float32 {
return v[1]
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 2x2 matrix. E.G. a Vec2 * Vec2 = Mat2.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec2) OuterProd2(v2 Vec2) Mat2 {
return Mat2{v1[0] * v2[0], v1[1] * v2[0], v1[0] * v2[1], v1[1] * v2[1]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 2x3 matrix. E.G. a Vec2 * Vec3 = Mat2x3.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec2) OuterProd3(v2 Vec3) Mat2x3 {
return Mat2x3{v1[0] * v2[0], v1[1] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[0] * v2[2], v1[1] * v2[2]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 2x4 matrix. E.G. a Vec2 * Vec4 = Mat2x4.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec2) OuterProd4(v2 Vec4) Mat2x4 {
return Mat2x4{v1[0] * v2[0], v1[1] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[0] * v2[3], v1[1] * v2[3]}
}
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 Vec3) Add(v2 Vec3) Vec3 {
return Vec3{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 Vec3) Sub(v2 Vec3) Vec3 {
return Vec3{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 Vec3) Mul(c float32) Vec3 {
return Vec3{v1[0] * c, v1[1] * c, v1[2] * c}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 Vec3) Dot(v2 Vec3) float32 {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 Vec3) Len() float32 {
return float32(math.Sqrt(float64(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2])))
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 Vec3) Normalize() Vec3 {
l := 1.0 / v1.Len()
return Vec3{v1[0] * l, v1[1] * l, v1[2] * l}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 Vec3) ApproxEqual(v2 Vec3) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 Vec3) ApproxEqualThreshold(v2 Vec3, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 Vec3) ApproxFuncEqual(v2 Vec3, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec3) X() float32 {
return v[0]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec3) Y() float32 {
return v[1]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec3) Z() float32 {
return v[2]
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 3x2 matrix. E.G. a Vec3 * Vec2 = Mat3x2.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec3) OuterProd2(v2 Vec2) Mat3x2 {
return Mat3x2{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 3x3 matrix. E.G. a Vec3 * Vec3 = Mat3.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec3) OuterProd3(v2 Vec3) Mat3 {
return Mat3{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 3x4 matrix. E.G. a Vec3 * Vec4 = Mat3x4.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec3) OuterProd4(v2 Vec4) Mat3x4 {
return Mat3x4{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2], v1[0] * v2[3], v1[1] * v2[3], v1[2] * v2[3]}
}
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 Vec4) Add(v2 Vec4) Vec4 {
return Vec4{v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2], v1[3] + v2[3]}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 Vec4) Sub(v2 Vec4) Vec4 {
return Vec4{v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2], v1[3] - v2[3]}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 Vec4) Mul(c float32) Vec4 {
return Vec4{v1[0] * c, v1[1] * c, v1[2] * c, v1[3] * c}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 Vec4) Dot(v2 Vec4) float32 {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2] + v1[3]*v2[3]
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 Vec4) Len() float32 {
return float32(math.Sqrt(float64(v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2] + v1[3]*v1[3])))
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 Vec4) Normalize() Vec4 {
l := 1.0 / v1.Len()
return Vec4{v1[0] * l, v1[1] * l, v1[2] * l, v1[3] * l}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 Vec4) ApproxEqual(v2 Vec4) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 Vec4) ApproxEqualThreshold(v2 Vec4, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 Vec4) ApproxFuncEqual(v2 Vec4, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) X() float32 {
return v[0]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) Y() float32 {
return v[1]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) Z() float32 {
return v[2]
}
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v Vec4) W() float32 {
return v[3]
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 4x2 matrix. E.G. a Vec4 * Vec2 = Mat4x2.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec4) OuterProd2(v2 Vec2) Mat4x2 {
return Mat4x2{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[3] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[3] * v2[1]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 4x3 matrix. E.G. a Vec4 * Vec3 = Mat4x3.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec4) OuterProd3(v2 Vec3) Mat4x3 {
return Mat4x3{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[3] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[3] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2], v1[3] * v2[2]}
}
// Does the vector outer product
// of two vectors. The outer product produces an
// 4x4 matrix. E.G. a Vec4 * Vec4 = Mat4.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 Vec4) OuterProd4(v2 Vec4) Mat4 {
return Mat4{v1[0] * v2[0], v1[1] * v2[0], v1[2] * v2[0], v1[3] * v2[0], v1[0] * v2[1], v1[1] * v2[1], v1[2] * v2[1], v1[3] * v2[1], v1[0] * v2[2], v1[1] * v2[2], v1[2] * v2[2], v1[3] * v2[2], v1[0] * v2[3], v1[1] * v2[3], v1[2] * v2[3], v1[3] * v2[3]}
}

207
vendor/github.com/go-gl/mathgl/mgl32/vector.tmpl generated vendored Normal file
View File

@ -0,0 +1,207 @@
// Copyright 2014 The go-gl/mathgl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// <<.Comment>>
// Edit <<.TemplateName>> and run "go generate" to make changes.
package mgl32
import (
"golang.org/x/image/math/f32"
"math"
)
type Vec2 f32.Vec2
type Vec3 f32.Vec3
type Vec4 f32.Vec4
func (v Vec2) Vec3(z float32) Vec3 {
return Vec3{v[0], v[1], z}
}
func (v Vec2) Vec4(z, w float32) Vec4 {
return Vec4{v[0], v[1], z, w}
}
func (v Vec3) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec3) Vec4(w float32) Vec4 {
return Vec4{v[0], v[1], v[2], w}
}
func (v Vec4) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
func (v Vec4) Vec3() Vec3 {
return Vec3{v[0], v[1], v[2]}
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec2) Elem() (x, y float32) {
return v[0], v[1]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec3) Elem() (x, y, z float32) {
return v[0], v[1], v[2]
}
// Elem extracts the elements of the vector for direct value assignment.
func (v Vec4) Elem() (x, y, z, w float32) {
return v[0], v[1], v[2], v[3]
}
// The vector cross product is an operation only defined on 3D vectors. It is equivalent to
// Vec3{v1[1]*v2[2]-v1[2]*v2[1], v1[2]*v2[0]-v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}.
// Another interpretation is that it's the vector whose magnitude is |v1||v2|sin(theta)
// where theta is the angle between v1 and v2.
//
// The cross product is most often used for finding surface normals. The cross product of vectors
// will generate a vector that is perpendicular to the plane they form.
//
// Technically, a generalized cross product exists as an "(N-1)ary" operation
// (that is, the 4D cross product requires 3 4D vectors). But the binary
// 3D (and 7D) cross product is the most important. It can be considered
// the area of a parallelogram with sides v1 and v2.
//
// Like the dot product, the cross product is roughly a measure of directionality.
// Two normalized perpendicular vectors will return a vector with a magnitude of
// 1.0 or -1.0 and two parallel vectors will return a vector with magnitude 0.0.
// The cross product is "anticommutative" meaning v1.Cross(v2) = -v2.Cross(v1),
// this property can be useful to know when finding normals,
// as taking the wrong cross product can lead to the opposite normal of the one you want.
func (v1 Vec3) Cross(v2 Vec3) Vec3 {
return Vec3{v1[1]*v2[2] - v1[2]*v2[1], v1[2]*v2[0] - v1[0]*v2[2], v1[0]*v2[1] - v1[1]*v2[0]}
}
<</* Common functions for all vectors */>>
<<range $m := enum 2 3 4>>
<<$type := typename $m 1>>
// Add performs element-wise addition between two vectors. It is equivalent to iterating
// over every element of v1 and adding the corresponding element of v2 to it.
func (v1 <<$type>>) Add(v2 <<$type>>) <<$type>> {
return <<$type>>{<<range $i := iter 0 $m>> v1[<<$i>>] + v2[<<$i>>], <<end>>}
}
// Sub performs element-wise subtraction between two vectors. It is equivalent to iterating
// over every element of v1 and subtracting the corresponding element of v2 from it.
func (v1 <<$type>>) Sub(v2 <<$type>>) <<$type>> {
return <<$type>>{<<range $i := iter 0 $m>> v1[<<$i>>] - v2[<<$i>>], <<end>>}
}
// Mul performs a scalar multiplication between the vector and some constant value
// c. This is equivalent to iterating over every vector element and multiplying by c.
func (v1 <<$type>>) Mul(c float32) <<$type>> {
return <<$type>>{<<range $i := iter 0 $m>> v1[<<$i>>] * c, <<end>>}
}
// Dot returns the dot product of this vector with another. There are multiple ways
// to describe this value. One is the multiplication of their lengths and cos(theta) where
// theta is the angle between the vectors: v1.v2 = |v1||v2|cos(theta).
//
// The other (and what is actually done) is the sum of the element-wise multiplication of all
// elements. So for instance, two Vec3s would yield v1.x * v2.x + v1.y * v2.y + v1.z * v2.z.
//
// This means that the dot product of a vector and itself is the square of its Len (within
// the bounds of floating points error).
//
// The dot product is roughly a measure of how closely two vectors are to pointing in the same
// direction. If both vectors are normalized, the value will be -1 for opposite pointing,
// one for same pointing, and 0 for perpendicular vectors.
func (v1 <<$type>>) Dot(v2 <<$type>>) float32 {
return <<range $i := iter 0 $m>><<sep "+" $i>> v1[<<$i>>]*v2[<<$i>>] <<end>>
}
// Len returns the vector's length. Note that this is NOT the dimension of
// the vector (len(v)), but the mathematical length. This is equivalent to the square
// root of the sum of the squares of all elements. E.G. for a Vec2 it's
// math.Hypot(v[0], v[1]).
func (v1 <<$type>>) Len() float32 {
<<if eq $m 2 >>
return float32(math.Hypot(float64(v1[0]), float64(v1[1])))
<<else>>
return float32(math.Sqrt(float64(<<repeat $m "v1[%d]*v1[%d]" "+">>)))
<<end>>
}
// Normalize normalizes the vector. Normalization is (1/|v|)*v,
// making this equivalent to v.Scale(1/v.Len()). If the len is 0.0,
// this function will return an infinite value for all elements due
// to how floating point division works in Go (n/0.0 = math.Inf(Sign(n))).
//
// Normalization makes a vector's Len become 1.0 (within the margin of floating point error),
// while maintaining its directionality.
//
// (Can be seen here: http://play.golang.org/p/Aaj7SnbqIp )
func (v1 <<$type>>) Normalize() <<$type>> {
l := 1.0 / v1.Len()
return <<$type>>{<<range $i := iter 0 $m>>v1[<<$i>>] * l,<<end>>}
}
// ApproxEqual takes in a vector and does an element-wise
// approximate float comparison as if FloatEqual had been used
func (v1 <<$type>>) ApproxEqual(v2 <<$type>>) bool {
for i := range v1 {
if !FloatEqual(v1[i], v2[i]) {
return false
}
}
return true
}
// ApproxThresholdEq takes in a threshold for comparing two floats, and uses it to do an
// element-wise comparison of the vector to another.
func (v1 <<$type>>) ApproxEqualThreshold(v2 <<$type>>, threshold float32) bool {
for i := range v1 {
if !FloatEqualThreshold(v1[i], v2[i], threshold) {
return false
}
}
return true
}
// ApproxFuncEq takes in a func that compares two floats, and uses it to do an element-wise
// comparison of the vector to another. This is intended to be used with FloatEqualFunc
func (v1 <<$type>>) ApproxFuncEqual(v2 <<$type>>, eq func(float32, float32) bool) bool {
for i := range v1 {
if !eq(v1[i], v2[i]) {
return false
}
}
return true
}
<<range $i := iter 0 $m>>
// This is an element access func, it is equivalent to v[n] where
// n is some valid index. The mappings are XYZW (X=0, Y=1 etc). Benchmarks
// show that this is more or less as fast as direct acces, probably due to
// inlining, so use v[0] or v.X() depending on personal preference.
func (v <<$type>>) <<elementname $i>>() float32 {
return v[<<$i>>]
}
<<end>>
<<range $n := enum 2 3 4>>
// Does the vector outer product
// of two vectors. The outer product produces an
// <<$m>>x<<$n>> matrix. E.G. a Vec<<$m>> * Vec<<$n>> = <<typename $m $n>>.
//
// The outer product can be thought of as the "opposite"
// of the Dot product. The Dot product treats both vectors like matrices
// oriented such that the left one has N columns and the right has N rows.
// So Vec3.Vec3 = Mat1x3*Mat3x1 = Mat1 = Scalar.
//
// The outer product orients it so they're facing "outward": Vec2*Vec3
// = Mat2x1*Mat1x3 = Mat2x3.
func (v1 <<$type>>) OuterProd<<$n>>(v2 <<typename $n 1>>) <<typename $m $n>> {
return <<typename $m $n>>{<<range $i := matiter $m $n>>v1[<<$i.M>>] * v2[<<$i.N>>], <<end>>}
}
<<end>>
<<end>> <</* range $m */>>

20
vendor/github.com/golang/freetype/AUTHORS generated vendored Normal file
View File

@ -0,0 +1,20 @@
# This is the official list of Freetype-Go authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS files.
# See the latter for an explanation.
#
# Freetype-Go is derived from Freetype, which is written in C. The latter
# is copyright 1996-2010 David Turner, Robert Wilhelm, and Werner Lemberg.
# Names should be added to this file as
# Name or Organization <email address>
# The email address is not required for organizations.
# Please keep the list sorted.
Google Inc.
Jeff R. Allen <jra@nella.org>
Maksim Kochkin <maxxarts@gmail.com>
Michael Fogleman <fogleman@gmail.com>
Rémy Oudompheng <oudomphe@phare.normalesup.org>
Roger Peppe <rogpeppe@gmail.com>
Steven Edwards <steven@stephenwithav.com>

38
vendor/github.com/golang/freetype/CONTRIBUTORS generated vendored Normal file
View File

@ -0,0 +1,38 @@
# This is the official list of people who can contribute
# (and typically have contributed) code to the Freetype-Go repository.
# The AUTHORS file lists the copyright holders; this file
# lists people. For example, Google employees are listed here
# but not in AUTHORS, because Google holds the copyright.
#
# The submission process automatically checks to make sure
# that people submitting code are listed in this file (by email address).
#
# Names should be added to this file only after verifying that
# the individual or the individual's organization has agreed to
# the appropriate Contributor License Agreement, found here:
#
# http://code.google.com/legal/individual-cla-v1.0.html
# http://code.google.com/legal/corporate-cla-v1.0.html
#
# The agreement for individuals can be filled out on the web.
#
# When adding J Random Contributor's name to this file,
# either J's name or J's organization's name should be
# added to the AUTHORS file, depending on whether the
# individual or corporate CLA was used.
# Names should be added to this file like so:
# Name <email address>
# Please keep the list sorted.
Andrew Gerrand <adg@golang.org>
Jeff R. Allen <jra@nella.org> <jeff.allen@gmail.com>
Maksim Kochkin <maxxarts@gmail.com>
Michael Fogleman <fogleman@gmail.com>
Nigel Tao <nigeltao@golang.org>
Rémy Oudompheng <oudomphe@phare.normalesup.org> <remyoudompheng@gmail.com>
Rob Pike <r@golang.org>
Roger Peppe <rogpeppe@gmail.com>
Russ Cox <rsc@golang.org>
Steven Edwards <steven@stephenwithav.com>

12
vendor/github.com/golang/freetype/LICENSE generated vendored Normal file
View File

@ -0,0 +1,12 @@
Use of the Freetype-Go software is subject to your choice of exactly one of
the following two licenses:
* The FreeType License, which is similar to the original BSD license with
an advertising clause, or
* The GNU General Public License (GPL), version 2 or later.
The text of these licenses are available in the licenses/ftl.txt and the
licenses/gpl.txt files respectively. They are also available at
http://freetype.sourceforge.net/license.html
The Luxi fonts in the testdata directory are licensed separately. See the
testdata/COPYING file for details.

21
vendor/github.com/golang/freetype/README generated vendored Normal file
View File

@ -0,0 +1,21 @@
The Freetype font rasterizer in the Go programming language.
To download and install from source:
$ go get github.com/golang/freetype
It is an incomplete port:
* It only supports TrueType fonts, and not Type 1 fonts nor bitmap fonts.
* It only supports the Unicode encoding.
There are also some implementation differences:
* It uses a 26.6 fixed point co-ordinate system everywhere internally,
as opposed to the original Freetype's mix of 26.6 (or 10.6 for 16-bit
systems) in some places, and 24.8 in the "smooth" rasterizer.
Freetype-Go is derived from Freetype, which is written in C. Freetype is
copyright 1996-2010 David Turner, Robert Wilhelm, and Werner Lemberg.
Freetype-Go is copyright The Freetype-Go Authors, who are listed in the
AUTHORS file.
Unless otherwise noted, the Freetype-Go source files are distributed
under the BSD-style license found in the LICENSE file.

341
vendor/github.com/golang/freetype/freetype.go generated vendored Normal file
View File

@ -0,0 +1,341 @@
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
// The freetype package provides a convenient API to draw text onto an image.
// Use the freetype/raster and freetype/truetype packages for lower level
// control over rasterization and TrueType parsing.
package freetype // import "github.com/golang/freetype"
import (
"errors"
"image"
"image/draw"
"github.com/golang/freetype/raster"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
// These constants determine the size of the glyph cache. The cache is keyed
// primarily by the glyph index modulo nGlyphs, and secondarily by sub-pixel
// position for the mask image. Sub-pixel positions are quantized to
// nXFractions possible values in both the x and y directions.
const (
nGlyphs = 256
nXFractions = 4
nYFractions = 1
)
// An entry in the glyph cache is keyed explicitly by the glyph index and
// implicitly by the quantized x and y fractional offset. It maps to a mask
// image and an offset.
type cacheEntry struct {
valid bool
glyph truetype.Index
advanceWidth fixed.Int26_6
mask *image.Alpha
offset image.Point
}
// ParseFont just calls the Parse function from the freetype/truetype package.
// It is provided here so that code that imports this package doesn't need
// to also include the freetype/truetype package.
func ParseFont(b []byte) (*truetype.Font, error) {
return truetype.Parse(b)
}
// Pt converts from a co-ordinate pair measured in pixels to a fixed.Point26_6
// co-ordinate pair measured in fixed.Int26_6 units.
func Pt(x, y int) fixed.Point26_6 {
return fixed.Point26_6{
X: fixed.Int26_6(x << 6),
Y: fixed.Int26_6(y << 6),
}
}
// A Context holds the state for drawing text in a given font and size.
type Context struct {
r *raster.Rasterizer
f *truetype.Font
glyphBuf truetype.GlyphBuf
// clip is the clip rectangle for drawing.
clip image.Rectangle
// dst and src are the destination and source images for drawing.
dst draw.Image
src image.Image
// fontSize and dpi are used to calculate scale. scale is the number of
// 26.6 fixed point units in 1 em. hinting is the hinting policy.
fontSize, dpi float64
scale fixed.Int26_6
hinting font.Hinting
// cache is the glyph cache.
cache [nGlyphs * nXFractions * nYFractions]cacheEntry
}
// PointToFixed converts the given number of points (as in "a 12 point font")
// into a 26.6 fixed point number of pixels.
func (c *Context) PointToFixed(x float64) fixed.Int26_6 {
return fixed.Int26_6(x * float64(c.dpi) * (64.0 / 72.0))
}
// drawContour draws the given closed contour with the given offset.
func (c *Context) drawContour(ps []truetype.Point, dx, dy fixed.Int26_6) {
if len(ps) == 0 {
return
}
// The low bit of each point's Flags value is whether the point is on the
// curve. Truetype fonts only have quadratic Bézier curves, not cubics.
// Thus, two consecutive off-curve points imply an on-curve point in the
// middle of those two.
//
// See http://chanae.walon.org/pub/ttf/ttf_glyphs.htm for more details.
// ps[0] is a truetype.Point measured in FUnits and positive Y going
// upwards. start is the same thing measured in fixed point units and
// positive Y going downwards, and offset by (dx, dy).
start := fixed.Point26_6{
X: dx + ps[0].X,
Y: dy - ps[0].Y,
}
others := []truetype.Point(nil)
if ps[0].Flags&0x01 != 0 {
others = ps[1:]
} else {
last := fixed.Point26_6{
X: dx + ps[len(ps)-1].X,
Y: dy - ps[len(ps)-1].Y,
}
if ps[len(ps)-1].Flags&0x01 != 0 {
start = last
others = ps[:len(ps)-1]
} else {
start = fixed.Point26_6{
X: (start.X + last.X) / 2,
Y: (start.Y + last.Y) / 2,
}
others = ps
}
}
c.r.Start(start)
q0, on0 := start, true
for _, p := range others {
q := fixed.Point26_6{
X: dx + p.X,
Y: dy - p.Y,
}
on := p.Flags&0x01 != 0
if on {
if on0 {
c.r.Add1(q)
} else {
c.r.Add2(q0, q)
}
} else {
if on0 {
// No-op.
} else {
mid := fixed.Point26_6{
X: (q0.X + q.X) / 2,
Y: (q0.Y + q.Y) / 2,
}
c.r.Add2(q0, mid)
}
}
q0, on0 = q, on
}
// Close the curve.
if on0 {
c.r.Add1(start)
} else {
c.r.Add2(q0, start)
}
}
// rasterize returns the advance width, glyph mask and integer-pixel offset
// to render the given glyph at the given sub-pixel offsets.
// The 26.6 fixed point arguments fx and fy must be in the range [0, 1).
func (c *Context) rasterize(glyph truetype.Index, fx, fy fixed.Int26_6) (
fixed.Int26_6, *image.Alpha, image.Point, error) {
if err := c.glyphBuf.Load(c.f, c.scale, glyph, c.hinting); err != nil {
return 0, nil, image.Point{}, err
}
// Calculate the integer-pixel bounds for the glyph.
xmin := int(fx+c.glyphBuf.Bounds.Min.X) >> 6
ymin := int(fy-c.glyphBuf.Bounds.Max.Y) >> 6
xmax := int(fx+c.glyphBuf.Bounds.Max.X+0x3f) >> 6
ymax := int(fy-c.glyphBuf.Bounds.Min.Y+0x3f) >> 6
if xmin > xmax || ymin > ymax {
return 0, nil, image.Point{}, errors.New("freetype: negative sized glyph")
}
// A TrueType's glyph's nodes can have negative co-ordinates, but the
// rasterizer clips anything left of x=0 or above y=0. xmin and ymin are
// the pixel offsets, based on the font's FUnit metrics, that let a
// negative co-ordinate in TrueType space be non-negative in rasterizer
// space. xmin and ymin are typically <= 0.
fx -= fixed.Int26_6(xmin << 6)
fy -= fixed.Int26_6(ymin << 6)
// Rasterize the glyph's vectors.
c.r.Clear()
e0 := 0
for _, e1 := range c.glyphBuf.Ends {
c.drawContour(c.glyphBuf.Points[e0:e1], fx, fy)
e0 = e1
}
a := image.NewAlpha(image.Rect(0, 0, xmax-xmin, ymax-ymin))
c.r.Rasterize(raster.NewAlphaSrcPainter(a))
return c.glyphBuf.AdvanceWidth, a, image.Point{xmin, ymin}, nil
}
// glyph returns the advance width, glyph mask and integer-pixel offset to
// render the given glyph at the given sub-pixel point. It is a cache for the
// rasterize method. Unlike rasterize, p's co-ordinates do not have to be in
// the range [0, 1).
func (c *Context) glyph(glyph truetype.Index, p fixed.Point26_6) (
fixed.Int26_6, *image.Alpha, image.Point, error) {
// Split p.X and p.Y into their integer and fractional parts.
ix, fx := int(p.X>>6), p.X&0x3f
iy, fy := int(p.Y>>6), p.Y&0x3f
// Calculate the index t into the cache array.
tg := int(glyph) % nGlyphs
tx := int(fx) / (64 / nXFractions)
ty := int(fy) / (64 / nYFractions)
t := ((tg*nXFractions)+tx)*nYFractions + ty
// Check for a cache hit.
if e := c.cache[t]; e.valid && e.glyph == glyph {
return e.advanceWidth, e.mask, e.offset.Add(image.Point{ix, iy}), nil
}
// Rasterize the glyph and put the result into the cache.
advanceWidth, mask, offset, err := c.rasterize(glyph, fx, fy)
if err != nil {
return 0, nil, image.Point{}, err
}
c.cache[t] = cacheEntry{true, glyph, advanceWidth, mask, offset}
return advanceWidth, mask, offset.Add(image.Point{ix, iy}), nil
}
// DrawString draws s at p and returns p advanced by the text extent. The text
// is placed so that the left edge of the em square of the first character of s
// and the baseline intersect at p. The majority of the affected pixels will be
// above and to the right of the point, but some may be below or to the left.
// For example, drawing a string that starts with a 'J' in an italic font may
// affect pixels below and left of the point.
//
// p is a fixed.Point26_6 and can therefore represent sub-pixel positions.
func (c *Context) DrawString(s string, p fixed.Point26_6) (fixed.Point26_6, error) {
if c.f == nil {
return fixed.Point26_6{}, errors.New("freetype: DrawText called with a nil font")
}
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := c.f.Index(rune)
if hasPrev {
kern := c.f.Kern(c.scale, prev, index)
if c.hinting != font.HintingNone {
kern = (kern + 32) &^ 63
}
p.X += kern
}
advanceWidth, mask, offset, err := c.glyph(index, p)
if err != nil {
return fixed.Point26_6{}, err
}
p.X += advanceWidth
glyphRect := mask.Bounds().Add(offset)
dr := c.clip.Intersect(glyphRect)
if !dr.Empty() {
mp := image.Point{0, dr.Min.Y - glyphRect.Min.Y}
draw.DrawMask(c.dst, dr, c.src, image.ZP, mask, mp, draw.Over)
}
prev, hasPrev = index, true
}
return p, nil
}
// recalc recalculates scale and bounds values from the font size, screen
// resolution and font metrics, and invalidates the glyph cache.
func (c *Context) recalc() {
c.scale = fixed.Int26_6(c.fontSize * c.dpi * (64.0 / 72.0))
if c.f == nil {
c.r.SetBounds(0, 0)
} else {
// Set the rasterizer's bounds to be big enough to handle the largest glyph.
b := c.f.Bounds(c.scale)
xmin := +int(b.Min.X) >> 6
ymin := -int(b.Max.Y) >> 6
xmax := +int(b.Max.X+63) >> 6
ymax := -int(b.Min.Y-63) >> 6
c.r.SetBounds(xmax-xmin, ymax-ymin)
}
for i := range c.cache {
c.cache[i] = cacheEntry{}
}
}
// SetDPI sets the screen resolution in dots per inch.
func (c *Context) SetDPI(dpi float64) {
if c.dpi == dpi {
return
}
c.dpi = dpi
c.recalc()
}
// SetFont sets the font used to draw text.
func (c *Context) SetFont(f *truetype.Font) {
if c.f == f {
return
}
c.f = f
c.recalc()
}
// SetFontSize sets the font size in points (as in "a 12 point font").
func (c *Context) SetFontSize(fontSize float64) {
if c.fontSize == fontSize {
return
}
c.fontSize = fontSize
c.recalc()
}
// SetHinting sets the hinting policy.
func (c *Context) SetHinting(hinting font.Hinting) {
c.hinting = hinting
for i := range c.cache {
c.cache[i] = cacheEntry{}
}
}
// SetDst sets the destination image for draw operations.
func (c *Context) SetDst(dst draw.Image) {
c.dst = dst
}
// SetSrc sets the source image for draw operations. This is typically an
// image.Uniform.
func (c *Context) SetSrc(src image.Image) {
c.src = src
}
// SetClip sets the clip rectangle for drawing.
func (c *Context) SetClip(clip image.Rectangle) {
c.clip = clip
}
// TODO(nigeltao): implement Context.SetGamma.
// NewContext creates a new Context.
func NewContext() *Context {
return &Context{
r: raster.NewRasterizer(0, 0),
fontSize: 12,
dpi: 72,
scale: 12 << 6,
}
}

245
vendor/github.com/golang/freetype/raster/geom.go generated vendored Normal file
View File

@ -0,0 +1,245 @@
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
package raster
import (
"fmt"
"math"
"golang.org/x/image/math/fixed"
)
// maxAbs returns the maximum of abs(a) and abs(b).
func maxAbs(a, b fixed.Int26_6) fixed.Int26_6 {
if a < 0 {
a = -a
}
if b < 0 {
b = -b
}
if a < b {
return b
}
return a
}
// pNeg returns the vector -p, or equivalently p rotated by 180 degrees.
func pNeg(p fixed.Point26_6) fixed.Point26_6 {
return fixed.Point26_6{-p.X, -p.Y}
}
// pDot returns the dot product p·q.
func pDot(p fixed.Point26_6, q fixed.Point26_6) fixed.Int52_12 {
px, py := int64(p.X), int64(p.Y)
qx, qy := int64(q.X), int64(q.Y)
return fixed.Int52_12(px*qx + py*qy)
}
// pLen returns the length of the vector p.
func pLen(p fixed.Point26_6) fixed.Int26_6 {
// TODO(nigeltao): use fixed point math.
x := float64(p.X)
y := float64(p.Y)
return fixed.Int26_6(math.Sqrt(x*x + y*y))
}
// pNorm returns the vector p normalized to the given length, or zero if p is
// degenerate.
func pNorm(p fixed.Point26_6, length fixed.Int26_6) fixed.Point26_6 {
d := pLen(p)
if d == 0 {
return fixed.Point26_6{}
}
s, t := int64(length), int64(d)
x := int64(p.X) * s / t
y := int64(p.Y) * s / t
return fixed.Point26_6{fixed.Int26_6(x), fixed.Int26_6(y)}
}
// pRot45CW returns the vector p rotated clockwise by 45 degrees.
//
// Note that the Y-axis grows downwards, so {1, 0}.Rot45CW is {1/√2, 1/√2}.
func pRot45CW(p fixed.Point26_6) fixed.Point26_6 {
// 181/256 is approximately 1/√2, or sin(π/4).
px, py := int64(p.X), int64(p.Y)
qx := (+px - py) * 181 / 256
qy := (+px + py) * 181 / 256
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
}
// pRot90CW returns the vector p rotated clockwise by 90 degrees.
//
// Note that the Y-axis grows downwards, so {1, 0}.Rot90CW is {0, 1}.
func pRot90CW(p fixed.Point26_6) fixed.Point26_6 {
return fixed.Point26_6{-p.Y, p.X}
}
// pRot135CW returns the vector p rotated clockwise by 135 degrees.
//
// Note that the Y-axis grows downwards, so {1, 0}.Rot135CW is {-1/√2, 1/√2}.
func pRot135CW(p fixed.Point26_6) fixed.Point26_6 {
// 181/256 is approximately 1/√2, or sin(π/4).
px, py := int64(p.X), int64(p.Y)
qx := (-px - py) * 181 / 256
qy := (+px - py) * 181 / 256
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
}
// pRot45CCW returns the vector p rotated counter-clockwise by 45 degrees.
//
// Note that the Y-axis grows downwards, so {1, 0}.Rot45CCW is {1/√2, -1/√2}.
func pRot45CCW(p fixed.Point26_6) fixed.Point26_6 {
// 181/256 is approximately 1/√2, or sin(π/4).
px, py := int64(p.X), int64(p.Y)
qx := (+px + py) * 181 / 256
qy := (-px + py) * 181 / 256
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
}
// pRot90CCW returns the vector p rotated counter-clockwise by 90 degrees.
//
// Note that the Y-axis grows downwards, so {1, 0}.Rot90CCW is {0, -1}.
func pRot90CCW(p fixed.Point26_6) fixed.Point26_6 {
return fixed.Point26_6{p.Y, -p.X}
}
// pRot135CCW returns the vector p rotated counter-clockwise by 135 degrees.
//
// Note that the Y-axis grows downwards, so {1, 0}.Rot135CCW is {-1/√2, -1/√2}.
func pRot135CCW(p fixed.Point26_6) fixed.Point26_6 {
// 181/256 is approximately 1/√2, or sin(π/4).
px, py := int64(p.X), int64(p.Y)
qx := (-px + py) * 181 / 256
qy := (-px - py) * 181 / 256
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
}
// An Adder accumulates points on a curve.
type Adder interface {
// Start starts a new curve at the given point.
Start(a fixed.Point26_6)
// Add1 adds a linear segment to the current curve.
Add1(b fixed.Point26_6)
// Add2 adds a quadratic segment to the current curve.
Add2(b, c fixed.Point26_6)
// Add3 adds a cubic segment to the current curve.
Add3(b, c, d fixed.Point26_6)
}
// A Path is a sequence of curves, and a curve is a start point followed by a
// sequence of linear, quadratic or cubic segments.
type Path []fixed.Int26_6
// String returns a human-readable representation of a Path.
func (p Path) String() string {
s := ""
for i := 0; i < len(p); {
if i != 0 {
s += " "
}
switch p[i] {
case 0:
s += "S0" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+3]))
i += 4
case 1:
s += "A1" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+3]))
i += 4
case 2:
s += "A2" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+5]))
i += 6
case 3:
s += "A3" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+7]))
i += 8
default:
panic("freetype/raster: bad path")
}
}
return s
}
// Clear cancels any previous calls to p.Start or p.AddXxx.
func (p *Path) Clear() {
*p = (*p)[:0]
}
// Start starts a new curve at the given point.
func (p *Path) Start(a fixed.Point26_6) {
*p = append(*p, 0, a.X, a.Y, 0)
}
// Add1 adds a linear segment to the current curve.
func (p *Path) Add1(b fixed.Point26_6) {
*p = append(*p, 1, b.X, b.Y, 1)
}
// Add2 adds a quadratic segment to the current curve.
func (p *Path) Add2(b, c fixed.Point26_6) {
*p = append(*p, 2, b.X, b.Y, c.X, c.Y, 2)
}
// Add3 adds a cubic segment to the current curve.
func (p *Path) Add3(b, c, d fixed.Point26_6) {
*p = append(*p, 3, b.X, b.Y, c.X, c.Y, d.X, d.Y, 3)
}
// AddPath adds the Path q to p.
func (p *Path) AddPath(q Path) {
*p = append(*p, q...)
}
// AddStroke adds a stroked Path.
func (p *Path) AddStroke(q Path, width fixed.Int26_6, cr Capper, jr Joiner) {
Stroke(p, q, width, cr, jr)
}
// firstPoint returns the first point in a non-empty Path.
func (p Path) firstPoint() fixed.Point26_6 {
return fixed.Point26_6{p[1], p[2]}
}
// lastPoint returns the last point in a non-empty Path.
func (p Path) lastPoint() fixed.Point26_6 {
return fixed.Point26_6{p[len(p)-3], p[len(p)-2]}
}
// addPathReversed adds q reversed to p.
// For example, if q consists of a linear segment from A to B followed by a
// quadratic segment from B to C to D, then the values of q looks like:
// index: 01234567890123
// value: 0AA01BB12CCDD2
// So, when adding q backwards to p, we want to Add2(C, B) followed by Add1(A).
func addPathReversed(p Adder, q Path) {
if len(q) == 0 {
return
}
i := len(q) - 1
for {
switch q[i] {
case 0:
return
case 1:
i -= 4
p.Add1(
fixed.Point26_6{q[i-2], q[i-1]},
)
case 2:
i -= 6
p.Add2(
fixed.Point26_6{q[i+2], q[i+3]},
fixed.Point26_6{q[i-2], q[i-1]},
)
case 3:
i -= 8
p.Add3(
fixed.Point26_6{q[i+4], q[i+5]},
fixed.Point26_6{q[i+2], q[i+3]},
fixed.Point26_6{q[i-2], q[i-1]},
)
default:
panic("freetype/raster: bad path")
}
}
}

287
vendor/github.com/golang/freetype/raster/paint.go generated vendored Normal file
View File

@ -0,0 +1,287 @@
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
package raster
import (
"image"
"image/color"
"image/draw"
"math"
)
// A Span is a horizontal segment of pixels with constant alpha. X0 is an
// inclusive bound and X1 is exclusive, the same as for slices. A fully opaque
// Span has Alpha == 0xffff.
type Span struct {
Y, X0, X1 int
Alpha uint32
}
// A Painter knows how to paint a batch of Spans. Rasterization may involve
// Painting multiple batches, and done will be true for the final batch. The
// Spans' Y values are monotonically increasing during a rasterization. Paint
// may use all of ss as scratch space during the call.
type Painter interface {
Paint(ss []Span, done bool)
}
// The PainterFunc type adapts an ordinary function to the Painter interface.
type PainterFunc func(ss []Span, done bool)
// Paint just delegates the call to f.
func (f PainterFunc) Paint(ss []Span, done bool) { f(ss, done) }
// An AlphaOverPainter is a Painter that paints Spans onto a *image.Alpha using
// the Over Porter-Duff composition operator.
type AlphaOverPainter struct {
Image *image.Alpha
}
// Paint satisfies the Painter interface.
func (r AlphaOverPainter) Paint(ss []Span, done bool) {
b := r.Image.Bounds()
for _, s := range ss {
if s.Y < b.Min.Y {
continue
}
if s.Y >= b.Max.Y {
return
}
if s.X0 < b.Min.X {
s.X0 = b.Min.X
}
if s.X1 > b.Max.X {
s.X1 = b.Max.X
}
if s.X0 >= s.X1 {
continue
}
base := (s.Y-r.Image.Rect.Min.Y)*r.Image.Stride - r.Image.Rect.Min.X
p := r.Image.Pix[base+s.X0 : base+s.X1]
a := int(s.Alpha >> 8)
for i, c := range p {
v := int(c)
p[i] = uint8((v*255 + (255-v)*a) / 255)
}
}
}
// NewAlphaOverPainter creates a new AlphaOverPainter for the given image.
func NewAlphaOverPainter(m *image.Alpha) AlphaOverPainter {
return AlphaOverPainter{m}
}
// An AlphaSrcPainter is a Painter that paints Spans onto a *image.Alpha using
// the Src Porter-Duff composition operator.
type AlphaSrcPainter struct {
Image *image.Alpha
}
// Paint satisfies the Painter interface.
func (r AlphaSrcPainter) Paint(ss []Span, done bool) {
b := r.Image.Bounds()
for _, s := range ss {
if s.Y < b.Min.Y {
continue
}
if s.Y >= b.Max.Y {
return
}
if s.X0 < b.Min.X {
s.X0 = b.Min.X
}
if s.X1 > b.Max.X {
s.X1 = b.Max.X
}
if s.X0 >= s.X1 {
continue
}
base := (s.Y-r.Image.Rect.Min.Y)*r.Image.Stride - r.Image.Rect.Min.X
p := r.Image.Pix[base+s.X0 : base+s.X1]
color := uint8(s.Alpha >> 8)
for i := range p {
p[i] = color
}
}
}
// NewAlphaSrcPainter creates a new AlphaSrcPainter for the given image.
func NewAlphaSrcPainter(m *image.Alpha) AlphaSrcPainter {
return AlphaSrcPainter{m}
}
// An RGBAPainter is a Painter that paints Spans onto a *image.RGBA.
type RGBAPainter struct {
// Image is the image to compose onto.
Image *image.RGBA
// Op is the Porter-Duff composition operator.
Op draw.Op
// cr, cg, cb and ca are the 16-bit color to paint the spans.
cr, cg, cb, ca uint32
}
// Paint satisfies the Painter interface.
func (r *RGBAPainter) Paint(ss []Span, done bool) {
b := r.Image.Bounds()
for _, s := range ss {
if s.Y < b.Min.Y {
continue
}
if s.Y >= b.Max.Y {
return
}
if s.X0 < b.Min.X {
s.X0 = b.Min.X
}
if s.X1 > b.Max.X {
s.X1 = b.Max.X
}
if s.X0 >= s.X1 {
continue
}
// This code mimics drawGlyphOver in $GOROOT/src/image/draw/draw.go.
ma := s.Alpha
const m = 1<<16 - 1
i0 := (s.Y-r.Image.Rect.Min.Y)*r.Image.Stride + (s.X0-r.Image.Rect.Min.X)*4
i1 := i0 + (s.X1-s.X0)*4
if r.Op == draw.Over {
for i := i0; i < i1; i += 4 {
dr := uint32(r.Image.Pix[i+0])
dg := uint32(r.Image.Pix[i+1])
db := uint32(r.Image.Pix[i+2])
da := uint32(r.Image.Pix[i+3])
a := (m - (r.ca * ma / m)) * 0x101
r.Image.Pix[i+0] = uint8((dr*a + r.cr*ma) / m >> 8)
r.Image.Pix[i+1] = uint8((dg*a + r.cg*ma) / m >> 8)
r.Image.Pix[i+2] = uint8((db*a + r.cb*ma) / m >> 8)
r.Image.Pix[i+3] = uint8((da*a + r.ca*ma) / m >> 8)
}
} else {
for i := i0; i < i1; i += 4 {
r.Image.Pix[i+0] = uint8(r.cr * ma / m >> 8)
r.Image.Pix[i+1] = uint8(r.cg * ma / m >> 8)
r.Image.Pix[i+2] = uint8(r.cb * ma / m >> 8)
r.Image.Pix[i+3] = uint8(r.ca * ma / m >> 8)
}
}
}
}
// SetColor sets the color to paint the spans.
func (r *RGBAPainter) SetColor(c color.Color) {
r.cr, r.cg, r.cb, r.ca = c.RGBA()
}
// NewRGBAPainter creates a new RGBAPainter for the given image.
func NewRGBAPainter(m *image.RGBA) *RGBAPainter {
return &RGBAPainter{Image: m}
}
// A MonochromePainter wraps another Painter, quantizing each Span's alpha to
// be either fully opaque or fully transparent.
type MonochromePainter struct {
Painter Painter
y, x0, x1 int
}
// Paint delegates to the wrapped Painter after quantizing each Span's alpha
// value and merging adjacent fully opaque Spans.
func (m *MonochromePainter) Paint(ss []Span, done bool) {
// We compact the ss slice, discarding any Spans whose alpha quantizes to zero.
j := 0
for _, s := range ss {
if s.Alpha >= 0x8000 {
if m.y == s.Y && m.x1 == s.X0 {
m.x1 = s.X1
} else {
ss[j] = Span{m.y, m.x0, m.x1, 1<<16 - 1}
j++
m.y, m.x0, m.x1 = s.Y, s.X0, s.X1
}
}
}
if done {
// Flush the accumulated Span.
finalSpan := Span{m.y, m.x0, m.x1, 1<<16 - 1}
if j < len(ss) {
ss[j] = finalSpan
j++
m.Painter.Paint(ss[:j], true)
} else if j == len(ss) {
m.Painter.Paint(ss, false)
if cap(ss) > 0 {
ss = ss[:1]
} else {
ss = make([]Span, 1)
}
ss[0] = finalSpan
m.Painter.Paint(ss, true)
} else {
panic("unreachable")
}
// Reset the accumulator, so that this Painter can be re-used.
m.y, m.x0, m.x1 = 0, 0, 0
} else {
m.Painter.Paint(ss[:j], false)
}
}
// NewMonochromePainter creates a new MonochromePainter that wraps the given
// Painter.
func NewMonochromePainter(p Painter) *MonochromePainter {
return &MonochromePainter{Painter: p}
}
// A GammaCorrectionPainter wraps another Painter, performing gamma-correction
// on each Span's alpha value.
type GammaCorrectionPainter struct {
// Painter is the wrapped Painter.
Painter Painter
// a is the precomputed alpha values for linear interpolation, with fully
// opaque == 0xffff.
a [256]uint16
// gammaIsOne is whether gamma correction is a no-op.
gammaIsOne bool
}
// Paint delegates to the wrapped Painter after performing gamma-correction on
// each Span.
func (g *GammaCorrectionPainter) Paint(ss []Span, done bool) {
if !g.gammaIsOne {
const n = 0x101
for i, s := range ss {
if s.Alpha == 0 || s.Alpha == 0xffff {
continue
}
p, q := s.Alpha/n, s.Alpha%n
// The resultant alpha is a linear interpolation of g.a[p] and g.a[p+1].
a := uint32(g.a[p])*(n-q) + uint32(g.a[p+1])*q
ss[i].Alpha = (a + n/2) / n
}
}
g.Painter.Paint(ss, done)
}
// SetGamma sets the gamma value.
func (g *GammaCorrectionPainter) SetGamma(gamma float64) {
g.gammaIsOne = gamma == 1
if g.gammaIsOne {
return
}
for i := 0; i < 256; i++ {
a := float64(i) / 0xff
a = math.Pow(a, gamma)
g.a[i] = uint16(0xffff * a)
}
}
// NewGammaCorrectionPainter creates a new GammaCorrectionPainter that wraps
// the given Painter.
func NewGammaCorrectionPainter(p Painter, gamma float64) *GammaCorrectionPainter {
g := &GammaCorrectionPainter{Painter: p}
g.SetGamma(gamma)
return g
}

601
vendor/github.com/golang/freetype/raster/raster.go generated vendored Normal file
View File

@ -0,0 +1,601 @@
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
// Package raster provides an anti-aliasing 2-D rasterizer.
//
// It is part of the larger Freetype suite of font-related packages, but the
// raster package is not specific to font rasterization, and can be used
// standalone without any other Freetype package.
//
// Rasterization is done by the same area/coverage accumulation algorithm as
// the Freetype "smooth" module, and the Anti-Grain Geometry library. A
// description of the area/coverage algorithm is at
// http://projects.tuxee.net/cl-vectors/section-the-cl-aa-algorithm
package raster // import "github.com/golang/freetype/raster"
import (
"strconv"
"golang.org/x/image/math/fixed"
)
// A cell is part of a linked list (for a given yi co-ordinate) of accumulated
// area/coverage for the pixel at (xi, yi).
type cell struct {
xi int
area, cover int
next int
}
type Rasterizer struct {
// If false, the default behavior is to use the even-odd winding fill
// rule during Rasterize.
UseNonZeroWinding bool
// An offset (in pixels) to the painted spans.
Dx, Dy int
// The width of the Rasterizer. The height is implicit in len(cellIndex).
width int
// splitScaleN is the scaling factor used to determine how many times
// to decompose a quadratic or cubic segment into a linear approximation.
splitScale2, splitScale3 int
// The current pen position.
a fixed.Point26_6
// The current cell and its area/coverage being accumulated.
xi, yi int
area, cover int
// Saved cells.
cell []cell
// Linked list of cells, one per row.
cellIndex []int
// Buffers.
cellBuf [256]cell
cellIndexBuf [64]int
spanBuf [64]Span
}
// findCell returns the index in r.cell for the cell corresponding to
// (r.xi, r.yi). The cell is created if necessary.
func (r *Rasterizer) findCell() int {
if r.yi < 0 || r.yi >= len(r.cellIndex) {
return -1
}
xi := r.xi
if xi < 0 {
xi = -1
} else if xi > r.width {
xi = r.width
}
i, prev := r.cellIndex[r.yi], -1
for i != -1 && r.cell[i].xi <= xi {
if r.cell[i].xi == xi {
return i
}
i, prev = r.cell[i].next, i
}
c := len(r.cell)
if c == cap(r.cell) {
buf := make([]cell, c, 4*c)
copy(buf, r.cell)
r.cell = buf[0 : c+1]
} else {
r.cell = r.cell[0 : c+1]
}
r.cell[c] = cell{xi, 0, 0, i}
if prev == -1 {
r.cellIndex[r.yi] = c
} else {
r.cell[prev].next = c
}
return c
}
// saveCell saves any accumulated r.area/r.cover for (r.xi, r.yi).
func (r *Rasterizer) saveCell() {
if r.area != 0 || r.cover != 0 {
i := r.findCell()
if i != -1 {
r.cell[i].area += r.area
r.cell[i].cover += r.cover
}
r.area = 0
r.cover = 0
}
}
// setCell sets the (xi, yi) cell that r is accumulating area/coverage for.
func (r *Rasterizer) setCell(xi, yi int) {
if r.xi != xi || r.yi != yi {
r.saveCell()
r.xi, r.yi = xi, yi
}
}
// scan accumulates area/coverage for the yi'th scanline, going from
// x0 to x1 in the horizontal direction (in 26.6 fixed point co-ordinates)
// and from y0f to y1f fractional vertical units within that scanline.
func (r *Rasterizer) scan(yi int, x0, y0f, x1, y1f fixed.Int26_6) {
// Break the 26.6 fixed point X co-ordinates into integral and fractional parts.
x0i := int(x0) / 64
x0f := x0 - fixed.Int26_6(64*x0i)
x1i := int(x1) / 64
x1f := x1 - fixed.Int26_6(64*x1i)
// A perfectly horizontal scan.
if y0f == y1f {
r.setCell(x1i, yi)
return
}
dx, dy := x1-x0, y1f-y0f
// A single cell scan.
if x0i == x1i {
r.area += int((x0f + x1f) * dy)
r.cover += int(dy)
return
}
// There are at least two cells. Apart from the first and last cells,
// all intermediate cells go through the full width of the cell,
// or 64 units in 26.6 fixed point format.
var (
p, q, edge0, edge1 fixed.Int26_6
xiDelta int
)
if dx > 0 {
p, q = (64-x0f)*dy, dx
edge0, edge1, xiDelta = 0, 64, 1
} else {
p, q = x0f*dy, -dx
edge0, edge1, xiDelta = 64, 0, -1
}
yDelta, yRem := p/q, p%q
if yRem < 0 {
yDelta -= 1
yRem += q
}
// Do the first cell.
xi, y := x0i, y0f
r.area += int((x0f + edge1) * yDelta)
r.cover += int(yDelta)
xi, y = xi+xiDelta, y+yDelta
r.setCell(xi, yi)
if xi != x1i {
// Do all the intermediate cells.
p = 64 * (y1f - y + yDelta)
fullDelta, fullRem := p/q, p%q
if fullRem < 0 {
fullDelta -= 1
fullRem += q
}
yRem -= q
for xi != x1i {
yDelta = fullDelta
yRem += fullRem
if yRem >= 0 {
yDelta += 1
yRem -= q
}
r.area += int(64 * yDelta)
r.cover += int(yDelta)
xi, y = xi+xiDelta, y+yDelta
r.setCell(xi, yi)
}
}
// Do the last cell.
yDelta = y1f - y
r.area += int((edge0 + x1f) * yDelta)
r.cover += int(yDelta)
}
// Start starts a new curve at the given point.
func (r *Rasterizer) Start(a fixed.Point26_6) {
r.setCell(int(a.X/64), int(a.Y/64))
r.a = a
}
// Add1 adds a linear segment to the current curve.
func (r *Rasterizer) Add1(b fixed.Point26_6) {
x0, y0 := r.a.X, r.a.Y
x1, y1 := b.X, b.Y
dx, dy := x1-x0, y1-y0
// Break the 26.6 fixed point Y co-ordinates into integral and fractional
// parts.
y0i := int(y0) / 64
y0f := y0 - fixed.Int26_6(64*y0i)
y1i := int(y1) / 64
y1f := y1 - fixed.Int26_6(64*y1i)
if y0i == y1i {
// There is only one scanline.
r.scan(y0i, x0, y0f, x1, y1f)
} else if dx == 0 {
// This is a vertical line segment. We avoid calling r.scan and instead
// manipulate r.area and r.cover directly.
var (
edge0, edge1 fixed.Int26_6
yiDelta int
)
if dy > 0 {
edge0, edge1, yiDelta = 0, 64, 1
} else {
edge0, edge1, yiDelta = 64, 0, -1
}
x0i, yi := int(x0)/64, y0i
x0fTimes2 := (int(x0) - (64 * x0i)) * 2
// Do the first pixel.
dcover := int(edge1 - y0f)
darea := int(x0fTimes2 * dcover)
r.area += darea
r.cover += dcover
yi += yiDelta
r.setCell(x0i, yi)
// Do all the intermediate pixels.
dcover = int(edge1 - edge0)
darea = int(x0fTimes2 * dcover)
for yi != y1i {
r.area += darea
r.cover += dcover
yi += yiDelta
r.setCell(x0i, yi)
}
// Do the last pixel.
dcover = int(y1f - edge0)
darea = int(x0fTimes2 * dcover)
r.area += darea
r.cover += dcover
} else {
// There are at least two scanlines. Apart from the first and last
// scanlines, all intermediate scanlines go through the full height of
// the row, or 64 units in 26.6 fixed point format.
var (
p, q, edge0, edge1 fixed.Int26_6
yiDelta int
)
if dy > 0 {
p, q = (64-y0f)*dx, dy
edge0, edge1, yiDelta = 0, 64, 1
} else {
p, q = y0f*dx, -dy
edge0, edge1, yiDelta = 64, 0, -1
}
xDelta, xRem := p/q, p%q
if xRem < 0 {
xDelta -= 1
xRem += q
}
// Do the first scanline.
x, yi := x0, y0i
r.scan(yi, x, y0f, x+xDelta, edge1)
x, yi = x+xDelta, yi+yiDelta
r.setCell(int(x)/64, yi)
if yi != y1i {
// Do all the intermediate scanlines.
p = 64 * dx
fullDelta, fullRem := p/q, p%q
if fullRem < 0 {
fullDelta -= 1
fullRem += q
}
xRem -= q
for yi != y1i {
xDelta = fullDelta
xRem += fullRem
if xRem >= 0 {
xDelta += 1
xRem -= q
}
r.scan(yi, x, edge0, x+xDelta, edge1)
x, yi = x+xDelta, yi+yiDelta
r.setCell(int(x)/64, yi)
}
}
// Do the last scanline.
r.scan(yi, x, edge0, x1, y1f)
}
// The next lineTo starts from b.
r.a = b
}
// Add2 adds a quadratic segment to the current curve.
func (r *Rasterizer) Add2(b, c fixed.Point26_6) {
// Calculate nSplit (the number of recursive decompositions) based on how
// 'curvy' it is. Specifically, how much the middle point b deviates from
// (a+c)/2.
dev := maxAbs(r.a.X-2*b.X+c.X, r.a.Y-2*b.Y+c.Y) / fixed.Int26_6(r.splitScale2)
nsplit := 0
for dev > 0 {
dev /= 4
nsplit++
}
// dev is 32-bit, and nsplit++ every time we shift off 2 bits, so maxNsplit
// is 16.
const maxNsplit = 16
if nsplit > maxNsplit {
panic("freetype/raster: Add2 nsplit too large: " + strconv.Itoa(nsplit))
}
// Recursively decompose the curve nSplit levels deep.
var (
pStack [2*maxNsplit + 3]fixed.Point26_6
sStack [maxNsplit + 1]int
i int
)
sStack[0] = nsplit
pStack[0] = c
pStack[1] = b
pStack[2] = r.a
for i >= 0 {
s := sStack[i]
p := pStack[2*i:]
if s > 0 {
// Split the quadratic curve p[:3] into an equivalent set of two
// shorter curves: p[:3] and p[2:5]. The new p[4] is the old p[2],
// and p[0] is unchanged.
mx := p[1].X
p[4].X = p[2].X
p[3].X = (p[4].X + mx) / 2
p[1].X = (p[0].X + mx) / 2
p[2].X = (p[1].X + p[3].X) / 2
my := p[1].Y
p[4].Y = p[2].Y
p[3].Y = (p[4].Y + my) / 2
p[1].Y = (p[0].Y + my) / 2
p[2].Y = (p[1].Y + p[3].Y) / 2
// The two shorter curves have one less split to do.
sStack[i] = s - 1
sStack[i+1] = s - 1
i++
} else {
// Replace the level-0 quadratic with a two-linear-piece
// approximation.
midx := (p[0].X + 2*p[1].X + p[2].X) / 4
midy := (p[0].Y + 2*p[1].Y + p[2].Y) / 4
r.Add1(fixed.Point26_6{midx, midy})
r.Add1(p[0])
i--
}
}
}
// Add3 adds a cubic segment to the current curve.
func (r *Rasterizer) Add3(b, c, d fixed.Point26_6) {
// Calculate nSplit (the number of recursive decompositions) based on how
// 'curvy' it is.
dev2 := maxAbs(r.a.X-3*(b.X+c.X)+d.X, r.a.Y-3*(b.Y+c.Y)+d.Y) / fixed.Int26_6(r.splitScale2)
dev3 := maxAbs(r.a.X-2*b.X+d.X, r.a.Y-2*b.Y+d.Y) / fixed.Int26_6(r.splitScale3)
nsplit := 0
for dev2 > 0 || dev3 > 0 {
dev2 /= 8
dev3 /= 4
nsplit++
}
// devN is 32-bit, and nsplit++ every time we shift off 2 bits, so
// maxNsplit is 16.
const maxNsplit = 16
if nsplit > maxNsplit {
panic("freetype/raster: Add3 nsplit too large: " + strconv.Itoa(nsplit))
}
// Recursively decompose the curve nSplit levels deep.
var (
pStack [3*maxNsplit + 4]fixed.Point26_6
sStack [maxNsplit + 1]int
i int
)
sStack[0] = nsplit
pStack[0] = d
pStack[1] = c
pStack[2] = b
pStack[3] = r.a
for i >= 0 {
s := sStack[i]
p := pStack[3*i:]
if s > 0 {
// Split the cubic curve p[:4] into an equivalent set of two
// shorter curves: p[:4] and p[3:7]. The new p[6] is the old p[3],
// and p[0] is unchanged.
m01x := (p[0].X + p[1].X) / 2
m12x := (p[1].X + p[2].X) / 2
m23x := (p[2].X + p[3].X) / 2
p[6].X = p[3].X
p[5].X = m23x
p[1].X = m01x
p[2].X = (m01x + m12x) / 2
p[4].X = (m12x + m23x) / 2
p[3].X = (p[2].X + p[4].X) / 2
m01y := (p[0].Y + p[1].Y) / 2
m12y := (p[1].Y + p[2].Y) / 2
m23y := (p[2].Y + p[3].Y) / 2
p[6].Y = p[3].Y
p[5].Y = m23y
p[1].Y = m01y
p[2].Y = (m01y + m12y) / 2
p[4].Y = (m12y + m23y) / 2
p[3].Y = (p[2].Y + p[4].Y) / 2
// The two shorter curves have one less split to do.
sStack[i] = s - 1
sStack[i+1] = s - 1
i++
} else {
// Replace the level-0 cubic with a two-linear-piece approximation.
midx := (p[0].X + 3*(p[1].X+p[2].X) + p[3].X) / 8
midy := (p[0].Y + 3*(p[1].Y+p[2].Y) + p[3].Y) / 8
r.Add1(fixed.Point26_6{midx, midy})
r.Add1(p[0])
i--
}
}
}
// AddPath adds the given Path.
func (r *Rasterizer) AddPath(p Path) {
for i := 0; i < len(p); {
switch p[i] {
case 0:
r.Start(
fixed.Point26_6{p[i+1], p[i+2]},
)
i += 4
case 1:
r.Add1(
fixed.Point26_6{p[i+1], p[i+2]},
)
i += 4
case 2:
r.Add2(
fixed.Point26_6{p[i+1], p[i+2]},
fixed.Point26_6{p[i+3], p[i+4]},
)
i += 6
case 3:
r.Add3(
fixed.Point26_6{p[i+1], p[i+2]},
fixed.Point26_6{p[i+3], p[i+4]},
fixed.Point26_6{p[i+5], p[i+6]},
)
i += 8
default:
panic("freetype/raster: bad path")
}
}
}
// AddStroke adds a stroked Path.
func (r *Rasterizer) AddStroke(q Path, width fixed.Int26_6, cr Capper, jr Joiner) {
Stroke(r, q, width, cr, jr)
}
// areaToAlpha converts an area value to a uint32 alpha value. A completely
// filled pixel corresponds to an area of 64*64*2, and an alpha of 0xffff. The
// conversion of area values greater than this depends on the winding rule:
// even-odd or non-zero.
func (r *Rasterizer) areaToAlpha(area int) uint32 {
// The C Freetype implementation (version 2.3.12) does "alpha := area>>1"
// without the +1. Round-to-nearest gives a more symmetric result than
// round-down. The C implementation also returns 8-bit alpha, not 16-bit
// alpha.
a := (area + 1) >> 1
if a < 0 {
a = -a
}
alpha := uint32(a)
if r.UseNonZeroWinding {
if alpha > 0x0fff {
alpha = 0x0fff
}
} else {
alpha &= 0x1fff
if alpha > 0x1000 {
alpha = 0x2000 - alpha
} else if alpha == 0x1000 {
alpha = 0x0fff
}
}
// alpha is now in the range [0x0000, 0x0fff]. Convert that 12-bit alpha to
// 16-bit alpha.
return alpha<<4 | alpha>>8
}
// Rasterize converts r's accumulated curves into Spans for p. The Spans passed
// to p are non-overlapping, and sorted by Y and then X. They all have non-zero
// width (and 0 <= X0 < X1 <= r.width) and non-zero A, except for the final
// Span, which has Y, X0, X1 and A all equal to zero.
func (r *Rasterizer) Rasterize(p Painter) {
r.saveCell()
s := 0
for yi := 0; yi < len(r.cellIndex); yi++ {
xi, cover := 0, 0
for c := r.cellIndex[yi]; c != -1; c = r.cell[c].next {
if cover != 0 && r.cell[c].xi > xi {
alpha := r.areaToAlpha(cover * 64 * 2)
if alpha != 0 {
xi0, xi1 := xi, r.cell[c].xi
if xi0 < 0 {
xi0 = 0
}
if xi1 >= r.width {
xi1 = r.width
}
if xi0 < xi1 {
r.spanBuf[s] = Span{yi + r.Dy, xi0 + r.Dx, xi1 + r.Dx, alpha}
s++
}
}
}
cover += r.cell[c].cover
alpha := r.areaToAlpha(cover*64*2 - r.cell[c].area)
xi = r.cell[c].xi + 1
if alpha != 0 {
xi0, xi1 := r.cell[c].xi, xi
if xi0 < 0 {
xi0 = 0
}
if xi1 >= r.width {
xi1 = r.width
}
if xi0 < xi1 {
r.spanBuf[s] = Span{yi + r.Dy, xi0 + r.Dx, xi1 + r.Dx, alpha}
s++
}
}
if s > len(r.spanBuf)-2 {
p.Paint(r.spanBuf[:s], false)
s = 0
}
}
}
p.Paint(r.spanBuf[:s], true)
}
// Clear cancels any previous calls to r.Start or r.AddXxx.
func (r *Rasterizer) Clear() {
r.a = fixed.Point26_6{}
r.xi = 0
r.yi = 0
r.area = 0
r.cover = 0
r.cell = r.cell[:0]
for i := 0; i < len(r.cellIndex); i++ {
r.cellIndex[i] = -1
}
}
// SetBounds sets the maximum width and height of the rasterized image and
// calls Clear. The width and height are in pixels, not fixed.Int26_6 units.
func (r *Rasterizer) SetBounds(width, height int) {
if width < 0 {
width = 0
}
if height < 0 {
height = 0
}
// Use the same ssN heuristic as the C Freetype (version 2.4.0)
// implementation.
ss2, ss3 := 32, 16
if width > 24 || height > 24 {
ss2, ss3 = 2*ss2, 2*ss3
if width > 120 || height > 120 {
ss2, ss3 = 2*ss2, 2*ss3
}
}
r.width = width
r.splitScale2 = ss2
r.splitScale3 = ss3
r.cell = r.cellBuf[:0]
if height > len(r.cellIndexBuf) {
r.cellIndex = make([]int, height)
} else {
r.cellIndex = r.cellIndexBuf[:height]
}
r.Clear()
}
// NewRasterizer creates a new Rasterizer with the given bounds.
func NewRasterizer(width, height int) *Rasterizer {
r := new(Rasterizer)
r.SetBounds(width, height)
return r
}

483
vendor/github.com/golang/freetype/raster/stroke.go generated vendored Normal file
View File

@ -0,0 +1,483 @@
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
package raster
import (
"golang.org/x/image/math/fixed"
)
// Two points are considered practically equal if the square of the distance
// between them is less than one quarter (i.e. 1024 / 4096).
const epsilon = fixed.Int52_12(1024)
// A Capper signifies how to begin or end a stroked path.
type Capper interface {
// Cap adds a cap to p given a pivot point and the normal vector of a
// terminal segment. The normal's length is half of the stroke width.
Cap(p Adder, halfWidth fixed.Int26_6, pivot, n1 fixed.Point26_6)
}
// The CapperFunc type adapts an ordinary function to be a Capper.
type CapperFunc func(Adder, fixed.Int26_6, fixed.Point26_6, fixed.Point26_6)
func (f CapperFunc) Cap(p Adder, halfWidth fixed.Int26_6, pivot, n1 fixed.Point26_6) {
f(p, halfWidth, pivot, n1)
}
// A Joiner signifies how to join interior nodes of a stroked path.
type Joiner interface {
// Join adds a join to the two sides of a stroked path given a pivot
// point and the normal vectors of the trailing and leading segments.
// Both normals have length equal to half of the stroke width.
Join(lhs, rhs Adder, halfWidth fixed.Int26_6, pivot, n0, n1 fixed.Point26_6)
}
// The JoinerFunc type adapts an ordinary function to be a Joiner.
type JoinerFunc func(lhs, rhs Adder, halfWidth fixed.Int26_6, pivot, n0, n1 fixed.Point26_6)
func (f JoinerFunc) Join(lhs, rhs Adder, halfWidth fixed.Int26_6, pivot, n0, n1 fixed.Point26_6) {
f(lhs, rhs, halfWidth, pivot, n0, n1)
}
// RoundCapper adds round caps to a stroked path.
var RoundCapper Capper = CapperFunc(roundCapper)
func roundCapper(p Adder, halfWidth fixed.Int26_6, pivot, n1 fixed.Point26_6) {
// The cubic Bézier approximation to a circle involves the magic number
// (√2 - 1) * 4/3, which is approximately 35/64.
const k = 35
e := pRot90CCW(n1)
side := pivot.Add(e)
start, end := pivot.Sub(n1), pivot.Add(n1)
d, e := n1.Mul(k), e.Mul(k)
p.Add3(start.Add(e), side.Sub(d), side)
p.Add3(side.Add(d), end.Add(e), end)
}
// ButtCapper adds butt caps to a stroked path.
var ButtCapper Capper = CapperFunc(buttCapper)
func buttCapper(p Adder, halfWidth fixed.Int26_6, pivot, n1 fixed.Point26_6) {
p.Add1(pivot.Add(n1))
}
// SquareCapper adds square caps to a stroked path.
var SquareCapper Capper = CapperFunc(squareCapper)
func squareCapper(p Adder, halfWidth fixed.Int26_6, pivot, n1 fixed.Point26_6) {
e := pRot90CCW(n1)
side := pivot.Add(e)
p.Add1(side.Sub(n1))
p.Add1(side.Add(n1))
p.Add1(pivot.Add(n1))
}
// RoundJoiner adds round joins to a stroked path.
var RoundJoiner Joiner = JoinerFunc(roundJoiner)
func roundJoiner(lhs, rhs Adder, haflWidth fixed.Int26_6, pivot, n0, n1 fixed.Point26_6) {
dot := pDot(pRot90CW(n0), n1)
if dot >= 0 {
addArc(lhs, pivot, n0, n1)
rhs.Add1(pivot.Sub(n1))
} else {
lhs.Add1(pivot.Add(n1))
addArc(rhs, pivot, pNeg(n0), pNeg(n1))
}
}
// BevelJoiner adds bevel joins to a stroked path.
var BevelJoiner Joiner = JoinerFunc(bevelJoiner)
func bevelJoiner(lhs, rhs Adder, haflWidth fixed.Int26_6, pivot, n0, n1 fixed.Point26_6) {
lhs.Add1(pivot.Add(n1))
rhs.Add1(pivot.Sub(n1))
}
// addArc adds a circular arc from pivot+n0 to pivot+n1 to p. The shorter of
// the two possible arcs is taken, i.e. the one spanning <= 180 degrees. The
// two vectors n0 and n1 must be of equal length.
func addArc(p Adder, pivot, n0, n1 fixed.Point26_6) {
// r2 is the square of the length of n0.
r2 := pDot(n0, n0)
if r2 < epsilon {
// The arc radius is so small that we collapse to a straight line.
p.Add1(pivot.Add(n1))
return
}
// We approximate the arc by 0, 1, 2 or 3 45-degree quadratic segments plus
// a final quadratic segment from s to n1. Each 45-degree segment has
// control points {1, 0}, {1, tan(π/8)} and {1/√2, 1/√2} suitably scaled,
// rotated and translated. tan(π/8) is approximately 27/64.
const tpo8 = 27
var s fixed.Point26_6
// We determine which octant the angle between n0 and n1 is in via three
// dot products. m0, m1 and m2 are n0 rotated clockwise by 45, 90 and 135
// degrees.
m0 := pRot45CW(n0)
m1 := pRot90CW(n0)
m2 := pRot90CW(m0)
if pDot(m1, n1) >= 0 {
if pDot(n0, n1) >= 0 {
if pDot(m2, n1) <= 0 {
// n1 is between 0 and 45 degrees clockwise of n0.
s = n0
} else {
// n1 is between 45 and 90 degrees clockwise of n0.
p.Add2(pivot.Add(n0).Add(m1.Mul(tpo8)), pivot.Add(m0))
s = m0
}
} else {
pm1, n0t := pivot.Add(m1), n0.Mul(tpo8)
p.Add2(pivot.Add(n0).Add(m1.Mul(tpo8)), pivot.Add(m0))
p.Add2(pm1.Add(n0t), pm1)
if pDot(m0, n1) >= 0 {
// n1 is between 90 and 135 degrees clockwise of n0.
s = m1
} else {
// n1 is between 135 and 180 degrees clockwise of n0.
p.Add2(pm1.Sub(n0t), pivot.Add(m2))
s = m2
}
}
} else {
if pDot(n0, n1) >= 0 {
if pDot(m0, n1) >= 0 {
// n1 is between 0 and 45 degrees counter-clockwise of n0.
s = n0
} else {
// n1 is between 45 and 90 degrees counter-clockwise of n0.
p.Add2(pivot.Add(n0).Sub(m1.Mul(tpo8)), pivot.Sub(m2))
s = pNeg(m2)
}
} else {
pm1, n0t := pivot.Sub(m1), n0.Mul(tpo8)
p.Add2(pivot.Add(n0).Sub(m1.Mul(tpo8)), pivot.Sub(m2))
p.Add2(pm1.Add(n0t), pm1)
if pDot(m2, n1) <= 0 {
// n1 is between 90 and 135 degrees counter-clockwise of n0.
s = pNeg(m1)
} else {
// n1 is between 135 and 180 degrees counter-clockwise of n0.
p.Add2(pm1.Sub(n0t), pivot.Sub(m0))
s = pNeg(m0)
}
}
}
// The final quadratic segment has two endpoints s and n1 and the middle
// control point is a multiple of s.Add(n1), i.e. it is on the angle
// bisector of those two points. The multiple ranges between 128/256 and
// 150/256 as the angle between s and n1 ranges between 0 and 45 degrees.
//
// When the angle is 0 degrees (i.e. s and n1 are coincident) then
// s.Add(n1) is twice s and so the middle control point of the degenerate
// quadratic segment should be half s.Add(n1), and half = 128/256.
//
// When the angle is 45 degrees then 150/256 is the ratio of the lengths of
// the two vectors {1, tan(π/8)} and {1 + 1/√2, 1/√2}.
//
// d is the normalized dot product between s and n1. Since the angle ranges
// between 0 and 45 degrees then d ranges between 256/256 and 181/256.
d := 256 * pDot(s, n1) / r2
multiple := fixed.Int26_6(150-(150-128)*(d-181)/(256-181)) >> 2
p.Add2(pivot.Add(s.Add(n1).Mul(multiple)), pivot.Add(n1))
}
// midpoint returns the midpoint of two Points.
func midpoint(a, b fixed.Point26_6) fixed.Point26_6 {
return fixed.Point26_6{(a.X + b.X) / 2, (a.Y + b.Y) / 2}
}
// angleGreaterThan45 returns whether the angle between two vectors is more
// than 45 degrees.
func angleGreaterThan45(v0, v1 fixed.Point26_6) bool {
v := pRot45CCW(v0)
return pDot(v, v1) < 0 || pDot(pRot90CW(v), v1) < 0
}
// interpolate returns the point (1-t)*a + t*b.
func interpolate(a, b fixed.Point26_6, t fixed.Int52_12) fixed.Point26_6 {
s := 1<<12 - t
x := s*fixed.Int52_12(a.X) + t*fixed.Int52_12(b.X)
y := s*fixed.Int52_12(a.Y) + t*fixed.Int52_12(b.Y)
return fixed.Point26_6{fixed.Int26_6(x >> 12), fixed.Int26_6(y >> 12)}
}
// curviest2 returns the value of t for which the quadratic parametric curve
// (1-t)²*a + 2*t*(1-t).b + t²*c has maximum curvature.
//
// The curvature of the parametric curve f(t) = (x(t), y(t)) is
// |xy″-yx″| / (x²+y²)^(3/2).
//
// Let d = b-a and e = c-2*b+a, so that f(t) = 2*d+2*e*t and f″(t) = 2*e.
// The curvature's numerator is (2*dx+2*ex*t)*(2*ey)-(2*dy+2*ey*t)*(2*ex),
// which simplifies to 4*dx*ey-4*dy*ex, which is constant with respect to t.
//
// Thus, curvature is extreme where the denominator is extreme, i.e. where
// (x²+y²) is extreme. The first order condition is that
// 2*x*x″+2*y*y″ = 0, or (dx+ex*t)*ex + (dy+ey*t)*ey = 0.
// Solving for t gives t = -(dx*ex+dy*ey) / (ex*ex+ey*ey).
func curviest2(a, b, c fixed.Point26_6) fixed.Int52_12 {
dx := int64(b.X - a.X)
dy := int64(b.Y - a.Y)
ex := int64(c.X - 2*b.X + a.X)
ey := int64(c.Y - 2*b.Y + a.Y)
if ex == 0 && ey == 0 {
return 2048
}
return fixed.Int52_12(-4096 * (dx*ex + dy*ey) / (ex*ex + ey*ey))
}
// A stroker holds state for stroking a path.
type stroker struct {
// p is the destination that records the stroked path.
p Adder
// u is the half-width of the stroke.
u fixed.Int26_6
// cr and jr specify how to end and connect path segments.
cr Capper
jr Joiner
// r is the reverse path. Stroking a path involves constructing two
// parallel paths 2*u apart. The first path is added immediately to p,
// the second path is accumulated in r and eventually added in reverse.
r Path
// a is the most recent segment point. anorm is the segment normal of
// length u at that point.
a, anorm fixed.Point26_6
}
// addNonCurvy2 adds a quadratic segment to the stroker, where the segment
// defined by (k.a, b, c) achieves maximum curvature at either k.a or c.
func (k *stroker) addNonCurvy2(b, c fixed.Point26_6) {
// We repeatedly divide the segment at its middle until it is straight
// enough to approximate the stroke by just translating the control points.
// ds and ps are stacks of depths and points. t is the top of the stack.
const maxDepth = 5
var (
ds [maxDepth + 1]int
ps [2*maxDepth + 3]fixed.Point26_6
t int
)
// Initially the ps stack has one quadratic segment of depth zero.
ds[0] = 0
ps[2] = k.a
ps[1] = b
ps[0] = c
anorm := k.anorm
var cnorm fixed.Point26_6
for {
depth := ds[t]
a := ps[2*t+2]
b := ps[2*t+1]
c := ps[2*t+0]
ab := b.Sub(a)
bc := c.Sub(b)
abIsSmall := pDot(ab, ab) < fixed.Int52_12(1<<12)
bcIsSmall := pDot(bc, bc) < fixed.Int52_12(1<<12)
if abIsSmall && bcIsSmall {
// Approximate the segment by a circular arc.
cnorm = pRot90CCW(pNorm(bc, k.u))
mac := midpoint(a, c)
addArc(k.p, mac, anorm, cnorm)
addArc(&k.r, mac, pNeg(anorm), pNeg(cnorm))
} else if depth < maxDepth && angleGreaterThan45(ab, bc) {
// Divide the segment in two and push both halves on the stack.
mab := midpoint(a, b)
mbc := midpoint(b, c)
t++
ds[t+0] = depth + 1
ds[t-1] = depth + 1
ps[2*t+2] = a
ps[2*t+1] = mab
ps[2*t+0] = midpoint(mab, mbc)
ps[2*t-1] = mbc
continue
} else {
// Translate the control points.
bnorm := pRot90CCW(pNorm(c.Sub(a), k.u))
cnorm = pRot90CCW(pNorm(bc, k.u))
k.p.Add2(b.Add(bnorm), c.Add(cnorm))
k.r.Add2(b.Sub(bnorm), c.Sub(cnorm))
}
if t == 0 {
k.a, k.anorm = c, cnorm
return
}
t--
anorm = cnorm
}
panic("unreachable")
}
// Add1 adds a linear segment to the stroker.
func (k *stroker) Add1(b fixed.Point26_6) {
bnorm := pRot90CCW(pNorm(b.Sub(k.a), k.u))
if len(k.r) == 0 {
k.p.Start(k.a.Add(bnorm))
k.r.Start(k.a.Sub(bnorm))
} else {
k.jr.Join(k.p, &k.r, k.u, k.a, k.anorm, bnorm)
}
k.p.Add1(b.Add(bnorm))
k.r.Add1(b.Sub(bnorm))
k.a, k.anorm = b, bnorm
}
// Add2 adds a quadratic segment to the stroker.
func (k *stroker) Add2(b, c fixed.Point26_6) {
ab := b.Sub(k.a)
bc := c.Sub(b)
abnorm := pRot90CCW(pNorm(ab, k.u))
if len(k.r) == 0 {
k.p.Start(k.a.Add(abnorm))
k.r.Start(k.a.Sub(abnorm))
} else {
k.jr.Join(k.p, &k.r, k.u, k.a, k.anorm, abnorm)
}
// Approximate nearly-degenerate quadratics by linear segments.
abIsSmall := pDot(ab, ab) < epsilon
bcIsSmall := pDot(bc, bc) < epsilon
if abIsSmall || bcIsSmall {
acnorm := pRot90CCW(pNorm(c.Sub(k.a), k.u))
k.p.Add1(c.Add(acnorm))
k.r.Add1(c.Sub(acnorm))
k.a, k.anorm = c, acnorm
return
}
// The quadratic segment (k.a, b, c) has a point of maximum curvature.
// If this occurs at an end point, we process the segment as a whole.
t := curviest2(k.a, b, c)
if t <= 0 || 4096 <= t {
k.addNonCurvy2(b, c)
return
}
// Otherwise, we perform a de Casteljau decomposition at the point of
// maximum curvature and process the two straighter parts.
mab := interpolate(k.a, b, t)
mbc := interpolate(b, c, t)
mabc := interpolate(mab, mbc, t)
// If the vectors ab and bc are close to being in opposite directions,
// then the decomposition can become unstable, so we approximate the
// quadratic segment by two linear segments joined by an arc.
bcnorm := pRot90CCW(pNorm(bc, k.u))
if pDot(abnorm, bcnorm) < -fixed.Int52_12(k.u)*fixed.Int52_12(k.u)*2047/2048 {
pArc := pDot(abnorm, bc) < 0
k.p.Add1(mabc.Add(abnorm))
if pArc {
z := pRot90CW(abnorm)
addArc(k.p, mabc, abnorm, z)
addArc(k.p, mabc, z, bcnorm)
}
k.p.Add1(mabc.Add(bcnorm))
k.p.Add1(c.Add(bcnorm))
k.r.Add1(mabc.Sub(abnorm))
if !pArc {
z := pRot90CW(abnorm)
addArc(&k.r, mabc, pNeg(abnorm), z)
addArc(&k.r, mabc, z, pNeg(bcnorm))
}
k.r.Add1(mabc.Sub(bcnorm))
k.r.Add1(c.Sub(bcnorm))
k.a, k.anorm = c, bcnorm
return
}
// Process the decomposed parts.
k.addNonCurvy2(mab, mabc)
k.addNonCurvy2(mbc, c)
}
// Add3 adds a cubic segment to the stroker.
func (k *stroker) Add3(b, c, d fixed.Point26_6) {
panic("freetype/raster: stroke unimplemented for cubic segments")
}
// stroke adds the stroked Path q to p, where q consists of exactly one curve.
func (k *stroker) stroke(q Path) {
// Stroking is implemented by deriving two paths each k.u apart from q.
// The left-hand-side path is added immediately to k.p; the right-hand-side
// path is accumulated in k.r. Once we've finished adding the LHS to k.p,
// we add the RHS in reverse order.
k.r = make(Path, 0, len(q))
k.a = fixed.Point26_6{q[1], q[2]}
for i := 4; i < len(q); {
switch q[i] {
case 1:
k.Add1(
fixed.Point26_6{q[i+1], q[i+2]},
)
i += 4
case 2:
k.Add2(
fixed.Point26_6{q[i+1], q[i+2]},
fixed.Point26_6{q[i+3], q[i+4]},
)
i += 6
case 3:
k.Add3(
fixed.Point26_6{q[i+1], q[i+2]},
fixed.Point26_6{q[i+3], q[i+4]},
fixed.Point26_6{q[i+5], q[i+6]},
)
i += 8
default:
panic("freetype/raster: bad path")
}
}
if len(k.r) == 0 {
return
}
// TODO(nigeltao): if q is a closed curve then we should join the first and
// last segments instead of capping them.
k.cr.Cap(k.p, k.u, q.lastPoint(), pNeg(k.anorm))
addPathReversed(k.p, k.r)
pivot := q.firstPoint()
k.cr.Cap(k.p, k.u, pivot, pivot.Sub(fixed.Point26_6{k.r[1], k.r[2]}))
}
// Stroke adds q stroked with the given width to p. The result is typically
// self-intersecting and should be rasterized with UseNonZeroWinding.
// cr and jr may be nil, which defaults to a RoundCapper or RoundJoiner.
func Stroke(p Adder, q Path, width fixed.Int26_6, cr Capper, jr Joiner) {
if len(q) == 0 {
return
}
if cr == nil {
cr = RoundCapper
}
if jr == nil {
jr = RoundJoiner
}
if q[0] != 0 {
panic("freetype/raster: bad path")
}
s := stroker{p: p, u: width / 2, cr: cr, jr: jr}
i := 0
for j := 4; j < len(q); {
switch q[j] {
case 0:
s.stroke(q[i:j])
i, j = j, j+4
case 1:
j += 4
case 2:
j += 6
case 3:
j += 8
default:
panic("freetype/raster: bad path")
}
}
s.stroke(q[i:])
}

42
vendor/github.com/golang/freetype/testdata/COPYING generated vendored Normal file
View File

@ -0,0 +1,42 @@
Luxi fonts copyright (c) 2001 by Bigelow & Holmes Inc. Luxi font
instruction code copyright (c) 2001 by URW++ GmbH. All Rights
Reserved. Luxi is a registered trademark of Bigelow & Holmes Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of these Fonts and associated documentation files (the "Font
Software"), to deal in the Font Software, including without
limitation the rights to use, copy, merge, publish, distribute,
sublicense, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to
the following conditions:
The above copyright and trademark notices and this permission notice
shall be included in all copies of one or more of the Font Software.
The Font Software may not be modified, altered, or added to, and in
particular the designs of glyphs or characters in the Fonts may not
be modified nor may additional glyphs or characters be added to the
Fonts. This License becomes null and void when the Fonts or Font
Software have been modified.
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL
BIGELOW & HOLMES INC. OR URW++ GMBH. BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR
INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT
SOFTWARE.
Except as contained in this notice, the names of Bigelow & Holmes
Inc. and URW++ GmbH. shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Font Software without
prior written authorization from Bigelow & Holmes Inc. and URW++ GmbH.
For further information, contact:
info@urwpp.de
or
design@bigelowandholmes.com

507
vendor/github.com/golang/freetype/truetype/face.go generated vendored Normal file
View File

@ -0,0 +1,507 @@
// Copyright 2015 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
package truetype
import (
"image"
"math"
"github.com/golang/freetype/raster"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
func powerOf2(i int) bool {
return i != 0 && (i&(i-1)) == 0
}
// Options are optional arguments to NewFace.
type Options struct {
// Size is the font size in points, as in "a 10 point font size".
//
// A zero value means to use a 12 point font size.
Size float64
// DPI is the dots-per-inch resolution.
//
// A zero value means to use 72 DPI.
DPI float64
// Hinting is how to quantize the glyph nodes.
//
// A zero value means to use no hinting.
Hinting font.Hinting
// GlyphCacheEntries is the number of entries in the glyph mask image
// cache.
//
// If non-zero, it must be a power of 2.
//
// A zero value means to use 512 entries.
GlyphCacheEntries int
// SubPixelsX is the number of sub-pixel locations a glyph's dot is
// quantized to, in the horizontal direction. For example, a value of 8
// means that the dot is quantized to 1/8th of a pixel. This quantization
// only affects the glyph mask image, not its bounding box or advance
// width. A higher value gives a more faithful glyph image, but reduces the
// effectiveness of the glyph cache.
//
// If non-zero, it must be a power of 2, and be between 1 and 64 inclusive.
//
// A zero value means to use 4 sub-pixel locations.
SubPixelsX int
// SubPixelsY is the number of sub-pixel locations a glyph's dot is
// quantized to, in the vertical direction. For example, a value of 8
// means that the dot is quantized to 1/8th of a pixel. This quantization
// only affects the glyph mask image, not its bounding box or advance
// width. A higher value gives a more faithful glyph image, but reduces the
// effectiveness of the glyph cache.
//
// If non-zero, it must be a power of 2, and be between 1 and 64 inclusive.
//
// A zero value means to use 1 sub-pixel location.
SubPixelsY int
}
func (o *Options) size() float64 {
if o != nil && o.Size > 0 {
return o.Size
}
return 12
}
func (o *Options) dpi() float64 {
if o != nil && o.DPI > 0 {
return o.DPI
}
return 72
}
func (o *Options) hinting() font.Hinting {
if o != nil {
switch o.Hinting {
case font.HintingVertical, font.HintingFull:
// TODO: support vertical hinting.
return font.HintingFull
}
}
return font.HintingNone
}
func (o *Options) glyphCacheEntries() int {
if o != nil && powerOf2(o.GlyphCacheEntries) {
return o.GlyphCacheEntries
}
// 512 is 128 * 4 * 1, which lets us cache 128 glyphs at 4 * 1 subpixel
// locations in the X and Y direction.
return 512
}
func (o *Options) subPixelsX() (value uint32, halfQuantum, mask fixed.Int26_6) {
if o != nil {
switch o.SubPixelsX {
case 1, 2, 4, 8, 16, 32, 64:
return subPixels(o.SubPixelsX)
}
}
// This default value of 4 isn't based on anything scientific, merely as
// small a number as possible that looks almost as good as no quantization,
// or returning subPixels(64).
return subPixels(4)
}
func (o *Options) subPixelsY() (value uint32, halfQuantum, mask fixed.Int26_6) {
if o != nil {
switch o.SubPixelsX {
case 1, 2, 4, 8, 16, 32, 64:
return subPixels(o.SubPixelsX)
}
}
// This default value of 1 isn't based on anything scientific, merely that
// vertical sub-pixel glyph rendering is pretty rare. Baseline locations
// can usually afford to snap to the pixel grid, so the vertical direction
// doesn't have the deal with the horizontal's fractional advance widths.
return subPixels(1)
}
// subPixels returns q and the bias and mask that leads to q quantized
// sub-pixel locations per full pixel.
//
// For example, q == 4 leads to a bias of 8 and a mask of 0xfffffff0, or -16,
// because we want to round fractions of fixed.Int26_6 as:
// - 0 to 7 rounds to 0.
// - 8 to 23 rounds to 16.
// - 24 to 39 rounds to 32.
// - 40 to 55 rounds to 48.
// - 56 to 63 rounds to 64.
// which means to add 8 and then bitwise-and with -16, in two's complement
// representation.
//
// When q == 1, we want bias == 32 and mask == -64.
// When q == 2, we want bias == 16 and mask == -32.
// When q == 4, we want bias == 8 and mask == -16.
// ...
// When q == 64, we want bias == 0 and mask == -1. (The no-op case).
// The pattern is clear.
func subPixels(q int) (value uint32, bias, mask fixed.Int26_6) {
return uint32(q), 32 / fixed.Int26_6(q), -64 / fixed.Int26_6(q)
}
// glyphCacheEntry caches the arguments and return values of rasterize.
type glyphCacheEntry struct {
key glyphCacheKey
val glyphCacheVal
}
type glyphCacheKey struct {
index Index
fx, fy uint8
}
type glyphCacheVal struct {
advanceWidth fixed.Int26_6
offset image.Point
gw int
gh int
}
type indexCacheEntry struct {
rune rune
index Index
}
// NewFace returns a new font.Face for the given Font.
func NewFace(f *Font, opts *Options) font.Face {
a := &face{
f: f,
hinting: opts.hinting(),
scale: fixed.Int26_6(0.5 + (opts.size() * opts.dpi() * 64 / 72)),
glyphCache: make([]glyphCacheEntry, opts.glyphCacheEntries()),
}
a.subPixelX, a.subPixelBiasX, a.subPixelMaskX = opts.subPixelsX()
a.subPixelY, a.subPixelBiasY, a.subPixelMaskY = opts.subPixelsY()
// Fill the cache with invalid entries. Valid glyph cache entries have fx
// and fy in the range [0, 64). Valid index cache entries have rune >= 0.
for i := range a.glyphCache {
a.glyphCache[i].key.fy = 0xff
}
for i := range a.indexCache {
a.indexCache[i].rune = -1
}
// Set the rasterizer's bounds to be big enough to handle the largest glyph.
b := f.Bounds(a.scale)
xmin := +int(b.Min.X) >> 6
ymin := -int(b.Max.Y) >> 6
xmax := +int(b.Max.X+63) >> 6
ymax := -int(b.Min.Y-63) >> 6
a.maxw = xmax - xmin
a.maxh = ymax - ymin
a.masks = image.NewAlpha(image.Rect(0, 0, a.maxw, a.maxh*len(a.glyphCache)))
a.r.SetBounds(a.maxw, a.maxh)
a.p = facePainter{a}
return a
}
type face struct {
f *Font
hinting font.Hinting
scale fixed.Int26_6
subPixelX uint32
subPixelBiasX fixed.Int26_6
subPixelMaskX fixed.Int26_6
subPixelY uint32
subPixelBiasY fixed.Int26_6
subPixelMaskY fixed.Int26_6
masks *image.Alpha
glyphCache []glyphCacheEntry
r raster.Rasterizer
p raster.Painter
paintOffset int
maxw int
maxh int
glyphBuf GlyphBuf
indexCache [indexCacheLen]indexCacheEntry
// TODO: clip rectangle?
}
const indexCacheLen = 256
func (a *face) index(r rune) Index {
const mask = indexCacheLen - 1
c := &a.indexCache[r&mask]
if c.rune == r {
return c.index
}
i := a.f.Index(r)
c.rune = r
c.index = i
return i
}
// Close satisfies the font.Face interface.
func (a *face) Close() error { return nil }
// Metrics satisfies the font.Face interface.
func (a *face) Metrics() font.Metrics {
scale := float64(a.scale)
fupe := float64(a.f.FUnitsPerEm())
return font.Metrics{
Height: a.scale,
Ascent: fixed.Int26_6(math.Ceil(scale * float64(+a.f.ascent) / fupe)),
Descent: fixed.Int26_6(math.Ceil(scale * float64(-a.f.descent) / fupe)),
}
}
// Kern satisfies the font.Face interface.
func (a *face) Kern(r0, r1 rune) fixed.Int26_6 {
i0 := a.index(r0)
i1 := a.index(r1)
kern := a.f.Kern(a.scale, i0, i1)
if a.hinting != font.HintingNone {
kern = (kern + 32) &^ 63
}
return kern
}
// Glyph satisfies the font.Face interface.
func (a *face) Glyph(dot fixed.Point26_6, r rune) (
dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
// Quantize to the sub-pixel granularity.
dotX := (dot.X + a.subPixelBiasX) & a.subPixelMaskX
dotY := (dot.Y + a.subPixelBiasY) & a.subPixelMaskY
// Split the coordinates into their integer and fractional parts.
ix, fx := int(dotX>>6), dotX&0x3f
iy, fy := int(dotY>>6), dotY&0x3f
index := a.index(r)
cIndex := uint32(index)
cIndex = cIndex*a.subPixelX - uint32(fx/a.subPixelMaskX)
cIndex = cIndex*a.subPixelY - uint32(fy/a.subPixelMaskY)
cIndex &= uint32(len(a.glyphCache) - 1)
a.paintOffset = a.maxh * int(cIndex)
k := glyphCacheKey{
index: index,
fx: uint8(fx),
fy: uint8(fy),
}
var v glyphCacheVal
if a.glyphCache[cIndex].key != k {
var ok bool
v, ok = a.rasterize(index, fx, fy)
if !ok {
return image.Rectangle{}, nil, image.Point{}, 0, false
}
a.glyphCache[cIndex] = glyphCacheEntry{k, v}
} else {
v = a.glyphCache[cIndex].val
}
dr.Min = image.Point{
X: ix + v.offset.X,
Y: iy + v.offset.Y,
}
dr.Max = image.Point{
X: dr.Min.X + v.gw,
Y: dr.Min.Y + v.gh,
}
return dr, a.masks, image.Point{Y: a.paintOffset}, v.advanceWidth, true
}
func (a *face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
if err := a.glyphBuf.Load(a.f, a.scale, a.index(r), a.hinting); err != nil {
return fixed.Rectangle26_6{}, 0, false
}
xmin := +a.glyphBuf.Bounds.Min.X
ymin := -a.glyphBuf.Bounds.Max.Y
xmax := +a.glyphBuf.Bounds.Max.X
ymax := -a.glyphBuf.Bounds.Min.Y
if xmin > xmax || ymin > ymax {
return fixed.Rectangle26_6{}, 0, false
}
return fixed.Rectangle26_6{
Min: fixed.Point26_6{
X: xmin,
Y: ymin,
},
Max: fixed.Point26_6{
X: xmax,
Y: ymax,
},
}, a.glyphBuf.AdvanceWidth, true
}
func (a *face) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
if err := a.glyphBuf.Load(a.f, a.scale, a.index(r), a.hinting); err != nil {
return 0, false
}
return a.glyphBuf.AdvanceWidth, true
}
// rasterize returns the advance width, integer-pixel offset to render at, and
// the width and height of the given glyph at the given sub-pixel offsets.
//
// The 26.6 fixed point arguments fx and fy must be in the range [0, 1).
func (a *face) rasterize(index Index, fx, fy fixed.Int26_6) (v glyphCacheVal, ok bool) {
if err := a.glyphBuf.Load(a.f, a.scale, index, a.hinting); err != nil {
return glyphCacheVal{}, false
}
// Calculate the integer-pixel bounds for the glyph.
xmin := int(fx+a.glyphBuf.Bounds.Min.X) >> 6
ymin := int(fy-a.glyphBuf.Bounds.Max.Y) >> 6
xmax := int(fx+a.glyphBuf.Bounds.Max.X+0x3f) >> 6
ymax := int(fy-a.glyphBuf.Bounds.Min.Y+0x3f) >> 6
if xmin > xmax || ymin > ymax {
return glyphCacheVal{}, false
}
// A TrueType's glyph's nodes can have negative co-ordinates, but the
// rasterizer clips anything left of x=0 or above y=0. xmin and ymin are
// the pixel offsets, based on the font's FUnit metrics, that let a
// negative co-ordinate in TrueType space be non-negative in rasterizer
// space. xmin and ymin are typically <= 0.
fx -= fixed.Int26_6(xmin << 6)
fy -= fixed.Int26_6(ymin << 6)
// Rasterize the glyph's vectors.
a.r.Clear()
pixOffset := a.paintOffset * a.maxw
clear(a.masks.Pix[pixOffset : pixOffset+a.maxw*a.maxh])
e0 := 0
for _, e1 := range a.glyphBuf.Ends {
a.drawContour(a.glyphBuf.Points[e0:e1], fx, fy)
e0 = e1
}
a.r.Rasterize(a.p)
return glyphCacheVal{
a.glyphBuf.AdvanceWidth,
image.Point{xmin, ymin},
xmax - xmin,
ymax - ymin,
}, true
}
func clear(pix []byte) {
for i := range pix {
pix[i] = 0
}
}
// drawContour draws the given closed contour with the given offset.
func (a *face) drawContour(ps []Point, dx, dy fixed.Int26_6) {
if len(ps) == 0 {
return
}
// The low bit of each point's Flags value is whether the point is on the
// curve. Truetype fonts only have quadratic Bézier curves, not cubics.
// Thus, two consecutive off-curve points imply an on-curve point in the
// middle of those two.
//
// See http://chanae.walon.org/pub/ttf/ttf_glyphs.htm for more details.
// ps[0] is a truetype.Point measured in FUnits and positive Y going
// upwards. start is the same thing measured in fixed point units and
// positive Y going downwards, and offset by (dx, dy).
start := fixed.Point26_6{
X: dx + ps[0].X,
Y: dy - ps[0].Y,
}
var others []Point
if ps[0].Flags&0x01 != 0 {
others = ps[1:]
} else {
last := fixed.Point26_6{
X: dx + ps[len(ps)-1].X,
Y: dy - ps[len(ps)-1].Y,
}
if ps[len(ps)-1].Flags&0x01 != 0 {
start = last
others = ps[:len(ps)-1]
} else {
start = fixed.Point26_6{
X: (start.X + last.X) / 2,
Y: (start.Y + last.Y) / 2,
}
others = ps
}
}
a.r.Start(start)
q0, on0 := start, true
for _, p := range others {
q := fixed.Point26_6{
X: dx + p.X,
Y: dy - p.Y,
}
on := p.Flags&0x01 != 0
if on {
if on0 {
a.r.Add1(q)
} else {
a.r.Add2(q0, q)
}
} else {
if on0 {
// No-op.
} else {
mid := fixed.Point26_6{
X: (q0.X + q.X) / 2,
Y: (q0.Y + q.Y) / 2,
}
a.r.Add2(q0, mid)
}
}
q0, on0 = q, on
}
// Close the curve.
if on0 {
a.r.Add1(start)
} else {
a.r.Add2(q0, start)
}
}
// facePainter is like a raster.AlphaSrcPainter, with an additional Y offset
// (face.paintOffset) to the painted spans.
type facePainter struct {
a *face
}
func (p facePainter) Paint(ss []raster.Span, done bool) {
m := p.a.masks
b := m.Bounds()
b.Min.Y = p.a.paintOffset
b.Max.Y = p.a.paintOffset + p.a.maxh
for _, s := range ss {
s.Y += p.a.paintOffset
if s.Y < b.Min.Y {
continue
}
if s.Y >= b.Max.Y {
return
}
if s.X0 < b.Min.X {
s.X0 = b.Min.X
}
if s.X1 > b.Max.X {
s.X1 = b.Max.X
}
if s.X0 >= s.X1 {
continue
}
base := (s.Y-m.Rect.Min.Y)*m.Stride - m.Rect.Min.X
p := m.Pix[base+s.X0 : base+s.X1]
color := uint8(s.Alpha >> 8)
for i := range p {
p[i] = color
}
}
}

522
vendor/github.com/golang/freetype/truetype/glyph.go generated vendored Normal file
View File

@ -0,0 +1,522 @@
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
package truetype
import (
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
// TODO: implement VerticalHinting.
// A Point is a co-ordinate pair plus whether it is 'on' a contour or an 'off'
// control point.
type Point struct {
X, Y fixed.Int26_6
// The Flags' LSB means whether or not this Point is 'on' the contour.
// Other bits are reserved for internal use.
Flags uint32
}
// A GlyphBuf holds a glyph's contours. A GlyphBuf can be re-used to load a
// series of glyphs from a Font.
type GlyphBuf struct {
// AdvanceWidth is the glyph's advance width.
AdvanceWidth fixed.Int26_6
// Bounds is the glyph's bounding box.
Bounds fixed.Rectangle26_6
// Points contains all Points from all contours of the glyph. If hinting
// was used to load a glyph then Unhinted contains those Points before they
// were hinted, and InFontUnits contains those Points before they were
// hinted and scaled.
Points, Unhinted, InFontUnits []Point
// Ends is the point indexes of the end point of each contour. The length
// of Ends is the number of contours in the glyph. The i'th contour
// consists of points Points[Ends[i-1]:Ends[i]], where Ends[-1] is
// interpreted to mean zero.
Ends []int
font *Font
scale fixed.Int26_6
hinting font.Hinting
hinter hinter
// phantomPoints are the co-ordinates of the synthetic phantom points
// used for hinting and bounding box calculations.
phantomPoints [4]Point
// pp1x is the X co-ordinate of the first phantom point. The '1' is
// using 1-based indexing; pp1x is almost always phantomPoints[0].X.
// TODO: eliminate this and consistently use phantomPoints[0].X.
pp1x fixed.Int26_6
// metricsSet is whether the glyph's metrics have been set yet. For a
// compound glyph, a sub-glyph may override the outer glyph's metrics.
metricsSet bool
// tmp is a scratch buffer.
tmp []Point
}
// Flags for decoding a glyph's contours. These flags are documented at
// http://developer.apple.com/fonts/TTRefMan/RM06/Chap6glyf.html.
const (
flagOnCurve = 1 << iota
flagXShortVector
flagYShortVector
flagRepeat
flagPositiveXShortVector
flagPositiveYShortVector
// The remaining flags are for internal use.
flagTouchedX
flagTouchedY
)
// The same flag bits (0x10 and 0x20) are overloaded to have two meanings,
// dependent on the value of the flag{X,Y}ShortVector bits.
const (
flagThisXIsSame = flagPositiveXShortVector
flagThisYIsSame = flagPositiveYShortVector
)
// Load loads a glyph's contours from a Font, overwriting any previously loaded
// contours for this GlyphBuf. scale is the number of 26.6 fixed point units in
// 1 em, i is the glyph index, and h is the hinting policy.
func (g *GlyphBuf) Load(f *Font, scale fixed.Int26_6, i Index, h font.Hinting) error {
g.Points = g.Points[:0]
g.Unhinted = g.Unhinted[:0]
g.InFontUnits = g.InFontUnits[:0]
g.Ends = g.Ends[:0]
g.font = f
g.hinting = h
g.scale = scale
g.pp1x = 0
g.phantomPoints = [4]Point{}
g.metricsSet = false
if h != font.HintingNone {
if err := g.hinter.init(f, scale); err != nil {
return err
}
}
if err := g.load(0, i, true); err != nil {
return err
}
// TODO: this selection of either g.pp1x or g.phantomPoints[0].X isn't ideal,
// and should be cleaned up once we have all the testScaling tests passing,
// plus additional tests for Freetype-Go's bounding boxes matching C Freetype's.
pp1x := g.pp1x
if h != font.HintingNone {
pp1x = g.phantomPoints[0].X
}
if pp1x != 0 {
for i := range g.Points {
g.Points[i].X -= pp1x
}
}
advanceWidth := g.phantomPoints[1].X - g.phantomPoints[0].X
if h != font.HintingNone {
if len(f.hdmx) >= 8 {
if n := u32(f.hdmx, 4); n > 3+uint32(i) {
for hdmx := f.hdmx[8:]; uint32(len(hdmx)) >= n; hdmx = hdmx[n:] {
if fixed.Int26_6(hdmx[0]) == scale>>6 {
advanceWidth = fixed.Int26_6(hdmx[2+i]) << 6
break
}
}
}
}
advanceWidth = (advanceWidth + 32) &^ 63
}
g.AdvanceWidth = advanceWidth
// Set g.Bounds to the 'control box', which is the bounding box of the
// Bézier curves' control points. This is easier to calculate, no smaller
// than and often equal to the tightest possible bounding box of the curves
// themselves. This approach is what C Freetype does. We can't just scale
// the nominal bounding box in the glyf data as the hinting process and
// phantom point adjustment may move points outside of that box.
if len(g.Points) == 0 {
g.Bounds = fixed.Rectangle26_6{}
} else {
p := g.Points[0]
g.Bounds.Min.X = p.X
g.Bounds.Max.X = p.X
g.Bounds.Min.Y = p.Y
g.Bounds.Max.Y = p.Y
for _, p := range g.Points[1:] {
if g.Bounds.Min.X > p.X {
g.Bounds.Min.X = p.X
} else if g.Bounds.Max.X < p.X {
g.Bounds.Max.X = p.X
}
if g.Bounds.Min.Y > p.Y {
g.Bounds.Min.Y = p.Y
} else if g.Bounds.Max.Y < p.Y {
g.Bounds.Max.Y = p.Y
}
}
// Snap the box to the grid, if hinting is on.
if h != font.HintingNone {
g.Bounds.Min.X &^= 63
g.Bounds.Min.Y &^= 63
g.Bounds.Max.X += 63
g.Bounds.Max.X &^= 63
g.Bounds.Max.Y += 63
g.Bounds.Max.Y &^= 63
}
}
return nil
}
func (g *GlyphBuf) load(recursion uint32, i Index, useMyMetrics bool) (err error) {
// The recursion limit here is arbitrary, but defends against malformed glyphs.
if recursion >= 32 {
return UnsupportedError("excessive compound glyph recursion")
}
// Find the relevant slice of g.font.glyf.
var g0, g1 uint32
if g.font.locaOffsetFormat == locaOffsetFormatShort {
g0 = 2 * uint32(u16(g.font.loca, 2*int(i)))
g1 = 2 * uint32(u16(g.font.loca, 2*int(i)+2))
} else {
g0 = u32(g.font.loca, 4*int(i))
g1 = u32(g.font.loca, 4*int(i)+4)
}
// Decode the contour count and nominal bounding box, from the first
// 10 bytes of the glyf data. boundsYMin and boundsXMax, at offsets 4
// and 6, are unused.
glyf, ne, boundsXMin, boundsYMax := []byte(nil), 0, fixed.Int26_6(0), fixed.Int26_6(0)
if g0+10 <= g1 {
glyf = g.font.glyf[g0:g1]
ne = int(int16(u16(glyf, 0)))
boundsXMin = fixed.Int26_6(int16(u16(glyf, 2)))
boundsYMax = fixed.Int26_6(int16(u16(glyf, 8)))
}
// Create the phantom points.
uhm, pp1x := g.font.unscaledHMetric(i), fixed.Int26_6(0)
uvm := g.font.unscaledVMetric(i, boundsYMax)
g.phantomPoints = [4]Point{
{X: boundsXMin - uhm.LeftSideBearing},
{X: boundsXMin - uhm.LeftSideBearing + uhm.AdvanceWidth},
{X: uhm.AdvanceWidth / 2, Y: boundsYMax + uvm.TopSideBearing},
{X: uhm.AdvanceWidth / 2, Y: boundsYMax + uvm.TopSideBearing - uvm.AdvanceHeight},
}
if len(glyf) == 0 {
g.addPhantomsAndScale(len(g.Points), len(g.Points), true, true)
copy(g.phantomPoints[:], g.Points[len(g.Points)-4:])
g.Points = g.Points[:len(g.Points)-4]
// TODO: also trim g.InFontUnits and g.Unhinted?
return nil
}
// Load and hint the contours.
if ne < 0 {
if ne != -1 {
// http://developer.apple.com/fonts/TTRefMan/RM06/Chap6glyf.html says that
// "the values -2, -3, and so forth, are reserved for future use."
return UnsupportedError("negative number of contours")
}
pp1x = g.font.scale(g.scale * (boundsXMin - uhm.LeftSideBearing))
if err := g.loadCompound(recursion, uhm, i, glyf, useMyMetrics); err != nil {
return err
}
} else {
np0, ne0 := len(g.Points), len(g.Ends)
program := g.loadSimple(glyf, ne)
g.addPhantomsAndScale(np0, np0, true, true)
pp1x = g.Points[len(g.Points)-4].X
if g.hinting != font.HintingNone {
if len(program) != 0 {
err := g.hinter.run(
program,
g.Points[np0:],
g.Unhinted[np0:],
g.InFontUnits[np0:],
g.Ends[ne0:],
)
if err != nil {
return err
}
}
// Drop the four phantom points.
g.InFontUnits = g.InFontUnits[:len(g.InFontUnits)-4]
g.Unhinted = g.Unhinted[:len(g.Unhinted)-4]
}
if useMyMetrics {
copy(g.phantomPoints[:], g.Points[len(g.Points)-4:])
}
g.Points = g.Points[:len(g.Points)-4]
if np0 != 0 {
// The hinting program expects the []Ends values to be indexed
// relative to the inner glyph, not the outer glyph, so we delay
// adding np0 until after the hinting program (if any) has run.
for i := ne0; i < len(g.Ends); i++ {
g.Ends[i] += np0
}
}
}
if useMyMetrics && !g.metricsSet {
g.metricsSet = true
g.pp1x = pp1x
}
return nil
}
// loadOffset is the initial offset for loadSimple and loadCompound. The first
// 10 bytes are the number of contours and the bounding box.
const loadOffset = 10
func (g *GlyphBuf) loadSimple(glyf []byte, ne int) (program []byte) {
offset := loadOffset
for i := 0; i < ne; i++ {
g.Ends = append(g.Ends, 1+int(u16(glyf, offset)))
offset += 2
}
// Note the TrueType hinting instructions.
instrLen := int(u16(glyf, offset))
offset += 2
program = glyf[offset : offset+instrLen]
offset += instrLen
if ne == 0 {
return program
}
np0 := len(g.Points)
np1 := np0 + int(g.Ends[len(g.Ends)-1])
// Decode the flags.
for i := np0; i < np1; {
c := uint32(glyf[offset])
offset++
g.Points = append(g.Points, Point{Flags: c})
i++
if c&flagRepeat != 0 {
count := glyf[offset]
offset++
for ; count > 0; count-- {
g.Points = append(g.Points, Point{Flags: c})
i++
}
}
}
// Decode the co-ordinates.
var x int16
for i := np0; i < np1; i++ {
f := g.Points[i].Flags
if f&flagXShortVector != 0 {
dx := int16(glyf[offset])
offset++
if f&flagPositiveXShortVector == 0 {
x -= dx
} else {
x += dx
}
} else if f&flagThisXIsSame == 0 {
x += int16(u16(glyf, offset))
offset += 2
}
g.Points[i].X = fixed.Int26_6(x)
}
var y int16
for i := np0; i < np1; i++ {
f := g.Points[i].Flags
if f&flagYShortVector != 0 {
dy := int16(glyf[offset])
offset++
if f&flagPositiveYShortVector == 0 {
y -= dy
} else {
y += dy
}
} else if f&flagThisYIsSame == 0 {
y += int16(u16(glyf, offset))
offset += 2
}
g.Points[i].Y = fixed.Int26_6(y)
}
return program
}
func (g *GlyphBuf) loadCompound(recursion uint32, uhm HMetric, i Index,
glyf []byte, useMyMetrics bool) error {
// Flags for decoding a compound glyph. These flags are documented at
// http://developer.apple.com/fonts/TTRefMan/RM06/Chap6glyf.html.
const (
flagArg1And2AreWords = 1 << iota
flagArgsAreXYValues
flagRoundXYToGrid
flagWeHaveAScale
flagUnused
flagMoreComponents
flagWeHaveAnXAndYScale
flagWeHaveATwoByTwo
flagWeHaveInstructions
flagUseMyMetrics
flagOverlapCompound
)
np0, ne0 := len(g.Points), len(g.Ends)
offset := loadOffset
for {
flags := u16(glyf, offset)
component := Index(u16(glyf, offset+2))
dx, dy, transform, hasTransform := fixed.Int26_6(0), fixed.Int26_6(0), [4]int16{}, false
if flags&flagArg1And2AreWords != 0 {
dx = fixed.Int26_6(int16(u16(glyf, offset+4)))
dy = fixed.Int26_6(int16(u16(glyf, offset+6)))
offset += 8
} else {
dx = fixed.Int26_6(int16(int8(glyf[offset+4])))
dy = fixed.Int26_6(int16(int8(glyf[offset+5])))
offset += 6
}
if flags&flagArgsAreXYValues == 0 {
return UnsupportedError("compound glyph transform vector")
}
if flags&(flagWeHaveAScale|flagWeHaveAnXAndYScale|flagWeHaveATwoByTwo) != 0 {
hasTransform = true
switch {
case flags&flagWeHaveAScale != 0:
transform[0] = int16(u16(glyf, offset+0))
transform[3] = transform[0]
offset += 2
case flags&flagWeHaveAnXAndYScale != 0:
transform[0] = int16(u16(glyf, offset+0))
transform[3] = int16(u16(glyf, offset+2))
offset += 4
case flags&flagWeHaveATwoByTwo != 0:
transform[0] = int16(u16(glyf, offset+0))
transform[1] = int16(u16(glyf, offset+2))
transform[2] = int16(u16(glyf, offset+4))
transform[3] = int16(u16(glyf, offset+6))
offset += 8
}
}
savedPP := g.phantomPoints
np0 := len(g.Points)
componentUMM := useMyMetrics && (flags&flagUseMyMetrics != 0)
if err := g.load(recursion+1, component, componentUMM); err != nil {
return err
}
if flags&flagUseMyMetrics == 0 {
g.phantomPoints = savedPP
}
if hasTransform {
for j := np0; j < len(g.Points); j++ {
p := &g.Points[j]
newX := 0 +
fixed.Int26_6((int64(p.X)*int64(transform[0])+1<<13)>>14) +
fixed.Int26_6((int64(p.Y)*int64(transform[2])+1<<13)>>14)
newY := 0 +
fixed.Int26_6((int64(p.X)*int64(transform[1])+1<<13)>>14) +
fixed.Int26_6((int64(p.Y)*int64(transform[3])+1<<13)>>14)
p.X, p.Y = newX, newY
}
}
dx = g.font.scale(g.scale * dx)
dy = g.font.scale(g.scale * dy)
if flags&flagRoundXYToGrid != 0 {
dx = (dx + 32) &^ 63
dy = (dy + 32) &^ 63
}
for j := np0; j < len(g.Points); j++ {
p := &g.Points[j]
p.X += dx
p.Y += dy
}
// TODO: also adjust g.InFontUnits and g.Unhinted?
if flags&flagMoreComponents == 0 {
break
}
}
instrLen := 0
if g.hinting != font.HintingNone && offset+2 <= len(glyf) {
instrLen = int(u16(glyf, offset))
offset += 2
}
g.addPhantomsAndScale(np0, len(g.Points), false, instrLen > 0)
points, ends := g.Points[np0:], g.Ends[ne0:]
g.Points = g.Points[:len(g.Points)-4]
for j := range points {
points[j].Flags &^= flagTouchedX | flagTouchedY
}
if instrLen == 0 {
if !g.metricsSet {
copy(g.phantomPoints[:], points[len(points)-4:])
}
return nil
}
// Hint the compound glyph.
program := glyf[offset : offset+instrLen]
// Temporarily adjust the ends to be relative to this compound glyph.
if np0 != 0 {
for i := range ends {
ends[i] -= np0
}
}
// Hinting instructions of a composite glyph completely refer to the
// (already) hinted subglyphs.
g.tmp = append(g.tmp[:0], points...)
if err := g.hinter.run(program, points, g.tmp, g.tmp, ends); err != nil {
return err
}
if np0 != 0 {
for i := range ends {
ends[i] += np0
}
}
if !g.metricsSet {
copy(g.phantomPoints[:], points[len(points)-4:])
}
return nil
}
func (g *GlyphBuf) addPhantomsAndScale(np0, np1 int, simple, adjust bool) {
// Add the four phantom points.
g.Points = append(g.Points, g.phantomPoints[:]...)
// Scale the points.
if simple && g.hinting != font.HintingNone {
g.InFontUnits = append(g.InFontUnits, g.Points[np1:]...)
}
for i := np1; i < len(g.Points); i++ {
p := &g.Points[i]
p.X = g.font.scale(g.scale * p.X)
p.Y = g.font.scale(g.scale * p.Y)
}
if g.hinting == font.HintingNone {
return
}
// Round the 1st phantom point to the grid, shifting all other points equally.
// Note that "all other points" starts from np0, not np1.
// TODO: delete this adjustment and the np0/np1 distinction, when
// we update the compatibility tests to C Freetype 2.5.3.
// See http://git.savannah.gnu.org/cgit/freetype/freetype2.git/commit/?id=05c786d990390a7ca18e62962641dac740bacb06
if adjust {
pp1x := g.Points[len(g.Points)-4].X
if dx := ((pp1x + 32) &^ 63) - pp1x; dx != 0 {
for i := np0; i < len(g.Points); i++ {
g.Points[i].X += dx
}
}
}
if simple {
g.Unhinted = append(g.Unhinted, g.Points[np1:]...)
}
// Round the 2nd and 4th phantom point to the grid.
p := &g.Points[len(g.Points)-3]
p.X = (p.X + 32) &^ 63
p = &g.Points[len(g.Points)-1]
p.Y = (p.Y + 32) &^ 63
}

1770
vendor/github.com/golang/freetype/truetype/hint.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

289
vendor/github.com/golang/freetype/truetype/opcodes.go generated vendored Normal file
View File

@ -0,0 +1,289 @@
// Copyright 2012 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
package truetype
// The Truetype opcodes are summarized at
// https://developer.apple.com/fonts/TTRefMan/RM07/appendixA.html
const (
opSVTCA0 = 0x00 // Set freedom and projection Vectors To Coordinate Axis
opSVTCA1 = 0x01 // .
opSPVTCA0 = 0x02 // Set Projection Vector To Coordinate Axis
opSPVTCA1 = 0x03 // .
opSFVTCA0 = 0x04 // Set Freedom Vector to Coordinate Axis
opSFVTCA1 = 0x05 // .
opSPVTL0 = 0x06 // Set Projection Vector To Line
opSPVTL1 = 0x07 // .
opSFVTL0 = 0x08 // Set Freedom Vector To Line
opSFVTL1 = 0x09 // .
opSPVFS = 0x0a // Set Projection Vector From Stack
opSFVFS = 0x0b // Set Freedom Vector From Stack
opGPV = 0x0c // Get Projection Vector
opGFV = 0x0d // Get Freedom Vector
opSFVTPV = 0x0e // Set Freedom Vector To Projection Vector
opISECT = 0x0f // moves point p to the InterSECTion of two lines
opSRP0 = 0x10 // Set Reference Point 0
opSRP1 = 0x11 // Set Reference Point 1
opSRP2 = 0x12 // Set Reference Point 2
opSZP0 = 0x13 // Set Zone Pointer 0
opSZP1 = 0x14 // Set Zone Pointer 1
opSZP2 = 0x15 // Set Zone Pointer 2
opSZPS = 0x16 // Set Zone PointerS
opSLOOP = 0x17 // Set LOOP variable
opRTG = 0x18 // Round To Grid
opRTHG = 0x19 // Round To Half Grid
opSMD = 0x1a // Set Minimum Distance
opELSE = 0x1b // ELSE clause
opJMPR = 0x1c // JuMP Relative
opSCVTCI = 0x1d // Set Control Value Table Cut-In
opSSWCI = 0x1e // Set Single Width Cut-In
opSSW = 0x1f // Set Single Width
opDUP = 0x20 // DUPlicate top stack element
opPOP = 0x21 // POP top stack element
opCLEAR = 0x22 // CLEAR the stack
opSWAP = 0x23 // SWAP the top two elements on the stack
opDEPTH = 0x24 // DEPTH of the stack
opCINDEX = 0x25 // Copy the INDEXed element to the top of the stack
opMINDEX = 0x26 // Move the INDEXed element to the top of the stack
opALIGNPTS = 0x27 // ALIGN PoinTS
op_0x28 = 0x28 // deprecated
opUTP = 0x29 // UnTouch Point
opLOOPCALL = 0x2a // LOOP and CALL function
opCALL = 0x2b // CALL function
opFDEF = 0x2c // Function DEFinition
opENDF = 0x2d // END Function definition
opMDAP0 = 0x2e // Move Direct Absolute Point
opMDAP1 = 0x2f // .
opIUP0 = 0x30 // Interpolate Untouched Points through the outline
opIUP1 = 0x31 // .
opSHP0 = 0x32 // SHift Point using reference point
opSHP1 = 0x33 // .
opSHC0 = 0x34 // SHift Contour using reference point
opSHC1 = 0x35 // .
opSHZ0 = 0x36 // SHift Zone using reference point
opSHZ1 = 0x37 // .
opSHPIX = 0x38 // SHift point by a PIXel amount
opIP = 0x39 // Interpolate Point
opMSIRP0 = 0x3a // Move Stack Indirect Relative Point
opMSIRP1 = 0x3b // .
opALIGNRP = 0x3c // ALIGN to Reference Point
opRTDG = 0x3d // Round To Double Grid
opMIAP0 = 0x3e // Move Indirect Absolute Point
opMIAP1 = 0x3f // .
opNPUSHB = 0x40 // PUSH N Bytes
opNPUSHW = 0x41 // PUSH N Words
opWS = 0x42 // Write Store
opRS = 0x43 // Read Store
opWCVTP = 0x44 // Write Control Value Table in Pixel units
opRCVT = 0x45 // Read Control Value Table entry
opGC0 = 0x46 // Get Coordinate projected onto the projection vector
opGC1 = 0x47 // .
opSCFS = 0x48 // Sets Coordinate From the Stack using projection vector and freedom vector
opMD0 = 0x49 // Measure Distance
opMD1 = 0x4a // .
opMPPEM = 0x4b // Measure Pixels Per EM
opMPS = 0x4c // Measure Point Size
opFLIPON = 0x4d // set the auto FLIP Boolean to ON
opFLIPOFF = 0x4e // set the auto FLIP Boolean to OFF
opDEBUG = 0x4f // DEBUG call
opLT = 0x50 // Less Than
opLTEQ = 0x51 // Less Than or EQual
opGT = 0x52 // Greater Than
opGTEQ = 0x53 // Greater Than or EQual
opEQ = 0x54 // EQual
opNEQ = 0x55 // Not EQual
opODD = 0x56 // ODD
opEVEN = 0x57 // EVEN
opIF = 0x58 // IF test
opEIF = 0x59 // End IF
opAND = 0x5a // logical AND
opOR = 0x5b // logical OR
opNOT = 0x5c // logical NOT
opDELTAP1 = 0x5d // DELTA exception P1
opSDB = 0x5e // Set Delta Base in the graphics state
opSDS = 0x5f // Set Delta Shift in the graphics state
opADD = 0x60 // ADD
opSUB = 0x61 // SUBtract
opDIV = 0x62 // DIVide
opMUL = 0x63 // MULtiply
opABS = 0x64 // ABSolute value
opNEG = 0x65 // NEGate
opFLOOR = 0x66 // FLOOR
opCEILING = 0x67 // CEILING
opROUND00 = 0x68 // ROUND value
opROUND01 = 0x69 // .
opROUND10 = 0x6a // .
opROUND11 = 0x6b // .
opNROUND00 = 0x6c // No ROUNDing of value
opNROUND01 = 0x6d // .
opNROUND10 = 0x6e // .
opNROUND11 = 0x6f // .
opWCVTF = 0x70 // Write Control Value Table in Funits
opDELTAP2 = 0x71 // DELTA exception P2
opDELTAP3 = 0x72 // DELTA exception P3
opDELTAC1 = 0x73 // DELTA exception C1
opDELTAC2 = 0x74 // DELTA exception C2
opDELTAC3 = 0x75 // DELTA exception C3
opSROUND = 0x76 // Super ROUND
opS45ROUND = 0x77 // Super ROUND 45 degrees
opJROT = 0x78 // Jump Relative On True
opJROF = 0x79 // Jump Relative On False
opROFF = 0x7a // Round OFF
op_0x7b = 0x7b // deprecated
opRUTG = 0x7c // Round Up To Grid
opRDTG = 0x7d // Round Down To Grid
opSANGW = 0x7e // Set ANGle Weight
opAA = 0x7f // Adjust Angle
opFLIPPT = 0x80 // FLIP PoinT
opFLIPRGON = 0x81 // FLIP RanGe ON
opFLIPRGOFF = 0x82 // FLIP RanGe OFF
op_0x83 = 0x83 // deprecated
op_0x84 = 0x84 // deprecated
opSCANCTRL = 0x85 // SCAN conversion ConTRoL
opSDPVTL0 = 0x86 // Set Dual Projection Vector To Line
opSDPVTL1 = 0x87 // .
opGETINFO = 0x88 // GET INFOrmation
opIDEF = 0x89 // Instruction DEFinition
opROLL = 0x8a // ROLL the top three stack elements
opMAX = 0x8b // MAXimum of top two stack elements
opMIN = 0x8c // MINimum of top two stack elements
opSCANTYPE = 0x8d // SCANTYPE
opINSTCTRL = 0x8e // INSTRuction execution ConTRoL
op_0x8f = 0x8f
op_0x90 = 0x90
op_0x91 = 0x91
op_0x92 = 0x92
op_0x93 = 0x93
op_0x94 = 0x94
op_0x95 = 0x95
op_0x96 = 0x96
op_0x97 = 0x97
op_0x98 = 0x98
op_0x99 = 0x99
op_0x9a = 0x9a
op_0x9b = 0x9b
op_0x9c = 0x9c
op_0x9d = 0x9d
op_0x9e = 0x9e
op_0x9f = 0x9f
op_0xa0 = 0xa0
op_0xa1 = 0xa1
op_0xa2 = 0xa2
op_0xa3 = 0xa3
op_0xa4 = 0xa4
op_0xa5 = 0xa5
op_0xa6 = 0xa6
op_0xa7 = 0xa7
op_0xa8 = 0xa8
op_0xa9 = 0xa9
op_0xaa = 0xaa
op_0xab = 0xab
op_0xac = 0xac
op_0xad = 0xad
op_0xae = 0xae
op_0xaf = 0xaf
opPUSHB000 = 0xb0 // PUSH Bytes
opPUSHB001 = 0xb1 // .
opPUSHB010 = 0xb2 // .
opPUSHB011 = 0xb3 // .
opPUSHB100 = 0xb4 // .
opPUSHB101 = 0xb5 // .
opPUSHB110 = 0xb6 // .
opPUSHB111 = 0xb7 // .
opPUSHW000 = 0xb8 // PUSH Words
opPUSHW001 = 0xb9 // .
opPUSHW010 = 0xba // .
opPUSHW011 = 0xbb // .
opPUSHW100 = 0xbc // .
opPUSHW101 = 0xbd // .
opPUSHW110 = 0xbe // .
opPUSHW111 = 0xbf // .
opMDRP00000 = 0xc0 // Move Direct Relative Point
opMDRP00001 = 0xc1 // .
opMDRP00010 = 0xc2 // .
opMDRP00011 = 0xc3 // .
opMDRP00100 = 0xc4 // .
opMDRP00101 = 0xc5 // .
opMDRP00110 = 0xc6 // .
opMDRP00111 = 0xc7 // .
opMDRP01000 = 0xc8 // .
opMDRP01001 = 0xc9 // .
opMDRP01010 = 0xca // .
opMDRP01011 = 0xcb // .
opMDRP01100 = 0xcc // .
opMDRP01101 = 0xcd // .
opMDRP01110 = 0xce // .
opMDRP01111 = 0xcf // .
opMDRP10000 = 0xd0 // .
opMDRP10001 = 0xd1 // .
opMDRP10010 = 0xd2 // .
opMDRP10011 = 0xd3 // .
opMDRP10100 = 0xd4 // .
opMDRP10101 = 0xd5 // .
opMDRP10110 = 0xd6 // .
opMDRP10111 = 0xd7 // .
opMDRP11000 = 0xd8 // .
opMDRP11001 = 0xd9 // .
opMDRP11010 = 0xda // .
opMDRP11011 = 0xdb // .
opMDRP11100 = 0xdc // .
opMDRP11101 = 0xdd // .
opMDRP11110 = 0xde // .
opMDRP11111 = 0xdf // .
opMIRP00000 = 0xe0 // Move Indirect Relative Point
opMIRP00001 = 0xe1 // .
opMIRP00010 = 0xe2 // .
opMIRP00011 = 0xe3 // .
opMIRP00100 = 0xe4 // .
opMIRP00101 = 0xe5 // .
opMIRP00110 = 0xe6 // .
opMIRP00111 = 0xe7 // .
opMIRP01000 = 0xe8 // .
opMIRP01001 = 0xe9 // .
opMIRP01010 = 0xea // .
opMIRP01011 = 0xeb // .
opMIRP01100 = 0xec // .
opMIRP01101 = 0xed // .
opMIRP01110 = 0xee // .
opMIRP01111 = 0xef // .
opMIRP10000 = 0xf0 // .
opMIRP10001 = 0xf1 // .
opMIRP10010 = 0xf2 // .
opMIRP10011 = 0xf3 // .
opMIRP10100 = 0xf4 // .
opMIRP10101 = 0xf5 // .
opMIRP10110 = 0xf6 // .
opMIRP10111 = 0xf7 // .
opMIRP11000 = 0xf8 // .
opMIRP11001 = 0xf9 // .
opMIRP11010 = 0xfa // .
opMIRP11011 = 0xfb // .
opMIRP11100 = 0xfc // .
opMIRP11101 = 0xfd // .
opMIRP11110 = 0xfe // .
opMIRP11111 = 0xff // .
)
// popCount is the number of stack elements that each opcode pops.
var popCount = [256]uint8{
// 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f
0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 5, // 0x00 - 0x0f
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, // 0x10 - 0x1f
1, 1, 0, 2, 0, 1, 1, 2, 0, 1, 2, 1, 1, 0, 1, 1, // 0x20 - 0x2f
0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 2, 2, 0, 0, 2, 2, // 0x30 - 0x3f
0, 0, 2, 1, 2, 1, 1, 1, 2, 2, 2, 0, 0, 0, 0, 0, // 0x40 - 0x4f
2, 2, 2, 2, 2, 2, 1, 1, 1, 0, 2, 2, 1, 1, 1, 1, // 0x50 - 0x5f
2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6f
2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 0, 0, 0, 0, 1, 1, // 0x70 - 0x7f
0, 2, 2, 0, 0, 1, 2, 2, 1, 1, 3, 2, 2, 1, 2, 0, // 0x80 - 0x8f
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90 - 0x9f
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xa0 - 0xaf
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xb0 - 0xbf
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xc0 - 0xcf
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xd0 - 0xdf
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xe0 - 0xef
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xf0 - 0xff
}

653
vendor/github.com/golang/freetype/truetype/truetype.go generated vendored Normal file
View File

@ -0,0 +1,653 @@
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
// Package truetype provides a parser for the TTF and TTC file formats.
// Those formats are documented at http://developer.apple.com/fonts/TTRefMan/
// and http://www.microsoft.com/typography/otspec/
//
// Some of a font's methods provide lengths or co-ordinates, e.g. bounds, font
// metrics and control points. All these methods take a scale parameter, which
// is the number of pixels in 1 em, expressed as a 26.6 fixed point value. For
// example, if 1 em is 10 pixels then scale is fixed.I(10), which is equal to
// fixed.Int26_6(10 << 6).
//
// To measure a TrueType font in ideal FUnit space, use scale equal to
// font.FUnitsPerEm().
package truetype // import "github.com/golang/freetype/truetype"
import (
"fmt"
"golang.org/x/image/math/fixed"
)
// An Index is a Font's index of a rune.
type Index uint16
// A NameID identifies a name table entry.
//
// See https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html
type NameID uint16
const (
NameIDCopyright NameID = 0
NameIDFontFamily = 1
NameIDFontSubfamily = 2
NameIDUniqueSubfamilyID = 3
NameIDFontFullName = 4
NameIDNameTableVersion = 5
NameIDPostscriptName = 6
NameIDTrademarkNotice = 7
NameIDManufacturerName = 8
NameIDDesignerName = 9
NameIDFontDescription = 10
NameIDFontVendorURL = 11
NameIDFontDesignerURL = 12
NameIDFontLicense = 13
NameIDFontLicenseURL = 14
NameIDPreferredFamily = 16
NameIDPreferredSubfamily = 17
NameIDCompatibleName = 18
NameIDSampleText = 19
)
const (
// A 32-bit encoding consists of a most-significant 16-bit Platform ID and a
// least-significant 16-bit Platform Specific ID. The magic numbers are
// specified at https://www.microsoft.com/typography/otspec/name.htm
unicodeEncodingBMPOnly = 0x00000003 // PID = 0 (Unicode), PSID = 3 (Unicode 2.0 BMP Only)
unicodeEncodingFull = 0x00000004 // PID = 0 (Unicode), PSID = 4 (Unicode 2.0 Full Repertoire)
microsoftSymbolEncoding = 0x00030000 // PID = 3 (Microsoft), PSID = 0 (Symbol)
microsoftUCS2Encoding = 0x00030001 // PID = 3 (Microsoft), PSID = 1 (UCS-2)
microsoftUCS4Encoding = 0x0003000a // PID = 3 (Microsoft), PSID = 10 (UCS-4)
)
// An HMetric holds the horizontal metrics of a single glyph.
type HMetric struct {
AdvanceWidth, LeftSideBearing fixed.Int26_6
}
// A VMetric holds the vertical metrics of a single glyph.
type VMetric struct {
AdvanceHeight, TopSideBearing fixed.Int26_6
}
// A FormatError reports that the input is not a valid TrueType font.
type FormatError string
func (e FormatError) Error() string {
return "freetype: invalid TrueType format: " + string(e)
}
// An UnsupportedError reports that the input uses a valid but unimplemented
// TrueType feature.
type UnsupportedError string
func (e UnsupportedError) Error() string {
return "freetype: unsupported TrueType feature: " + string(e)
}
// u32 returns the big-endian uint32 at b[i:].
func u32(b []byte, i int) uint32 {
return uint32(b[i])<<24 | uint32(b[i+1])<<16 | uint32(b[i+2])<<8 | uint32(b[i+3])
}
// u16 returns the big-endian uint16 at b[i:].
func u16(b []byte, i int) uint16 {
return uint16(b[i])<<8 | uint16(b[i+1])
}
// readTable returns a slice of the TTF data given by a table's directory entry.
func readTable(ttf []byte, offsetLength []byte) ([]byte, error) {
offset := int(u32(offsetLength, 0))
if offset < 0 {
return nil, FormatError(fmt.Sprintf("offset too large: %d", uint32(offset)))
}
length := int(u32(offsetLength, 4))
if length < 0 {
return nil, FormatError(fmt.Sprintf("length too large: %d", uint32(length)))
}
end := offset + length
if end < 0 || end > len(ttf) {
return nil, FormatError(fmt.Sprintf("offset + length too large: %d", uint32(offset)+uint32(length)))
}
return ttf[offset:end], nil
}
// parseSubtables returns the offset and platformID of the best subtable in
// table, where best favors a Unicode cmap encoding, and failing that, a
// Microsoft cmap encoding. offset is the offset of the first subtable in
// table, and size is the size of each subtable.
//
// If pred is non-nil, then only subtables that satisfy that predicate will be
// considered.
func parseSubtables(table []byte, name string, offset, size int, pred func([]byte) bool) (
bestOffset int, bestPID uint32, retErr error) {
if len(table) < 4 {
return 0, 0, FormatError(name + " too short")
}
nSubtables := int(u16(table, 2))
if len(table) < size*nSubtables+offset {
return 0, 0, FormatError(name + " too short")
}
ok := false
for i := 0; i < nSubtables; i, offset = i+1, offset+size {
if pred != nil && !pred(table[offset:]) {
continue
}
// We read the 16-bit Platform ID and 16-bit Platform Specific ID as a single uint32.
// All values are big-endian.
pidPsid := u32(table, offset)
// We prefer the Unicode cmap encoding. Failing to find that, we fall
// back onto the Microsoft cmap encoding.
if pidPsid == unicodeEncodingBMPOnly || pidPsid == unicodeEncodingFull {
bestOffset, bestPID, ok = offset, pidPsid>>16, true
break
} else if pidPsid == microsoftSymbolEncoding ||
pidPsid == microsoftUCS2Encoding ||
pidPsid == microsoftUCS4Encoding {
bestOffset, bestPID, ok = offset, pidPsid>>16, true
// We don't break out of the for loop, so that Unicode can override Microsoft.
}
}
if !ok {
return 0, 0, UnsupportedError(name + " encoding")
}
return bestOffset, bestPID, nil
}
const (
locaOffsetFormatUnknown int = iota
locaOffsetFormatShort
locaOffsetFormatLong
)
// A cm holds a parsed cmap entry.
type cm struct {
start, end, delta, offset uint32
}
// A Font represents a Truetype font.
type Font struct {
// Tables sliced from the TTF data. The different tables are documented
// at http://developer.apple.com/fonts/TTRefMan/RM06/Chap6.html
cmap, cvt, fpgm, glyf, hdmx, head, hhea, hmtx, kern, loca, maxp, name, os2, prep, vmtx []byte
cmapIndexes []byte
// Cached values derived from the raw ttf data.
cm []cm
locaOffsetFormat int
nGlyph, nHMetric, nKern int
fUnitsPerEm int32
ascent int32 // In FUnits.
descent int32 // In FUnits; typically negative.
bounds fixed.Rectangle26_6 // In FUnits.
// Values from the maxp section.
maxTwilightPoints, maxStorage, maxFunctionDefs, maxStackElements uint16
}
func (f *Font) parseCmap() error {
const (
cmapFormat4 = 4
cmapFormat12 = 12
languageIndependent = 0
)
offset, _, err := parseSubtables(f.cmap, "cmap", 4, 8, nil)
if err != nil {
return err
}
offset = int(u32(f.cmap, offset+4))
if offset <= 0 || offset > len(f.cmap) {
return FormatError("bad cmap offset")
}
cmapFormat := u16(f.cmap, offset)
switch cmapFormat {
case cmapFormat4:
language := u16(f.cmap, offset+4)
if language != languageIndependent {
return UnsupportedError(fmt.Sprintf("language: %d", language))
}
segCountX2 := int(u16(f.cmap, offset+6))
if segCountX2%2 == 1 {
return FormatError(fmt.Sprintf("bad segCountX2: %d", segCountX2))
}
segCount := segCountX2 / 2
offset += 14
f.cm = make([]cm, segCount)
for i := 0; i < segCount; i++ {
f.cm[i].end = uint32(u16(f.cmap, offset))
offset += 2
}
offset += 2
for i := 0; i < segCount; i++ {
f.cm[i].start = uint32(u16(f.cmap, offset))
offset += 2
}
for i := 0; i < segCount; i++ {
f.cm[i].delta = uint32(u16(f.cmap, offset))
offset += 2
}
for i := 0; i < segCount; i++ {
f.cm[i].offset = uint32(u16(f.cmap, offset))
offset += 2
}
f.cmapIndexes = f.cmap[offset:]
return nil
case cmapFormat12:
if u16(f.cmap, offset+2) != 0 {
return FormatError(fmt.Sprintf("cmap format: % x", f.cmap[offset:offset+4]))
}
length := u32(f.cmap, offset+4)
language := u32(f.cmap, offset+8)
if language != languageIndependent {
return UnsupportedError(fmt.Sprintf("language: %d", language))
}
nGroups := u32(f.cmap, offset+12)
if length != 12*nGroups+16 {
return FormatError("inconsistent cmap length")
}
offset += 16
f.cm = make([]cm, nGroups)
for i := uint32(0); i < nGroups; i++ {
f.cm[i].start = u32(f.cmap, offset+0)
f.cm[i].end = u32(f.cmap, offset+4)
f.cm[i].delta = u32(f.cmap, offset+8) - f.cm[i].start
offset += 12
}
return nil
}
return UnsupportedError(fmt.Sprintf("cmap format: %d", cmapFormat))
}
func (f *Font) parseHead() error {
if len(f.head) != 54 {
return FormatError(fmt.Sprintf("bad head length: %d", len(f.head)))
}
f.fUnitsPerEm = int32(u16(f.head, 18))
f.bounds.Min.X = fixed.Int26_6(int16(u16(f.head, 36)))
f.bounds.Min.Y = fixed.Int26_6(int16(u16(f.head, 38)))
f.bounds.Max.X = fixed.Int26_6(int16(u16(f.head, 40)))
f.bounds.Max.Y = fixed.Int26_6(int16(u16(f.head, 42)))
switch i := u16(f.head, 50); i {
case 0:
f.locaOffsetFormat = locaOffsetFormatShort
case 1:
f.locaOffsetFormat = locaOffsetFormatLong
default:
return FormatError(fmt.Sprintf("bad indexToLocFormat: %d", i))
}
return nil
}
func (f *Font) parseHhea() error {
if len(f.hhea) != 36 {
return FormatError(fmt.Sprintf("bad hhea length: %d", len(f.hhea)))
}
f.ascent = int32(int16(u16(f.hhea, 4)))
f.descent = int32(int16(u16(f.hhea, 6)))
f.nHMetric = int(u16(f.hhea, 34))
if 4*f.nHMetric+2*(f.nGlyph-f.nHMetric) != len(f.hmtx) {
return FormatError(fmt.Sprintf("bad hmtx length: %d", len(f.hmtx)))
}
return nil
}
func (f *Font) parseKern() error {
// Apple's TrueType documentation (http://developer.apple.com/fonts/TTRefMan/RM06/Chap6kern.html) says:
// "Previous versions of the 'kern' table defined both the version and nTables fields in the header
// as UInt16 values and not UInt32 values. Use of the older format on the Mac OS is discouraged
// (although AAT can sense an old kerning table and still make correct use of it). Microsoft
// Windows still uses the older format for the 'kern' table and will not recognize the newer one.
// Fonts targeted for the Mac OS only should use the new format; fonts targeted for both the Mac OS
// and Windows should use the old format."
// Since we expect that almost all fonts aim to be Windows-compatible, we only parse the "older" format,
// just like the C Freetype implementation.
if len(f.kern) == 0 {
if f.nKern != 0 {
return FormatError("bad kern table length")
}
return nil
}
if len(f.kern) < 18 {
return FormatError("kern data too short")
}
version, offset := u16(f.kern, 0), 2
if version != 0 {
return UnsupportedError(fmt.Sprintf("kern version: %d", version))
}
n, offset := u16(f.kern, offset), offset+2
if n == 0 {
return UnsupportedError("kern nTables: 0")
}
// TODO: support multiple subtables. In practice, almost all .ttf files
// have only one subtable, if they have a kern table at all. But it's not
// impossible. Xolonium Regular (https://fontlibrary.org/en/font/xolonium)
// has 3 subtables. Those subtables appear to be disjoint, rather than
// being the same kerning pairs encoded in three different ways.
//
// For now, we'll use only the first subtable.
offset += 2 // Skip the version.
length, offset := int(u16(f.kern, offset)), offset+2
coverage, offset := u16(f.kern, offset), offset+2
if coverage != 0x0001 {
// We only support horizontal kerning.
return UnsupportedError(fmt.Sprintf("kern coverage: 0x%04x", coverage))
}
f.nKern, offset = int(u16(f.kern, offset)), offset+2
if 6*f.nKern != length-14 {
return FormatError("bad kern table length")
}
return nil
}
func (f *Font) parseMaxp() error {
if len(f.maxp) != 32 {
return FormatError(fmt.Sprintf("bad maxp length: %d", len(f.maxp)))
}
f.nGlyph = int(u16(f.maxp, 4))
f.maxTwilightPoints = u16(f.maxp, 16)
f.maxStorage = u16(f.maxp, 18)
f.maxFunctionDefs = u16(f.maxp, 20)
f.maxStackElements = u16(f.maxp, 24)
return nil
}
// scale returns x divided by f.fUnitsPerEm, rounded to the nearest integer.
func (f *Font) scale(x fixed.Int26_6) fixed.Int26_6 {
if x >= 0 {
x += fixed.Int26_6(f.fUnitsPerEm) / 2
} else {
x -= fixed.Int26_6(f.fUnitsPerEm) / 2
}
return x / fixed.Int26_6(f.fUnitsPerEm)
}
// Bounds returns the union of a Font's glyphs' bounds.
func (f *Font) Bounds(scale fixed.Int26_6) fixed.Rectangle26_6 {
b := f.bounds
b.Min.X = f.scale(scale * b.Min.X)
b.Min.Y = f.scale(scale * b.Min.Y)
b.Max.X = f.scale(scale * b.Max.X)
b.Max.Y = f.scale(scale * b.Max.Y)
return b
}
// FUnitsPerEm returns the number of FUnits in a Font's em-square's side.
func (f *Font) FUnitsPerEm() int32 {
return f.fUnitsPerEm
}
// Index returns a Font's index for the given rune.
func (f *Font) Index(x rune) Index {
c := uint32(x)
for i, j := 0, len(f.cm); i < j; {
h := i + (j-i)/2
cm := &f.cm[h]
if c < cm.start {
j = h
} else if cm.end < c {
i = h + 1
} else if cm.offset == 0 {
return Index(c + cm.delta)
} else {
offset := int(cm.offset) + 2*(h-len(f.cm)+int(c-cm.start))
return Index(u16(f.cmapIndexes, offset))
}
}
return 0
}
// Name returns the Font's name value for the given NameID. It returns "" if
// there was an error, or if that name was not found.
func (f *Font) Name(id NameID) string {
x, platformID, err := parseSubtables(f.name, "name", 6, 12, func(b []byte) bool {
return NameID(u16(b, 6)) == id
})
if err != nil {
return ""
}
offset, length := u16(f.name, 4)+u16(f.name, x+10), u16(f.name, x+8)
// Return the ASCII value of the encoded string.
// The string is encoded as UTF-16 on non-Apple platformIDs; Apple is platformID 1.
src := f.name[offset : offset+length]
var dst []byte
if platformID != 1 { // UTF-16.
if len(src)&1 != 0 {
return ""
}
dst = make([]byte, len(src)/2)
for i := range dst {
dst[i] = printable(u16(src, 2*i))
}
} else { // ASCII.
dst = make([]byte, len(src))
for i, c := range src {
dst[i] = printable(uint16(c))
}
}
return string(dst)
}
func printable(r uint16) byte {
if 0x20 <= r && r < 0x7f {
return byte(r)
}
return '?'
}
// unscaledHMetric returns the unscaled horizontal metrics for the glyph with
// the given index.
func (f *Font) unscaledHMetric(i Index) (h HMetric) {
j := int(i)
if j < 0 || f.nGlyph <= j {
return HMetric{}
}
if j >= f.nHMetric {
p := 4 * (f.nHMetric - 1)
return HMetric{
AdvanceWidth: fixed.Int26_6(u16(f.hmtx, p)),
LeftSideBearing: fixed.Int26_6(int16(u16(f.hmtx, p+2*(j-f.nHMetric)+4))),
}
}
return HMetric{
AdvanceWidth: fixed.Int26_6(u16(f.hmtx, 4*j)),
LeftSideBearing: fixed.Int26_6(int16(u16(f.hmtx, 4*j+2))),
}
}
// HMetric returns the horizontal metrics for the glyph with the given index.
func (f *Font) HMetric(scale fixed.Int26_6, i Index) HMetric {
h := f.unscaledHMetric(i)
h.AdvanceWidth = f.scale(scale * h.AdvanceWidth)
h.LeftSideBearing = f.scale(scale * h.LeftSideBearing)
return h
}
// unscaledVMetric returns the unscaled vertical metrics for the glyph with
// the given index. yMax is the top of the glyph's bounding box.
func (f *Font) unscaledVMetric(i Index, yMax fixed.Int26_6) (v VMetric) {
j := int(i)
if j < 0 || f.nGlyph <= j {
return VMetric{}
}
if 4*j+4 <= len(f.vmtx) {
return VMetric{
AdvanceHeight: fixed.Int26_6(u16(f.vmtx, 4*j)),
TopSideBearing: fixed.Int26_6(int16(u16(f.vmtx, 4*j+2))),
}
}
// The OS/2 table has grown over time.
// https://developer.apple.com/fonts/TTRefMan/RM06/Chap6OS2.html
// says that it was originally 68 bytes. Optional fields, including
// the ascender and descender, are described at
// http://www.microsoft.com/typography/otspec/os2.htm
if len(f.os2) >= 72 {
sTypoAscender := fixed.Int26_6(int16(u16(f.os2, 68)))
sTypoDescender := fixed.Int26_6(int16(u16(f.os2, 70)))
return VMetric{
AdvanceHeight: sTypoAscender - sTypoDescender,
TopSideBearing: sTypoAscender - yMax,
}
}
return VMetric{
AdvanceHeight: fixed.Int26_6(f.fUnitsPerEm),
TopSideBearing: 0,
}
}
// VMetric returns the vertical metrics for the glyph with the given index.
func (f *Font) VMetric(scale fixed.Int26_6, i Index) VMetric {
// TODO: should 0 be bounds.YMax?
v := f.unscaledVMetric(i, 0)
v.AdvanceHeight = f.scale(scale * v.AdvanceHeight)
v.TopSideBearing = f.scale(scale * v.TopSideBearing)
return v
}
// Kern returns the horizontal adjustment for the given glyph pair. A positive
// kern means to move the glyphs further apart.
func (f *Font) Kern(scale fixed.Int26_6, i0, i1 Index) fixed.Int26_6 {
if f.nKern == 0 {
return 0
}
g := uint32(i0)<<16 | uint32(i1)
lo, hi := 0, f.nKern
for lo < hi {
i := (lo + hi) / 2
ig := u32(f.kern, 18+6*i)
if ig < g {
lo = i + 1
} else if ig > g {
hi = i
} else {
return f.scale(scale * fixed.Int26_6(int16(u16(f.kern, 22+6*i))))
}
}
return 0
}
// Parse returns a new Font for the given TTF or TTC data.
//
// For TrueType Collections, the first font in the collection is parsed.
func Parse(ttf []byte) (font *Font, err error) {
return parse(ttf, 0)
}
func parse(ttf []byte, offset int) (font *Font, err error) {
if len(ttf)-offset < 12 {
err = FormatError("TTF data is too short")
return
}
originalOffset := offset
magic, offset := u32(ttf, offset), offset+4
switch magic {
case 0x00010000:
// No-op.
case 0x74746366: // "ttcf" as a big-endian uint32.
if originalOffset != 0 {
err = FormatError("recursive TTC")
return
}
ttcVersion, offset := u32(ttf, offset), offset+4
if ttcVersion != 0x00010000 && ttcVersion != 0x00020000 {
err = FormatError("bad TTC version")
return
}
numFonts, offset := int(u32(ttf, offset)), offset+4
if numFonts <= 0 {
err = FormatError("bad number of TTC fonts")
return
}
if len(ttf[offset:])/4 < numFonts {
err = FormatError("TTC offset table is too short")
return
}
// TODO: provide an API to select which font in a TrueType collection to return,
// not just the first one. This may require an API to parse a TTC's name tables,
// so users of this package can select the font in a TTC by name.
offset = int(u32(ttf, offset))
if offset <= 0 || offset > len(ttf) {
err = FormatError("bad TTC offset")
return
}
return parse(ttf, offset)
default:
err = FormatError("bad TTF version")
return
}
n, offset := int(u16(ttf, offset)), offset+2
offset += 6 // Skip the searchRange, entrySelector and rangeShift.
if len(ttf) < 16*n+offset {
err = FormatError("TTF data is too short")
return
}
f := new(Font)
// Assign the table slices.
for i := 0; i < n; i++ {
x := 16*i + offset
switch string(ttf[x : x+4]) {
case "cmap":
f.cmap, err = readTable(ttf, ttf[x+8:x+16])
case "cvt ":
f.cvt, err = readTable(ttf, ttf[x+8:x+16])
case "fpgm":
f.fpgm, err = readTable(ttf, ttf[x+8:x+16])
case "glyf":
f.glyf, err = readTable(ttf, ttf[x+8:x+16])
case "hdmx":
f.hdmx, err = readTable(ttf, ttf[x+8:x+16])
case "head":
f.head, err = readTable(ttf, ttf[x+8:x+16])
case "hhea":
f.hhea, err = readTable(ttf, ttf[x+8:x+16])
case "hmtx":
f.hmtx, err = readTable(ttf, ttf[x+8:x+16])
case "kern":
f.kern, err = readTable(ttf, ttf[x+8:x+16])
case "loca":
f.loca, err = readTable(ttf, ttf[x+8:x+16])
case "maxp":
f.maxp, err = readTable(ttf, ttf[x+8:x+16])
case "name":
f.name, err = readTable(ttf, ttf[x+8:x+16])
case "OS/2":
f.os2, err = readTable(ttf, ttf[x+8:x+16])
case "prep":
f.prep, err = readTable(ttf, ttf[x+8:x+16])
case "vmtx":
f.vmtx, err = readTable(ttf, ttf[x+8:x+16])
}
if err != nil {
return
}
}
// Parse and sanity-check the TTF data.
if err = f.parseHead(); err != nil {
return
}
if err = f.parseMaxp(); err != nil {
return
}
if err = f.parseCmap(); err != nil {
return
}
if err = f.parseKern(); err != nil {
return
}
if err = f.parseHhea(); err != nil {
return
}
font = f
return
}

15
vendor/go.uber.org/atomic/.codecov.yml generated vendored Normal file
View File

@ -0,0 +1,15 @@
coverage:
range: 80..100
round: down
precision: 2
status:
project: # measuring the overall project coverage
default: # context, you can create multiple ones with custom titles
enabled: yes # must be yes|true to enable this status
target: 100 # specify the target coverage for each commit status
# option: "auto" (must increase from parent commit or pull request base)
# option: "X%" a static target percentage to hit
if_not_found: success # if parent is not found report status as success, error, or failure
if_ci_failed: error # if ci fails report status as success, error, or failure

11
vendor/go.uber.org/atomic/.gitignore generated vendored Normal file
View File

@ -0,0 +1,11 @@
.DS_Store
/vendor
/cover
cover.out
lint.log
# Binaries
*.test
# Profiling output
*.prof

23
vendor/go.uber.org/atomic/.travis.yml generated vendored Normal file
View File

@ -0,0 +1,23 @@
sudo: false
language: go
go_import_path: go.uber.org/atomic
go:
- 1.7
- 1.8
- 1.9
cache:
directories:
- vendor
install:
- make install_ci
script:
- make test_ci
- scripts/test-ubergo.sh
- make lint
after_success:
- bash <(curl -s https://codecov.io/bash)

19
vendor/go.uber.org/atomic/LICENSE.txt generated vendored Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2016 Uber Technologies, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

64
vendor/go.uber.org/atomic/Makefile generated vendored Normal file
View File

@ -0,0 +1,64 @@
PACKAGES := $(shell glide nv)
# Many Go tools take file globs or directories as arguments instead of packages.
PACKAGE_FILES ?= *.go
# The linting tools evolve with each Go version, so run them only on the latest
# stable release.
GO_VERSION := $(shell go version | cut -d " " -f 3)
GO_MINOR_VERSION := $(word 2,$(subst ., ,$(GO_VERSION)))
LINTABLE_MINOR_VERSIONS := 7 8
ifneq ($(filter $(LINTABLE_MINOR_VERSIONS),$(GO_MINOR_VERSION)),)
SHOULD_LINT := true
endif
export GO15VENDOREXPERIMENT=1
.PHONY: build
build:
go build -i $(PACKAGES)
.PHONY: install
install:
glide --version || go get github.com/Masterminds/glide
glide install
.PHONY: test
test:
go test -cover -race $(PACKAGES)
.PHONY: install_ci
install_ci: install
go get github.com/wadey/gocovmerge
go get github.com/mattn/goveralls
go get golang.org/x/tools/cmd/cover
ifdef SHOULD_LINT
go get github.com/golang/lint/golint
endif
.PHONY: lint
lint:
ifdef SHOULD_LINT
@rm -rf lint.log
@echo "Checking formatting..."
@gofmt -d -s $(PACKAGE_FILES) 2>&1 | tee lint.log
@echo "Checking vet..."
@$(foreach dir,$(PACKAGE_FILES),go tool vet $(dir) 2>&1 | tee -a lint.log;)
@echo "Checking lint..."
@$(foreach dir,$(PKGS),golint $(dir) 2>&1 | tee -a lint.log;)
@echo "Checking for unresolved FIXMEs..."
@git grep -i fixme | grep -v -e vendor -e Makefile | tee -a lint.log
@[ ! -s lint.log ]
else
@echo "Skipping linters on" $(GO_VERSION)
endif
.PHONY: test_ci
test_ci: install_ci build
./scripts/cover.sh $(shell go list $(PACKAGES))

36
vendor/go.uber.org/atomic/README.md generated vendored Normal file
View File

@ -0,0 +1,36 @@
# atomic [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![Go Report Card][reportcard-img]][reportcard]
Simple wrappers for primitive types to enforce atomic access.
## Installation
`go get -u go.uber.org/atomic`
## Usage
The standard library's `sync/atomic` is powerful, but it's easy to forget which
variables must be accessed atomically. `go.uber.org/atomic` preserves all the
functionality of the standard library, but wraps the primitive types to
provide a safer, more convenient API.
```go
var atom atomic.Uint32
atom.Store(42)
atom.Sub(2)
atom.CAS(40, 11)
```
See the [documentation][doc] for a complete API specification.
## Development Status
Stable.
<hr>
Released under the [MIT License](LICENSE.txt).
[doc-img]: https://godoc.org/github.com/uber-go/atomic?status.svg
[doc]: https://godoc.org/go.uber.org/atomic
[ci-img]: https://travis-ci.org/uber-go/atomic.svg?branch=master
[ci]: https://travis-ci.org/uber-go/atomic
[cov-img]: https://codecov.io/gh/uber-go/atomic/branch/master/graph/badge.svg
[cov]: https://codecov.io/gh/uber-go/atomic
[reportcard-img]: https://goreportcard.com/badge/go.uber.org/atomic
[reportcard]: https://goreportcard.com/report/go.uber.org/atomic

351
vendor/go.uber.org/atomic/atomic.go generated vendored Normal file
View File

@ -0,0 +1,351 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Package atomic provides simple wrappers around numerics to enforce atomic
// access.
package atomic
import (
"math"
"sync/atomic"
"time"
)
// Int32 is an atomic wrapper around an int32.
type Int32 struct{ v int32 }
// NewInt32 creates an Int32.
func NewInt32(i int32) *Int32 {
return &Int32{i}
}
// Load atomically loads the wrapped value.
func (i *Int32) Load() int32 {
return atomic.LoadInt32(&i.v)
}
// Add atomically adds to the wrapped int32 and returns the new value.
func (i *Int32) Add(n int32) int32 {
return atomic.AddInt32(&i.v, n)
}
// Sub atomically subtracts from the wrapped int32 and returns the new value.
func (i *Int32) Sub(n int32) int32 {
return atomic.AddInt32(&i.v, -n)
}
// Inc atomically increments the wrapped int32 and returns the new value.
func (i *Int32) Inc() int32 {
return i.Add(1)
}
// Dec atomically decrements the wrapped int32 and returns the new value.
func (i *Int32) Dec() int32 {
return i.Sub(1)
}
// CAS is an atomic compare-and-swap.
func (i *Int32) CAS(old, new int32) bool {
return atomic.CompareAndSwapInt32(&i.v, old, new)
}
// Store atomically stores the passed value.
func (i *Int32) Store(n int32) {
atomic.StoreInt32(&i.v, n)
}
// Swap atomically swaps the wrapped int32 and returns the old value.
func (i *Int32) Swap(n int32) int32 {
return atomic.SwapInt32(&i.v, n)
}
// Int64 is an atomic wrapper around an int64.
type Int64 struct{ v int64 }
// NewInt64 creates an Int64.
func NewInt64(i int64) *Int64 {
return &Int64{i}
}
// Load atomically loads the wrapped value.
func (i *Int64) Load() int64 {
return atomic.LoadInt64(&i.v)
}
// Add atomically adds to the wrapped int64 and returns the new value.
func (i *Int64) Add(n int64) int64 {
return atomic.AddInt64(&i.v, n)
}
// Sub atomically subtracts from the wrapped int64 and returns the new value.
func (i *Int64) Sub(n int64) int64 {
return atomic.AddInt64(&i.v, -n)
}
// Inc atomically increments the wrapped int64 and returns the new value.
func (i *Int64) Inc() int64 {
return i.Add(1)
}
// Dec atomically decrements the wrapped int64 and returns the new value.
func (i *Int64) Dec() int64 {
return i.Sub(1)
}
// CAS is an atomic compare-and-swap.
func (i *Int64) CAS(old, new int64) bool {
return atomic.CompareAndSwapInt64(&i.v, old, new)
}
// Store atomically stores the passed value.
func (i *Int64) Store(n int64) {
atomic.StoreInt64(&i.v, n)
}
// Swap atomically swaps the wrapped int64 and returns the old value.
func (i *Int64) Swap(n int64) int64 {
return atomic.SwapInt64(&i.v, n)
}
// Uint32 is an atomic wrapper around an uint32.
type Uint32 struct{ v uint32 }
// NewUint32 creates a Uint32.
func NewUint32(i uint32) *Uint32 {
return &Uint32{i}
}
// Load atomically loads the wrapped value.
func (i *Uint32) Load() uint32 {
return atomic.LoadUint32(&i.v)
}
// Add atomically adds to the wrapped uint32 and returns the new value.
func (i *Uint32) Add(n uint32) uint32 {
return atomic.AddUint32(&i.v, n)
}
// Sub atomically subtracts from the wrapped uint32 and returns the new value.
func (i *Uint32) Sub(n uint32) uint32 {
return atomic.AddUint32(&i.v, ^(n - 1))
}
// Inc atomically increments the wrapped uint32 and returns the new value.
func (i *Uint32) Inc() uint32 {
return i.Add(1)
}
// Dec atomically decrements the wrapped int32 and returns the new value.
func (i *Uint32) Dec() uint32 {
return i.Sub(1)
}
// CAS is an atomic compare-and-swap.
func (i *Uint32) CAS(old, new uint32) bool {
return atomic.CompareAndSwapUint32(&i.v, old, new)
}
// Store atomically stores the passed value.
func (i *Uint32) Store(n uint32) {
atomic.StoreUint32(&i.v, n)
}
// Swap atomically swaps the wrapped uint32 and returns the old value.
func (i *Uint32) Swap(n uint32) uint32 {
return atomic.SwapUint32(&i.v, n)
}
// Uint64 is an atomic wrapper around a uint64.
type Uint64 struct{ v uint64 }
// NewUint64 creates a Uint64.
func NewUint64(i uint64) *Uint64 {
return &Uint64{i}
}
// Load atomically loads the wrapped value.
func (i *Uint64) Load() uint64 {
return atomic.LoadUint64(&i.v)
}
// Add atomically adds to the wrapped uint64 and returns the new value.
func (i *Uint64) Add(n uint64) uint64 {
return atomic.AddUint64(&i.v, n)
}
// Sub atomically subtracts from the wrapped uint64 and returns the new value.
func (i *Uint64) Sub(n uint64) uint64 {
return atomic.AddUint64(&i.v, ^(n - 1))
}
// Inc atomically increments the wrapped uint64 and returns the new value.
func (i *Uint64) Inc() uint64 {
return i.Add(1)
}
// Dec atomically decrements the wrapped uint64 and returns the new value.
func (i *Uint64) Dec() uint64 {
return i.Sub(1)
}
// CAS is an atomic compare-and-swap.
func (i *Uint64) CAS(old, new uint64) bool {
return atomic.CompareAndSwapUint64(&i.v, old, new)
}
// Store atomically stores the passed value.
func (i *Uint64) Store(n uint64) {
atomic.StoreUint64(&i.v, n)
}
// Swap atomically swaps the wrapped uint64 and returns the old value.
func (i *Uint64) Swap(n uint64) uint64 {
return atomic.SwapUint64(&i.v, n)
}
// Bool is an atomic Boolean.
type Bool struct{ v uint32 }
// NewBool creates a Bool.
func NewBool(initial bool) *Bool {
return &Bool{boolToInt(initial)}
}
// Load atomically loads the Boolean.
func (b *Bool) Load() bool {
return truthy(atomic.LoadUint32(&b.v))
}
// CAS is an atomic compare-and-swap.
func (b *Bool) CAS(old, new bool) bool {
return atomic.CompareAndSwapUint32(&b.v, boolToInt(old), boolToInt(new))
}
// Store atomically stores the passed value.
func (b *Bool) Store(new bool) {
atomic.StoreUint32(&b.v, boolToInt(new))
}
// Swap sets the given value and returns the previous value.
func (b *Bool) Swap(new bool) bool {
return truthy(atomic.SwapUint32(&b.v, boolToInt(new)))
}
// Toggle atomically negates the Boolean and returns the previous value.
func (b *Bool) Toggle() bool {
return truthy(atomic.AddUint32(&b.v, 1) - 1)
}
func truthy(n uint32) bool {
return n&1 == 1
}
func boolToInt(b bool) uint32 {
if b {
return 1
}
return 0
}
// Float64 is an atomic wrapper around float64.
type Float64 struct {
v uint64
}
// NewFloat64 creates a Float64.
func NewFloat64(f float64) *Float64 {
return &Float64{math.Float64bits(f)}
}
// Load atomically loads the wrapped value.
func (f *Float64) Load() float64 {
return math.Float64frombits(atomic.LoadUint64(&f.v))
}
// Store atomically stores the passed value.
func (f *Float64) Store(s float64) {
atomic.StoreUint64(&f.v, math.Float64bits(s))
}
// Add atomically adds to the wrapped float64 and returns the new value.
func (f *Float64) Add(s float64) float64 {
for {
old := f.Load()
new := old + s
if f.CAS(old, new) {
return new
}
}
}
// Sub atomically subtracts from the wrapped float64 and returns the new value.
func (f *Float64) Sub(s float64) float64 {
return f.Add(-s)
}
// CAS is an atomic compare-and-swap.
func (f *Float64) CAS(old, new float64) bool {
return atomic.CompareAndSwapUint64(&f.v, math.Float64bits(old), math.Float64bits(new))
}
// Duration is an atomic wrapper around time.Duration
// https://godoc.org/time#Duration
type Duration struct {
v Int64
}
// NewDuration creates a Duration.
func NewDuration(d time.Duration) *Duration {
return &Duration{v: *NewInt64(int64(d))}
}
// Load atomically loads the wrapped value.
func (d *Duration) Load() time.Duration {
return time.Duration(d.v.Load())
}
// Store atomically stores the passed value.
func (d *Duration) Store(n time.Duration) {
d.v.Store(int64(n))
}
// Add atomically adds to the wrapped time.Duration and returns the new value.
func (d *Duration) Add(n time.Duration) time.Duration {
return time.Duration(d.v.Add(int64(n)))
}
// Sub atomically subtracts from the wrapped time.Duration and returns the new value.
func (d *Duration) Sub(n time.Duration) time.Duration {
return time.Duration(d.v.Sub(int64(n)))
}
// Swap atomically swaps the wrapped time.Duration and returns the old value.
func (d *Duration) Swap(n time.Duration) time.Duration {
return time.Duration(d.v.Swap(int64(n)))
}
// CAS is an atomic compare-and-swap.
func (d *Duration) CAS(old, new time.Duration) bool {
return d.v.CAS(int64(old), int64(new))
}
// Value shadows the type of the same name from sync/atomic
// https://godoc.org/sync/atomic#Value
type Value struct{ atomic.Value }

17
vendor/go.uber.org/atomic/glide.lock generated vendored Normal file
View File

@ -0,0 +1,17 @@
hash: f14d51408e3e0e4f73b34e4039484c78059cd7fc5f4996fdd73db20dc8d24f53
updated: 2016-10-27T00:10:51.16960137-07:00
imports: []
testImports:
- name: github.com/davecgh/go-spew
version: 5215b55f46b2b919f50a1df0eaa5886afe4e3b3d
subpackages:
- spew
- name: github.com/pmezard/go-difflib
version: d8ed2627bdf02c080bf22230dbb337003b7aba2d
subpackages:
- difflib
- name: github.com/stretchr/testify
version: d77da356e56a7428ad25149ca77381849a6a5232
subpackages:
- assert
- require

6
vendor/go.uber.org/atomic/glide.yaml generated vendored Normal file
View File

@ -0,0 +1,6 @@
package: go.uber.org/atomic
testImport:
- package: github.com/stretchr/testify
subpackages:
- assert
- require

49
vendor/go.uber.org/atomic/string.go generated vendored Normal file
View File

@ -0,0 +1,49 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package atomic
// String is an atomic type-safe wrapper around Value for strings.
type String struct{ v Value }
// NewString creates a String.
func NewString(str string) *String {
s := &String{}
if str != "" {
s.Store(str)
}
return s
}
// Load atomically loads the wrapped string.
func (s *String) Load() string {
v := s.v.Load()
if v == nil {
return ""
}
return v.(string)
}
// Store atomically stores the passed string.
// Note: Converting the string to an interface{} to store in the Value
// requires an allocation.
func (s *String) Store(str string) {
s.v.Store(str)
}

15
vendor/go.uber.org/multierr/.codecov.yml generated vendored Normal file
View File

@ -0,0 +1,15 @@
coverage:
range: 80..100
round: down
precision: 2
status:
project: # measuring the overall project coverage
default: # context, you can create multiple ones with custom titles
enabled: yes # must be yes|true to enable this status
target: 100 # specify the target coverage for each commit status
# option: "auto" (must increase from parent commit or pull request base)
# option: "X%" a static target percentage to hit
if_not_found: success # if parent is not found report status as success, error, or failure
if_ci_failed: error # if ci fails report status as success, error, or failure

1
vendor/go.uber.org/multierr/.gitignore generated vendored Normal file
View File

@ -0,0 +1 @@
/vendor

33
vendor/go.uber.org/multierr/.travis.yml generated vendored Normal file
View File

@ -0,0 +1,33 @@
sudo: false
language: go
go_import_path: go.uber.org/multierr
env:
global:
- GO15VENDOREXPERIMENT=1
go:
- 1.7
- 1.8
- tip
cache:
directories:
- vendor
before_install:
- go version
install:
- |
set -e
make install_ci
script:
- |
set -e
make lint
make test_ci
after_success:
- bash <(curl -s https://codecov.io/bash)

28
vendor/go.uber.org/multierr/CHANGELOG.md generated vendored Normal file
View File

@ -0,0 +1,28 @@
Releases
========
v1.1.0 (2017-06-30)
===================
- Added an `Errors(error) []error` function to extract the underlying list of
errors for a multierr error.
v1.0.0 (2017-05-31)
===================
No changes since v0.2.0. This release is committing to making no breaking
changes to the current API in the 1.X series.
v0.2.0 (2017-04-11)
===================
- Repeatedly appending to the same error is now faster due to fewer
allocations.
v0.1.0 (2017-31-03)
===================
- Initial release

19
vendor/go.uber.org/multierr/LICENSE.txt generated vendored Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2017 Uber Technologies, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

74
vendor/go.uber.org/multierr/Makefile generated vendored Normal file
View File

@ -0,0 +1,74 @@
export GO15VENDOREXPERIMENT=1
PACKAGES := $(shell glide nv)
GO_FILES := $(shell \
find . '(' -path '*/.*' -o -path './vendor' ')' -prune \
-o -name '*.go' -print | cut -b3-)
.PHONY: install
install:
glide --version || go get github.com/Masterminds/glide
glide install
.PHONY: build
build:
go build -i $(PACKAGES)
.PHONY: test
test:
go test -cover -race $(PACKAGES)
.PHONY: gofmt
gofmt:
$(eval FMT_LOG := $(shell mktemp -t gofmt.XXXXX))
@gofmt -e -s -l $(GO_FILES) > $(FMT_LOG) || true
@[ ! -s "$(FMT_LOG)" ] || (echo "gofmt failed:" | cat - $(FMT_LOG) && false)
.PHONY: govet
govet:
$(eval VET_LOG := $(shell mktemp -t govet.XXXXX))
@go vet $(PACKAGES) 2>&1 \
| grep -v '^exit status' > $(VET_LOG) || true
@[ ! -s "$(VET_LOG)" ] || (echo "govet failed:" | cat - $(VET_LOG) && false)
.PHONY: golint
golint:
@go get github.com/golang/lint/golint
$(eval LINT_LOG := $(shell mktemp -t golint.XXXXX))
@cat /dev/null > $(LINT_LOG)
@$(foreach pkg, $(PACKAGES), golint $(pkg) >> $(LINT_LOG) || true;)
@[ ! -s "$(LINT_LOG)" ] || (echo "golint failed:" | cat - $(LINT_LOG) && false)
.PHONY: staticcheck
staticcheck:
@go get honnef.co/go/tools/cmd/staticcheck
$(eval STATICCHECK_LOG := $(shell mktemp -t staticcheck.XXXXX))
@staticcheck $(PACKAGES) 2>&1 > $(STATICCHECK_LOG) || true
@[ ! -s "$(STATICCHECK_LOG)" ] || (echo "staticcheck failed:" | cat - $(STATICCHECK_LOG) && false)
.PHONY: lint
lint: gofmt govet golint staticcheck
.PHONY: cover
cover:
./scripts/cover.sh $(shell go list $(PACKAGES))
go tool cover -html=cover.out -o cover.html
update-license:
@go get go.uber.org/tools/update-license
@update-license \
$(shell go list -json $(PACKAGES) | \
jq -r '.Dir + "/" + (.GoFiles | .[])')
##############################################################################
.PHONY: install_ci
install_ci: install
go get github.com/wadey/gocovmerge
go get github.com/mattn/goveralls
go get golang.org/x/tools/cmd/cover
.PHONY: test_ci
test_ci: install_ci
./scripts/cover.sh $(shell go list $(PACKAGES))

Some files were not shown because too many files have changed in this diff Show More