- Load also the last font file in the font directory.
[reactos.git] / reactos / subsys / win32k / objects / text.c
index a41b7a3..4934617 100644 (file)
@@ -22,7 +22,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: text.c,v 1.96 2004/06/18 15:18:58 navaraf Exp $ */
+/* $Id: text.c,v 1.110 2004/08/24 17:29:00 navaraf Exp $ */
 #include <w32k.h>
 
 #include <ft2build.h>
@@ -44,7 +44,6 @@ static FAST_MUTEX FreeTypeLock;
 
 static LIST_ENTRY FontListHead;
 static FAST_MUTEX FontListLock;
-static INT FontsLoaded = 0; /* number of all fonts loaded (including private fonts */
 static BOOL RenderingEnabled = TRUE;
 
 static PWCHAR ElfScripts[32] = { /* these are in the order of the fsCsb[0] bits */
@@ -114,291 +113,342 @@ static CHARSETINFO FontTci[MAXTCIINDEX] = {
   { SYMBOL_CHARSET, CP_SYMBOL, FS(31)},
 };
 
+VOID FASTCALL
+IntLoadSystemFonts(VOID);
+
+INT FASTCALL
+IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics);
 
 BOOL FASTCALL
-IntIsFontRenderingEnabled(VOID)
+InitFontSupport(VOID)
 {
-  BOOL Ret;
-  HDC hDC;
-  PDC dc;
-  SURFOBJ *SurfObj;
-  Ret = RenderingEnabled;
-  hDC = IntGetScreenDC();
-  if(hDC)
-  {
-    dc = DC_LockDc(hDC);
-    SurfObj = (SURFOBJ*)AccessUserObject((ULONG) dc->Surface);
-    if(SurfObj)
-      Ret = (SurfObj->iBitmapFormat >= BMF_8BPP);
-    DC_UnlockDc(hDC);
-  }
-  return Ret;
+   ULONG ulError;
+
+   InitializeListHead(&FontListHead);
+   ExInitializeFastMutex(&FontListLock);
+   ExInitializeFastMutex(&FreeTypeLock);
+
+   ulError = FT_Init_FreeType(&library);
+   if (ulError)
+      return FALSE;
+
+   IntLoadSystemFonts();
+
+   return TRUE;
 }
 
+/*
+ * IntLoadSystemFonts
+ *
+ * Search the system font directory and adds each font found.
+ */
+
 VOID FASTCALL
-IntEnableFontRendering(BOOL Enable)
+IntLoadSystemFonts(VOID)
 {
-  RenderingEnabled = Enable;
-}
+   OBJECT_ATTRIBUTES ObjectAttributes;
+   UNICODE_STRING Directory, SearchPattern, FileName, TempString;
+   IO_STATUS_BLOCK Iosb;
+   HANDLE hDirectory;
+   BYTE *DirInfoBuffer;
+   PFILE_DIRECTORY_INFORMATION DirInfo;
+   BOOL bRestartScan = TRUE;
+   NTSTATUS Status;
 
-FT_Render_Mode FASTCALL
-IntGetFontRenderMode(LOGFONTW *logfont)
-{  
-  switch(logfont->lfQuality)
-  {
-    //case ANTIALIASED_QUALITY:
-    case DEFAULT_QUALITY:
-      return FT_RENDER_MODE_NORMAL;
-    case DRAFT_QUALITY:
-      return FT_RENDER_MODE_LIGHT;
-    //case NONANTIALIASED_QUALITY:
-    case PROOF_QUALITY:
-      return FT_RENDER_MODE_MONO;
-    //case CLEARTYPE_QUALITY:
-    //  return FT_RENDER_MODE_LCD;
-  }
-  return FT_RENDER_MODE_MONO;
+   RtlInitUnicodeString(&Directory, L"\\SystemRoot\\media\\fonts\\");
+   /* FIXME: Add support for other font types */
+   RtlInitUnicodeString(&SearchPattern, L"*.ttf");
+
+   InitializeObjectAttributes(
+      &ObjectAttributes,
+      &Directory,
+      OBJ_CASE_INSENSITIVE,
+      NULL,
+      NULL);
+
+   Status = ZwOpenFile(
+      &hDirectory,
+      SYNCHRONIZE | FILE_LIST_DIRECTORY,
+      &ObjectAttributes,
+      &Iosb,
+      FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+      FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE);
+
+   if (NT_SUCCESS(Status))
+   {
+      DirInfoBuffer = ExAllocatePool(PagedPool, 0x4000);
+      if (DirInfoBuffer == NULL)
+      {
+         ZwClose(hDirectory);
+         return;
+      }
+      
+      FileName.Buffer = ExAllocatePool(PagedPool, MAX_PATH);
+      if (FileName.Buffer == NULL)
+      {
+         ExFreePool(DirInfoBuffer);
+         ZwClose(hDirectory);
+         return;
+      }
+      FileName.Length = 0;
+      FileName.MaximumLength = MAX_PATH;
+
+      while (1)
+      {
+         Status = ZwQueryDirectoryFile(
+            hDirectory,
+            NULL,
+            NULL,
+            NULL,
+            &Iosb,
+            DirInfoBuffer,
+            0x4000,
+            FileDirectoryInformation,
+            FALSE,
+            &SearchPattern,
+            bRestartScan);
+
+         if (!NT_SUCCESS(Status) || Status == STATUS_NO_MORE_FILES)
+         {
+            break;
+         }
+
+         DirInfo = (PFILE_DIRECTORY_INFORMATION)DirInfoBuffer;
+         while (1)
+         {
+            TempString.Buffer = DirInfo->FileName;
+            TempString.Length =
+            TempString.MaximumLength = DirInfo->FileNameLength;
+            RtlCopyUnicodeString(&FileName, &Directory);
+            RtlAppendUnicodeStringToString(&FileName, &TempString);
+            IntGdiAddFontResource(&FileName, 0);
+            if (DirInfo->NextEntryOffset == 0)
+               break;
+            DirInfo = (PFILE_DIRECTORY_INFORMATION)((ULONG_PTR)DirInfo + DirInfo->NextEntryOffset);
+         }
+
+         bRestartScan = FALSE;
+      }
+
+      ExFreePool(FileName.Buffer);
+      ExFreePool(DirInfoBuffer);
+      ZwClose(hDirectory);
+   }
 }
 
