From ae85ad8a3e829100b2220e24fd870934ea739f70 Mon Sep 17 00:00:00 2001 From: cdillond <93200981+cdillond@users.noreply.github.com> Date: Thu, 12 Sep 2024 15:27:00 -0400 Subject: [PATCH] fixed date formatting and color comparisons --- format.go | 44 ++++++++++++++++++-------------------------- text/controller.go | 30 +++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/format.go b/format.go index 4e28b15..4195c9c 100644 --- a/format.go +++ b/format.go @@ -20,36 +20,30 @@ func abs(i int) int { func date(t time.Time) []byte { dst := make([]byte, len("(D:YYYYMMDDHHmmSSOHH'mm)")) n := copy(dst, []byte("(D:")) - dst = itobuf(t.Year(), dst[n:n]) + itobuf(t.Year(), dst[n:n]) n += 4 // to be used for 0-padded digits - two := [2]byte{'0', '0'} + four := [4]byte{'0', '0'} + two := four[:2] - buf := itobuf(int64(t.Month()), two[:]) - n += copy(dst[n:], buf[len(buf)-2:]) - - buf = itobuf(int64(t.Day()), two[:]) - n += copy(dst[n:], buf[len(buf)-2:]) - - buf = itobuf(int64(t.Hour()), two[:]) - n += copy(dst[n:], buf[len(buf)-2:]) - - buf = itobuf(int64(t.Minute()), two[:]) - n += copy(dst[n:], buf[len(buf)-2:]) - - buf = itobuf(int64(t.Second()), two[:]) - n += copy(dst[n:], buf[len(buf)-2:]) + pad := func(i int64) int { + buf := strconv.AppendInt(two, i, 10) + return copy(dst[n:], buf[len(buf)-2:]) // should always be 2 + } + n += pad(int64(t.Month())) + n += pad(int64(t.Day())) + n += pad(int64(t.Hour())) + n += pad(int64(t.Minute())) + n += pad(int64(t.Second())) _, offset := t.Zone() - - if offset < 0 { + switch { + case offset < 0: dst[n] = '-' - - } else if offset == 0 { + case offset == 0: dst[n] = 'Z' - - } else { + default: dst[n] = '+' } n++ @@ -59,12 +53,10 @@ func date(t time.Time) []byte { offset -= hours * 60 * 60 minutes := offset / 60 - buf = itobuf(hours, two[:]) - n += copy(dst[n:], buf[len(buf)-2:]) + n += pad(int64(hours)) dst[n] = '\'' n++ - buf = itobuf(minutes, two[:]) - n += copy(dst[n:], buf[len(buf)-2:]) + n += pad(int64(minutes)) dst[n] = ')' return dst diff --git a/text/controller.go b/text/controller.go index e21a6b0..2314d8f 100644 --- a/text/controller.go +++ b/text/controller.go @@ -113,6 +113,28 @@ to the formatting directives contained in FormatText: */ type FormatText []rune +func cmpColor(a, b gdf.Color) bool { + switch v := a.(type) { + case gdf.RGBColor: + if rb, ok := b.(gdf.RGBColor); ok { + return v == rb + } + case gdf.CMYKColor: + if rb, ok := b.(gdf.CMYKColor); ok { + return v == rb + } + case gdf.GColor: + if rb, ok := b.(gdf.GColor); ok { + return v == rb + } + case nil: + if b == nil { + return true + } + } + return false +} + // NewController returns a Controller that is ready to write src to ContentStreams. It returns an invalid Controller // and an error if it encounters a problem while parsing and shaping src. lineWidth should be the maximum desired // width, in points, of each line of the output text when drawn to a gdf.ContentStream. @@ -183,7 +205,9 @@ func NewController(src FormatText, lineWidth float64, f FontFamily, cfg Controll // the position of c's TextCursor after the text has been drawn. (This value would not be otherwise accessible because // each call to DrawText encompasses a c.BeginText/EndText pair.) The returned bool indicates whether the Controller's // buffer still contains additional source text. If this value is true, then future calls to DrawText can be used to -// draw the remaining source text - usually to different areas or ContentStreams. +// draw the remaining source text - usually to different areas or ContentStreams. Changes made to c's +// stroking color, nonstroking color, or render mode by the Controller will continue to affect operations on c +// after DrawText returns, unless they are manually reverted. func (tc *Controller) DrawText(c *gdf.ContentStream, area gdf.Rect) (gdf.Point, bool, error) { if gdf.PtToFU(area.Width(), tc.fontSize) < tc.lineWidth { return *new(gdf.Point), false, ErrWidth @@ -204,10 +228,10 @@ func (tc *Controller) DrawText(c *gdf.ContentStream, area gdf.Rect) (gdf.Point, if c.Font != tc.curFont || c.FontSize != tc.fontSize { c.SetFont(tc.fontSize, tc.curFont) } - if c.NColor != tc.ncolor && tc.ncolor != nil { + if !cmpColor(c.NColor, tc.ncolor) && tc.ncolor != nil { c.SetColor(tc.ncolor) } - if c.SColor != tc.scolor && tc.scolor != nil { + if !cmpColor(c.SColor, tc.scolor) && tc.scolor != nil { c.SetColorStroke(tc.scolor) } et, err := c.BeginText()