2020-09-14 09:32:45 +00:00
|
|
|
//
|
|
|
|
// 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/
|
|
|
|
|
2022-07-13 21:28:43 +00:00
|
|
|
package pdfutil ;import (_b "github.com/unidoc/unipdf/v3/common";_ef "github.com/unidoc/unipdf/v3/contentstream";_bb "github.com/unidoc/unipdf/v3/contentstream/draw";_c "github.com/unidoc/unipdf/v3/core";_d "github.com/unidoc/unipdf/v3/model";);
|
2020-09-14 09:32:45 +00:00
|
|
|
|
2020-09-21 01:20:10 +00:00
|
|
|
// 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.
|
2020-09-14 09:32:45 +00:00
|
|
|
// After normalization, the page should look the same if openend using a
|
|
|
|
// PDF viewer.
|
2020-10-19 10:58:10 +00:00
|
|
|
// NOTE: This function does not normalize annotations, outlines other parts
|
|
|
|
// that are not part of the basic geometry and page content streams.
|
2022-07-13 21:28:43 +00:00
|
|
|
func NormalizePage (page *_d .PdfPage )error {_g ,_f :=page .GetMediaBox ();if _f !=nil {return _f ;};_a ,_f :=page .GetRotate ();if _f !=nil {_b .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",_f .Error ());
|
|
|
|
};_fc :=_a %360!=0&&_a %90==0;_g .Normalize ();_cf ,_eg ,_gg ,_gc :=_g .Llx ,_g .Lly ,_g .Width (),_g .Height ();_ee :=_cf !=0||_eg !=0;if !_fc &&!_ee {return nil ;};_dc :=func (_gge ,_be ,_ea float64 )_bb .BoundingBox {return _bb .Path {Points :[]_bb .Point {_bb .NewPoint (0,0).Rotate (_ea ),_bb .NewPoint (_gge ,0).Rotate (_ea ),_bb .NewPoint (0,_be ).Rotate (_ea ),_bb .NewPoint (_gge ,_be ).Rotate (_ea )}}.GetBoundingBox ();
|
|
|
|
};_ad :=_ef .NewContentCreator ();var _bf float64 ;if _fc {_bf =-float64 (_a );_ed :=_dc (_gg ,_gc ,_bf );_ad .Translate ((_ed .Width -_gg )/2+_gg /2,(_ed .Height -_gc )/2+_gc /2);_ad .RotateDeg (_bf );_ad .Translate (-_gg /2,-_gc /2);_gg ,_gc =_ed .Width ,_ed .Height ;
|
|
|
|
};if _ee {_ad .Translate (-_cf ,-_eg );};_df :=_ad .Operations ();_ab ,_f :=_c .MakeStream (_df .Bytes (),_c .NewFlateEncoder ());if _f !=nil {return _f ;};_ce :=_c .MakeArray (_ab );_ce .Append (page .GetContentStreamObjs ()...);*_g =_d .PdfRectangle {Urx :_gg ,Ury :_gc };
|
|
|
|
if _beg :=page .CropBox ;_beg !=nil {_beg .Normalize ();_bc ,_db ,_egc ,_af :=_beg .Llx -_cf ,_beg .Lly -_eg ,_beg .Width (),_beg .Height ();if _fc {_aa :=_dc (_egc ,_af ,_bf );_egc ,_af =_aa .Width ,_aa .Height ;};*_beg =_d .PdfRectangle {Llx :_bc ,Lly :_db ,Urx :_bc +_egc ,Ury :_db +_af };
|
|
|
|
};_b .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",_bf ,_df ,_g );page .Contents =_ce ;page .Rotate =nil ;
|
2021-09-23 22:37:42 +00:00
|
|
|
return nil ;};
|