jenkins-bot has submitted this change and it was merged.

Change subject: win32_unicode: force truetype font in console
......................................................................


win32_unicode: force truetype font in console

This will automatically turn the terminal into one that supports unicode
for Windows Vista+ (i.e. any supported version of Windows). I have removed
the warning, as it is now no longer necessary to follow any steps for
a useful console.

Change-Id: I6fb1b418eae9b995e390de34639a6c3fc110dd32
---
M pywikibot/config2.py
M pywikibot/userinterfaces/win32_unicode.py
2 files changed, 59 insertions(+), 18 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/config2.py b/pywikibot/config2.py
index 9b73444..8bad32c 100644
--- a/pywikibot/config2.py
+++ b/pywikibot/config2.py
@@ -52,7 +52,6 @@
 
 from warnings import warn
 
-from pywikibot import __url__
 from pywikibot.logging import error, output, warning
 from pywikibot.tools import PY2
 
@@ -410,7 +409,7 @@
 #    transliteration_target = console_encoding
 # After emitting the warning, this last option will be set.
 
-transliteration_target = 'not set'
+transliteration_target = None
 
 # The encoding in which textfiles are stored, which contain lists of page
 # titles. The most used is: 'utf-8'. 'utf-8-sig' recognizes BOM but it is
@@ -1055,19 +1054,6 @@
 
 if console_encoding is None:
     console_encoding = 'utf-8'
-
-# Fix up transliteration_target
-if transliteration_target == 'not set':
-    if OSWIN32:
-        transliteration_target = console_encoding
-        warning(
-            'Running on Windows and transliteration_target is not set.\n'
-            'Please see {0}/Windows'.format(__url__))
-    else:
-        transliteration_target = None
-elif transliteration_target in ('None', 'none'):
-    transliteration_target = None
-
 
 if OSWIN32 and editor is None:
     editor = _detect_win32_editor()
diff --git a/pywikibot/userinterfaces/win32_unicode.py 
b/pywikibot/userinterfaces/win32_unicode.py
index 2ae926a..2425fc9 100755
--- a/pywikibot/userinterfaces/win32_unicode.py
+++ b/pywikibot/userinterfaces/win32_unicode.py
@@ -29,7 +29,7 @@
 import codecs
 import sys
 
-from ctypes import byref, c_int, create_unicode_buffer
+from ctypes import Structure, byref, c_int, create_unicode_buffer, sizeof
 from ctypes import c_void_p as LPVOID
 from io import IOBase, UnsupportedOperation
 
@@ -49,8 +49,9 @@
 original_stderr = sys.stderr
 
 if OSWIN32:
-    from ctypes import WINFUNCTYPE, windll, POINTER
-    from ctypes.wintypes import BOOL, HANDLE, DWORD, LPWSTR, LPCWSTR
+    from ctypes import WINFUNCTYPE, windll, POINTER, WinError
+    from ctypes.wintypes import (BOOL, DWORD, HANDLE, LPCWSTR, LPWSTR,
+                                 SHORT, ULONG, UINT, WCHAR)
 
 try:
     ReadConsoleW = WINFUNCTYPE(BOOL, HANDLE, LPVOID, DWORD, POINTER(DWORD),
@@ -183,6 +184,58 @@
     codecs.register(lambda name: name == 'cp65001' and codecs.lookup('utf-8') 
or None)
 
 
+def force_truetype_console(h_stdout):
+    """Force the console to use a TrueType font (Vista+)."""
+    TMPF_TRUETYPE = 0x04
+    LF_FACESIZE = 32
+
+    class COORD(Structure):
+        _fields_ = [('X', SHORT),
+                    ('Y', SHORT)]
+
+    class CONSOLE_FONT_INFOEX(Structure):
+        _fields_ = [('cbSize', ULONG),
+                    ('nFont', DWORD),
+                    ('dwFontSize', COORD),
+                    ('FontFamily', UINT),
+                    ('FontWeight', UINT),
+                    ('FaceName', WCHAR * LF_FACESIZE)]
+
+    try:
+        GetCurrentConsoleFontEx = WINFUNCTYPE(
+            BOOL,
+            HANDLE,  # hConsoleOutput
+            BOOL,    # bMaximumWindow
+            POINTER(CONSOLE_FONT_INFOEX),  # lpConsoleCurrentFontEx
+        )(("GetCurrentConsoleFontEx", windll.kernel32))
+
+        SetCurrentConsoleFontEx = WINFUNCTYPE(
+            BOOL,
+            HANDLE,  # hConsoleOutput
+            BOOL,    # bMaximumWindow
+            POINTER(CONSOLE_FONT_INFOEX),  # lpConsoleCurrentFontEx
+        )(("SetCurrentConsoleFontEx", windll.kernel32))
+    except AttributeError:
+        # pre Windows Vista. Return without doing anything.
+        return
+
+    current_font = CONSOLE_FONT_INFOEX()
+    current_font.cbSize = sizeof(CONSOLE_FONT_INFOEX)
+
+    if not GetCurrentConsoleFontEx(h_stdout, True, byref(current_font)):
+        WinError()
+
+    truetype_font = (current_font.FontFamily & TMPF_TRUETYPE)
+
+    if not truetype_font:
+        new_font = CONSOLE_FONT_INFOEX()
+        new_font.cbSize = sizeof(CONSOLE_FONT_INFOEX)
+        new_font.FaceName = u'Lucida Console'
+
+        if not SetCurrentConsoleFontEx(h_stdout, True, byref(new_font)):
+            WinError()
+
+
 def get_unicode_console():
     """
     Get Unicode console objects.
@@ -248,11 +301,13 @@
 
         if real_stdout:
             hStdout = GetStdHandle(STD_OUTPUT_HANDLE)
+            force_truetype_console(hStdout)
             if not_a_console(hStdout):
                 real_stdout = False
 
         if real_stderr:
             hStderr = GetStdHandle(STD_ERROR_HANDLE)
+            force_truetype_console(hStderr)
             if not_a_console(hStderr):
                 real_stderr = False
 

-- 
To view, visit https://gerrit.wikimedia.org/r/270490
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I6fb1b418eae9b995e390de34639a6c3fc110dd32
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Gerrit Patch Uploader <[email protected]>
Gerrit-Reviewer: Gerrit Patch Uploader <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to