- Load also the last font file in the font directory.
[reactos.git] / reactos / subsys / win32k / objects / text.c
index 29cd537..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.101 2004/07/03 13:55:37 navaraf Exp $ */
+/* $Id: text.c,v 1.110 2004/08/24 17:29:00 navaraf Exp $ */
 #include <w32k.h>
 
 #include <ft2build.h>
@@ -155,7 +155,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 +213,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 +222,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;
@@ -380,7 +382,8 @@ IntGdiAddFontResource(PUNICODE_STRING FileName, DWORD Characteristics)
    /* 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);
@@ -648,6 +651,7 @@ NtGdiCreateScalableFontResource(DWORD  Hidden,
                                      LPCWSTR  CurrentPath)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 /*************************************************************************
@@ -814,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;
 
@@ -962,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);
@@ -1494,6 +1499,7 @@ NtGdiEnumFonts(HDC  hDC,
                    LPARAM  lParam)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL STDCALL
@@ -1519,14 +1525,17 @@ NtGdiExtTextOut(
    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;
@@ -1572,7 +1581,7 @@ NtGdiExtTextOut(
    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;
 
    /* Create the brushes */
@@ -1587,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
       {
@@ -1625,7 +1636,7 @@ NtGdiExtTextOut(
          &DestRect,
          &SourcePoint,
          &SourcePoint,
-         &BrushBg->BrushObject,
+         &BrushBgInst.BrushObject,
          &BrushOrigin,
          PATCOPY);
       fuOptions &= ~ETO_OPAQUE;
@@ -1711,7 +1722,7 @@ NtGdiExtTextOut(
 
    if (dc->w.textAlign & (TA_RIGHT | TA_CENTER))
    {
-      UINT TextWidth = 0;
+      ULONGLONG TextWidth = 0;
       LPCWSTR TempText = String;
       int Start;
 
@@ -1722,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
       {
@@ -1751,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++;
@@ -1764,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.
@@ -1804,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)
@@ -1827,7 +1838,7 @@ 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(
@@ -1839,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;
        
@@ -1878,7 +1889,7 @@ NtGdiExtTextOut(
          &DestRect,
          &SourcePoint,
          (PPOINTL)&MaskRect,
-         &BrushFg->BrushObject,
+         &BrushFgInst.BrushObject,
          &BrushOrigin);
 
       EngUnlockSurface(SourceGlyphSurf);
@@ -1886,11 +1897,11 @@ NtGdiExtTextOut(
 
       if (NULL == Dx)
       {
-        TextLeft += (glyph->advance.x + 32) >> 6;
+         TextLeft += glyph->advance.x;
       }
       else
       {
-        TextLeft += Dx[i];
+         TextLeft += Dx[i] << 6;
       }
       previous = glyph_index;
 
@@ -1944,6 +1955,7 @@ NtGdiGetAspectRatioFilterEx(HDC  hDC,
                                  LPSIZE  AspectRatio)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL
@@ -1965,6 +1977,7 @@ NtGdiGetCharABCWidthsFloat(HDC  hDC,
                                 LPABCFLOAT  abcF)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -1977,6 +1990,7 @@ NtGdiGetCharacterPlacement(HDC  hDC,
                                  DWORD  Flags)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL
@@ -1993,7 +2007,7 @@ NtGdiGetCharWidth32(HDC  hDC,
    FT_Face face;
    FT_CharMap charmap, found = NULL;
    UINT i, glyph_index, BufferSize;
-   HFONT hFont;
+   HFONT hFont = 0;
 
    if (LastChar < FirstChar)
    {
@@ -2016,10 +2030,17 @@ NtGdiGetCharWidth32(HDC  hDC,
       SetLastWin32Error(ERROR_INVALID_HANDLE);
       return FALSE;
    }
-   TextObj = TEXTOBJ_LockText(hFont);
    hFont = dc->w.hFont;
+   TextObj = TEXTOBJ_LockText(hFont);
    DC_UnlockDc(hDC);
 
+   if (TextObj == NULL)
+   {
+      ExFreePool(SafeBuffer);
+      SetLastWin32Error(ERROR_INVALID_HANDLE);
+      return FALSE;
+   }
+
    if (!NT_SUCCESS(GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGDI)))
    {
       ExFreePool(SafeBuffer);
@@ -2066,7 +2087,7 @@ 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(hFont);
@@ -2083,6 +2104,7 @@ NtGdiGetCharWidthFloat(HDC  hDC,
                             PFLOAT  Buffer)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -2090,6 +2112,7 @@ STDCALL
 NtGdiGetFontLanguageInfo(HDC  hDC)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 DWORD
@@ -2103,8 +2126,7 @@ NtGdiGetGlyphOutline(HDC  hDC,
                            CONST LPMAT2 mat2)
 {
   UNIMPLEMENTED;
-
-
+  return 0;
 }
 
 DWORD
@@ -2114,6 +2136,7 @@ NtGdiGetKerningPairs(HDC  hDC,
                            LPKERNINGPAIR  krnpair)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2123,6 +2146,7 @@ NtGdiGetOutlineTextMetrics(HDC  hDC,
                                 LPOUTLINETEXTMETRICW  otm)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 BOOL
@@ -2131,6 +2155,7 @@ NtGdiGetRasterizerCaps(LPRASTERIZER_STATUS  rs,
                             UINT  Size)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 UINT
@@ -2138,6 +2163,7 @@ STDCALL
 NtGdiGetTextCharset(HDC  hDC)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2147,6 +2173,7 @@ NtGdiGetTextCharsetInfo(HDC  hDC,
                              DWORD  Flags)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 static BOOL
@@ -2164,7 +2191,7 @@ 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;
 
@@ -2244,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);
 
@@ -2498,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
@@ -2571,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));
        }
     }
@@ -2600,6 +2649,7 @@ NtGdiPolyTextOut(HDC  hDC,
                       int  Count)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL
@@ -2607,6 +2657,7 @@ STDCALL
 NtGdiRemoveFontResource(LPCWSTR  FileName)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 DWORD
@@ -2615,6 +2666,7 @@ NtGdiSetMapperFlags(HDC  hDC,
                           DWORD  Flag)
 {
   UNIMPLEMENTED;
+  return 0;
 }
 
 UINT
@@ -2667,6 +2719,7 @@ NtGdiSetTextJustification(HDC  hDC,
                                int  BreakCount)
 {
   UNIMPLEMENTED;
+  return FALSE;
 }
 
 BOOL STDCALL
@@ -2680,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)
 {
@@ -2754,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;
@@ -2862,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;
 }