-int FASTCALL
-IntGdiAddFontResource(PUNICODE_STRING Filename, DWORD fl)
+/*
+ * IntGdiAddFontResource
+ *
+ * Adds the font resource from the specified file to the system.
+ */
+
+INT FASTCALL
+IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
 {
-  HFONT NewFont;
-  FONTOBJ *FontObj;
-  PFONTGDI FontGDI;
-  NTSTATUS Status;
-  HANDLE FileHandle;
-  OBJECT_ATTRIBUTES ObjectAttributes;
-  FILE_STANDARD_INFORMATION FileStdInfo;
-  PVOID buffer;
-  ULONG size;
-  INT error;
-  FT_Face face;
-  ANSI_STRING StringA;
-  IO_STATUS_BLOCK Iosb;
-  PFONT_ENTRY entry;
+   HFONT NewFont;
+   FONTOBJ *FontObj;
+   PFONTGDI FontGDI;
+   NTSTATUS Status;
+   HANDLE FileHandle;
+   OBJECT_ATTRIBUTES ObjectAttributes;
+   FILE_STANDARD_INFORMATION FileStdInfo;
+   PVOID Buffer;
+   IO_STATUS_BLOCK Iosb;
+   INT Error;
+   FT_Face Face;
+   ANSI_STRING AnsiFaceName;
+   PFONT_ENTRY Entry;
+
+   /* Create handle for the font */
+
+   NewFont = (HFONT)CreateGDIHandle(
+      sizeof(FONTGDI),
+      sizeof(FONTOBJ),
+      (PVOID*)&FontGDI,
+      (PVOID*)&FontObj);
+
+   if (NewFont == 0)
+   {
+      DPRINT("Could not allocate a new GDI font object\n");
+      return 0;
+   }
 
-  NewFont = (HFONT)CreateGDIHandle(sizeof( FONTGDI ), sizeof( FONTOBJ ), (PVOID*)&FontGDI, (PVOID*)&FontObj);
-  if(NewFont == 0)
-  {
-    DPRINT1("Could not allocate a new GDI font object\n");
-    return 0;
-  }
+   /* Open the font file */
+
+   InitializeObjectAttributes(&ObjectAttributes, FileName, 0, NULL, NULL);
+   Status = ZwOpenFile(
+      &FileHandle,
+      GENERIC_READ | SYNCHRONIZE,
+      &ObjectAttributes,
+      &Iosb,
+      0,
+      FILE_SYNCHRONOUS_IO_NONALERT);
 
-  //  Open the Module
-  InitializeObjectAttributes(&ObjectAttributes, Filename, 0, NULL, NULL);
+   if (!NT_SUCCESS(Status))
+   {
+      DPRINT("Could not font file: %wZ\n", FileName);
+      NtGdiDeleteObject(NewFont);
+      return 0;
+   }
 
-  Status = ZwOpenFile(&FileHandle, 
-                      GENERIC_READ|SYNCHRONIZE, 
-                      &ObjectAttributes, 
-                      &Iosb, 
-                      0, //ShareAccess
-                      FILE_SYNCHRONOUS_IO_NONALERT);
+   /* Get the size of the file */
 
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Could not open module file: %wZ\n", Filename);
-    return 0;
-  }
+   Status = NtQueryInformationFile(
+      FileHandle,
+      &Iosb,
+      &FileStdInfo,
+      sizeof(FileStdInfo),
+      FileStandardInformation);
 
-  //  Get the size of the file
-  Status = NtQueryInformationFile(FileHandle, &Iosb, &FileStdInfo, sizeof(FileStdInfo), FileStandardInformation);
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("Could not get file size\n");
-    return 0;
-  }
+   if (!NT_SUCCESS(Status))
+   {
+      DPRINT("Could not get file size\n");
+      NtGdiDeleteObject(NewFont);
+      ZwClose(FileHandle);
+      return 0;
+   }
 
-  //  Allocate nonpageable memory for driver
-  size = FileStdInfo.EndOfFile.u.LowPart;
-  buffer = ExAllocatePoolWithTag(NonPagedPool, size, TAG_GDITEXT);
+   /* Allocate pageable memory for the font */
 
-  if (buffer == NULL)
-  {
-    DPRINT1("could not allocate memory for module");
-    return 0;
-  }
+   Buffer = ExAllocatePoolWithTag(
+      PagedPool,
+      FileStdInfo.EndOfFile.u.LowPart,
+      TAG_GDITEXT);
 
-  //  Load driver into memory chunk
-  Status = ZwReadFile(FileHandle, 
-                      NULL, 
-                      NULL, 
-                      NULL, 
-                      &Iosb, 
-                      buffer, 
-                      FileStdInfo.EndOfFile.u.LowPart, 
-                      NULL, 
-                      NULL);
-                      
-  if (!NT_SUCCESS(Status))
-  {
-    DPRINT1("could not read module file into memory");
-    ExFreePool(buffer);
-    return 0;
-  }
+   if (Buffer == NULL)
+   {
+      DPRINT("Could not allocate memory for font");
+      NtGdiDeleteObject(NewFont);
+      ZwClose(FileHandle);
+      return 0;
+   }
 
-  ZwClose(FileHandle);
+   /* Load the font into memory chunk */
 
