mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
35 lines
3.4 KiB
Go
35 lines
3.4 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";_b "github.com/unidoc/unipdf/v3/contentstream";_fd "github.com/unidoc/unipdf/v3/contentstream/draw";_eg "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 {_ac ,_bf :=page .GetMediaBox ();if _bf !=nil {return _bf ;};_g ,_bf :=page .GetRotate ();if _bf !=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",_bf .Error ());
|
|
};_ba :=_g %360!=0&&_g %90==0;_ac .Normalize ();_ea ,_bc ,_gd ,_c :=_ac .Llx ,_ac .Lly ,_ac .Width (),_ac .Height ();_ed :=_ea !=0||_bc !=0;if !_ba &&!_ed {return nil ;};_ge :=func (_af ,_cc ,_gb float64 )_fd .BoundingBox {return _fd .Path {Points :[]_fd .Point {_fd .NewPoint (0,0).Rotate (_gb ),_fd .NewPoint (_af ,0).Rotate (_gb ),_fd .NewPoint (0,_cc ).Rotate (_gb ),_fd .NewPoint (_af ,_cc ).Rotate (_gb )}}.GetBoundingBox ();
|
|
};_ec :=_b .NewContentCreator ();var _gc float64 ;if _ba {_gc =-float64 (_g );_bd :=_ge (_gd ,_c ,_gc );_ec .Translate ((_bd .Width -_gd )/2+_gd /2,(_bd .Height -_c )/2+_c /2);_ec .RotateDeg (_gc );_ec .Translate (-_gd /2,-_c /2);_gd ,_c =_bd .Width ,_bd .Height ;
|
|
};if _ed {_ec .Translate (-_ea ,-_bc );};_ga :=_ec .Operations ();_bfc ,_bf :=_eg .MakeStream (_ga .Bytes (),_eg .NewFlateEncoder ());if _bf !=nil {return _bf ;};_bg :=_eg .MakeArray (_bfc );_bg .Append (page .GetContentStreamObjs ()...);*_ac =_f .PdfRectangle {Urx :_gd ,Ury :_c };
|
|
if _gg :=page .CropBox ;_gg !=nil {_gg .Normalize ();_gf ,_d ,_ff ,_gba :=_gg .Llx -_ea ,_gg .Lly -_bc ,_gg .Width (),_gg .Height ();if _ba {_ef :=_ge (_ff ,_gba ,_gc );_ff ,_gba =_ef .Width ,_ef .Height ;};*_gg =_f .PdfRectangle {Llx :_gf ,Lly :_d ,Urx :_gf +_ff ,Ury :_d +_gba };
|
|
};_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",_gc ,_ga ,_ac );page .Contents =_bg ;page .Rotate =nil ;
|
|
return nil ;}; |