[pyglet] 2 new revisions pushed by useboxnet on 2014-05-31 07:53 GMT

1 view
Skip to first unread message

pyg...@googlecode.com

unread,
May 31, 2014, 3:53:56 AM5/31/14
to pyglet-...@googlegroups.com
2 new revisions:

Revision: 021505b88a84
Branch: default
Author: Juan J. Martínez <j...@usebox.net>
Date: Sat May 31 07:49:58 2014 UTC
Log: Workaround for window 64-bit issue with GetDC...
http://code.google.com/p/pyglet/source/detail?r=021505b88a84

Revision: b5cabd481804
Branch: default
Author: Juan J. Martínez <j...@usebox.net>
Date: Sat May 31 07:53:39 2014 UTC
Log: Fixed monospace detection, improved comments...
http://code.google.com/p/pyglet/source/detail?r=b5cabd481804

==============================================================================
Revision: 021505b88a84
Branch: default
Author: Juan J. Martínez <j...@usebox.net>
Date: Sat May 31 07:49:58 2014 UTC
Log: Workaround for window 64-bit issue with GetDC

Fixes issue #664.

Thanks to jayborseth for the patch.
http://code.google.com/p/pyglet/source/detail?r=021505b88a84

Modified:
/pyglet/libs/win32/__init__.py

=======================================
--- /pyglet/libs/win32/__init__.py Sun Jan 6 23:18:42 2013 UTC
+++ /pyglet/libs/win32/__init__.py Sat May 31 07:49:58 2014 UTC
@@ -150,8 +150,9 @@
_user32.GetClientRect.argtypes = [HWND, LPRECT]
_user32.GetCursorPos.restype = BOOL
_user32.GetCursorPos.argtypes = [LPPOINT]
-_user32.GetDC.restype = HDC
-_user32.GetDC.argtypes = [HWND]
+# workaround for win 64-bit, see issue #664
+_user32.GetDC.restype = c_void_p # HDC
+_user32.GetDC.argtypes = [c_void_p] # [HWND]
_user32.GetDesktopWindow.restype = HWND
_user32.GetDesktopWindow.argtypes = []
_user32.GetKeyState.restype = c_short
@@ -184,8 +185,9 @@
_user32.RegisterHotKey.argtypes = [HWND, c_int, UINT, UINT]
_user32.ReleaseCapture.restype = BOOL
_user32.ReleaseCapture.argtypes = []
-_user32.ReleaseDC.restype = c_int
-_user32.ReleaseDC.argtypes = [HWND, HDC]
+# workaround for win 64-bit, see issue #664
+_user32.ReleaseDC.restype = c_int32 # c_int
+_user32.ReleaseDC.argtypes = [c_void_p, c_void_p] # [HWND, HDC]
_user32.ScreenToClient.restype = BOOL
_user32.ScreenToClient.argtypes = [HWND, LPPOINT]
_user32.SetCapture.restype = HWND

==============================================================================
Revision: b5cabd481804
Branch: default
Author: Juan J. Martínez <j...@usebox.net>
Date: Sat May 31 07:53:39 2014 UTC
Log: Fixed monospace detection, improved comments

Fixes issue #744.

Thanks to Claudio Canepa for the patch!
http://code.google.com/p/pyglet/source/detail?r=b5cabd481804

Modified:
/examples/font_comparison.py
/pyglet/font/win32query.py

=======================================
--- /examples/font_comparison.py Sat Dec 7 19:02:32 2013 UTC
+++ /examples/font_comparison.py Sat May 31 07:53:39 2014 UTC
@@ -33,58 +33,88 @@
# POSSIBILITY OF SUCH DAMAGE.
#
----------------------------------------------------------------------------

-'''A simple tool that may be used to compare font faces.
+'''A simple tool that may be used to explore font faces. (Windows only)
+
+Only the fonts installed in the system are visible.

