Skip to content

Commit

Permalink
fixed date formatting and color comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
cdillond committed Sep 12, 2024
1 parent 3fb1223 commit ae85ad8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
44 changes: 18 additions & 26 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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

Expand Down
30 changes: 27 additions & 3 deletions text/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit ae85ad8

Please sign in to comment.