Fix <rdar://5549919> Add font database initialization code to WebCore and call it...
authoraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Oct 2007 05:23:20 +0000 (05:23 +0000)
committeraroben <aroben@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Oct 2007 05:23:20 +0000 (05:23 +0000)
 WebCore:

         Add font database initialization code to WebCore

         Reviewed by Ada.

         * WebCore.vcproj/WebCore.vcproj: Added FontDatabase.{cpp,h}.
         * platform/win/FontDatabase.cpp: Added.
         (WebCore::systemFontsDirectory):
         (WebCore::fontsPlistPath):
         (WebCore::systemHasFontsNewerThanFontsPlist):
         (WebCore::readFontPlist):
         (WebCore::populateFontDatabaseFromPlist):
         (WebCore::populateFontDatabaseFromFileSystem):
         (WebCore::writeFontDatabaseToPlist):
         (WebCore::populateFontDatabase): This is the only function callable
         from outside this file. It populates the font database once, either
         from the fonts plist, or from the filesystem (and then saves a new
         fonts plist).
         * platform/win/FontDatabase.h: Added.

 WebKitTools:

         Remove now-unnecessary call to InitializeCoreGraphics

         WebKit takes care of this now.

         Reviewed by Ada.

         * DumpRenderTree/win/DumpRenderTree.cpp:
         (initialize):

 WebKit/win:

         Fix <rdar://5549919> Initialize the font database before any font code is invoked

         We initialize the database in two places:
            1. When instantiating WebKitClassFactory, which is guaranteed to
            happen before any WebView is instantiated.
            2. When making a WebCore::Font in WebKitGraphics.

         This ensures that the font database will be populated before any font
         code is invoked. We rely on WebCore to only populate the database
         once.

         Reviewed by Ada.

         * WebKitClassFactory.cpp:
         (WebKitClassFactory::WebKitClassFactory): Populate the font database.
         * WebKitGraphics.cpp:
         (makeFont): Ditto.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@27020 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/WebCore.vcproj/WebCore.vcproj
WebCore/platform/win/FontDatabase.cpp [new file with mode: 0644]
WebCore/platform/win/FontDatabase.h [new file with mode: 0644]
WebKit/win/ChangeLog
WebKit/win/WebKitClassFactory.cpp
WebKit/win/WebKitGraphics.cpp
WebKitTools/ChangeLog
WebKitTools/DumpRenderTree/win/DumpRenderTree.cpp

index b98ce202cb9bab215cf1c40a623c8098fa6221c2..3fc50282a43dfe78bf31adf7cbcba0969b836944 100644 (file)
@@ -1,3 +1,24 @@
+2007-10-24  Adam Roben  <aroben@apple.com>
+
+        Add font database initialization code to WebCore
+
+        Reviewed by Ada.
+
+        * WebCore.vcproj/WebCore.vcproj: Added FontDatabase.{cpp,h}.
+        * platform/win/FontDatabase.cpp: Added.
+        (WebCore::systemFontsDirectory):
+        (WebCore::fontsPlistPath):
+        (WebCore::systemHasFontsNewerThanFontsPlist):
+        (WebCore::readFontPlist):
+        (WebCore::populateFontDatabaseFromPlist):
+        (WebCore::populateFontDatabaseFromFileSystem):
+        (WebCore::writeFontDatabaseToPlist):
+        (WebCore::populateFontDatabase): This is the only function callable
+        from outside this file. It populates the font database once, either
+        from the fonts plist, or from the filesystem (and then saves a new
+        fonts plist).
+        * platform/win/FontDatabase.h: Added.
+
 2007-10-24  Justin Garcia  <justin.garcia@apple.com>
 
         Reviewed by Darin Adler.
         (WebCore::HTMLImageLoader::haveFiredLoadEvent):
         Make this public.
 
-2007-10-24  Adam Roben  <aroben@apple.com>
+2007-10-23  Adam Roben  <aroben@apple.com>
 
         Move Windows safe file creation code into WebCore from WebPreferences
 
index 4296f221416232c9c6e2e196e5d47990721333be..2b6d492fc641bca180dbf596783d25f06dee205c 100644 (file)
                                        RelativePath="..\platform\win\FontCustomPlatformData.h"\r
                                        >\r
                                </File>\r
+                               <File\r
+                                       RelativePath="..\platform\win\FontDatabase.cpp"\r
+                                       >\r
+                               </File>\r
+                               <File\r
+                                       RelativePath="..\platform\win\FontDatabase.h"\r
+                                       >\r
+                               </File>\r
                                <File\r
                                        RelativePath="..\platform\win\FontDataWin.cpp"\r
                                        >\r
