unipdf/pdfutil/pdfutil.go
2021-05-31 17:17:31 +00:00

35 lines
3.1 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 (_a "github.com/unidoc/unipdf/v3/common";_d "github.com/unidoc/unipdf/v3/contentstream";_ag "github.com/unidoc/unipdf/v3/contentstream/draw";_aca "github.com/unidoc/unipdf/v3/core";_ac "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.
// NOTE: This function does not normalize annotations, outlines other parts
// that are not part of the basic geometry and page content streams.
func NormalizePage (page *_ac .PdfPage )error {_c ,_g :=page .GetMediaBox ();if _g !=nil {return _g ;};_f :=page .Rotate ;_de :=_f !=nil &&*_f %360!=0&&*_f %90==0;_c .Normalize ();_ce ,_cc ,_gd ,_ad :=_c .Llx ,_c .Lly ,_c .Width (),_c .Height ();_gg :=_ce !=0||_cc !=0;
if !_de &&!_gg {return nil ;};_db :=func (_ge ,_adc ,_acc float64 )_ag .BoundingBox {return _ag .Path {Points :[]_ag .Point {_ag .NewPoint (0,0).Rotate (_acc ),_ag .NewPoint (_ge ,0).Rotate (_acc ),_ag .NewPoint (0,_adc ).Rotate (_acc ),_ag .NewPoint (_ge ,_adc ).Rotate (_acc )}}.GetBoundingBox ();
};_eb :=_d .NewContentCreator ();var _gc float64 ;if _de {_gc =-float64 (*page .Rotate );_gdg :=_db (_gd ,_ad ,_gc );_eb .Translate ((_gdg .Width -_gd )/2+_gd /2,(_gdg .Height -_ad )/2+_ad /2);_eb .RotateDeg (_gc );_eb .Translate (-_gd /2,-_ad /2);_gd ,_ad =_gdg .Width ,_gdg .Height ;
};if _gg {_eb .Translate (-_ce ,-_cc );};_dg :=_eb .Operations ();_cb ,_g :=_aca .MakeStream (_dg .Bytes (),_aca .NewFlateEncoder ());if _g !=nil {return _g ;};_cd :=_aca .MakeArray (_cb );_cd .Append (page .GetContentStreamObjs ()...);*_c =_ac .PdfRectangle {Urx :_gd ,Ury :_ad };
if _cba :=page .CropBox ;_cba !=nil {_cba .Normalize ();_cee ,_ef ,_fd ,_ggb :=_cba .Llx -_ce ,_cba .Lly -_cc ,_cba .Width (),_cba .Height ();if _de {_dc :=_db (_fd ,_ggb ,_gc );_fd ,_ggb =_dc .Width ,_dc .Height ;};*_cba =_ac .PdfRectangle {Llx :_cee ,Lly :_ef ,Urx :_cee +_fd ,Ury :_ef +_ggb };
};_a .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",_gc ,_dg ,_c );page .Contents =_cd ;page .Rotate =nil ;
return nil ;};