unipdf/pdfutil/pdfutil.go
2024-09-14 00:10:30 +00:00

36 lines
3.5 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 (_f "github.com/unidoc/unipdf/v3/common";_agc "github.com/unidoc/unipdf/v3/contentstream";_ag "github.com/unidoc/unipdf/v3/contentstream/draw";_d "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.
// 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 *_e .PdfPage )error {_fd ,_db :=page .GetMediaBox ();if _db !=nil {return _db ;};_ae ,_db :=page .GetRotate ();if _db !=nil {_f .Log .Debug ("\u0045\u0052R\u004f\u0052\u003a\u0020\u0025\u0073\u0020\u002d\u0020\u0069\u0067\u006e\u006f\u0072\u0069\u006e\u0067\u0020\u0061\u006e\u0064\u0020\u0061\u0073\u0073\u0075\u006d\u0069\u006e\u0067\u0020\u006e\u006f\u0020\u0072\u006f\u0074\u0061\u0074\u0069\u006f\u006e\u000a",_db .Error ());
};_ab :=_ae %360!=0&&_ae %90==0;_fd .Normalize ();_da ,_ff ,_c ,_dc :=_fd .Llx ,_fd .Lly ,_fd .Width (),_fd .Height ();_g :=_da !=0||_ff !=0;if !_ab &&!_g {return nil ;};_dbd :=func (_b ,_aec ,_fb float64 )_ag .BoundingBox {return _ag .Path {Points :[]_ag .Point {_ag .NewPoint (0,0).Rotate (_fb ),_ag .NewPoint (_b ,0).Rotate (_fb ),_ag .NewPoint (0,_aec ).Rotate (_fb ),_ag .NewPoint (_b ,_aec ).Rotate (_fb )}}.GetBoundingBox ();
};_aea :=_agc .NewContentCreator ();var _gc float64 ;if _ab {_gc =-float64 (_ae );_eg :=_dbd (_c ,_dc ,_gc );_aea .Translate ((_eg .Width -_c )/2+_c /2,(_eg .Height -_dc )/2+_dc /2);_aea .RotateDeg (_gc );_aea .Translate (-_c /2,-_dc /2);_c ,_dc =_eg .Width ,_eg .Height ;
};if _g {_aea .Translate (-_da ,-_ff );};_cc :=_aea .Operations ();_ea ,_db :=_d .MakeStream (_cc .Bytes (),_d .NewFlateEncoder ());if _db !=nil {return _db ;};_dbe :=_d .MakeArray (_ea );_dbe .Append (page .GetContentStreamObjs ()...);*_fd =_e .PdfRectangle {Urx :_c ,Ury :_dc };
if _abg :=page .CropBox ;_abg !=nil {_abg .Normalize ();_gf ,_gff ,_cb ,_ad :=_abg .Llx -_da ,_abg .Lly -_ff ,_abg .Width (),_abg .Height ();if _ab {_cg :=_dbd (_cb ,_ad ,_gc );_cb ,_ad =_cg .Width ,_cg .Height ;};*_abg =_e .PdfRectangle {Llx :_gf ,Lly :_gff ,Urx :_gf +_cb ,Ury :_gff +_ad };
};_f .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 ,_cc ,_fd );page .Contents =_dbe ;page .Rotate =nil ;
return nil ;};