unipdf/pdfutil/pdfutil.go
2022-09-23 18:05:51 +00:00

35 lines
3.4 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";_af "github.com/unidoc/unipdf/v3/contentstream";_eg "github.com/unidoc/unipdf/v3/contentstream/draw";_e "github.com/unidoc/unipdf/v3/core";_f "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 *_f .PdfPage )error {_ac ,_ae :=page .GetMediaBox ();if _ae !=nil {return _ae ;};_g ,_ae :=page .GetRotate ();if _ae !=nil {_a .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",_ae .Error ());
};_ge :=_g %360!=0&&_g %90==0;_ac .Normalize ();_b ,_aef ,_c ,_gb :=_ac .Llx ,_ac .Lly ,_ac .Width (),_ac .Height ();_fg :=_b !=0||_aef !=0;if !_ge &&!_fg {return nil ;};_gg :=func (_dd ,_fb ,_cd float64 )_eg .BoundingBox {return _eg .Path {Points :[]_eg .Point {_eg .NewPoint (0,0).Rotate (_cd ),_eg .NewPoint (_dd ,0).Rotate (_cd ),_eg .NewPoint (0,_fb ).Rotate (_cd ),_eg .NewPoint (_dd ,_fb ).Rotate (_cd )}}.GetBoundingBox ();
};_ggc :=_af .NewContentCreator ();var _gf float64 ;if _ge {_gf =-float64 (_g );_afa :=_gg (_c ,_gb ,_gf );_ggc .Translate ((_afa .Width -_c )/2+_c /2,(_afa .Height -_gb )/2+_gb /2);_ggc .RotateDeg (_gf );_ggc .Translate (-_c /2,-_gb /2);_c ,_gb =_afa .Width ,_afa .Height ;
};if _fg {_ggc .Translate (-_b ,-_aef );};_gge :=_ggc .Operations ();_be ,_ae :=_e .MakeStream (_gge .Bytes (),_e .NewFlateEncoder ());if _ae !=nil {return _ae ;};_cf :=_e .MakeArray (_be );_cf .Append (page .GetContentStreamObjs ()...);*_ac =_f .PdfRectangle {Urx :_c ,Ury :_gb };
if _gec :=page .CropBox ;_gec !=nil {_gec .Normalize ();_ec ,_ff ,_ca ,_fa :=_gec .Llx -_b ,_gec .Lly -_aef ,_gec .Width (),_gec .Height ();if _ge {_bb :=_gg (_ca ,_fa ,_gf );_ca ,_fa =_bb .Width ,_bb .Height ;};*_gec =_f .PdfRectangle {Llx :_ec ,Lly :_ff ,Urx :_ec +_ca ,Ury :_ff +_fa };
};_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",_gf ,_gge ,_ac );page .Contents =_cf ;page .Rotate =nil ;
return nil ;};