github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/creator/rectangle.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  // Rectangle defines a rectangle with upper left corner at (x,y) and a specified width and height.  The rectangle
    14  // can have a colored fill and/or border with a specified width.
    15  // Implements the Drawable interface and can be drawn on PDF using the Creator.
    16  type Rectangle struct {
    17  	x           float64 // Upper left corner
    18  	y           float64
    19  	width       float64
    20  	height      float64
    21  	fillColor   *model.PdfColorDeviceRGB
    22  	borderColor *model.PdfColorDeviceRGB
    23  	borderWidth float64
    24  }
    25  
    26  // NewRectangle creates a new Rectangle with default parameters with left corner at (x,y) and width, height as specified.
    27  func NewRectangle(x, y, width, height float64) *Rectangle {
    28  	rect := &Rectangle{}
    29  
    30  	rect.x = x
    31  	rect.y = y
    32  	rect.width = width
    33  	rect.height = height
    34  
    35  	rect.borderColor = model.NewPdfColorDeviceRGB(0, 0, 0)
    36  	rect.borderWidth = 1.0
    37  
    38  	return rect
    39  }
    40  
    41  // GetCoords returns coordinates of the Rectangle's upper left corner (x,y).
    42  func (rect *Rectangle) GetCoords() (float64, float64) {
    43  	return rect.x, rect.y
    44  }
    45  
    46  // SetBorderWidth sets the border width.
    47  func (rect *Rectangle) SetBorderWidth(bw float64) {
    48  	rect.borderWidth = bw
    49  }
    50  
    51  // SetBorderColor sets border color.
    52  func (rect *Rectangle) SetBorderColor(col Color) {
    53  	rect.borderColor = model.NewPdfColorDeviceRGB(col.ToRGB())
    54  }
    55  
    56  // SetFillColor sets the fill color.
    57  func (rect *Rectangle) SetFillColor(col Color) {
    58  	rect.fillColor = model.NewPdfColorDeviceRGB(col.ToRGB())
    59  }
    60  
    61  // GeneratePageBlocks draws the rectangle on a new block representing the page. Implements the Drawable interface.
    62  func (rect *Rectangle) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error) {
    63  	block := NewBlock(ctx.PageWidth, ctx.PageHeight)
    64  
    65  	drawrect := draw.Rectangle{
    66  		Opacity: 1.0,
    67  		X:       rect.x,
    68  		Y:       ctx.PageHeight - rect.y - rect.height,
    69  		Height:  rect.height,
    70  		Width:   rect.width,
    71  	}
    72  	if rect.fillColor != nil {
    73  		drawrect.FillEnabled = true
    74  		drawrect.FillColor = rect.fillColor
    75  	}
    76  	if rect.borderColor != nil && rect.borderWidth > 0 {
    77  		drawrect.BorderEnabled = true
    78  		drawrect.BorderColor = rect.borderColor
    79  		drawrect.BorderWidth = rect.borderWidth
    80  	}
    81  
    82  	contents, _, err := drawrect.Draw("")
    83  	if err != nil {
    84  		return nil, ctx, err
    85  	}
    86  
    87  	err = block.addContentsByString(string(contents))
    88  	if err != nil {
    89  		return nil, ctx, err
    90  	}
    91  
    92  	return []*Block{block}, ctx, nil
    93  }