mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-07 19:29:16 +08:00
111 lines
23 KiB
Go
111 lines
23 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 redactor ;import (_bg "errors";_ba "fmt";_bd "github.com/unidoc/unipdf/v3/common";_bge "github.com/unidoc/unipdf/v3/contentstream";_cd "github.com/unidoc/unipdf/v3/core";_e "github.com/unidoc/unipdf/v3/creator";_fc "github.com/unidoc/unipdf/v3/extractor";
|
|
_dae "github.com/unidoc/unipdf/v3/model";_c "io";_f "regexp";_da "sort";_d "strings";);func _fef (_ff *_bge .ContentStreamOperation ,_dee _cd .PdfObject ,_cgbg []localSpanMarks )error {var _fag *_cd .PdfObjectArray ;_bfe ,_ggce :=_fcf (_cgbg );if len (_ggce )==1{_cgg :=_ggce [0];
|
|
_ffb :=_bfe [_cgg ];if len (_ffb )==1{_fbgf :=_ffb [0];_gae :=_fbgf ._gcgg ;_gb :=_bfa (_gae );_cgbe ,_deec :=_caf (_dee ,_gb );if _deec !=nil {return _deec ;};_daed ,_deec :=_agc (_fbgf ,_gae ,_gb ,_cgbe ,_cgg );if _deec !=nil {return _deec ;};_fag =_cd .MakeArray (_daed ...);
|
|
}else {_bca :=_ffb [0]._gcgg ;_cebg :=_bfa (_bca );_ddf ,_dde :=_caf (_dee ,_cebg );if _dde !=nil {return _dde ;};_edf ,_dde :=_agfb (_ddf ,_ffb );if _dde !=nil {return _dde ;};_bag :=_afcc (_edf );_ffg :=_bdf (_ddf ,_bag ,_cebg );_fag =_cd .MakeArray (_ffg ...);
|
|
};}else if len (_ggce )> 1{_gee :=_cgbg [0];_bcd :=_gee ._gcgg ;_ ,_ca :=_ebe (_bcd );_dcb :=_bcd .Elements ()[_ca ];_edg :=_dcb .Font ;_fecc ,_ffgc :=_caf (_dee ,_edg );if _ffgc !=nil {return _ffgc ;};_cbbc ,_ffgc :=_agfb (_fecc ,_cgbg );if _ffgc !=nil {return _ffgc ;
|
|
};_fbe :=_afcc (_cbbc );_gef :=_bdf (_fecc ,_fbe ,_edg );_fag =_cd .MakeArray (_gef ...);};_ff .Params [0]=_fag ;_ff .Operand ="\u0054\u004a";return nil ;};func _bdf (_ead string ,_ecae []replacement ,_beaf *_dae .PdfFont )[]_cd .PdfObject {_egfc :=[]_cd .PdfObject {};
|
|
_bed :=0;_afcf :=_ead ;for _fbc ,_abe :=range _ecae {_cccf :=_abe ._bc ;_dgefc :=_abe ._bb ;_ccg :=_abe ._cde ;_gcaf :=_cd .MakeFloat (_dgefc );if _bed > _cccf ||_cccf ==-1{continue ;};_egc :=_ead [_bed :_cccf ];_cebc :=_ecge (_egc ,_beaf );_agg :=_cd .MakeStringFromBytes (_cebc );
|
|
_egfc =append (_egfc ,_agg );_egfc =append (_egfc ,_gcaf );_agd :=_cccf +len (_ccg );_afcf =_ead [_agd :];_bed =_agd ;if _fbc ==len (_ecae )-1{_cebc =_ecge (_afcf ,_beaf );_agg =_cd .MakeStringFromBytes (_cebc );_egfc =append (_egfc ,_agg );};};return _egfc ;
|
|
};func _ffba (_eab int ,_ccbd []int )bool {for _ ,_ddc :=range _ccbd {if _ddc ==_eab {return true ;};};return false ;};func _afcc (_dea []placeHolders )[]replacement {_dfb :=[]replacement {};for _ ,_cbc :=range _dea {_afff :=_cbc ._fd ;_bgcg :=_cbc ._fb ;
|
|
_fcb :=_cbc ._cc ;for _ ,_acab :=range _afff {_cgfg :=replacement {_cde :_bgcg ,_bb :_fcb ,_bc :_acab };_dfb =append (_dfb ,_cgfg );};};_da .Slice (_dfb ,func (_cga ,_age int )bool {return _dfb [_cga ]._bc < _dfb [_age ]._bc });return _dfb ;};
|
|
|
|
// RectangleProps defines properties of the redaction rectangle to be drawn.
|
|
type RectangleProps struct{FillColor _e .Color ;BorderWidth float64 ;FillOpacity float64 ;};func _fcf (_cbbd []localSpanMarks )(map[string ][]localSpanMarks ,[]string ){_ccc :=make (map[string ][]localSpanMarks );_fca :=[]string {};for _ ,_bgcd :=range _cbbd {_fbg :=_bgcd ._gaa ;
|
|
if _fdcd ,_cce :=_ccc [_fbg ];_cce {_ccc [_fbg ]=append (_fdcd ,_bgcd );}else {_ccc [_fbg ]=[]localSpanMarks {_bgcd };_fca =append (_fca ,_fbg );};};return _ccc ,_fca ;};
|
|
|
|
// Redact executes the redact operation on a pdf file and updates the content streams of all pages of the file.
|
|
func (_aae *Redactor )Redact ()error {_bbac ,_fbgb :=_aae ._ggdb .GetNumPages ();if _fbgb !=nil {return _ba .Errorf ("\u0066\u0061\u0069\u006c\u0065\u0064 \u0074\u006f\u0020\u0067\u0065\u0074\u0020\u0074\u0068\u0065\u0020\u006e\u0075m\u0062\u0065\u0072\u0020\u006f\u0066\u0020P\u0061\u0067\u0065\u0073");
|
|
};_cdc :=_aae ._agbb .FillColor ;_afbb :=_aae ._agbb .BorderWidth ;_decd :=_aae ._agbb .FillOpacity ;for _gbd :=1;_gbd <=_bbac ;_gbd ++{_fea ,_cafd :=_aae ._ggdb .GetPage (_gbd );if _cafd !=nil {return _cafd ;};_cbg ,_cafd :=_fc .New (_fea );if _cafd !=nil {return _cafd ;
|
|
};_afbf ,_ ,_ ,_cafd :=_cbg .ExtractPageText ();if _cafd !=nil {return _cafd ;};_cgfb :=_afbf .GetContentStreamOps ();_cfd ,_fba ,_cafd :=_aae .redactPage (_cgfb ,_fea .Resources );if _fba ==nil {_bd .Log .Info ("N\u006f\u0020\u006d\u0061\u0074\u0063\u0068\u0020\u0066\u006f\u0075\u006e\u0064\u0020\u0066\u006f\u0072\u0020t\u0068\u0065\u0020\u0070\u0072\u006f\u0076\u0069\u0064\u0065d \u0070\u0061\u0074t\u0061r\u006e\u002e");
|
|
_fba =_cgfb ;};_eea :=_bge .ContentStreamOperation {Operand :"\u006e"};*_fba =append (*_fba ,&_eea );_fea .SetContentStreams ([]string {_fba .String ()},_cd .NewFlateEncoder ());if _cafd !=nil {return _cafd ;};_aggf ,_cafd :=_fea .GetMediaBox ();if _cafd !=nil {return _cafd ;
|
|
};if _fea .MediaBox ==nil {_fea .MediaBox =_aggf ;};if _cdcf :=_aae ._fgg .AddPage (_fea );_cdcf !=nil {return _cdcf ;};_da .Slice (_cfd ,func (_dgcb ,_fcac int )bool {return _cfd [_dgcb ]._fdb < _cfd [_fcac ]._fdb });_fdbf :=_aggf .Ury ;for _ ,_beb :=range _cfd {_bedd :=_beb ._eeg ;
|
|
_afd :=_aae ._fgg .NewRectangle (_bedd .Llx ,_fdbf -_bedd .Lly ,_bedd .Urx -_bedd .Llx ,-(_bedd .Ury -_bedd .Lly ));_afd .SetFillColor (_cdc );_afd .SetBorderWidth (_afbb );_afd .SetFillOpacity (_decd );if _efa :=_aae ._fgg .Draw (_afd );_efa !=nil {return nil ;
|
|
};};};_aae ._fgg .SetOutlineTree (_aae ._ggdb .GetOutlineTree ());return nil ;};func (_eff *Redactor )redactPage (_dbaf *_bge .ContentStreamOperations ,_fgeg *_dae .PdfPageResources )([]matchedBBox ,*_bge .ContentStreamOperations ,error ){_egcf ,_cfcg :=_fc .NewFromContents (_dbaf .String (),_fgeg );
|
|
if _cfcg !=nil {return nil ,nil ,_cfcg ;};_ebg ,_ ,_ ,_cfcg :=_egcf .ExtractPageText ();if _cfcg !=nil {return nil ,nil ,_cfcg ;};_dbaf =_ebg .GetContentStreamOps ();_aeg :=_ebg .Marks ();_cccd :=_ebg .Text ();_cccd ,_baag :=_acca (_cccd );_bcdb :=[]matchedBBox {};
|
|
_aafa :=make (map[_cd .PdfObject ][]localSpanMarks );_cgd :=[]*targetMap {};for _ ,_bab :=range _eff ._cec .Terms {_bec ,_dab :=_fcc (_bab );if _dab !=nil {return nil ,nil ,_dab ;};_gfc ,_dab :=_bec .match (_cccd );if _dab !=nil {return nil ,nil ,_dab ;
|
|
};_gfc =_ddg (_gfc ,_baag );_fced :=_ada (_gfc );_cgd =append (_cgd ,_fced ...);};_ddad (_cgd );for _ ,_aag :=range _cgd {_dgad :=_aag ._ebf ;_bbag :=_aag ._cfeb ;_dgege :=[]matchedBBox {};for _ ,_fgb :=range _bbag {_bfb ,_edb ,_cac :=_bcfa (_fgb ,_aeg ,_dgad );
|
|
if _cac !=nil {return nil ,nil ,_cac ;};_edgd :=_agce (_bfb );for _bfbe ,_acb :=range _edgd {_dgd :=localSpanMarks {_gcgg :_acb ,_eee :_bfbe ,_gaa :_dgad };_gcf ,_ :=_ebe (_acb );if _bdfd ,_bgf :=_aafa [_gcf ];_bgf {_aafa [_gcf ]=append (_bdfd ,_dgd );
|
|
}else {_aafa [_gcf ]=[]localSpanMarks {_dgd };};};_dgege =append (_dgege ,_edb );};_bcdb =append (_bcdb ,_dgege ...);};_cfcg =_df (_dbaf ,_aafa );if _cfcg !=nil {return nil ,nil ,_cfcg ;};return _bcdb ,_dbaf ,nil ;};func _ecd (_fga *_fc .TextMarkArray )string {_afb :="";
|
|
for _ ,_dgegf :=range _fga .Elements (){_afb +=_dgegf .Text ;};return _afb ;};type targetMap struct{_ebf string ;_cfeb [][]int ;};
|
|
|
|
// Redactor represents a Redactor object.
|
|
type Redactor struct{_ggdb *_dae .PdfReader ;_cec *RedactionOptions ;_fgg *_e .Creator ;_agbb *RectangleProps ;};func _gg (_eg *_bge .ContentStreamOperations ,_af string ,_dec int )error {_eb :=_bge .ContentStreamOperations {};var _fec _bge .ContentStreamOperation ;
|
|
for _bf ,_dfg :=range *_eg {if _bf ==_dec {if _af =="\u0027"{_ag :=_bge .ContentStreamOperation {Operand :"\u0054\u002a"};_eb =append (_eb ,&_ag );_fec .Params =_dfg .Params ;_fec .Operand ="\u0054\u006a";_eb =append (_eb ,&_fec );}else if _af =="\u0022"{_be :=_dfg .Params [:2];
|
|
Tc ,Tw :=_be [0],_be [1];_ge :=_bge .ContentStreamOperation {Params :[]_cd .PdfObject {Tc },Operand :"\u0054\u0063"};_eb =append (_eb ,&_ge );_ge =_bge .ContentStreamOperation {Params :[]_cd .PdfObject {Tw },Operand :"\u0054\u0077"};_eb =append (_eb ,&_ge );
|
|
_fec .Params =[]_cd .PdfObject {_dfg .Params [2]};_fec .Operand ="\u0054\u006a";_eb =append (_eb ,&_fec );};};_eb =append (_eb ,_dfg );};*_eg =_eb ;return nil ;};type matchedBBox struct{_eeg _dae .PdfRectangle ;_fdb string ;};
|
|
|
|
// RedactRectanglePropsNew return a new pointer to a default RectangleProps object.
|
|
func RedactRectanglePropsNew ()*RectangleProps {return &RectangleProps {FillColor :_e .ColorBlack ,BorderWidth :0.0,FillOpacity :1.0};};func _acca (_dbac string )(string ,[][]int ){_abb :=_f .MustCompile ("\u005c\u006e");_caag :=_abb .FindAllStringIndex (_dbac ,-1);
|
|
_gcee :=_abb .ReplaceAllString (_dbac ,"\u0020");return _gcee ,_caag ;};func _eace (_bgd *_fc .TextMarkArray )(float64 ,error ){_bbc ,_cae :=_bgd .BBox ();if !_cae {return 0.0,_ba .Errorf ("\u0073\u0070\u0061\u006e\u004d\u0061\u0072\u006bs\u002e\u0042\u0042ox\u0020\u0068\u0061\u0073\u0020\u006eo\u0020\u0062\u006f\u0075\u006e\u0064\u0069\u006e\u0067\u0020\u0062\u006f\u0078\u002e\u0020s\u0070\u0061\u006e\u004d\u0061\u0072\u006b\u0073=\u0025\u0073",_bgd );
|
|
};_ccba :=_ebc (_bgd );_baf :=0.0;_ ,_ace :=_ebe (_bgd );_dac :=_bgd .Elements ()[_ace ];_bcdf :=_dac .Font ;if _ccba > 0{_baf =_gd (_bcdf ,_dac );};_dcde :=(_bbc .Urx -_bbc .Llx );_dcde =_dcde +_baf *float64 (_ccba );Tj :=_geb (_dcde ,_dac .FontSize ,_dac .Th );
|
|
return Tj ,nil ;};
|
|
|
|
// RedactionOptions is a collection of RedactionTerm objects.
|
|
type RedactionOptions struct{Terms []RedactionTerm ;};func _ddad (_bagd []*targetMap ){for _ffa ,_faed :=range _bagd {for _ceg ,_cff :=range _bagd {if _ffa !=_ceg {_gcfd ,_aac :=_bcca (*_faed ,*_cff );if _gcfd {_cacc (_cff ,_aac );};};};};};func _geb (_bbbg ,_bcb ,_gcab float64 )float64 {_gcab =_gcab /100;
|
|
_ffcd :=(-1000*_bbbg )/(_bcb *_gcab );return _ffcd ;};func _bcca (_ded ,_dcg targetMap )(bool ,[]int ){_feee :=_d .Contains (_ded ._ebf ,_dcg ._ebf );var _eaa []int ;for _ ,_fgce :=range _ded ._cfeb {for _bgca ,_abf :=range _dcg ._cfeb {if _abf [0]>=_fgce [0]&&_abf [1]<=_fgce [1]{_eaa =append (_eaa ,_bgca );
|
|
};};};return _feee ,_eaa ;};
|
|
|
|
// RedactionTerm holds the regexp pattern and the replacement string for the redaction process.
|
|
type RedactionTerm struct{Pattern *_f .Regexp ;};func _bcfa (_gdf []int ,_cbfc *_fc .TextMarkArray ,_bbbc string )(*_fc .TextMarkArray ,matchedBBox ,error ){_dag :=matchedBBox {};_aacb :=_gdf [0];_agbf :=_gdf [1];_ecc :=len (_bbbc )-len (_d .TrimLeft (_bbbc ,"\u0020"));
|
|
_dfac :=len (_bbbc )-len (_d .TrimRight (_bbbc ,"\u0020\u000a"));_aacb =_aacb +_ecc ;_agbf =_agbf -_dfac ;_bbbc =_d .Trim (_bbbc ,"\u0020\u000a");_egaa ,_egcd :=_cbfc .RangeOffset (_aacb ,_agbf );if _egcd !=nil {return nil ,_dag ,_egcd ;};_bdg ,_gdfb :=_egaa .BBox ();
|
|
if !_gdfb {return nil ,_dag ,_ba .Errorf ("\u0073\u0070\u0061\u006e\u004d\u0061\u0072\u006bs\u002e\u0042\u0042ox\u0020\u0068\u0061\u0073\u0020\u006eo\u0020\u0062\u006f\u0075\u006e\u0064\u0069\u006e\u0067\u0020\u0062\u006f\u0078\u002e\u0020s\u0070\u0061\u006e\u004d\u0061\u0072\u006b\u0073=\u0025\u0073",_egaa );
|
|
};_dag =matchedBBox {_fdb :_bbbc ,_eeg :_bdg };return _egaa ,_dag ,nil ;};func _gbg (_gcbc ,_fgdd string )[]int {if len (_fgdd )==0{return nil ;};var _dda []int ;for _bcf :=0;_bcf < len (_gcbc );{_ecf :=_d .Index (_gcbc [_bcf :],_fgdd );if _ecf < 0{return _dda ;
|
|
};_dda =append (_dda ,_bcf +_ecf );_bcf +=_ecf +len (_fgdd );};return _dda ;};
|
|
|
|
// WriteToFile writes the redacted document to file specified by `outputPath`.
|
|
func (_acea *Redactor )WriteToFile (outputPath string )error {if _cca :=_acea ._fgg .WriteToFile (outputPath );_cca !=nil {return _ba .Errorf ("\u0066\u0061\u0069l\u0065\u0064\u0020\u0074o\u0020\u0077\u0072\u0069\u0074\u0065\u0020t\u0068\u0065\u0020\u006f\u0075\u0074\u0070\u0075\u0074\u0020\u0066\u0069\u006c\u0065");
|
|
};return nil ;};func _fcc (_cbd RedactionTerm )(*regexMatcher ,error ){return ®exMatcher {_bcfg :_cbd },nil };func _bcdg (_acd *_bge .ContentStreamOperations ,PdfObj _cd .PdfObject )(*_bge .ContentStreamOperation ,int ,bool ){for _cfg ,_dbd :=range *_acd {_deg :=_dbd .Operand ;
|
|
if _deg =="\u0054\u006a"{_gfe :=_cd .TraceToDirectObject (_dbd .Params [0]);if _gfe ==PdfObj {return _dbd ,_cfg ,true ;};}else if _deg =="\u0054\u004a"{_fgde ,_edc :=_cd .GetArray (_dbd .Params [0]);if !_edc {return nil ,_cfg ,_edc ;};for _ ,_faf :=range _fgde .Elements (){if _faf ==PdfObj {return _dbd ,_cfg ,true ;
|
|
};};}else if _deg =="\u0022"{_ef :=_cd .TraceToDirectObject (_dbd .Params [2]);if _ef ==PdfObj {return _dbd ,_cfg ,true ;};}else if _deg =="\u0027"{_gefe :=_cd .TraceToDirectObject (_dbd .Params [0]);if _gefe ==PdfObj {return _dbd ,_cfg ,true ;};};};return nil ,-1,false ;
|
|
};func _bfa (_aaffb *_fc .TextMarkArray )*_dae .PdfFont {_ ,_acc :=_ebe (_aaffb );_dcd :=_aaffb .Elements ()[_acc ];_fgd :=_dcd .Font ;return _fgd ;};func _gd (_feb *_dae .PdfFont ,_dg _fc .TextMark )float64 {_aaf :=0.001;_ae :=_dg .Th /100;if _feb .Subtype ()=="\u0054\u0079\u0070e\u0033"{_aaf =1;
|
|
};_agf ,_dfgb :=_feb .GetRuneMetrics (' ');if !_dfgb {_agf ,_dfgb =_feb .GetCharMetrics (32);};if !_dfgb {_agf ,_ =_dae .DefaultFont ().GetRuneMetrics (' ');};_dge :=_aaf *((_agf .Wx *_dg .FontSize +_dg .Tc +_dg .Tw )/_ae );return _dge ;};func _ada (_fcgb []*matchedIndex )[]*targetMap {_cegb :=make (map[string ][][]int );
|
|
_fdga :=[]*targetMap {};for _ ,_fgf :=range _fcgb {_eef :=_fgf ._fge ;_efd :=[]int {_fgf ._caee ,_fgf ._fee };if _dece ,_ffeg :=_cegb [_eef ];_ffeg {_cegb [_eef ]=append (_dece ,_efd );}else {_cegb [_eef ]=[][]int {_efd };};};for _bef ,_ccac :=range _cegb {_eda :=&targetMap {_ebf :_bef ,_cfeb :_ccac };
|
|
_fdga =append (_fdga ,_eda );};return _fdga ;};func _fg (_ad *_bge .ContentStreamOperation ,_cbf _cd .PdfObject ,_fdc []localSpanMarks )error {_agb ,_gc :=_cd .GetArray (_ad .Params [0]);_bea :=[]_cd .PdfObject {};if !_gc {_bd .Log .Debug ("\u0045\u0052\u0052OR\u003a\u0020\u0054\u004a\u0020\u006f\u0070\u003d\u0025s\u0020G\u0065t\u0041r\u0072\u0061\u0079\u0056\u0061\u006c\u0020\u0066\u0061\u0069\u006c\u0065\u0064",_ad );
|
|
return _ba .Errorf ("\u0073\u0070\u0061\u006e\u004d\u0061\u0072\u006bs\u002e\u0042\u0042ox\u0020\u0068\u0061\u0073\u0020\u006eo\u0020\u0062\u006f\u0075\u006e\u0064\u0069\u006e\u0067\u0020\u0062\u006f\u0078\u002e\u0020s\u0070\u0061\u006e\u004d\u0061\u0072\u006b\u0073=\u0025\u0073",_ad );
|
|
};_cea ,_ged :=_fcf (_fdc );if len (_ged )==1{_fa :=_ged [0];_gce :=_cea [_fa ];if len (_gce )==1{_baa :=_gce [0];_fda :=_baa ._gcgg ;_cgb :=_bfa (_fda );_ee ,_cfa :=_caf (_cbf ,_cgb );if _cfa !=nil {return _cfa ;};_ceb ,_cfa :=_agc (_baa ,_fda ,_cgb ,_ee ,_fa );
|
|
if _cfa !=nil {return _cfa ;};for _ ,_bcg :=range _agb .Elements (){if _bcg ==_cbf {_bea =append (_bea ,_ceb ...);}else {_bea =append (_bea ,_bcg );};};}else {_dc :=_gce [0]._gcgg ;_ccf :=_bfa (_dc );_egf ,_ggc :=_caf (_cbf ,_ccf );if _ggc !=nil {return _ggc ;
|
|
};_ecg ,_ggc :=_agfb (_egf ,_gce );if _ggc !=nil {return _ggc ;};_aff :=_afcc (_ecg );_gcg :=_bdf (_egf ,_aff ,_ccf );for _ ,_cda :=range _agb .Elements (){if _cda ==_cbf {_bea =append (_bea ,_gcg ...);}else {_bea =append (_bea ,_cda );};};};_ad .Params [0]=_cd .MakeArray (_bea ...);
|
|
}else if len (_ged )> 1{_gcb :=_fdc [0];_cgf :=_gcb ._gcgg ;_ ,_cdg :=_ebe (_cgf );_gca :=_cgf .Elements ()[_cdg ];_fcg :=_gca .Font ;_aa ,_eac :=_caf (_cbf ,_fcg );if _eac !=nil {return _eac ;};_fbd ,_eac :=_agfb (_aa ,_fdc );if _eac !=nil {return _eac ;
|
|
};_dca :=_afcc (_fbd );_ga :=_bdf (_aa ,_dca ,_fcg );for _ ,_daa :=range _agb .Elements (){if _daa ==_cbf {_bea =append (_bea ,_ga ...);}else {_bea =append (_bea ,_daa );};};_ad .Params [0]=_cd .MakeArray (_bea ...);};return nil ;};
|
|
|
|
// New instantiates a Redactor object with given PdfReader and `regex` pattern.
|
|
func New (reader *_dae .PdfReader ,opts *RedactionOptions ,rectProps *RectangleProps )*Redactor {if rectProps ==nil {rectProps =RedactRectanglePropsNew ();};return &Redactor {_ggdb :reader ,_cec :opts ,_fgg :_e .New (),_agbb :rectProps };};func _dacc (_fbeac *matchedIndex ,_acaf [][]int )(bool ,[][]int ){_eggb :=[][]int {};
|
|
for _ ,_edff :=range _acaf {if _fbeac ._caee < _edff [0]&&_fbeac ._fee > _edff [1]{_eggb =append (_eggb ,_edff );};};return len (_eggb )> 0,_eggb ;};type localSpanMarks struct{_gcgg *_fc .TextMarkArray ;_eee int ;_gaa string ;};func _agfb (_gad string ,_dba []localSpanMarks )([]placeHolders ,error ){_aab :="";
|
|
_cfe :=[]placeHolders {};for _fbb ,_fde :=range _dba {_fdg :=_fde ._gcgg ;_bac :=_fde ._gaa ;_dcbd :=_ecd (_fdg );_baca ,_bcc :=_eace (_fdg );if _bcc !=nil {return nil ,_bcc ;};if _dcbd !=_aab {var _bfcf []int ;if _fbb ==0&&_bac !=_dcbd {_cbe :=_d .Index (_gad ,_dcbd );
|
|
_bfcf =[]int {_cbe };}else if _fbb ==len (_dba )-1{_ffe :=_d .LastIndex (_gad ,_dcbd );_bfcf =[]int {_ffe };}else {_bfcf =_gbg (_gad ,_dcbd );};_fgc :=placeHolders {_fd :_bfcf ,_fb :_dcbd ,_cc :_baca };_cfe =append (_cfe ,_fgc );};_aab =_dcbd ;};return _cfe ,nil ;
|
|
};func _ddg (_bdfg []*matchedIndex ,_cdcb [][]int )[]*matchedIndex {_fdae :=[]*matchedIndex {};for _ ,_cagf :=range _bdfg {_gag ,_bcdd :=_dacc (_cagf ,_cdcb );if _gag {_gda :=_decc (_cagf ,_bcdd );_fdae =append (_fdae ,_gda ...);}else {_fdae =append (_fdae ,_cagf );
|
|
};};return _fdae ;};
|
|
|
|
// Write writes the content of `re.creator` to writer of type io.Writer interface.
|
|
func (_cfb *Redactor )Write (writer _c .Writer )error {return _cfb ._fgg .Write (writer )};func _agce (_fagc *_fc .TextMarkArray )[]*_fc .TextMarkArray {_ggdbe :=_fagc .Elements ();_decb :=len (_ggdbe );var _bdfe _cd .PdfObject ;_fcge :=[]*_fc .TextMarkArray {};
|
|
_ecac :=&_fc .TextMarkArray {};_bce :=-1;for _feag ,_bbe :=range _ggdbe {_fcgc :=_bbe .DirectObject ;_bce =_bbe .Index ;if _fcgc ==nil {_gab :=_ecb (_fagc ,_feag ,_bce );if _bdfe !=nil {if _gab ==-1||_gab > _feag {_fcge =append (_fcge ,_ecac );_ecac =&_fc .TextMarkArray {};
|
|
};};}else if _fcgc !=nil &&_bdfe ==nil {if _bce ==0&&_feag > 0{_fcge =append (_fcge ,_ecac );_ecac =&_fc .TextMarkArray {};};}else if _fcgc !=nil &&_bdfe !=nil {if _fcgc !=_bdfe {_fcge =append (_fcge ,_ecac );_ecac =&_fc .TextMarkArray {};};};_bdfe =_fcgc ;
|
|
_ecac .Append (_bbe );if _feag ==(_decb -1){_fcge =append (_fcge ,_ecac );};};return _fcge ;};func _cacc (_afbg *targetMap ,_fcab []int ){var _gebd [][]int ;for _aec ,_eag :=range _afbg ._cfeb {if _ffba (_aec ,_fcab ){continue ;};_gebd =append (_gebd ,_eag );
|
|
};_afbg ._cfeb =_gebd ;};func _ebe (_cdea *_fc .TextMarkArray )(_cd .PdfObject ,int ){var _daf _cd .PdfObject ;_cbb :=-1;for _bgc ,_ggg :=range _cdea .Elements (){_daf =_ggg .DirectObject ;_cbb =_bgc ;if _daf !=nil {break ;};};return _daf ,_cbb ;};func _ecb (_ddec *_fc .TextMarkArray ,_eagb int ,_egg int )int {_aaeb :=_ddec .Elements ();
|
|
_cgaf :=_eagb -1;_aea :=_eagb +1;_bde :=-1;if _cgaf >=0{_dbc :=_aaeb [_cgaf ];_bgbb :=_dbc .ObjString ;_dcf :=len (_bgbb );_fdca :=_dbc .Index ;if _fdca +1< _dcf {_bde =_cgaf ;return _bde ;};};if _aea < len (_aaeb ){_cgfc :=_aaeb [_aea ];_gdbe :=_cgfc .ObjString ;
|
|
if _gdbe [0]!=_cgfc .Text {_bde =_aea ;return _bde ;};};return _bde ;};func _df (_cg *_bge .ContentStreamOperations ,_cb map[_cd .PdfObject ][]localSpanMarks )error {for _g ,_de :=range _cb {if _g ==nil {continue ;};_db ,_ce ,_ec :=_bcdg (_cg ,_g );if !_ec {_bd .Log .Debug ("Pd\u0066\u004fb\u006a\u0065\u0063\u0074\u0020\u0025\u0073\u006e\u006ft\u0020\u0066\u006f\u0075\u006e\u0064\u0020\u0069\u006e\u0020\u0073\u0069\u0064\u0065\u0020\u0074\u0068\u0065\u0020\u0063\u006f\u006e\u0074\u0065\u006e\u0074\u0073\u0074r\u0065a\u006d\u0020\u006f\u0070\u0065\u0072\u0061\u0074i\u006fn\u0020\u0025s",_g ,_cg );
|
|
return nil ;};if _db .Operand =="\u0054\u006a"{_cf :=_fef (_db ,_g ,_de );if _cf !=nil {return _cf ;};}else if _db .Operand =="\u0054\u004a"{_fe :=_fg (_db ,_g ,_de );if _fe !=nil {return _fe ;};}else if _db .Operand =="\u0027"||_db .Operand =="\u0022"{_a :=_gg (_cg ,_db .Operand ,_ce );
|
|
if _a !=nil {return _a ;};_a =_fef (_db ,_g ,_de );if _a !=nil {return _a ;};};};return nil ;};func _ebc (_afc *_fc .TextMarkArray )int {_bgb :=0;_ega :=_afc .Elements ();if _ega [0].Text =="\u0020"{_bgb ++;};if _ega [_afc .Len ()-1].Text =="\u0020"{_bgb ++;
|
|
};return _bgb ;};func _agc (_dfd localSpanMarks ,_dga *_fc .TextMarkArray ,_bfc *_dae .PdfFont ,_cfc ,_ffd string )([]_cd .PdfObject ,error ){_caa :=_ecd (_dga );Tj ,_aeb :=_eace (_dga );if _aeb !=nil {return nil ,_aeb ;};_ffgb :=len (_cfc );_fce :=len (_caa );
|
|
_bbb :=-1;_gbb :=_cd .MakeFloat (Tj );if _caa !=_ffd {_geg :=_dfd ._eee ;if _geg ==0{_bbb =_d .LastIndex (_cfc ,_caa );}else {_bbb =_d .Index (_cfc ,_caa );};}else {_bbb =_d .Index (_cfc ,_caa );};_deb :=_bbb +_fce ;_faa :=[]_cd .PdfObject {};if _bbb ==0&&_deb ==_ffgb {_faa =append (_faa ,_gbb );
|
|
}else if _bbb ==0&&_deb < _ffgb {_aga :=_ecge (_cfc [_deb :],_bfc );_aebb :=_cd .MakeStringFromBytes (_aga );_faa =append (_faa ,_gbb ,_aebb );}else if _bbb > 0&&_deb >=_ffgb {_adb :=_ecge (_cfc [:_bbb ],_bfc );_bfag :=_cd .MakeStringFromBytes (_adb );
|
|
_faa =append (_faa ,_bfag ,_gbb );}else if _bbb > 0&&_deb < _ffgb {_ccb :=_ecge (_cfc [:_bbb ],_bfc );_cag :=_ecge (_cfc [_deb :],_bfc );_dgef :=_cd .MakeStringFromBytes (_ccb );_dbf :=_cd .MakeString (string (_cag ));_faa =append (_faa ,_dgef ,_gbb ,_dbf );
|
|
};return _faa ,nil ;};type placeHolders struct{_fd []int ;_fb string ;_cc float64 ;};type matchedIndex struct{_caee int ;_fee int ;_fge string ;};type regexMatcher struct{_bcfg RedactionTerm };func _decc (_cgfga *matchedIndex ,_dgdg [][]int )[]*matchedIndex {_cdeb :=[]*matchedIndex {};
|
|
_fbea :=_cgfga ._caee ;_gcc :=_fbea ;_dacf :=_cgfga ._fge ;_faab :=0;for _ ,_abbg :=range _dgdg {_fcbc :=_abbg [0]-_fbea ;if _faab >=_fcbc {continue ;};_dbfg :=_dacf [_faab :_fcbc ];_cfdf :=&matchedIndex {_fge :_dbfg ,_caee :_gcc ,_fee :_abbg [0]};if len (_d .TrimSpace (_dbfg ))!=0{_cdeb =append (_cdeb ,_cfdf );
|
|
};_faab =_abbg [1]-_fbea ;_gcc =_fbea +_faab ;};_deae :=_dacf [_faab :];_cgba :=&matchedIndex {_fge :_deae ,_caee :_gcc ,_fee :_cgfga ._fee };if len (_d .TrimSpace (_deae ))!=0{_cdeb =append (_cdeb ,_cgba );};return _cdeb ;};func (_bbg *regexMatcher )match (_ffae string )([]*matchedIndex ,error ){_cfeba :=_bbg ._bcfg .Pattern ;
|
|
if _cfeba ==nil {return nil ,_bg .New ("\u006e\u006f\u0020\u0070at\u0074\u0065\u0072\u006e\u0020\u0063\u006f\u006d\u0070\u0069\u006c\u0065\u0064");};var (_geff =_cfeba .FindAllStringIndex (_ffae ,-1);_fgfb []*matchedIndex ;);for _ ,_fcba :=range _geff {_fgfb =append (_fgfb ,&matchedIndex {_caee :_fcba [0],_fee :_fcba [1],_fge :_ffae [_fcba [0]:_fcba [1]]});
|
|
};return _fgfb ,nil ;};func _ecge (_ed string ,_ab *_dae .PdfFont )[]byte {_dgc ,_aaff :=_ab .StringToCharcodeBytes (_ed );if _aaff !=0{_bd .Log .Debug ("\u0057\u0041\u0052\u004e\u003a\u0020\u0073\u006fm\u0065\u0020\u0072un\u0065\u0073\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0062\u0065\u0020\u0065\u006e\u0063\u006f\u0064\u0065d\u002e\u000a\u0009\u0025\u0073\u0020\u002d\u003e \u0025\u0076",_ed ,_dgc );
|
|
};return _dgc ;};func _caf (_bbae _cd .PdfObject ,_ffgf *_dae .PdfFont )(string ,error ){_dfa ,_dbe :=_cd .GetStringBytes (_bbae );if !_dbe {return "",_cd .ErrTypeError ;};_ceba :=_ffgf .BytesToCharcodes (_dfa );_bbf ,_dgeg ,_aad :=_ffgf .CharcodesToStrings (_ceba ,"");
|
|
if _aad > 0{_bd .Log .Debug ("\u0072\u0065nd\u0065\u0072\u0054e\u0078\u0074\u003a\u0020num\u0043ha\u0072\u0073\u003d\u0025\u0064\u0020\u006eum\u004d\u0069\u0073\u0073\u0065\u0073\u003d%\u0064",_dgeg ,_aad );};_bccd :=_d .Join (_bbf ,"");return _bccd ,nil ;
|
|
};type replacement struct{_cde string ;_bb float64 ;_bc int ;}; |