Repository navigation
Expand file tree
/
Copy pathgenerateSVG.go
More file actions
1425 lines (1260 loc) · 51.9 KB
/
Copy pathgenerateSVG.go
File metadata and controls
1425 lines (1260 loc) · 51.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"encoding/base64"
"fmt"
"log"
"math"
"mime"
"os"
"path/filepath"
"regexp"
"strings"
)
// Constants (Consider moving some to LayoutOptions in Template)
const defaultFontSize = 12.0
const defaultFont = "Arial, sans-serif"
const imagePlaceholderHeight = 50.0 // Default height for images if not specified/calculable
const foreignObjectHeightEstimate = 100.0 // Default height for foreignObject (adjust as needed) - VERY ROUGH
// Structure to hold calculated bounds
type bounds struct {
minX, maxX, minY, maxY float64
isSet bool
}
// Update bounds considering a point (x, y)
func (b *bounds) updatePoint(x, y float64) {
if !b.isSet {
b.minX, b.maxX = x, x
b.minY, b.maxY = y, y
b.isSet = true
} else {
b.minX = math.Min(b.minX, x)
b.maxX = math.Max(b.maxX, x)
b.minY = math.Min(b.minY, y)
b.maxY = math.Max(b.maxY, y)
}
}
// Update bounds considering a rectangle
func (b *bounds) updateRect(x, y, width, height float64) {
if width > 0 && height > 0 {
b.updatePoint(x, y)
b.updatePoint(x+width, y+height)
}
}
// Parameter structs for functions with too many parameters
type ElementCenterParams struct {
AxisX float64
AxisY float64
MainOffset float64
CrossOffset float64
ConnectorLen float64
CrossDir float64
IsHorizontal bool
}
type JunctionMarkerParams struct {
Style JunctionMarkerStyle
CenterX float64
CenterY float64
MarkerColor string
IsHorizontal bool
CenterLineWidth float64
}
type CommentParams struct {
Style CommentTextStyle
AnchorX float64
AnchorY float64
CrossAxisDir float64
IsHorizontal bool
SegmentWidth float64
DefaultColor string
TitleText string
BodyText string
ImageURL string
}
// Add a new parameter struct for drawConnector
type ConnectorParams struct {
X1 float64
Y1 float64
X2 float64
Y2 float64
Style ConnectorStyle
SegmentColor string
IsHorizontal bool
CrossAxisDir float64
LineIsVisible bool
ElementCrossOffset float64 // Offset of the connected element (year/comment)
}
// Add a new parameter struct for drawYearShape
type YearShapeParams struct {
ShapeType string
ShapeParams map[string]float64
CenterX float64
CenterY float64
TextWidth float64
TextHeight float64
YearStyle YearTextStyle
}
// Add a parameter struct for drawConnectorDot
type ConnectorDotParams struct {
DotStyle DotStyle
P1x, P1y float64
P2x, P2y float64
DefaultColor string
IsHorizontal bool
CrossAxisDir float64
LineIsVisible bool
}
// Add a parameter struct for drawCenterLineSegment
type DrawCenterLineSegmentParams struct {
SVG *bytes.Buffer
Bounds *bounds
X1, Y1 float64
X2, Y2 float64
Color string
Width float64
LineType string
RoundedCaps bool
}
// Add a parameter struct for drawAndAdvanceAxisSegment
type DrawAndAdvanceAxisSegmentParams struct {
SVG *bytes.Buffer
Bounds *bounds
CurrentX, CurrentY float64
StartIndex int // -1 for initial segment, 0 to len(entries)-2 for others
Entries []TimelineEntry
Data TimelinePositionData
LayoutConfig LayoutConfig
BaseOrientation string
GlobalAxisAngle *float64
CenterLineType string
}
// calculateAxisGeometry determines the start/end coordinates and effective angle
// for a segment of the timeline axis based on orientation and angle overrides.
func calculateAxisGeometry(x1, y1, length float64, orientation string, globalAngle, overrideAngle *float64) (nx1, ny1, nx2, ny2, effectiveAngleDeg float64) {
// Determine base angle from orientation
var baseAngleDeg float64
if orientation == "vertical" {
baseAngleDeg = 90.0 // Upwards
} else {
baseAngleDeg = 0.0 // Rightwards (default horizontal)
}
// Determine the effective angle in degrees
effectiveAngleDeg = baseAngleDeg // Start with the orientation angle
if globalAngle != nil {
effectiveAngleDeg = *globalAngle // Override with global angle if set
}
if overrideAngle != nil {
effectiveAngleDeg = *overrideAngle // Override with entry-specific angle if set
}
// Convert effective angle to radians for trig functions
effectiveAngleRad := effectiveAngleDeg * math.Pi / 180.0
// Calculate end point coordinates
nx1 = x1
ny1 = y1
nx2 = x1 + length*math.Cos(effectiveAngleRad)
ny2 = y1 + length*math.Sin(effectiveAngleRad)
return nx1, ny1, nx2, ny2, effectiveAngleDeg
}
// --- Helper Functions for Timeline Generation ---
// LayoutConfig holds the configuration for timeline layout
type LayoutConfig struct {
layoutPadding float64
defaultEntrySpacing float64
defaultConnectorLength float64
centerLineBaseColor string
centerLineWidth float64
centerLineIsRounded bool
}
// TimelinePositionData holds pre-calculated data for timeline entries
type TimelinePositionData struct {
entryPoints []float64
junctionPoints []float64
segmentColors []string
markerStyles []JunctionMarkerStyle
connectorStyles []ConnectorStyle
yearStyles []YearTextStyle
commentStyles []CommentTextStyle
}
// CommentBlockLayout holds layout information for comment blocks
type CommentBlockLayout struct {
blockX, blockY float64 // Top-left corner of the visual block
visualBlockWidth float64 // Width of the visual block (content + L/R padding)
visualBlockHeight float64 // Height of the visual block (content + T/B padding)
contentCenterX float64 // Center X relative to content area
titleTextAbsY float64
titleLineAbsY float64
bodyAbsX, bodyAbsY float64 // Top-left corner of the foreignObject
foHeight float64 // Estimated height of content *within* foreignObject
// Parsed padding values
padTop, padRight, padBottom, padLeft float64
contentWidth float64 // Width available for content inside padding (FO width)
}
// Initialize layout configuration from template
func initializeLayoutConfig(template Template) LayoutConfig {
config := LayoutConfig{}
config.layoutPadding = template.Layout.Padding
if config.layoutPadding <= 0 {
config.layoutPadding = 50.0
}
config.defaultEntrySpacing = template.Layout.EntrySpacing
if config.defaultEntrySpacing <= 0 {
config.defaultEntrySpacing = 150.0
}
config.defaultConnectorLength = template.Layout.ConnectorLength
if config.defaultConnectorLength <= 0 {
config.defaultConnectorLength = 50.0
}
config.centerLineBaseColor = template.CenterLine.Color
if config.centerLineBaseColor == "" {
config.centerLineBaseColor = "#000000"
}
config.centerLineWidth = float64(template.CenterLine.Width)
if config.centerLineWidth <= 0 {
config.centerLineWidth = 2
}
config.centerLineIsRounded = template.CenterLine.RoundedCaps
return config
}
// Calculate timeline positions and styles
func calculateTimelinePositionsAndStyles(entries []TimelineEntry, template Template, config LayoutConfig) TimelinePositionData {
data := TimelinePositionData{
entryPoints: make([]float64, len(entries)),
junctionPoints: make([]float64, len(entries)+1),
segmentColors: make([]string, len(entries)),
markerStyles: make([]JunctionMarkerStyle, len(entries)),
connectorStyles: make([]ConnectorStyle, len(entries)),
yearStyles: make([]YearTextStyle, len(entries)),
commentStyles: make([]CommentTextStyle, len(entries)),
}
currentPos := 0.0
for i, entry := range entries {
// Spacing
spacing := config.defaultEntrySpacing
if entry.EntrySpacingOverride != nil {
spacing = *entry.EntrySpacingOverride
}
if spacing <= 0 {
spacing = config.defaultEntrySpacing
}
// Positions
data.junctionPoints[i] = currentPos
data.entryPoints[i] = currentPos + spacing/2.0
currentPos += spacing
// Styles
projStyle := getEffectiveCenterlineProjectionStyle(template.PeriodDefaults.CenterlineProjection, entry.CenterlineProjectionOverride)
data.segmentColors[i] = projStyle.Color
if data.segmentColors[i] == "" {
data.segmentColors[i] = config.centerLineBaseColor
}
data.markerStyles[i] = getEffectiveJunctionMarkerStyle(template.PeriodDefaults.JunctionMarker, entry.JunctionMarkerOverride)
data.connectorStyles[i] = getEffectiveConnectorStyle(template.PeriodDefaults.Connector, entry.ConnectorOverride)
data.yearStyles[i] = getEffectiveYearTextStyle(template.GlobalFont, template.PeriodDefaults.YearText, entry.YearTextOverride)
data.commentStyles[i] = getEffectiveCommentTextStyle(template.GlobalFont, template.PeriodDefaults.CommentText, entry.CommentTextOverride)
}
data.junctionPoints[len(entries)] = currentPos
return data
}
// Add a parameter struct for drawTimelineEntry
type TimelineEntryParams struct {
Index int
Entry TimelineEntry
Data TimelinePositionData
EntryAxisX float64 // X coordinate of the entry on the potentially angled axis
EntryAxisY float64 // Y coordinate of the entry on the potentially angled axis
IsHorizontal bool // True if base orientation is horizontal (for annotation direction)
Config LayoutConfig
}
// Update the drawTimelineEntry function to handle connectors correctly based on config
func drawTimelineEntry(svg *bytes.Buffer, bounds *bounds, params TimelineEntryParams) {
i := params.Index
entry := params.Entry
timelineData := params.Data
entryAxisX := params.EntryAxisX // Use the passed exact coordinates
entryAxisY := params.EntryAxisY // Use the passed exact coordinates
// Determine effective orientation for *this specific entry*
effectiveIsHorizontal := params.IsHorizontal // Start with global orientation
if entry.OrientationOverride != nil {
if *entry.OrientationOverride == "horizontal" {
effectiveIsHorizontal = true
} else if *entry.OrientationOverride == "vertical" {
effectiveIsHorizontal = false
}
// Ignore invalid override values, keep global default
}
config := params.Config
// --- Get Styles for this entry ---
connStyle := timelineData.connectorStyles[i]
commentStyle := timelineData.commentStyles[i]
yearStyle := timelineData.yearStyles[i]
markerStyle := timelineData.markerStyles[i]
segmentColor := timelineData.segmentColors[i] // Color of segment LEADING to this entry
// Determine cross-axis direction based on *effective* orientation
commentCrossAxisDir := 1.0
yearCrossAxisDir := -1.0
if i%2 != 0 { // Alternate sides
commentCrossAxisDir = -1.0
yearCrossAxisDir = 1.0
}
// Allow override for connector side, checking against *effective* orientation
if connStyle.Side != "" {
if (effectiveIsHorizontal && connStyle.Side == "top") || (!effectiveIsHorizontal && connStyle.Side == "left") {
commentCrossAxisDir = -1.0
yearCrossAxisDir = 1.0 // Year goes opposite comment
} else if (effectiveIsHorizontal && connStyle.Side == "bottom") || (!effectiveIsHorizontal && connStyle.Side == "right") {
commentCrossAxisDir = 1.0
yearCrossAxisDir = -1.0 // Year goes opposite comment
}
}
// --- Junction Marker ---
markerColor := determineMarkerColor(markerStyle, segmentColor, connStyle)
drawJunctionMarker(svg, bounds, JunctionMarkerParams{
Style: markerStyle,
CenterX: entryAxisX,
CenterY: entryAxisY,
MarkerColor: markerColor,
IsHorizontal: effectiveIsHorizontal, // Use effective orientation
CenterLineWidth: config.centerLineWidth,
})
// --- Year Element ---
// Calculate center based on axis point and *effective* orientation
yearCenterX, yearCenterY := calculateElementCenter(ElementCenterParams{
AxisX: entryAxisX,
AxisY: entryAxisY,
MainOffset: yearStyle.MainAxisOffset,
CrossOffset: yearStyle.CrossAxisOffset,
ConnectorLen: config.defaultConnectorLength,
CrossDir: yearCrossAxisDir,
IsHorizontal: effectiveIsHorizontal,
})
// --- Draw Connector to Year Element (Restored Logic) ---
drawPeriodLine := connStyle.DrawToPeriod == nil || *connStyle.DrawToPeriod
if drawPeriodLine {
drawConnector(svg, bounds, ConnectorParams{
X1: yearCenterX,
Y1: yearCenterY,
X2: entryAxisX,
Y2: entryAxisY,
Style: connStyle,
SegmentColor: segmentColor,
IsHorizontal: effectiveIsHorizontal,
CrossAxisDir: yearCrossAxisDir,
LineIsVisible: drawPeriodLine,
ElementCrossOffset: yearStyle.CrossAxisOffset,
})
}
// --- Draw Year Element itself ---
drawYearElement(svg, bounds, entry, yearStyle, yearCenterX, yearCenterY)
// --- Comment Element and Connector ---
if entry.CommentText != "" || entry.TitleText != "" || entry.CommentImage != "" {
// Calculate Comment Anchor Point using *effective* orientation
commentAnchorX, commentAnchorY := calculateElementCenter(ElementCenterParams{
AxisX: entryAxisX,
AxisY: entryAxisY,
MainOffset: commentStyle.MainAxisOffset,
CrossOffset: commentStyle.CrossAxisOffset,
ConnectorLen: config.defaultConnectorLength,
CrossDir: commentCrossAxisDir,
IsHorizontal: effectiveIsHorizontal,
})
// Calculate comment block layout based on the anchor point and *effective* orientation
blockLayout := calculateCommentBlockLayout(CommentParams{
Style: commentStyle,
AnchorX: commentAnchorX,
AnchorY: commentAnchorY,
CrossAxisDir: commentCrossAxisDir,
IsHorizontal: effectiveIsHorizontal,
SegmentWidth: config.defaultEntrySpacing,
DefaultColor: connStyle.Color,
TitleText: entry.TitleText,
BodyText: entry.CommentText,
ImageURL: entry.CommentImage,
})
// Determine comment edge point based on *effective* orientation
commentEdgeX, commentEdgeY := calculateCommentEdgePoint(blockLayout, commentCrossAxisDir, effectiveIsHorizontal)
// --- Draw Connector to comment using *effective* orientation
drawCommentLine := connStyle.DrawToComment == nil || *connStyle.DrawToComment
drawConnector(svg, bounds, ConnectorParams{
X1: commentEdgeX,
Y1: commentEdgeY,
X2: entryAxisX,
Y2: entryAxisY,
Style: connStyle,
SegmentColor: segmentColor,
IsHorizontal: effectiveIsHorizontal,
CrossAxisDir: commentCrossAxisDir,
LineIsVisible: drawCommentLine,
ElementCrossOffset: commentStyle.CrossAxisOffset,
})
// --- Draw Comment Block ---
drawComment(svg, bounds, CommentParams{
Style: commentStyle,
AnchorX: commentAnchorX,
AnchorY: commentAnchorY,
CrossAxisDir: commentCrossAxisDir,
IsHorizontal: effectiveIsHorizontal,
SegmentWidth: config.defaultEntrySpacing,
DefaultColor: connStyle.Color,
TitleText: entry.TitleText,
BodyText: entry.CommentText,
ImageURL: entry.CommentImage,
})
}
}
// --- Helper to find the edge point of the comment box ---
func calculateCommentEdgePoint(layout CommentBlockLayout, crossAxisDir float64, isHorizontal bool) (float64, float64) {
// Calculate the center of the edge facing the timeline axis
if isHorizontal {
if crossAxisDir < 0 { // Top edge center
return layout.blockX + layout.visualBlockWidth/2.0, layout.blockY
} else { // Bottom edge center
return layout.blockX + layout.visualBlockWidth/2.0, layout.blockY + layout.visualBlockHeight
}
} else { // Vertical
if crossAxisDir < 0 { // Left edge center
return layout.blockX, layout.blockY + layout.visualBlockHeight/2.0
} else { // Right edge center
return layout.blockX + layout.visualBlockWidth, layout.blockY + layout.visualBlockHeight/2.0
}
}
}
// Determine the color for a marker based on style and defaults
func determineMarkerColor(markerStyle JunctionMarkerStyle, segmentColor string, connStyle ConnectorStyle) string {
markerColor := segmentColor // Marker color matches current segment/connector color
if markerStyle.Color != nil { // Allow explicit override
markerColor = *markerStyle.Color
} else {
// Fallback further to connector color if segment color is bland?
if connStyle.Color != "" {
markerColor = connStyle.Color
}
}
return markerColor
}
// --- Helper function to determine connector line style attributes ---
func calculateConnectorStyleAttributes(style ConnectorStyle, segmentColor string) (string, float64, string) {
connDrawColor := style.Color
if connDrawColor == "" {
connDrawColor = segmentColor
}
connDrawWidth := float64(style.Width)
if connDrawWidth <= 0 {
connDrawWidth = 1
}
connDashArray := getStrokeDashArray(style.LineType, int(connDrawWidth))
return connDrawColor, connDrawWidth, connDashArray
}
// --- Helper function to calculate direction vectors for the connector ---
func calculateConnectorVectors(x1, y1, x2, y2 float64) (ux, uy, nx, ny, lineLen float64) {
// Vectors point FROM axis (X2,Y2) TOWARDS comment edge (X1,Y1)
dx := x1 - x2
dy := y1 - y2
lineLen = math.Sqrt(dx*dx + dy*dy)
ux, uy = 1.0, 0.0 // Default unit vector along line from X2 towards X1
nx, ny = 0.0, 1.0 // Default unit vector perpendicular
if lineLen > 0.001 {
ux = dx / lineLen
uy = dy / lineLen
nx = -uy // Perpendicular: Points left relative to direction X2->X1
ny = ux
}
return ux, uy, nx, ny, lineLen
}
// --- Helper function to calculate the absolute dot position ---
func calculateConnectorDotPosition(axisX, axisY, ux, uy, nx, ny float64, dotStyle DotStyle) (dotX, dotY float64) {
offsetMain := float64(dotStyle.OffsetMain)
offsetCross := float64(dotStyle.OffsetCross)
// Dot position = Axis Point + Main Offset along X2->X1 + Cross Offset perpendicular to X2->X1
dotX = axisX + ux*offsetMain + nx*offsetCross
dotY = axisY + uy*offsetMain + ny*offsetCross
return dotX, dotY
}
// --- Parameter struct for drawConnectorLineSegments ---
type ConnectorLineSegmentsParams struct {
SVG *bytes.Buffer
Bounds *bounds
ConnParams ConnectorParams // Original ConnectorParams for context
DotX, DotY float64 // Calculated dot position
Ux, Uy, Nx, Ny float64 // Direction vectors (Ux, Uy is Axis->Element; Nx, Ny is perpendicular)
DrawWidth float64
DrawColor string
DashArray string
}
// --- Helper function to draw the connector line segments ---
func drawConnectorLineSegments(params ConnectorLineSegmentsParams) {
// Only draw if the line is marked as visible in the original connector params
if !params.ConnParams.LineIsVisible {
return
}
dotStyle := params.ConnParams.Style.Dot
if !dotStyle.StopAtDot {
// Case 1: Line does NOT stop at dot - Draw straight line from element (X1,Y1) to axis point (X2,Y2)
fmt.Fprintf(params.SVG, ` <line x1="%.2f" y1="%.2f" x2="%.2f" y2="%.2f" stroke="%s" stroke-width="%.2f"%s />`,
params.ConnParams.X1, params.ConnParams.Y1, params.ConnParams.X2, params.ConnParams.Y2,
params.DrawColor, params.DrawWidth, params.DashArray)
params.SVG.WriteString("\n")
params.Bounds.updatePoint(params.ConnParams.X1, params.ConnParams.Y1)
params.Bounds.updatePoint(params.ConnParams.X2, params.ConnParams.Y2)
} else { // Case 2: Line STOPS at dot
// Determine if a dogleg is needed based on dot OR element offset
dotOffsetCross := float64(dotStyle.OffsetCross)
elementOffsetCross := params.ConnParams.ElementCrossOffset
isDogleg := math.Abs(dotOffsetCross) >= 0.001 || math.Abs(elementOffsetCross) >= 0.001
if isDogleg {
// Subcase 2a: Dogleg line Element(X1,Y1) -> midPoint -> Dot(DotX, DotY)
// Calculate midpoint (elbow) based on orientation:
var midPointX, midPointY float64
if params.ConnParams.IsHorizontal {
// Horizontal: Midpoint aligns vertically with Dot, horizontally with Element
midPointX = params.ConnParams.X1 // Same X as element
midPointY = params.DotY // Same Y as dot
} else {
// Vertical: Midpoint aligns horizontally with Dot, vertically with Element
midPointX = params.DotX // Same X as dot
midPointY = params.ConnParams.Y1 // Same Y as element
}
// Draw segment 1: Element (X1, Y1) to Midpoint
fmt.Fprintf(params.SVG, ` <line x1="%.2f" y1="%.2f" x2="%.2f" y2="%.2f" stroke="%s" stroke-width="%.2f"%s />`,
params.ConnParams.X1, params.ConnParams.Y1, midPointX, midPointY,
params.DrawColor, params.DrawWidth, params.DashArray)
params.SVG.WriteString("\n")
// Draw segment 2: Midpoint to Dot
fmt.Fprintf(params.SVG, ` <line x1="%.2f" y1="%.2f" x2="%.2f" y2="%.2f" stroke="%s" stroke-width="%.2f"%s />`,
midPointX, midPointY, params.DotX, params.DotY,
params.DrawColor, params.DrawWidth, params.DashArray)
params.SVG.WriteString("\n")
params.Bounds.updatePoint(params.ConnParams.X1, params.ConnParams.Y1)
params.Bounds.updatePoint(midPointX, midPointY)
params.Bounds.updatePoint(params.DotX, params.DotY)
} else {
// Subcase 2b: No dogleg, draw single line Element(X1, Y1) -> Dot(DotX, DotY)
// (Handle potential zero-length line by drawing to axis point if needed)
finalEndX, finalEndY := params.DotX, params.DotY
// Check if stopping at the dot would create a zero-length line between Element and Dot
isZeroLengthStop := math.Abs(params.DotX-params.ConnParams.X1) < 0.001 && math.Abs(params.DotY-params.ConnParams.Y1) < 0.001
if isZeroLengthStop {
// Avoid zero-length line: Draw to the original axis point instead
finalEndX, finalEndY = params.ConnParams.X2, params.ConnParams.Y2
}
// Draw the single line segment
fmt.Fprintf(params.SVG, ` <line x1="%.2f" y1="%.2f" x2="%.2f" y2="%.2f" stroke="%s" stroke-width="%.2f"%s />`,
params.ConnParams.X1, params.ConnParams.Y1, finalEndX, finalEndY,
params.DrawColor, params.DrawWidth, params.DashArray)
params.SVG.WriteString("\n")
params.Bounds.updatePoint(params.ConnParams.X1, params.ConnParams.Y1)
params.Bounds.updatePoint(finalEndX, finalEndY)
}
}
}
// --- Refactored drawConnector function ---
// Orchestrates drawing the connector by calling helper functions.
func drawConnector(svg *bytes.Buffer, bounds *bounds, params ConnectorParams) {
// 1. Calculate Style Attributes
connDrawColor, connDrawWidth, connDashArray := calculateConnectorStyleAttributes(params.Style, params.SegmentColor)
// 2. Calculate Direction Vectors (from axis X2,Y2 towards comment X1,Y1)
ux, uy, nx, ny, _ := calculateConnectorVectors(params.X1, params.Y1, params.X2, params.Y2)
// 3. Calculate Dot Position (relative to axis X2,Y2)
dotStyle := params.Style.Dot
dotX, dotY := calculateConnectorDotPosition(params.X2, params.Y2, ux, uy, nx, ny, dotStyle)
// 4. Draw Line Segment(s) if visible
drawConnectorLineSegments(ConnectorLineSegmentsParams{
SVG: svg,
Bounds: bounds,
ConnParams: params, // Pass original params for context
DotX: dotX,
DotY: dotY,
Ux: ux,
Uy: uy,
Nx: nx,
Ny: ny,
DrawWidth: connDrawWidth,
DrawColor: connDrawColor,
DashArray: connDashArray,
})
// 5. Draw the Dot itself (if visible)
drawConnectorDot(svg, bounds, ConnectorDotParams{
DotStyle: dotStyle,
P1x: params.X1, // Comment edge point
P1y: params.Y1,
P2x: params.X2, // Axis point
P2y: params.Y2,
DefaultColor: connDrawColor,
IsHorizontal: params.IsHorizontal,
CrossAxisDir: params.CrossAxisDir,
LineIsVisible: params.LineIsVisible,
}, dotX, dotY) // Pass calculated dot position
}
// --- Helper function to draw the connector dot ---
// Update parameters: Pass calculated dot center (dotX, dotY) and reference points for arrow logic (params includes P1x/y, P2x/y)
func drawConnectorDot(svg *bytes.Buffer, bounds *bounds, params ConnectorDotParams, dotX, dotY float64) {
// Check if the dot style itself is visible/valid (Line visibility checked before calling drawConnector)
if !params.DotStyle.Visible || params.DotStyle.Shape == "none" || params.DotStyle.Size <= 0 {
return
}
dotSize := float64(params.DotStyle.Size)
halfDotSize := dotSize / 2.0
dotColor := params.DotStyle.Color
if dotColor == "" {
dotColor = params.DefaultColor // Default to connector color
}
// Dot position (dotX, dotY) is pre-calculated
// Arrow logic uses reference points from params (P1x/y, P2x/y)
// Remove redundant calculations - only needed for arrow orientation logic below
// dx := params.P2x - params.P1x
// dy := params.P2y - params.P1y
// lineLen := math.Sqrt(dx*dx + dy*dy)
// nx, ny := 0.0, 1.0 // Default perpendicular vector
// if lineLen > 0.001 {
// // Normalized perpendicular vector: (-dy/lineLen, dx/lineLen)
// nx = -dy / lineLen
// ny = dx / lineLen
// }
switch params.DotStyle.Shape {
case "circle":
fmt.Fprintf(svg, ` <circle cx="%.2f" cy="%.2f" r="%.2f" fill="%s"/>\n`,
dotX, dotY, halfDotSize, dotColor)
case "square":
rectX := dotX - halfDotSize
rectY := dotY - halfDotSize
fmt.Fprintf(svg, ` <rect x="%.2f" y="%.2f" width="%.2f" height="%.2f" fill="%s"/>\n`,
rectX, rectY, dotSize, dotSize, dotColor)
case "arrow":
var p1xArrow, p1yArrow, p2xArrow, p2yArrow, tipX, tipY float64
// Arrow points towards the axis (determined by CrossAxisDir)
if params.IsHorizontal {
// Base is horizontal, relative to dotX, dotY
p1xArrow, p1yArrow = dotX-halfDotSize, dotY
p2xArrow, p2yArrow = dotX+halfDotSize, dotY
// Tip points vertically towards axis
tipX = dotX
tipY = dotY - params.CrossAxisDir*halfDotSize*1.2
} else { // Vertical timeline
// Base is vertical, relative to dotX, dotY
p1xArrow, p1yArrow = dotX, dotY-halfDotSize
p2xArrow, p2yArrow = dotX, dotY+halfDotSize
// Tip points horizontally towards axis
tipX = dotX - params.CrossAxisDir*halfDotSize*1.2
tipY = dotY
}
points := fmt.Sprintf("%.2f,%.2f %.2f,%.2f %.2f,%.2f", p1xArrow, p1yArrow, p2xArrow, p2yArrow, tipX, tipY)
fmt.Fprintf(svg, ` <polygon points="%s" fill="%s"/>\n`, points, dotColor)
}
// Update bounds for the dot itself
bounds.updateRect(dotX-halfDotSize, dotY-halfDotSize, dotSize, dotSize)
}
// Draw the year element with optional shape and link
func drawYearElement(svg *bytes.Buffer, bounds *bounds, entry TimelineEntry,
yearStyle YearTextStyle, centerX, centerY float64) {
yearStr := entry.Period
yearWidth, yearHeight := estimateTextSVGWidth(yearStr, yearStyle.Font), getEstimatedHeight(yearStyle.Font)
// --- Link Wrapper (around Year element) ---
if entry.Link != "" {
linkOpenTag := fmt.Sprintf(`<a xlink:href="%s">, escapeXML(entry.Link))
svg.WriteString(" " + linkOpenTag + "\n")
}
// Draw background shape
shapeType, shapeParams, err := parseShapeString(yearStyle.Shape)
if err != nil {
log.Printf("Warning: Error parsing shape string \"%s\" for year \"%s\": %v. Skipping shape.",
yearStyle.Shape, yearStr, err)
shapeType = "none"
}
drawYearShape(svg, YearShapeParams{
ShapeType: shapeType,
ShapeParams: shapeParams,
CenterX: centerX,
CenterY: centerY,
TextWidth: yearWidth,
TextHeight: yearHeight,
YearStyle: yearStyle,
})
// --- DEBUG LOGGING START ---
// // log.Printf("DEBUG drawYearElement (%s): CenterX=%.2f, CenterY=%.2f, Color=%s, Size=%d, Family=%s",
// // yearStr, centerX, centerY, yearStyle.TextColor, yearStyle.Font.FontSize, yearStyle.Font.FontFamily)
// --- DEBUG LOGGING END ---
// Draw the year text
fmt.Fprintf(svg, ` <text x="%.2f" y="%.2f" font-family="%s" font-size="%d" font-weight="%s" font-style="%s" fill="%s" dominant-baseline="middle" text-anchor="middle">`,
centerX, centerY, yearStyle.Font.FontFamily, yearStyle.Font.FontSize,
yearStyle.Font.FontWeight, yearStyle.Font.FontStyle, yearStyle.TextColor)
svg.WriteString(escapeXML(yearStr))
svg.WriteString(`</text>`)
svg.WriteString("\n")
// Update bounds for text
estWidth := math.Min(float64(len(yearStr))*float64(yearStyle.Font.FontSize)*0.7, 200)
estHeight := float64(yearStyle.Font.FontSize)
boundsX := centerX - estWidth/2.0
boundsY := centerY - estHeight/2.0
bounds.updateRect(boundsX, boundsY, estWidth, estHeight)
// Close link wrapper
if entry.Link != "" {
svg.WriteString(" </a>\n")
}
}
// Update the drawYearShape function to use the parameter struct
func drawYearShape(svg *bytes.Buffer, params YearShapeParams) {
switch params.ShapeType {
case "circle":
radius := params.ShapeParams["r"]
if radius < 0 { // Handle 'auto' radius
// Calculate radius based on text dimensions + default internal padding
const defaultAutoPadding = 4.0
textHalfWidth := params.TextWidth / 2.0
textHalfHeight := params.TextHeight / 2.0
radius = math.Max(textHalfWidth, textHalfHeight) + defaultAutoPadding
// Ensure minimum reasonable radius if text is tiny
if radius < defaultAutoPadding*1.5 {
radius = defaultAutoPadding * 1.5
}
} else if radius == 0 {
// If radius is explicitly 0, draw nothing
return
}
// Draw the circle
fmt.Fprintf(svg, ` <circle cx="%.2f" cy="%.2f" r="%.2f" fill="%s" stroke="%s" stroke-width="%.2f"/>`,
params.CenterX, params.CenterY, radius,
params.YearStyle.FillColor, params.YearStyle.BorderColor, params.YearStyle.BorderWidth)
svg.WriteString("\n")
case "rectangle":
rectW := params.ShapeParams["w"]
rectH := params.ShapeParams["h"]
if rectW > 0 && rectH > 0 {
rectX := params.CenterX - rectW/2.0
rectY := params.CenterY - rectH/2.0
fmt.Fprintf(svg, ` <rect x="%.2f" y="%.2f" width="%.2f" height="%.2f" fill="%s" stroke="%s" stroke-width="%.2f"/>`,
rectX, rectY, rectW, rectH,
params.YearStyle.FillColor, params.YearStyle.BorderColor, params.YearStyle.BorderWidth)
svg.WriteString("\n")
}
}
}
// Calculate the layout for a comment block
func calculateCommentBlockLayout(params CommentParams) CommentBlockLayout {
layout := CommentBlockLayout{}
// --- Parse Padding ---
padTop, padRight, padBottom, padLeft := parsePadding(params.Style.Padding)
layout.padTop, layout.padRight, layout.padBottom, layout.padLeft = padTop, padRight, padBottom, padLeft
// --- Calculate Vertical Positions & Estimated Heights (Relative to Block Top - Inside Padding) ---
currentRelY := padTop // Start drawing below top padding
var titleLineRelY, titleTextRelY, bodyRelY float64
var estTitleHeight, estLineHeight float64
titleFont := params.Style.TitleFont
titleLine := params.Style.TitleLine
// Title Text Position & Height
estTitleWidth := 0.0
if params.TitleText != "" {
titleTextRelY = currentRelY
estTitleHeight = getEstimatedHeight(titleFont)
estTitleWidth = estimateTextSVGWidth(params.TitleText, titleFont)
currentRelY += estTitleHeight
} else {
estTitleHeight = 0
}
// Title Line Position & Height
if titleLine.Visible && titleLine.Width > 0 && titleLine.Length > 0 {
currentRelY += titleLine.Margin
titleLineRelY = currentRelY
estLineHeight = titleLine.Width
currentRelY += estLineHeight
currentRelY += titleLine.Margin
// Consider title line length for width calculation
estTitleWidth = math.Max(estTitleWidth, titleLine.Length)
} else {
estLineHeight = 0
}
// Body Position (foreignObject Y relative to Block Top)
bodyRelY = currentRelY
// Estimate foreignObject height (content only, no padding)
layout.foHeight = calculateForeignObjectHeight(params.BodyText, params.ImageURL)
// --- Calculate Visual Block Dimensions ---
requiredContentWidth := estTitleWidth // Base width on title/line
// Check for fixed block width from style
if params.Style.BlockWidth != nil && *params.Style.BlockWidth > 0 {
layout.contentWidth = *params.Style.BlockWidth // Use specified width for content
} else {
// Fallback: Use title/line width as content width (current behavior)
layout.contentWidth = requiredContentWidth
}
// Ensure content width is not negative
if layout.contentWidth < 0 {
layout.contentWidth = 0
}
// Calculate visual block width including padding
layout.visualBlockWidth = layout.contentWidth + padLeft + padRight
// Calculate visual block height (unchanged)
layout.visualBlockHeight = currentRelY + layout.foHeight + padBottom // Includes top padding, content, bottom padding
// --- Calculate Block Position (Top-Left Corner of Visual Block) ---
layout.blockX, layout.blockY = calculateBlockPosition(params.AnchorX, params.AnchorY,
layout.visualBlockWidth, layout.visualBlockHeight,
params.CrossAxisDir, params.IsHorizontal)
// --- Calculate Absolute Content Positions (relative to SVG origin) ---
layout.contentCenterX = layout.blockX + padLeft + layout.contentWidth/2.0
layout.titleTextAbsY = layout.blockY + titleTextRelY
layout.titleLineAbsY = layout.blockY + titleLineRelY
layout.bodyAbsY = layout.blockY + bodyRelY
layout.bodyAbsX = layout.blockX + padLeft // Body/FO starts after left padding
return layout
}
// Calculate height needed for foreignObject content
func calculateForeignObjectHeight(bodyText, imageURL string) float64 {
foHeight := foreignObjectHeightEstimate
if bodyText == "" && imageURL == "" {
foHeight = 0
} else if bodyText == "" && imageURL != "" {
foHeight = imagePlaceholderHeight + 10 // Rough estimate for image only
}
return foHeight
}
// Calculate the position of a comment block based on anchor and direction
func calculateBlockPosition(anchorX, anchorY, blockWidth, totalHeight, crossAxisDir float64, isHorizontal bool) (float64, float64) {
var blockX, blockY float64
if isHorizontal {
blockX = anchorX - blockWidth/2.0 // Horizontal centering relative to anchorX
if crossAxisDir < 0 { // Block is ABOVE the anchor point (e.g., horizontal top)
// Position block so its BOTTOM edge is at anchorY
blockY = anchorY - totalHeight // Correct: Top edge = AnchorY - Full Height
} else { // Block is BELOW the anchor point (e.g., horizontal bottom)
// Position block so its TOP edge is at anchorY
blockY = anchorY
}
} else {
blockY = anchorY - totalHeight/2.0 // Vertical centering relative to anchorY
if crossAxisDir < 0 { // Block is LEFT of the anchor point (e.g., vertical left)
// Position block so its RIGHT edge is at anchorX
blockX = anchorX - blockWidth // Adjust based on total height
} else { // Block is RIGHT of the anchor point (e.g., vertical right)
// Position block so its LEFT edge is at anchorX
blockX = anchorX
}
}
return blockX, blockY
}
// Draw the background rectangle for a comment
func drawCommentBackground(svg *bytes.Buffer, bounds *bounds, style CommentTextStyle, layout CommentBlockLayout) {
if style.Shape == "rectangle" {
// Use the calculated visual block dimensions and position
rectX := layout.blockX
rectY := layout.blockY
rectW := layout.visualBlockWidth
rectH := layout.visualBlockHeight
rectFill := style.FillColor
if rectFill == "" {
rectFill = "none"
}
rectBorderColor := style.BorderColor
if rectBorderColor == "" {
rectBorderColor = "none"
}
rectBorderWidth := float64(style.BorderWidth)
if rectBorderWidth < 0 {
rectBorderWidth = 0
}
rectBorderStyle := style.BorderStyle
rectBorderDashArray := getStrokeDashArray(rectBorderStyle, int(rectBorderWidth))
fmt.Fprintf(svg, ` <rect x="%.2f" y="%.2f" width="%.2f" height="%.2f" fill="%s" stroke="%s" stroke-width="%.2f"%s rx="3" ry="3"/>`,
rectX, rectY, rectW, rectH, rectFill, rectBorderColor, rectBorderWidth, rectBorderDashArray)
svg.WriteString("\n")
bounds.updateRect(rectX, rectY, rectW, rectH)
}
}
// Add parameter structs for remaining functions with too many parameters
type CommentTitleParams struct {
TitleText string
TitleFont FontStyle
TitleColor string
Layout CommentBlockLayout
}
type CommentBodyParams struct {
Params CommentParams
BodyFont FontStyle
TextColor string
Layout CommentBlockLayout
}
// Update drawCommentTitle to use the parameter struct
func drawCommentTitle(svg *bytes.Buffer, bounds *bounds, params CommentTitleParams) {
fmt.Fprintf(svg, ` <text x="%.2f" y="%.2f" font-family="%s" font-size="%d" font-weight="%s" font-style="%s" fill="%s" text-anchor="middle" dominant-baseline="hanging">`,
params.Layout.contentCenterX, params.Layout.titleTextAbsY, params.TitleFont.FontFamily, params.TitleFont.FontSize,
params.TitleFont.FontWeight, params.TitleFont.FontStyle, params.TitleColor)
svg.WriteString(escapeXML(params.TitleText))
svg.WriteString(`</text>`)
svg.WriteString("\n")
bounds.updatePoint(params.Layout.contentCenterX, params.Layout.titleTextAbsY) // Approximate bounds update
}
// Helper function to get MIME type from file extension
func getMimeType(filename string) string {
ext := strings.ToLower(filepath.Ext(filename))
mimeType := mime.TypeByExtension(ext)
if mimeType == "" { // Fallback if mime package doesn't know
switch ext {
case ".jpg", ".jpeg":
return "image/jpeg"