mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
29 lines
3.1 KiB
Go
29 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 (_ef "github.com/unidoc/unipdf/v3/common";_gb "github.com/unidoc/unipdf/v3/contentstream";_g "github.com/unidoc/unipdf/v3/contentstream/draw";_f "github.com/unidoc/unipdf/v3/core";_ec "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 *_ec .PdfPage )error {_eg ,_b :=page .GetMediaBox ();if _b !=nil {return _b ;};_c :=page .Rotate ;_d :=_c !=nil &&*_c %360!=0&&*_c %90==0;_eg .Normalize ();_bc ,_ca ,_fd ,_bd :=_eg .Llx ,_eg .Lly ,_eg .Width (),_eg .Height ();_eb :=_bc !=0||_ca !=0;if !_d &&!_eb {return nil ;};_ce :=func (_ed ,_cg ,_bca float64 )_g .BoundingBox {return _g .Path {Points :[]_g .Point {_g .NewPoint (0,0).Rotate (_bca ),_g .NewPoint (_ed ,0).Rotate (_bca ),_g .NewPoint (0,_cg ).Rotate (_bca ),_g .NewPoint (_ed ,_cg ).Rotate (_bca )}}.GetBoundingBox ();};_fa :=_gb .NewContentCreator ();var _gd float64 ;if _d {_gd =-float64 (*page .Rotate );_edd :=_ce (_fd ,_bd ,_gd );_fa .Translate ((_edd .Width -_fd )/2+_fd /2,(_edd .Height -_bd )/2+_bd /2);_fa .RotateDeg (_gd );_fa .Translate (-_fd /2,-_bd /2);_fd ,_bd =_edd .Width ,_edd .Height ;};if _eb {_fa .Translate (-_bc ,-_ca );};_egg :=_fa .Operations ();_ee ,_b :=_f .MakeStream (_egg .Bytes (),_f .NewFlateEncoder ());if _b !=nil {return _b ;};_bg :=_f .MakeArray (_ee );_bg .Append (page .GetContentStreamObjs ()...);*_eg =_ec .PdfRectangle {Urx :_fd ,Ury :_bd };if _eee :=page .CropBox ;_eee !=nil {_eee .Normalize ();_bge ,_a ,_ecb ,_cc :=_eee .Llx -_bc ,_eee .Lly -_ca ,_eee .Width (),_eee .Height ();if _d {_fad :=_ce (_ecb ,_cc ,_gd );_ecb ,_cc =_fad .Width ,_fad .Height ;};*_eee =_ec .PdfRectangle {Llx :_bge ,Lly :_a ,Urx :_bge +_ecb ,Ury :_a +_cc };};_ef .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",_gd ,_egg ,_eg );page .Contents =_bg ;page .Rotate =nil ;return nil ;}; |