예제 #1
0
def put(scrap_type, thing):
    board = NSPasteboard.generalPasteboard()
    if scrap_type == SCRAP_TEXT:
        board.declareTypes_owner_([NSStringPboardType, ScrapPboardType], None)
        if isinstance(thing, unicode):
            text_thing = thing
        else:
            text_thing = unicode(thing, 'utf-8')
        board.setString_forType_(text_thing, NSStringPboardType)
        board.setString_forType_(unicode_(''), ScrapPboardType)
    elif 1:
        raise NotImplementedError(err)





    elif 0 and scrap_type == SCRAP_BMP:
        # Don't use this code... we put the data in as a string.

        #if type(thing) != type(pygame.Surface((1,1))):
        #    thing = pygame.image.fromstring(thing, len(thing) * 4, "RGBA")


        # This is pretty silly, we shouldn't have to do this...
        fh = tempfile.NamedTemporaryFile(suffix='.png')
        pygame.image.save(thing, fh.name)
        path = fh.name
        if not isinstance(path, unicode):
            path = unicode(path, sys.getfilesystemencoding())
        img = NSImage.alloc().initByReferencingFile_(path)
        tiff = img.TIFFRepresentation()
        fh.close()
        board.declareTypes_owner_([NSTIFFPboardType, ScrapPboardType], None)
        board.setData_forType_(tiff, NSTIFFPboardType)
        board.setString_forType_(unicode_(''), ScrapPboardType)
    elif scrap_type == SCRAP_BMP:
        
        other_type = scrap_type
        board.declareTypes_owner_([other_type], None)
        board.setData_forType_(thing, other_type)

    else:
        other_type = scrap_type
        if 0:
            board.declareTypes_owner_([NSStringPboardType, other_type], None)
            board.setString_forType_(text_thing, NSStringPboardType)
        elif 0:
            board.declareTypes_owner_([other_type], None)
            #board.setString_forType_(thing, other_type)
            board.setData_forType_(thing, other_type)
        else:
            board.declareTypes_owner_([NSStringPboardType, other_type], None)
            board.setString_forType_(thing, NSStringPboardType)
예제 #2
0
    def test_freetype_Face_render_raw(self):
    
        face = self._TEST_FONTS['sans']
        
        text = "abc"
        size = face.get_rect(text, ptsize=24).size
        rend = face.render_raw(text, ptsize=24)
        self.assertTrue(isinstance(rend, tuple))
        self.assertEqual(len(rend), 2)
        r, s = rend
        self.assertTrue(isinstance(r, bytes_))
        self.assertTrue(isinstance(s, tuple))
        self.assertTrue(len(s), 2)
        w, h = s
        self.assertTrue(isinstance(w, int))
        self.assertTrue(isinstance(w, int))
        self.assertEqual(s, size)
        self.assertEqual(len(r), w * h)
        
        r, (w, h) = face.render_raw('', ptsize=24)
        self.assertEqual(w, 0)
        self.assertEqual(h, face.height)
        self.assertEqual(len(r), 0)
        
        # bug with decenders: this would crash
        rend = face.render_raw('render_raw', ptsize=24)

        # bug with non-printable characters: this would cause a crash
        # because the text length was not adjusted for skipped characters.
        text = unicode_("").join([unichr_(i) for i in range(31, 64)])
        rend = face.render_raw(text, ptsize=10)
예제 #3
0
    def test_caption_unicode(self):
        TEST_CAPTION = u"台"
        display.set_caption(TEST_CAPTION)
        import sys

        if sys.version_info.major >= 3:
            self.assertEqual(display.get_caption()[0], TEST_CAPTION)
        else:
            self.assertEqual(unicode_(display.get_caption()[0], "utf8"), TEST_CAPTION)
 def test_unload(self):
     import shutil
     import tempfile
     ep = unicode_(example_path('data'))
     org_file = os.path.join(ep, u'house_lo.wav')
     tmpfd, tmppath = tempfile.mkstemp('.wav')
     os.close(tmpfd)
     shutil.copy(org_file, tmppath)
     try:
         pygame.mixer.music.load(tmppath)
         pygame.mixer.music.unload()
     finally:
         os.remove(tmppath)