-  IntLockFreeType;
-  error = FT_New_Memory_Face(library, buffer, size, 0, &face);
-  IntUnLockFreeType;
-  if (error == FT_Err_Unknown_File_Format)
-  {
-    DPRINT1("Unknown font file format\n");
-    return 0;
-  }
-  else if (error)
-  {
-    DPRINT1("Error reading font file (error code: %u)\n", error); // 48
-    return 0;
-  }
-  
-  entry = ExAllocatePoolWithTag(NonPagedPool, sizeof(FONT_ENTRY), TAG_FONT);
-  if(!entry)
-  {
-    SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
-    return 0;
-  }
+   Status = ZwReadFile(
+      FileHandle,
+      NULL,
+      NULL,
+      NULL,
+      &Iosb,
+      Buffer,
+      FileStdInfo.EndOfFile.u.LowPart,
+      NULL,
+      NULL);
 
-  // FontGDI->Filename = Filename; perform strcpy
-  FontGDI->face = face;
+   if (!NT_SUCCESS(Status))
+   {
+      DPRINT("Could not read the font file into memory");
+      ExFreePool(Buffer);
+      NtGdiDeleteObject(NewFont);
+      ZwClose(FileHandle);
+      return 0;
+   }
 
-  // FIXME: Complete text metrics
-  FontGDI->TextMetric.tmAscent = (face->size->metrics.ascender + 32) >> 6; // units above baseline
-  FontGDI->TextMetric.tmDescent = (32 - face->size->metrics.descender) >> 6; // units below baseline
-  FontGDI->TextMetric.tmHeight = FontGDI->TextMetric.tmAscent + FontGDI->TextMetric.tmDescent;
+   ZwClose(FileHandle);
 
-  DPRINT("Font loaded: %s (%s)\n", face->family_name, face->style_name);
-  DPRINT("Num glyphs: %u\n", face->num_glyphs);
+   IntLockFreeType;
+   Error = FT_New_Memory_Face(
+      library,
+      Buffer,
+      FileStdInfo.EndOfFile.u.LowPart,
+      0,
+      &Face);
+   IntUnLockFreeType;
 
-  // Add this font resource to the font table
-  entry->hFont = NewFont;
-  entry->NotEnum = (fl & FR_NOT_ENUM);
-  RtlInitAnsiString(&StringA, (LPSTR)face->family_name);
-  RtlAnsiStringToUnicodeString(&entry->FaceName, &StringA, TRUE);
-  
-  if(fl & FR_PRIVATE)
-  {
-    PW32PROCESS Win32Process = PsGetWin32Process();
-    
-    IntLockProcessPrivateFonts(Win32Process);
-    InsertTailList(&Win32Process->PrivateFontListHead, &entry->ListEntry);
-    FontsLoaded++;
-    IntUnLockProcessPrivateFonts(Win32Process);
-  }
-  else
-  {
-    IntLockGlobalFonts;
-    InsertTailList(&FontListHead, &entry->ListEntry);
-    FontsLoaded++;
-    IntUnLockGlobalFonts;
-  }
+   if (Error)
+   {
+      if (Error == FT_Err_Unknown_File_Format)
+         DPRINT("Unknown font file format\n");
+      else
+         DPRINT("Error reading font file (error code: %u)\n", Error);
+      ExFreePool(Buffer);
+      NtGdiDeleteObject(NewFont);
+      return 0;
+   }
 
-  return 1;
+   Entry = ExAllocatePoolWithTag(PagedPool, sizeof(FONT_ENTRY), TAG_FONT);
+   if (!Entry)
+   {
+      FT_Done_Face(Face);
+      ExFreePool(Buffer);
+      SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
+      return 0;
+   }
+
+   /* FontGDI->Filename = FileName; perform strcpy */
+   FontGDI->face = Face;
+
+   /* FIXME: Complete text metrics */
+   FontGDI->TextMetric.tmAscent = (Face->size->metrics.ascender + 32) >> 6; /* units above baseline */
+   FontGDI->TextMetric.tmDescent = (32 - Face->size->metrics.descender) >> 6; /* units below baseline */
+   FontGDI->TextMetric.tmHeight = (Face->size->metrics.ascender -
+                                   Face->size->metrics.descender) >> 6;
+
+   DPRINT("Font loaded: %s (%s)\n", Face->family_name, Face->style_name);
+   DPRINT("Num glyphs: %u\n", Face->num_glyphs);
+
+   /* Add this font resource to the font table */
+
+   Entry->hFont = NewFont;
+   Entry->NotEnum = (Characteristics & FR_NOT_ENUM);
+   RtlInitAnsiString(&AnsiFaceName, (LPSTR)Face->family_name);
+   RtlAnsiStringToUnicodeString(&Entry->FaceName, &AnsiFaceName, TRUE);
+
+   if (Characteristics & FR_PRIVATE)
+   {
+      PW32PROCESS Win32Process = PsGetWin32Process();
+      IntLockProcessPrivateFonts(Win32Process);
+      InsertTailList(&Win32Process->PrivateFontListHead, &Entry->ListEntry);
+      IntUnLockProcessPrivateFonts(Win32Process);
+   }
+   else
+   {
+      IntLockGlobalFonts;
+      InsertTailList(&FontListHead, &Entry->ListEntry);
+      IntUnLockGlobalFonts;
+   }
+
+   return 1;
 }
 
