-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGentlePDFExporter.swift
More file actions
976 lines (818 loc) · 38.8 KB
/
GentlePDFExporter.swift
File metadata and controls
976 lines (818 loc) · 38.8 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
// Jonathan Ritchey
import UIKit
import SwiftUI
// MARK: - PDF Export Error
public enum GentlePDFExportError: Error, LocalizedError {
case failedToCreatePDF
case failedToWriteFile(Error)
public var errorDescription: String? {
switch self {
case .failedToCreatePDF:
return "Failed to create PDF document"
case .failedToWriteFile(let error):
return "Failed to write PDF file: \(error.localizedDescription)"
}
}
}
// MARK: - PDF Exporter
@MainActor
public enum GentlePDFExporter {
// MARK: - Layout Constants
private static let pageWidth: CGFloat = 620
private static let margin: CGFloat = 24
private static var contentWidth: CGFloat { pageWidth - (margin * 2) }
private static let sectionSpacing: CGFloat = 24
private static let itemSpacing: CGFloat = 10
private static let gridGap: CGFloat = 10
// MARK: - Typography Constants
private static let sectionTitleFontSize: CGFloat = 18
private static let labelFontSize: CGFloat = 10
// MARK: - Label Color
/// Gentle dark blue at 70% opacity for all role/token labels
private static let labelColor = UIColor(red: 0.2, green: 0.3, blue: 0.5, alpha: 0.7)
/// Returns the appropriate primary text color for displaying text on a given surface
private static func textColor(for surfaceRole: GentleSurfaceRole, spec: GentleDesignSystemSpec, scheme: ColorScheme) -> UIColor {
let colorRole: GentleColorRole
switch surfaceRole {
case .overlayScrim:
colorRole = .textOnScrim
case .overlaySheet, .overlayPopover:
colorRole = .textOnOverlay
default:
return labelColor
}
let hex = scheme == .dark
? spec.colors.pairByRole[colorRole.rawValue]?.darkHex
: spec.colors.pairByRole[colorRole.rawValue]?.lightHex
return hex.map { uiColorFromHex($0) } ?? labelColor
}
/// Returns the appropriate secondary text color for displaying text on a given surface
private static func secondaryTextColor(for surfaceRole: GentleSurfaceRole, spec: GentleDesignSystemSpec, scheme: ColorScheme) -> UIColor {
let colorRole: GentleColorRole
switch surfaceRole {
case .overlayScrim:
colorRole = .textOnScrimSecondary
case .overlaySheet, .overlayPopover:
colorRole = .textOnOverlaySecondary
default:
return labelColor
}
let hex = scheme == .dark
? spec.colors.pairByRole[colorRole.rawValue]?.darkHex
: spec.colors.pairByRole[colorRole.rawValue]?.lightHex
return hex.map { uiColorFromHex($0) } ?? labelColor
}
// MARK: - Public API
/// Generates PDF data for the given design system spec on a single sheet.
public static func generatePDFData(for spec: GentleDesignSystemSpec, themeName: String? = nil) throws -> Data {
// Calculate required height
let totalHeight = calculateTotalHeight(for: spec)
let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: totalHeight)
let renderer = UIGraphicsPDFRenderer(bounds: pageRect)
let data = renderer.pdfData { context in
context.beginPage()
var currentY: CGFloat = margin
// Draw Typography section
currentY = drawTypographySection(in: context.cgContext, spec: spec, startY: currentY)
currentY += sectionSpacing
// Draw Buttons section
currentY = drawButtonsSection(in: context.cgContext, spec: spec, startY: currentY)
currentY += sectionSpacing
// Draw Surfaces section
currentY = drawSurfacesSection(in: context.cgContext, spec: spec, startY: currentY)
currentY += sectionSpacing
// Draw Colors section
_ = drawColorsSection(in: context.cgContext, spec: spec, startY: currentY)
}
return data
}
/// Exports the PDF to a temporary file and returns its URL.
public static func exportPDFURL(for spec: GentleDesignSystemSpec, themeName: String? = nil) throws -> URL {
let data = try generatePDFData(for: spec, themeName: themeName)
let tempDir = FileManager.default.temporaryDirectory
let timestamp = formattedTimestamp()
let fileName = "GentleDesignSystem_\(timestamp).pdf"
let url = tempDir.appendingPathComponent(fileName)
do {
try data.write(to: url, options: [.atomic])
return url
} catch {
throw GentlePDFExportError.failedToWriteFile(error)
}
}
// MARK: - Height Calculation
private static func calculateTotalHeight(for spec: GentleDesignSystemSpec) -> CGFloat {
var height: CGFloat = margin
// Typography: 4 columns, rows sized for large fonts
let typographyRows = ceil(Double(GentleTextRole.allCases.count) / 4.0)
height += sectionTitleFontSize + 12 + (CGFloat(typographyRows) * 62) + 14 // 58 cell + 4 gap
height += sectionSpacing
// Buttons: 3 rows of buttons sized for 17pt text
height += sectionTitleFontSize + 12 + 40 + gridGap + 40 + gridGap + 40 + 20
height += sectionSpacing
// Surfaces: 3 columns
let surfaceCount = GentleSurfaceRole.allCases.filter { $0 != .appBackground }.count
let surfaceRows = ceil(Double(surfaceCount) / 3.0)
height += sectionTitleFontSize + 12 + (CGFloat(surfaceRows) * 75)
height += sectionSpacing
// Colors: 4 columns
let colorRows = ceil(Double(GentleColorRole.allCases.count) / 4.0)
height += sectionTitleFontSize + 12 + (CGFloat(colorRows) * 32)
height += margin
return height
}
// MARK: - Typography Section
private static func drawTypographySection(in cgContext: CGContext, spec: GentleDesignSystemSpec, startY: CGFloat) -> CGFloat {
var y = startY
// Section title
y = drawSectionTitle("Typography", in: cgContext, at: y)
y += 12
// Draw container card
let roles = GentleTextRole.allCases
let columns = 4
let rows = Int(ceil(Double(roles.count) / Double(columns)))
let cellWidth = (contentWidth - CGFloat(columns - 1) * gridGap - 20) / CGFloat(columns)
let cellHeight: CGFloat = 58 // Accommodate large fonts like largeTitle_xxl (34pt)
let rowGap: CGFloat = 4
let containerHeight = CGFloat(rows) * cellHeight + CGFloat(rows - 1) * rowGap + 14
let containerRect = CGRect(x: margin, y: y, width: contentWidth, height: containerHeight)
drawContainerCard(in: cgContext, rect: containerRect, spec: spec)
// Draw typography items in grid
for (index, role) in roles.enumerated() {
let col = index % columns
let row = index / columns
let cellX = margin + 10 + CGFloat(col) * (cellWidth + gridGap)
let cellY = y + 8 + CGFloat(row) * (cellHeight + rowGap)
drawTypographyItem(in: cgContext, spec: spec, role: role, at: CGPoint(x: cellX, y: cellY), width: cellWidth)
}
return y + containerHeight
}
private static func drawTypographyItem(in cgContext: CGContext, spec: GentleDesignSystemSpec, role: GentleTextRole, at point: CGPoint, width: CGFloat) {
let roleSpec = spec.typography.roleSpec(for: role)
// Role name label - with minimum scale factor
let baseLabelFontSize: CGFloat = 12
let minScaleFactor: CGFloat = 0.5
// Try full size first, then scale down if needed
var labelFontSize = baseLabelFontSize
var labelFont = UIFont.systemFont(ofSize: labelFontSize, weight: .regular)
var labelAttrs: [NSAttributedString.Key: Any] = [
.font: labelFont,
.foregroundColor: Self.labelColor
]
var labelString = NSAttributedString(string: role.rawValue, attributes: labelAttrs)
var labelSize = labelString.size()
// Scale down if needed, respecting minimum scale factor
if labelSize.width > width {
let scaleFactor = max(width / labelSize.width, minScaleFactor)
labelFontSize = baseLabelFontSize * scaleFactor
labelFont = UIFont.systemFont(ofSize: labelFontSize, weight: .regular)
labelAttrs[.font] = labelFont
labelString = NSAttributedString(string: role.rawValue, attributes: labelAttrs)
}
labelString.draw(at: point)
// Sample "Aa Bb" in actual font style using the theme's point size, design, and width
let sampleFont = buildUIFont(from: roleSpec)
let sampleColor = colorFromSpec(spec, role: .textPrimary)
let sampleAttrs: [NSAttributedString.Key: Any] = [
.font: sampleFont,
.foregroundColor: sampleColor
]
let sampleString = NSAttributedString(string: "Aa Bb", attributes: sampleAttrs)
sampleString.draw(at: CGPoint(x: point.x, y: point.y + labelFont.lineHeight))
}
// MARK: - Buttons Section
private static func drawButtonsSection(in cgContext: CGContext, spec: GentleDesignSystemSpec, startY: CGFloat) -> CGFloat {
var y = startY
// Section title
y = drawSectionTitle("Buttons", in: cgContext, at: y)
y += 12
// Container card - sized for 17pt button text
let containerHeight: CGFloat = 40 + gridGap + 40 + gridGap + 40 + 20
let containerRect = CGRect(x: margin, y: y, width: contentWidth, height: containerHeight)
drawContainerCard(in: cgContext, rect: containerRect, spec: spec)
let columns = 4
let buttonGap: CGFloat = 10
let containerPadding: CGFloat = 10
let buttonWidth = (contentWidth - containerPadding * 2 - CGFloat(columns - 1) * buttonGap) / CGFloat(columns)
let buttonHeight: CGFloat = 36
let startX = margin + containerPadding
// Row 1: Primary, Secondary (light/dark variants)
var rowY = y + 12
let row1Buttons: [(GentleButtonRole, Bool)] = [
(.primary, false),
(.primary, true),
(.secondary, false),
(.secondary, true)
]
for (index, (role, isDark)) in row1Buttons.enumerated() {
let buttonX = startX + CGFloat(index) * (buttonWidth + buttonGap)
let buttonRect = CGRect(x: buttonX, y: rowY, width: buttonWidth, height: buttonHeight)
drawButtonPreview(in: cgContext, rect: buttonRect, role: role, spec: spec, isDarkMode: isDark)
}
// Row 2: Tertiary, Quaternary (light/dark variants)
rowY += buttonHeight + gridGap
let row2Buttons: [(GentleButtonRole, Bool)] = [
(.tertiary, false),
(.tertiary, true),
(.quaternary, false),
(.quaternary, true)
]
for (index, (role, isDark)) in row2Buttons.enumerated() {
let buttonX = startX + CGFloat(index) * (buttonWidth + buttonGap)
let buttonRect = CGRect(x: buttonX, y: rowY, width: buttonWidth, height: buttonHeight)
drawButtonPreview(in: cgContext, rect: buttonRect, role: role, spec: spec, isDarkMode: isDark)
}
// Row 3: Destructive (light/dark)
rowY += buttonHeight + gridGap
let destructiveLightRect = CGRect(x: startX, y: rowY, width: buttonWidth, height: buttonHeight)
drawButtonPreview(in: cgContext, rect: destructiveLightRect, role: .destructive, spec: spec, isDarkMode: false)
let destructiveDarkRect = CGRect(x: startX + buttonWidth + buttonGap, y: rowY, width: buttonWidth, height: buttonHeight)
drawButtonPreview(in: cgContext, rect: destructiveDarkRect, role: .destructive, spec: spec, isDarkMode: true)
return y + containerHeight
}
private static func drawButtonPreview(in cgContext: CGContext, rect: CGRect, role: GentleButtonRole, spec: GentleDesignSystemSpec, isDarkMode: Bool) {
let roleSpec = spec.buttons.roleSpec(for: role)
let cornerRadius: CGFloat = roleSpec.shape == .pill ? rect.height / 2 : 8
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
// Get colors
let primaryCTA = spec.colors.pair(for: .primaryCTA)
let destructive = spec.colors.pair(for: .destructive)
let borderSubtle = spec.colors.pair(for: .borderSubtle)
// Fill
let fillColor: UIColor
switch roleSpec.fillRole {
case .solidFillPrimaryCTA:
let hex = isDarkMode ? (primaryCTA?.darkHex ?? "#3B82F6") : (primaryCTA?.lightHex ?? "#4A6EF5")
fillColor = uiColorFromHex(hex)
case .solidFillDestructive:
let hex = isDarkMode ? (destructive?.darkHex ?? "#F87171") : (destructive?.lightHex ?? "#E35D5B")
fillColor = uiColorFromHex(hex)
case .hollow:
fillColor = UIColor.clear
}
cgContext.setFillColor(fillColor.cgColor)
cgContext.addPath(path.cgPath)
cgContext.fillPath()
// Border
if roleSpec.borderRole != .hidden {
let borderColor: UIColor
switch roleSpec.borderRole {
case .accent:
let hex = isDarkMode ? (primaryCTA?.darkHex ?? "#3B82F6") : (primaryCTA?.lightHex ?? "#4A6EF5")
borderColor = uiColorFromHex(hex)
case .subtle:
let hex = isDarkMode ? (borderSubtle?.darkHex ?? "#374151") : (borderSubtle?.lightHex ?? "#E5E7EB")
borderColor = uiColorFromHex(hex)
case .hidden:
borderColor = UIColor.clear
}
cgContext.setStrokeColor(borderColor.cgColor)
cgContext.setLineWidth(1)
cgContext.addPath(path.cgPath)
cgContext.strokePath()
}
// Button label - use the typography spec for this button role's text role
let textRole = role.defaultTextRole
let typographySpec = spec.typography.roleSpec(for: textRole)
let labelFont = buildUIFont(from: typographySpec)
let labelColor: UIColor
switch roleSpec.fillRole {
case .solidFillPrimaryCTA, .solidFillDestructive:
labelColor = UIColor.white
case .hollow:
let hex = isDarkMode ? (primaryCTA?.darkHex ?? "#3B82F6") : (primaryCTA?.lightHex ?? "#4A6EF5")
labelColor = uiColorFromHex(hex)
}
let labelAttrs: [NSAttributedString.Key: Any] = [
.font: labelFont,
.foregroundColor: labelColor
]
let labelText = role.rawValue.capitalized
let labelString = NSAttributedString(string: labelText, attributes: labelAttrs)
let labelSize = labelString.size()
let labelX = rect.midX - labelSize.width / 2
let labelY = rect.midY - labelSize.height / 2
labelString.draw(at: CGPoint(x: labelX, y: labelY))
}
// MARK: - Surfaces Section
private static func drawSurfacesSection(in cgContext: CGContext, spec: GentleDesignSystemSpec, startY: CGFloat) -> CGFloat {
var y = startY
// Section title
y = drawSectionTitle("Surfaces", in: cgContext, at: y)
y += 12
// Filter out appBackground since it's not really a card-style surface
let surfaceRoles = GentleSurfaceRole.allCases.filter { $0 != .appBackground }
let columns = 3
let rows = Int(ceil(Double(surfaceRoles.count) / Double(columns)))
let cellWidth = (contentWidth - CGFloat(columns - 1) * gridGap - 20) / CGFloat(columns)
let cellHeight: CGFloat = 55
let containerHeight = CGFloat(rows) * (cellHeight + gridGap) - gridGap + 20
let containerRect = CGRect(x: margin, y: y, width: contentWidth, height: containerHeight)
drawContainerCard(in: cgContext, rect: containerRect, spec: spec)
for (index, role) in surfaceRoles.enumerated() {
let col = index % columns
let row = index / columns
let cellX = margin + 10 + CGFloat(col) * (cellWidth + gridGap)
let cellY = y + 10 + CGFloat(row) * (cellHeight + gridGap)
drawSurfaceItem(in: cgContext, spec: spec, role: role, rect: CGRect(x: cellX, y: cellY, width: cellWidth, height: cellHeight))
}
return y + containerHeight
}
private static func drawSurfaceItem(in cgContext: CGContext, spec: GentleDesignSystemSpec, role: GentleSurfaceRole, rect: CGRect) {
let roleSpec = spec.surfaces.roleSpec(for: role)
// Use actual corner radius, capped to half the smallest dimension
let maxRadius = min(rect.width, rect.height) / 2
let cornerRadius = min(CGFloat(roleSpec.cornerRadius), maxRadius)
// Check if this is a material or glass surface that needs SwiftUI rendering
let needsSwiftUIRendering: Bool
switch roleSpec.backgroundStyle {
case .solid:
needsSwiftUIRendering = false
case .material, .glass:
needsSwiftUIRendering = true
}
if needsSwiftUIRendering {
// Render using SwiftUI with actual materials
if let surfaceImage = renderSurfaceWithSwiftUI(spec: spec, role: role, size: rect.size) {
// Draw shadow first if needed
if roleSpec.shadowRadius > 0 && roleSpec.shadowOpacity > 0 {
cgContext.saveGState()
let shadowColor = UIColor.black.withAlphaComponent(CGFloat(roleSpec.shadowOpacity))
cgContext.setShadow(
offset: CGSize(width: CGFloat(roleSpec.shadowOffsetX), height: CGFloat(roleSpec.shadowOffsetY)),
blur: CGFloat(roleSpec.shadowRadius),
color: shadowColor.cgColor
)
// Draw a filled shape to cast the shadow
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
cgContext.setFillColor(UIColor.white.cgColor)
cgContext.addPath(path.cgPath)
cgContext.fillPath()
cgContext.restoreGState()
}
// Draw the rendered SwiftUI image
surfaceImage.draw(in: rect)
} else {
// Fallback to simple rendering if SwiftUI rendering fails
drawSolidSurface(in: cgContext, spec: spec, role: role, roleSpec: roleSpec, rect: rect, cornerRadius: cornerRadius)
}
} else {
// Use simple Core Graphics rendering for solid surfaces
drawSolidSurface(in: cgContext, spec: spec, role: role, roleSpec: roleSpec, rect: rect, cornerRadius: cornerRadius)
}
// Role name - with minimum scale factor
// Use appropriate text color based on surface type (overlay surfaces need special colors)
let primaryTextColor = textColor(for: role, spec: spec, scheme: .light)
let secondaryTextColor = secondaryTextColor(for: role, spec: spec, scheme: .light)
let labelInset: CGFloat = 16
let availableWidth = rect.width - labelInset * 2
let baseLabelFontSize: CGFloat = 12
let minScaleFactor: CGFloat = 0.5
var labelFontSize = baseLabelFontSize
var labelFont = UIFont.systemFont(ofSize: labelFontSize, weight: .medium)
var labelAttrs: [NSAttributedString.Key: Any] = [
.font: labelFont,
.foregroundColor: primaryTextColor
]
var labelString = NSAttributedString(string: role.rawValue, attributes: labelAttrs)
var labelSize = labelString.size()
// Scale down if needed
if labelSize.width > availableWidth {
let scaleFactor = max(availableWidth / labelSize.width, minScaleFactor)
labelFontSize = baseLabelFontSize * scaleFactor
labelFont = UIFont.systemFont(ofSize: labelFontSize, weight: .medium)
labelAttrs[.font] = labelFont
labelString = NSAttributedString(string: role.rawValue, attributes: labelAttrs)
}
labelString.draw(at: CGPoint(x: rect.minX + labelInset, y: rect.minY + 10))
// Subtitle based on type
let subtitle: String
switch roleSpec.backgroundStyle {
case .solid:
subtitle = roleSpec.borderWidth > 0 ? "Subtle border" : "Solid"
case .material:
subtitle = "Material blur"
case .glass:
subtitle = "Glass effect"
}
let subtitleFont = UIFont.systemFont(ofSize: 10, weight: .regular)
let subtitleAttrs: [NSAttributedString.Key: Any] = [
.font: subtitleFont,
.foregroundColor: secondaryTextColor
]
let subtitleString = NSAttributedString(string: subtitle, attributes: subtitleAttrs)
subtitleString.draw(at: CGPoint(x: rect.minX + labelInset, y: rect.minY + 10 + labelFont.lineHeight + 1))
}
/// Draws a solid surface using Core Graphics (for non-material/glass surfaces)
private static func drawSolidSurface(in cgContext: CGContext, spec: GentleDesignSystemSpec, role: GentleSurfaceRole, roleSpec: GentleSurfaceRoleSpec, rect: CGRect, cornerRadius: CGFloat) {
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
// Background
let fillColor: UIColor
switch roleSpec.backgroundStyle {
case .solid(let colorRole):
fillColor = colorFromSpec(spec, role: colorRole)
case .material(_, let tintColorRole, _):
if let tintRole = tintColorRole {
fillColor = colorFromSpec(spec, role: tintRole).withAlphaComponent(0.15)
} else {
fillColor = UIColor(white: 0.96, alpha: 1.0)
}
case .glass(_, let fallbackColorRole):
fillColor = colorFromSpec(spec, role: fallbackColorRole).withAlphaComponent(0.3)
}
// Draw shadow if specified
if roleSpec.shadowRadius > 0 && roleSpec.shadowOpacity > 0 {
cgContext.saveGState()
let shadowColor = UIColor.black.withAlphaComponent(CGFloat(roleSpec.shadowOpacity))
cgContext.setShadow(
offset: CGSize(width: CGFloat(roleSpec.shadowOffsetX), height: CGFloat(roleSpec.shadowOffsetY)),
blur: CGFloat(roleSpec.shadowRadius),
color: shadowColor.cgColor
)
cgContext.setFillColor(fillColor.cgColor)
cgContext.addPath(path.cgPath)
cgContext.fillPath()
cgContext.restoreGState()
} else {
cgContext.setFillColor(fillColor.cgColor)
cgContext.addPath(path.cgPath)
cgContext.fillPath()
}
// Border
if roleSpec.borderWidth > 0 {
let borderColor = uiColorFromHex(roleSpec.border.lightHex)
cgContext.setStrokeColor(borderColor.cgColor)
cgContext.setLineWidth(CGFloat(roleSpec.borderWidth))
cgContext.addPath(path.cgPath)
cgContext.strokePath()
}
}
/// Renders a surface using SwiftUI and captures it as a UIImage
private static func renderSurfaceWithSwiftUI(spec: GentleDesignSystemSpec, role: GentleSurfaceRole, size: CGSize) -> UIImage? {
let theme = GentleTheme(defaultSpec: spec, editableSpec: spec)
// Create a SwiftUI view that renders the surface with a gradient background to show the blur
let surfaceView = SurfacePreviewView(role: role, size: size)
// Use ImageRenderer to capture the view
let renderer = ImageRenderer(content:
GentleThemeRoot(theme: theme) {
surfaceView
}
.environment(\.colorScheme, .light)
)
renderer.scale = 8.0 // 8x scale for crisp PDF output
return renderer.uiImage
}
// MARK: - Colors Section
private static func drawColorsSection(in cgContext: CGContext, spec: GentleDesignSystemSpec, startY: CGFloat) -> CGFloat {
var y = startY
// Section title
y = drawSectionTitle("Colors", in: cgContext, at: y)
y += 12
let roles = GentleColorRole.allCases
let columns = 4
let rows = Int(ceil(Double(roles.count) / Double(columns)))
let cellWidth = (contentWidth - CGFloat(columns - 1) * gridGap - 20) / CGFloat(columns)
let cellHeight: CGFloat = 24
let containerHeight = CGFloat(rows) * (cellHeight + 6) + 14
let containerRect = CGRect(x: margin, y: y, width: contentWidth, height: containerHeight)
drawContainerCard(in: cgContext, rect: containerRect, spec: spec)
let swatchSize: CGFloat = 18
let swatchGap: CGFloat = 2
for (index, role) in roles.enumerated() {
let col = index % columns
let row = index / columns
let cellX = margin + 10 + CGFloat(col) * (cellWidth + gridGap)
let cellY = y + 10 + CGFloat(row) * (cellHeight + 6)
guard let pair = spec.colors.pair(for: role) else { continue }
// Light mode swatch (left, with top-left and bottom-left corners rounded)
let lightSwatchRect = CGRect(x: cellX, y: cellY, width: swatchSize, height: swatchSize)
let lightColor = uiColorFromHex(pair.lightHex)
let lightPath = UIBezierPath(
roundedRect: lightSwatchRect,
byRoundingCorners: [.topLeft, .bottomLeft],
cornerRadii: CGSize(width: 3, height: 3)
)
cgContext.setFillColor(lightColor.cgColor)
cgContext.addPath(lightPath.cgPath)
cgContext.fillPath()
// Border for light swatch
cgContext.setStrokeColor(UIColor.lightGray.withAlphaComponent(0.3).cgColor)
cgContext.setLineWidth(0.5)
cgContext.addPath(lightPath.cgPath)
cgContext.strokePath()
// Dark mode swatch (right, with top-right and bottom-right corners rounded)
let darkSwatchRect = CGRect(x: cellX + swatchSize + swatchGap, y: cellY, width: swatchSize, height: swatchSize)
let darkColor = uiColorFromHex(pair.darkHex)
let darkPath = UIBezierPath(
roundedRect: darkSwatchRect,
byRoundingCorners: [.topRight, .bottomRight],
cornerRadii: CGSize(width: 3, height: 3)
)
cgContext.setFillColor(darkColor.cgColor)
cgContext.addPath(darkPath.cgPath)
cgContext.fillPath()
// Border for dark swatch
cgContext.setStrokeColor(UIColor.lightGray.withAlphaComponent(0.3).cgColor)
cgContext.setLineWidth(0.5)
cgContext.addPath(darkPath.cgPath)
cgContext.strokePath()
// Role name (after both swatches) - with minimum scale factor
let labelX = cellX + swatchSize * 2 + swatchGap + 6
let availableWidth = cellWidth - (swatchSize * 2 + swatchGap + 6)
let baseFontSize: CGFloat = 10
let minScaleFactor: CGFloat = 0.7
// Try full size first, then scale down if needed
var fontSize = baseFontSize
var labelFont = UIFont.systemFont(ofSize: fontSize, weight: .regular)
var labelAttrs: [NSAttributedString.Key: Any] = [
.font: labelFont,
.foregroundColor: Self.labelColor
]
var labelString = NSAttributedString(string: role.rawValue, attributes: labelAttrs)
var labelSize = labelString.size()
// Scale down if needed, respecting minimum scale factor
if labelSize.width > availableWidth {
let scaleFactor = max(availableWidth / labelSize.width, minScaleFactor)
fontSize = baseFontSize * scaleFactor
labelFont = UIFont.systemFont(ofSize: fontSize, weight: .regular)
labelAttrs[.font] = labelFont
labelString = NSAttributedString(string: role.rawValue, attributes: labelAttrs)
}
labelString.draw(at: CGPoint(x: labelX, y: cellY + 2))
}
return y + containerHeight
}
// MARK: - Helpers
private static func drawSectionTitle(_ title: String, in cgContext: CGContext, at y: CGFloat) -> CGFloat {
let font = UIFont.systemFont(ofSize: sectionTitleFontSize, weight: .bold)
let attrs: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: UIColor.black
]
let attrString = NSAttributedString(string: title, attributes: attrs)
attrString.draw(at: CGPoint(x: margin, y: y))
return y + font.lineHeight
}
private static func drawContainerCard(in cgContext: CGContext, rect: CGRect, spec: GentleDesignSystemSpec) {
let cornerRadius: CGFloat = 12
let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
// Fill with surface color
let fillColor = colorFromSpec(spec, role: .surfaceBase)
cgContext.setFillColor(fillColor.cgColor)
cgContext.addPath(path.cgPath)
cgContext.fillPath()
// Border
let borderColor = colorFromSpec(spec, role: .borderSubtle)
cgContext.setStrokeColor(borderColor.cgColor)
cgContext.setLineWidth(1)
cgContext.addPath(path.cgPath)
cgContext.strokePath()
}
private static func colorFromSpec(_ spec: GentleDesignSystemSpec, role: GentleColorRole) -> UIColor {
guard let pair = spec.colors.pair(for: role) else {
return UIColor.black
}
return uiColorFromHex(pair.lightHex)
}
private static func uiColorFromHex(_ hex: String) -> UIColor {
let (r, g, b, a) = parseHexToRGBA(hex)
return UIColor(red: r, green: g, blue: b, alpha: a)
}
private static func parseHexToRGBA(_ hex: String) -> (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) {
var hexString = hex.trimmingCharacters(in: .whitespacesAndNewlines)
if hexString.hasPrefix("#") { hexString.removeFirst() }
var hexNumber: UInt64 = 0
let scanner = Scanner(string: hexString)
if scanner.scanHexInt64(&hexNumber) {
switch hexString.count {
case 6:
let r = CGFloat((hexNumber & 0xFF0000) >> 16) / 255.0
let g = CGFloat((hexNumber & 0x00FF00) >> 8) / 255.0
let b = CGFloat(hexNumber & 0x0000FF) / 255.0
return (r, g, b, 1.0)
case 8:
let r = CGFloat((hexNumber & 0xFF000000) >> 24) / 255.0
let g = CGFloat((hexNumber & 0x00FF0000) >> 16) / 255.0
let b = CGFloat((hexNumber & 0x0000FF00) >> 8) / 255.0
let a = CGFloat(hexNumber & 0x000000FF) / 255.0
return (r, g, b, a)
default:
return (0, 0, 0, 1)
}
}
return (0, 0, 0, 1)
}
private static func buildUIFont(from spec: GentleTypographyRoleSpec, maxSize: CGFloat? = nil) -> UIFont {
let size = maxSize.map { min(CGFloat(spec.pointSize), $0) } ?? CGFloat(spec.pointSize)
var font = UIFont.systemFont(ofSize: size, weight: spec.weight.swiftUIWeight.uiKitWeight)
// Apply design
if let designed = font.fontDescriptor.withDesign(spec.design.uiKitDesign) {
font = UIFont(descriptor: designed, size: size)
}
// Apply width if available
if let width = spec.width {
let traits = font.fontDescriptor.object(forKey: .traits) as? [UIFontDescriptor.TraitKey: Any] ?? [:]
var newTraits = traits
newTraits[.width] = width.uiKitWidthTrait
let widenedDescriptor = font.fontDescriptor.addingAttributes([.traits: newTraits])
font = UIFont(descriptor: widenedDescriptor, size: size)
}
return font
}
private static func formattedTimestamp() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd_HHmmss"
return formatter.string(from: Date())
}
}
// MARK: - SwiftUI Surface Preview View
/// A SwiftUI view that renders a surface preview with colorful stripes background
/// to make material/glass effects visible.
/// Matches the pattern used in ThemePickerView's surfacePreview method.
private struct SurfacePreviewView: View {
@Environment(\.gentleTheme) private var theme
@Environment(\.colorScheme) private var colorScheme
let role: GentleSurfaceRole
let size: CGSize
private var isGlass: Bool {
switch theme.surfaces.roleSpec(for: role).backgroundStyle {
case .glass:
return true
case .material, .solid:
return false
}
}
var body: some View {
let roleSpec = theme.surfaces.roleSpec(for: role)
let cornerRadius = roleSpec.cornerRadius
ZStack {
// Background - orbs for materials, stripes for glass
if isGlass {
PDFColorfulStripesBackground(
baseColor: theme.color(for: .themePrimary, scheme: colorScheme),
stripeWidth: 8
)
} else {
PDFGradientOrbsBackground(
primaryColor: theme.color(for: .themePrimary, scheme: colorScheme),
secondaryColor: theme.color(for: .themeSecondary, scheme: colorScheme)
)
}
// The surface on top
RoundedRectangle(cornerRadius: cornerRadius)
.fill(.clear)
.gentleSurface(role)
}
.frame(width: size.width, height: size.height)
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
}
}
/// Colorful diagonal stripes background for glass surfaces
private struct PDFColorfulStripesBackground: View {
let baseColor: Color
var stripeWidth: CGFloat = 10
private var shades: [Color] {
[
baseColor.opacity(1.0),
baseColor.opacity(0.85),
baseColor.opacity(0.7),
baseColor.opacity(0.9),
baseColor.opacity(0.75)
]
}
var body: some View {
Canvas { context, size in
let stripeWidth = self.stripeWidth
let totalWidth = size.width + size.height
var x: CGFloat = -size.height
var colorIndex = 0
var isColoredStripe = true
while x < totalWidth {
var path = Path()
path.move(to: CGPoint(x: x, y: 0))
path.addLine(to: CGPoint(x: x + size.height, y: size.height))
path.addLine(to: CGPoint(x: x + size.height + stripeWidth, y: size.height))
path.addLine(to: CGPoint(x: x + stripeWidth, y: 0))
path.closeSubpath()
if isColoredStripe {
let color1 = shades[colorIndex % shades.count]
let color2 = shades[(colorIndex + 1) % shades.count]
let gradient = Gradient(colors: [color1, color2])
context.fill(
path,
with: .linearGradient(
gradient,
startPoint: CGPoint(x: x, y: 0),
endPoint: CGPoint(x: x + stripeWidth + size.height, y: size.height)
)
)
colorIndex += 1
} else {
context.fill(path, with: .color(baseColor.opacity(0.2)))
}
isColoredStripe.toggle()
x += stripeWidth
}
}
}
}
/// High-contrast gradient orbs background for PDF surface previews
/// Creates overlapping soft circles to demonstrate material blur effects
private struct PDFGradientOrbsBackground: View {
let primaryColor: Color
let secondaryColor: Color
// Dark, high-contrast orb colors derived from theme
private var orbColors: [Color] {
[
primaryColor,
secondaryColor,
Color(red: 0.8, green: 0.3, blue: 0.1), // Deep orange
Color(red: 0.7, green: 0.1, blue: 0.4), // Deep magenta
Color(red: 0.1, green: 0.5, blue: 0.6), // Dark teal
Color(red: 0.4, green: 0.2, blue: 0.7) // Deep purple
]
}
var body: some View {
Canvas { context, size in
// Define orb positions and sizes for an interesting composition
let orbs: [(x: CGFloat, y: CGFloat, radius: CGFloat, colorIndex: Int)] = [
// Large background orbs
(0.1, 0.2, 0.7, 0), // Primary - top left
(0.9, 0.8, 0.6, 1), // Secondary - bottom right
(0.8, 0.1, 0.5, 2), // Orange - top right
// Medium orbs
(0.3, 0.9, 0.45, 3), // Pink - bottom left
(0.6, 0.4, 0.4, 4), // Cyan - center
// Smaller accent orbs
(0.15, 0.7, 0.3, 5), // Yellow - left
(0.75, 0.5, 0.35, 0), // Primary - right
(0.5, 0.15, 0.25, 1), // Secondary - top center
]
for orb in orbs {
let centerX = orb.x * size.width
let centerY = orb.y * size.height
let radius = orb.radius * max(size.width, size.height)
let color = orbColors[orb.colorIndex % orbColors.count]
// Create radial gradient for soft orb effect
let gradient = Gradient(colors: [
color.opacity(0.9),
color.opacity(0.6),
color.opacity(0.0)
])
let center = CGPoint(x: centerX, y: centerY)
let orbRect = CGRect(
x: centerX - radius,
y: centerY - radius,
width: radius * 2,
height: radius * 2
)
context.fill(
Path(ellipseIn: orbRect),
with: .radialGradient(
gradient,
center: center,
startRadius: 0,
endRadius: radius
)
)
}
}
}
}
// MARK: - UIKit Extensions (internal to this file)
private extension GentleFontDesignToken {
var uiKitDesign: UIFontDescriptor.SystemDesign {
switch self {
case .default: return .default
case .serif: return .serif
case .rounded: return .rounded
case .monospaced: return .monospaced
}
}
}
private extension GentleFontWidthToken {
var uiKitWidthTrait: CGFloat {
switch self {
case .compressed: return -0.5
case .condensed: return -0.3
case .standard: return 0.0
case .expanded: return 0.3
}
}
}
private extension Font.Weight {
var uiKitWeight: UIFont.Weight {
switch self {
case .ultraLight: return .ultraLight
case .thin: return .thin
case .light: return .light
case .regular: return .regular
case .medium: return .medium
case .semibold: return .semibold
case .bold: return .bold
case .heavy: return .heavy
case .black: return .black
default: return .regular
}
}
}