예제 #5
0
 def _load_unicode(self, path):
     import shutil
     fdir = unicode_(FONTDIR)
     temp = os.path.join(fdir, path)
     pgfont = os.path.join(fdir, u'test_sans.ttf')
     shutil.copy(pgfont, temp)
     try:
         with open(temp, 'rb') as f:
             pass
     except IOError:
         raise unittest.SkipTest('the path cannot be opened')
     try:
         pygame_font.Font(temp, 20)
     finally:
         os.remove(temp)
예제 #6
0
 def test_save_unicode_path(self):
     temp_file = unicode_("tmpimg.png")
     im = pygame.Surface((10, 10), 0, 32)
     try:
         os.remove(temp_file)
     except EnvironmentError:
         pass
     self.assert_(not os.path.exists(temp_file))
     try:
         save_extended(im, temp_file)
         self.assert_(os.path.getsize(temp_file) > 10)
     finally:
         try:
             os.remove(temp_file)
         except EnvironmentError:
             pass
 def test_save_unicode_path(self):
     temp_file = unicode_("tmpimg.png")
     im = pygame.Surface((10, 10), 0, 32)
     try:
         os.remove(temp_file)
     except EnvironmentError:
         pass
     self.assert_(not os.path.exists(temp_file))
     try:
         imageext.save_extended(im, temp_file)
         self.assert_(os.path.getsize(temp_file) > 10)
     finally:
         try:
             os.remove(temp_file)
         except EnvironmentError:
             pass
예제 #8
0
 def test_load_unicode(self):
     """test non-ASCII unicode path"""
     import shutil
     ep = unicode_(example_path('data'))
     temp_file = os.path.join(ep, u'你好.wav')
     org_file = os.path.join(ep, u'house_lo.wav')
     try:
         with open(temp_file, 'w') as f:
             pass
         os.remove(temp_file)
     except IOError:
         raise unittest.SkipTest('the path cannot be opened')
     shutil.copy(org_file, temp_file)
     try:
         pygame.mixer.music.load(temp_file)
         pygame.mixer.music.load(org_file) # unload
     finally:
         os.remove(temp_file)
예제 #9
0
 def test_sound_unicode(self):
     """test non-ASCII unicode path"""
     mixer.init()
     import shutil
     ep = unicode_(example_path('data'))
     temp_file = os.path.join(ep, u'你好.wav')
     org_file = os.path.join(ep, u'house_lo.wav')
     shutil.copy(org_file, temp_file)
     try:
         with open(temp_file, 'rb') as f:
             pass
     except IOError:
         raise unittest.SkipTest('the path cannot be opened')
     try:
         sound = mixer.Sound(temp_file)
         del sound
     finally:
         os.remove(temp_file)
예제 #10
0
 def test_unicode_(self):
     self.assertTrue(hasattr(compat.unicode_(), "isdecimal"))
예제 #11
0
'darkorchid' : (153, 50, 204, 255) ,
'thistle3' : (205, 181, 205, 255) ,
'thistle2' : (238, 210, 238, 255) ,
'thistle1' : (255, 225, 255, 255) ,
'salmon' : (250, 128, 114, 255) ,
'gray93' : (237, 237, 237, 255) ,
'thistle4' : (139, 123, 139, 255) ,
'gray39' : (99, 99, 99, 255) ,
'lawngreen' : (124, 252, 0, 255) ,
'hotpink3' : (205, 96, 144, 255) ,
'hotpink2' : (238, 106, 167, 255) ,
'hotpink1' : (255, 110, 180, 255) ,
'lightgreen' : (144, 238, 144, 255) ,
'hotpink4' : (139, 58, 98, 255) ,
'darkseagreen4' : (105, 139, 105, 255) ,
'darkseagreen3' : (155, 205, 155, 255) ,
'darkseagreen2' : (180, 238, 180, 255) ,
'darkseagreen1' : (193, 255, 193, 255) ,
'deepskyblue4' : (0, 104, 139, 255) ,
'gray44' : (112, 112, 112, 255) ,
'navyblue' : (0, 0, 128, 255) ,
'darkblue' : (0, 0, 139, 255) ,
'forestgreen' : (34, 139, 34, 255) ,
'gray53' : (135, 135, 135, 255) ,
'grey100' : (255, 255, 255, 255) ,
'brown1' : (255, 64, 64, 255) ,
}

