mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-02 22:17:06 +08:00
Simplify drawable interface, remove unnecessary methods.
This commit is contained in:
parent
825c10fc3e
commit
a422aecfec
@ -8,7 +8,6 @@ package creator
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/unidoc/unidoc/common"
|
"github.com/unidoc/unidoc/common"
|
||||||
"github.com/unidoc/unidoc/pdf/model/fonts"
|
"github.com/unidoc/unidoc/pdf/model/fonts"
|
||||||
@ -88,24 +87,6 @@ func (chap *Chapter) GetHeading() *paragraph {
|
|||||||
return chap.heading
|
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.
|
// Set absolute coordinates.
|
||||||
func (chap *Chapter) SetPos(x, y float64) {
|
func (chap *Chapter) SetPos(x, y float64) {
|
||||||
chap.positioning = positionAbsolute
|
chap.positioning = positionAbsolute
|
||||||
|
@ -7,53 +7,25 @@ package creator
|
|||||||
|
|
||||||
// All widgets that can be used to draw with the creator need to implement the Drawable interface.
|
// All widgets that can be used to draw with the creator need to implement the Drawable interface.
|
||||||
type 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
|
// 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
|
// 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.
|
// where to draw (if relative positioning) and the available height to draw on accounting for Margins etc.
|
||||||
GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error)
|
GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Some drawables can be scaled. Mostly objects that fit on a single Page. E.g. image, block.
|
// Drawing context. Continuously used when drawing the page contents. Keeps track of current X, Y position,
|
||||||
type Scalable interface {
|
// available height as well as other page parameters such as margins and dimensions.
|
||||||
// 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.
|
|
||||||
type DrawContext struct {
|
type DrawContext struct {
|
||||||
|
// Current page number.
|
||||||
Page int
|
Page int
|
||||||
|
|
||||||
// Current position. In a relative positioning mode, a drawable will be placed at these coordinates.
|
// Current position. In a relative positioning mode, a drawable will be placed at these coordinates.
|
||||||
X, Y float64
|
X, Y float64
|
||||||
|
|
||||||
// Context dimensions. Available width and height.
|
// Context dimensions. Available width and height.
|
||||||
Width, Height float64
|
Width, Height float64
|
||||||
|
|
||||||
// Page Margins...
|
// Page Margins.
|
||||||
Margins margins
|
Margins margins
|
||||||
|
|
||||||
// Absolute Page size, widths and height.
|
// Absolute Page size, widths and height.
|
||||||
|
@ -8,8 +8,6 @@ package creator
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/unidoc/unidoc/common"
|
"github.com/unidoc/unidoc/common"
|
||||||
"github.com/unidoc/unidoc/pdf/model/fonts"
|
"github.com/unidoc/unidoc/pdf/model/fonts"
|
||||||
)
|
)
|
||||||
@ -64,24 +62,6 @@ func (c *Creator) NewSubchapter(ch *Chapter, title string) *subchapter {
|
|||||||
return subchap
|
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.
|
// Set absolute coordinates.
|
||||||
func (subchap *subchapter) SetPos(x, y float64) {
|
func (subchap *subchapter) SetPos(x, y float64) {
|
||||||
subchap.positioning = positionAbsolute
|
subchap.positioning = positionAbsolute
|
||||||
|
Loading…
x
Reference in New Issue
Block a user