unipdf/pdfutil/pdfutil.go
2024-12-20 06:39:10 +00:00

36 lines
3.5 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 (_e "github.com/unidoc/unipdf/v3/common";_ec "github.com/unidoc/unipdf/v3/contentstream";_gg "github.com/unidoc/unipdf/v3/contentstream/draw";_g "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 {_fe ,_b :=page .GetMediaBox ();if _b !=nil {return _b ;};_c ,_b :=page .GetRotate ();if _b !=nil {_e .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",_b .Error ());
};_bf :=_c %360!=0&&_c %90==0;_fe .Normalize ();_cd ,_cg ,_dc ,_ed :=_fe .Llx ,_fe .Lly ,_fe .Width (),_fe .Height ();_da :=_cd !=0||_cg !=0;if !_bf &&!_da {return nil ;};_cf :=func (_ca ,_bfa ,_cdf float64 )_gg .BoundingBox {return _gg .Path {Points :[]_gg .Point {_gg .NewPoint (0,0).Rotate (_cdf ),_gg .NewPoint (_ca ,0).Rotate (_cdf ),_gg .NewPoint (0,_bfa ).Rotate (_cdf ),_gg .NewPoint (_ca ,_bfa ).Rotate (_cdf )}}.GetBoundingBox ();
};_fa :=_ec .NewContentCreator ();var _db float64 ;if _bf {_db =-float64 (_c );_a :=_cf (_dc ,_ed ,_db );_fa .Translate ((_a .Width -_dc )/2+_dc /2,(_a .Height -_ed )/2+_ed /2);_fa .RotateDeg (_db );_fa .Translate (-_dc /2,-_ed /2);_dc ,_ed =_a .Width ,_a .Height ;
};if _da {_fa .Translate (-_cd ,-_cg );};_ce :=_fa .Operations ();_ab ,_b :=_g .MakeStream (_ce .Bytes (),_g .NewFlateEncoder ());if _b !=nil {return _b ;};_abe :=_g .MakeArray (_ab );_abe .Append (page .GetContentStreamObjs ()...);*_fe =_f .PdfRectangle {Urx :_dc ,Ury :_ed };
if _gc :=page .CropBox ;_gc !=nil {_gc .Normalize ();_cc ,_fb ,_dba ,_cde :=_gc .Llx -_cd ,_gc .Lly -_cg ,_gc .Width (),_gc .Height ();if _bf {_gb :=_cf (_dba ,_cde ,_db );_dba ,_cde =_gb .Width ,_gb .Height ;};*_gc =_f .PdfRectangle {Llx :_cc ,Lly :_fb ,Urx :_cc +_dba ,Ury :_fb +_cde };
};_e .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",_db ,_ce ,_fe );page .Contents =_abe ;page .Rotate =nil ;
return nil ;};