diff --git a/WebCore/platform/win/FontDatabase.cpp b/WebCore/platform/win/FontDatabase.cpp
new file mode 100644 (file)
index 0000000..4339045
--- /dev/null
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2007 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "FontDatabase.h"
+
+#include "CString.h"
+#include "FileSystem.h"
+#include "PlatformString.h"
+#include <WebKitSystemInterface/WebKitSystemInterface.h>
+#include <shlobj.h>
+#include <wtf/RetainPtr.h>
+
+namespace WebCore {
+
+static String systemFontsDirectory()
+{
+    static bool initialized;
+    static String directory;
+
+    if (!initialized) {
+        initialized = true;
+
+        Vector<UChar> buffer(MAX_PATH);
+        if (FAILED(SHGetFolderPath(0, CSIDL_FONTS | CSIDL_FLAG_CREATE, 0, 0, buffer.data())))
+            return directory;
+        buffer.resize(wcslen(buffer.data()));
+
+        directory = String::adopt(buffer);
+    }
+
+    return directory;
+}
+
+static String fontsPlistPath()
+{
+    static String path = pathByAppendingComponent(localUserSpecificStorageDirectory(), "FontsTable.plist");
+    return path;
+}
+
+static bool systemHasFontsNewerThanFontsPlist()
+{
+    WIN32_FILE_ATTRIBUTE_DATA plistAttributes = {0};
+    if (!GetFileAttributesEx(fontsPlistPath().charactersWithNullTermination(), GetFileExInfoStandard, &plistAttributes))
+        return true;
+
+    WIN32_FILE_ATTRIBUTE_DATA fontsDirectoryAttributes = {0};
+    if (!GetFileAttributesEx(systemFontsDirectory().charactersWithNullTermination(), GetFileExInfoStandard, &fontsDirectoryAttributes))
+        return true;
+
+    return CompareFileTime(&plistAttributes.ftLastWriteTime, &fontsDirectoryAttributes.ftLastWriteTime) < 0;
+}
+
+static RetainPtr<CFPropertyListRef> readFontPlist()
+{
+    CString plistPath = fontsPlistPath().utf8();
+
+    RetainPtr<CFURLRef> url(AdoptCF, CFURLCreateFromFileSystemRepresentation(0, reinterpret_cast<const UInt8*>(plistPath.data()), plistPath.length(), false));
+    if (!url)
+        return 0;
+
+    RetainPtr<CFReadStreamRef> stream(AdoptCF, CFReadStreamCreateWithFile(0, url.get()));
+    if (!stream)
+        return 0;
+
+    if (!CFReadStreamOpen(stream.get()))
+        return 0;
+
+    CFPropertyListFormat format = kCFPropertyListBinaryFormat_v1_0 | kCFPropertyListXMLFormat_v1_0;
+    RetainPtr<CFPropertyListRef> plist(AdoptCF, CFPropertyListCreateFromStream(0, stream.get(), 0, kCFPropertyListMutableContainersAndLeaves, &format, 0));
+
+    CFReadStreamClose(stream.get());
+
+    return plist;
+}
+
+static bool populateFontDatabaseFromPlist()
+{
+    RetainPtr<CFPropertyListRef> plist = readFontPlist();
+    if (!plist)
+        return false;
+
+    RetainPtr<CFDataRef> data(AdoptCF, CFPropertyListCreateXMLData(0, plist.get()));
+    if (!data)
+        return false;
+
+    wkAddFontsFromPlistRepresentation(data.get());
+    return true;
+}
+
+static bool populateFontDatabaseFromFileSystem()
+{
+    RetainPtr<CFStringRef> directory(AdoptCF, systemFontsDirectory().createCFString());
+    if (!directory)
+        return false;
+
+    wkAddFontsInDirectory(directory.get());
+    return true;
+}
+
+static void writeFontDatabaseToPlist()
+{
+    RetainPtr<CFDataRef> data(AdoptCF, wkCreateFontsPlistRepresentation());
+    if (!data)
+        return;
+
+    safeCreateFile(fontsPlistPath(), data.get());
+}
+
+void populateFontDatabase()
+{
+    static bool initialized;
+    if (initialized)
+        return;
+    initialized = true;
+
+    if (!systemHasFontsNewerThanFontsPlist())
+        if (populateFontDatabaseFromPlist())
+            return;
+
+    if (populateFontDatabaseFromFileSystem())
+        writeFontDatabaseToPlist();
+}
+
+} // namespace WebCore
diff --git a/WebCore/platform/win/FontDatabase.h b/WebCore/platform/win/FontDatabase.h
new file mode 100644 (file)
index 0000000..4f76c9e
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2007 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef FontDatabase_h
+#define FontDatabase_h
+
+namespace WebCore {
+
+    void populateFontDatabase();
+
+} // namespace WebCore
+
+#endif // !defined(FontDatabase_h)
index d2dcd43ec4783fa3c572695a298ffa51e3e89fa7..4063931b3f03e7dd30943f0752558c5caf828a68 100644 (file)
@@ -1,3 +1,23 @@
+2007-10-24  Adam Roben  <aroben@apple.com>
+
+        Fix <rdar://5549919> Initialize the font database before any font code is invoked
+
+        We initialize the database in two places:
+           1. When instantiating WebKitClassFactory, which is guaranteed to
+           happen before any WebView is instantiated.
+           2. When making a WebCore::Font in WebKitGraphics.
+
+        This ensures that the font database will be populated before any font
+        code is invoked. We rely on WebCore to only populate the database
+        once.
+
+        Reviewed by Ada.
+
+        * WebKitClassFactory.cpp:
+        (WebKitClassFactory::WebKitClassFactory): Populate the font database.
+        * WebKitGraphics.cpp:
+        (makeFont): Ditto.
+
 2007-10-24  Adam Roben  <aroben@apple.com>
 
         Add [I]WebTextRenderer