Use the left/right cursor keys to change font faces.
+
+By default only the pyglet safe fonts are shown, toggle the safe flag
+to see all.
+
+Don't include tabs in the text sample (see
+http://pyglet.org/doc-current/programming_guide/text.html#id9 )
'''
+from __future__ import print_function, unicode_literals

-__docformat__ = 'restructuredtext'
-__version__ = '$Id: $'
import pyglet
+import pyglet.font.win32query as wq

-FONTS = ['Andale
Mono', 'Consolas', 'Inconsolata', 'Inconsolata-dz', 'Monaco',
- 'Menlo']

+# support to generate a sample text good to spot monospace compliance.
+# Chosen to do a table of fields_per_line columns, each column with
field_size
+# characters. Fields are filled with a rolling subset of ASCII characters.
+class SampleTable(object):
+ field_size = 7
+ gap_size = 3
+ fields_per_line = 7
+ spaces = ' ' * field_size
+ max_chars_per_line = (field_size + gap_size) * fields_per_line -
gap_size

-# [ ] tab character is buggy even with monospace fonts
-#FONTS = list(set(pyglet.font.win32query.font_list(vector_only=True,
monospace_only=True)))
-# [ ] some fonts marked as monospace are shown incorrectly
-# 1. space is too narrow
-# 2. symbols are cropped by height
-# [ ] maybe the requested font could not by found, and pyglet
-# receives the closest one, but not monospaced
-# [ ] check if pyglet receives signal when requested font
-# can not be found
-#
-# FP M 400 CHARSET: 0 @SimHei
-# FP M 400 CHARSET: 0 @MS Gothic
+ def __init__(self):
+ self.lines = []
+ self.current_line = ''
+
+ def newline(self):
+ self.lines.append(self.current_line)
+ self.current_line = ''
+
+ def add_field(self, s):
+ assert len(s) <= self.field_size
+ to_add = self.spaces[len(s):] + s
+ if self.current_line:
+ to_add = ' ' * self.gap_size + to_add
+ if len(self.current_line) + len(to_add) > self.max_chars_per_line:
+ self.newline()
+ self.add_field(s)
+ else:
+ self.current_line = self.current_line + to_add
+
+ def text(self):
+ return '\n'.join(self.lines)
+
+def sample_text_monospaced_table():
+ printables
= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|
}~ '
+ table = SampleTable()
+ for i in range(6):
+ s = printables[i:] + printables[:i]
+ for k in range(0, len(printables), table.field_size):
+ table.add_field(s[k:k + table.field_size])
+ table.newline()
+ return table.text()

+# this worked right with all fonts in a win xp installation
+def pyglet_safe(fontentry):
+ """ this is heuristic and conservative. YMMV. """
+ return fontentry.vector and fontentry.family != wq.FF_DONTCARE

-SAMPLE = '''
-class Spam(object):
- def __init__(self):
- # The quick brown fox
- self.spam = {"jumped": 'over'}
- @the
- def lazy(self, *dog):
- self.dog = [lazy, lazy]'''

class Window(pyglet.window.Window):
font_num = 0
def on_text_motion(self, motion):
if motion == pyglet.window.key.MOTION_RIGHT:
self.font_num += 1
- if self.font_num == len(FONTS):
+ if self.font_num == len(font_names):
self.font_num = 0
elif motion == pyglet.window.key.MOTION_LEFT:
self.font_num -= 1
if self.font_num < 0:
- self.font_num = len(FONTS) - 1
+ self.font_num = len(font_names) - 1

- face = FONTS[self.font_num]
- self.head = pyglet.text.Label(face, font_size=24, y=0,
+ face = font_names[self.font_num]
+ self.head = pyglet.text.Label(face, font_size=16, y=0,
anchor_y='bottom')
- self.text = pyglet.text.Label(SAMPLE, font_name=face, font_size=18,
+ self.text = pyglet.text.Label(sample_text, font_name=face,
font_size=12,
y=self.height, anchor_y='top', width=self.width,
multiline=True)

def on_draw(self):
@@ -92,6 +122,33 @@
self.head.draw()
self.text.draw()

-window = Window()
-window.on_text_motion(None)
-pyglet.app.run()
+
+lorem_ipsum = """
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam
lectus.
+Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
+consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget
+libero egestas mattis sit amet vitae augue.
+
+
+"""
+
+if __name__ == '__main__':
+ print(__doc__)
+ safe = True
+ sample_text = lorem_ipsum + sample_text_monospaced_table()
+ # all fonts known by the OS
+ fontdb = wq.query()
+
+ if safe:
+ candidates = [ f for f in fontdb if pyglet_safe(f)]
+ else:
+ canditates = fontdb
+
+ # theres one fontentry for each charset supported, so reduce names
+ font_names = list(set([f.name for f in candidates]))
+
+ font_names.sort()
+ window = Window(1024, 600)
+ window.on_text_motion(None)
+ pyglet.app.run()
+
=======================================
--- /pyglet/font/win32query.py Sat Dec 7 19:02:32 2013 UTC
+++ /pyglet/font/win32query.py Sat May 31 07:53:39 2014 UTC
@@ -21,13 +21,30 @@
sans-serif - (LOGFONT.lfPitchAndFamily >> 4) == FF_SWISS
cursive - (LOGFONT.lfPitchAndFamily >> 4) == FF_SCRIPT
fantasy - (LOGFONT.lfPitchAndFamily >> 4) == FF_DECORATIVE
- monospace - (LOGFONT.lfPitchAndFamily >> 4) == FF_MODERN
+ monospace - (lf.lfPitchAndFamily & 0b11) == FIXED_PITCH

-NOTE: Raster 'Modern' font and OpenType 'OCR A Extended' are
- FF_MODERN, but have VARIABLE_PITCH for some reason
+NOTE: ATM, May 2015, the Microsoft documentation related to monospace
+is misleading due to poor wording:
+ - FF_MODERN in the description of LOGFONT structure tells
+ "Fonts with constant stroke width (monospace), with or without serifs.
+ Monospace fonts are usually modern.
+ Pica, Elite, and CourierNew are examples.
+ "
+
+ Stroke width is the 'pen width', not glyph width. It should read

- [ ] find a way to check char's pitch matches manually
+ "Fonts with constant stroke width, with or without serifs.
+ Monospace fonts are usually modern, but not all modern are monospace
+ "

+PYGLET NOTE:
+Examination of all fonts in a windows xp machine shows that all fonts
+with
+
+ fontentry.vector and fontentry.family != FF_DONTCARE
+
+are rendered fine.
+

Use cases:
[x] get the list of all available system font names
@@ -75,11 +92,12 @@
- vector (True if font is vector, False for raster fonts)
- format: ttf | ...
"""
- def __init__(self, name, vector, format, monospace):
+ def __init__(self, name, vector, format, monospace, family):
self.name = name
self.vector = vector
self.format = format
self.monospace = monospace
+ self.family = family

# List of FontEntry objects
FONTDB = []
@@ -144,10 +162,12 @@
CHARSET_NAMES[value] = name

# font pitch constants ('fixed pitch' means 'monospace')
+DEFAULT_PITCH = 0
FIXED_PITCH = 1
VARIABLE_PITCH = 2

# Windows font family constants
+FF_DONTCARE = 0 # Don't care or don't know
FF_ROMAN = 1 # with serifs, proportional
FF_SWISS = 2 # w/out serifs, proportional
FF_MODERN = 3 # constant stroke width
@@ -198,12 +218,12 @@
# PROOF_QUALITY
('lfPitchAndFamily', BYTE),
# DEFAULT_PITCH
- # FIXED_PITCH
+ # FIXED_PITCH - authoritative for monospace
# VARIABLE_PITCH
# stacked with any of
# FF_DECORATIVE - novelty
# FF_DONTCARE - default font
- # FF_MODERN - monospace
+ # FF_MODERN - stroke width ('pen width') near constant
# FF_ROMAN - proportional (variable char width) with serifs
# FF_SCRIPT - handwritten
# FF_SWISS - proportional without serifs
@@ -302,11 +322,11 @@
# FP T NM 400 CHARSET: 136 @DFKai-SB
# VP T M 400 CHARSET: 0 OCR A Extended

- monospace = False
- if family == FF_MODERN:
- monospace = True
-
- FONTDB.append(FontEntry(name, vector, format, monospace))
+ monospace = (pitch == FIXED_PITCH)
+
+ charset = lf.lfCharSet
+
+ FONTDB.append(FontEntry(name, vector, format, monospace, family))

if DEBUG:
info = ''
@@ -438,7 +458,7 @@
print 'Have font "Arial"? %s' % test_arial
print 'Have font "missing-one"? %s' % have_font('missing-one')
# test cache is not rebuilt
- FONTDB = [FontEntry('stub', False, '', False)]
+ FONTDB = [FontEntry('stub', False, '', False, FF_MODERN)]
assert(have_font('Arial') != test_arial)
# test cache is rebiult
assert(have_font('Arial', refresh=True) == test_arial)
Reply all
Reply to author
Forward
0 new messages