Convert the width map for glyphs into the same new HashMap-style as the
authorhyatt <hyatt@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 May 2006 07:36:34 +0000 (07:36 +0000)
committerhyatt <hyatt@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 May 2006 07:36:34 +0000 (07:36 +0000)
        glyph map for characters.

        * WebCore.xcodeproj/project.pbxproj:
        * platform/FontData.cpp:
        (WebCore::FontData::~FontData):
        (WebCore::FontData::widthForGlyph):
        * platform/FontData.h:
        * platform/GlyphWidthMap.cpp: Added.
        (WebCore::GlyphWidthMap::widthForGlyph):
        (WebCore::GlyphWidthMap::setWidthForGlyph):
        (WebCore::GlyphWidthMap::locatePage):
        * platform/GlyphWidthMap.h: Added.
        (WebCore::GlyphWidthMap::GlyphWidthMap):
        (WebCore::GlyphWidthMap::~GlyphWidthMap):
        (WebCore::GlyphWidthMap::GlyphWidthPage::widthForGlyph):
        (WebCore::GlyphWidthMap::GlyphWidthPage::setWidthForGlyph):
        (WebCore::GlyphWidthMap::GlyphWidthPage::setWidthForIndex):

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

WebCore/ChangeLog
WebCore/WebCore.xcodeproj/project.pbxproj
WebCore/platform/FontData.cpp
WebCore/platform/FontData.h
WebCore/platform/GlyphWidthMap.cpp [new file with mode: 0644]
WebCore/platform/GlyphWidthMap.h [new file with mode: 0644]

index 19576b66c187a19c7fe5aa5993a4cf0aeaa4a434..c6828e3d3e28e901615a2db5dfe707baa1bfe5d6 100644 (file)
@@ -1,3 +1,24 @@
+2006-05-17  David Hyatt  <hyatt@apple.com>
+
+        Convert the width map for glyphs into the same new HashMap-style as the
+        glyph map for characters.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * platform/FontData.cpp:
+        (WebCore::FontData::~FontData):
+        (WebCore::FontData::widthForGlyph):
+        * platform/FontData.h:
+        * platform/GlyphWidthMap.cpp: Added.
+        (WebCore::GlyphWidthMap::widthForGlyph):
+        (WebCore::GlyphWidthMap::setWidthForGlyph):
+        (WebCore::GlyphWidthMap::locatePage):
+        * platform/GlyphWidthMap.h: Added.
+        (WebCore::GlyphWidthMap::GlyphWidthMap):
+        (WebCore::GlyphWidthMap::~GlyphWidthMap):
+        (WebCore::GlyphWidthMap::GlyphWidthPage::widthForGlyph):
+        (WebCore::GlyphWidthMap::GlyphWidthPage::setWidthForGlyph):
+        (WebCore::GlyphWidthMap::GlyphWidthPage::setWidthForIndex):
+
 2006-05-17  David Hyatt  <hyatt@apple.com>
 
         Rename FontData.mm to FontData.cpp, since it has no obj-c in it.
index 154ef3d9b6d8691af5d075a72f2a01cd61acc24c..bc756e09461c121e654668197fc3a09f4b4c6dd6 100644 (file)
                BCB16C2F0979C3BD00467741 /* Request.h in Headers */ = {isa = PBXBuildFile; fileRef = BCB16C160979C3BD00467741 /* Request.h */; };
                BCC088860A1BD78D006189A6 /* FontDataMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = BCC088850A1BD78D006189A6 /* FontDataMac.mm */; };
                BCC0891A0A1C4652006189A6 /* FontData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BCC089190A1C4652006189A6 /* FontData.cpp */; };
+               BCC0892A0A1C4991006189A6 /* GlyphWidthMap.h in Headers */ = {isa = PBXBuildFile; fileRef = BCC089290A1C4991006189A6 /* GlyphWidthMap.h */; };
+               BCC089560A1C4CC9006189A6 /* GlyphWidthMap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BCC089550A1C4CC9006189A6 /* GlyphWidthMap.cpp */; };
                BCC47E2609A3D6F100ADB771 /* FontFamily.cpp in Sources */ = {isa = PBXBuildFile; fileRef = BCC47E2409A3D6F100ADB771 /* FontFamily.cpp */; };
                BCC47E2709A3D6F100ADB771 /* FontFamily.h in Headers */ = {isa = PBXBuildFile; fileRef = BCC47E2509A3D6F100ADB771 /* FontFamily.h */; };
                BCC47E6B09A3FE4700ADB771 /* FontDescription.h in Headers */ = {isa = PBXBuildFile; fileRef = BCC47E6A09A3FE4700ADB771 /* FontDescription.h */; };
                BCB16C160979C3BD00467741 /* Request.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = Request.h; sourceTree = "<group>"; };
                BCC088850A1BD78D006189A6 /* FontDataMac.mm */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.objcpp; path = FontDataMac.mm; sourceTree = "<group>"; };
                BCC089190A1C4652006189A6 /* FontData.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = FontData.cpp; sourceTree = "<group>"; };
+               BCC089290A1C4991006189A6 /* GlyphWidthMap.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = GlyphWidthMap.h; sourceTree = "<group>"; };
+               BCC089550A1C4CC9006189A6 /* GlyphWidthMap.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = GlyphWidthMap.cpp; sourceTree = "<group>"; };
                BCC47E2409A3D6F100ADB771 /* FontFamily.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = FontFamily.cpp; sourceTree = "<group>"; };
                BCC47E2509A3D6F100ADB771 /* FontFamily.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FontFamily.h; sourceTree = "<group>"; };
                BCC47E6A09A3FE4700ADB771 /* FontDescription.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FontDescription.h; sourceTree = "<group>"; };
                                BCC71A120A0FF94D0014EE6E /* GlyphBuffer.h */,
                                BC6DB3680A1A7CB700E5CD14 /* GlyphMap.h */,
                                BC6DB4730A1A90FB00E5CD14 /* GlyphMap.cpp */,
