[GDI32] Improve IntFontFamilyListUnique function (#1491)
[reactos.git] / win32ss / gdi / gdi32 / objects / font.c
index 4225005..2f187b0 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
- * FILE:            lib/gdi32/object/font.c
+ * FILE:            win32ss/gdi/gdi32/objects/font.c
  * PURPOSE:
  * PROGRAMMER:
  *
@@ -10,6 +10,7 @@
 #include <precomp.h>
 
 #include <math.h>
+#include <strsafe.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -131,7 +132,11 @@ static LPWSTR FONT_mbtowc(HDC hdc, LPCSTR str, INT count, INT *plenW, UINT *pCP)
     strW = HeapAlloc(GetProcessHeap(), 0, lenW*sizeof(WCHAR));
     if (!strW)
         return NULL;
-    MultiByteToWideChar(cp, 0, str, count, strW, lenW);
+    if(!MultiByteToWideChar(cp, 0, str, count, strW, lenW))
+    {
+        HeapFree(GetProcessHeap(), 0, strW);
+        return NULL;
+    }
     DPRINT("mapped %s -> %S\n", str, strW);
     if(plenW) *plenW = lenW;
     if(pCP) *pCP = cp;
@@ -199,9 +204,89 @@ NewTextMetricExW2A(NEWTEXTMETRICEXA *tma, NEWTEXTMETRICEXW *tmw)
     tma->ntmFontSig = tmw->ntmFontSig;
 }
 
+// IntFontFamilyCompareEx's flags
+#define IFFCX_CHARSET 1
+#define IFFCX_STYLE 2
+
+FORCEINLINE int FASTCALL
+IntFontFamilyCompareEx(const FONTFAMILYINFO *ffi1,
+                       const FONTFAMILYINFO *ffi2, DWORD dwCompareFlags)
+{
+    const LOGFONTW *plf1 = &ffi1->EnumLogFontEx.elfLogFont;
+    const LOGFONTW *plf2 = &ffi2->EnumLogFontEx.elfLogFont;
+    ULONG WeightDiff1, WeightDiff2;
+    int cmp = _wcsicmp(plf1->lfFaceName, plf2->lfFaceName);
+    if (cmp)
+        return cmp;
+    if (dwCompareFlags & IFFCX_STYLE)
+    {
+        WeightDiff1 = labs(plf1->lfWeight - FW_NORMAL);
+        WeightDiff2 = labs(plf2->lfWeight - FW_NORMAL);
+        if (WeightDiff1 < WeightDiff2)
+            return -1;
+        if (WeightDiff1 > WeightDiff2)
+            return 1;
+        if (plf1->lfItalic < plf2->lfItalic)
+            return -1;
+        if (plf1->lfItalic > plf2->lfItalic)
+            return 1;
+    }
+    if (dwCompareFlags & IFFCX_CHARSET)
+    {
+        if (plf1->lfCharSet < plf2->lfCharSet)
+            return -1;
+        if (plf1->lfCharSet > plf2->lfCharSet)
+            return 1;
+    }
+    return 0;
+}
+
+static int __cdecl
+IntFontFamilyCompare(const void *ffi1, const void *ffi2)
+{
+    return IntFontFamilyCompareEx(ffi1, ffi2, IFFCX_STYLE | IFFCX_CHARSET);
+}
+
+// IntEnumFontFamilies' flags:
+#define IEFF_UNICODE 1
+#define IEFF_EXTENDED 2
+
+int FASTCALL
+IntFontFamilyListUnique(FONTFAMILYINFO *InfoList, INT nCount,
+                        const LOGFONTW *plf, DWORD dwFlags)
+{
+    FONTFAMILYINFO *first, *last, *result;
+    DWORD dwCompareFlags = 0;
+
+    if (plf->lfFaceName[0])
+        dwCompareFlags |= IFFCX_STYLE;
+
+    if ((dwFlags & IEFF_EXTENDED) && plf->lfCharSet == DEFAULT_CHARSET)
+        dwCompareFlags |= IFFCX_CHARSET;
+
+    first = InfoList;
+    last = &InfoList[nCount];
+
+    // std::unique(first, last, IntFontFamilyCompareEx);
+    if (first == last)
+        return 0;
+
+    result = first;
+    while (++first != last)
+    {
+        if (IntFontFamilyCompareEx(result, first, dwCompareFlags) != 0)
+        {
+            *(++result) = *first;
+        }
+    }
+    nCount = (int)(++result - InfoList);
+
+    return nCount;
+}
+
 static int FASTCALL
 IntEnumFontFamilies(HDC Dc, LPLOGFONTW LogFont, PVOID EnumProc, LPARAM lParam,
-                    BOOL Unicode)
+                    DWORD dwFlags)
 {
     int FontFamilyCount;
     int FontFamilySize;
@@ -251,9 +336,12 @@ IntEnumFontFamilies(HDC Dc, LPLOGFONTW LogFont, PVOID EnumProc, LPARAM lParam,
         }
     }
 
