Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove code duplications in createFontCustomPlatformData()
https://bugs.webkit.org/show_bug.cgi?id=123706

Reviewed by Darin Adler.

Move OpenTypeSanitizer and WOFF handling from the port specific
implementations in createFontCustomPlatformData() into the only
caller of this function CachedFont::ensureCustomFontData().
Also change the parameter passing the SharedBuffer from a
pointer to a reference since it is never null.

* loader/cache/CachedFont.cpp:
(WebCore::CachedFont::ensureCustomFontData):
* platform/graphics/blackberry/FontCustomPlatformData.h:
* platform/graphics/blackberry/FontCustomPlatformDataBlackBerry.cpp:
(WebCore::FontCustomPlatformData::FontCustomPlatformData):
(WebCore::createFontCustomPlatformData):
* platform/graphics/cairo/FontCustomPlatformData.h:
* platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp:
(WebCore::FontCustomPlatformData::FontCustomPlatformData):
(WebCore::createFontCustomPlatformData):
* platform/graphics/mac/FontCustomPlatformData.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/mac/FontCustomPlatformData.h:
* platform/graphics/win/FontCustomPlatformData.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/win/FontCustomPlatformData.h:
* platform/graphics/win/FontCustomPlatformDataCairo.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/wince/FontCustomPlatformData.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/wince/FontCustomPlatformData.h:


Canonical link: https://commits.webkit.org/141957@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@158623 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
paroga committed Nov 5, 2013
1 parent 98b8fbc commit 4af1302
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 88 deletions.
35 changes: 35 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,38 @@
2013-11-04 Patrick Gansterer <paroga@webkit.org>

Remove code duplications in createFontCustomPlatformData()
https://bugs.webkit.org/show_bug.cgi?id=123706

Reviewed by Darin Adler.

Move OpenTypeSanitizer and WOFF handling from the port specific
implementations in createFontCustomPlatformData() into the only
caller of this function CachedFont::ensureCustomFontData().
Also change the parameter passing the SharedBuffer from a
pointer to a reference since it is never null.

* loader/cache/CachedFont.cpp:
(WebCore::CachedFont::ensureCustomFontData):
* platform/graphics/blackberry/FontCustomPlatformData.h:
* platform/graphics/blackberry/FontCustomPlatformDataBlackBerry.cpp:
(WebCore::FontCustomPlatformData::FontCustomPlatformData):
(WebCore::createFontCustomPlatformData):
* platform/graphics/cairo/FontCustomPlatformData.h:
* platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp:
(WebCore::FontCustomPlatformData::FontCustomPlatformData):
(WebCore::createFontCustomPlatformData):
* platform/graphics/mac/FontCustomPlatformData.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/mac/FontCustomPlatformData.h:
* platform/graphics/win/FontCustomPlatformData.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/win/FontCustomPlatformData.h:
* platform/graphics/win/FontCustomPlatformDataCairo.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/wince/FontCustomPlatformData.cpp:
(WebCore::createFontCustomPlatformData):
* platform/graphics/wince/FontCustomPlatformData.h:

2013-11-04 Thiago de Barros Lacerda <thiago.lacerda@openbossa.org>

Fixing MediaStreamDescriptor addSource and addTrack methods
Expand Down
24 changes: 23 additions & 1 deletion Source/WebCore/loader/cache/CachedFont.cpp
Expand Up @@ -33,8 +33,11 @@
#include "FontCustomPlatformData.h"
#include "FontPlatformData.h"
#include "MemoryCache.h"
#include "OpenTypeSanitizer.h"
#include "ResourceBuffer.h"
#include "SharedBuffer.h"
#include "TextResourceDecoder.h"
#include "WOFFFileFormat.h"
#include <wtf/Vector.h>