+                               BCC089550A1C4CC9006189A6 /* GlyphWidthMap.cpp */,
+                               BCC089290A1C4991006189A6 /* GlyphWidthMap.h */,
                                A823A75B09B6E53900B60641 /* GraphicsContext.cpp */,
                                935367E409AF77DD00D35CD6 /* GraphicsContext.h */,
                                938E685309F0BE04008A48EC /* GraphicsTypes.cpp */,
                                A80E7E9D0A1A83E3007FB8C5 /* JSHTMLTextAreaElement.h in Headers */,
                                A80E7E9F0A1A83E3007FB8C5 /* JSHTMLButtonElement.h in Headers */,
                                A826E8AE0A1A8F2300CD1BB6 /* JSHTMLOptionElementConstructor.h in Headers */,
+                               BCC0892A0A1C4991006189A6 /* GlyphWidthMap.h in Headers */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                                1A9EF4570A1B957D00332B63 /* JSCanvasRenderingContext2DCustom.cpp in Sources */,
                                BCC088860A1BD78D006189A6 /* FontDataMac.mm in Sources */,
                                BCC0891A0A1C4652006189A6 /* FontData.cpp in Sources */,
+                               BCC089560A1C4CC9006189A6 /* GlyphWidthMap.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
index eb247ed978939ce73acc26f6650832a9cf037296..c4c8ea2fa924a0520fc6028b10ffbad0b15a5342 100644 (file)
 
 #import "config.h"
 #import "Font.h"
-#import "WebCoreSystemInterface.h"
 
 namespace WebCore
 {
 
-typedef float WebGlyphWidth;
-
-struct WidthMap {
-    WidthMap() :next(0), widths(0) {}
-
-    ATSGlyphRef startRange;
-    ATSGlyphRef endRange;
-    WidthMap *next;
-    WebGlyphWidth *widths;
-};
-
-static WidthMap *extendWidthMap(const FontData *, ATSGlyphRef);
-
-static void freeWidthMap(WidthMap *);
-
-// Map utility functions
-
-float FontData::widthForGlyph(Glyph glyph) const
-{
-    WidthMap *map;
-    for (map = m_glyphToWidthMap; 1; map = map->next) {
-        if (!map)
-            map = extendWidthMap(this, glyph);
-        if (glyph >= map->startRange && glyph <= map->endRange)
-            break;
-    }
-    float width = map->widths[glyph - map->startRange];
-    if (width >= 0)
-        return width;
-        
-    width = platformWidthForGlyph(glyph);
-    map->widths[glyph - map->startRange] = width;
-    return width;
-}
-
 FontData::FontData(const FontPlatformData& f)
-:m_font(f), m_glyphToWidthMap(0), m_treatAsFixedPitch(false),
+:m_font(f), m_treatAsFixedPitch(false),
  m_smallCapsFontData(0)
  {    
     m_font = f;
@@ -90,52 +54,20 @@ FontData::~FontData()
 {
     platformDestroy();
 
-    freeWidthMap(m_glyphToWidthMap);
-
     // We only get deleted when the cache gets cleared.  Since the smallCapsRenderer is also in that cache,
     // it will be deleted then, so we don't need to do anything here.
 }
 
-static WidthMap *extendWidthMap(const FontData *renderer, ATSGlyphRef glyph)
+float FontData::widthForGlyph(Glyph glyph) const
 {
-    WidthMap *map = new WidthMap;
-    unsigned end;
-    ATSGlyphRef start;
-    unsigned blockSize;
-    unsigned i, count;
+    float width = m_glyphToWidthMap.widthForGlyph(glyph);
+    if (width != cGlyphWidthUnknown)
+        return width;
     
-    blockSize = cGlyphPageSize;
-    start = (glyph / cGlyphPageSize) * cGlyphPageSize;
-    end = ((unsigned)start) + blockSize; 
-
-    map->startRange = start;
-    map->endRange = end;
-    count = end - start + 1;
-
-    map->widths = new WebGlyphWidth[count];
-    for (i = 0; i < count; i++)
-        map->widths[i] = NAN;
-
-    if (renderer->m_glyphToWidthMap == 0)
-        renderer->m_glyphToWidthMap = map;
-    else {
-        WidthMap *lastMap = renderer->m_glyphToWidthMap;
-        while (lastMap->next != 0)
-            lastMap = lastMap->next;
-        lastMap->next = map;
-    }
-
-    return map;
-}
-
-static void freeWidthMap(WidthMap *map)
-{
-    while (map) {
-        WidthMap *next = map->next;
-        delete []map->widths;
-        delete map;
-        map = next;
-    }
+    width = platformWidthForGlyph(glyph);
+    m_glyphToWidthMap.setWidthForGlyph(glyph, width);
+    
+    return width;
 }
 
 }
index 23b070747b4501924791d4e1901bbdd82b635397..7383babc3b47a52181fb3c196388e9092bde24bd 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "FontPlatformData.h"
 #include "GlyphMap.h"
+#include "GlyphWidthMap.h"
 #include <wtf/Noncopyable.h>
 
 // FIXME: Temporary.  Only needed to support API that's going to move.
@@ -91,7 +92,7 @@ public:
     
     FontPlatformData m_font;
     mutable GlyphMap m_characterToGlyphMap;
-    mutable WidthMap* m_glyphToWidthMap;
+    mutable GlyphWidthMap m_glyphToWidthMap;
 
     bool m_treatAsFixedPitch;
     ATSGlyphRef m_spaceGlyph;
diff --git a/WebCore/platform/GlyphWidthMap.cpp b/WebCore/platform/GlyphWidthMap.cpp
new file mode 100644 (file)
index 0000000..3345889
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2006 Apple Computer, 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 "GlyphWidthMap.h"
+
+namespace WebCore
+{
+
+float GlyphWidthMap::widthForGlyph(Glyph g)
+{
+    unsigned pageNumber = (g / cGlyphWidthPageSize);
+    GlyphWidthPage* page = locatePage(pageNumber);
+    if (page)
+        return page->widthForGlyph(g);
+    return cGlyphWidthUnknown;
+}
+
+void GlyphWidthMap::setWidthForGlyph(Glyph glyph, float width)
+{
+    unsigned pageNumber = (glyph / cGlyphWidthPageSize);
+    GlyphWidthPage* page = locatePage(pageNumber);
+    if (page)
+        page->setWidthForGlyph(glyph, width);
+}
+
+inline GlyphWidthMap::GlyphWidthPage* GlyphWidthMap::locatePage(unsigned pageNumber)
+{
+    GlyphWidthPage* page;
+    if (pageNumber == 0) {
+        if (m_filledPrimaryPage)
+            return &m_primaryPage;
+        page = &m_primaryPage; 
+    } else {
+        if (m_pages) {
+            GlyphWidthPage* result = m_pages->get(pageNumber);
+            if (result)
+                return result;
+        }
+        page = new GlyphWidthPage;
+        if (!m_pages)
+            m_pages = new HashMap<int, GlyphWidthPage*>;
+        m_pages->set(pageNumber, page);
+    }
+    
+    // Fill in the whole page with the unknown glyph width value.
+    for (unsigned i = 0; i < cGlyphWidthPageSize; i++)
+        page->setWidthForIndex(i, cGlyphWidthUnknown);
+
+    return page;
+}
+
+}
diff --git a/WebCore/platform/GlyphWidthMap.h b/WebCore/platform/GlyphWidthMap.h
new file mode 100644 (file)
index 0000000..0400d6b
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2006 Apple Computer, 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 GLYPH_WIDTH_MAP_H
+#define GLYPH_WIDTH_MAP_H
+
+#include <unicode/umachine.h>
+#include "GlyphBuffer.h"
+#include <wtf/Noncopyable.h>
+#include <wtf/HashMap.h>
+
+namespace WebCore {
+
+// Covers Latin-1.
+const unsigned cGlyphWidthPageSize = 256;
+const float cGlyphWidthUnknown = -1;
+
+class FontData;
+
+class GlyphWidthMap : Noncopyable
+{
+public:
+    GlyphWidthMap() : m_filledPrimaryPage(false), m_pages(0) {}
+    ~GlyphWidthMap() { deleteAllValues(*m_pages); delete m_pages; }
+
+    float widthForGlyph(Glyph g);
+    void setWidthForGlyph(Glyph g, float f);
+
+private:
+    struct GlyphWidthPage {
+        float m_widths[cGlyphWidthPageSize];
+
+        float widthForGlyph(Glyph g) const { return m_widths[g % cGlyphWidthPageSize]; }
+        void setWidthForGlyph(Glyph g, float f)
+        {
+            setWidthForIndex(g % cGlyphWidthPageSize, f);
+        }
+        
+        void setWidthForIndex(unsigned index, float f) {
+            m_widths[index] = f;
+        }
+    };
+    
+    GlyphWidthPage* locatePage(unsigned page);
+    
+    bool m_filledPrimaryPage;
+    GlyphWidthPage m_primaryPage; // We optimize for the page that contains glyph indices 0-255.
+    HashMap<int, GlyphWidthPage*>* m_pages;
+};
+
+}
+#endif