Skip to content

Commit

Permalink
add Config.ConversionForUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
oneplus1000 committed Mar 30, 2024
1 parent 597b99a commit f45727c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 48 deletions.
28 changes: 24 additions & 4 deletions box.go
Expand Up @@ -2,7 +2,7 @@ package gopdf

type Box struct {
Left, Top, Right, Bottom float64
unitOverride int
unitOverride defaultUnitConfig
}

// UnitsToPoints converts the box coordinates to Points. When this is called it is assumed the values of the box are in Units
Expand All @@ -11,8 +11,9 @@ func (box *Box) UnitsToPoints(t int) (b *Box) {
return
}

if box.unitOverride != UnitUnset {
t = box.unitOverride
unitCfg := defaultUnitConfig{Unit: t}
if box.unitOverride.getUnit() != UnitUnset {
unitCfg = box.unitOverride
}

b = &Box{
Expand All @@ -21,6 +22,25 @@ func (box *Box) UnitsToPoints(t int) (b *Box) {
Right: box.Right,
Bottom: box.Bottom,
}
UnitsToPointsVar(t, &b.Left, &b.Top, &b.Right, &b.Bottom)
unitsToPointsVar(unitCfg, &b.Left, &b.Top, &b.Right, &b.Bottom)
return
}

func (box *Box) unitsToPoints(unitCfg unitConfigurator) (b *Box) {
if box == nil {
return
}

if box.unitOverride.getUnit() != UnitUnset {
unitCfg = box.unitOverride
}

b = &Box{
Left: box.Left,
Top: box.Top,
Right: box.Right,
Bottom: box.Bottom,
}
unitsToPointsVar(unitCfg, &b.Left, &b.Top, &b.Right, &b.Bottom)
return
}
72 changes: 63 additions & 9 deletions config.go
Expand Up @@ -14,6 +14,10 @@ const (
conversionUnitMM = 72.0 / 25.4
conversionUnitCM = 72.0 / 2.54
conversionUnitIN = 72.0
//We use a dpi of 96 dpi as the default, so we get a conversionUnitPX = 3.0 / 4.0, which comes from 72.0 / 96.0.
//If you want to change this value, you can change it at Config.ConversionForUnit
//example: If you use dpi at 300.0
//pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}, ConversionForUnit: 72.0 / 300.0 })
conversionUnitPX = 3.0 / 4.0
)

Expand All @@ -30,11 +34,22 @@ const (

// Config static config
type Config struct {
Unit int // The unit type to use when composing the document.
TrimBox Box // The default trim box for all pages in the document
PageSize Rect // The default page size for all pages in the document
K float64 // Not sure
Protection PDFProtectionConfig // Protection settings
Unit int // The unit type to use when composing the document.
//Value that use to convert units to points.
//If this variable is not 0. This value will be used to calculate the unit conversion instead of the existing const value in the system.
//And if this variable is not 0. Value ​​in Config.Unit will not be used.
ConversionForUnit float64
TrimBox Box // The default trim box for all pages in the document
PageSize Rect // The default page size for all pages in the document
K float64 // Not sure
Protection PDFProtectionConfig // Protection settings
}

func (c Config) getUnit() int {
return c.Unit
}
func (c Config) getConversionForUnit() float64 {
return c.ConversionForUnit
}

// PDFProtectionConfig config of pdf protection
Expand All @@ -47,7 +62,14 @@ type PDFProtectionConfig struct {

// UnitsToPoints converts units of the provided type to points
func UnitsToPoints(t int, u float64) float64 {
switch t {
return unitsToPoints(defaultUnitConfig{Unit: t}, u)
}

func unitsToPoints(unitCfg unitConfigurator, u float64) float64 {
if unitCfg.getConversionForUnit() != 0 {
return u * unitCfg.getConversionForUnit()
}
switch unitCfg.getUnit() {
case UnitPT:
return u * conversionUnitPT
case UnitMM:
Expand All @@ -65,7 +87,14 @@ func UnitsToPoints(t int, u float64) float64 {

// PointsToUnits converts points to the provided units
func PointsToUnits(t int, u float64) float64 {
switch t {
return pointsToUnits(defaultUnitConfig{Unit: t}, u)
}

func pointsToUnits(unitCfg unitConfigurator, u float64) float64 {
if unitCfg.getConversionForUnit() != 0 {
return u * unitCfg.getConversionForUnit()
}
switch unitCfg.getUnit() {
case UnitPT:
return u / conversionUnitPT
case UnitMM:
Expand All @@ -83,14 +112,39 @@ func PointsToUnits(t int, u float64) float64 {

// UnitsToPointsVar converts units of the provided type to points for all variables supplied
func UnitsToPointsVar(t int, u ...*float64) {
unitsToPointsVar(defaultUnitConfig{Unit: t}, u...)
}

func unitsToPointsVar(unitCfg unitConfigurator, u ...*float64) {
for x := 0; x < len(u); x++ {
*u[x] = UnitsToPoints(t, *u[x])
*u[x] = unitsToPoints(unitCfg, *u[x])
}
}

// PointsToUnitsVar converts points to the provided units for all variables supplied
func PointsToUnitsVar(t int, u ...*float64) {
pointsToUnitsVar(defaultUnitConfig{Unit: t}, u...)
}

func pointsToUnitsVar(unitCfg unitConfigurator, u ...*float64) {
for x := 0; x < len(u); x++ {
*u[x] = PointsToUnits(t, *u[x])
*u[x] = pointsToUnits(unitCfg, *u[x])
}
}

type unitConfigurator interface {
getUnit() int
getConversionForUnit() float64
}

type defaultUnitConfig struct {
Unit int
ConversionForUnit float64
}

func (d defaultUnitConfig) getUnit() int {
return d.Unit
}
func (d defaultUnitConfig) getConversionForUnit() float64 {
return d.ConversionForUnit
}
16 changes: 8 additions & 8 deletions gopdf.go
Expand Up @@ -1735,7 +1735,7 @@ func (gp *GoPdf) MeasureTextWidth(text string) (float64, error) {
if err != nil {
return 0, err
}
return PointsToUnits(gp.config.Unit, textWidthPdfUnit), nil
return pointsToUnits(gp.config, textWidthPdfUnit), nil
}

// MeasureCellHeightByText : measure Height of cell by text (use current font)
Expand All @@ -1750,7 +1750,7 @@ func (gp *GoPdf) MeasureCellHeightByText(text string) (float64, error) {
if err != nil {
return 0, err
}
return PointsToUnits(gp.config.Unit, cellHeightPdfUnit), nil
return pointsToUnits(gp.config, cellHeightPdfUnit), nil
}

// Curve Draws a Bézier curve (the Bézier curve is tangent to the line between the control points at either end of the curve)
Expand Down Expand Up @@ -1969,8 +1969,8 @@ func (gp *GoPdf) init(importer ...*gofpdi.Importer) {
gp.compressLevel = zlib.DefaultCompression

// change the unit type
gp.config.PageSize = *gp.config.PageSize.UnitsToPoints(gp.config.Unit)
gp.config.TrimBox = *gp.config.TrimBox.UnitsToPoints(gp.config.Unit)
gp.config.PageSize = *gp.config.PageSize.unitsToPoints(gp.config)
gp.config.TrimBox = *gp.config.TrimBox.unitsToPoints(gp.config)

// init gofpdi free pdf document importer
gp.fpdi = importerOrDefault(importer...)
Expand All @@ -1991,22 +1991,22 @@ func (gp *GoPdf) resetCurrXY() {

// UnitsToPoints converts the units to the documents unit type
func (gp *GoPdf) UnitsToPoints(u float64) float64 {
return UnitsToPoints(gp.config.Unit, u)
return unitsToPoints(gp.config, u)
}

// UnitsToPointsVar converts the units to the documents unit type for all variables passed in
func (gp *GoPdf) UnitsToPointsVar(u ...*float64) {
UnitsToPointsVar(gp.config.Unit, u...)
unitsToPointsVar(gp.config, u...)
}

// PointsToUnits converts the points to the documents unit type
func (gp *GoPdf) PointsToUnits(u float64) float64 {
return PointsToUnits(gp.config.Unit, u)
return pointsToUnits(gp.config, u)
}

// PointsToUnitsVar converts the points to the documents unit type for all variables passed in
func (gp *GoPdf) PointsToUnitsVar(u ...*float64) {
PointsToUnitsVar(gp.config.Unit, u...)
pointsToUnitsVar(gp.config, u...)
}

func (gp *GoPdf) isUseProtection() bool {
Expand Down
40 changes: 20 additions & 20 deletions page_sizes.go
@@ -1,64 +1,64 @@
package gopdf

// PageSizeLetter page format
var PageSizeLetter = &Rect{W: 612, H: 792, unitOverride: UnitPT}
var PageSizeLetter = &Rect{W: 612, H: 792, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeLetterSmall page format
var PageSizeLetterSmall = &Rect{W: 612, H: 792, unitOverride: UnitPT}
var PageSizeLetterSmall = &Rect{W: 612, H: 792, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeTabloid page format
var PageSizeTabloid = &Rect{W: 792, H: 1224, unitOverride: UnitPT}
var PageSizeTabloid = &Rect{W: 792, H: 1224, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeLedger page format
var PageSizeLedger = &Rect{W: 1224, H: 792, unitOverride: UnitPT}
var PageSizeLedger = &Rect{W: 1224, H: 792, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeLegal page format
var PageSizeLegal = &Rect{W: 612, H: 1008, unitOverride: UnitPT}
var PageSizeLegal = &Rect{W: 612, H: 1008, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeStatement page format
var PageSizeStatement = &Rect{W: 396, H: 612, unitOverride: UnitPT}
var PageSizeStatement = &Rect{W: 396, H: 612, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeExecutive page format
var PageSizeExecutive = &Rect{W: 540, H: 720, unitOverride: UnitPT}
var PageSizeExecutive = &Rect{W: 540, H: 720, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA0 page format
var PageSizeA0 = &Rect{W: 2384, H: 3371, unitOverride: UnitPT}
var PageSizeA0 = &Rect{W: 2384, H: 3371, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA1 page format
var PageSizeA1 = &Rect{W: 1685, H: 2384, unitOverride: UnitPT}
var PageSizeA1 = &Rect{W: 1685, H: 2384, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA2 page format
var PageSizeA2 = &Rect{W: 1190, H: 1684, unitOverride: UnitPT}
var PageSizeA2 = &Rect{W: 1190, H: 1684, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA3 page format
var PageSizeA3 = &Rect{W: 842, H: 1190, unitOverride: UnitPT}
var PageSizeA3 = &Rect{W: 842, H: 1190, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA4 page format
var PageSizeA4 = &Rect{W: 595, H: 842, unitOverride: UnitPT}
var PageSizeA4 = &Rect{W: 595, H: 842, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA4Landscape page format
var PageSizeA4Landscape = &Rect{W: 842, H: 595, unitOverride: UnitPT}
var PageSizeA4Landscape = &Rect{W: 842, H: 595, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA4Small page format
var PageSizeA4Small = &Rect{W: 595, H: 842, unitOverride: UnitPT}
var PageSizeA4Small = &Rect{W: 595, H: 842, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeA5 page format
var PageSizeA5 = &Rect{W: 420, H: 595, unitOverride: UnitPT}
var PageSizeA5 = &Rect{W: 420, H: 595, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeB4 page format
var PageSizeB4 = &Rect{W: 729, H: 1032, unitOverride: UnitPT}
var PageSizeB4 = &Rect{W: 729, H: 1032, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeB5 page format
var PageSizeB5 = &Rect{W: 516, H: 729, unitOverride: UnitPT}
var PageSizeB5 = &Rect{W: 516, H: 729, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeFolio page format
var PageSizeFolio = &Rect{W: 612, H: 936, unitOverride: UnitPT}
var PageSizeFolio = &Rect{W: 612, H: 936, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSizeQuarto page format
var PageSizeQuarto = &Rect{W: 610, H: 780, unitOverride: UnitPT}
var PageSizeQuarto = &Rect{W: 610, H: 780, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// PageSize10x14 page format
var PageSize10x14 = &Rect{W: 720, H: 1008, unitOverride: UnitPT}
var PageSize10x14 = &Rect{W: 720, H: 1008, unitOverride: defaultUnitConfig{Unit: UnitPT}}

// // PageSizeEnvelope page format
// var PageSizeEnvelope = Rect{W:???,H:???}
28 changes: 21 additions & 7 deletions rect.go
Expand Up @@ -4,7 +4,7 @@ package gopdf
type Rect struct {
W float64
H float64
unitOverride int
unitOverride defaultUnitConfig
}

// PointsToUnits converts the rectangles width and height to Units. When this is called it is assumed the values of the rectangle are in Points
Expand All @@ -13,12 +13,13 @@ func (rect *Rect) PointsToUnits(t int) (r *Rect) {
return
}

if rect.unitOverride != UnitUnset {
t = rect.unitOverride
unitCfg := defaultUnitConfig{Unit: t}
if rect.unitOverride.getUnit() != UnitUnset {
unitCfg = rect.unitOverride
}

r = &Rect{W: rect.W, H: rect.H}
PointsToUnitsVar(t, &r.W, &r.H)
pointsToUnitsVar(unitCfg, &r.W, &r.H)
return
}

Expand All @@ -28,11 +29,24 @@ func (rect *Rect) UnitsToPoints(t int) (r *Rect) {
return
}

if rect.unitOverride != UnitUnset {
t = rect.unitOverride
unitCfg := defaultUnitConfig{Unit: t}
if rect.unitOverride.getUnit() != UnitUnset {
unitCfg = rect.unitOverride
}

r = &Rect{W: rect.W, H: rect.H}
UnitsToPointsVar(t, &r.W, &r.H)
unitsToPointsVar(unitCfg, &r.W, &r.H)
return
}

func (rect *Rect) unitsToPoints(unitCfg unitConfigurator) (r *Rect) {
if rect == nil {
return
}
if rect.unitOverride.getUnit() != UnitUnset {
unitCfg = rect.unitOverride
}
r = &Rect{W: rect.W, H: rect.H}
unitsToPointsVar(unitCfg, &r.W, &r.H)
return
}

0 comments on commit f45727c

Please sign in to comment.