mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-24 13:48:49 +08:00
35 lines
3.1 KiB
Go
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 (_c "github.com/unidoc/unipdf/v3/common";_gg "github.com/unidoc/unipdf/v3/contentstream";_g "github.com/unidoc/unipdf/v3/contentstream/draw";_f "github.com/unidoc/unipdf/v3/core";_cf "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 *_cf .PdfPage )error {_a ,_ae :=page .GetMediaBox ();if _ae !=nil {return _ae ;};_fc :=page .Rotate ;_cb :=_fc !=nil &&*_fc %360!=0&&*_fc %90==0;_a .Normalize ();_bf ,_cc ,_bc ,_aa :=_a .Llx ,_a .Lly ,_a .Width (),_a .Height ();
|
|
_fb :=_bf !=0||_cc !=0;if !_cb &&!_fb {return nil ;};_bg :=func (_bd ,_cg ,_d float64 )_g .BoundingBox {return _g .Path {Points :[]_g .Point {_g .NewPoint (0,0).Rotate (_d ),_g .NewPoint (_bd ,0).Rotate (_d ),_g .NewPoint (0,_cg ).Rotate (_d ),_g .NewPoint (_bd ,_cg ).Rotate (_d )}}.GetBoundingBox ();
|
|
};_ge :=_gg .NewContentCreator ();var _geb float64 ;if _cb {_geb =-float64 (*page .Rotate );_cfc :=_bg (_bc ,_aa ,_geb );_ge .Translate ((_cfc .Width -_bc )/2+_bc /2,(_cfc .Height -_aa )/2+_aa /2);_ge .RotateDeg (_geb );_ge .Translate (-_bc /2,-_aa /2);
|
|
_bc ,_aa =_cfc .Width ,_cfc .Height ;};if _fb {_ge .Translate (-_bf ,-_cc );};_ab :=_ge .Operations ();_e ,_ae :=_f .MakeStream (_ab .Bytes (),_f .NewFlateEncoder ());if _ae !=nil {return _ae ;};_cgb :=_f .MakeArray (_e );_cgb .Append (page .GetContentStreamObjs ()...);
|
|
*_a =_cf .PdfRectangle {Urx :_bc ,Ury :_aa };if _be :=page .CropBox ;_be !=nil {_be .Normalize ();_cbc ,_ee ,_ca ,_abd :=_be .Llx -_bf ,_be .Lly -_cc ,_be .Width (),_be .Height ();if _cb {_bgb :=_bg (_ca ,_abd ,_geb );_ca ,_abd =_bgb .Width ,_bgb .Height ;
|
|
};*_be =_cf .PdfRectangle {Llx :_cbc ,Lly :_ee ,Urx :_cbc +_ca ,Ury :_ee +_abd };};_c .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",_geb ,_ab ,_a );
|
|
page .Contents =_cgb ;page .Rotate =nil ;return nil ;}; |