-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from Venafi/pdf-visual-sig
initial pdf visual signature support
- Loading branch information
Showing
15 changed files
with
1,101 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pdfsig | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
func (context *SignContext) createAppearance(rect [4]float64) ([]byte, error) { | ||
text := context.SignData.Signature.Info.Name | ||
|
||
rectWidth := rect[2] - rect[0] | ||
rectHeight := rect[3] - rect[1] | ||
|
||
if rectWidth < 1 || rectHeight < 1 { | ||
return nil, fmt.Errorf("invalid rectangle dimensions: width %.2f and height %.2f must be greater than 0", rectWidth, rectHeight) | ||
} | ||
|
||
// Calculate font size | ||
fontSize := rectHeight * 0.8 // Initial font size | ||
textWidth := float64(len(text)) * fontSize * 0.5 // Approximate text width | ||
if textWidth > rectWidth { | ||
fontSize = rectWidth / (float64(len(text)) * 0.5) // Adjust font size to fit text within rect width | ||
} | ||
|
||
var appearance_stream_buffer bytes.Buffer | ||
appearance_stream_buffer.WriteString("q\n") // Save graphics state | ||
appearance_stream_buffer.WriteString("BT\n") // Begin text | ||
appearance_stream_buffer.WriteString(fmt.Sprintf("/F1 %.2f Tf\n", fontSize)) // Font and size | ||
appearance_stream_buffer.WriteString(fmt.Sprintf("0 %.2f Td\n", rectHeight-fontSize)) // Position in unit square | ||
appearance_stream_buffer.WriteString("0.2 0.2 0.6 rg\n") // Set font color to ballpoint-like color (RGB) | ||
appearance_stream_buffer.WriteString(fmt.Sprintf("%s Tj\n", pdfString(text))) // Show text | ||
appearance_stream_buffer.WriteString("ET\n") // End text | ||
appearance_stream_buffer.WriteString("Q\n") // Restore graphics state | ||
|
||
var appearance_buffer bytes.Buffer | ||
appearance_buffer.WriteString("<<\n") | ||
appearance_buffer.WriteString(" /Type /XObject\n") | ||
appearance_buffer.WriteString(" /Subtype /Form\n") | ||
appearance_buffer.WriteString(fmt.Sprintf(" /BBox [0 0 %f %f]\n", rectWidth, rectHeight)) | ||
appearance_buffer.WriteString(" /Matrix [1 0 0 1 0 0]\n") // No scaling or translation | ||
|
||
// Resources dictionary | ||
appearance_buffer.WriteString(" /Resources <<\n") | ||
appearance_buffer.WriteString(" /Font <<\n") | ||
appearance_buffer.WriteString(" /F1 <<\n") | ||
appearance_buffer.WriteString(" /Type /Font\n") | ||
appearance_buffer.WriteString(" /Subtype /Type1\n") | ||
appearance_buffer.WriteString(" /BaseFont /Times-Roman\n") | ||
appearance_buffer.WriteString(" >>\n") | ||
appearance_buffer.WriteString(" >>\n") | ||
appearance_buffer.WriteString(" >>\n") | ||
|
||
appearance_buffer.WriteString(" /FormType 1\n") | ||
appearance_buffer.WriteString(fmt.Sprintf(" /Length %d\n", appearance_stream_buffer.Len())) | ||
appearance_buffer.WriteString(">>\n") | ||
|
||
appearance_buffer.WriteString("stream\n") | ||
appearance_buffer.Write(appearance_stream_buffer.Bytes()) | ||
appearance_buffer.WriteString("endstream\n") | ||
|
||
return appearance_buffer.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package pdfsig | ||
|
||
import "strconv" | ||
|
||
func _() { | ||
// An "invalid array index" compiler error signifies that the constant values have changed. | ||
// Re-run the stringer command to generate them again. | ||
var x [1]struct{} | ||
_ = x[CertificationSignature-1] | ||
_ = x[ApprovalSignature-2] | ||
_ = x[UsageRightsSignature-3] | ||
_ = x[TimeStampSignature-4] | ||
} | ||
|
||
const _CertType_name = "CertificationSignatureApprovalSignatureUsageRightsSignatureTimeStampSignature" | ||
|
||
var _CertType_index = [...]uint8{0, 22, 39, 59, 77} | ||
|
||
func (i CertType) String() string { | ||
i -= 1 | ||
if i >= CertType(len(_CertType_index)-1) { | ||
return "CertType(" + strconv.FormatInt(int64(i+1), 10) + ")" | ||
} | ||
return _CertType_name[_CertType_index[i]:_CertType_index[i+1]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.