github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/creator/ellipse.go (about)

     1  /*
     2   * This file is subject to the terms and conditions defined in
     3   * file 'LICENSE.md', which is part of this source code package.
     4   */
     5  
     6  package creator
     7  
     8  import (
     9  	"github.com/unidoc/unidoc/pdf/contentstream/draw"
    10  	"github.com/unidoc/unidoc/pdf/model"
    11  )
    12  
    13  // Ellipse defines an ellipse with a center at (xc,yc) and a specified width and height.  The ellipse can have a colored
    14  // fill and/or border with a specified width.
    15  // Implements the Drawable interface and can be drawn on PDF using the Creator.
    16  type Ellipse struct {
    17  	xc          float64
    18  	yc          float64
    19  	width       float64
    20  	height      float64
    21  	fillColor   *model.PdfColorDeviceRGB
    22  	borderColor *model.PdfColorDeviceRGB
    23  	borderWidth float64
    24  }
    25  
    26  // NewEllipse creates a new ellipse centered at (xc,yc) with a width and height specified.
    27  func NewEllipse(xc, yc, width, height float64) *Ellipse {
    28  	ell := &Ellipse{}
    29  
    30  	ell.xc = xc
    31  	ell.yc = yc
    32  	ell.width = width
    33  	ell.height = height
    34  
    35  	ell.borderColor = model.NewPdfColorDeviceRGB(0, 0, 0)
    36  	ell.borderWidth = 1.0
    37  
    38  	return ell
    39  }
    40  
    41  // GetCoords returns the coordinates of the Ellipse's center (xc,yc).
    42  func (ell *Ellipse) GetCoords() (float64, float64) {
    43  	return ell.xc, ell.yc
    44  }
    45  
    46  // SetBorderWidth sets the border width.
    47  func (ell *Ellipse) SetBorderWidth(bw float64) {
    48  	ell.borderWidth = bw
    49  }
    50  
    51  // SetBorderColor sets the border color.
    52  func (ell *Ellipse) SetBorderColor(col Color) {
    53  	ell.borderColor = model.NewPdfColorDeviceRGB(col.ToRGB())
    54  }
    55  
    56  // SetFillColor sets the fill color.
    57  func (ell *Ellipse) SetFillColor(col Color) {
    58  	ell.fillColor = model.NewPdfColorDeviceRGB(col.ToRGB())
    59  }
    60  
    61  // GeneratePageBlocks draws the rectangle on a new block representing the page.
    62  func (ell *Ellipse) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
    63  	block := NewBlock(ctx.PageWidth, ctx.PageHeight)
    64  
    65  	drawell := draw.Circle{
    66  		X:           ell.xc - ell.width/2,
    67  		Y:           ctx.PageHeight - ell.yc - ell.height/2,
    68  		Width:       ell.width,
    69  		Height:      ell.height,
    70  		Opacity:     1.0,
    71  		BorderWidth: ell.borderWidth,
    72  	}
    73  	if ell.fillColor != nil {
    74  		drawell.FillEnabled = true
    75  		drawell.FillColor = ell.fillColor
    76  	}
    77  	if ell.borderColor != nil {
    78  		drawell.BorderEnabled = true
    79  		drawell.BorderColor = ell.borderColor
    80  		drawell.BorderWidth = ell.borderWidth
    81  	}
    82  
    83  	contents, _, err := drawell.Draw("")
    84  	if err != nil {
    85  		return nil, ctx, err
    86  	}
    87  
    88  	err = block.addContentsByString(string(contents))
    89  	if err != nil {
    90  		return nil, ctx, err
    91  	}
    92  
    93  	return []*Block{block}, ctx, nil
    94  }