-BOOL FASTCALL InitFontSupport(VOID)
+BOOL FASTCALL
+IntIsFontRenderingEnabled(VOID)
 {
-       ULONG ulError;
-       UNICODE_STRING cchDir, cchFilename, cchSearchPattern ;
-       OBJECT_ATTRIBUTES obAttr;
-       IO_STATUS_BLOCK Iosb;
-       HANDLE hDirectory;
-       NTSTATUS Status;
-       PFILE_DIRECTORY_INFORMATION iFileData;
-       PVOID   pBuff;
-       BOOLEAN bRestartScan = TRUE;
-    BOOLEAN Result = FALSE;
-       
-       InitializeListHead(&FontListHead);
-    ExInitializeFastMutex(&FontListLock);
-    ExInitializeFastMutex(&FreeTypeLock);
-       
-    ulError = FT_Init_FreeType(&library);
-    
-    if(!ulError)
-    {
-        RtlInitUnicodeString(&cchDir, L"\\SystemRoot\\Media\\Fonts\\");
-
-               RtlInitUnicodeString(&cchSearchPattern,L"*.ttf");
-        InitializeObjectAttributes( &obAttr,
-                                                               &cchDir,
-                                                               OBJ_CASE_INSENSITIVE, 
-                                                               NULL,
-                                                               NULL );
-                                                                       
-        Status = ZwOpenFile( &hDirectory,
-                             SYNCHRONIZE | FILE_LIST_DIRECTORY,
-                             &obAttr,
-                             &Iosb,
-                             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
-                             FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE );         
-        if( NT_SUCCESS(Status) )
-        {          
-            while(1)
-            {   
-               if (bRestartScan)
-                 {
-                pBuff = ExAllocatePool(NonPagedPool,0x4000);
-                   if (pBuff == NULL)
-                     {
-                       break;
-                     }
-                RtlInitUnicodeString(&cchFilename,0);
-                cchFilename.MaximumLength = 0x1000;
-                cchFilename.Buffer = ExAllocatePoolWithTag(PagedPool,cchFilename.MaximumLength, TAG_STRING);
-                   if (cchFilename.Buffer == NULL)
-                     {
-                       ExFreePool(pBuff);
-                       break;
-                     }
-                 }
-                                   
-                               Status = NtQueryDirectoryFile( hDirectory,
-                                                              NULL,
-                                                              NULL,
-                                                              NULL,
-                                                              &Iosb,
-                                                              pBuff,
-                                                              0x4000,
-                                                              FileDirectoryInformation,
-                                              FALSE,
-                                                              &cchSearchPattern,
-                                                              bRestartScan );
-                                  
-                               if( !NT_SUCCESS(Status) || Status == STATUS_NO_MORE_FILES )
-                 {
-                               ExFreePool(pBuff);
-                               ExFreePool(cchFilename.Buffer);
-                   break;
-                 }
-                               bRestartScan = FALSE;
-                iFileData = (PFILE_DIRECTORY_INFORMATION)pBuff;
-               while(1)
-                 {
-                   UNICODE_STRING tmpString;
-                   tmpString.Buffer = iFileData->FileName;
-                   tmpString.Length = tmpString.MaximumLength = iFileData->FileNameLength;
-                    RtlCopyUnicodeString(&cchFilename, &cchDir);
-                    RtlAppendUnicodeStringToString(&cchFilename, &tmpString);
-                   cchFilename.Buffer[cchFilename.Length / sizeof(WCHAR)] = 0;
-                   if (0 != IntGdiAddFontResource(&cchFilename, 0))
-                     {
-                       Result = TRUE;
-                     }
-                   if (iFileData->NextEntryOffset == 0)
-                     {
-                       break;
-                     }
-                   iFileData = (PVOID)iFileData + iFileData->NextEntryOffset;
-                 }
-                       }
-        }
-    }
-    ZwClose(hDirectory);
-    return Result;
+   BOOL Ret = RenderingEnabled;
+   HDC hDC;
+
+   hDC = IntGetScreenDC();
+   if (hDC)
+      Ret = (NtGdiGetDeviceCaps(hDC, BITSPIXEL) > 8) && RenderingEnabled;
+
+   return Ret;
 }
 