for k,v in THECOLORS.items():
    THECOLORS[unicode_(k)] = v
예제 #12
0
import pygame
from pygame.locals import *

import sys
try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO
from pygame.compat import unicode_

QUIT_CHAR = unicode_('q')

usage = """\
python movieplayer.py <movie file>

A simple movie player that plays an MPEG movie in a Pygame window. It showcases
the pygame.movie module. The window adjusts to the size of the movie image. It
is given a boarder to demonstrate that a movie can play autonomously in a sub-
window. Also, the file is copied to a file like object to show that not just
Python files can be used as a movie source.
"""


def main(filepath):
    pygame.init()
    pygame.mixer.quit()

    f = BytesIO(open(filepath, 'rb').read())
    movie = pygame.movie.Movie(f)
    w, h = movie.get_size()
    w = int(w * 1.3 + 0.5)
예제 #13
0
 def test_load_unicode_path_0(self):
     u = unicode_(example_path("data/alien1.png"))
     im = imageext.load_extended(u)
예제 #14
0
def main():
    # initialize
    pygame.init()
    resolution = 400, 200
    screen = pygame.display.set_mode(resolution)

    ##    pygame.mouse.set_cursor(*pygame.cursors.diamond)

    fg = 250, 240, 230
    bg = 5, 5, 5
    wincolor = 40, 40, 90

    # fill background
    screen.fill(wincolor)

    # load font, prepare values
    font = pygame.font.Font(None, 80)
    text = 'Fonty'
    size = font.size(text)

    # no AA, no transparancy, normal
    ren = font.render(text, 0, fg, bg)
    screen.blit(ren, (10, 10))

    # no AA, transparancy, underline
    font.set_underline(1)
    ren = font.render(text, 0, fg)
    screen.blit(ren, (10, 40 + size[1]))
    font.set_underline(0)

    a_sys_font = pygame.font.SysFont("Arial", 60)

    # AA, no transparancy, bold
    a_sys_font.set_bold(1)
    ren = a_sys_font.render(text, 1, fg, bg)
    screen.blit(ren, (30 + size[0], 10))
    a_sys_font.set_bold(0)

    # AA, transparancy, italic
    a_sys_font.set_italic(1)
    ren = a_sys_font.render(text, 1, fg)
    screen.blit(ren, (30 + size[0], 40 + size[1]))
    a_sys_font.set_italic(0)

    # Get some metrics.
    print("Font metrics for 'Fonty':  %s" % a_sys_font.metrics(text))
    ch = unicode_("%c") % 0x3060
    msg = (unicode_("Font metrics for '%s':  %s") %
           (ch, a_sys_font.metrics(ch)))
    print_unicode(msg)

    ## #some_japanese_unicode = u"\u304b\u3070\u306b"
    ##some_japanese_unicode = unicode_('%c%c%c') % (0x304b, 0x3070, 0x306b)

    # AA, transparancy, italic
    ##ren = a_sys_font.render(some_japanese_unicode, 1, fg)
    ##screen.blit(ren, (30 + size[0], 40 + size[1]))

    # show the surface and await user quit
    pygame.display.flip()
    while 1:
        # use event.wait to keep from polling 100% cpu
        if pygame.event.wait().type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
            break
예제 #15
0
 def test_load_unicode_path(self):
     u = unicode_(example_path("data/alien1.png"))
     im = load_extended(u)
예제 #16
0
#!/usr/bin/env python

import pygame
from pygame.locals import *

import sys

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO
from pygame.compat import unicode_

QUIT_CHAR = unicode_("q")

usage = """\
python movieplayer.py <movie file>

A simple movie player that plays an MPEG movie in a Pygame window. It showcases
the pygame.movie module. The window adjusts to the size of the movie image. It
is given a boarder to demonstrate that a movie can play autonomously in a sub-
window. Also, the file is copied to a file like object to show that not just
Python files can be used as a movie source.

"""


def main(filepath):
    pygame.init()
    pygame.mixer.quit()