+    qsort(Info, FontFamilyCount, sizeof(*Info), IntFontFamilyCompare);
+    FontFamilyCount = IntFontFamilyListUnique(Info, FontFamilyCount, LogFont, dwFlags);
+
     for (i = 0; i < FontFamilyCount; i++)
     {
-        if (Unicode)
+        if (dwFlags & IEFF_UNICODE)
         {
             Ret = ((FONTENUMPROCW) EnumProc)(
                       (VOID*)&Info[i].EnumLogFontEx,
@@ -277,6 +365,9 @@ IntEnumFontFamilies(HDC Dc, LPLOGFONTW LogFont, PVOID EnumProc, LPARAM lParam,
                       (VOID*)&NewTextMetricExA,
                       Info[i].FontType, lParam);
         }
+
+        if(Ret == 0)
+            break;
     }
 
     RtlFreeHeap(GetProcessHeap(), 0, Info);
@@ -291,7 +382,19 @@ int WINAPI
 EnumFontFamiliesExW(HDC hdc, LPLOGFONTW lpLogfont, FONTENUMPROCW lpEnumFontFamExProc,
                     LPARAM lParam, DWORD dwFlags)
 {
-    return IntEnumFontFamilies(hdc, lpLogfont, lpEnumFontFamExProc, lParam, TRUE);
+    if (lpLogfont)
+    {
+        DPRINT("EnumFontFamiliesExW(%p, %p(%S, %u, %u), %p, %p, 0x%08lX)\n",
+               hdc, lpLogfont, lpLogfont->lfFaceName, lpLogfont->lfCharSet,
+               lpLogfont->lfPitchAndFamily, lpEnumFontFamExProc, lParam, dwFlags);
+    }
+    else
+    {
+        DPRINT("EnumFontFamiliesExW(%p, NULL, %p, %p, 0x%08lX)\n",
+               hdc, lpEnumFontFamExProc, lParam, dwFlags);
+    }
+    return IntEnumFontFamilies(hdc, lpLogfont, lpEnumFontFamExProc, lParam,
+                               IEFF_UNICODE | IEFF_EXTENDED);
 }
 
 
@@ -304,6 +407,9 @@ EnumFontFamiliesW(HDC hdc, LPCWSTR lpszFamily, FONTENUMPROCW lpEnumFontFamProc,
 {
     LOGFONTW LogFont;
 
+    DPRINT("EnumFontFamiliesW(%p, %S, %p, %p)\n",
+           hdc, lpszFamily, lpEnumFontFamProc, lParam);
+
     ZeroMemory(&LogFont, sizeof(LOGFONTW));
     LogFont.lfCharSet = DEFAULT_CHARSET;
     if (NULL != lpszFamily)
@@ -312,7 +418,7 @@ EnumFontFamiliesW(HDC hdc, LPCWSTR lpszFamily, FONTENUMPROCW lpEnumFontFamProc,
         lstrcpynW(LogFont.lfFaceName, lpszFamily, LF_FACESIZE);
     }
 
-    return IntEnumFontFamilies(hdc, &LogFont, lpEnumFontFamProc, lParam, TRUE);
+    return IntEnumFontFamilies(hdc, &LogFont, lpEnumFontFamProc, lParam, IEFF_UNICODE);
 }
 
 
@@ -325,6 +431,18 @@ EnumFontFamiliesExA (HDC hdc, LPLOGFONTA lpLogfont, FONTENUMPROCA lpEnumFontFamE
 {
     LOGFONTW LogFontW, *pLogFontW;
 
+    if (lpLogfont)
+    {
+        DPRINT("EnumFontFamiliesExA(%p, %p(%s, %u, %u), %p, %p, 0x%08lX)\n",
+               hdc, lpLogfont, lpLogfont->lfFaceName, lpLogfont->lfCharSet,
+               lpLogfont->lfPitchAndFamily, lpEnumFontFamExProc, lParam, dwFlags);
+    }
+    else
+    {
+        DPRINT("EnumFontFamiliesExA(%p, NULL, %p, %p, 0x%08lX)\n",
+               hdc, lpEnumFontFamExProc, lParam, dwFlags);
+    }
+
     if (lpLogfont)
     {
         LogFontA2W(&LogFontW,lpLogfont);
@@ -333,7 +451,7 @@ EnumFontFamiliesExA (HDC hdc, LPLOGFONTA lpLogfont, FONTENUMPROCA lpEnumFontFamE
     else pLogFontW = NULL;
 
     /* no need to convert LogFontW back to lpLogFont b/c it's an [in] parameter only */
-    return IntEnumFontFamilies(hdc, pLogFontW, lpEnumFontFamExProc, lParam, FALSE);
+    return IntEnumFontFamilies(hdc, pLogFontW, lpEnumFontFamExProc, lParam, IEFF_EXTENDED);
 }
 
 
@@ -346,6 +464,9 @@ EnumFontFamiliesA(HDC hdc, LPCSTR lpszFamily, FONTENUMPROCA lpEnumFontFamProc,
 {
     LOGFONTW LogFont;
 
+    DPRINT("EnumFontFamiliesA(%p, %s, %p, %p)\n",
+           hdc, lpszFamily, lpEnumFontFamProc, lParam);
+
     ZeroMemory(&LogFont, sizeof(LOGFONTW));
     LogFont.lfCharSet = DEFAULT_CHARSET;
     if (NULL != lpszFamily)
@@ -354,7 +475,7 @@ EnumFontFamiliesA(HDC hdc, LPCSTR lpszFamily, FONTENUMPROCA lpEnumFontFamProc,
         MultiByteToWideChar(CP_THREAD_ACP, 0, lpszFamily, -1, LogFont.lfFaceName, LF_FACESIZE);
     }
 
-    return IntEnumFontFamilies(hdc, &LogFont, lpEnumFontFamProc, lParam, FALSE);
+    return IntEnumFontFamilies(hdc, &LogFont, lpEnumFontFamProc, lParam, 0);
 }
 
 
@@ -377,7 +498,7 @@ GetCharacterPlacementA(
     DWORD ret;
     UINT font_cp;
 
-    if ( !lpString || uCount <= 0 || (nMaxExtent < 0 && nMaxExtent != -1 ) )
+    if ( !lpString || uCount <= 0 || !lpResults || (nMaxExtent < 0 && nMaxExtent != -1 ) )
     {
         SetLastError(ERROR_INVALID_PARAMETER);
         return 0;
@@ -440,38 +561,32 @@ GetCharacterPlacementW(
     UINT i, nSet;
     DPRINT("GetCharacterPlacementW\n");
 
-    if(dwFlags&(~GCP_REORDER)) DPRINT("flags 0x%08lx ignored\n", dwFlags);
-    if(lpResults->lpClass) DPRINT("classes not implemented\n");
-    if (lpResults->lpCaretPos && (dwFlags & GCP_REORDER))
-        DPRINT("Caret positions for complex scripts not implemented\n");
+    if (dwFlags&(~GCP_REORDER)) DPRINT("flags 0x%08lx ignored\n", dwFlags);
+    if (lpResults->lpClass) DPRINT("classes not implemented\n");
 
     nSet = (UINT)uCount;
-    if(nSet > lpResults->nGlyphs)
+    if (nSet > lpResults->nGlyphs)
         nSet = lpResults->nGlyphs;
 
     /* return number of initialized fields */
     lpResults->nGlyphs = nSet;
 
-    /*if((dwFlags&GCP_REORDER)==0 || !BidiAvail)
-      {*/
+    if (dwFlags & GCP_REORDER)
+    {
+        if (LoadLPK(LPK_GCP))
+            return LpkGetCharacterPlacement(hdc, lpString, uCount, nMaxExtent, lpResults, dwFlags, 0);
+    }
+
     /* Treat the case where no special handling was requested in a fastpath way */
-    /* copy will do if the GCP_REORDER flag is not set */
-    if(lpResults->lpOutString)
+    /* copy will do if the GCP_REORDER flag is not set */ 
+    if (lpResults->lpOutString)
         lstrcpynW( lpResults->lpOutString, lpString, nSet );
 
-    if(lpResults->lpGlyphs)
-        lstrcpynW( lpResults->lpGlyphs, lpString, nSet );
-
-    if(lpResults->lpOrder)
+    if (lpResults->lpOrder)
     {
-        for(i = 0; i < nSet; i++)
+        for (i = 0; i < nSet; i++)
             lpResults->lpOrder[i] = i;
     }
-    /*} else
-      {
-          BIDI_Reorder( lpString, uCount, dwFlags, WINE_GCPW_FORCE_LTR, lpResults->lpOutString,
-                        nSet, lpResults->lpOrder );
-      }*/
 
     /* FIXME: Will use the placement chars */
     if (lpResults->lpDx)
@@ -494,8 +609,8 @@ GetCharacterPlacementW(
                 lpResults->lpCaretPos[i] = (pos += size.cx);
     }
 
-    /*if(lpResults->lpGlyphs)
-      NtGdiGetGlyphIndicesW(hdc, lpString, nSet, lpResults->lpGlyphs, 0);*/
+    if (lpResults->lpGlyphs)
+        NtGdiGetGlyphIndicesW(hdc, lpString, nSet, lpResults->lpGlyphs, 0);
 
     if (GetTextExtentPoint32W(hdc, lpString, uCount, &size))
         ret = MAKELONG(size.cx, size.cy);
@@ -825,7 +940,7 @@ GetCharABCWidthsA(
 
     ret = NtGdiGetCharABCWidthsW( hdc,
                                   wstr[0],
-                                  (ULONG)count,
+                                  wlen - 1,
                                   (PWCHAR)wstr,
                                   GCABCW_NOFLOAT,
                                   (PVOID)lpabc);
@@ -1008,6 +1123,8 @@ GetGlyphOutlineA(
             mbchs[0] = (uChar & 0xff);
         }
         p = FONT_mbtowc(hdc, mbchs, len, NULL, NULL);
+        if(!p)
+            return GDI_ERROR;
         c = p[0];
     }
     else
@@ -1091,19 +1208,19 @@ GetOutlineTextMetricsA(
     needed = sizeof(OUTLINETEXTMETRICA);
     if(lpOTMW->otmpFamilyName)
         needed += WideCharToMultiByte(CP_ACP, 0,
-                                      (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpFamilyName), -1,
+                                      (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpFamilyName), -1,
                                       NULL, 0, NULL, NULL);
     if(lpOTMW->otmpFaceName)
         needed += WideCharToMultiByte(CP_ACP, 0,
-                                      (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpFaceName), -1,
+                                      (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpFaceName), -1,
                                       NULL, 0, NULL, NULL);
     if(lpOTMW->otmpStyleName)
         needed += WideCharToMultiByte(CP_ACP, 0,
-                                      (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpStyleName), -1,
+                                      (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpStyleName), -1,
                                       NULL, 0, NULL, NULL);
     if(lpOTMW->otmpFullName)
         needed += WideCharToMultiByte(CP_ACP, 0,
-                                      (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpFullName), -1,
+                                      (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpFullName), -1,
                                       NULL, 0, NULL, NULL);
 
     if(!lpOTM)
@@ -1162,7 +1279,7 @@ GetOutlineTextMetricsA(
     {
         output->otmpFamilyName = (LPSTR)(ptr - (char*)output);
         len = WideCharToMultiByte(CP_ACP, 0,
-                                  (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpFamilyName), -1,
+                                  (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpFamilyName), -1,
                                   ptr, left, NULL, NULL);
         left -= len;
         ptr += len;
@@ -1174,7 +1291,7 @@ GetOutlineTextMetricsA(
     {
         output->otmpFaceName = (LPSTR)(ptr - (char*)output);
         len = WideCharToMultiByte(CP_ACP, 0,
-                                  (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpFaceName), -1,
+                                  (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpFaceName), -1,
                                   ptr, left, NULL, NULL);
         left -= len;
         ptr += len;
@@ -1186,7 +1303,7 @@ GetOutlineTextMetricsA(
     {
         output->otmpStyleName = (LPSTR)(ptr - (char*)output);
         len = WideCharToMultiByte(CP_ACP, 0,
-                                  (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpStyleName), -1,
+                                  (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpStyleName), -1,
                                   ptr, left, NULL, NULL);
         left -= len;
         ptr += len;
@@ -1198,7 +1315,7 @@ GetOutlineTextMetricsA(
     {
         output->otmpFullName = (LPSTR)(ptr - (char*)output);
         len = WideCharToMultiByte(CP_ACP, 0,
-                                  (WCHAR*)((char*)lpOTMW + (int)lpOTMW->otmpFullName), -1,
+                                  (WCHAR*)((char*)lpOTMW + (intptr_t)lpOTMW->otmpFullName), -1,
                                   ptr, left, NULL, NULL);
         left -= len;
     }
@@ -1875,6 +1992,7 @@ RemoveFontResourceExW(LPCWSTR lpFileName,
     /* FIXME the flags */
     /* FIXME the pdv */
     /* FIXME NtGdiRemoveFontResource handle flags and pdv */
+    DPRINT("RemoveFontResourceExW\n");
     return 0;
 }
 
@@ -2115,7 +2233,8 @@ NewEnumFontFamiliesExW(
     LPARAM lParam,
     DWORD dwFlags)
 {
-    ULONG_PTR idEnum, cbDataSize, cbRetSize;
+    ULONG_PTR idEnum;
+    ULONG cbDataSize, cbRetSize;
     PENUMFONTDATAW pEfdw;
     PBYTE pBuffer;
     PBYTE pMax;
@@ -2181,7 +2300,7 @@ NewEnumFontFamiliesExW(
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
 int
 WINAPI
@@ -2190,7 +2309,45 @@ GdiAddFontResourceW(
     FLONG fl,
     DESIGNVECTOR *pdv)
 {
-    return NtGdiAddFontResourceW((PWSTR)lpszFilename, 0, 0, fl, 0, pdv);
+    INT Ret;
+    WCHAR lpszBuffer[MAX_PATH];
+    WCHAR lpszAbsPath[MAX_PATH];
+    UNICODE_STRING NtAbsPath;
+
+    /* FIXME: We don't support multiple files passed in lpszFilename
+     *        as L"abcxxxxx.pfm|abcxxxxx.pfb"
+     */
+
+    /* Does the file exist in CurrentDirectory or in the Absolute Path passed? */
+    GetCurrentDirectoryW(MAX_PATH, lpszBuffer);
+
+    if (!SearchPathW(lpszBuffer, lpszFilename, NULL, MAX_PATH, lpszAbsPath, NULL))
+    {
+        /* Nope. Then let's check Fonts folder */
+        GetWindowsDirectoryW(lpszBuffer, MAX_PATH);
+        StringCbCatW(lpszBuffer, sizeof(lpszBuffer), L"\\Fonts");
+
+        if (!SearchPathW(lpszBuffer, lpszFilename, NULL, MAX_PATH, lpszAbsPath, NULL))
+        {
+            DPRINT1("Font not found. The Buffer is: %ls, the FileName is: %S\n", lpszBuffer, lpszFilename);
+            return 0;
+        }
+    }
+
+    /* We found the font file so: */
+    if (!RtlDosPathNameToNtPathName_U(lpszAbsPath, &NtAbsPath, NULL, NULL))
+    {
+        DPRINT1("Can't convert Path! Path: %ls\n", lpszAbsPath);
+        return 0;
+    }
+
+    /* The Nt call expects a null-terminator included in cwc param. */
+    ASSERT(NtAbsPath.Buffer[NtAbsPath.Length / sizeof(WCHAR)] == UNICODE_NULL);
+    Ret = NtGdiAddFontResourceW(NtAbsPath.Buffer, NtAbsPath.Length / sizeof(WCHAR) + 1, 1, fl, 0, pdv);
+
+    RtlFreeUnicodeString(&NtAbsPath);
+
+    return Ret;
 }
 
 /*