github.com/go-graphite/carbonapi@v0.17.0/expr/functions/cairo/png/pixel_ratio.go (about)

     1  // +build cairo
     2  
     3  package png
     4  
     5  import "github.com/evmar/gocairo/cairo"
     6  
     7  // interface with all used cairo.Context methods
     8  type cairoContext interface {
     9  	Rectangle(x, y, width, height float64) // pixel ratio required
    10  	GetLineWidth() float64                 // pixel ratio required
    11  	LineTo(x, y float64)                   // pixel ratio required
    12  	MoveTo(x, y float64)                   // pixel ratio required
    13  	SetLineWidth(width float64)            // pixel ratio required
    14  	SetFontSize(size float64)              // pixel ratio required
    15  	SetFontOptions(options *cairo.FontOptions)
    16  	Stroke()
    17  	SetDash(dashes []float64, offset float64)            // pixel ratio required
    18  	TextExtents(utf8 string, extents *cairo.TextExtents) // pixel ratio required
    19  	FontExtents(extents *cairo.FontExtents)              // pixel ratio required
    20  	Rotate(angle float64)
    21  	SetLineCap(lineCap cairo.LineCap)
    22  	SetLineJoin(lineJoin cairo.LineJoin)
    23  	RelMoveTo(dx, dy float64) // pixel ratio required
    24  	SetSourceRGBA(red, green, blue, alpha float64)
    25  	SetMatrix(matrix *cairo.Matrix) // pixel ratio required
    26  	GetMatrix(matrix *cairo.Matrix) // pixel ratio required
    27  	Clip()
    28  	Fill()
    29  	ClosePath()
    30  	SelectFontFace(family string, slant cairo.FontSlant, weight cairo.FontWeight) // pixel ratio required
    31  	TextPath(utf8 string)
    32  	Save()
    33  	Restore()
    34  	FillPreserve()
    35  	AppendPath(path *cairo.Path)
    36  	CopyPath() *cairo.Path
    37  }
    38  
    39  type pixelRatioContext struct {
    40  	*cairo.Context
    41  	pr float64 // pixel ratio
    42  }
    43  
    44  type cairoSurfaceContext struct {
    45  	context cairoContext
    46  }
    47  
    48  func isDefaultRatio(pixelRatio float64) bool {
    49  	if pixelRatio > 0.9999 && pixelRatio < 1.0001 {
    50  		return true
    51  	}
    52  	return false
    53  }
    54  
    55  func svgSurfaceCreate(filename string, widthInPoints, heightInPoints float64, pixelRatio float64) *cairo.SVGSurface {
    56  	if isDefaultRatio(pixelRatio) {
    57  		return cairo.SVGSurfaceCreate(filename, widthInPoints, heightInPoints)
    58  	}
    59  	return cairo.SVGSurfaceCreate(filename, pixelRatio*widthInPoints, pixelRatio*heightInPoints)
    60  }
    61  
    62  func imageSurfaceCreate(format cairo.Format, width, height float64, pixelRatio float64) *cairo.ImageSurface {
    63  	if isDefaultRatio(pixelRatio) {
    64  		return cairo.ImageSurfaceCreate(format, int(width), int(height))
    65  	}
    66  	return cairo.ImageSurfaceCreate(format, int(pixelRatio*float64(width)), int(pixelRatio*float64(height)))
    67  }
    68  
    69  func createContext(surface *cairo.Surface, pixelRatio float64) *cairoSurfaceContext {
    70  	if isDefaultRatio(pixelRatio) {
    71  		return &cairoSurfaceContext{context: cairo.Create(surface)}
    72  	}
    73  
    74  	return &cairoSurfaceContext{
    75  		context: &pixelRatioContext{
    76  			Context: cairo.Create(surface),
    77  			pr:      pixelRatio,
    78  		},
    79  	}
    80  }
    81  
    82  func (c *pixelRatioContext) Rectangle(x, y, width, height float64) {
    83  	c.Context.Rectangle(c.pr*x, c.pr*y, c.pr*width, c.pr*height)
    84  }
    85  
    86  func (c *pixelRatioContext) GetLineWidth() float64 {
    87  	return c.Context.GetLineWidth() / c.pr
    88  }
    89  
    90  func (c *pixelRatioContext) LineTo(x, y float64) {
    91  	c.Context.LineTo(c.pr*x, c.pr*y)
    92  }
    93  
    94  func (c *pixelRatioContext) MoveTo(x, y float64) {
    95  	c.Context.MoveTo(c.pr*x, c.pr*y)
    96  }
    97  
    98  func (c *pixelRatioContext) SetLineWidth(width float64) {
    99  	c.Context.SetLineWidth(c.pr * width)
   100  }
   101  
   102  func (c *pixelRatioContext) SetFontSize(size float64) {
   103  	c.Context.SetFontSize(c.pr * size)
   104  }
   105  
   106  func (c *pixelRatioContext) SetDash(dashes []float64, offset float64) {
   107  	dr := make([]float64, len(dashes))
   108  	for i := 0; i < len(dashes); i++ {
   109  		dr[i] = dashes[i] * c.pr
   110  	}
   111  	c.Context.SetDash(dr, offset*c.pr)
   112  }
   113  
   114  func (c *pixelRatioContext) TextExtents(utf8 string, extents *cairo.TextExtents) {
   115  	var e cairo.TextExtents
   116  	c.Context.TextExtents(utf8, &e)
   117  	extents.XBearing = e.XBearing / c.pr
   118  	extents.YBearing = e.YBearing / c.pr
   119  	extents.Width = e.Width / c.pr
   120  	extents.Height = e.Height / c.pr
   121  	extents.XAdvance = e.XAdvance / c.pr
   122  	extents.YAdvance = e.YAdvance / c.pr
   123  }
   124  
   125  func (c *pixelRatioContext) FontExtents(extents *cairo.FontExtents) {
   126  	var e cairo.FontExtents
   127  	c.Context.FontExtents(&e)
   128  	extents.Ascent = e.Ascent / c.pr
   129  	extents.Descent = e.Descent / c.pr
   130  	extents.Height = e.Height / c.pr
   131  	extents.MaxXAdvance = e.MaxXAdvance / c.pr
   132  	extents.MaxYAdvance = e.MaxYAdvance / c.pr
   133  }
   134  
   135  func (c *pixelRatioContext) RelMoveTo(dx, dy float64) {
   136  	c.Context.RelMoveTo(c.pr*dx, c.pr*dy)
   137  }
   138  
   139  func (c *pixelRatioContext) SetMatrix(matrix *cairo.Matrix) {
   140  	var m cairo.Matrix
   141  	m.Xx = matrix.Xx * c.pr
   142  	m.Yx = matrix.Yx * c.pr
   143  	m.Xy = matrix.Xy * c.pr
   144  	m.Yy = matrix.Yy * c.pr
   145  	m.X0 = matrix.X0 * c.pr
   146  	m.Y0 = matrix.Y0 * c.pr
   147  	c.Context.SetMatrix(&m)
   148  }
   149  
   150  func (c *pixelRatioContext) GetMatrix(matrix *cairo.Matrix) {
   151  	var m cairo.Matrix
   152  	c.Context.GetMatrix(&m)
   153  	matrix.Xx = m.Xx / c.pr
   154  	matrix.Yx = m.Yx / c.pr
   155  	matrix.Xy = m.Xy / c.pr
   156  	matrix.Yy = m.Yy / c.pr
   157  	matrix.X0 = m.X0 / c.pr
   158  	matrix.Y0 = m.Y0 / c.pr
   159  }
   160  
   161  func (c *pixelRatioContext) SelectFontFace(family string, slant cairo.FontSlant, weight cairo.FontWeight) {
   162  	c.Context.SelectFontFace(family, slant, cairo.FontWeight(c.pr*float64(weight)))
   163  }