#if ENABLE(SVG_FONTS)
Expand Down Expand Up @@ -92,7 +95,26 @@ void CachedFont::beginLoadIfNeeded(CachedResourceLoader* dl)
bool CachedFont::ensureCustomFontData()
{
if (!m_fontData && !errorOccurred() && !isLoading() && m_data) {
m_fontData = createFontCustomPlatformData(m_data.get()->sharedBuffer());
SharedBuffer* buffer = m_data.get()->sharedBuffer();
ASSERT(buffer);

#if USE(OPENTYPE_SANITIZER)
OpenTypeSanitizer sanitizer(buffer);
RefPtr<SharedBuffer> transcodeBuffer = sanitizer.sanitize();
buffer = transcodeBuffer.get();
#else
RefPtr<SharedBuffer> sfntBuffer;
if (isWOFF(buffer)) {
Vector<char> sfnt;
if (convertWOFFToSfnt(buffer, sfnt)) {
sfntBuffer = SharedBuffer::adoptVector(sfnt);
buffer = sfntBuffer.get();
} else
buffer = nullptr;
}
#endif

m_fontData = buffer ? createFontCustomPlatformData(*buffer) : nullptr;
if (m_fontData)
m_hasCreatedFontData = true;
else
Expand Down
Expand Up @@ -36,7 +36,7 @@ class SharedBuffer;
struct FontCustomPlatformData {
WTF_MAKE_NONCOPYABLE(FontCustomPlatformData);
public:
FontCustomPlatformData(FILECHAR* fontName, PassRefPtr<SharedBuffer>);
FontCustomPlatformData(FILECHAR* fontName, SharedBuffer&);
~FontCustomPlatformData();

FontPlatformData fontPlatformData(int size, bool syntheticBold, bool syntheticItalic, FontOrientation = Horizontal,
Expand All @@ -48,7 +48,7 @@ struct FontCustomPlatformData {
RefPtr<SharedBuffer> m_buffer;
};

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer*);
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer&);

} // namespace WebCore

Expand Down
Expand Up @@ -21,17 +21,16 @@

#include "FontPlatformData.h"
#include "ITypeUtils.h"
#include "OpenTypeSanitizer.h"
#include "SharedBuffer.h"

#include <BlackBerryPlatformGraphicsContext.h>
#include <fs_api.h>

namespace WebCore {

FontCustomPlatformData::FontCustomPlatformData(FILECHAR* fontName, PassRefPtr<SharedBuffer> buffer)
FontCustomPlatformData::FontCustomPlatformData(FILECHAR* fontName, SharedBuffer& buffer)
: m_fontName(strdup(fontName))
, m_buffer(buffer)
, m_buffer(&buffer)
{
}

Expand All @@ -56,21 +55,11 @@ bool FontCustomPlatformData::supportsFormat(const String& format)
return isSupported;
}

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer* buffer)
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer)
{
ASSERT_ARG(buffer, buffer);

#if USE(OPENTYPE_SANITIZER)
OpenTypeSanitizer sanitizer(buffer);
RefPtr<SharedBuffer> transcodeBuffer = sanitizer.sanitize();
if (!transcodeBuffer)
return nullptr; // validation failed.
buffer = transcodeBuffer.get();
#endif

FILECHAR name[MAX_FONT_NAME_LEN+1];
memset(name, 0, MAX_FONT_NAME_LEN+1);
if (FS_load_font(BlackBerry::Platform::Graphics::getIType(), 0, const_cast<FS_BYTE*>(reinterpret_cast<const FS_BYTE*>(buffer->data())), 0, MAX_FONT_NAME_LEN, name) != SUCCESS)
if (FS_load_font(BlackBerry::Platform::Graphics::getIType(), 0, const_cast<FS_BYTE*>(reinterpret_cast<const FS_BYTE*>(buffer.data())), 0, MAX_FONT_NAME_LEN, name) != SUCCESS)
return nullptr;

return std::make_unique<FontCustomPlatformData>(name, buffer);
Expand Down
Expand Up @@ -39,7 +39,7 @@ class SharedBuffer;
struct FontCustomPlatformData {
WTF_MAKE_NONCOPYABLE(FontCustomPlatformData);
public:
FontCustomPlatformData(FT_Face, SharedBuffer*);
FontCustomPlatformData(FT_Face, SharedBuffer&);
~FontCustomPlatformData();
FontPlatformData fontPlatformData(int size, bool bold, bool italic, FontOrientation = Horizontal, FontWidthVariant = RegularWidth, FontRenderingMode = NormalRenderingMode);
static bool supportsFormat(const String&);
Expand All @@ -49,7 +49,7 @@ struct FontCustomPlatformData {
cairo_font_face_t* m_fontFace;
};

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer*);
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer&);

}

