mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-30 13:48:51 +08:00
Merge pull request #139 from gunnsth/dev-textmarks-append
Append function to TextMarkArray for processing and grouping text
This commit is contained in:
commit
c5c68303a4
@ -8,10 +8,14 @@ package license
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/unidoc/unipdf/v3/common"
|
||||
)
|
||||
|
||||
// Defaults to the open source license.
|
||||
// Defaults to unlicensed.
|
||||
var licenseKey = MakeUnlicensedKey()
|
||||
|
||||
// SetLicenseKey sets and validates the license key.
|
||||
@ -35,6 +39,29 @@ func SetLicenseKey(content string, customerName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const licensePathEnvironmentVar = `UNIPDF_LICENSE_PATH`
|
||||
const licenseCustomerNameEnvironmentVar = `UNIPDF_CUSTOMER_NAME`
|
||||
|
||||
func init() {
|
||||
lpath := os.Getenv(licensePathEnvironmentVar)
|
||||
custName := os.Getenv(licenseCustomerNameEnvironmentVar)
|
||||
|
||||
if len(lpath) == 0 || len(custName) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(lpath)
|
||||
if err != nil {
|
||||
common.Log.Debug("Unable to read license file: %v", err)
|
||||
return
|
||||
}
|
||||
err = SetLicenseKey(string(data), custName)
|
||||
if err != nil {
|
||||
common.Log.Debug("Unable to load license: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func GetLicenseKey() *LicenseKey {
|
||||
if licenseKey == nil {
|
||||
return nil
|
||||
|
@ -47,6 +47,8 @@ func (e *Extractor) ExtractPageText() (*PageText, int, int, error) {
|
||||
return nil, numChars, numMisses, err
|
||||
}
|
||||
pt.computeViews()
|
||||
procBuf(pt)
|
||||
|
||||
return pt, numChars, numMisses, err
|
||||
}
|
||||
|
||||
@ -937,6 +939,11 @@ type TextMarkArray struct {
|
||||
marks []TextMark
|
||||
}
|
||||
|
||||
// Append appends `mark` to the mark array.
|
||||
func (ma *TextMarkArray) Append(mark TextMark) {
|
||||
ma.marks = append(ma.marks, mark)
|
||||
}
|
||||
|
||||
// String returns a string describing `ma`.
|
||||
func (ma TextMarkArray) String() string {
|
||||
n := len(ma.marks)
|
||||
|
@ -54,7 +54,7 @@ func maxFloat(a, b float64) float64 {
|
||||
return b
|
||||
}
|
||||
|
||||
func procBuf(buf *bytes.Buffer) {
|
||||
func procBuf(pt *PageText) {
|
||||
if isTesting {
|
||||
return
|
||||
}
|
||||
@ -66,12 +66,23 @@ func procBuf(buf *bytes.Buffer) {
|
||||
fmt.Printf("Unlicensed copy of unidoc\n")
|
||||
fmt.Printf("To get rid of the watermark and keep entire text - Please get a license on https://unidoc.io\n")
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(pt.viewText)
|
||||
|
||||
s := "- [Unlicensed UniDoc - Get a license on https://unidoc.io]"
|
||||
if buf.Len() > 100 {
|
||||
s = "... [Truncated - Unlicensed UniDoc - Get a license on https://unidoc.io]"
|
||||
buf.Truncate(buf.Len() - 100)
|
||||
}
|
||||
buf.WriteString(s)
|
||||
pt.viewText = buf.String()
|
||||
|
||||
if len(pt.marks) > 200 {
|
||||
pt.marks = pt.marks[:200]
|
||||
}
|
||||
if len(pt.viewMarks) > 200 {
|
||||
pt.viewMarks = pt.viewMarks[:200]
|
||||
}
|
||||
}
|
||||
|
||||
// truncate returns the first `n` characters in string `s`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user