+VOID FASTCALL
+IntEnableFontRendering(BOOL Enable)
+{
+  RenderingEnabled = Enable;
+}
+
+FT_Render_Mode FASTCALL
+IntGetFontRenderMode(LOGFONTW *logfont)
+{  
+  switch(logfont->lfQuality)
+  {
+    //case ANTIALIASED_QUALITY:
+    case DEFAULT_QUALITY:
+      return FT_RENDER_MODE_NORMAL;
+    case DRAFT_QUALITY:
+      return FT_RENDER_MODE_LIGHT;
+    //case NONANTIALIASED_QUALITY:
+    case PROOF_QUALITY:
+      return FT_RENDER_MODE_MONO;
+    //case CLEARTYPE_QUALITY:
+    //  return FT_RENDER_MODE_LCD;
+  }
+  return FT_RENDER_MODE_MONO;
+}
 static NTSTATUS STDCALL
 GetFontObjectsFromTextObj(PTEXTOBJ TextObj, HFONT *FontHandle, FONTOBJ **FontObj, PFONTGDI *FontGDI)
 {
@@ -601,6 +651,7 @@ NtGdiCreateScalableFontResource(DWORD  Hidden,
                                      LPCWSTR  CurrentPath)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 /*************************************************************************
@@ -767,7 +818,6 @@ IntGetOutlineTextMetrics(PFONTGDI FontGDI, UINT Size,
     }
 
   pPost = FT_Get_Sfnt_Table(FontGDI->face, ft_sfnt_post); /* we can live with this failing */
-  IntUnLockFreeType;
 
   Otm->otmSize = Needed;
 
@@ -915,6 +965,8 @@ IntGetOutlineTextMetrics(PFONTGDI FontGDI, UINT Size,
       Otm->otmsUnderscorePosition = (FT_MulFix(pPost->underlinePosition, YScale) + 32) >> 6;
     }
 
+  IntUnLockFreeType;
+
   /* otmp* members should clearly have type ptrdiff_t, but M$ knows best */
   Cp = (char*) Otm + sizeof(OUTLINETEXTMETRICW);
   Otm->otmpFamilyName = (LPSTR)(Cp - (char*) Otm);
@@ -1447,6 +1499,7 @@ NtGdiEnumFonts(HDC  hDC,
                    LPARAM  lParam)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL STDCALL
@@ -1468,17 +1521,21 @@ NtGdiExtTextOut(
 
    DC *dc;
    SURFOBJ *SurfObj;
+   BITMAPOBJ *BitmapObj;
    int error, glyph_index, n, i;
    FT_Face face;
    FT_GlyphSlot glyph;
-   ULONG TextLeft, TextTop, pitch, previous, BackgroundLeft;
+   LONGLONG TextLeft, RealXStart;
+   ULONG TextTop, pitch, previous, BackgroundLeft;
    FT_Bool use_kerning;
    RECTL DestRect, MaskRect;
    POINTL SourcePoint, BrushOrigin;
    HBRUSH hBrushFg = NULL;
    PGDIBRUSHOBJ BrushFg = NULL;
+   GDIBRUSHINST BrushFgInst;
    HBRUSH hBrushBg = NULL;
    PGDIBRUSHOBJ BrushBg = NULL;
+   GDIBRUSHINST BrushBgInst;
    HBITMAP HSourceGlyph;
    SURFOBJ *SourceGlyphSurf;
    SIZEL bitSize;
@@ -1517,67 +1574,16 @@ NtGdiExtTextOut(
       }
    }
  
-   SurfObj = (SURFOBJ*)AccessUserObject((ULONG) dc->Surface);
+   BitmapObj = BITMAPOBJ_LockBitmap(dc->w.hBitmap);
+   ASSERT(BitmapObj);
+   SurfObj = &BitmapObj->SurfObj;
 
    Start.x = XStart; Start.y = YStart;
    IntLPtoDP(dc, &Start, 1);
 
-   XStart = Start.x + dc->w.DCOrgX;
+   RealXStart = (Start.x + dc->w.DCOrgX) << 6;
    YStart = Start.y + dc->w.DCOrgY;
 
-   TextObj = TEXTOBJ_LockText(dc->w.hFont);
-
-   if (!NT_SUCCESS(GetFontObjectsFromTextObj(TextObj, NULL, &FontObj, &FontGDI)))
-   {
-      goto fail;
-   }
-
-   face = FontGDI->face;
-   if (face->charmap == NULL)
-   {
-      DPRINT("WARNING: No charmap selected!\n");
-      DPRINT("This font face has %d charmaps\n", face->num_charmaps);
-
-      for (n = 0; n < face->num_charmaps; n++)
-      {
-         charmap = face->charmaps[n];
-         DPRINT("found charmap encoding: %u\n", charmap->encoding);
-         if (charmap->encoding != 0)
-         {
-            found = charmap;
-            break;
-         }
-      }
-      if (!found)
-         DPRINT1("WARNING: Could not find desired charmap!\n");
-      IntLockFreeType;
-      error = FT_Set_Charmap(face, found);
-      IntUnLockFreeType;
-      if (error)
-         DPRINT1("WARNING: Could not set the charmap!\n");
-   }
-
-   Render = IntIsFontRenderingEnabled();
-   if (Render)
-      RenderMode = IntGetFontRenderMode(&TextObj->logfont);
-   else
-      RenderMode = FT_RENDER_MODE_MONO;
-  
-   IntLockFreeType;
-   error = FT_Set_Pixel_Sizes(
-      face,
-      /* FIXME should set character height if neg */
-      (TextObj->logfont.lfHeight < 0 ?
-      - TextObj->logfont.lfHeight :
-      TextObj->logfont.lfHeight),
-      TextObj->logfont.lfWidth);
-   IntUnLockFreeType;
-   if (error)
-   {
-      DPRINT1("Error in setting pixel sizes: %u\n", error);
-      goto fail;
-   }
-
    /* Create the brushes */
    PalDestGDI = PALETTE_LockPalette(dc->w.hPalette);
    if ( !PalDestGDI )
@@ -1590,12 +1596,14 @@ NtGdiExtTextOut(
    XlateObj = (XLATEOBJ*)IntEngCreateXlate(Mode, PAL_RGB, dc->w.hPalette, NULL);
    hBrushFg = NtGdiCreateSolidBrush(XLATEOBJ_iXlate(XlateObj, dc->w.textColor));
    BrushFg = BRUSHOBJ_LockBrush(hBrushFg);
+   IntGdiInitBrushInstance(&BrushFgInst, BrushFg, NULL);
    if ((fuOptions & ETO_OPAQUE) || dc->w.backgroundMode == OPAQUE)
    {
       hBrushBg = NtGdiCreateSolidBrush(XLATEOBJ_iXlate(XlateObj, dc->w.backgroundColor));
       if (hBrushBg)
       {
          BrushBg = BRUSHOBJ_LockBrush(hBrushBg);
+         IntGdiInitBrushInstance(&BrushBgInst, BrushBg, NULL);
       }
       else
       {
@@ -1620,7 +1628,7 @@ NtGdiExtTextOut(
       DestRect.right += dc->w.DCOrgX;
       DestRect.bottom += dc->w.DCOrgY;
       IntEngBitBlt(
-         SurfObj,
+         BitmapObj,
          NULL,
          NULL,
          dc->CombinedClip,
@@ -1628,7 +1636,7 @@ NtGdiExtTextOut(
          &DestRect,
          &SourcePoint,
          &SourcePoint,
-         &BrushBg->BrushObject,
+         &BrushBgInst.BrushObject,
          &BrushOrigin,
          PATCOPY);
       fuOptions &= ~ETO_OPAQUE;
@@ -1641,6 +1649,59 @@ NtGdiExtTextOut(
       }
    }
 
+   TextObj = TEXTOBJ_LockText(dc->w.hFont);
+
+   if (!NT_SUCCESS(GetFontObjectsFromTextObj(TextObj, NULL, &FontObj, &FontGDI)))
+   {
+      goto fail;
+   }
+
+   face = FontGDI->face;
+   if (face->charmap == NULL)
+   {
+      DPRINT("WARNING: No charmap selected!\n");
+      DPRINT("This font face has %d charmaps\n", face->num_charmaps);
+
+      for (n = 0; n < face->num_charmaps; n++)
+      {
+         charmap = face->charmaps[n];
+         DPRINT("found charmap encoding: %u\n", charmap->encoding);
+         if (charmap->encoding != 0)
+         {
+            found = charmap;
+            break;
+         }
+      }
+      if (!found)
+         DPRINT1("WARNING: Could not find desired charmap!\n");
+      IntLockFreeType;
+      error = FT_Set_Charmap(face, found);
+      IntUnLockFreeType;
+      if (error)
+         DPRINT1("WARNING: Could not set the charmap!\n");
+   }
+
+   Render = IntIsFontRenderingEnabled();
+   if (Render)
+      RenderMode = IntGetFontRenderMode(&TextObj->logfont);
+   else
+      RenderMode = FT_RENDER_MODE_MONO;
+  
+   IntLockFreeType;
+   error = FT_Set_Pixel_Sizes(
+      face,
+      /* FIXME should set character height if neg */
+      (TextObj->logfont.lfHeight < 0 ?
+      - TextObj->logfont.lfHeight :
+      TextObj->logfont.lfHeight),
+      TextObj->logfont.lfWidth);
+   IntUnLockFreeType;
+   if (error)
+   {
+      DPRINT1("Error in setting pixel sizes: %u\n", error);
+      goto fail;
+   }
+
    /*
     * Process the vertical alignment and determine the yoff.
     */
@@ -1661,7 +1722,7 @@ NtGdiExtTextOut(
 
    if (dc->w.textAlign & (TA_RIGHT | TA_CENTER))
    {
-      UINT TextWidth = 0;
+      ULONGLONG TextWidth = 0;
       LPCWSTR TempText = String;
       int Start;
 
@@ -1672,7 +1733,7 @@ NtGdiExtTextOut(
       if (NULL != Dx)
       {
          Start = Count < 2 ? 0 : Count - 2;
-         TextWidth = Count < 2 ? 0 : Dx[Count - 2];
+         TextWidth = Count < 2 ? 0 : (Dx[Count - 2] << 6);
       }
       else
       {
@@ -1701,10 +1762,10 @@ NtGdiExtTextOut(
             IntLockFreeType;
             FT_Get_Kerning(face, previous, glyph_index, 0, &delta);
             IntUnLockFreeType;
-            TextWidth += delta.x >> 6;
+            TextWidth += delta.x;
          }
 
-         TextWidth += glyph->advance.x >> 6;
+         TextWidth += glyph->advance.x;
 
          previous = glyph_index;
          TempText++;
@@ -1714,17 +1775,17 @@ NtGdiExtTextOut(
 
       if (dc->w.textAlign & TA_RIGHT)
       {
-         XStart -= TextWidth;
+         RealXStart -= TextWidth;
       }
       else
       {
-         XStart -= TextWidth / 2;
+         RealXStart -= TextWidth / 2;
       }
    }
 
-   TextLeft = XStart;
+   TextLeft = RealXStart;
    TextTop = YStart;
-   BackgroundLeft = XStart;
+   BackgroundLeft = (RealXStart + 32) >> 6;
 
    /*
     * The main rendering loop.
@@ -1754,7 +1815,7 @@ NtGdiExtTextOut(
          IntLockFreeType;
          FT_Get_Kerning(face, previous, glyph_index, 0, &delta);
          IntUnLockFreeType;
-         TextLeft += delta.x >> 6;
+         TextLeft += delta.x;
       }
 
       if (glyph->format == ft_glyph_format_outline)
@@ -1777,11 +1838,11 @@ NtGdiExtTextOut(
       if (fuOptions & ETO_OPAQUE)
       {
          DestRect.left = BackgroundLeft;
-         DestRect.right = TextLeft + ((glyph->advance.x + 32) >> 6);
+         DestRect.right = (TextLeft + glyph->advance.x + 32) >> 6;
          DestRect.top = TextTop + yoff - ((face->size->metrics.ascender + 32) >> 6);
          DestRect.bottom = TextTop + yoff + ((32 - face->size->metrics.descender) >> 6);
          IntEngBitBlt(
-            SurfObj,
+            BitmapObj,
             NULL,
             NULL,
             dc->CombinedClip,
@@ -1789,14 +1850,14 @@ NtGdiExtTextOut(
             &DestRect,
             &SourcePoint,
             &SourcePoint,
-            &BrushBg->BrushObject,
+            &BrushBgInst.BrushObject,
             &BrushOrigin,
             PATCOPY);
          BackgroundLeft = DestRect.right;
       }
 
-      DestRect.left = TextLeft;
-      DestRect.right = TextLeft + glyph->bitmap.width;
+      DestRect.left = ((TextLeft + 32) >> 6) + glyph->bitmap_left;
+      DestRect.right = DestRect.left + glyph->bitmap.width;
       DestRect.top = TextTop + yoff - glyph->bitmap_top;
       DestRect.bottom = DestRect.top + glyph->bitmap.rows;
        
@@ -1811,8 +1872,8 @@ NtGdiExtTextOut(
        * limit the work of the transbitblt.
        */
 
-      HSourceGlyph = EngCreateBitmap(bitSize, pitch, (glyph->bitmap.pixel_mode == ft_pixel_mode_grays) ? BMF_8BPP : BMF_1BPP, 0, glyph->bitmap.buffer);
-      SourceGlyphSurf = (SURFOBJ*)AccessUserObject((ULONG) HSourceGlyph);
+      HSourceGlyph = EngCreateBitmap(bitSize, pitch, (glyph->bitmap.pixel_mode == ft_pixel_mode_grays) ? BMF_8BPP : BMF_1BPP, BMF_TOPDOWN, glyph->bitmap.buffer);
+      SourceGlyphSurf = EngLockSurface((HSURF)HSourceGlyph);
     
       /*
        * Use the font data as a mask to paint onto the DCs surface using a
@@ -1828,18 +1889,19 @@ NtGdiExtTextOut(
          &DestRect,
          &SourcePoint,
          (PPOINTL)&MaskRect,
-         &BrushFg->BrushObject,
+         &BrushFgInst.BrushObject,
          &BrushOrigin);
 
+      EngUnlockSurface(SourceGlyphSurf);
       EngDeleteSurface((HSURF)HSourceGlyph);
 
       if (NULL == Dx)
       {
-        TextLeft += (glyph->advance.x + 32) >> 6;
+         TextLeft += glyph->advance.x;
       }
       else
       {
-        TextLeft += Dx[i];
+         TextLeft += Dx[i] << 6;
       }
       previous = glyph_index;
 
@@ -1848,6 +1910,7 @@ NtGdiExtTextOut(
 
    EngDeleteXlate(XlateObj);
    EngDeleteXlate(XlateObj2);
+   BITMAPOBJ_UnlockBitmap(dc->w.hBitmap);
    TEXTOBJ_UnlockText(dc->w.hFont);
    if (hBrushBg != NULL)
    {
@@ -1866,6 +1929,7 @@ NtGdiExtTextOut(
 
 fail:
    TEXTOBJ_UnlockText(dc->w.hFont);
+   BITMAPOBJ_UnlockBitmap(dc->w.hBitmap);
    if (hBrushBg != NULL)
    {
       BRUSHOBJ_UnlockBrush(hBrushBg);
@@ -1891,6 +1955,7 @@ NtGdiGetAspectRatioFilterEx(HDC  hDC,
                                  LPSIZE  AspectRatio)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL
@@ -1912,6 +1977,7 @@ NtGdiGetCharABCWidthsFloat(HDC  hDC,
                                 LPABCFLOAT  abcF)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -1924,6 +1990,7 @@ NtGdiGetCharacterPlacement(HDC  hDC,
                                  DWORD  Flags)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL
@@ -1940,6 +2007,7 @@ NtGdiGetCharWidth32(HDC  hDC,
    FT_Face face;
    FT_CharMap charmap, found = NULL;
    UINT i, glyph_index, BufferSize;
+   HFONT hFont = 0;
 
    if (LastChar < FirstChar)
    {
@@ -1962,10 +2030,24 @@ NtGdiGetCharWidth32(HDC  hDC,
       SetLastWin32Error(ERROR_INVALID_HANDLE);
       return FALSE;
    }
-   TextObj = TEXTOBJ_LockText(dc->w.hFont);
+   hFont = dc->w.hFont;
+   TextObj = TEXTOBJ_LockText(hFont);
    DC_UnlockDc(hDC);
 
-   GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGDI);
+   if (TextObj == NULL)
+   {
+      ExFreePool(SafeBuffer);
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return FALSE;
+   }
+
+   if (!NT_SUCCESS(GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGDI)))
+   {
+      ExFreePool(SafeBuffer);
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      TEXTOBJ_UnlockText(hFont);
+      return FALSE;
+   }
    
    face = FontGDI->face;
    if (face->charmap == NULL)
@@ -2005,10 +2087,10 @@ NtGdiGetCharWidth32(HDC  hDC,
    {
       glyph_index = FT_Get_Char_Index(face, i);
       FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
-      SafeBuffer[i - FirstChar] = face->glyph->advance.x >> 6;
+      SafeBuffer[i - FirstChar] = (face->glyph->advance.x + 32) >> 6;
    }
    IntUnLockFreeType;
-   TEXTOBJ_UnlockText(dc->w.hFont);
+   TEXTOBJ_UnlockText(hFont);
    MmCopyToCaller(Buffer, SafeBuffer, BufferSize);
    ExFreePool(SafeBuffer);
    return TRUE;
@@ -2022,6 +2104,7 @@ NtGdiGetCharWidthFloat(HDC  hDC,
                             PFLOAT  Buffer)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -2029,6 +2112,7 @@ STDCALL
 NtGdiGetFontLanguageInfo(HDC  hDC)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 DWORD
@@ -2042,8 +2126,7 @@ NtGdiGetGlyphOutline(HDC  hDC,
                            CONST LPMAT2 mat2)
 {
   UNIMPLEMENTED;
-
-
+  return 0;
 }
 
 DWORD
@@ -2053,6 +2136,7 @@ NtGdiGetKerningPairs(HDC  hDC,
                            LPKERNINGPAIR  krnpair)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2062,6 +2146,7 @@ NtGdiGetOutlineTextMetrics(HDC  hDC,
                                 LPOUTLINETEXTMETRICW  otm)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL
@@ -2070,6 +2155,7 @@ NtGdiGetRasterizerCaps(LPRASTERIZER_STATUS  rs,
                             UINT  Size)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 UINT
@@ -2077,6 +2163,7 @@ STDCALL
 NtGdiGetTextCharset(HDC  hDC)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2086,6 +2173,7 @@ NtGdiGetTextCharsetInfo(HDC  hDC,
                              DWORD  Flags)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 static BOOL
@@ -2103,11 +2191,15 @@ TextIntGetTextExtentPoint(HDC hDC,
   FT_Face face;
   FT_GlyphSlot glyph;
   INT error, n, glyph_index, i, previous;
-  LONG TotalWidth = 0;
+  ULONGLONG TotalWidth = 0;
   FT_CharMap charmap, found = NULL;
   BOOL use_kerning;
 
-  GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGDI);
+  if (!NT_SUCCESS(GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGDI)))
+    {
+      return FALSE;
+    }
+
   face = FontGDI->face;
   if (NULL != Fit)
     {
@@ -2179,25 +2271,25 @@ TextIntGetTextExtentPoint(HDC hDC,
           IntLockFreeType;
          FT_Get_Kerning(face, previous, glyph_index, 0, &delta);
           IntUnLockFreeType;
-         TotalWidth += delta.x >> 6;
+         TotalWidth += delta.x;
        }
 
-      TotalWidth += glyph->advance.x >> 6;
+      TotalWidth += glyph->advance.x;
 
-      if (TotalWidth <= MaxExtent && NULL != Fit)
+      if (((TotalWidth + 32) >> 6) <= MaxExtent && NULL != Fit)
        {
          *Fit = i + 1;
        }
       if (NULL != Dx)
        {
-         Dx[i] = TotalWidth;
+         Dx[i] = (TotalWidth + 32) >> 6;
        }
 
       previous = glyph_index;
       String++;
     }
 
-  Size->cx = TotalWidth;
+  Size->cx = (TotalWidth + 32) >> 6;
   Size->cy = (TextObj->logfont.lfHeight < 0 ? - TextObj->logfont.lfHeight : TextObj->logfont.lfHeight);
   Size->cy = EngMulDiv(Size->cy, NtGdiGetDeviceCaps(hDC, LOGPIXELSY), 72);
 
@@ -2433,13 +2525,31 @@ NtGdiGetTextExtentPoint32(HDC hDC,
   return TRUE;
 }
 
-int
-STDCALL
-NtGdiGetTextFace(HDC  hDC,
-                     int  Count,
-                     LPWSTR  FaceName)
+INT STDCALL
+NtGdiGetTextFace(HDC hDC, INT Count, LPWSTR FaceName)
 {
-  UNIMPLEMENTED;
+   PDC Dc;
+   PTEXTOBJ TextObj;
+   NTSTATUS Status;
+
+   Dc = DC_LockDc(hDC);
+   if (Dc == NULL)
+   {
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return FALSE;
+   }
+   TextObj = TEXTOBJ_LockText(Dc->w.hFont);
+   DC_UnlockDc(hDC);
+
+   Count = min(Count, wcslen(TextObj->logfont.lfFaceName));
+   Status = MmCopyToCaller(FaceName, TextObj->logfont.lfFaceName, Count * sizeof(WCHAR));
+   if (!NT_SUCCESS(Status))
+   {
+      SetLastNtError(Status);
+      return 0;
+   }
+
+   return Count;
 }
 
 BOOL
@@ -2506,7 +2616,11 @@ NtGdiGetTextMetrics(HDC hDC,
        SafeTm.tmAscent = (Face->size->metrics.ascender + 32) >> 6; // units above baseline
        SafeTm.tmDescent = (32 - Face->size->metrics.descender) >> 6; // units below baseline
        SafeTm.tmHeight = SafeTm.tmAscent + SafeTm.tmDescent;
-        SafeTm.tmMaxCharWidth = (Face->size->metrics.max_advance + 32) >> 6;
+    SafeTm.tmMaxCharWidth = (Face->size->metrics.max_advance + 32) >> 6;
+    if (FT_IS_SFNT(FontGDI->face))
+    {
+      SafeTm.tmPitchAndFamily |= TMPF_TRUETYPE;
+    }
        Status = MmCopyToCaller(tm, &SafeTm, sizeof(TEXTMETRICW));
        }
     }
@@ -2535,6 +2649,7 @@ NtGdiPolyTextOut(HDC  hDC,
                       int  Count)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL
@@ -2542,6 +2657,7 @@ STDCALL
 NtGdiRemoveFontResource(LPCWSTR  FileName)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -2550,6 +2666,7 @@ NtGdiSetMapperFlags(HDC  hDC,
                           DWORD  Flag)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2602,6 +2719,7 @@ NtGdiSetTextJustification(HDC  hDC,
                                int  BreakCount)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL STDCALL
@@ -2615,6 +2733,63 @@ NtGdiTextOut(
    return NtGdiExtTextOut(hDC, XStart, YStart, 0, NULL, String, Count, NULL);
 }
 
+DWORD STDCALL
+NtGdiGetFontData(
+   HDC hDC,
+   DWORD Table,
+   DWORD Offset,
+   LPVOID Buffer,
+   DWORD Size)
+{
+   PDC Dc;
+   HFONT hFont;
+   PTEXTOBJ TextObj;
+   PFONTGDI FontGdi;
+   DWORD Result = GDI_ERROR;
+   NTSTATUS Status;
+
+   Dc = DC_LockDc(hDC);
+   if (Dc == NULL)
+   {
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return GDI_ERROR;
+   }
+   hFont = Dc->w.hFont;
+   TextObj = TEXTOBJ_LockText(hFont);
+   DC_UnlockDc(hDC);
+
+   if (TextObj == NULL)
+   {
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return GDI_ERROR;
+   }
+   
+   Status = GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGdi);
+   if (NT_SUCCESS(Status))
+   {
+      IntLockFreeType;
+
+      if (FT_IS_SFNT(FontGdi->face))
+      {
+         if (Table)
+            Table = Table >> 24 | Table << 24 | (Table >> 8 & 0xFF00) |
+                    (Table << 8 & 0xFF0000);
+
+         if (Buffer == NULL)
+            Size = 0;
+
+         if (!FT_Load_Sfnt_Table(FontGdi->face, Table, Offset, Buffer, &Size))
+            Result = Size;
+      }
+
+      IntUnLockFreeType;
+   }
+
+   TEXTOBJ_UnlockText(hFont);
+
+   return Result; 
+}
+
 static UINT FASTCALL
 GetFontScore(LOGFONTW *LogFont, PUNICODE_STRING FaceName, PFONTGDI FontGDI)
 {
@@ -2689,7 +2864,7 @@ FindBestFontFromList(HFONT *Font, UINT *MatchScore, LOGFONTW *LogFont,
           continue;
         }
       Score = GetFontScore(LogFont, FaceName, FontGDI);
-      if (*MatchScore < Score)
+      if (*MatchScore == 0 || *MatchScore < Score)
         {
           *Font = CurrentEntry->hFont;
           *MatchScore = Score;
@@ -2797,7 +2972,8 @@ TextIntRealizeFont(HFONT FontHandle)
 
   RtlFreeUnicodeString(&FaceName);
   TEXTOBJ_UnlockText(FontHandle);
-  ASSERT(! NT_SUCCESS(Status) || NULL != TextObj->GDIFontHandle);
+  
+  ASSERT((NT_SUCCESS(Status) ^ (NULL == TextObj->GDIFontHandle)) != 0);
 
   return Status;
 }