File: gdi32plus.py

package info (click to toggle)
python-vispy 0.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,240 kB
  • sloc: python: 57,407; javascript: 6,810; makefile: 63; sh: 5
file content (206 lines) | stat: -rw-r--r-- 6,617 bytes parent folder | download | duplicates (4)
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
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------

# Adapted from Pyglet

import atexit
from functools import partial
import struct

from ctypes import (windll, Structure, POINTER, byref, WINFUNCTYPE,
                    c_uint, c_float, c_int, c_ulong, c_uint64,
                    c_void_p, c_uint32, c_wchar, c_wchar_p)
from ctypes.wintypes import (LONG, BYTE, HFONT, HGDIOBJ, BOOL, UINT, INT,
                             DWORD, LPARAM)

try:
    import _winreg as winreg
except ImportError:
    import winreg  # noqa, analysis:ignore

_64_bit = (8 * struct.calcsize("P")) == 64

LF_FACESIZE = 32
FW_BOLD = 700
FW_NORMAL = 400
ANTIALIASED_QUALITY = 4
FontStyleBold = 1
FontStyleItalic = 2
UnitPixel = 2
UnitPoint = 3
DEFAULT_CHARSET = 1
ANSI_CHARSET = 0
TRUETYPE_FONTTYPE = 4
GM_ADVANCED = 2
CSIDL_FONTS = 0x0014

PixelFormat24bppRGB = 137224
PixelFormat32bppRGB = 139273
PixelFormat32bppARGB = 2498570

DriverStringOptionsCmapLookup = 1
DriverStringOptionsRealizedAdvance = 4
TextRenderingHintAntiAlias = 4
TextRenderingHintAntiAliasGridFit = 3
ImageLockModeRead = 1
StringFormatFlagsMeasureTrailingSpaces = 0x00000800
StringFormatFlagsNoClip = 0x00004000
StringFormatFlagsNoFitBlackBox = 0x00000004

INT_PTR = c_int
REAL = c_float
TCHAR = c_wchar
UINT32 = c_uint32
HDC = c_void_p
PSTR = c_uint64 if _64_bit else c_uint

HORZSIZE = 4
VERTSIZE = 6

HORZRES = 8
VERTRES = 10


# gdi32

class POINT(Structure):
    _fields_ = [('x', LONG), ('y', LONG)]


class RECT(Structure):
    _fields_ = [('left', LONG), ('top', LONG),
                ('right', LONG), ('bottom', LONG)]


class PANOSE(Structure):
    _fields_ = [
        ('bFamilyType', BYTE), ('bSerifStyle', BYTE), ('bWeight', BYTE),
        ('bProportion', BYTE), ('bContrast', BYTE), ('bStrokeVariation', BYTE),
        ('bArmStyle', BYTE), ('bLetterform', BYTE), ('bMidline', BYTE),
        ('bXHeight', BYTE)]


class TEXTMETRIC(Structure):
    _fields_ = [
        ('tmHeight', LONG), ('tmAscent', LONG), ('tmDescent', LONG),
        ('tmInternalLeading', LONG), ('tmExternalLeading', LONG),
        ('tmAveCharWidth', LONG), ('tmMaxCharWidth', LONG),
        ('tmWeight', LONG), ('tmOverhang', LONG),
        ('tmDigitizedAspectX', LONG), ('tmDigitizedAspectY', LONG),
        ('tmFirstChar', TCHAR), ('tmLastChar', TCHAR),
        ('tmDefaultChar', TCHAR), ('tmBreakChar', TCHAR),
        ('tmItalic', BYTE), ('tmUnderlined', BYTE),
        ('tmStruckOut', BYTE), ('tmPitchAndFamily', BYTE),
        ('tmCharSet', BYTE)]


class OUTLINETEXTMETRIC(Structure):
    _fields_ = [
        ('otmSize', UINT), ('otmTextMetrics', TEXTMETRIC),
        ('otmMysteryBytes', BYTE), ('otmPanoseNumber', PANOSE),
        ('otmMysteryByte', BYTE),
        ('otmfsSelection', UINT), ('otmfsType', UINT),
        ('otmsCharSlopeRise', INT), ('otmsCharSlopeRun', INT),
        ('otmItalicAngle', INT), ('otmEMSquare', UINT), ('otmAscent', INT),
        ('otmDescent', INT), ('otmLineGap', UINT), ('otmsCapEmHeight', UINT),
        ('otmsXHeight', UINT), ('otmrcFontBox', RECT), ('otmMacAscent', INT),
        ('otmMacDescent', INT), ('otmMacLineGap', UINT),
        ('otmusMinimumPPEM', UINT), ('otmptSubscriptSize', POINT),
        ('otmptSubscriptOffset', POINT), ('otmptSuperscriptSize', POINT),
        ('otmptSuperscriptOffset', POINT), ('otmsStrikeoutSize', UINT),
        ('otmsStrikeoutPosition', INT), ('otmsUnderscoreSize', INT),
        ('otmsUnderscorePosition', INT), ('otmpFamilyName', PSTR),
        ('otmpFaceName', PSTR), ('otmpStyleName', PSTR),
        ('otmpFullName', PSTR), ('junk', (BYTE) * 1024)]  # room for strs


