BUGFIX: get rid of an incorrect ASSERT
[reactos.git] / reactos / subsys / win32k / objects / text.c
index bd5731d..5880f93 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.97 2004/06/20 00:45:37 navaraf Exp $ */
+/* $Id: text.c,v 1.116 2004/12/13 01:45:18 royce Exp $ */
 #include <w32k.h>
 
 #include <ft2build.h>
@@ -33,7 +33,7 @@ FT_Library  library;
 
 typedef struct _FONT_ENTRY {
   LIST_ENTRY ListEntry;
-  HFONT hFont;
+  FONTGDI *Font;
   UNICODE_STRING FaceName;
   BYTE NotEnum;
 } FONT_ENTRY, *PFONT_ENTRY;
@@ -129,8 +129,10 @@ InitFontSupport(VOID)
    ExInitializeFastMutex(&FreeTypeLock);
 
    ulError = FT_Init_FreeType(&library);
-   if (ulError)
+   if (ulError) {
+      DPRINT1("FT_Init_FreeType failed with error code 0x%x\n", ulError);
       return FALSE;
+   }
 
    IntLoadSystemFonts();
 
@@ -155,7 +157,7 @@ IntLoadSystemFonts(VOID)
    BOOL bRestartScan = TRUE;
    NTSTATUS Status;
 
-   RtlInitUnicodeString(&Directory, L"\\SystemRoot\\Media\\Fonts\\");
+   RtlInitUnicodeString(&Directory, L"\\SystemRoot\\media\\fonts\\");
    /* FIXME: Add support for other font types */
    RtlInitUnicodeString(&SearchPattern, L"*.ttf");
 
@@ -213,9 +215,8 @@ IntLoadSystemFonts(VOID)
             break;
          }
 
-         for (DirInfo = (PFILE_DIRECTORY_INFORMATION)DirInfoBuffer;
-              DirInfo->NextEntryOffset != 0;
-              DirInfo = (PFILE_DIRECTORY_INFORMATION)((ULONG_PTR)DirInfo + DirInfo->NextEntryOffset))
+         DirInfo = (PFILE_DIRECTORY_INFORMATION)DirInfoBuffer;
+         while (1)
          {
             TempString.Buffer = DirInfo->FileName;
             TempString.Length =
@@ -223,6 +224,9 @@ IntLoadSystemFonts(VOID)
             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;
@@ -243,9 +247,7 @@ IntLoadSystemFonts(VOID)
 INT FASTCALL
 IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
 {
-   HFONT NewFont;
-   FONTOBJ *FontObj;
-   PFONTGDI FontGDI;
+   FONTGDI *FontGDI;
    NTSTATUS Status;
    HANDLE FileHandle;
    OBJECT_ATTRIBUTES ObjectAttributes;
@@ -257,20 +259,6 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
    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;
-   }
-
    /* Open the font file */
 
    InitializeObjectAttributes(&ObjectAttributes, FileName, 0, NULL, NULL);
@@ -284,8 +272,7 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
 
    if (!NT_SUCCESS(Status))
    {
-      DPRINT("Could not font file: %wZ\n", Filename);
-      NtGdiDeleteObject(NewFont);
+      DPRINT("Could not font file: %wZ\n", FileName);
       return 0;
    }
 
@@ -301,7 +288,6 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
    if (!NT_SUCCESS(Status))
    {
       DPRINT("Could not get file size\n");
-      NtGdiDeleteObject(NewFont);
       ZwClose(FileHandle);
       return 0;
    }
@@ -311,12 +297,11 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
    Buffer = ExAllocatePoolWithTag(
       PagedPool,
       FileStdInfo.EndOfFile.u.LowPart,
-      TAG_GDITEXT);
+      TAG_FNTFILE);
 
    if (Buffer == NULL)
    {
       DPRINT("Could not allocate memory for font");
-      NtGdiDeleteObject(NewFont);
       ZwClose(FileHandle);
       return 0;
    }
