-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathFont.swift
672 lines (580 loc) · 27.8 KB
/
Font.swift
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
// Copyright © 2020 Saleem Abdulrasool <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause
import WinSDK
// Values derived from the Apple Human Inteface Guidelines
// https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/typography
private typealias TypographyInfo = (weight: Font.Weight, size: Double, leading: Int)
private let typography: [ContentSizeCategory:[Font.TextStyle:TypographyInfo]] = [
.extraSmall: [
.largeTitle: TypographyInfo(weight: .regular, size: 31, leading: 38),
.title1: TypographyInfo(weight: .regular, size: 25, leading: 31),
.title2: TypographyInfo(weight: .regular, size: 19, leading: 24),
.title3: TypographyInfo(weight: .regular, size: 17, leading: 22),
.headline: TypographyInfo(weight: .semibold, size: 14, leading: 19),
.body: TypographyInfo(weight: .regular, size: 14, leading: 19),
.callout: TypographyInfo(weight: .regular, size: 13, leading: 18),
.subheadline: TypographyInfo(weight: .regular, size: 12, leading: 16),
.footnote: TypographyInfo(weight: .regular, size: 12, leading: 16),
.caption1: TypographyInfo(weight: .regular, size: 11, leading: 13),
.caption2: TypographyInfo(weight: .regular, size: 11, leading: 13),
],
.small: [
.largeTitle: TypographyInfo(weight: .regular, size: 32, leading: 39),
.title1: TypographyInfo(weight: .regular, size: 26, leading: 32),
.title2: TypographyInfo(weight: .regular, size: 20, leading: 25),
.title3: TypographyInfo(weight: .regular, size: 18, leading: 23),
.headline: TypographyInfo(weight: .semibold, size: 15, leading: 20),
.body: TypographyInfo(weight: .regular, size: 15, leading: 20),
.callout: TypographyInfo(weight: .regular, size: 14, leading: 19),
.subheadline: TypographyInfo(weight: .regular, size: 13, leading: 18),
.footnote: TypographyInfo(weight: .regular, size: 12, leading: 16),
.caption1: TypographyInfo(weight: .regular, size: 11, leading: 13),
.caption2: TypographyInfo(weight: .regular, size: 11, leading: 13),
],
.medium: [
.largeTitle: TypographyInfo(weight: .regular, size: 33, leading: 40),
.title1: TypographyInfo(weight: .regular, size: 27, leading: 33),
.title2: TypographyInfo(weight: .regular, size: 21, leading: 26),
.title3: TypographyInfo(weight: .regular, size: 19, leading: 24),
.headline: TypographyInfo(weight: .semibold, size: 16, leading: 21),
.body: TypographyInfo(weight: .regular, size: 16, leading: 21),
.callout: TypographyInfo(weight: .regular, size: 15, leading: 20),
.subheadline: TypographyInfo(weight: .regular, size: 14, leading: 19),
.footnote: TypographyInfo(weight: .regular, size: 12, leading: 16),
.caption1: TypographyInfo(weight: .regular, size: 11, leading: 13),
.caption2: TypographyInfo(weight: .regular, size: 11, leading: 13),
],
.large: [
.largeTitle: TypographyInfo(weight: .regular, size: 34, leading: 41),
.title1: TypographyInfo(weight: .regular, size: 28, leading: 34),
.title2: TypographyInfo(weight: .regular, size: 22, leading: 28),
.title3: TypographyInfo(weight: .regular, size: 20, leading: 25),
.headline: TypographyInfo(weight: .semibold, size: 17, leading: 22),
.body: TypographyInfo(weight: .regular, size: 17, leading: 22),
.callout: TypographyInfo(weight: .regular, size: 16, leading: 21),
.subheadline: TypographyInfo(weight: .regular, size: 15, leading: 20),
.footnote: TypographyInfo(weight: .regular, size: 13, leading: 18),
.caption1: TypographyInfo(weight: .regular, size: 12, leading: 16),
.caption2: TypographyInfo(weight: .regular, size: 11, leading: 13),
],
.extraLarge: [
.largeTitle: TypographyInfo(weight: .regular, size: 36, leading: 43),
.title1: TypographyInfo(weight: .regular, size: 30, leading: 37),
.title2: TypographyInfo(weight: .regular, size: 24, leading: 30),
.title3: TypographyInfo(weight: .regular, size: 22, leading: 28),
.headline: TypographyInfo(weight: .semibold, size: 19, leading: 24),
.body: TypographyInfo(weight: .regular, size: 19, leading: 24),
.callout: TypographyInfo(weight: .regular, size: 18, leading: 23),
.subheadline: TypographyInfo(weight: .regular, size: 17, leading: 22),
.footnote: TypographyInfo(weight: .regular, size: 15, leading: 20),
.caption1: TypographyInfo(weight: .regular, size: 14, leading: 19),
.caption2: TypographyInfo(weight: .regular, size: 13, leading: 18),
],
.extraExtraLarge: [
.largeTitle: TypographyInfo(weight: .regular, size: 38, leading: 46),
.title1: TypographyInfo(weight: .regular, size: 32, leading: 39),
.title2: TypographyInfo(weight: .regular, size: 26, leading: 32),
.title3: TypographyInfo(weight: .regular, size: 24, leading: 30),
.headline: TypographyInfo(weight: .semibold, size: 21, leading: 26),
.body: TypographyInfo(weight: .regular, size: 21, leading: 26),
.callout: TypographyInfo(weight: .regular, size: 20, leading: 25),
.subheadline: TypographyInfo(weight: .regular, size: 19, leading: 24),
.footnote: TypographyInfo(weight: .regular, size: 17, leading: 22),
.caption1: TypographyInfo(weight: .regular, size: 16, leading: 21),
.caption2: TypographyInfo(weight: .regular, size: 15, leading: 20),
],
.extraExtraExtraLarge: [
.largeTitle: TypographyInfo(weight: .regular, size: 40, leading: 48),
.title1: TypographyInfo(weight: .regular, size: 34, leading: 41),
.title2: TypographyInfo(weight: .regular, size: 28, leading: 34),
.title3: TypographyInfo(weight: .regular, size: 26, leading: 32),
.headline: TypographyInfo(weight: .semibold, size: 23, leading: 29),
.body: TypographyInfo(weight: .regular, size: 23, leading: 29),
.callout: TypographyInfo(weight: .regular, size: 22, leading: 28),
.subheadline: TypographyInfo(weight: .regular, size: 21, leading: 28),
.footnote: TypographyInfo(weight: .regular, size: 19, leading: 24),
.caption1: TypographyInfo(weight: .regular, size: 18, leading: 23),
.caption2: TypographyInfo(weight: .regular, size: 17, leading: 22),
],
.accessibilityMedium: [
.largeTitle: TypographyInfo(weight: .regular, size: 44, leading: 52),
.title1: TypographyInfo(weight: .regular, size: 38, leading: 46),
.title2: TypographyInfo(weight: .regular, size: 34, leading: 41),
.title3: TypographyInfo(weight: .regular, size: 31, leading: 38),
.headline: TypographyInfo(weight: .semibold, size: 28, leading: 34),
.body: TypographyInfo(weight: .regular, size: 28, leading: 34),
.callout: TypographyInfo(weight: .regular, size: 26, leading: 32),
.subheadline: TypographyInfo(weight: .regular, size: 25, leading: 31),
.footnote: TypographyInfo(weight: .regular, size: 23, leading: 29),
.caption1: TypographyInfo(weight: .regular, size: 22, leading: 28),
.caption2: TypographyInfo(weight: .regular, size: 20, leading: 25),
],
.accessibilityLarge: [
.largeTitle: TypographyInfo(weight: .regular, size: 48, leading: 57),
.title1: TypographyInfo(weight: .regular, size: 43, leading: 51),
.title2: TypographyInfo(weight: .regular, size: 39, leading: 47),
.title3: TypographyInfo(weight: .regular, size: 37, leading: 44),
.headline: TypographyInfo(weight: .semibold, size: 33, leading: 40),
.body: TypographyInfo(weight: .regular, size: 33, leading: 40),
.callout: TypographyInfo(weight: .regular, size: 32, leading: 39),
.subheadline: TypographyInfo(weight: .regular, size: 30, leading: 37),
.footnote: TypographyInfo(weight: .regular, size: 27, leading: 33),
.caption1: TypographyInfo(weight: .regular, size: 26, leading: 32),
.caption2: TypographyInfo(weight: .regular, size: 24, leading: 30),
],
.accessibilityExtraLarge: [
.largeTitle: TypographyInfo(weight: .regular, size: 52, leading: 61),
.title1: TypographyInfo(weight: .regular, size: 48, leading: 57),
.title2: TypographyInfo(weight: .regular, size: 44, leading: 52),
.title3: TypographyInfo(weight: .regular, size: 43, leading: 51),
.headline: TypographyInfo(weight: .semibold, size: 40, leading: 48),
.body: TypographyInfo(weight: .regular, size: 40, leading: 48),
.callout: TypographyInfo(weight: .regular, size: 38, leading: 46),
.subheadline: TypographyInfo(weight: .regular, size: 36, leading: 43),
.footnote: TypographyInfo(weight: .regular, size: 33, leading: 40),
.caption1: TypographyInfo(weight: .regular, size: 32, leading: 39),
.caption2: TypographyInfo(weight: .regular, size: 29, leading: 35),
],
.accessibilityExtraExtraLarge: [
.largeTitle: TypographyInfo(weight: .regular, size: 56, leading: 66),
.title1: TypographyInfo(weight: .regular, size: 53, leading: 62),
.title2: TypographyInfo(weight: .regular, size: 50, leading: 59),
.title3: TypographyInfo(weight: .regular, size: 49, leading: 58),
.headline: TypographyInfo(weight: .semibold, size: 47, leading: 56),
.body: TypographyInfo(weight: .regular, size: 47, leading: 56),
.callout: TypographyInfo(weight: .regular, size: 44, leading: 52),
.subheadline: TypographyInfo(weight: .regular, size: 42, leading: 50),
.footnote: TypographyInfo(weight: .regular, size: 38, leading: 46),
.caption1: TypographyInfo(weight: .regular, size: 37, leading: 44),
.caption2: TypographyInfo(weight: .regular, size: 34, leading: 41),
],
.accessibilityExtraExtraExtraLarge: [
.largeTitle: TypographyInfo(weight: .regular, size: 60, leading: 70),
.title1: TypographyInfo(weight: .regular, size: 58, leading: 68),
.title2: TypographyInfo(weight: .regular, size: 56, leading: 66),
.title3: TypographyInfo(weight: .regular, size: 55, leading: 65),
.headline: TypographyInfo(weight: .semibold, size: 53, leading: 62),
.body: TypographyInfo(weight: .regular, size: 53, leading: 62),
.callout: TypographyInfo(weight: .regular, size: 51, leading: 62),
.subheadline: TypographyInfo(weight: .regular, size: 49, leading: 58),
.footnote: TypographyInfo(weight: .regular, size: 44, leading: 52),
.caption1: TypographyInfo(weight: .regular, size: 43, leading: 51),
.caption2: TypographyInfo(weight: .regular, size: 40, leading: 47),
],
]
private func PointToLogical(_ points: Double) -> Int32 {
return -MulDiv(Int32(points), GetDeviceCaps(GetDC(nil), LOGPIXELSY), 72)
}
private func LogicalToPoint(_ logical: Int32) -> Double {
return Double(MulDiv(-logical, 72, GetDeviceCaps(GetDC(nil), LOGPIXELSY)))
}
/// Represents a font.
public class Font {
internal var hFont: FontHandle
// MARK - Creating Fonts
/// Returns an instance of the system font for the specified text style with
/// scaling for the user's selected content size category.
public static func preferredFont(forTextStyle style: Font.TextStyle) -> Font {
return Font.preferredFont(forTextStyle: style, compatibleWith: nil)
}
/// Returns an instance of the system font for the appropriate text style and
/// traits.
public static func preferredFont(forTextStyle style: Font.TextStyle,
compatibleWith traits: TraitCollection?)
-> Font {
var size: ContentSizeCategory = traits?.preferredContentSizeCategory ?? .large
if size == .unspecified { size = .large }
guard let font = typography[size]?[style] else {
log.error("\(#function) missing typography for \(style)@\(size)")
return systemFont(ofSize: 12)
}
return systemFont(ofSize: font.size, weight: font.weight)
}
/// Creates an returns a font object for the specified font name and size.
public init?(name: String, size: Double) {
self.hFont = FontHandle(owning: CreateFontW(PointToLogical(size),
/*cWidth=*/0,
/*cEscapement=*/0,
/*cOrientation=*/0,
Font.Weight.regular.rawValue,
/*bItalic=*/DWORD(0),
/*bUnderline=*/DWORD(0),
/*bStrikeOut=*/DWORD(0),
DWORD(DEFAULT_CHARSET),
DWORD(OUT_DEFAULT_PRECIS),
DWORD(CLIP_DEFAULT_PRECIS),
DWORD(DEFAULT_QUALITY),
DWORD((FF_DONTCARE << 2) | DEFAULT_PITCH),
name.wide))
}
// public init(descriptor: FontDescriptor, size pointSize: Double)
// Returns a font object that is the ame as the font, but has the specified
// size.
public func withSize(_ fontSize: Double) -> Font {
var lfFont: LOGFONTW = LOGFONTW()
if GetObjectW(self.hFont.value, Int32(MemoryLayout<LOGFONTW>.size),
&lfFont) == 0 {
log.error("GetObjectW: \(Error(win32: GetLastError()))")
return self
}
lfFont.lfHeight = LONG(PointToLogical(fontSize))
return Font(owning: CreateFontIndirectW(&lfFont))
}
// MARK - Creating System Fonts
/// Returns the font object for standard interface items in the specified
/// size.
public static func systemFont(ofSize fontSize: Double) -> Font {
return systemFont(ofSize: fontSize, weight: .regular, italic: false)
}
/// Returns the font object for standard interface items in the specifed size
/// and weight.
public static func systemFont(ofSize fontSize: Double, weight: Font.Weight)
-> Font {
return systemFont(ofSize: fontSize, weight: weight, italic: false)
}
/// Returns the font object for standard interface items in boldface type in
/// the specified size.
public static func boldSystemFont(ofSize fontSize: Double) -> Font {
return systemFont(ofSize: fontSize, weight: .bold, italic: false)
}
/// Returns the font object for standard interface items in italic type in the
/// specified size.
public static func italicSystemFont(ofSize fontSize: Double) -> Font {
return systemFont(ofSize: fontSize, weight: .regular, italic: true)
}
/// Returns the fixed-width font for standard interface text in the specified
/// size.
public static func monospacedSystemFont(ofSize fontSize: Double,
weight: Font.Weight) -> Font {
return Font(owning: CreateFontW(PointToLogical(fontSize),
/*cWidth=*/0,
/*cEscapement=*/0,
/*cOrientation=*/0,
weight.rawValue,
/*bItalic=*/DWORD(0),
/*bUnderline=*/DWORD(0),
/*bStrikeOut=*/DWORD(0),
DWORD(DEFAULT_CHARSET),
DWORD(OUT_DEFAULT_PRECIS),
DWORD(CLIP_DEFAULT_PRECIS),
DWORD(DEFAULT_QUALITY),
DWORD((FF_DONTCARE << 2) | FIXED_PITCH),
nil))
}
/// Returns the standard system font with all digits of consistent width.
public static func monospacedDigitSystemFont(ofSize fontSize: Double,
weight: Font.Weight) -> Font {
return Font(owning: CreateFontW(PointToLogical(fontSize),
/*cWidth=*/0,
/*cEscapement=*/0,
/*cOrientation=*/0,
weight.rawValue,
/*bItalic=*/DWORD(0),
/*bUnderline=*/DWORD(0),
/*bStrikeOut=*/DWORD(0),
DWORD(DEFAULT_CHARSET),
DWORD(OUT_DEFAULT_PRECIS),
DWORD(CLIP_DEFAULT_PRECIS),
DWORD(DEFAULT_QUALITY),
DWORD((FF_DONTCARE << 2) | FIXED_PITCH),
nil))
}
// MARK - Getting the Available Font Names
/// Returns an array of font family names available on the system.
public static var familyNames: [String] {
let hDC: HDC = GetDC(nil)
var arrFamilies: Set<String> = []
var lfFont: LOGFONTW = LOGFONTW()
lfFont.lfCharSet = BYTE(DEFAULT_CHARSET)
let pfnEnumerateFontFamilies: FONTENUMPROCW = { (lpelfe, lpnt, FontType, lParam) in
// NOTE: '@' indicates a vertical-oriented flags; treat the enumeration
// as if CF_NOVERTFONTS is specified.
let bVerticalFont: Bool =
UnicodeScalar(lpelfe?.pointee.lfFaceName.0 ?? 0) == UnicodeScalar("@")
if bVerticalFont { return 1 }
let parrFamilies: UnsafeMutablePointer<Set<String>> =
UnsafeMutablePointer<Set<String>>(bitPattern: Int(lParam))!
let family: String = withUnsafePointer(to: lpelfe?.pointee.lfFaceName) {
let capacity: Int =
MemoryLayout.size(ofValue: $0) / MemoryLayout<WCHAR>.size
return $0.withMemoryRebound(to: UInt16.self, capacity: capacity) {
String(decodingCString: $0, as: UTF16.self)
}
}
parrFamilies.pointee.insert(family)
return 1
}
_ = withUnsafeMutablePointer(to: &arrFamilies) {
EnumFontFamiliesExW(hDC, &lfFont, pfnEnumerateFontFamilies,
LPARAM(Int(bitPattern: $0)), 0)
}
return Array<String>(arrFamilies)
}
/// Returns an array of font names available in a particular font family.
public static func fontNames(forFontFamily family: String) -> [String] {
let hDC: HDC = GetDC(nil)
var arrFonts: Set<String> = []
let pfnEnumerateFonts: FONTENUMPROCW = { (lplf, lptm, dwType, lpData) in
let parrFonts: UnsafeMutablePointer<Set<String>> =
UnsafeMutablePointer<Set<String>>(bitPattern: Int(lpData))!
let font: String = withUnsafePointer(to: lplf?.pointee.lfFaceName) {
let capacity: Int =
MemoryLayout.size(ofValue: $0) / MemoryLayout<WCHAR>.size
return $0.withMemoryRebound(to: UInt16.self, capacity: capacity) {
String(decodingCString: $0, as: UTF16.self)
}
}
parrFonts.pointee.insert(font)
return 1
}
_ = withUnsafeMutablePointer(to: &arrFonts) {
EnumFontsW(hDC, family.wide, pfnEnumerateFonts,
LPARAM(Int(bitPattern: $0)))
}
return Array<String>(arrFonts)
}
// MARK - Getting Font Name Attributes
// public var familyName: String { }
/// The font face name.
public var fontName: String {
var lfFont: LOGFONTW = LOGFONTW()
if GetObjectW(self.hFont.value, Int32(MemoryLayout<LOGFONTW>.size),
&lfFont) == 0 {
log.error("GetObjectW: \(Error(win32: GetLastError()))")
return ""
}
return withUnsafePointer(to: &lfFont.lfFaceName) {
let capacity: Int =
MemoryLayout.size(ofValue: $0) / MemoryLayout<WCHAR>.size
return $0.withMemoryRebound(to: UInt16.self, capacity: capacity) {
return String(decodingCString: $0, as: UTF16.self)
}
}
}
// MARK - Getting Font Metrics
/// The font's point size, or the effective vertical point size for a font
/// with a non-standard matrix.
public var pointSize: Double {
var lfFont: LOGFONTW = LOGFONTW()
if GetObjectW(self.hFont.value, Int32(MemoryLayout<LOGFONTW>.size),
&lfFont) == 0 {
log.error("GetObjectW: \(Error(win32: GetLastError()))")
return 0.0
}
return LogicalToPoint(Int32(lfFont.lfHeight))
}
/// The top y-coordinate, offset from the baseline, of the font's longest
/// ascender.
public var ascender: Double {
let hDC: DeviceContextHandle =
DeviceContextHandle(owning: CreateCompatibleDC(nil))
let _ = SelectObject(hDC.value, self.hFont.value)
var metrics: TEXTMETRICW = TEXTMETRICW()
if !GetTextMetricsW(hDC.value, &metrics) {
log.warning("GetTextMetricsW: \(Error(win32: GetLastError()))")
return 0.0
}
return Double(metrics.tmAscent)
}
/// The bottom y-coordinate, offset from the baseline, of the font's longest
/// descender.
public var descender: Double {
let hDC: DeviceContextHandle =
DeviceContextHandle(owning: CreateCompatibleDC(nil))
let _ = SelectObject(hDC.value, self.hFont.value)
var metrics: TEXTMETRICW = TEXTMETRICW()
if !GetTextMetricsW(hDC.value, &metrics) {
log.warning("GetTextMetricsW: \(Error(win32: GetLastError()))")
return 0.0
}
return Double(metrics.tmDescent)
}
/// The font's leading information.
public var leading: Double {
let hDC: DeviceContextHandle =
DeviceContextHandle(owning: CreateCompatibleDC(nil))
let _ = SelectObject(hDC.value, self.hFont.value)
var metrics: TEXTMETRICW = TEXTMETRICW()
if !GetTextMetricsW(hDC.value, &metrics) {
log.warning("GetTextMetricsW: \(Error(win32: GetLastError()))")
return 0.0
}
return Double(metrics.tmExternalLeading)
}
/// The font's cap height information.
public var capHeight: Double {
let hDC: DeviceContextHandle =
DeviceContextHandle(owning: CreateCompatibleDC(nil))
let _ = SelectObject(hDC.value, self.hFont.value)
var size: SIZE = SIZE()
if !GetTextExtentPoint32W(hDC.value, "u{0048}".wide, 1, &size) {
log.warning("GetTextExtentPoint32W: \(Error(win32: GetLastError()))")
return 0.0
}
return Double(size.cy)
}
/// The x-height of the font.
public var xHeight: Double {
let hDC: DeviceContextHandle =
DeviceContextHandle(owning: CreateCompatibleDC(nil))
let _ = SelectObject(hDC.value, self.hFont.value)
var size: SIZE = SIZE()
if !GetTextExtentPoint32W(hDC.value, "u{0078}".wide, 1, &size) {
log.warning("GetTextExtentPoint32W: \(Error(win32: GetLastError()))")
return 0.0
}
return Double(size.cy)
}
/// The height, in points, of text lines.
// public var lineHeight: Double { }
// MARK - Getting System Font Information
/// The standard font size, in points, for labels.
public static var labelFontSize: Double { 17.0 }
/// The standard font size, in points, for buttons.
public static var buttonFontSize: Double { 18.0 }
/// The size, in points, of the standard small system font.
public static var smallSystemFontSize: Double { 12.0 }
/// The size, in points, of the standard system font.
public static var systemFontSize: Double { 14.0 }
/// Getting Font Descriptors
// public var fontDescriptor: FontDescriptor { }
internal init(_ hFont: FontHandle) {
self.hFont = hFont
}
private init(owning hFont: HFONT) {
self.hFont = FontHandle(owning: hFont)
}
private static func systemFont(ofSize fontSize: Double, weight: Font.Weight,
italic bItalic: Bool) -> Font {
// Windows XP+ default fault name
var fontName: String = "Segoe UI"
var metrics: NONCLIENTMETRICSW = NONCLIENTMETRICSW()
metrics.cbSize = UINT(MemoryLayout<NONCLIENTMETRICSW>.size)
if SystemParametersInfoW(UINT(SPI_GETNONCLIENTMETRICS),
metrics.cbSize, &metrics, 0) {
fontName = withUnsafePointer(to: metrics.lfMessageFont.lfFaceName) {
let capacity: Int =
MemoryLayout.size(ofValue: $0) / MemoryLayout<WCHAR>.size
return $0.withMemoryRebound(to: UInt16.self, capacity: capacity) {
String(decodingCString: $0, as: UTF16.self)
}
}
}
return Font(owning: CreateFontW(PointToLogical(fontSize),
/*cWidth=*/0,
/*cEscapement=*/0,
/*cOrientation=*/0,
weight.rawValue,
bItalic ? 1 : 0,
/*bUnderline=*/DWORD(0),
/*bStrikeOut=*/DWORD(0),
DWORD(DEFAULT_CHARSET),
DWORD(OUT_DEFAULT_PRECIS),
DWORD(CLIP_DEFAULT_PRECIS),
DWORD(DEFAULT_QUALITY),
DWORD((FF_DONTCARE << 2) | DEFAULT_PITCH),
fontName.wide))
}
}
public extension Font {
/// Constants that represent standard font weights.
struct Weight: Hashable, Equatable, RawRepresentable {
public let rawValue: Int32
public init(rawValue: Int32) {
self.rawValue = rawValue
}
}
}
public extension Font.Weight {
/// The ultra-light font weight.
static var ultraLight: Font.Weight {
Font.Weight(rawValue: FW_ULTRALIGHT)
}
/// The thin font weight.
static var thin: Font.Weight {
Font.Weight(rawValue: FW_THIN)
}
/// The light font weight.
static var light: Font.Weight {
Font.Weight(rawValue: FW_LIGHT)
}
/// The regular font weight.
static var regular: Font.Weight {
Font.Weight(rawValue: FW_REGULAR)
}
/// The medium font weight.
static var medium: Font.Weight {
Font.Weight(rawValue: FW_MEDIUM)
}
/// The semibold font weight.
static var semibold: Font.Weight {
Font.Weight(rawValue: FW_SEMIBOLD)
}
/// The bold font weight.
static var bold: Font.Weight {
Font.Weight(rawValue: FW_BOLD)
}
/// The heavy font weight.
static var heavy: Font.Weight {
Font.Weight(rawValue: FW_HEAVY)
}
/// The black font weight.
static var black: Font.Weight {
Font.Weight(rawValue: FW_BLACK)
}
}
public extension Font {
/// Constants that describe the preferred styles for fonts.
struct TextStyle: Hashable, Equatable, RawRepresentable {
public let rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
}
}
public extension Font.TextStyle {
/// The font for body text.
static var body: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleBody")
}
/// The font for callouts.
static var callout: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleCallout")
}
/// The font for standard captions.
static var caption1: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleCaption1")
}
/// The font for alternate captions.
static var caption2: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleCaption2")
}
/// The font for footnotes.
static var footnote: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleFootnote")
}
/// The font for headings.
static var headline: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleHeadline")
}
/// The font for subheadings.
static var subheadline: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleSubhead")
}
/// The font style for large titles.
static var largeTitle: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleTitle0")
}
/// The font for first-level hierarchical headings.
static var title1: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleTitle1")
}
/// The font for second-level hierarchical headings.
static var title2: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleTitle2")
}
/// The font for third-level hierarchical headings.
static var title3: Font.TextStyle {
Font.TextStyle(rawValue: "UICTFontTextStyleTitle3")
}
}