예제 #17
0
    "darkorchid": (153, 50, 204, 255),
    "thistle3": (205, 181, 205, 255),
    "thistle2": (238, 210, 238, 255),
    "thistle1": (255, 225, 255, 255),
    "salmon": (250, 128, 114, 255),
    "gray93": (237, 237, 237, 255),
    "thistle4": (139, 123, 139, 255),
    "gray39": (99, 99, 99, 255),
    "lawngreen": (124, 252, 0, 255),
    "hotpink3": (205, 96, 144, 255),
    "hotpink2": (238, 106, 167, 255),
    "hotpink1": (255, 110, 180, 255),
    "lightgreen": (144, 238, 144, 255),
    "hotpink4": (139, 58, 98, 255),
    "darkseagreen4": (105, 139, 105, 255),
    "darkseagreen3": (155, 205, 155, 255),
    "darkseagreen2": (180, 238, 180, 255),
    "darkseagreen1": (193, 255, 193, 255),
    "deepskyblue4": (0, 104, 139, 255),
    "gray44": (112, 112, 112, 255),
    "navyblue": (0, 0, 128, 255),
    "darkblue": (0, 0, 139, 255),
    "forestgreen": (34, 139, 34, 255),
    "gray53": (135, 135, 135, 255),
    "grey100": (255, 255, 255, 255),
    "brown1": (255, 64, 64, 255),
}

for k, v in THECOLORS.items():
    THECOLORS[unicode_(k)] = v
예제 #18
0
    def test_sound_args(self):
        def get_bytes(snd):
            return snd.get_raw()

        mixer.init()

        sample = as_bytes("\x00\xff") * 24
        wave_path = example_path(os.path.join("data", "house_lo.wav"))
        uwave_path = unicode_(wave_path)
        bwave_path = uwave_path.encode(sys.getfilesystemencoding())
        snd = mixer.Sound(file=wave_path)
        self.assertTrue(snd.get_length() > 0.5)
        snd_bytes = get_bytes(snd)
        self.assertTrue(len(snd_bytes) > 1000)

        self.assertEqual(get_bytes(mixer.Sound(wave_path)), snd_bytes)

        self.assertEqual(get_bytes(mixer.Sound(file=uwave_path)), snd_bytes)
        self.assertEqual(get_bytes(mixer.Sound(uwave_path)), snd_bytes)
        arg_emsg = "Sound takes either 1 positional or 1 keyword argument"

        with self.assertRaises(TypeError) as cm:
            mixer.Sound()
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(wave_path, buffer=sample)
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(sample, file=wave_path)
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(buffer=sample, file=wave_path)
        self.assertEqual(str(cm.exception), arg_emsg)

        with self.assertRaises(TypeError) as cm:
            mixer.Sound(foobar=sample)
        self.assertEqual(str(cm.exception),
                         "Unrecognized keyword argument 'foobar'")

        snd = mixer.Sound(wave_path, **{})
        self.assertEqual(get_bytes(snd), snd_bytes)
        snd = mixer.Sound(*[], **{"file": wave_path})

        with self.assertRaises(TypeError) as cm:
            mixer.Sound([])
        self.assertEqual(str(cm.exception),
                         "Unrecognized argument (type list)")

        with self.assertRaises(TypeError) as cm:
            snd = mixer.Sound(buffer=[])
        emsg = "Expected object with buffer interface: got a list"
        self.assertEqual(str(cm.exception), emsg)

        ufake_path = unicode_("12345678")
        self.assertRaises(IOError, mixer.Sound, ufake_path)
        self.assertRaises(IOError, mixer.Sound, "12345678")

        with self.assertRaises(TypeError) as cm:
            mixer.Sound(buffer=unicode_("something"))
        emsg = "Unicode object not allowed as buffer object"
        self.assertEqual(str(cm.exception), emsg)
        self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample)
        if type(sample) != str:
            somebytes = get_bytes(mixer.Sound(sample))
            # on python 2 we do not allow using string except as file name.
            self.assertEqual(somebytes, sample)
        self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)), snd_bytes)
        self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes)

        snd = mixer.Sound(wave_path)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(wave_path, array=snd)
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(buffer=sample, array=snd)
        self.assertEqual(str(cm.exception), arg_emsg)
        snd2 = mixer.Sound(array=snd)
        self.assertEqual(snd.get_raw(), snd2.get_raw())
예제 #19
0
 def test_unicode_(self):
     self.failUnless(hasattr(compat.unicode_(), 'isdecimal'))
예제 #20
0
#
# These methods are called internally by pygame.scrap
#
from AppKit import *
from Foundation import *