@@ -338,7 +323,6 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
    {
       DPRINT("Could not read the font file into memory");
       ExFreePool(Buffer);
-      NtGdiDeleteObject(NewFont);
       ZwClose(FileHandle);
       return 0;
    }
@@ -361,7 +345,6 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
       else
          DPRINT("Error reading font file (error code: %u)\n", Error);
       ExFreePool(Buffer);
-      NtGdiDeleteObject(NewFont);
       return 0;
    }
 
@@ -374,20 +357,31 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
       return 0;
    }
 
+   FontGDI = EngAllocMem(0, sizeof(FONTGDI), TAG_FONTOBJ);
+   if(FontGDI == NULL)
+   {
+      FT_Done_Face(Face);
+      ExFreePool(Buffer);
+      ExFreePool(Entry);
+      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 = FontGDI->TextMetric.tmAscent + FontGDI->TextMetric.tmDescent;
+   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);
+   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->Font = FontGDI;
    Entry->NotEnum = (Characteristics & FR_NOT_ENUM);
    RtlInitAnsiString(&AnsiFaceName, (LPSTR)Face->family_name);
    RtlAnsiStringToUnicodeString(&Entry->FaceName, &AnsiFaceName, TRUE);
@@ -412,21 +406,14 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
 BOOL FASTCALL
 IntIsFontRenderingEnabled(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;
+   BOOL Ret = RenderingEnabled;
+   HDC hDC;
+
+   hDC = IntGetScreenDC();
+   if (hDC)
+      Ret = (NtGdiGetDeviceCaps(hDC, BITSPIXEL) > 8) && RenderingEnabled;
+
+   return Ret;
 }
 
 VOID FASTCALL
@@ -453,39 +440,6 @@ IntGetFontRenderMode(LOGFONTW *logfont)
   }
   return FT_RENDER_MODE_MONO;
 }
-static NTSTATUS STDCALL
-GetFontObjectsFromTextObj(PTEXTOBJ TextObj, HFONT *FontHandle, FONTOBJ **FontObj, PFONTGDI *FontGDI)
-{
-  FONTOBJ *FntObj;
-  NTSTATUS Status = STATUS_SUCCESS;
-
-  ASSERT(NULL != TextObj && NULL != TextObj->GDIFontHandle);
-  if (NULL != TextObj && NULL != TextObj->GDIFontHandle)
-  {
-    if (NULL != FontHandle)
-    {
-      *FontHandle = TextObj->GDIFontHandle;
-    }
-    FntObj = (FONTOBJ*)AccessUserObject((ULONG) TextObj->GDIFontHandle);
-    if (NULL != FontObj)
-    {
-      *FontObj = FntObj;
-      if (NULL == *FontObj)
-      {
-       ASSERT(FALSE);
-       Status = STATUS_INVALID_HANDLE;
-      }
-    }
-    if (NT_SUCCESS(Status) && NULL != FontGDI)
-    {
-      *FontGDI = AccessInternalObjectFromUserObject(FntObj);
-    }
-    
-    return Status;
-  }
-  
-  return STATUS_INVALID_HANDLE;
-}
 
 int
 STDCALL
