unipdf/pdfutil/pdfutil.go
2021-03-13 21:28:23 +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 (_d "github.com/unidoc/unipdf/v3/common";_e "github.com/unidoc/unipdf/v3/contentstream";_gb "github.com/unidoc/unipdf/v3/contentstream/draw";_a "github.com/unidoc/unipdf/v3/core";_db "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 *_db .PdfPage )error {_c ,_af :=page .GetMediaBox ();if _af !=nil {return _af ;};_b :=page .Rotate ;_f :=_b !=nil &&*_b %360!=0&&*_b %90==0;_c .Normalize ();_gbc ,_bb ,_cd ,_gg :=_c .Llx ,_c .Lly ,_c .Width (),_c .Height ();_ea :=_gbc !=0||_bb !=0;
if !_f &&!_ea {return nil ;};_gd :=func (_cb ,_da ,_ac float64 )_gb .BoundingBox {return _gb .Path {Points :[]_gb .Point {_gb .NewPoint (0,0).Rotate (_ac ),_gb .NewPoint (_cb ,0).Rotate (_ac ),_gb .NewPoint (0,_da ).Rotate (_ac ),_gb .NewPoint (_cb ,_da ).Rotate (_ac )}}.GetBoundingBox ();
};_cg :=_e .NewContentCreator ();var _de float64 ;if _f {_de =-float64 (*page .Rotate );_ga :=_gd (_cd ,_gg ,_de );_cg .Translate ((_ga .Width -_cd )/2+_cd /2,(_ga .Height -_gg )/2+_gg /2);_cg .RotateDeg (_de );_cg .Translate (-_cd /2,-_gg /2);_cd ,_gg =_ga .Width ,_ga .Height ;
};if _ea {_cg .Translate (-_gbc ,-_bb );};_ad :=_cg .Operations ();_fe ,_af :=_a .MakeStream (_ad .Bytes (),_a .NewFlateEncoder ());if _af !=nil {return _af ;};_gad :=_a .MakeArray (_fe );_gad .Append (page .GetContentStreamObjs ()...);*_c =_db .PdfRectangle {Urx :_cd ,Ury :_gg };
if _ca :=page .CropBox ;_ca !=nil {_ca .Normalize ();_ab ,_dec ,_be ,_bba :=_ca .Llx -_gbc ,_ca .Lly -_bb ,_ca .Width (),_ca .Height ();if _f {_cgc :=_gd (_be ,_bba ,_de );_be ,_bba =_cgc .Width ,_cgc .Height ;};*_ca =_db .PdfRectangle {Llx :_ab ,Lly :_dec ,Urx :_ab +_be ,Ury :_dec +_bba };
};_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",_de ,_ad ,_c );page .Contents =_gad ;page .Rotate =nil ;
return nil ;};