import sys
import tempfile
import pygame.image
from pygame.locals import SCRAP_TEXT, SCRAP_BMP, SCRAP_SELECTION, SCRAP_CLIPBOARD
from cStringIO import StringIO
from pygame.compat import unicode_

ScrapPboardType = unicode_('org.pygame.scrap')

err = "Only text has been implemented for scrap on mac. See lib/mac_scrap.py to debug."


def init():
    return 1


def get(scrap_type):
    board = NSPasteboard.generalPasteboard()

    if 0:
        print(board.types)
        print(dir(board.types))
        print(dir(board))
        print(board.__doc__)
예제 #21
0
    def test_freetype_Face_cache(self):
        glyphs = "abcde"
        glen = len(glyphs)
        other_glyphs = "123"
        oglen = len(other_glyphs)
        uempty = unicode_("")
##        many_glyphs = (uempty.join([unichr_(i) for i in range(32,127)] +
##                                   [unichr_(i) for i in range(161,172)] +
##                                   [unichr_(i) for i in range(174,239)]))
        many_glyphs = uempty.join([unichr_(i) for i in range(32,127)])
        mglen = len(many_glyphs)

        count = 0
        access = 0
        hit = 0
        miss = 0

        f = ft.Face(None, ptsize=24, style=ft.STYLE_NORMAL, vertical=False)
        f.antialiased = True

        # Ensure debug counters are zero
        self.assertEqual(f._debug_cache_stats, (0, 0, 0, 0, 0))
        # Load some basic glyphs
        count = access = miss = glen
        f.render_raw(glyphs)
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Vertical should not affect the cache
        access += glen
        hit += glen
        f.vertical = True
        f.render_raw(glyphs)
        f.vertical = False
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # New glyphs will
        count += oglen
        access += oglen
        miss += oglen
        f.render_raw(other_glyphs)
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Point size does
        count += glen
        access += glen
        miss += glen
        f.render_raw(glyphs, ptsize=12)
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Underline style does not
        access += oglen
        hit += oglen
        f.underline = True
        f.render_raw(other_glyphs)
        f.underline = False
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Oblique style does
        count += glen
        access += glen
        miss += glen
        f.oblique = True
        f.render_raw(glyphs)
        f.oblique = False
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Strong style does; by this point cache clears can happen
        count += glen
        access += glen
        miss += glen
        f.strong = True
        f.render_raw(glyphs)
        f.strong = False
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))
        # Rotation does
        count += glen
        access += glen
        miss += glen
        f.render_raw(glyphs, rotation=10)
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))
        # aliased (mono) glyphs do
        count += oglen
        access += oglen
        miss += oglen
        f.antialiased = False
        f.render_raw(other_glyphs)
        f.antialiased = True
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))
        # Trigger a cleanup for sure.
        count += 2 * mglen
        access += 2 * mglen
        miss += 2 * mglen
        f.get_metrics(many_glyphs, ptsize=8)
        f.get_metrics(many_glyphs, ptsize=10)
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        print (ccount, cdelete_count, caccess, chit, cmiss)
        self.assertTrue(ccount < count)
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))
예제 #22
0
 def test_unicode_(self):
     self.failUnless(hasattr(compat.unicode_(), 'isdecimal'))
