-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows_fonts.py
439 lines (361 loc) · 21.4 KB
/
windows_fonts.py
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
from comtypes import COMError, GUID, HRESULT, IUnknown, STDMETHOD
from ctypes import byref, create_unicode_buffer, POINTER, windll, wintypes
from enum import IntEnum
from os.path import isfile
from sys import getwindowsversion
from typing import Set
from .exceptions import OSNotSupported
from .system_fonts import SystemFonts
class DWRITE_FACTORY_TYPE(IntEnum):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/ne-dwrite-dwrite_factory_type
DWRITE_FACTORY_TYPE_SHARED = 0
DWRITE_FACTORY_TYPE_ISOLATED = 1
class DWRITE_FONT_FILE_TYPE(IntEnum):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/ne-dwrite-dwrite_font_file_type
DWRITE_FONT_FILE_TYPE_UNKNOWN = 0
DWRITE_FONT_FILE_TYPE_CFF = 1
DWRITE_FONT_FILE_TYPE_TRUETYPE = 2
DWRITE_FONT_FILE_TYPE_OPENTYPE_COLLECTION = 3
DWRITE_FONT_FILE_TYPE_TYPE1_PFM = 4
DWRITE_FONT_FILE_TYPE_TYPE1_PFB = 5
DWRITE_FONT_FILE_TYPE_VECTOR = 6
DWRITE_FONT_FILE_TYPE_BITMAP = 7
DWRITE_FONT_FILE_TYPE_TRUETYPE_COLLECTION = DWRITE_FONT_FILE_TYPE_OPENTYPE_COLLECTION
class DWRITE_LOCALITY(IntEnum):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite_3/ne-dwrite_3-dwrite_locality
DWRITE_LOCALITY_REMOTE = 0
DWRITE_LOCALITY_PARTIAL = 1
DWRITE_LOCALITY_LOCAL = 2
class IDWriteFontFileLoader(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefontfileloader
_iid_ = GUID("{727cad4e-d6af-4c9e-8a08-d695b11caa49}")
_methods_ = [
STDMETHOD(None, "CreateStreamFromKey"), # Need to be implemented
]
class IDWriteLocalFontFileLoader(IDWriteFontFileLoader):
# https://learn.microsoft.com/en-us/windows/win32/directwrite/idwritelocalfontfileloader
_iid_ = GUID("{b2d9f3ec-c9fe-4a11-a2ec-d86208f7c0a2}")
_methods_ = [
STDMETHOD(HRESULT, "GetFilePathLengthFromKey", [wintypes.LPCVOID, wintypes.UINT, POINTER(wintypes.UINT)]),
STDMETHOD(HRESULT, "GetFilePathFromKey", [wintypes.LPCVOID, wintypes.UINT, POINTER(wintypes.WCHAR), wintypes.UINT]),
STDMETHOD(None, "GetLastWriteTimeFromKey"), # Need to be implemented
]
class IDWriteFontFile(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefontfile
_iid_ = GUID("{739d886a-cef5-47dc-8769-1a8b41bebbb0}")
_methods_ = [
STDMETHOD(HRESULT, "GetReferenceKey", [POINTER(wintypes.LPCVOID), POINTER(wintypes.UINT)]),
STDMETHOD(HRESULT, "GetLoader", [POINTER(POINTER(IDWriteFontFileLoader))]),
STDMETHOD(HRESULT, "Analyze", [POINTER(wintypes.BOOLEAN), POINTER(wintypes.UINT), POINTER(wintypes.UINT), POINTER(wintypes.UINT)]),
]
class IDWriteFontFaceReference(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite_3/nn-dwrite_3-idwritefontfacereference
_iid_ = GUID("{5E7FA7CA-DDE3-424C-89F0-9FCD6FED58CD}")
_methods_ = [
STDMETHOD(None, "CreateFontFace"), # Need to be implemented
STDMETHOD(None, "CreateFontFaceWithSimulations"), # Need to be implemented
STDMETHOD(None, "Equals"), # Need to be implemented
STDMETHOD(None, "GetFontFaceIndex"), # Need to be implemented
STDMETHOD(None, "GetSimulations"), # Need to be implemented
STDMETHOD(HRESULT, "GetFontFile", [POINTER(POINTER(IDWriteFontFile))]),
STDMETHOD(None, "GetLocalFileSize"), # Need to be implemented
STDMETHOD(None, "GetFileSize"), # Need to be implemented
STDMETHOD(None, "GetFileTime"), # Need to be implemented
STDMETHOD(wintypes.UINT, "GetLocality"),
STDMETHOD(None, "EnqueueFontDownloadRequest"), # Need to be implemented
STDMETHOD(None, "EnqueueCharacterDownloadRequest"), # Need to be implemented
STDMETHOD(None, "EnqueueGlyphDownloadRequest"), # Need to be implemented
STDMETHOD(None, "EnqueueFileFragmentDownloadRequest"), # Need to be implemented
]
class IDWriteFontSet(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite_3/nn-dwrite_3-idwritefontset
_iid_ = GUID("{53585141-D9F8-4095-8321-D73CF6BD116B}")
_methods_ = [
STDMETHOD(wintypes.UINT, "GetFontCount"),
STDMETHOD(HRESULT, "GetFontFaceReference", [wintypes.UINT, POINTER(POINTER(IDWriteFontFaceReference))]),
STDMETHOD(None, "FindFontFaceReference"), # Need to be implemented
STDMETHOD(None, "FindFontFace"), # Need to be implemented
STDMETHOD(None, "GetPropertyValues"), # Need to be implemented
STDMETHOD(None, "GetPropertyValues"), # Need to be implemented
STDMETHOD(None, "GetPropertyValues"), # Need to be implemented
STDMETHOD(None, "GetPropertyOccurrenceCount"), # Need to be implemented
STDMETHOD(None, "GetMatchingFonts"), # Need to be implemented
STDMETHOD(None, "GetMatchingFonts"), # Need to be implemented
]
class IDWriteFontFace(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefontface
_iid_ = GUID("{5f49804d-7024-4d43-bfa9-d25984f53849}")
_methods_ = [
STDMETHOD(None, "GetType"), # Need to be implemented
STDMETHOD(HRESULT, "GetFiles", [POINTER(wintypes.UINT), POINTER(POINTER(IDWriteFontFile))]),
STDMETHOD(None, "GetIndex"), # Need to be implemented
STDMETHOD(None, "GetSimulations"), # Need to be implemented
STDMETHOD(None, "IsSymbolFont"), # Need to be implemented
STDMETHOD(None, "GetMetrics"), # Need to be implemented
STDMETHOD(None, "GetGlyphCount"), # Need to be implemented
STDMETHOD(None, "GetDesignGlyphMetrics"), # Need to be implemented
STDMETHOD(None, "GetGlyphIndices"), # Need to be implemented
STDMETHOD(None, "TryGetFontTable"), # Need to be implemented
STDMETHOD(None, "ReleaseFontTable"), # Need to be implemented
STDMETHOD(None, "GetGlyphRunOutline"), # Need to be implemented
STDMETHOD(None, "GetRecommendedRenderingMode"), # Need to be implemented
STDMETHOD(None, "GetGdiCompatibleMetrics"), # Need to be implemented
STDMETHOD(None, "GetGdiCompatibleGlyphMetrics"), # Need to be implemented
]
class IDWriteFont(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefont
_iid_ = GUID("{acd16696-8c14-4f5d-877e-fe3fc1d32737}")
_methods_ = [
STDMETHOD(None, "GetFontFamily"), # Need to be implemented
STDMETHOD(None, "GetWeight"), # Need to be implemented
STDMETHOD(None, "GetStretch"), # Need to be implemented
STDMETHOD(None, "GetStyle"), # Need to be implemented
STDMETHOD(None, "IsSymbolFont"), # Need to be implemented
STDMETHOD(None, "GetFaceNames"), # Need to be implemented
STDMETHOD(None, "GetInformationalStrings"), # Need to be implemented
STDMETHOD(None, "GetSimulations"), # Need to be implemented
STDMETHOD(None, "GetMetrics"), # Need to be implemented
STDMETHOD(None, "HasCharacter"), # Need to be implemented
STDMETHOD(HRESULT, "CreateFontFace", [POINTER(POINTER(IDWriteFontFace))]),
]
class IDWriteFontList(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefontlist
_iid_ = GUID("{1a0d8438-1d97-4ec1-aef9-a2fb86ed6acb}")
_methods_ = [
STDMETHOD(None, "GetFontCollection"), # Need to be implemented
STDMETHOD(wintypes.UINT, "GetFontCount"),
STDMETHOD(HRESULT, "GetFont", [wintypes.UINT, POINTER(POINTER(IDWriteFont))]),
]
class IDWriteFontFamily(IDWriteFontList):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefontfamily
_iid_ = GUID("{da20d8ef-812a-4c43-9802-62ec4abd7add}")
_methods_ = [
STDMETHOD(None, "GetFamilyNames"), # Need to be implemented
STDMETHOD(None, "GetFirstMatchingFont"), # Need to be implemented
STDMETHOD(None, "GetMatchingFonts"), # Need to be implemented
]
class IDWriteFontCollection(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefontcollection
_iid_ = GUID("{a84cee02-3eea-4eee-a827-87c1a02a0fcc}")
_methods_ = [
STDMETHOD(wintypes.UINT, "GetFontFamilyCount"),
STDMETHOD(HRESULT, "GetFontFamily", [wintypes.UINT, POINTER(POINTER(IDWriteFontFamily))]),
STDMETHOD(None, "FindFamilyName"), # Need to be implemented
STDMETHOD(None, "GetFontFromFontFace"), # Need to be implemented
]
class IDWriteFactory(IUnknown):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite/nn-dwrite-idwritefactory
_iid_ = GUID("{b859ee5a-d838-4b5b-a2e8-1adc7d93db48}")
_methods_ = [
STDMETHOD(HRESULT, "GetSystemFontCollection", [POINTER(POINTER(IDWriteFontCollection)), wintypes.BOOLEAN]),
STDMETHOD(None, "CreateCustomFontCollection"), # Need to be implemented
STDMETHOD(None, "RegisterFontCollectionLoader"), # Need to be implemented
STDMETHOD(None, "UnregisterFontCollectionLoader"), # Need to be implemented
STDMETHOD(None, "CreateFontFileReference"), # Need to be implemented
STDMETHOD(None, "CreateCustomFontFileReference"), # Need to be implemented
STDMETHOD(None, "CreateFontFace"), # Need to be implemented
STDMETHOD(None, "CreateRenderingParams"), # Need to be implemented
STDMETHOD(None, "CreateMonitorRenderingParams"), # Need to be implemented
STDMETHOD(None, "CreateCustomRenderingParams"), # Need to be implemented
STDMETHOD(None, "RegisterFontFileLoader"), # Need to be implemented
STDMETHOD(None, "UnregisterFontFileLoader"), # Need to be implemented
STDMETHOD(None, "CreateTextFormat"), # Need to be implemented
STDMETHOD(None, "CreateTypography"), # Need to be implemented
STDMETHOD(None, "GetGdiInterop"), # Need to be implemented
STDMETHOD(None, "CreateTextLayout"), # Need to be implemented
STDMETHOD(None, "CreateGdiCompatibleTextLayout"), # Need to be implemented
STDMETHOD(None, "CreateEllipsisTrimmingSign"), # Need to be implemented
STDMETHOD(None, "CreateTextAnalyzer"), # Need to be implemented
STDMETHOD(None, "CreateNumberSubstitution"), # Need to be implemented
STDMETHOD(None, "CreateGlyphRunAnalysis"), # Need to be implemented
]
class IDWriteFactory1(IDWriteFactory):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite_1/nn-dwrite_1-idwritefactory1
_iid_ = GUID("{30572f99-dac6-41db-a16e-0486307e606a}")
_methods_ = [
STDMETHOD(None, "GetEudcFontCollection"), # Need to be implemented
STDMETHOD(None, "CreateCustomRenderingParams1"), # Need to be implemented
]
class IDWriteFactory2(IDWriteFactory1):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite_2/nn-dwrite_2-idwritefactory2
_iid_ = GUID("{0439fc60-ca44-4994-8dee-3a9af7b732ec}")
_methods_ = [
STDMETHOD(None, "GetSystemFontFallback"), # Need to be implemented
STDMETHOD(None, "CreateFontFallbackBuilder"), # Need to be implemented
STDMETHOD(None, "TranslateColorGlyphRun"), # Need to be implemented
STDMETHOD(None, "CreateCustomRenderingParams2"), # Need to be implemented
STDMETHOD(None, "CreateGlyphRunAnalysis"), # Need to be implemented
]
class IDWriteFactory3(IDWriteFactory2):
# https://learn.microsoft.com/en-us/windows/win32/api/dwrite_3/nn-dwrite_3-idwritefactory3
_iid_ = GUID("{9A1B41C3-D3BB-466A-87FC-FE67556A3B65}")
_methods_ = [
STDMETHOD(None, "CreateGlyphRunAnalysis"), # Need to be implemented
STDMETHOD(None, "CreateCustomRenderingParams3"), # Need to be implemented
STDMETHOD(None, "CreateFontFaceReference"), # Need to be implemented
STDMETHOD(None, "CreateFontFaceReference"), # Need to be implemented
STDMETHOD(HRESULT, "GetSystemFontSet", [POINTER(POINTER(IDWriteFontSet))]),
STDMETHOD(None, "CreateFontSetBuilder"), # Need to be implemented
STDMETHOD(None, "CreateFontCollectionFromFontSet"), # Need to be implemented
STDMETHOD(None, "GetSystemFontCollection3"), # Need to be implemented
STDMETHOD(None, "GetFontDownloadQueue"), # Need to be implemented
]
class WindowsFonts(SystemFonts):
_DWriteCreateFactory = None
VALID_FONT_FORMATS = [
DWRITE_FONT_FILE_TYPE.DWRITE_FONT_FILE_TYPE_CFF,
DWRITE_FONT_FILE_TYPE.DWRITE_FONT_FILE_TYPE_TRUETYPE,
DWRITE_FONT_FILE_TYPE.DWRITE_FONT_FILE_TYPE_OPENTYPE_COLLECTION,
DWRITE_FONT_FILE_TYPE.DWRITE_FONT_FILE_TYPE_TRUETYPE_COLLECTION,
]
def get_system_fonts_filename() -> Set[str]:
windows_version = getwindowsversion()
if WindowsVersionHelpers.is_windows_10_or_greater(windows_version):
fonts_filename = WindowsFonts._get_fonts_filename_windows_10_or_more()
elif WindowsVersionHelpers.is_windows_vista_sp2_or_greater(windows_version):
fonts_filename = WindowsFonts._get_fonts_filename_windows_vista_sp2_or_more()
else:
raise OSNotSupported("FindSystemFontsFilename only works on Windows Vista SP2 or more")
return fonts_filename
@staticmethod
def _get_fonts_filename_windows_10_or_more() -> Set[str]:
"""
Return an set of all the installed fonts filename.
"""
if WindowsFonts._DWriteCreateFactory is None:
WindowsFonts._load_DWriteCreateFactory()
fonts_filename = set()
dwrite_factory = POINTER(IDWriteFactory3)()
WindowsFonts._DWriteCreateFactory(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_ISOLATED, IDWriteFactory3._iid_, byref(dwrite_factory))
font_set = POINTER(IDWriteFontSet)()
dwrite_factory.GetSystemFontSet(byref(font_set))
for i in range(font_set.GetFontCount()):
font_face_reference = POINTER(IDWriteFontFaceReference)()
font_set.GetFontFaceReference(i, byref(font_face_reference))
locality = font_face_reference.GetLocality()
if DWRITE_LOCALITY(locality) != DWRITE_LOCALITY.DWRITE_LOCALITY_LOCAL:
continue
font_file = POINTER(IDWriteFontFile)()
font_face_reference.GetFontFile(byref(font_file))
loader = POINTER(IDWriteFontFileLoader)()
font_file.GetLoader(byref(loader))
font_file_reference_key = wintypes.LPCVOID()
font_file_reference_key_size = wintypes.UINT()
font_file.GetReferenceKey(byref(font_file_reference_key), byref(font_file_reference_key_size))
# For a user, even if IDWriteFontFaceReference::GetLocality returned DWRITE_LOCALITY_LOCAL,
# the QueryInterface always fails for one specific font.
# I did a bunch of tests with him to try to understand why it fails.
# Here are my conclusions:
# First, with IDWriteFontFace3::GetInformationalStrings, we found the family name of the problematic font. It was "Levenim MT".
# Secondly, WindowsFonts._get_fonts_filename_windows_vista_sp2_or_more doesn't list "Levenim MT",
# so QueryInterface doesn't raise an exception.
# Thirdly, EnumFontFamiliesEx didn't enumerate "Levenim MT". Also, TextOut also doesn't display the font.
# Fourthly, if we search "Levenim MT" with IDWriteFontSet::GetMatchingFonts, the font is not found.
# Fifthly, the font doesn't show up in "C:\Windows\Fonts".
# Sixthly, in Word, Aegisub, Paint.NET, libass, and VSFilter the font doesn't show up.
# Finally, with IDWriteFontFileStream, we have been able to get the file.
# So the font should be physically on the hard drive, but we couldn't find it.
# If we are able to get the data of the font file, why can't we display the font in any software?
# This seems to be a DirectWrite bug. To bypass it, if QueryInterface fails, ignore the font.
# Anyways, the font cannot be displayed, so it doesn't matter.
try:
local_loader = loader.QueryInterface(IDWriteLocalFontFileLoader)
except COMError:
continue
path_len = wintypes.UINT()
local_loader.GetFilePathLengthFromKey(font_file_reference_key, font_file_reference_key_size, byref(path_len))
buffer = create_unicode_buffer(path_len.value + 1)
local_loader.GetFilePathFromKey(font_file_reference_key, font_file_reference_key_size, buffer, len(buffer))
font_filename = buffer.value
if isfile(font_filename):
is_supported_font_type = wintypes.BOOLEAN()
font_file_type = wintypes.UINT()
font_face_type = wintypes.UINT()
number_of_faces = wintypes.UINT()
font_file.Analyze(byref(is_supported_font_type), byref(font_file_type), byref(font_face_type), byref(number_of_faces))
if DWRITE_FONT_FILE_TYPE(font_file_type.value) in WindowsFonts.VALID_FONT_FORMATS:
fonts_filename.add(buffer.value)
return fonts_filename
@staticmethod
def _get_fonts_filename_windows_vista_sp2_or_more() -> Set[str]:
"""
Return an set of all the installed fonts filename.
"""
if WindowsFonts._DWriteCreateFactory is None:
WindowsFonts._load_DWriteCreateFactory()
fonts_filename = set()
dwrite_factory = POINTER(IDWriteFactory)()
WindowsFonts._DWriteCreateFactory(DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_ISOLATED, IDWriteFactory._iid_, byref(dwrite_factory))
sys_collection = POINTER(IDWriteFontCollection)()
dwrite_factory.GetSystemFontCollection(byref(sys_collection), False)
for i in range(sys_collection.GetFontFamilyCount()):
family = POINTER(IDWriteFontFamily)()
sys_collection.GetFontFamily(i, byref(family))
for j in range(family.GetFontCount()):
try:
font = POINTER(IDWriteFont)()
family.GetFont(j, byref(font))
except COMError:
# If the file doesn't exist, DirectWrite raise an exception
continue
font_face = POINTER(IDWriteFontFace)()
font.CreateFontFace(byref(font_face))
file_count = wintypes.UINT()
font_face.GetFiles(byref(file_count), None)
font_files = (POINTER(IDWriteFontFile) * file_count.value)()
font_face.GetFiles(byref(file_count), font_files)
for font_file in font_files:
font_file_reference_key = wintypes.LPCVOID()
font_file_reference_key_size = wintypes.UINT()
font_file.GetReferenceKey(byref(font_file_reference_key), byref(font_file_reference_key_size))
loader = POINTER(IDWriteFontFileLoader)()
font_file.GetLoader(byref(loader))
local_loader = loader.QueryInterface(IDWriteLocalFontFileLoader)
is_supported_font_type = wintypes.BOOLEAN()
font_file_type = wintypes.UINT()
font_face_type = wintypes.UINT()
number_of_faces = wintypes.UINT()
font_file.Analyze(byref(is_supported_font_type), byref(font_file_type), byref(font_face_type), byref(number_of_faces))
if DWRITE_FONT_FILE_TYPE(font_file_type.value) not in WindowsFonts.VALID_FONT_FORMATS:
continue
path_len = wintypes.UINT()
local_loader.GetFilePathLengthFromKey(font_file_reference_key, font_file_reference_key_size, byref(path_len))
buffer = create_unicode_buffer(path_len.value + 1)
local_loader.GetFilePathFromKey(font_file_reference_key, font_file_reference_key_size, buffer, len(buffer))
fonts_filename.add(buffer.value)
return fonts_filename
@staticmethod
def _load_DWriteCreateFactory():
WindowsFonts._DWriteCreateFactory = windll.dwrite.DWriteCreateFactory
WindowsFonts._DWriteCreateFactory.restype = HRESULT
WindowsFonts._DWriteCreateFactory.argtypes = [wintypes.UINT, GUID, POINTER(POINTER(IUnknown))]
class WindowsVersionHelpers:
@staticmethod
def is_windows_version_or_greater(windows_version, major: int, minor: int, build: int) -> bool:
"""
Parameters:
windows_version: An object from getwindowsversion.
major (int): The minimum major OS version number.
minor (int): The minimum minor OS version number.
build (int): The minimum build version number.
Returns:
True if the specified version matches or if it is greater than the version of the current Windows OS. Otherwise, False.
"""
if windows_version.major > major:
return True
elif windows_version.major == major and windows_version.minor > minor:
return True
else:
return (
windows_version.major == major
and windows_version.minor == minor
and windows_version.build >= build
)
@staticmethod
def is_windows_vista_sp2_or_greater(windows_version) -> bool:
# From https://www.lifewire.com/windows-version-numbers-2625171
return WindowsVersionHelpers.is_windows_version_or_greater(windows_version, 6, 0, 6002)
@staticmethod
def is_windows_10_or_greater(windows_version) -> bool:
# From https://www.lifewire.com/windows-version-numbers-2625171
return WindowsVersionHelpers.is_windows_version_or_greater(windows_version, 10, 0, 10240)