Expand Down
Expand Up @@ -24,7 +24,6 @@

#include "FontPlatformData.h"
#include "SharedBuffer.h"
#include "WOFFFileFormat.h"
#include <cairo-ft.h>
#include <cairo.h>

Expand All @@ -35,15 +34,15 @@ static void releaseCustomFontData(void* data)
static_cast<SharedBuffer*>(data)->deref();
}

FontCustomPlatformData::FontCustomPlatformData(FT_Face freeTypeFace, SharedBuffer* buffer)
FontCustomPlatformData::FontCustomPlatformData(FT_Face freeTypeFace, SharedBuffer& buffer)
: m_freeTypeFace(freeTypeFace)
, m_fontFace(cairo_ft_font_face_create_for_ft_face(freeTypeFace, 0))
{
// FIXME Should we be setting some hinting options here?

buffer->ref(); // This is balanced by the buffer->deref() in releaseCustomFontData.
buffer.ref(); // This is balanced by the buffer->deref() in releaseCustomFontData.
static cairo_user_data_key_t bufferKey;
cairo_font_face_set_user_data(m_fontFace, &bufferKey, buffer,
cairo_font_face_set_user_data(m_fontFace, &bufferKey, &buffer,
static_cast<cairo_destroy_func_t>(releaseCustomFontData));

// Cairo doesn't do FreeType reference counting, so we need to ensure that when
Expand All @@ -64,28 +63,16 @@ FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, b
return FontPlatformData(m_fontFace, size, bold, italic);
}

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer* buffer)
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer)
{
ASSERT_ARG(buffer, buffer);

RefPtr<SharedBuffer> sfntBuffer;
if (isWOFF(buffer)) {
Vector<char> sfnt;
if (!convertWOFFToSfnt(buffer, sfnt))
return nullptr;

sfntBuffer = SharedBuffer::adoptVector(sfnt);
buffer = sfntBuffer.get();
}

static FT_Library library;
if (!library && FT_Init_FreeType(&library)) {
library = nullptr;
return nullptr;
}

FT_Face freeTypeFace;
if (FT_New_Memory_Face(library, reinterpret_cast<const FT_Byte*>(buffer->data()), buffer->size(), 0, &freeTypeFace))
if (FT_New_Memory_Face(library, reinterpret_cast<const FT_Byte*>(buffer.data()), buffer.size(), 0, &freeTypeFace))
return nullptr;
return std::make_unique<FontCustomPlatformData>(freeTypeFace, buffer);
}
Expand Down
26 changes: 2 additions & 24 deletions Source/WebCore/platform/graphics/mac/FontCustomPlatformData.cpp
Expand Up @@ -22,9 +22,7 @@
#include "FontCustomPlatformData.h"

#include "FontPlatformData.h"
#include "OpenTypeSanitizer.h"
#include "SharedBuffer.h"
#include "WOFFFileFormat.h"
#include <ApplicationServices/ApplicationServices.h>