예제 #23
0
 def test_sound_args(self):
     def get_bytes(snd):
         return snd.get_buffer().raw
     
     mixer.init()
     try:
         sample = as_bytes('\x00\xff') * 24
         wave_path = example_path(os.path.join('data', 'house_lo.wav'))
         uwave_path = unicode_(wave_path)
         bwave_path = uwave_path.encode(sys.getfilesystemencoding())
         snd = mixer.Sound(file=wave_path)
         self.assert_(snd.get_length() > 0.5)
         snd_bytes = get_bytes(snd)
         self.assert_(len(snd_bytes) > 1000)
         self.assert_(get_bytes(mixer.Sound(wave_path)) == snd_bytes)
         self.assert_(get_bytes(mixer.Sound(file=uwave_path)) == snd_bytes)
         self.assert_(get_bytes(mixer.Sound(uwave_path)) == snd_bytes)
         arg_emsg = 'Sound takes either 1 positional or 1 keyword argument'
         try:
             mixer.Sound()
         except TypeError:
             self.assertEqual(str(geterror()), arg_emsg)
         else:
             self.fail("no exception")
         try:
             mixer.Sound(wave_path, buffer=sample)
         except TypeError:
             self.assertEqual(str(geterror()), arg_emsg)
         else:
             self.fail("no exception")
         try:
             mixer.Sound(sample, file=wave_path)
         except TypeError:
             self.assertEqual(str(geterror()), arg_emsg)
         else:
             self.fail("no exception")           
         try:
             mixer.Sound(buffer=sample, file=wave_path)
         except TypeError:
             self.assertEqual(str(geterror()), arg_emsg)
         else:
             self.fail("no exception")           
         try:
             mixer.Sound(foobar=sample)
         except TypeError:
             emsg = "Unrecognized keyword argument 'foobar'"
             self.assertEqual(str(geterror()), emsg)
         else:
             self.fail("no exception")
         snd = mixer.Sound(wave_path, **{})
         self.assertEqual(get_bytes(snd), snd_bytes)
         snd = mixer.Sound(*[], **{'file': wave_path})          
         try:
            snd = mixer.Sound([])
         except TypeError:
             emsg = 'Unrecognized argument (type list)'
             self.assertEqual(str(geterror()), emsg)
         else:   
             self.fail("no exception")
         try:
             snd = mixer.Sound(buffer=[])
         except TypeError:
             emsg = 'Expected object with buffer interface: got a list'
             self.assertEqual(str(geterror()), emsg)
         else:
             self.fail("no exception")
         ufake_path = unicode_('12345678')
         self.assertRaises(pygame.error, mixer.Sound, ufake_path)
         try:
             mixer.Sound(buffer=unicode_('something'))
         except TypeError:
             emsg = 'Unicode object not allowed as buffer object'
             self.assertEqual(str(geterror()), emsg)
         else:
             self.fail("no exception")
         self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample)
         self.assertEqual(get_bytes(mixer.Sound(sample)), sample)
         self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)), snd_bytes)
         self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes)
         
         snd = mixer.Sound(wave_path)
         try:
             mixer.Sound(wave_path, array=snd)
         except TypeError:
             self.assertEqual(str(geterror()), arg_emsg)
         else:
             self.fail("no exception")
         try:
             mixer.Sound(buffer=sample, array=snd)
         except TypeError:
             self.assertEqual(str(geterror()), arg_emsg)
         else:
             self.fail("no exception")
         snd2 = mixer.Sound(array=snd)
         self.assertEqual(snd.get_buffer().raw, snd2.get_buffer().raw)
         
     finally:
         mixer.quit()
예제 #24
0
def main():
    #initialize
    pygame.init()
    resolution = 400, 200
    screen = pygame.display.set_mode(resolution)

##    pygame.mouse.set_cursor(*pygame.cursors.diamond)

    fg = 250, 240, 230
    bg = 5, 5, 5
    wincolor = 40, 40, 90

    #fill background
    screen.fill(wincolor)

    #load font, prepare values
    font = pygame.font.Font(None, 80)
    text = 'Fonty'
    size = font.size(text)

    #no AA, no transparancy, normal
    ren = font.render(text, 0, fg, bg)
    screen.blit(ren, (10, 10))

    #no AA, transparancy, underline
    font.set_underline(1)
    ren = font.render(text, 0, fg)
    screen.blit(ren, (10, 40 + size[1]))
    font.set_underline(0)


    a_sys_font = pygame.font.SysFont("Arial", 60)


    #AA, no transparancy, bold
    a_sys_font.set_bold(1)
    ren = a_sys_font.render(text, 1, fg, bg)
    screen.blit(ren, (30 + size[0], 10))
    a_sys_font.set_bold(0)

    #AA, transparancy, italic
    a_sys_font.set_italic(1)
    ren = a_sys_font.render(text, 1, fg)
    screen.blit(ren, (30 + size[0], 40 + size[1]))
    a_sys_font.set_italic(0)


    # Get some metrics.
    print ("Font metrics for 'Fonty':  %s" % a_sys_font.metrics (text))
    ch = unicode_("%c") % 0x3060
    msg = (unicode_("Font metrics for '%s':  %s") %
           (ch, a_sys_font.metrics (ch)))
    print_unicode(msg)

    ## #some_japanese_unicode = u"\u304b\u3070\u306b"
    ##some_japanese_unicode = unicode_('%c%c%c') % (0x304b, 0x3070, 0x306b)
    
    #AA, transparancy, italic
    ##ren = a_sys_font.render(some_japanese_unicode, 1, fg)
    ##screen.blit(ren, (30 + size[0], 40 + size[1]))
    




    #show the surface and await user quit
    pygame.display.flip()
    while 1:
        #use event.wait to keep from polling 100% cpu
        if pygame.event.wait().type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
            break
