Commits

Lenard Lindstrom committed bb1dac8

for bitmap files freetype.Font uses first available size as default (towards Issue #75)

Comments (0)

Files changed (3)

docs/reST/ref/freetype.rst

    passed explicitly to each method call. Because of the way the caching
    system works, specifying a default size on the constructor doesn't imply a
    performance gain over manually passing the size on each function call.
+   If the font is bitmap and no *size* is given, the default size is set
+   to the first available size for the font, if possible.
 
    If the font file has more than one font, the font to load can be chosen with
    the *index* argument. An exception is raised for an out-of-range font index
     Scale_t face_size = self->face_size;
     int ucs4 = self->render_flags & FT_RFLAG_UCS4 ? 1 : 0;
     unsigned resolution = 0;
+    long size = 0;
+    long height = 0;
+    long width = 0;
+    double x_ppem = 0;
+    double y_ppem = 0;
+    int rval = -1;
 
     FreeTypeInstance *ft;
     ASSERT_GRAB_FREETYPE(ft, -1);
         return -1;
     }
     if (Bytes_Check(file)) {
-        if (_PGFT_TryLoadFont_Filename(ft, self, Bytes_AS_STRING(file),
-                                       font_index)) {
-            goto end;
-        }
-
         if (PyUnicode_Check(original_file)) {
             /* Make sure to save a pure Unicode object to prevent possible
              * cycles from a derived class. This means no tp_traverse or
             self->path = PyUnicode_FromEncodedObject(file, "raw_unicode_escape",
                                                      "replace");
         }
+        if (!self->path) {
+            goto end;
+        }
+
+        if (_PGFT_TryLoadFont_Filename(ft, self, Bytes_AS_STRING(file),
+                                       font_index)) {
+            goto end;
+        }
     }
     else {
         SDL_RWops *source = RWopsFromFileObjectThreaded(original_file);
             goto end;
         }
 
-        if (_PGFT_TryLoadFont_RWops(ft, self, source, font_index)) {
-            goto end;
-        }
-
         path = PyObject_GetAttrString(original_file, "name");
         if (!path) {
             PyErr_Clear();
             self->path = Object_Unicode(path);
         }
         Py_XDECREF(path);
+        if (!self->path) {
+            goto end;
+        }
+
+        if (_PGFT_TryLoadFont_RWops(ft, self, source, font_index)) {
+            goto end;
+        }
     }
 
+    if (!self->is_scalable && self->face_size.x == 0) {
+        if (_PGFT_Font_GetAvailableSize(ft, self, 0,
+                                        &size, &height, &width,
+                                        &x_ppem, &y_ppem)) {
+            self->face_size.x = DBL_TO_FX6(x_ppem);
+            self->face_size.y = DBL_TO_FX6(y_ppem);
+        }
+        else {
+            PyErr_Clear();
+        }
+    }
+    rval = 0;
+
 end:
-
     if (file != original_file) {
         Py_XDECREF(file);
     }
 
-    return PyErr_Occurred() ? -1 : 0;
+    return rval;
 }
 
 static PyObject *

test/freetype_test.py

         self.assertTrue(f.oblique)
         self.assertTrue(f.ucs4)
 
+        # For a bitmap font, the size is automatically set to the first
+        # size in the available sizes list.
+        f = ft.Font(self._bmp_8_75dpi_path)
+        sizes = f.get_sizes()
+        self.assertEqual(len(sizes), 1)
+        size_pt, width_px, height_px, x_ppem, y_ppem = sizes[0]
+        self.assertEqual(f.size, (x_ppem, y_ppem))
+        f.__init__(self._bmp_8_75dpi_path, size=12)
+        self.assertEqual(f.size, 12.0)
+
     def test_freetype_Font_scalable(self):
 
         f = self._TEST_FONTS['sans']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.