@@ -654,7 +608,8 @@ NtGdiCreateScalableFontResource(DWORD  Hidden,
                                      LPCWSTR  FontFile,
                                      LPCWSTR  CurrentPath)
 {
-  UNIMPLEMENTED;
+       DPRINT1("NtGdiCreateScalableFontResource - is unimplemented, have a nice day and keep going");
+  return FALSE;
 }
 
 /*************************************************************************
@@ -821,7 +776,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;
 
@@ -969,6 +923,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);
@@ -1007,18 +963,16 @@ FindFaceNameInList(PUNICODE_STRING FaceName, PLIST_ENTRY Head)
   PFONT_ENTRY CurrentEntry;
   ANSI_STRING EntryFaceNameA;
   UNICODE_STRING EntryFaceNameW;
-  PFONTGDI FontGDI;
+  FONTGDI *FontGDI;
 
   Entry = Head->Flink;
   while (Entry != Head)
     {
       CurrentEntry = (PFONT_ENTRY) CONTAINING_RECORD(Entry, FONT_ENTRY, ListEntry);
 
-      if (NULL == (FontGDI = AccessInternalObject((ULONG) CurrentEntry->hFont)))
-        {
-          Entry = Entry->Flink;
-          continue;
-        }
+      FontGDI = CurrentEntry->Font;
+      ASSERT(FontGDI);
+      
       RtlInitAnsiString(&EntryFaceNameA, FontGDI->face->family_name);
       RtlAnsiStringToUnicodeString(&EntryFaceNameW, &EntryFaceNameA, TRUE);
       if ((LF_FACESIZE - 1) * sizeof(WCHAR) < EntryFaceNameW.Length)
@@ -1275,18 +1229,16 @@ GetFontFamilyInfoForList(LPLOGFONTW LogFont,
   PFONT_ENTRY CurrentEntry;
   ANSI_STRING EntryFaceNameA;
   UNICODE_STRING EntryFaceNameW;
-  PFONTGDI FontGDI;
+  FONTGDI *FontGDI;
 
   Entry = Head->Flink;
   while (Entry != Head)
     {
       CurrentEntry = (PFONT_ENTRY) CONTAINING_RECORD(Entry, FONT_ENTRY, ListEntry);
 
-      if (NULL == (FontGDI = AccessInternalObject((ULONG) CurrentEntry->hFont)))
-        {
-          Entry = Entry->Flink;
-          continue;
-        }
+      FontGDI = CurrentEntry->Font;
+      ASSERT(FontGDI);
+
       RtlInitAnsiString(&EntryFaceNameA, FontGDI->face->family_name);
       RtlAnsiStringToUnicodeString(&EntryFaceNameW, &EntryFaceNameA, TRUE);
       if ((LF_FACESIZE - 1) * sizeof(WCHAR) < EntryFaceNameW.Length)
@@ -1501,6 +1453,7 @@ NtGdiEnumFonts(HDC  hDC,
                    LPARAM  lParam)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL STDCALL
@@ -1522,17 +1475,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;
@@ -1540,7 +1497,7 @@ NtGdiExtTextOut(
    INT yoff;
    FONTOBJ *FontObj;
    PFONTGDI FontGDI;
-   PTEXTOBJ TextObj;
+   PTEXTOBJ TextObj = NULL;
    PPALGDI PalDestGDI;
    XLATEOBJ *XlateObj, *XlateObj2;
    ULONG Mode;
@@ -1571,67 +1528,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 )
@@ -1644,12 +1550,15 @@ 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);
+         /* FIXME - Handle BrushBg == NULL !!!!! */
+         IntGdiInitBrushInstance(&BrushBgInst, BrushBg, NULL);
       }
       else
       {
@@ -1674,7 +1583,7 @@ NtGdiExtTextOut(
       DestRect.right += dc->w.DCOrgX;
       DestRect.bottom += dc->w.DCOrgY;
       IntEngBitBlt(
-         SurfObj,
+         BitmapObj,
          NULL,
          NULL,
          dc->CombinedClip,
@@ -1682,7 +1591,7 @@ NtGdiExtTextOut(
          &DestRect,
          &SourcePoint,
          &SourcePoint,
-         &BrushBg->BrushObject,
+         &BrushBgInst.BrushObject,
          &BrushOrigin,
          PATCOPY);
       fuOptions &= ~ETO_OPAQUE;
@@ -1695,6 +1604,61 @@ NtGdiExtTextOut(
       }
    }
 
+   TextObj = TEXTOBJ_LockText(dc->w.hFont);
+   if(TextObj == NULL)
+   {
+     goto fail;
+   }
+
+   FontObj = TextObj->Font;
+   FontGDI = ObjToGDI(FontObj, FONT);
+
+   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.
     */
@@ -1715,7 +1679,7 @@ NtGdiExtTextOut(
 
    if (dc->w.textAlign & (TA_RIGHT | TA_CENTER))
    {
-      UINT TextWidth = 0;
+      ULONGLONG TextWidth = 0;
       LPCWSTR TempText = String;
       int Start;
 
@@ -1726,7 +1690,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
       {
@@ -1755,10 +1719,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++;
@@ -1768,17 +1732,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.
@@ -1808,7 +1772,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)
@@ -1831,11 +1795,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,
@@ -1843,14 +1807,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;
        
@@ -1865,8 +1829,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
@@ -1882,18 +1846,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;
 
@@ -1902,7 +1867,9 @@ NtGdiExtTextOut(
 
    EngDeleteXlate(XlateObj);
    EngDeleteXlate(XlateObj2);
-   TEXTOBJ_UnlockText(dc->w.hFont);
+   BITMAPOBJ_UnlockBitmap(dc->w.hBitmap);
+   if(TextObj != NULL)
+     TEXTOBJ_UnlockText(dc->w.hFont);
    if (hBrushBg != NULL)
    {
       BRUSHOBJ_UnlockBrush(hBrushBg);
@@ -1914,12 +1881,14 @@ NtGdiExtTextOut(
    {
       ExFreePool(Dx);
    }
-   DC_UnlockDc(hDC);
+   DC_UnlockDc( hDC );
  
    return TRUE;
 
 fail:
-   TEXTOBJ_UnlockText(dc->w.hFont);
+   if(TextObj != NULL)
+     TEXTOBJ_UnlockText(dc->w.hFont);
+   BITMAPOBJ_UnlockBitmap(dc->w.hBitmap);
    if (hBrushBg != NULL)
    {
       BRUSHOBJ_UnlockBrush(hBrushBg);
@@ -1945,6 +1914,7 @@ NtGdiGetAspectRatioFilterEx(HDC  hDC,
                                  LPSIZE  AspectRatio)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL
@@ -1966,6 +1936,7 @@ NtGdiGetCharABCWidthsFloat(HDC  hDC,
                                 LPABCFLOAT  abcF)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -1978,6 +1949,7 @@ NtGdiGetCharacterPlacement(HDC  hDC,
                                  DWORD  Flags)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL
@@ -1994,6 +1966,7 @@ NtGdiGetCharWidth32(HDC  hDC,
    FT_Face face;
    FT_CharMap charmap, found = NULL;
    UINT i, glyph_index, BufferSize;
+   HFONT hFont = 0;
 
    if (LastChar < FirstChar)
    {
@@ -2016,10 +1989,18 @@ 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;
+   }
+
+   FontGDI = ObjToGDI(TextObj->Font, FONT);
    
    face = FontGDI->face;
    if (face->charmap == NULL)
@@ -2059,10 +2040,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;
@@ -2076,6 +2057,7 @@ NtGdiGetCharWidthFloat(HDC  hDC,
                             PFLOAT  Buffer)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -2083,6 +2065,7 @@ STDCALL
 NtGdiGetFontLanguageInfo(HDC  hDC)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 DWORD
@@ -2096,8 +2079,7 @@ NtGdiGetGlyphOutline(HDC  hDC,
                            CONST LPMAT2 mat2)
 {
   UNIMPLEMENTED;
-
-
+  return 0;
 }
 
 DWORD
@@ -2107,6 +2089,7 @@ NtGdiGetKerningPairs(HDC  hDC,
                            LPKERNINGPAIR  krnpair)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2116,6 +2099,7 @@ NtGdiGetOutlineTextMetrics(HDC  hDC,
                                 LPOUTLINETEXTMETRICW  otm)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL
@@ -2124,6 +2108,7 @@ NtGdiGetRasterizerCaps(LPRASTERIZER_STATUS  rs,
                             UINT  Size)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 UINT
@@ -2131,6 +2116,7 @@ STDCALL
 NtGdiGetTextCharset(HDC  hDC)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2140,11 +2126,12 @@ NtGdiGetTextCharsetInfo(HDC  hDC,
                              DWORD  Flags)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 static BOOL
 FASTCALL
-TextIntGetTextExtentPoint(HDC hDC,
+TextIntGetTextExtentPoint(PDC dc,
                           PTEXTOBJ TextObj,
                           LPCWSTR String,
                           int Count,
@@ -2157,11 +2144,12 @@ 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);
+  FontGDI = ObjToGDI(TextObj->Font, FONT);
+
   face = FontGDI->face;
   if (NULL != Fit)
     {
@@ -2233,27 +2221,27 @@ 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);
+  Size->cy = EngMulDiv(Size->cy, IntGdiGetDeviceCaps(dc, LOGPIXELSY), 72);
 
   return TRUE;
 }
@@ -2341,10 +2329,15 @@ NtGdiGetTextExtentExPoint(HDC hDC,
       return FALSE;
     }
   TextObj = TEXTOBJ_LockText(dc->w.hFont);
-  DC_UnlockDc(hDC);
-  Result = TextIntGetTextExtentPoint(hDC, TextObj, String, Count, MaxExtent,
+  if ( TextObj )
+  {
+    Result = TextIntGetTextExtentPoint(dc, TextObj, String, Count, MaxExtent,
                                      NULL == UnsafeFit ? NULL : &Fit, Dx, &Size);
+  }
+  else
+    Result = FALSE;
   TEXTOBJ_UnlockText(dc->w.hFont);
+  DC_UnlockDc(hDC);
 
   ExFreePool(String);
   if (! Result)
@@ -2463,12 +2456,14 @@ NtGdiGetTextExtentPoint32(HDC hDC,
       return FALSE;
     }
   TextObj = TEXTOBJ_LockText(dc->w.hFont);
-  DC_UnlockDc(hDC);
-  Result = TextIntGetTextExtentPoint (
-         hDC, TextObj, String, Count, 0, NULL, NULL, &Size);
-  dc = DC_LockDc(hDC);
-  ASSERT(dc); // it succeeded earlier, it should now, too
-  TEXTOBJ_UnlockText(dc->w.hFont);
+  if ( TextObj != NULL )
+  {
+    Result = TextIntGetTextExtentPoint (
+      dc, TextObj, String, Count, 0, NULL, NULL, &Size);
+    TEXTOBJ_UnlockText(dc->w.hFont);
+  }
+  else
+    Result = FALSE;
   DC_UnlockDc(hDC);
 
   ExFreePool(String);
@@ -2487,13 +2482,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
@@ -2525,9 +2538,8 @@ NtGdiGetTextMetrics(HDC hDC,
   TextObj = TEXTOBJ_LockText(dc->w.hFont);
   if (NULL != TextObj)
   {
-    Status = GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGDI);
-    if (NT_SUCCESS(Status))
-    {
+      FontGDI = ObjToGDI(TextObj->Font, FONT);
+
       Face = FontGDI->face;
       IntLockFreeType;
       Error = FT_Set_Pixel_Sizes(Face,
@@ -2560,15 +2572,17 @@ 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));
        }
-    }
     TEXTOBJ_UnlockText(dc->w.hFont);
   }
   else
   {
-    ASSERT(FALSE);
     Status = STATUS_INVALID_HANDLE;
   }
   DC_UnlockDc(hDC);
@@ -2589,6 +2603,7 @@ NtGdiPolyTextOut(HDC  hDC,
                       int  Count)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL
@@ -2596,6 +2611,7 @@ STDCALL
 NtGdiRemoveFontResource(LPCWSTR  FileName)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -2604,6 +2620,7 @@ NtGdiSetMapperFlags(HDC  hDC,
                           DWORD  Flag)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2656,6 +2673,7 @@ NtGdiSetTextJustification(HDC  hDC,
                                int  BreakCount)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL STDCALL
@@ -2669,6 +2687,60 @@ 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;
+
+   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;
+   }
+   
+   FontGdi = ObjToGDI(TextObj->Font, FONT);
+   
+   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)
 {
@@ -2724,35 +2796,34 @@ GetFontScore(LOGFONTW *LogFont, PUNICODE_STRING FaceName, PFONTGDI FontGDI)
   return Score;
 }
 
-static VOID FASTCALL
-FindBestFontFromList(HFONT *Font, UINT *MatchScore, LOGFONTW *LogFont,
+static inline VOID
+FindBestFontFromList(FONTOBJ **FontObj, UINT *MatchScore, LOGFONTW *LogFont,
                      PUNICODE_STRING FaceName, PLIST_ENTRY Head)
 {
   PLIST_ENTRY Entry;
   PFONT_ENTRY CurrentEntry;
-  PFONTGDI FontGDI;
+  FONTGDI *FontGDI;
   UINT Score;
 
   Entry = Head->Flink;
   while (Entry != Head)
     {
       CurrentEntry = (PFONT_ENTRY) CONTAINING_RECORD(Entry, FONT_ENTRY, ListEntry);
-      if (NULL == (FontGDI = AccessInternalObject((ULONG) CurrentEntry->hFont)))
-        {
-          Entry = Entry->Flink;
-          continue;
-        }
+
+      FontGDI = CurrentEntry->Font;
+      ASSERT(FontGDI);
+
       Score = GetFontScore(LogFont, FaceName, FontGDI);
-      if (*MatchScore < Score)
+      if (*MatchScore == 0 || *MatchScore < Score)
         {
-          *Font = CurrentEntry->hFont;
+          *FontObj = GDIToObj(FontGDI, FONT);
           *MatchScore = Score;
         }
       Entry = Entry->Flink;
     }
 }
 
-static BOOLEAN FASTCALL
+static inline BOOLEAN
 SubstituteFontFamilyKey(PUNICODE_STRING FaceName,
                         LPCWSTR Key)
 {
@@ -2788,7 +2859,7 @@ SubstituteFontFamilyKey(PUNICODE_STRING FaceName,
   return NT_SUCCESS(Status);
 }
 
-static void FASTCALL
+static inline void
 SubstituteFontFamily(PUNICODE_STRING FaceName, UINT Level)
 {
   if (10 < Level) /* Enough is enough */
@@ -2825,33 +2896,38 @@ TextIntRealizeFont(HFONT FontHandle)
     }
   SubstituteFontFamily(&FaceName, 0);
   MatchScore = 0;
-  TextObj->GDIFontHandle = NULL;
+  TextObj->Font = NULL;
     
   /* First search private fonts */
   Win32Process = PsGetWin32Process();
   IntLockProcessPrivateFonts(Win32Process);
-  FindBestFontFromList(&TextObj->GDIFontHandle, &MatchScore,
+  FindBestFontFromList(&TextObj->Font, &MatchScore,
                        &TextObj->logfont, &FaceName,
                        &Win32Process->PrivateFontListHead);
   IntUnLockProcessPrivateFonts(Win32Process);
     
   /* Search system fonts */
   IntLockGlobalFonts;
-  FindBestFontFromList(&TextObj->GDIFontHandle, &MatchScore,
+  FindBestFontFromList(&TextObj->Font, &MatchScore,
                        &TextObj->logfont, &FaceName,
                        &FontListHead);
   IntUnLockGlobalFonts;
 
-  if (NULL == TextObj->GDIFontHandle)
+  if (NULL == TextObj->Font)
     {
       DPRINT1("Requested font %S not found, no fonts loaded at all\n",
               TextObj->logfont.lfFaceName);
       Status = STATUS_NOT_FOUND;
     }
+  else
+    {
+      Status = STATUS_SUCCESS;
+    }
 
   RtlFreeUnicodeString(&FaceName);
   TEXTOBJ_UnlockText(FontHandle);
-  ASSERT(! NT_SUCCESS(Status) || NULL != TextObj->GDIFontHandle);
+  
+  ASSERT((NT_SUCCESS(Status) ^ (NULL == TextObj->Font)) != 0);
 
   return Status;
 }