예제 #25
0
    def test_sound_args(self):
        def get_bytes(snd):
            return snd.get_raw()

        mixer.init()

        sample = as_bytes('\x00\xff') * 24
        wave_path = example_path(os.path.join('data', 'house_lo.wav'))
        uwave_path = unicode_(wave_path)
        bwave_path = uwave_path.encode(sys.getfilesystemencoding())
        snd = mixer.Sound(file=wave_path)
        self.assertTrue(snd.get_length() > 0.5)
        snd_bytes = get_bytes(snd)
        self.assertTrue(len(snd_bytes) > 1000)

        # TODO: FIXME: skipped because appveyor fails here sometimes.
        # self.assertEqual(get_bytes(mixer.Sound(wave_path)), snd_bytes)

        self.assertEqual(get_bytes(mixer.Sound(file=uwave_path)), snd_bytes)
        self.assertEqual(get_bytes(mixer.Sound(uwave_path)), snd_bytes)
        arg_emsg = 'Sound takes either 1 positional or 1 keyword argument'

        with self.assertRaises(TypeError) as cm:
            mixer.Sound()
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(wave_path, buffer=sample)
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(sample, file=wave_path)
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(buffer=sample, file=wave_path)
        self.assertEqual(str(cm.exception), arg_emsg)

        with self.assertRaises(TypeError) as cm:
            mixer.Sound(foobar=sample)
        self.assertEqual(str(cm.exception),
                         "Unrecognized keyword argument 'foobar'")

        snd = mixer.Sound(wave_path, **{})
        self.assertEqual(get_bytes(snd), snd_bytes)
        snd = mixer.Sound(*[], **{'file': wave_path})

        with self.assertRaises(TypeError) as cm:
            mixer.Sound([])
        self.assertEqual(str(cm.exception),
                         'Unrecognized argument (type list)')

        with self.assertRaises(TypeError) as cm:
            snd = mixer.Sound(buffer=[])
        emsg = 'Expected object with buffer interface: got a list'
        self.assertEqual(str(cm.exception), emsg)

        ufake_path = unicode_('12345678')
        self.assertRaises(IOError, mixer.Sound, ufake_path)
        self.assertRaises(IOError, mixer.Sound, '12345678')

        with self.assertRaises(TypeError) as cm:
            mixer.Sound(buffer=unicode_('something'))
        emsg = 'Unicode object not allowed as buffer object'
        self.assertEqual(str(cm.exception), emsg)
        self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample)
        if type(sample) != str:
            somebytes = get_bytes(mixer.Sound(sample))
            # on python 2 we do not allow using string except as file name.
            self.assertEqual(somebytes, sample)
        self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)), snd_bytes)
        self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes)

        snd = mixer.Sound(wave_path)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(wave_path, array=snd)
        self.assertEqual(str(cm.exception), arg_emsg)
        with self.assertRaises(TypeError) as cm:
            mixer.Sound(buffer=sample, array=snd)
        self.assertEqual(str(cm.exception), arg_emsg)
        snd2 = mixer.Sound(array=snd)
        self.assertEqual(snd.get_raw(), snd2.get_raw())
예제 #26
0
if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5: # condi. and
    # On NT like Windows versions smpeg video needs windb.
    os.environ['SDL_VIDEODRIVER'] = 'windib'
    
import pygame
import pygame._movie
from pygame.locals import *

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO
from pygame.compat import unicode_

QUIT_CHAR = unicode_('q')

usage = """\
python movieplayer.py <movie file>

A simple movie player that plays an MPEG movie in a Pygame window. It showcases
the pygame.movie module. The window adjusts to the size of the movie image. It
is given a boarder to demonstrate that a movie can play autonomously in a sub-
window. Also, the file is copied to a file like object to show that not just
Python files can be used as a movie source.

"""

