unipdf/pdfutil/pdfutil.go
2020-10-12 14:17:59 +00:00

27 lines
3.0 KiB
Go

//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
package pdfutil ;import (_g "github.com/unidoc/unipdf/v3/common";_gc "github.com/unidoc/unipdf/v3/contentstream";_ea "github.com/unidoc/unipdf/v3/contentstream/draw";_f "github.com/unidoc/unipdf/v3/core";_e "github.com/unidoc/unipdf/v3/model";);
// NormalizePage performs the following operations on the passed in page:
// - Normalize the page rotation.
// Rotates the contents of the page according to the Rotate entry, thus
// flattening the rotation. The Rotate entry of the page is set to nil.
// - Normalize the media box.
// If the media box of the page is offsetted (Llx != 0 or Lly != 0),
// the contents of the page are translated to (-Llx, -Lly). After
// normalization, the media box is updated (Llx and Lly are set to 0 and
// Urx and Ury are updated accordingly).
// - Normalize the crop box.
// The crop box of the page is updated based on the previous operations.
// After normalization, the page should look the same if openend using a
// PDF viewer.
func NormalizePage (page *_e .PdfPage )error {_b ,_bg :=page .GetMediaBox ();if _bg !=nil {return _bg ;};_c :=page .Rotate ;_bd :=_c !=nil &&*_c %360!=0&&*_c %90==0;_b .Normalize ();_fe ,_eaa ,_cf ,_ba :=_b .Llx ,_b .Lly ,_b .Width (),_b .Height ();_gf :=_fe !=0||_eaa !=0;if !_bd &&!_gf {return nil ;};_bdd :=func (_bge ,_cd ,_fa float64 )_ea .BoundingBox {return _ea .Path {Points :[]_ea .Point {_ea .NewPoint (0,0).Rotate (_fa ),_ea .NewPoint (_bge ,0).Rotate (_fa ),_ea .NewPoint (0,_cd ).Rotate (_fa ),_ea .NewPoint (_bge ,_cd ).Rotate (_fa )}}.GetBoundingBox ();};_gd :=_gc .NewContentCreator ();var _db float64 ;if _bd {_db =-float64 (*page .Rotate );_a :=_bdd (_cf ,_ba ,_db );_gd .Translate ((_a .Width -_cf )/2+_cf /2,(_a .Height -_ba )/2+_ba /2);_gd .RotateDeg (_db );_gd .Translate (-_cf /2,-_ba /2);_cf ,_ba =_a .Width ,_a .Height ;};if _gf {_gd .Translate (-_fe ,-_eaa );};_ed :=_gd .Operations ();_bc ,_bg :=_f .MakeStream (_ed .Bytes (),_f .NewFlateEncoder ());if _bg !=nil {return _bg ;};_bag :=_f .MakeArray (_bc );_bag .Append (page .GetContentStreamObjs ()...);*_b =_e .PdfRectangle {Urx :_cf ,Ury :_ba };if _gb :=page .CropBox ;_gb !=nil {_gb .Normalize ();_eb ,_bad ,_aa ,_cg :=_gb .Llx -_fe ,_gb .Lly -_eaa ,_gb .Width (),_gb .Height ();if _bd {_bgb :=_bdd (_aa ,_cg ,_db );_aa ,_cg =_bgb .Width ,_bgb .Height ;};*_gb =_e .PdfRectangle {Llx :_eb ,Lly :_bad ,Urx :_eb +_aa ,Ury :_bad +_cg };};_g .Log .Debug ("\u0052\u006f\u0074\u0061\u0074\u0065\u003d\u0025\u0066\u00b0\u0020\u004f\u0070\u0073\u003d%\u0071 \u004d\u0065\u0064\u0069\u0061\u0042\u006f\u0078\u003d\u0025\u002e\u0032\u0066",_db ,_ed ,_b );page .Contents =_bag ;page .Rotate =nil ;return nil ;};