class LOGFONT(Structure):
    _fields_ = [
        ('lfHeight', LONG), ('lfWidth', LONG), ('lfEscapement', LONG),
        ('lfOrientation', LONG), ('lfWeight', LONG), ('lfItalic', BYTE),
        ('lfUnderline', BYTE), ('lfStrikeOut', BYTE), ('lfCharSet', BYTE),
        ('lfOutPrecision', BYTE), ('lfClipPrecision', BYTE),
        ('lfQuality', BYTE), ('lfPitchAndFamily', BYTE),
        ('lfFaceName', (TCHAR * LF_FACESIZE))]


gdi32 = windll.gdi32

gdi32.CreateFontIndirectW.restype = HFONT
gdi32.CreateFontIndirectW.argtypes = [POINTER(LOGFONT)]

gdi32.SelectObject.restype = HGDIOBJ
gdi32.SelectObject.argtypes = [HDC, HGDIOBJ]

gdi32.SetGraphicsMode.restype = INT
gdi32.SetGraphicsMode.argtypes = [HDC, INT]

gdi32.GetTextMetricsW.restype = BOOL
gdi32.GetTextMetricsW.argtypes = [HDC, POINTER(TEXTMETRIC)]

FONTENUMPROC = WINFUNCTYPE(INT, POINTER(LOGFONT), POINTER(TEXTMETRIC),
                           DWORD, c_void_p)
gdi32.EnumFontFamiliesExW.restype = INT
gdi32.EnumFontFamiliesExW.argtypes = [HDC, POINTER(LOGFONT),
                                      FONTENUMPROC, LPARAM, DWORD]

gdi32.GetOutlineTextMetricsW.restype = UINT
gdi32.GetOutlineTextMetricsW.argtypes = [HDC, UINT,
                                         POINTER(OUTLINETEXTMETRIC)]


gdi32.GetDeviceCaps.argtypes = [HDC, INT]
gdi32.GetDeviceCaps.restype = INT

user32 = windll.user32

user32.GetDC.restype = HDC  # HDC
user32.GetDC.argtypes = [UINT32]  # HWND

user32.ReleaseDC.argtypes = [c_void_p, HDC]

try:
    user32.SetProcessDPIAware.argtypes = []
except AttributeError:
    pass  # not present on XP


# gdiplus

class GdiplusStartupInput(Structure):
    _fields_ = [
        ('GdiplusVersion', UINT32), ('DebugEventCallback', c_void_p),
        ('SuppressBackgroundThread', BOOL), ('SuppressExternalCodecs', BOOL)]


class GdiplusStartupOutput(Structure):
    _fields = [('NotificationHookProc', c_void_p),
               ('NotificationUnhookProc', c_void_p)]

gdiplus = windll.gdiplus

gdiplus.GdipCreateFontFamilyFromName.restype = c_int
gdiplus.GdipCreateFontFamilyFromName.argtypes = [c_wchar_p, c_void_p, c_void_p]

gdiplus.GdipNewPrivateFontCollection.restype = c_int
gdiplus.GdipNewPrivateFontCollection.argtypes = [c_void_p]

gdiplus.GdipPrivateAddFontFile.restype = c_int
gdiplus.GdipPrivateAddFontFile.argtypes = [c_void_p, c_wchar_p]

gdiplus.GdipGetFamilyName.restype = c_int
gdiplus.GdipGetFamilyName.argtypes = [c_void_p, c_wchar_p, c_int]


def gdiplus_init():
    token = c_ulong()
    startup_in = GdiplusStartupInput()
    startup_in.GdiplusVersion = 1
    startup_out = GdiplusStartupOutput()
    gdiplus.GdiplusStartup(byref(token), byref(startup_in), byref(startup_out))
    atexit.register(partial(gdiplus.GdiplusShutdown, token))

gdiplus_init()