@@ -93,9 +93,19 @@ func NewBlockFromPage(page *model.PdfPage) (*Block, error) {
9393 b .width = mbox .Urx - mbox .Llx
9494 b .height = mbox .Ury - mbox .Lly
9595
96+ // Inherit page rotation angle.
97+ if page .Rotate != nil {
98+ b .angle = - float64 (* page .Rotate )
99+ }
100+
96101 return b , nil
97102}
98103
104+ // Angle returns the block rotation angle in degrees.
105+ func (blk * Block ) Angle () float64 {
106+ return blk .angle
107+ }
108+
99109// SetAngle sets the rotation angle in degrees.
100110func (blk * Block ) SetAngle (angleDeg float64 ) {
101111 blk .angle = angleDeg
@@ -132,47 +142,39 @@ func (blk *Block) duplicate() *Block {
132142// GeneratePageBlocks draws the block contents on a template Page block.
133143// Implements the Drawable interface.
134144func (blk * Block ) GeneratePageBlocks (ctx DrawContext ) ([]* Block , DrawContext , error ) {
135- var blocks [] * Block
145+ cc := contentstream . NewContentCreator ()
136146
147+ // Position block.
148+ blkWidth , blkHeight := blk .Width (), blk .Height ()
137149 if blk .positioning .isRelative () {
138- // Draw at current ctx.X, ctx.Y position
139- dup := blk .duplicate ()
140- cc := contentstream .NewContentCreator ()
141- cc .Translate (ctx .X , ctx .PageHeight - ctx .Y - blk .height )
142- if blk .angle != 0 {
143- // Make the rotation about the upper left corner.
144- // TODO: Account for rotation origin. (Consider).
145- cc .Translate (0 , blk .Height ())
146- cc .RotateDeg (blk .angle )
147- cc .Translate (0 , - blk .Height ())
148- }
149- contents := append (* cc .Operations (), * dup .contents ... )
150- contents .WrapIfNeeded ()
151- dup .contents = & contents
150+ // Relative. Draw at current ctx.X, ctx.Y position.
151+ cc .Translate (ctx .X , ctx .PageHeight - ctx .Y - blkHeight )
152+ } else {
153+ // Absolute. Draw at blk.xPos, blk.yPos position.
154+ cc .Translate (blk .xPos , ctx .PageHeight - blk .yPos - blkHeight )
155+ }
152156
153- blocks = append (blocks , dup )
157+ // Rotate block.
158+ rotatedHeight := blkHeight
159+ if blk .angle != 0 {
160+ // Make the rotation about the center of the block.
161+ cc .Translate (blkWidth / 2 , blkHeight / 2 )
162+ cc .RotateDeg (blk .angle )
163+ cc .Translate (- blkWidth / 2 , - blkHeight / 2 )
154164
155- ctx .Y += blk .height
156- } else {
157- // Absolute. Draw at blk.xPos, blk.yPos position
158- dup := blk .duplicate ()
159- cc := contentstream .NewContentCreator ()
160- cc .Translate (blk .xPos , ctx .PageHeight - blk .yPos - blk .height )
161- if blk .angle != 0 {
162- // Make the rotation about the upper left corner.
163- // TODO: Consider supporting specification of rotation origin.
164- cc .Translate (0 , blk .Height ())
165- cc .RotateDeg (blk .angle )
166- cc .Translate (0 , - blk .Height ())
167- }
168- contents := append (* cc .Operations (), * dup .contents ... )
169- contents .WrapIfNeeded ()
170- dup .contents = & contents
165+ _ , rotatedHeight = blk .RotatedSize ()
166+ }
171167
172- blocks = append (blocks , dup )
168+ if blk .positioning .isRelative () {
169+ ctx .Y += rotatedHeight
173170 }
174171
175- return blocks , ctx , nil
172+ dup := blk .duplicate ()
173+ contents := append (* cc .Operations (), * dup .contents ... )
174+ contents .WrapIfNeeded ()
175+ dup .contents = & contents
176+
177+ return []* Block {dup }, ctx , nil
176178}
177179
178180// Height returns the Block's height.
@@ -185,6 +187,12 @@ func (blk *Block) Width() float64 {
185187 return blk .width
186188}
187189
190+ // RotatedSize returns the width and height of the rotated block.
191+ func (blk * Block ) RotatedSize () (float64 , float64 ) {
192+ _ , _ , w , h := rotateRect (blk .width , blk .height , blk .angle )
193+ return w , h
194+ }
195+
188196// addContents adds contents to a block. Wrap both existing and new contents to ensure
189197// independence of content operations.
190198func (blk * Block ) addContents (operations * contentstream.ContentStreamOperations ) {
@@ -335,8 +343,7 @@ func (blk *Block) Draw(d Drawable) error {
335343 }
336344
337345 for _ , newBlock := range blocks {
338- err := mergeContents (blk .contents , blk .resources , newBlock .contents , newBlock .resources )
339- if err != nil {
346+ if err := blk .mergeBlocks (newBlock ); err != nil {
340347 return err
341348 }
342349 }
@@ -356,8 +363,7 @@ func (blk *Block) DrawWithContext(d Drawable, ctx DrawContext) error {
356363 }
357364
358365 for _ , newBlock := range blocks {
359- err := mergeContents (blk .contents , blk .resources , newBlock .contents , newBlock .resources )
360- if err != nil {
366+ if err := blk .mergeBlocks (newBlock ); err != nil {
361367 return err
362368 }
363369 }
0 commit comments