def main(filepath):
    pygame.init()
    pygame.mixer.quit()
예제 #27
0
    def test_sound_args(self):
        def get_bytes(snd):
            return snd.get_raw()

        mixer.init()
        try:
            sample = as_bytes('\x00\xff') * 24
            wave_path = example_path(os.path.join('data', 'house_lo.wav'))
            uwave_path = unicode_(wave_path)
            bwave_path = uwave_path.encode(sys.getfilesystemencoding())
            snd = mixer.Sound(file=wave_path)
            self.assert_(snd.get_length() > 0.5)
            snd_bytes = get_bytes(snd)
            self.assert_(len(snd_bytes) > 1000)
            self.assert_(get_bytes(mixer.Sound(wave_path)) == snd_bytes)
            self.assert_(get_bytes(mixer.Sound(file=uwave_path)) == snd_bytes)
            self.assert_(get_bytes(mixer.Sound(uwave_path)) == snd_bytes)
            arg_emsg = 'Sound takes either 1 positional or 1 keyword argument'
            try:
                mixer.Sound()
            except TypeError:
                self.assertEqual(str(geterror()), arg_emsg)
            else:
                self.fail("no exception")
            try:
                mixer.Sound(wave_path, buffer=sample)
            except TypeError:
                self.assertEqual(str(geterror()), arg_emsg)
            else:
                self.fail("no exception")
            try:
                mixer.Sound(sample, file=wave_path)
            except TypeError:
                self.assertEqual(str(geterror()), arg_emsg)
            else:
                self.fail("no exception")
            try:
                mixer.Sound(buffer=sample, file=wave_path)
            except TypeError:
                self.assertEqual(str(geterror()), arg_emsg)
            else:
                self.fail("no exception")
            try:
                mixer.Sound(foobar=sample)
            except TypeError:
                emsg = "Unrecognized keyword argument 'foobar'"
                self.assertEqual(str(geterror()), emsg)
            else:
                self.fail("no exception")
            snd = mixer.Sound(wave_path, **{})
            self.assertEqual(get_bytes(snd), snd_bytes)
            snd = mixer.Sound(*[], **{'file': wave_path})
            try:
                snd = mixer.Sound([])
            except TypeError:
                emsg = 'Unrecognized argument (type list)'
                self.assertEqual(str(geterror()), emsg)
            else:
                self.fail("no exception")
            try:
                snd = mixer.Sound(buffer=[])
            except TypeError:
                emsg = 'Expected object with buffer interface: got a list'
                self.assertEqual(str(geterror()), emsg)
            else:
                self.fail("no exception")
            ufake_path = unicode_('12345678')
            self.assertRaises(pygame.error, mixer.Sound, ufake_path)
            try:
                mixer.Sound(buffer=unicode_('something'))
            except TypeError:
                emsg = 'Unicode object not allowed as buffer object'
                self.assertEqual(str(geterror()), emsg)
            else:
                self.fail("no exception")
            self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample)
            self.assertEqual(get_bytes(mixer.Sound(sample)), sample)
            self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)),
                             snd_bytes)
            self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes)

            snd = mixer.Sound(wave_path)
            try:
                mixer.Sound(wave_path, array=snd)
            except TypeError:
                self.assertEqual(str(geterror()), arg_emsg)
            else:
                self.fail("no exception")
            try:
                mixer.Sound(buffer=sample, array=snd)
            except TypeError:
                self.assertEqual(str(geterror()), arg_emsg)
            else:
                self.fail("no exception")
            snd2 = mixer.Sound(array=snd)
            self.assertEqual(snd.get_raw(), snd2.get_raw())

        finally:
            mixer.quit()
예제 #28
0
#
# These methods are called internally by pygame.scrap
#
from cStringIO import StringIO
import sys
import tempfile

from pygame.compat import unicode_
import pygame.image
from pygame.locals import SCRAP_TEXT, SCRAP_BMP, SCRAP_SELECTION, SCRAP_CLIPBOARD

from AppKit import *
from Foundation import *


ScrapPboardType = unicode_('org.pygame.scrap')


err = "Only text has been implemented for scrap on mac. See lib/mac_scrap.py to debug."



def init():
    return 1

def get(scrap_type):
    board = NSPasteboard.generalPasteboard()
    
    if 0:
        print (board.types)
        print (dir(board.types))