index bbaa793d3aad9cb1d67b67d0289b7d0c67e3eca1..b41713b1ce433721edbe85ec1ce4dae4acdbbb29 100644 (file)
@@ -50,6 +50,9 @@
 #include "WebDebugProgram.h"
 #include "WebView.h"
 #include <SafariTheme/SafariTheme.h>
+#pragma warning(push, 0)
+#include <WebCore/FontDatabase.h>
+#pragma warning(pop)
 
 // WebKitClassFactory ---------------------------------------------------------
 
@@ -67,6 +70,8 @@ WebKitClassFactory::WebKitClassFactory(CLSID targetClass)
         didInitializeSafariTheme = true;
     }
 
+    WebCore::populateFontDatabase();
+
     gClassCount++;
 }
 
index a782f2c23099494dd94b9069eb54b8d649ac4dde..66ec08e706abd316626ae4942832f2b48f6d7c5f 100644 (file)
@@ -32,6 +32,7 @@
 #pragma warning(push, 0)
 #include <WebCore/CharacterNames.h>
 #include <WebCore/Font.h>
+#include <WebCore/FontDatabase.h>
 #include <WebCore/FontDescription.h>
 #include <WebCore/FontSelector.h>
 #include <WebCore/GraphicsContext.h>
@@ -49,6 +50,7 @@ using namespace WebCore;
 static Font makeFont(const WebFontDescription& description)
 {
     AtomicString::init();
+    populateFontDatabase();
 
     String fontFamilyString(description.family, description.familyLength);
 
index 6cc6a31b9fa52a20a929829bf2cb5f21d45e7582..53a58ce0ee64b959a6be70a618d4a8c865c66739 100644 (file)
@@ -1,3 +1,14 @@
+2007-10-24  Adam Roben  <aroben@apple.com>
+
+        Remove now-unnecessary call to InitializeCoreGraphics
+
+        WebKit takes care of this now.
+
+        Reviewed by Ada.
+
+        * DumpRenderTree/win/DumpRenderTree.cpp:
+        (initialize):
+
 2007-10-24  Adam Roben  <aroben@apple.com>
 
         Use WebTextRenderer in DRT
index 3cfbd8f760bc929ee2fac79132b9dee0f98f57b9..6fb962afe4ef280ab87c0cca42de585b45f43ba6 100644 (file)
@@ -109,8 +109,6 @@ static LRESULT CALLBACK DumpRenderTreeWndProc(HWND hWnd, UINT msg, WPARAM wParam
     }
 }
 
-extern "C" BOOL InitializeCoreGraphics();
-
 #ifdef DEBUG_WEBKIT_HAS_SUFFIX
 #define WEBKITDLL TEXT("WebKit_debug.dll")
 #else
@@ -167,9 +165,6 @@ static wstring initialize(HMODULE hModule)
         for (int i = 0; i < ARRAYSIZE(fontsToInstall); ++i)
             textRenderer->registerPrivateFont(wstring(resourcesPath + fontsToInstall[i]).c_str());
 
-    // Initialize CG
-    InitializeCoreGraphics();
-
     // Register a host window
     WNDCLASSEX wcex;