Simplify drawable interface, remove unnecessary methods.

This commit is contained in:
Gunnsteinn Hall 2017-07-11 13:16:01 +00:00
parent 825c10fc3e
commit a422aecfec
3 changed files with 5 additions and 72 deletions

View File

@ -8,7 +8,6 @@ package creator
import (
"errors"
"fmt"
"math"
"github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/model/fonts"
@ -88,24 +87,6 @@ func (chap *Chapter) GetHeading() *paragraph {
return chap.heading
}
// Chapter height is a sum of the content heights.
func (chap *Chapter) Height() float64 {
h := float64(0)
for _, d := range chap.contents {
h += d.Height()
}
return h
}
// Chapter width is the maximum of the content widths.
func (chap *Chapter) Width() float64 {
maxW := float64(0)
for _, d := range chap.contents {
maxW = math.Max(maxW, d.Width())
}
return maxW
}
// Set absolute coordinates.
func (chap *Chapter) SetPos(x, y float64) {
chap.positioning = positionAbsolute

View File

@ -7,53 +7,25 @@ package creator
// All widgets that can be used to draw with the creator need to implement the Drawable interface.
type Drawable interface {
// Set absolute position of the widget on the Page/template to be drawn onto.
SetPos(x, y float64)
// Set the left, right, top, bottom Margins.
SetMargins(float64, float64, float64, float64)
// Get the left, right, top, bottom Margins.
GetMargins() (float64, float64, float64, float64)
// Returns the width/height of the drawable.
Width() float64
Height() float64
// Draw onto blocks representing Page contents. As the content can wrap over many pages, multiple
// templates are returned, one per Page. The function also takes a draw context containing information
// where to draw (if relative positioning) and the available height to draw on accounting for Margins etc.
GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error)
}
// Some drawables can be scaled. Mostly objects that fit on a single Page. E.g. image, block.
type Scalable interface {
// Scale the drawable. Does not actually influence the object contents but rather how it is represented
// when drawn to the screen. I.e. a coordinate transform.
// Does change the Width and Height properties.
Scale(float64, float64)
ScaleToHeight(float64)
ScaleToWidth(float64)
}
// Some drawables can be rotated. Mostly vector graphics that fit on a single Page. E.g. image, block.
type Rotatable interface {
// Set the rotation angle of the drawable in degrees.
// The rotation does not change the dimensions of the Drawable and is only applied at the time of drawing.
SetAngle(angleDeg float64)
}
// Drawing context. Continuously used when drawing the Page contents. Keeps track of current X, Y position,
// available height as well as other Page parameters such as Margins and dimensions.
// Drawing context. Continuously used when drawing the page contents. Keeps track of current X, Y position,
// available height as well as other page parameters such as margins and dimensions.
type DrawContext struct {
// Current page number.
Page int
// Current position. In a relative positioning mode, a drawable will be placed at these coordinates.
X, Y float64
// Context dimensions. Available width and height.
Width, Height float64
// Page Margins...
// Page Margins.
Margins margins
// Absolute Page size, widths and height.

View File

@ -8,8 +8,6 @@ package creator
import (
"fmt"
"math"
"github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/model/fonts"
)
@ -64,24 +62,6 @@ func (c *Creator) NewSubchapter(ch *Chapter, title string) *subchapter {
return subchap
}
// Chapter height is a sum of the content heights.
func (subchap *subchapter) Height() float64 {
h := float64(0)
for _, d := range subchap.contents {
h += d.Height()
}
return h
}
// Chapter width is the maximum of the content widths.
func (subchap *subchapter) Width() float64 {
maxW := float64(0)
for _, d := range subchap.contents {
maxW = math.Max(maxW, d.Width())
}
return maxW
}
// Set absolute coordinates.
func (subchap *subchapter) SetPos(x, y float64) {
subchap.positioning = positionAbsolute