namespace WebCore {
Expand All @@ -38,31 +36,11 @@ FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, b
return FontPlatformData(m_cgFont.get(), size, bold, italic, orientation, widthVariant);
}

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer* buffer)
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer)
{
ASSERT_ARG(buffer, buffer);

#if USE(OPENTYPE_SANITIZER)
OpenTypeSanitizer sanitizer(buffer);
RefPtr<SharedBuffer> transcodeBuffer = sanitizer.sanitize();
if (!transcodeBuffer)
return nullptr; // validation failed.
buffer = transcodeBuffer.get();
#else
RefPtr<SharedBuffer> sfntBuffer;
if (isWOFF(buffer)) {
Vector<char> sfnt;
if (!convertWOFFToSfnt(buffer, sfnt))
return nullptr;

sfntBuffer = SharedBuffer::adoptVector(sfnt);
buffer = sfntBuffer.get();
}
#endif

ATSFontContainerRef containerRef = 0;

RetainPtr<CFDataRef> bufferData = buffer->createCFData();
RetainPtr<CFDataRef> bufferData = buffer.createCFData();
RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateWithCFData(bufferData.get()));

RetainPtr<CGFontRef> cgFontRef = adoptCF(CGFontCreateWithDataProvider(dataProvider.get()));
Expand Down
Expand Up @@ -57,7 +57,7 @@ struct FontCustomPlatformData {
RetainPtr<CGFontRef> m_cgFont;
};

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer*);
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer&);

}

Expand Down
17 changes: 2 additions & 15 deletions Source/WebCore/platform/graphics/win/FontCustomPlatformData.cpp
Expand Up @@ -24,7 +24,6 @@
#include "FontPlatformData.h"
#include "OpenTypeUtilities.h"
#include "SharedBuffer.h"
#include "WOFFFileFormat.h"
#include <ApplicationServices/ApplicationServices.h>
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#include <wtf/RetainPtr.h>
Expand Down Expand Up @@ -81,23 +80,11 @@ static String createUniqueFontName()
return fontName;
}

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer* buffer)
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer)
{
ASSERT_ARG(buffer, buffer);

RefPtr<SharedBuffer> sfntBuffer;
if (isWOFF(buffer)) {
Vector<char> sfnt;
if (!convertWOFFToSfnt(buffer, sfnt))
return nullptr;

sfntBuffer = SharedBuffer::adoptVector(sfnt);
buffer = sfntBuffer.get();
}

String fontName = createUniqueFontName();
HANDLE fontReference;
fontReference = renameAndActivateFont(*buffer, fontName);
fontReference = renameAndActivateFont(buffer, fontName);
if (!fontReference)
return nullptr;
return std::make_unique<FontCustomPlatformData>(fontReference, fontName);
Expand Down
Expand Up @@ -56,7 +56,7 @@ struct FontCustomPlatformData {
String m_name;
};

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer*);
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer&);

}

Expand Down
Expand Up @@ -80,12 +80,10 @@ static String createUniqueFontName()
return fontName;
}

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer* buffer)
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer)
{
ASSERT_ARG(buffer, buffer);

String fontName = createUniqueFontName();
HANDLE fontReference = renameAndActivateFont(*buffer, fontName);
HANDLE fontReference = renameAndActivateFont(buffer, fontName);

if (!fontReference)
return nullptr;
Expand Down
Expand Up @@ -69,14 +69,12 @@ static String createUniqueFontName()
return fontName.replace('/', '_');
}

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(const SharedBuffer* buffer)
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer& buffer)
{
ASSERT_ARG(buffer, buffer);

String fontName = createUniqueFontName();

Vector<char> rewrittenFontData;
if (!renameFont(*buffer, fontName, rewrittenFontData))
if (!renameFont(buffer, fontName, rewrittenFontData))
return nullptr;

RefPtr<SharedBuffer> localBuffer = SharedBuffer::adoptVector(rewrittenFontData);
Expand Down
Expand Up @@ -56,7 +56,7 @@ namespace WebCore {
String m_name;
};

std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(const SharedBuffer*);
std::unique_ptr<FontCustomPlatformData> createFontCustomPlatformData(SharedBuffer&);
void setCustomFontCache(CustomFontCache*);
}

Expand Down

0 comments on commit 4af1302

Please sign in to comment.