unipdf/annotator/annotator.go

191 lines
52 KiB
Go
Raw Normal View History

2020-08-27 21:45:09 +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/
// Package annotator provides an interface for creating annotations with appearance
// streams. It goes beyond the models package which includes definitions of basic annotation models, in that it
// can create the appearance streams which specify the exact appearance as needed by many pdf viewers for consistent
// appearance of the annotations.
// It also contains methods for generating appearance streams for fields via widget annotations.
2020-09-21 01:20:10 +00:00
package annotator ;import (_e "bytes";_fg "errors";_b "github.com/unidoc/unipdf/v3/common";_ee "github.com/unidoc/unipdf/v3/contentstream";_cg "github.com/unidoc/unipdf/v3/contentstream/draw";_be "github.com/unidoc/unipdf/v3/core";_d "github.com/unidoc/unipdf/v3/internal/textencoding";_bd "github.com/unidoc/unipdf/v3/model";_c "math";_a "strings";_ba "unicode";);
2020-09-14 09:32:45 +00:00
2020-09-21 01:20:10 +00:00
// SetStyle applies appearance `style` to `fa`.
func (_de *FieldAppearance )SetStyle (style AppearanceStyle ){_de ._bb =&style };
2020-08-27 21:45:09 +00:00
2020-09-21 01:20:10 +00:00
// WrapContentStream ensures that the entire content stream for a `page` is wrapped within q ... Q operands.
// Ensures that following operands that are added are not affected by additional operands that are added.
// Implements interface model.ContentStreamWrapper.
func (_fgae FieldAppearance )WrapContentStream (page *_bd .PdfPage )error {_ecg ,_bca :=page .GetAllContentStreams ();if _bca !=nil {return _bca ;};_gddd :=_ee .NewContentStreamParser (_ecg );_efe ,_bca :=_gddd .Parse ();if _bca !=nil {return _bca ;};_efe .WrapIfNeeded ();_ffe :=[]string {_efe .String ()};return page .SetContentStreams (_ffe ,_dea ());};
// CreateLineAnnotation creates a line annotation object that can be added to page PDF annotations.
func CreateLineAnnotation (lineDef LineAnnotationDef )(*_bd .PdfAnnotation ,error ){_cdeb :=_bd .NewPdfAnnotationLine ();_cdeb .L =_be .MakeArrayFromFloats ([]float64 {lineDef .X1 ,lineDef .Y1 ,lineDef .X2 ,lineDef .Y2 });_abbd :=_be .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle1 ==_cg .LineEndingStyleArrow {_abbd =_be .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_fcbg :=_be .MakeName ("\u004e\u006f\u006e\u0065");if lineDef .LineEndingStyle2 ==_cg .LineEndingStyleArrow {_fcbg =_be .MakeName ("C\u006c\u006f\u0073\u0065\u0064\u0041\u0072\u0072\u006f\u0077");};_cdeb .LE =_be .MakeArray (_abbd ,_fcbg );if lineDef .Opacity < 1.0{_cdeb .CA =_be .MakeFloat (lineDef .Opacity );};_cagd ,_ccd ,_dga :=lineDef .LineColor .R (),lineDef .LineColor .G (),lineDef .LineColor .B ();_cdeb .IC =_be .MakeArrayFromFloats ([]float64 {_cagd ,_ccd ,_dga });_cdeb .C =_be .MakeArrayFromFloats ([]float64 {_cagd ,_ccd ,_dga });_dab :=_bd .NewBorderStyle ();_dab .SetBorderWidth (lineDef .LineWidth );_cdeb .BS =_dab .ToPdfObject ();_agd ,_gefce ,_gcdd :=_faeb (lineDef );if _gcdd !=nil {return nil ,_gcdd ;};_cdeb .AP =_agd ;_cdeb .Rect =_be .MakeArrayFromFloats ([]float64 {_gefce .Llx ,_gefce .Lly ,_gefce .Urx ,_gefce .Ury });return _cdeb .PdfAnnotation ,nil ;};
// AppearanceFont represents a font used for generating the appearance of a
// field in the filling/flattening process.
type AppearanceFont struct{
// Name represents the name of the font which will be added to the
// AcroForm resources (DR).
Name string ;
// Font represents the actual font used for the field appearance.
Font *_bd .PdfFont ;
// Size represents the size of the font used for the field appearance.
// If size is 0, a default font size will be used.
// The default font size is calculated using the available annotation
// height and the AutoFontSizeFraction of the AppearanceStyle.
Size float64 ;};func _bbba (_cfg *_bd .PdfAcroForm ,_aea *_bd .PdfAnnotationWidget ,_egfc *_bd .PdfFieldChoice ,_cdb AppearanceStyle )(*_be .PdfObjectDictionary ,error ){_ada ,_cfgf :=_be .GetArray (_aea .Rect );if !_cfgf {return nil ,_fg .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_gcd ,_bbga :=_bd .NewPdfRectangle (*_ada );if _bbga !=nil {return nil ,_bbga ;};_fbbe ,_fbdg :=_gcd .Width (),_gcd .Height ();_b .Log .Debug ("\u0043\u0068\u006f\u0069\u0063\u0065\u002c\u0020\u0077\u0061\u0020\u0042S\u003a\u0020\u0025\u0076",_aea .BS );_fbga ,_bbga :=_ee .NewContentStreamParser (_eafc (_egfc .PdfField )).Parse ();if _bbga !=nil {return nil ,_bbga ;};_gga ,_ebga :=_be .GetDict (_aea .MK );if _ebga {_beefb ,_ :=_be .GetDict (_aea .BS );_fdf :=_cdb .applyAppearanceCharacteristics (_gga ,_beefb ,nil );if _fdf !=nil {return nil ,_fdf ;};};_eacd :=_be .MakeDict ();for _ ,_gcde :=range _egfc .Opt .Elements (){if _afb ,_bbf :=_be .GetArray (_gcde );_bbf &&_afb .Len ()==2{_gcde =_afb .Get (1);};var _agbc string ;if _bdfa ,_aga :=_be .GetString (_gcde );_aga {_agbc =_bdfa .Decoded ();}else if _aee ,_bef :=_be .GetName (_gcde );_bef {_agbc =_aee .String ();}else {_b .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a \u004f\u0070\u0074\u0020\u006e\u006f\u0074\u0020\u0061\u0020\u006e\u0061\u006de\u002f\u0073\u0074\u0072\u0069\u006e\u0067 \u002d\u0020\u0025\u0054",_gcde );return nil ,_fg .New ("\u006e\u006f\u0074\u0020\u0061\u0020\u006e\u0061\u006d\u0065\u002f\u0073t\u0072\u0069\u006e\u0067");};if len (_agbc )> 0{_aba ,_agbb :=_fge (_egfc .PdfField ,_fbbe ,_fbdg ,_agbc ,_cdb ,_fbga ,_cfg .DR ,_gga );if _agbb !=nil {return nil ,_agbb ;};_eacd .Set (*_be .MakeName (_agbc ),_aba .ToPdfObject ());};};_cgg :=_be .MakeDict ();_cgg .Set ("\u004e",_eacd );return _cgg ,nil ;};type quadding int ;
// RectangleAnnotationDef is a rectangle defined with a specified Width and Height and a lower left corner at (X,Y).
// The rectangle can optionally have a border and a filling color.
// The Width/Height includes the border (if any specified).
type RectangleAnnotationDef struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_bd .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_bd .PdfColorDeviceRGB ;Opacity float64 ;};
2020-09-07 00:23:12 +00:00
// ComboboxFieldOptions defines optional parameters for a combobox form field.
type ComboboxFieldOptions struct{
// Choices is the list of string values that can be selected.
2020-09-21 01:20:10 +00:00
Choices []string ;};const (_gbf quadding =0;_eb quadding =1;_bab quadding =2;);
// SignatureLine represents a line of information in the signature field appearance.
type SignatureLine struct{Desc string ;Text string ;};
// AppearanceStyle defines style parameters for appearance stream generation.
type AppearanceStyle struct{
// How much of Rect height to fill when autosizing text.
AutoFontSizeFraction float64 ;
// CheckmarkRune is a rune used for check mark in checkboxes (for ZapfDingbats font).
CheckmarkRune rune ;BorderSize float64 ;BorderColor _bd .PdfColor ;FillColor _bd .PdfColor ;
// Multiplier for lineheight for multi line text.
MultilineLineHeight float64 ;MultilineVAlignMiddle bool ;
// Visual guide checking alignment of field contents (debugging).
DrawAlignmentReticle bool ;
// Allow field MK appearance characteristics to override style settings.
AllowMK bool ;
// Fonts holds appearance styles for fonts.
Fonts *AppearanceFontStyle ;};func _fbe (_bgb []*SignatureLine ,_cfe *SignatureFieldOpts )(*_be .PdfObjectDictionary ,error ){if _cfe ==nil {_cfe =NewSignatureFieldOpts ();};var _baec error ;var _egc *_be .PdfObjectName ;_dbde :=_cfe .Font ;if _dbde !=nil {_abee ,_ :=_dbde .GetFontDescriptor ();if _abee !=nil {if _abbb ,_gdcb :=_abee .FontName .(*_be .PdfObjectName );_gdcb {_egc =_abbb ;};};if _egc ==nil {_egc =_be .MakeName ("\u0046\u006f\u006et\u0031");};}else {if _dbde ,_baec =_bd .NewStandard14Font ("\u0048e\u006c\u0076\u0065\u0074\u0069\u0063a");_baec !=nil {return nil ,_baec ;};_egc =_be .MakeName ("\u0048\u0065\u006c\u0076");};_bfea :=_cfe .FontSize ;if _bfea <=0{_bfea =10;};if _cfe .LineHeight <=0{_cfe .LineHeight =1;};_fgde :=_cfe .LineHeight *_bfea ;_afbd ,_gfeg :=_dbde .GetRuneMetrics (' ');if !_gfeg {return nil ,_fg .New ("\u0074\u0068e \u0066\u006f\u006et\u0020\u0064\u006f\u0065s n\u006ft \u0068\u0061\u0076\u0065\u0020\u0061\u0020sp\u0061\u0063\u0065\u0020\u0067\u006c\u0079p\u0068");};_cgagg :=_afbd .Wx ;var _bfae float64 ;var _edec []string ;for _ ,_bgbe :=range _bgb {if _bgbe .Text ==""{continue ;};_cebe :=_bgbe .Text ;if _bgbe .Desc !=""{_cebe =_bgbe .Desc +"\u003a\u0020"+_cebe ;};_edec =append (_edec ,_cebe );var _fdga float64 ;for _ ,_ggc :=range _cebe {_edab ,_gcb :=_dbde .GetRuneMetrics (_ggc );if !_gcb {continue ;};_fdga +=_edab .Wx ;};if _fdga > _bfae {_bfae =_fdga ;};};_bfae =_bfae *_bfea /1000.0;_dgdg :=float64 (len (_edec ))*_fgde ;_fgc :=_cfe .Rect ;if _fgc ==nil {_fgc =[]float64 {0,0,_bfae ,_dgdg };_cfe .Rect =_fgc ;};_fba :=_fgc [2]-_fgc [0];_cfgg :=_fgc [3]-_fgc [1];var _fafa float64 ;if _cfe .AutoSize {if _bfae > _fba ||_dgdg > _cfgg {_gefc :=_c .Min (_fba /_bfae ,_cfgg /_dgdg );_bfea *=_gefc ;};_fgde =_cfe .LineHeight *_bfea ;_fafa +=(_cfgg -float64 (len (_edec ))*_fgde )/2;};_ggg :=_ee .NewContentCreator ();if _cfe .BorderSize <=0{_cfe .BorderSize =0;_cfe .BorderColor =_bd .NewPdfColorDeviceGray (1);};if _cfe .BorderColor ==nil {_cfe .BorderColor =_bd .NewPdfColorDeviceGray (1);};if _cfe .FillColor ==nil {_cfe .FillColor =_bd .NewPdfColorDeviceGray (1);};_ggg .Add_q ().SetNonStrokingColor (_cfe .FillColor ).SetStrokingColor (_cfe .BorderColor ).Add_w (_cfe .BorderSize ).Add_re (_fgc [0],_fgc [1],_fba ,_cfgg ).Add_B ().Add_Q ();_ggg .Add_q ();_ggg .Translate (_fgc [0],_fgc [3]-_fgde -_fafa );_ggg .Add_BT ();_dce :=_dbde .Encoder ();for _ ,_dcf :=range _edec {var _eca []byte ;for _ ,_bbc :=range _dcf {if _ba .IsSpace (_bbc ){if len (_eca )> 0{_ggg .SetNonStrokingColor (_cfe .TextColor ).Add_Tf (*_egc ,_bfea ).Add_TL (_fgde ).Add_TJ ([]_be .PdfObject {_be .MakeStringFromBytes (_eca )}...);_eca =nil ;};_ggg .Add_Tf (*_egc ,_bfea ).Add_TL (_fgde ).Add_TJ ([]_be .PdfObject {_be .MakeFloat (-_cgagg )}...);}else {_eca =append (_eca ,_dce .Encode (string (_bbc ))...);};};if len (_eca )> 0{_ggg .SetNonStrokingColor (_cfe .TextColor ).Add_Tf (*_egc ,_bfea ).Add_TL (_fgde ).Add_TJ ([]_be .PdfObject {_be .MakeStringFromBytes (_eca )}...);};_ggg .Add_Td (0,-_fgde );};_ggg .Add_ET ();_ggg .Add_Q ();_dge :=_bd .NewPdfPageResources ();_dge .SetFontByName (*_egc ,_dbde .ToPdfObject ());_bebe :=_bd .NewXObjectForm ();_bebe .Resources =_dge ;_bebe .BBox =_be .MakeArrayFromFloats (_fgc );_bebe .SetContentStream (_ggg .Bytes (),_dea ());_fae :=_be .MakeDict ();_fae .Set ("\u004e",_bebe .ToPdfObject ());return _fae ,nil ;};
// NewSignatureFieldOpts returns a new initialized instance of options
// used to generate a signature appearance.
func NewSignatureFieldOpts ()*SignatureFieldOpts {return &SignatureFieldOpts {Font :_bd .DefaultFont (),FontSize :10,LineHeight :1,AutoSize :true ,TextColor :_bd .NewPdfColorDeviceGray (0),BorderColor :_bd .NewPdfColorDeviceGray (0),FillColor :_bd .NewPdfColorDeviceGray (1)};};func (_fafc *AppearanceStyle )applyRotation (_fefb *_be .PdfObjectDictionary ,_afd ,_efb float64 ,_aca *_ee .ContentCreator )(float64 ,float64 ){if !_fafc .AllowMK {return _afd ,_efb ;};if _fefb ==nil {return _afd ,_efb ;};_fbdc ,_ :=_be .GetNumberAsFloat (_fefb .Get ("\u0052"));if _fbdc ==0{return _afd ,_efb ;};_cgad :=-_fbdc ;_adcd :=_cg .Path {Points :[]_cg .Point {_cg .NewPoint (0,0).Rotate (_cgad ),_cg .NewPoint (_afd ,0).Rotate (_cgad ),_cg .NewPoint (0,_efb ).Rotate (_cgad ),_cg .NewPoint (_afd ,_efb ).Rotate (_cgad )}}.GetBoundingBox ();_aca .RotateDeg (_fbdc );_aca .Translate (_adcd .X ,_adcd .Y );return _adcd .Width ,_adcd .Height ;};
// CreateCircleAnnotation creates a circle/ellipse annotation object with appearance stream that can be added to
// page PDF annotations.
func CreateCircleAnnotation (circDef CircleAnnotationDef )(*_bd .PdfAnnotation ,error ){_cc :=_bd .NewPdfAnnotationCircle ();if circDef .BorderEnabled {_cd ,_cf ,_ae :=circDef .BorderColor .R (),circDef .BorderColor .G (),circDef .BorderColor .B ();_cc .C =_be .MakeArrayFromFloats ([]float64 {_cd ,_cf ,_ae });_fe :=_bd .NewBorderStyle ();_fe .SetBorderWidth (circDef .BorderWidth );_cc .BS =_fe .ToPdfObject ();};if circDef .FillEnabled {_df ,_ff ,_af :=circDef .FillColor .R (),circDef .FillColor .G (),circDef .FillColor .B ();_cc .IC =_be .MakeArrayFromFloats ([]float64 {_df ,_ff ,_af });}else {_cc .IC =_be .MakeArrayFromIntegers ([]int {});};if circDef .Opacity < 1.0{_cc .CA =_be .MakeFloat (circDef .Opacity );};_ca ,_g ,_bed :=_feg (circDef );if _bed !=nil {return nil ,_bed ;};_cc .AP =_ca ;_cc .Rect =_be .MakeArrayFromFloats ([]float64 {_g .Llx ,_g .Lly ,_g .Urx ,_g .Ury });return _cc .PdfAnnotation ,nil ;};func (_dbe *AppearanceStyle )processDA (_abb *_bd .PdfField ,_dcc *_ee .ContentStreamOperations ,_aag ,_gab *_bd .PdfPageResources ,_cfb *_ee .ContentCreator )(*AppearanceFont ,bool ,error ){var _ecde *AppearanceFont ;var _eef bool ;if _dbe .Fonts !=nil {if _dbe .Fonts .Fallback !=nil {_ecde =_dbe .Fonts .Fallback ;};if _fed :=_dbe .Fonts .FieldFallbacks ;_fed !=nil {if _ccca ,_gdb :=_fed [_abb .PartialName ()];_gdb {_ecde =_ccca ;}else if _gde ,_fdfg :=_abb .FullName ();_fdfg ==nil {if _dagg ,_eeaf :=_fed [_gde ];_eeaf {_ecde =_dagg ;};};};_eef =_dbe .Fonts .ForceReplace ;};var _abc string ;var _cccaf float64 ;var _edce bool ;if _dcc !=nil {for _ ,_cecg :=range *_dcc {if _cecg .Operand =="\u0054\u0066"&&len (_cecg .Params )==2{if _bda ,_fdg :=_be .GetNameVal (_cecg .Params [0]);_fdg {_abc =_bda ;};if _abea ,_ebe :=_be .GetNumberAsFloat (_cecg .Params [1]);_ebe ==nil {_cccaf =_abea ;};_edce =true ;continue ;};_cfb .AddOperand (*_cecg );};};var _gaa *AppearanceFont ;var _eba _be .PdfObject ;if _eef &&_ecde !=nil {_gaa =_ecde ;}else {if _aag !=nil &&_abc !=""{if _ddg ,_gbaf :=_aag .GetFontByName (*_be .MakeName (_abc ));_gbaf {if _bffe ,_babg :=_bd .NewPdfFontFromPdfObject (_ddg );_babg ==nil {_eba =_ddg ;_gaa =&AppearanceFont {Name :_abc ,Font :_bffe ,Size :_cccaf };}else {_b .Log .Debug ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0063\u006f\u0075\u006c\u0064\u0020\u006e\u006f\u0074\u0020\u006c\u006fa\u0064\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0061\u006e\u0063\u0065\u0020\u0066\u006f\u006e\u0074\u003a\u0020\u0025\u0076",_babg );};};};if _gaa ==nil &&_ecde !=nil {_gaa =_ecde ;};if _gaa ==nil {_fegc ,_agf :=_bd .NewStandard14Font ("\u0048e\u006c\u0076\u0065\u0074\u0069\u0063a");if _agf !=nil {return nil ,false ,_agf ;};_gaa =&AppearanceFont {Name :"\u0048\u0065\u006c\u0076",Font :_fegc ,Size :_cccaf };};};_eeca :=*_be .MakeName (_gaa .Name );if _eba ==nil {_eba =_gaa .Font .ToPdfObject ();};if _aag !=nil &&!_aag .HasFontByName (_eeca ){_aag .SetFontByName (_eeca ,_eba );};if _gab !=nil &&!_gab .HasFontByName (_eeca ){_gab .SetFontByName (_eeca ,_eba );};return _gaa ,_edce ,nil ;};
// NewSignatureLine returns a new signature line displayed as a part of the
// signature field appearance.
func NewSignatureLine (desc ,text string )*SignatureLine {return &SignatureLine {Desc :desc ,Text :text };};
// CreateRectangleAnnotation creates a rectangle annotation object that can be added to page PDF annotations.
func CreateRectangleAnnotation (rectDef RectangleAnnotationDef )(*_bd .PdfAnnotation ,error ){_ecf :=_bd .NewPdfAnnotationSquare ();if rectDef .BorderEnabled {_aabg ,_edg ,_efd :=rectDef .BorderColor .R (),rectDef .BorderColor .G (),rectDef .BorderColor .B ();_ecf .C =_be .MakeArrayFromFloats ([]float64 {_aabg ,_edg ,_efd });_afe :=_bd .NewBorderStyle ();_afe .SetBorderWidth (rectDef .BorderWidth );_ecf .BS =_afe .ToPdfObject ();};if rectDef .FillEnabled {_gfb ,_cdbd ,_ebb :=rectDef .FillColor .R (),rectDef .FillColor .G (),rectDef .FillColor .B ();_ecf .IC =_be .MakeArrayFromFloats ([]float64 {_gfb ,_cdbd ,_ebb });}else {_ecf .IC =_be .MakeArrayFromIntegers ([]int {});};if rectDef .Opacity < 1.0{_ecf .CA =_be .MakeFloat (rectDef .Opacity );};_bbfg ,_ceg ,_ebce :=_cbfcc (rectDef );if _ebce !=nil {return nil ,_ebce ;};_ecf .AP =_bbfg ;_ecf .Rect =_be .MakeArrayFromFloats ([]float64 {_ceg .Llx ,_ceg .Lly ,_ceg .Urx ,_ceg .Ury });return _ecf .PdfAnnotation ,nil ;};func _edc (_cgfb *_ee .ContentCreator ,_aaag AppearanceStyle ,_eda ,_gdd float64 ){_cgfb .Add_q ().Add_re (0,0,_eda ,_gdd ).Add_w (_aaag .BorderSize ).SetStrokingColor (_aaag .BorderColor ).SetNonStrokingColor (_aaag .FillColor ).Add_B ().Add_Q ();};func _faeb (_afbf LineAnnotationDef )(*_be .PdfObjectDictionary ,*_bd .PdfRectangle ,error ){_gdfe :=_bd .NewXObjectForm ();_gdfe .Resources =_bd .NewPdfPageResources ();_bgbc :="";if _afbf .Opacity < 1.0{_cdbe :=_be .MakeDict ();_cdbe .Set ("\u0063\u0061",_be .MakeFloat (_afbf .Opacity ));_acfb :=_gdfe .Resources .AddExtGState ("\u0067\u0073\u0031",_cdbe );if _acfb !=nil {_b .Log .Debug ("U\u006e\u0061\u0062\u006c\u0065\u0020t\u006f\u0020\u0061\u0064\u0064\u0020\u0065\u0078\u0074g\u0073\u0074\u0061t\u0065 \u0067\u0073\u0031");return nil ,nil ,_acfb ;};_bgbc ="\u0067\u0073\u0031";};_eafa ,_ggda ,_egfg ,_beg :=_bagb (_afbf ,_bgbc );if _beg !=nil {return nil ,nil ,_beg ;};_beg =_gdfe .SetContentStream (_eafa ,nil );if _beg !=nil {return nil ,nil ,_beg ;};_gdfe .BBox =_ggda .ToPdfObject ();_fbfg :=_be .MakeDict ();_fbfg .Set ("\u004e",_gdfe .ToPdfObject ());return _fbfg ,_egfg ,nil ;};
2020-09-07 00:23:12 +00:00
2020-09-14 09:32:45 +00:00
// AppearanceFontStyle defines font style characteristics for form fields,
// used in the filling/flattening process.
type AppearanceFontStyle struct{
2020-09-07 00:23:12 +00:00
2020-09-14 09:32:45 +00:00
// Fallback represents a global font fallback, used for fields which do
// not specify a font in their default appearance (DA). The fallback is
// also used if there is a font specified in the DA, but it is not
// found in the AcroForm resources (DR).
Fallback *AppearanceFont ;
2020-08-31 21:12:07 +00:00
2020-09-14 09:32:45 +00:00
// FieldFallbacks defines font fallbacks for specific fields. The map keys
// represent the names of the fields (which can be specified by their
// partial or full names). Specific field fallback fonts take precedence
// over the global font fallback.
FieldFallbacks map[string ]*AppearanceFont ;
2020-08-31 21:12:07 +00:00
2020-09-14 09:32:45 +00:00
// ForceReplace forces the replacement of fonts in the filling/flattening
// process, even if the default appearance (DA) specify a valid font.
// If no fallback font is provided, setting this field has no effect.
2020-09-21 01:20:10 +00:00
ForceReplace bool ;};func _gbd (_cb *_bd .PdfAnnotationWidget ,_egf *_bd .PdfFieldText ,_cgag *_bd .PdfPageResources ,_fcgf AppearanceStyle )(*_be .PdfObjectDictionary ,error ){_gee :=_bd .NewPdfPageResources ();_bcbc ,_bbb :=_be .GetArray (_cb .Rect );if !_bbb {return nil ,_fg .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_ecdd ,_baf :=_bd .NewPdfRectangle (*_bcbc );if _baf !=nil {return nil ,_baf ;};_cgae ,_cfc :=_ecdd .Width (),_ecdd .Height ();_agb ,_ceb :=_cgae ,_cfc ;_cccg ,_adf :=_be .GetDict (_cb .MK );if _adf {_gbb ,_ :=_be .GetDict (_cb .BS );_cgf :=_fcgf .applyAppearanceCharacteristics (_cccg ,_gbb ,nil );if _cgf !=nil {return nil ,_cgf ;};};_aaa ,_adf :=_be .GetIntVal (_egf .MaxLen );if !_adf {return nil ,_fg .New ("\u006d\u0061\u0078\u006c\u0065\u006e\u0020\u006e\u006ft\u0020\u0073\u0065\u0074");};if _aaa <=0{return nil ,_fg .New ("\u006d\u0061\u0078\u004c\u0065\u006e\u0020\u0069\u006ev\u0061\u006c\u0069\u0064");};_fbg :=float64 (_cgae )/float64 (_aaa );_dc ,_baf :=_ee .NewContentStreamParser (_eafc (_egf .PdfField )).Parse ();if _baf !=nil {return nil ,_baf ;};_dba :=_ee .NewContentCreator ();if _fcgf .BorderSize > 0{_edc (_dba ,_fcgf ,_cgae ,_cfc );};if _fcgf .DrawAlignmentReticle {_bdb :=_fcgf ;_bdb .BorderSize =0.2;_fefd (_dba ,_bdb ,_cgae ,_cfc );};_dba .Add_BMC ("\u0054\u0078");_dba .Add_q ();_cgae ,_cfc =_fcgf .applyRotation (_cccg ,_cgae ,_cfc ,_dba );_dba .Add_BT ();_bag ,_afgb ,_baf :=_fcgf .processDA (_egf .PdfField ,_dc ,_cgag ,_gee ,_dba );if _baf !=nil {return nil ,_baf ;};_dfe :=_bag .Font ;_geb :=_be .MakeName (_bag .Name );_cgagd :=_bag .Size ;_beab :=_cgagd ==0;if _beab &&_afgb {_cgagd =_cfc *_fcgf .AutoFontSizeFraction ;};_egfe :=_dfe .Encoder ();if _egfe ==nil {_b .Log .Debug ("\u0057\u0041RN\u003a\u0020\u0066\u006f\u006e\u0074\u0020\u0065\u006e\u0063\u006f\u0064\u0065\u0072\u0020\u0069\u0073\u0020\u006e\u0069l\u002e\u0020\u0041\u0073s\u0075\u006d\u0069\u006eg \u0069\u0064e\u006et\u0069\u0074\u0079\u0020\u0065\u006ec\u006f\u0064\u0065r\u002e\u0020O\u0075\u0074\u0070\u0075\u0074\u0020\u006d\u0061\u0079\u0020\u0062\u0065\u0020\u0069n\u0063\u006f\u0072\u0072\u0065\u0063\u0074\u002e");_egfe =_d .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};var _cad string ;if _ced ,_ccff :=_be .GetString (_egf .V );_ccff {_cad =_ced .Decoded ();};_dba .Add_Tf (*_geb ,_cgagd );var _eab float64 ;for _ ,_adb :=range _cad {_ega ,_bec :=_dfe .GetRuneMetrics (_adb );if !_bec {_b .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a \u0052\u0075\u006e\u0065\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064\u0020\u0069\u006e\u0020\u0066\u006fn\u0074\u003a\u0020\u0025\u0076\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069n\u0067 \u006f\u0076\u0065\u0072",_adb );continue ;};_fbfe :=_ega .Wy ;if int (_fbfe )<=0{_fbfe =_ega .Wx ;};if _fbfe > _eab {_eab =_fbfe ;};};if int (_eab )==0{_b .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a\u0020\u0055\u006e\u0061\u0062\u006c\u0065\u0020\u0074o\u0020\u0064\u0065\u0074\u0065\u0072\u006d\u0069\u006e\u0065\u0020\u006d\u0061x\u0020\u0067\u006c\u0079\u0070\u0068\u0020\u0073\u0069\u007a\u0065\u0020- \u0075\u0073\u0069\u006e\u0067\u0020\u0031\u0030\u0030\u0030");_eab =1000;};_gdf ,_baf :=_dfe .GetFontDescriptor ();if _baf !=nil {_b .Log .Debug ("\u0045\u0072ro\u0072\u003a\u0020U\u006e\u0061\u0062\u006ce t\u006f g\u0065\u0074\u0020\u0066\u006f\u006e\u0074 d\u0065\u0073\u0063\u0072\u0069\u0070\u0074o\u0072");};var _acf float64 ;if _gdf !=nil {_acf ,_baf =_gdf .GetCapHeight ();if _baf !=nil {_b .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u003a\u0020\u0055\u006e\u0061\u0062\u006c\u0065 \u0074\u006f\u0020\u0067\u0065\u0074 \u0066\u006f\u006e\u0074\u0020\u0043\u0061\u0070\u0048\u0065\u0069\u0067\u0068t\u003a\u0020\u0025\u0076",_baf );};};if int (_acf )<=0{_b .Log .Debug ("W\u0041\u0052\u004e\u003a\u0020\u0043\u0061\u0070\u0048e\u0069\u0067\u0068\u0074\u0020\u006e\u006ft \u0061\u0076\u0061\u0069l\u0061\u0062\u006c\u0065\u0020\u002d\u0020\u0073\u0065tt\u0069\u006eg\u0020\u0074\u006f\u0020\u0031\u0030\
2020-09-07 00:23:12 +00:00
2020-09-21 01:20:10 +00:00
// NewCheckboxField generates a new checkbox field with partial name `name` at location `rect`
// on specified `page` and with field specific options `opt`.
func NewCheckboxField (page *_bd .PdfPage ,name string ,rect []float64 ,opt CheckboxFieldOptions )(*_bd .PdfFieldButton ,error ){if page ==nil {return nil ,_fg .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_fg .New ("\u0072\u0065\u0071\u0075\u0069\u0072\u0065\u0064\u0020\u0061\u0074\u0074\u0072\u0069\u0062u\u0074e\u0020\u006e\u006f\u0074\u0020\u0073\u0070\u0065\u0063\u0069\u0066\u0069\u0065\u0064");};if len (rect )!=4{return nil ,_fg .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_fff ,_ffda :=_bd .NewStandard14Font (_bd .ZapfDingbatsName );if _ffda !=nil {return nil ,_ffda ;};_adde :=_bd .NewPdfField ();_fca :=&_bd .PdfFieldButton {};_adde .SetContext (_fca );_fca .PdfField =_adde ;_fca .T =_be .MakeString (name );_fca .SetType (_bd .ButtonTypeCheckbox );_eaaa :="\u004f\u0066\u0066";if opt .Checked {_eaaa ="\u0059\u0065\u0073";};_fca .V =_be .MakeName (_eaaa );_gagg :=_bd .NewPdfAnnotationWidget ();_gagg .Rect =_be .MakeArrayFromFloats (rect );_gagg .P =page .ToPdfObject ();_gagg .F =_be .MakeInteger (4);_gagg .Parent =_fca .ToPdfObject ();_aeee :=rect [2]-rect [0];_aeeb :=rect [3]-rect [1];var _aeaa _e .Buffer ;_aeaa .WriteString ("\u0071\u000a");_aeaa .WriteString ("\u0030 \u0030\u0020\u0031\u0020\u0072\u0067\n");_aeaa .WriteString ("\u0042\u0054\u000a");_aeaa .WriteString ("\u002f\u005a\u0061D\u0062\u0020\u0031\u0032\u0020\u0054\u0066\u000a");_aeaa .WriteString ("\u0045\u0054\u000a");_aeaa .WriteString ("\u0051\u000a");_fgdf :=_ee .NewContentCreator ();_fgdf .Add_q ();_fgdf .Add_rg (0,0,1);_fgdf .Add_BT ();_fgdf .Add_Tf (*_be .MakeName ("\u005a\u0061\u0044\u0062"),12);_fgdf .Add_Td (0,0);_fgdf .Add_ET ();_fgdf .Add_Q ();_cbd :=_bd .NewXObjectForm ();_cbd .SetContentStream (_fgdf .Bytes (),_be .NewRawEncoder ());_cbd .BBox =_be .MakeArrayFromFloats ([]float64 {0,0,_aeee ,_aeeb });_cbd .Resources =_bd .NewPdfPageResources ();_cbd .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_fff .ToPdfObject ());_fgdf =_ee .NewContentCreator ();_fgdf .Add_q ();_fgdf .Add_re (0,0,_aeee ,_aeeb );_fgdf .Add_W ().Add_n ();_fgdf .Add_rg (0,0,1);_fgdf .Translate (0,3.0);_fgdf .Add_BT ();_fgdf .Add_Tf (*_be .MakeName ("\u005a\u0061\u0044\u0062"),12);_fgdf .Add_Td (0,0);_fgdf .Add_Tj (*_be .MakeString ("\u0034"));_fgdf .Add_ET ();_fgdf .Add_Q ();_cee :=_bd .NewXObjectForm ();_cee .SetContentStream (_fgdf .Bytes (),_be .NewRawEncoder ());_cee .BBox =_be .MakeArrayFromFloats ([]float64 {0,0,_aeee ,_aeeb });_cee .Resources =_bd .NewPdfPageResources ();_cee .Resources .SetFontByName ("\u005a\u0061\u0044\u0062",_fff .ToPdfObject ());_dagd :=_be .MakeDict ();_dagd .Set ("\u004f\u0066\u0066",_cbd .ToPdfObject ());_dagd .Set ("\u0059\u0065\u0073",_cee .ToPdfObject ());_egg :=_be .MakeDict ();_egg .Set ("\u004e",_dagd );_gagg .AP =_egg ;_gagg .AS =_be .MakeName (_eaaa );_fca .Annotations =append (_fca .Annotations ,_gagg );return _fca ,nil ;};
2020-09-14 09:32:45 +00:00
// LineAnnotationDef defines a line between point 1 (X1,Y1) and point 2 (X2,Y2). The line ending styles can be none
// (regular line), or arrows at either end. The line also has a specified width, color and opacity.
2020-09-21 01:20:10 +00:00
type LineAnnotationDef struct{X1 float64 ;Y1 float64 ;X2 float64 ;Y2 float64 ;LineColor *_bd .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineEndingStyle1 _cg .LineEndingStyle ;LineEndingStyle2 _cg .LineEndingStyle ;};func _eafc (_eec *_bd .PdfField )string {if _eec ==nil {return "";};_acg ,_efa :=_eec .GetContext ().(*_bd .PdfFieldText );if !_efa {return _eafc (_eec .Parent );};if _acg .DA !=nil {return _acg .DA .Str ();};return _eafc (_acg .Parent );};
2020-09-14 09:32:45 +00:00
2020-09-21 01:20:10 +00:00
// CheckboxFieldOptions defines optional parameters for a checkbox field a form.
type CheckboxFieldOptions struct{Checked bool ;};func (_fgd *AppearanceStyle )applyAppearanceCharacteristics (_beb *_be .PdfObjectDictionary ,_bgcg *_be .PdfObjectDictionary ,_cgc *_bd .PdfFont )error {if !_fgd .AllowMK {return nil ;};if _bgcg !=nil {if _gegb ,_fcgc :=_be .GetNumberAsFloat (_bgcg .Get ("\u0057"));_fcgc ==nil {_fgd .BorderSize =_gegb ;};};if CA ,_cef :=_be .GetString (_beb .Get ("\u0043\u0041"));_cef &&_cgc !=nil {_cagg :=CA .Bytes ();if len (_cagg )!=0{_eace :=[]rune (_cgc .Encoder ().Decode (_cagg ));if len (_eace )==1{_fgd .CheckmarkRune =_eace [0];};};};if BC ,_bafd :=_be .GetArray (_beb .Get ("\u0042\u0043"));_bafd {_eacdg ,_bfada :=BC .ToFloat64Array ();if _bfada !=nil {return _bfada ;};switch len (_eacdg ){case 1:_fgd .BorderColor =_bd .NewPdfColorDeviceGray (_eacdg [0]);case 3:_fgd .BorderColor =_bd .NewPdfColorDeviceRGB (_eacdg [0],_eacdg [1],_eacdg [2]);case 4:_fgd .BorderColor =_bd .NewPdfColorDeviceCMYK (_eacdg [0],_eacdg [1],_eacdg [2],_eacdg [3]);default:_b .Log .Debug ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0042\u0043\u0020\u002d\u0020\u0049\u006e\u0076\u0061\u006c\u0069\u0064 \u006e\u0075\u006d\u0062\u0065\u0072\u0020\u006f\u0066\u0020\u0063\u006f\u006c\u006f\u0072\u0020\u0063\u006f\u006d\u0070\u006f\u006e\u0065\u006e\u0074\u0073\u0020\u0028\u0025\u0064)",len (_eacdg ));};};if BG ,_abac :=_be .GetArray (_beb .Get ("\u0042\u0047"));_abac {_fdc ,_cbaf :=BG .ToFloat64Array ();if _cbaf !=nil {return _cbaf ;};switch len (_fdc ){case 1:_fgd .FillColor =_bd .NewPdfColorDeviceGray (_fdc [0]);case 3:_fgd .FillColor =_bd .NewPdfColorDeviceRGB (_fdc [0],_fdc [1],_fdc [2]);case 4:_fgd .FillColor =_bd .NewPdfColorDeviceCMYK (_fdc [0],_fdc [1],_fdc [2],_fdc [3]);default:_b .Log .Debug ("\u0045\u0052\u0052\u004f\u0052:\u0020\u0042\u0047\u0020\u002d\u0020\u0049\u006e\u0076\u0061\u006c\u0069\u0064 \u006e\u0075\u006d\u0062\u0065\u0072\u0020\u006f\u0066\u0020\u0063\u006f\u006c\u006f\u0072\u0020\u0063\u006f\u006d\u0070\u006f\u006e\u0065\u006e\u0074\u0073\u0020\u0028\u0025\u0064)",len (_fdc ));};};return nil ;};func _feg (_fa CircleAnnotationDef )(*_be .PdfObjectDictionary ,*_bd .PdfRectangle ,error ){_ccb :=_bd .NewXObjectForm ();_ccb .Resources =_bd .NewPdfPageResources ();_gb :="";if _fa .Opacity < 1.0{_ea :=_be .MakeDict ();_ea .Set ("\u0063\u0061",_be .MakeFloat (_fa .Opacity ));_ea .Set ("\u0043\u0041",_be .MakeFloat (_fa .Opacity ));_cgd :=_ccb .Resources .AddExtGState ("\u0067\u0073\u0031",_ea );if _cgd !=nil {_b .Log .Debug ("U\u006e\u0061\u0062\u006c\u0065\u0020t\u006f\u0020\u0061\u0064\u0064\u0020\u0065\u0078\u0074g\u0073\u0074\u0061t\u0065 \u0067\u0073\u0031");return nil ,nil ,_cgd ;};_gb ="\u0067\u0073\u0031";};_gc ,_cff ,_bc ,_eaf :=_ag (_fa ,_gb );if _eaf !=nil {return nil ,nil ,_eaf ;};_eaf =_ccb .SetContentStream (_gc ,nil );if _eaf !=nil {return nil ,nil ,_eaf ;};_ccb .BBox =_cff .ToPdfObject ();_fb :=_be .MakeDict ();_fb .Set ("\u004e",_ccb .ToPdfObject ());return _fb ,_bc ,nil ;};func _bdda (_fag RectangleAnnotationDef ,_aegd string )([]byte ,*_bd .PdfRectangle ,*_bd .PdfRectangle ,error ){_ecad :=_cg .Rectangle {X :0,Y :0,Width :_fag .Width ,Height :_fag .Height ,FillEnabled :_fag .FillEnabled ,FillColor :_fag .FillColor ,BorderEnabled :_fag .BorderEnabled ,BorderWidth :2*_fag .BorderWidth ,BorderColor :_fag .BorderColor ,Opacity :_fag .Opacity };_fda ,_bgbf ,_cefb :=_ecad .Draw (_aegd );if _cefb !=nil {return nil ,nil ,nil ,_cefb ;};_bfbf :=&_bd .PdfRectangle {};_bfbf .Llx =_fag .X +_bgbf .Llx ;_bfbf .Lly =_fag .Y +_bgbf .Lly ;_bfbf .Urx =_fag .X +_bgbf .Urx ;_bfbf .Ury =_fag .Y +_bgbf .Ury ;return _fda ,_bgbf ,_bfbf ,nil ;};func _bagb (_bgag LineAnnotationDef ,_egbb string )([]byte ,*_bd .PdfRectangle ,*_bd .PdfRectangle ,error ){_cbfe :=_cg .Line {X1 :0,Y1 :0,X2 :_bgag .X2 -_bgag .X1 ,Y2 :_bgag .Y2 -_bgag .Y1 ,LineColor :_bgag .LineColor ,Opacity :_bgag .Opacity ,LineWidth :_bgag .LineWidth ,LineEndingStyle1 :_bgag .LineEndingStyle1 ,LineEndingStyle2 :_bgag .LineEndingStyle2 };_cabe ,_fbfd ,_fbee :=_cbfe .Draw (_egbb );if _fbee !=nil {return nil ,nil ,nil ,_fbee ;};_fcbgc :=&_bd .PdfR
2020-08-27 21:45:09 +00:00
// SignatureFieldOpts represents a set of options used to configure
// an appearance widget dictionary.
type SignatureFieldOpts struct{
// Rect represents the area the signature annotation is displayed on.
Rect []float64 ;
// AutoSize specifies if the content of the appearance should be
// scaled to fit in the annotation rectangle.
AutoSize bool ;
// Font specifies the font of the text content.
2020-09-21 01:20:10 +00:00
Font *_bd .PdfFont ;
2020-08-27 21:45:09 +00:00
// FontSize specifies the size of the text content.
FontSize float64 ;
// LineHeight specifies the height of a line of text in the appearance annotation.
LineHeight float64 ;
// TextColor represents the color of the text content displayed.
2020-09-21 01:20:10 +00:00
TextColor _bd .PdfColor ;
2020-08-27 21:45:09 +00:00
// FillColor represents the background color of the appearance annotation area.
2020-09-21 01:20:10 +00:00
FillColor _bd .PdfColor ;
2020-08-27 21:45:09 +00:00
// BorderSize represents border size of the appearance annotation area.
BorderSize float64 ;
// BorderColor represents the border color of the appearance annotation area.
2020-09-21 01:20:10 +00:00
BorderColor _bd .PdfColor ;};func _ag (_fc CircleAnnotationDef ,_cab string )([]byte ,*_bd .PdfRectangle ,*_bd .PdfRectangle ,error ){_ga :=_cg .Circle {X :_fc .X ,Y :_fc .Y ,Width :_fc .Width ,Height :_fc .Height ,FillEnabled :_fc .FillEnabled ,FillColor :_fc .FillColor ,BorderEnabled :_fc .BorderEnabled ,BorderWidth :_fc .BorderWidth ,BorderColor :_fc .BorderColor ,Opacity :_fc .Opacity };_afg ,_bf ,_fd :=_ga .Draw (_cab );if _fd !=nil {return nil ,nil ,nil ,_fd ;};_dd :=&_bd .PdfRectangle {};_dd .Llx =_fc .X +_bf .Llx ;_dd .Lly =_fc .Y +_bf .Lly ;_dd .Urx =_fc .X +_bf .Urx ;_dd .Ury =_fc .Y +_bf .Ury ;return _afg ,_bf ,_dd ,nil ;};
// NewComboboxField generates a new combobox form field with partial name `name` at location `rect`
// on specified `page` and with field specific options `opt`.
func NewComboboxField (page *_bd .PdfPage ,name string ,rect []float64 ,opt ComboboxFieldOptions )(*_bd .PdfFieldChoice ,error ){if page ==nil {return nil ,_fg .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_fg .New ("\u0072\u0065\u0071\u0075\u0069\u0072\u0065\u0064\u0020\u0061\u0074\u0074\u0072\u0069\u0062u\u0074e\u0020\u006e\u006f\u0074\u0020\u0073\u0070\u0065\u0063\u0069\u0066\u0069\u0065\u0064");};if len (rect )!=4{return nil ,_fg .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_gdgf :=_bd .NewPdfField ();_bdd :=&_bd .PdfFieldChoice {};_gdgf .SetContext (_bdd );_bdd .PdfField =_gdgf ;_bdd .T =_be .MakeString (name );_bdd .Opt =_be .MakeArray ();for _ ,_aeac :=range opt .Choices {_bdd .Opt .Append (_be .MakeString (_aeac ));};_bdd .SetFlag (_bd .FieldFlagCombo );_gebe :=_bd .NewPdfAnnotationWidget ();_gebe .Rect =_be .MakeArrayFromFloats (rect );_gebe .P =page .ToPdfObject ();_gebe .F =_be .MakeInteger (4);_gebe .Parent =_bdd .ToPdfObject ();_bdd .Annotations =append (_bdd .Annotations ,_gebe );return _bdd ,nil ;};func _dea ()_be .StreamEncoder {return _be .NewFlateEncoder ()};
2020-08-27 21:45:09 +00:00
2020-09-14 09:32:45 +00:00
// NewSignatureField returns a new signature field with a visible appearance
// containing the specified signature lines and styled according to the
// specified options.
2020-09-21 01:20:10 +00:00
func NewSignatureField (signature *_bd .PdfSignature ,lines []*SignatureLine ,opts *SignatureFieldOpts )(*_bd .PdfFieldSignature ,error ){if signature ==nil {return nil ,_fg .New ("\u0073\u0069\u0067na\u0074\u0075\u0072\u0065\u0020\u0063\u0061\u006e\u006e\u006f\u0074\u0020\u0062\u0065\u0020\u006e\u0069\u006c");};_gdbe ,_gec :=_fbe (lines ,opts );if _gec !=nil {return nil ,_gec ;};_ebgc :=_bd .NewPdfFieldSignature (signature );_ebgc .Rect =_be .MakeArrayFromFloats (opts .Rect );_ebgc .AP =_gdbe ;return _ebgc ,nil ;};func _fge (_cffd *_bd .PdfField ,_acfa ,_gbdc float64 ,_bde string ,_ceba AppearanceStyle ,_edd *_ee .ContentStreamOperations ,_fbbef *_bd .PdfPageResources ,_dgf *_be .PdfObjectDictionary )(*_bd .XObjectForm ,error ){_cbfc :=_bd .NewPdfPageResources ();_gdc ,_befe :=_acfa ,_gbdc ;_gaed :=_ee .NewContentCreator ();if _ceba .BorderSize > 0{_edc (_gaed ,_ceba ,_acfa ,_gbdc );};if _ceba .DrawAlignmentReticle {_dcg :=_ceba ;_dcg .BorderSize =0.2;_fefd (_gaed ,_dcg ,_acfa ,_gbdc );};_gaed .Add_BMC ("\u0054\u0078");_gaed .Add_q ();_gaed .Add_BT ();_acfa ,_gbdc =_ceba .applyRotation (_dgf ,_acfa ,_gbdc ,_gaed );_bac ,_dff ,_acff :=_ceba .processDA (_cffd ,_edd ,_fbbef ,_cbfc ,_gaed );if _acff !=nil {return nil ,_acff ;};_gaea :=_bac .Font ;_gfd :=_bac .Size ;_cffb :=_be .MakeName (_bac .Name );_gfec :=_gfd ==0;if _gfec &&_dff {_gfd =_gbdc *_ceba .AutoFontSizeFraction ;};_dag :=_gaea .Encoder ();if _dag ==nil {_b .Log .Debug ("\u0057\u0041RN\u003a\u0020\u0066\u006f\u006e\u0074\u0020\u0065\u006e\u0063\u006f\u0064\u0065\u0072\u0020\u0069\u0073\u0020\u006e\u0069l\u002e\u0020\u0041\u0073s\u0075\u006d\u0069\u006eg \u0069\u0064e\u006et\u0069\u0074\u0079\u0020\u0065\u006ec\u006f\u0064\u0065r\u002e\u0020O\u0075\u0074\u0070\u0075\u0074\u0020\u006d\u0061\u0079\u0020\u0062\u0065\u0020\u0069n\u0063\u006f\u0072\u0072\u0065\u0063\u0074\u002e");_dag =_d .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};if len (_bde )==0{return nil ,nil ;};_gegg :=2.0;_aff :=0.0;if _dag !=nil {for _ ,_cbcb :=range _bde {_cag ,_agg :=_gaea .GetRuneMetrics (_cbcb );if !_agg {_b .Log .Debug ("\u0046\u006f\u006e\u0074\u0020\u0064o\u0065\u0073\u0020\u006e\u006f\u0074\u0020\u0068\u0061\u0076\u0065\u0020\u0072\u0075\u006e\u0065\u0020\u006d\u0065\u0074r\u0069\u0063\u0073\u0020\u0066\u006f\u0072\u0020\u0025\u0076\u0020\u002d\u0020\u0073k\u0069p\u0070\u0069\u006e\u0067",_cbcb );continue ;};_aff +=_cag .Wx ;};_bde =string (_dag .Encode (_bde ));};if _gfd ==0||_gfec &&_aff > 0&&_gegg +_aff *_gfd /1000.0> _acfa {_gfd =0.95*1000.0*(_acfa -_gegg )/_aff ;};_ccg :=1.0*_gfd ;_cdbg :=2.0;{_egb :=_ccg ;if _gfec &&_cdbg +_egb > _gbdc {_gfd =0.95*(_gbdc -_cdbg );_ccg =1.0*_gfd ;_egb =_ccg ;};if _gbdc > _egb {_cdbg =(_gbdc -_egb )/2.0;_cdbg +=1.50;};};_gaed .Add_Tf (*_cffb ,_gfd );_gaed .Add_Td (_gegg ,_cdbg );_gaed .Add_Tj (*_be .MakeString (_bde ));_gaed .Add_ET ();_gaed .Add_Q ();_gaed .Add_EMC ();_adbc :=_bd .NewXObjectForm ();_adbc .Resources =_cbfc ;_adbc .BBox =_be .MakeArrayFromFloats ([]float64 {0,0,_gdc ,_befe });_adbc .SetContentStream (_gaed .Bytes (),_dea ());return _adbc ,nil ;};
2020-08-31 21:12:07 +00:00
2020-09-21 01:20:10 +00:00
// TextFieldOptions defines optional parameter for a text field in a form.
type TextFieldOptions struct{MaxLen int ;Value string ;};
2020-08-31 21:12:07 +00:00
2020-09-21 01:20:10 +00:00
// FieldAppearance implements interface model.FieldAppearanceGenerator and generates appearance streams
// for fields taking into account what value is in the field. A common use case is for generating the
// appearance stream prior to flattening fields.
//
// If `OnlyIfMissing` is true, the field appearance is generated only for fields that do not have an
// appearance stream specified.
// If `RegenerateTextFields` is true, all text fields are regenerated (even if OnlyIfMissing is true).
type FieldAppearance struct{OnlyIfMissing bool ;RegenerateTextFields bool ;_bb *AppearanceStyle ;};func _cbfcc (_fcdf RectangleAnnotationDef )(*_be .PdfObjectDictionary ,*_bd .PdfRectangle ,error ){_beacb :=_bd .NewXObjectForm ();_beacb .Resources =_bd .NewPdfPageResources ();_ccga :="";if _fcdf .Opacity < 1.0{_fabe :=_be .MakeDict ();_fabe .Set ("\u0063\u0061",_be .MakeFloat (_fcdf .Opacity ));_fabe .Set ("\u0043\u0041",_be .MakeFloat (_fcdf .Opacity ));_efcfa :=_beacb .Resources .AddExtGState ("\u0067\u0073\u0031",_fabe );if _efcfa !=nil {_b .Log .Debug ("U\u006e\u0061\u0062\u006c\u0065\u0020t\u006f\u0020\u0061\u0064\u0064\u0020\u0065\u0078\u0074g\u0073\u0074\u0061t\u0065 \u0067\u0073\u0031");return nil ,nil ,_efcfa ;};_ccga ="\u0067\u0073\u0031";};_afbe ,_deab ,_gebb ,_aaaga :=_bdda (_fcdf ,_ccga );if _aaaga !=nil {return nil ,nil ,_aaaga ;};_aaaga =_beacb .SetContentStream (_afbe ,nil );if _aaaga !=nil {return nil ,nil ,_aaaga ;};_beacb .BBox =_deab .ToPdfObject ();_afdd :=_be .MakeDict ();_afdd .Set ("\u004e",_beacb .ToPdfObject ());return _afdd ,_gebb ,nil ;};func _fefd (_bfad *_ee .ContentCreator ,_bcbb AppearanceStyle ,_fbdd ,_ecc float64 ){_bfad .Add_q ().Add_re (0,0,_fbdd ,_ecc ).Add_re (0,_ecc /2,_fbdd ,_ecc /2).Add_re (0,0,_fbdd ,_ecc ).Add_re (_fbdd /2,0,_fbdd /2,_ecc ).Add_w (_bcbb .BorderSize ).SetStrokingColor (_bcbb .BorderColor ).SetNonStrokingColor (_bcbb .FillColor ).Add_B ().Add_Q ();};func _dde (_dg *_bd .PdfAnnotationWidget ,_def *_bd .PdfFieldText ,_ebc *_bd .PdfPageResources ,_dgb AppearanceStyle )(*_be .PdfObjectDictionary ,error ){_eed :=_bd .NewPdfPageResources ();_fbf ,_fcb :=_be .GetArray (_dg .Rect );if !_fcb {return nil ,_fg .New ("\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0052\u0065\u0063\u0074");};_dgd ,_cca :=_bd .NewPdfRectangle (*_fbf );if _cca !=nil {return nil ,_cca ;};_baa ,_feb :=_dgd .Width (),_dgd .Height ();_gdg ,_fga :=_baa ,_feb ;_ebg ,_bbg :=_be .GetDict (_dg .MK );if _bbg {_gfe ,_ :=_be .GetDict (_dg .BS );_bcd :=_dgb .applyAppearanceCharacteristics (_ebg ,_gfe ,nil );if _bcd !=nil {return nil ,_bcd ;};};_afgc ,_cca :=_ee .NewContentStreamParser (_eafc (_def .PdfField )).Parse ();if _cca !=nil {return nil ,_cca ;};_feba :=_ee .NewContentCreator ();if _dgb .BorderSize > 0{_edc (_feba ,_dgb ,_baa ,_feb );};if _dgb .DrawAlignmentReticle {_febad :=_dgb ;_febad .BorderSize =0.2;_fefd (_feba ,_febad ,_baa ,_feb );};_feba .Add_BMC ("\u0054\u0078");_feba .Add_q ();_baa ,_feb =_dgb .applyRotation (_ebg ,_baa ,_feb ,_feba );_feba .Add_BT ();_ed ,_dbd ,_cca :=_dgb .processDA (_def .PdfField ,_afgc ,_ebc ,_eed ,_feba );if _cca !=nil {return nil ,_cca ;};_bg :=_ed .Font ;_fde :=_ed .Size ;_gag :=_be .MakeName (_ed .Name );_ef :=_fde ==0;if _ef &&_dbd {_fde =_feb *_dgb .AutoFontSizeFraction ;};_ec :=_bg .Encoder ();if _ec ==nil {_b .Log .Debug ("\u0057\u0041RN\u003a\u0020\u0066\u006f\u006e\u0074\u0020\u0065\u006e\u0063\u006f\u0064\u0065\u0072\u0020\u0069\u0073\u0020\u006e\u0069l\u002e\u0020\u0041\u0073s\u0075\u006d\u0069\u006eg \u0069\u0064e\u006et\u0069\u0074\u0079\u0020\u0065\u006ec\u006f\u0064\u0065r\u002e\u0020O\u0075\u0074\u0070\u0075\u0074\u0020\u006d\u0061\u0079\u0020\u0062\u0065\u0020\u0069n\u0063\u006f\u0072\u0072\u0065\u0063\u0074\u002e");_ec =_d .NewIdentityTextEncoder ("\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079\u002d\u0048");};_cfaa ,_cca :=_bg .GetFontDescriptor ();if _cca !=nil {_b .Log .Debug ("\u0045\u0072ro\u0072\u003a\u0020U\u006e\u0061\u0062\u006ce t\u006f g\u0065\u0074\u0020\u0066\u006f\u006e\u0074 d\u0065\u0073\u0063\u0072\u0069\u0070\u0074o\u0072");};var _ffd string ;if _gg ,_deg :=_be .GetString (_def .V );_deg {_ffd =_gg .Decoded ();};if len (_ffd )==0{return nil ,nil ;};_ge :=[]string {_ffd };_ac :=false ;if _def .Flags ().Has (_bd .FieldFlagMultiline ){_ac =true ;_ffd =_a .Replace (_ffd ,"\u000d\u000a","\u000a",-1);_ffd =_a .Replace (_ffd ,"\u000d","\u000a",-1);_ge =_a .Split (_ffd ,"\u000a");};_cdg :=0.0;_bad :=0;if _ec !=nil {_faf :=len (_ge );_fcf :=0;for _fcf < _faf {var _ged float64 ;_aef :=-1;_bfe :=0.0;for _eac ,_gge :=range _ge [_fcf ]{if _gge
2020-08-31 21:12:07 +00:00
2020-09-21 01:20:10 +00:00
// GenerateAppearanceDict generates an appearance dictionary for widget annotation `wa` for the `field` in `form`.
// Implements interface model.FieldAppearanceGenerator.
func (_bea FieldAppearance )GenerateAppearanceDict (form *_bd .PdfAcroForm ,field *_bd .PdfField ,wa *_bd .PdfAnnotationWidget )(*_be .PdfObjectDictionary ,error ){_b .Log .Trace ("\u0047\u0065n\u0065\u0072\u0061\u0074e\u0041\u0070p\u0065\u0061\u0072\u0061\u006e\u0063\u0065\u0044i\u0063\u0074\u0020\u0066\u006f\u0072\u0020\u0025\u0076\u0020\u0020\u0056:\u0020\u0025\u002b\u0076",field .PartialName (),field .V );_ ,_aa :=field .GetContext ().(*_bd .PdfFieldText );_eeg ,_cfa :=_be .GetDict (wa .AP );if _cfa &&_bea .OnlyIfMissing &&(!_aa ||!_bea .RegenerateTextFields ){_b .Log .Trace ("\u0041\u006c\u0072\u0065a\u0064\u0079\u0020\u0070\u006f\u0070\u0075\u006c\u0061\u0074e\u0064 \u002d\u0020\u0069\u0067\u006e\u006f\u0072i\u006e\u0067");return _eeg ,nil ;};if form .DR ==nil {form .DR =_bd .NewPdfPageResources ();};switch _bee :=field .GetContext ().(type ){case *_bd .PdfFieldText :_gd :=_bee ;switch {case _gd .Flags ().Has (_bd .FieldFlagPassword ):return nil ,nil ;case _gd .Flags ().Has (_bd .FieldFlagFileSelect ):return nil ,nil ;case _gd .Flags ().Has (_bd .FieldFlagComb ):if _gd .MaxLen !=nil {_baba ,_fcd :=_gbd (wa ,_gd ,form .DR ,_bea .Style ());if _fcd !=nil {return nil ,_fcd ;};return _baba ,nil ;};};_gbfd ,_aae :=_dde (wa ,_gd ,form .DR ,_bea .Style ());if _aae !=nil {return nil ,_aae ;};return _gbfd ,nil ;case *_bd .PdfFieldButton :_db :=_bee ;if _db .IsCheckbox (){_gf ,_eg :=_cfag (wa ,_db ,form .DR ,_bea .Style ());if _eg !=nil {return nil ,_eg ;};return _gf ,nil ;};_b .Log .Debug ("\u0054\u004f\u0044\u004f\u003a\u0020\u0055\u004e\u0048\u0041\u004e\u0044\u004c\u0045\u0044 \u0062u\u0074\u0074\u006f\u006e\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u002b\u0076",_db .GetType ());case *_bd .PdfFieldChoice :_bae :=_bee ;switch {case _bae .Flags ().Has (_bd .FieldFlagCombo ):_fcdd ,_gfg :=_bbba (form ,wa ,_bae ,_bea .Style ());if _gfg !=nil {return nil ,_gfg ;};return _fcdd ,nil ;default:_b .Log .Debug ("\u0054\u004f\u0044\u004f\u003a\u0020\u0055N\u0048\u0041\u004eD\u004c\u0045\u0044\u0020c\u0068\u006f\u0069\u0063\u0065\u0020\u0066\u0069\u0065\u006c\u0064\u0020\u0077\u0069\u0074\u0068\u0020\u0066\u006c\u0061\u0067\u0073\u003a\u0020\u0025\u0073",_bae .Flags ().String ());};default:_b .Log .Debug ("\u0054\u004f\u0044\u004f\u003a\u0020\u0055\u004e\u0048\u0041N\u0044\u004c\u0045\u0044\u0020\u0066\u0069e\u006c\u0064\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_bee );};return nil ,nil ;};
2020-08-31 21:12:07 +00:00
2020-09-21 01:20:10 +00:00
// NewTextField generates a new text field with partial name `name` at location
// specified by `rect` on given `page` and with field specific options `opt`.
func NewTextField (page *_bd .PdfPage ,name string ,rect []float64 ,opt TextFieldOptions )(*_bd .PdfFieldText ,error ){if page ==nil {return nil ,_fg .New ("\u0070a\u0067e\u0020\u006e\u006f\u0074\u0020s\u0070\u0065c\u0069\u0066\u0069\u0065\u0064");};if len (name )<=0{return nil ,_fg .New ("\u0072\u0065\u0071\u0075\u0069\u0072\u0065\u0064\u0020\u0061\u0074\u0074\u0072\u0069\u0062u\u0074e\u0020\u006e\u006f\u0074\u0020\u0073\u0070\u0065\u0063\u0069\u0066\u0069\u0065\u0064");};if len (rect )!=4{return nil ,_fg .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0072\u0061\u006e\u0067\u0065");};_fbag :=_bd .NewPdfField ();_eaa :=&_bd .PdfFieldText {};_fbag .SetContext (_eaa );_eaa .PdfField =_fbag ;_eaa .T =_be .MakeString (name );if opt .MaxLen > 0{_eaa .MaxLen =_be .MakeInteger (int64 (opt .MaxLen ));};if len (opt .Value )> 0{_eaa .V =_be .MakeString (opt .Value );};_ebed :=_bd .NewPdfAnnotationWidget ();_ebed .Rect =_be .MakeArrayFromFloats (rect );_ebed .P =page .ToPdfObject ();_ebed .F =_be .MakeInteger (4);_ebed .Parent =_eaa .ToPdfObject ();_eaa .Annotations =append (_eaa .Annotations ,_ebed );return _eaa ,nil ;};
2020-08-31 21:12:07 +00:00
2020-09-21 01:20:10 +00:00
// Style returns the appearance style of `fa`. If not specified, returns default style.
func (_fad FieldAppearance )Style ()AppearanceStyle {if _fad ._bb !=nil {return *_fad ._bb ;};return AppearanceStyle {AutoFontSizeFraction :0.65,CheckmarkRune :'✔',BorderSize :0.0,BorderColor :_bd .NewPdfColorDeviceGray (0),FillColor :_bd .NewPdfColorDeviceGray (1),MultilineLineHeight :1.2,MultilineVAlignMiddle :false ,DrawAlignmentReticle :false ,AllowMK :true };};