unipdf/pdfutil/pdfutil.go

36 lines
3.4 KiB
Go
Raw Normal View History

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/
2024-11-22 00:42:43 +00:00
package pdfutil ;import (_g "github.com/unidoc/unipdf/v3/common";_a "github.com/unidoc/unipdf/v3/contentstream";_b "github.com/unidoc/unipdf/v3/contentstream/draw";_e "github.com/unidoc/unipdf/v3/core";_ce "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:
2024-03-27 22:34:33 +00:00
// - 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.
2024-11-22 00:42:43 +00:00
func NormalizePage (page *_ce .PdfPage )error {_gd ,_f :=page .GetMediaBox ();if _f !=nil {return _f ;};_bf ,_f :=page .GetRotate ();if _f !=nil {_g .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 ());
};_bg :=_bf %360!=0&&_bf %90==0;_gd .Normalize ();_ad ,_d ,_aa ,_cb :=_gd .Llx ,_gd .Lly ,_gd .Width (),_gd .Height ();_af :=_ad !=0||_d !=0;if !_bg &&!_af {return nil ;};_gdc :=func (_ga ,_ef ,_ac float64 )_b .BoundingBox {return _b .Path {Points :[]_b .Point {_b .NewPoint (0,0).Rotate (_ac ),_b .NewPoint (_ga ,0).Rotate (_ac ),_b .NewPoint (0,_ef ).Rotate (_ac ),_b .NewPoint (_ga ,_ef ).Rotate (_ac )}}.GetBoundingBox ();
};_efc :=_a .NewContentCreator ();var _gc float64 ;if _bg {_gc =-float64 (_bf );_gg :=_gdc (_aa ,_cb ,_gc );_efc .Translate ((_gg .Width -_aa )/2+_aa /2,(_gg .Height -_cb )/2+_cb /2);_efc .RotateDeg (_gc );_efc .Translate (-_aa /2,-_cb /2);_aa ,_cb =_gg .Width ,_gg .Height ;
};if _af {_efc .Translate (-_ad ,-_d );};_bb :=_efc .Operations ();_ca ,_f :=_e .MakeStream (_bb .Bytes (),_e .NewFlateEncoder ());if _f !=nil {return _f ;};_gb :=_e .MakeArray (_ca );_gb .Append (page .GetContentStreamObjs ()...);*_gd =_ce .PdfRectangle {Urx :_aa ,Ury :_cb };
if _ec :=page .CropBox ;_ec !=nil {_ec .Normalize ();_ge ,_eg ,_adf ,_gea :=_ec .Llx -_ad ,_ec .Lly -_d ,_ec .Width (),_ec .Height ();if _bg {_dc :=_gdc (_adf ,_gea ,_gc );_adf ,_gea =_dc .Width ,_dc .Height ;};*_ec =_ce .PdfRectangle {Llx :_ge ,Lly :_eg ,Urx :_ge +_adf ,Ury :_eg +_gea };
};_g .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",_gc ,_bb ,_gd );page .Contents =_gb ;page .Rotate =nil ;
2021-09-23 22:37:42 +00:00
return nil ;};