unipdf/pdfutil/pdfutil.go
2020-10-05 19:28:24 +00:00

27 lines
2.9 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 (_d "github.com/unidoc/unipdf/v3/common";_gg "github.com/unidoc/unipdf/v3/contentstream";_b "github.com/unidoc/unipdf/v3/contentstream/draw";_g "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 *_g .PdfPage )error {_f ,_fb :=page .GetMediaBox ();if _fb !=nil {return _fb ;};_be ,_fb :=page .GetContentStreams ();if _fb !=nil {return _fb ;};_gf :=page .Rotate ;_bg :=_gf !=nil &&*_gf %360!=0&&*_gf %90==0;_f .Normalize ();_ee ,_gge ,_bc ,_dc :=_f .Llx ,_f .Lly ,_f .Width (),_f .Height ();_a :=_ee !=0||_gge !=0;if !_bg &&!_a {return nil ;};_gd :=func (_fc ,_gdd ,_gfd float64 )_b .BoundingBox {return _b .Path {Points :[]_b .Point {_b .NewPoint (0,0).Rotate (_gfd ),_b .NewPoint (_fc ,0).Rotate (_gfd ),_b .NewPoint (0,_gdd ).Rotate (_gfd ),_b .NewPoint (_fc ,_gdd ).Rotate (_gfd )}}.GetBoundingBox ();};_c :=_gg .NewContentCreator ();var _ag float64 ;if _bg {_ag =-float64 (*page .Rotate );_eb :=_gd (_bc ,_dc ,_ag );_c .Translate ((_eb .Width -_bc )/2+_bc /2,(_eb .Height -_dc )/2+_dc /2);_c .RotateDeg (_ag );_c .Translate (-_bc /2,-_dc /2);_bc ,_dc =_eb .Width ,_eb .Height ;};if _a {_c .Translate (-_ee ,-_gge );};_ad :=_c .Operations ().String ();_be =append ([]string {_ad },_be ...);if _fb =page .SetContentStreams (_be ,nil );_fb !=nil {return _fb ;};*_f =_g .PdfRectangle {Urx :_bc ,Ury :_dc };if _dcc :=page .CropBox ;_dcc !=nil {_dcc .Normalize ();_gddg ,_ca ,_cd ,_dd :=_dcc .Llx -_ee ,_dcc .Lly -_gge ,_dcc .Width (),_dcc .Height ();if _bg {_eec :=_gd (_cd ,_dd ,_ag );_cd ,_dd =_eec .Width ,_eec .Height ;};*_dcc =_g .PdfRectangle {Llx :_gddg ,Lly :_ca ,Urx :_gddg +_cd ,Ury :_ca +_dd };};_d .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",_ag ,_ad ,_f );page .Rotate =nil ;return nil ;};