2011-03-10 14:07:55 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-05-11 10:27:36 -07:00
|
|
|
|
2012-08-23 03:23:43 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
|
2010-05-11 10:27:36 -07:00
|
|
|
#include "gfxAndroidPlatform.h"
|
2011-11-02 12:55:03 -07:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2010-05-11 10:27:36 -07:00
|
|
|
|
2011-09-23 04:15:36 -07:00
|
|
|
#include "gfxFT2FontList.h"
|
2010-05-11 10:27:36 -07:00
|
|
|
#include "gfxImageSurface.h"
|
2012-08-23 03:23:43 -07:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2012-02-08 14:52:57 -08:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-03-13 13:59:26 -07:00
|
|
|
#include "nsIScreen.h"
|
|
|
|
#include "nsIScreenManager.h"
|
2010-05-11 10:27:36 -07:00
|
|
|
|
2011-09-23 04:15:36 -07:00
|
|
|
#include "cairo.h"
|
2010-05-11 10:27:36 -07:00
|
|
|
|
|
|
|
#include "ft2build.h"
|
|
|
|
#include FT_FREETYPE_H
|
2012-11-19 19:00:19 -08:00
|
|
|
#include FT_MODULE_H
|
|
|
|
|
2011-11-02 12:55:03 -07:00
|
|
|
using namespace mozilla;
|
2012-08-23 03:23:43 -07:00
|
|
|
using namespace mozilla::dom;
|
2011-11-02 12:55:03 -07:00
|
|
|
using namespace mozilla::gfx;
|
2010-05-11 10:27:36 -07:00
|
|
|
|
|
|
|
static FT_Library gPlatformFTLibrary = NULL;
|
|
|
|
|
2011-01-12 20:04:42 -08:00
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GeckoFonts" , ## args)
|
2010-05-11 10:27:36 -07:00
|
|
|
|
2012-11-19 19:00:19 -08:00
|
|
|
static int64_t sFreetypeMemoryUsed;
|
|
|
|
static FT_MemoryRec_ sFreetypeMemoryRecord;
|
|
|
|
|
|
|
|
static int64_t
|
|
|
|
GetFreetypeSize()
|
|
|
|
{
|
|
|
|
return sFreetypeMemoryUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_MEMORY_REPORTER_IMPLEMENT(Freetype,
|
|
|
|
"explicit/freetype",
|
|
|
|
KIND_HEAP,
|
|
|
|
UNITS_BYTES,
|
|
|
|
GetFreetypeSize,
|
|
|
|
"Memory used by Freetype."
|
|
|
|
)
|
|
|
|
|
2012-12-23 16:59:51 -08:00
|
|
|
NS_MEMORY_REPORTER_MALLOC_SIZEOF_ON_ALLOC_FUN(FreetypeMallocSizeOfOnAlloc)
|
2012-12-10 16:05:07 -08:00
|
|
|
NS_MEMORY_REPORTER_MALLOC_SIZEOF_ON_FREE_FUN(FreetypeMallocSizeOfOnFree)
|
2012-11-19 19:00:19 -08:00
|
|
|
|
2012-11-19 20:14:14 -08:00
|
|
|
static void*
|
|
|
|
CountingAlloc(FT_Memory memory, long size)
|
2012-11-19 19:00:19 -08:00
|
|
|
{
|
|
|
|
void *p = malloc(size);
|
2012-12-10 16:05:07 -08:00
|
|
|
sFreetypeMemoryUsed += FreetypeMallocSizeOfOnAlloc(p);
|
2012-11-19 19:00:19 -08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CountingFree(FT_Memory memory, void* p)
|
|
|
|
{
|
2012-12-10 16:05:07 -08:00
|
|
|
sFreetypeMemoryUsed -= FreetypeMallocSizeOfOnFree(p);
|
2012-11-19 19:00:19 -08:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2012-11-19 20:14:14 -08:00
|
|
|
static void*
|
|
|
|
CountingRealloc(FT_Memory memory, long cur_size, long new_size, void* p)
|
2012-11-19 19:00:19 -08:00
|
|
|
{
|
2012-12-10 16:05:07 -08:00
|
|
|
sFreetypeMemoryUsed -= FreetypeMallocSizeOfOnFree(p);
|
2012-11-19 19:00:19 -08:00
|
|
|
void *pnew = realloc(p, new_size);
|
|
|
|
if (pnew) {
|
2012-12-10 16:05:07 -08:00
|
|
|
sFreetypeMemoryUsed += FreetypeMallocSizeOfOnAlloc(pnew);
|
2012-11-19 19:00:19 -08:00
|
|
|
} else {
|
|
|
|
// realloc failed; undo the decrement from above
|
2012-12-10 16:05:07 -08:00
|
|
|
sFreetypeMemoryUsed += FreetypeMallocSizeOfOnAlloc(p);
|
2012-11-19 19:00:19 -08:00
|
|
|
}
|
|
|
|
return pnew;
|
|
|
|
}
|
|
|
|
|
2010-05-11 10:27:36 -07:00
|
|
|
gfxAndroidPlatform::gfxAndroidPlatform()
|
|
|
|
{
|
2012-11-19 19:00:19 -08:00
|
|
|
// A custom allocator. It counts allocations, enabling memory reporting.
|
|
|
|
sFreetypeMemoryRecord.user = nullptr;
|
|
|
|
sFreetypeMemoryRecord.alloc = CountingAlloc;
|
|
|
|
sFreetypeMemoryRecord.free = CountingFree;
|
|
|
|
sFreetypeMemoryRecord.realloc = CountingRealloc;
|
|
|
|
|
|
|
|
// These two calls are equivalent to FT_Init_FreeType(), but allow us to
|
|
|
|
// provide a custom memory allocator.
|
|
|
|
FT_New_Library(&sFreetypeMemoryRecord, &gPlatformFTLibrary);
|
|
|
|
FT_Add_Default_Modules(gPlatformFTLibrary);
|
|
|
|
|
|
|
|
NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(Freetype));
|
2012-03-13 13:59:26 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIScreenManager> screenMgr = do_GetService("@mozilla.org/gfx/screenmanager;1");
|
|
|
|
nsCOMPtr<nsIScreen> screen;
|
|
|
|
screenMgr->GetPrimaryScreen(getter_AddRefs(screen));
|
2012-05-15 12:41:20 -07:00
|
|
|
mScreenDepth = 24;
|
|
|
|
screen->GetColorDepth(&mScreenDepth);
|
2012-03-13 13:59:26 -07:00
|
|
|
|
2012-05-15 12:41:20 -07:00
|
|
|
mOffscreenFormat = mScreenDepth == 16
|
|
|
|
? gfxASurface::ImageFormatRGB16_565
|
2012-06-11 20:41:46 -07:00
|
|
|
: gfxASurface::ImageFormatRGB24;
|
2010-05-11 10:27:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxAndroidPlatform::~gfxAndroidPlatform()
|
|
|
|
{
|
|
|
|
cairo_debug_reset_static_data();
|
|
|
|
|
2012-11-19 19:00:19 -08:00
|
|
|
FT_Done_Library(gPlatformFTLibrary);
|
2010-05-11 10:27:36 -07:00
|
|
|
gPlatformFTLibrary = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
gfxAndroidPlatform::CreateOffscreenSurface(const gfxIntSize& size,
|
2010-09-16 14:34:53 -07:00
|
|
|
gfxASurface::gfxContentType contentType)
|
2010-05-11 10:27:36 -07:00
|
|
|
{
|
2010-06-28 23:42:28 -07:00
|
|
|
nsRefPtr<gfxASurface> newSurface;
|
2012-05-25 21:38:17 -07:00
|
|
|
newSurface = new gfxImageSurface(size, OptimalFormatForContent(contentType));
|
2010-05-11 10:27:36 -07:00
|
|
|
|
|
|
|
return newSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxAndroidPlatform::GetFontList(nsIAtom *aLangGroup,
|
|
|
|
const nsACString& aGenericFamily,
|
|
|
|
nsTArray<nsString>& aListOfFonts)
|
|
|
|
{
|
2011-09-23 04:15:36 -07:00
|
|
|
gfxPlatformFontList::PlatformFontList()->GetFontList(aLangGroup,
|
|
|
|
aGenericFamily,
|
|
|
|
aListOfFonts);
|
2010-05-11 10:27:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-12 20:04:42 -08:00
|
|
|
void
|
|
|
|
gfxAndroidPlatform::GetFontList(InfallibleTArray<FontListEntry>* retValue)
|
2010-05-11 10:27:36 -07:00
|
|
|
{
|
2011-09-23 04:15:36 -07:00
|
|
|
gfxFT2FontList::PlatformFontList()->GetFontList(retValue);
|
2011-01-12 20:04:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxAndroidPlatform::UpdateFontList()
|
|
|
|
{
|
2011-09-23 04:15:36 -07:00
|
|
|
gfxPlatformFontList::PlatformFontList()->UpdateFontList();
|
2010-05-11 10:27:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxAndroidPlatform::ResolveFontName(const nsAString& aFontName,
|
|
|
|
FontResolverCallback aCallback,
|
|
|
|
void *aClosure,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool& aAborted)
|
2010-05-11 10:27:36 -07:00
|
|
|
{
|
2010-07-15 23:03:45 -07:00
|
|
|
nsAutoString resolvedName;
|
2011-09-23 04:15:36 -07:00
|
|
|
if (!gfxPlatformFontList::PlatformFontList()->
|
|
|
|
ResolveFontName(aFontName, resolvedName)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aAborted = false;
|
2011-09-23 04:15:36 -07:00
|
|
|
return NS_OK;
|
2010-05-11 10:27:36 -07:00
|
|
|
}
|
2011-09-23 04:15:36 -07:00
|
|
|
aAborted = !(*aCallback)(resolvedName, aClosure);
|
2010-05-11 10:27:36 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxAndroidPlatform::GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName)
|
|
|
|
{
|
2011-09-23 04:15:36 -07:00
|
|
|
gfxPlatformFontList::PlatformFontList()->GetStandardFamilyName(aFontName, aFamilyName);
|
|
|
|
return NS_OK;
|
2010-05-11 10:27:36 -07:00
|
|
|
}
|
|
|
|
|
2010-07-15 23:03:45 -07:00
|
|
|
gfxPlatformFontList*
|
|
|
|
gfxAndroidPlatform::CreatePlatformFontList()
|
|
|
|
{
|
2010-11-08 03:02:27 -08:00
|
|
|
gfxPlatformFontList* list = new gfxFT2FontList();
|
|
|
|
if (NS_SUCCEEDED(list->InitFontList())) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
gfxPlatformFontList::Shutdown();
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2010-07-15 23:03:45 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxAndroidPlatform::IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags)
|
2010-07-15 23:03:45 -07:00
|
|
|
{
|
|
|
|
// check for strange format flags
|
|
|
|
NS_ASSERTION(!(aFormatFlags & gfxUserFontSet::FLAG_FORMAT_NOT_USED),
|
|
|
|
"strange font format hint set");
|
|
|
|
|
|
|
|
// accept supported formats
|
2011-03-22 18:40:00 -07:00
|
|
|
if (aFormatFlags & (gfxUserFontSet::FLAG_FORMAT_OPENTYPE |
|
|
|
|
gfxUserFontSet::FLAG_FORMAT_WOFF |
|
2010-07-15 23:03:45 -07:00
|
|
|
gfxUserFontSet::FLAG_FORMAT_TRUETYPE)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-07-15 23:03:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// reject all other formats, known and unknown
|
|
|
|
if (aFormatFlags != 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-07-15 23:03:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// no format hint set, need to look at data
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-07-15 23:03:45 -07:00
|
|
|
}
|
|
|
|
|
2010-05-11 10:27:36 -07:00
|
|
|
gfxFontGroup *
|
|
|
|
gfxAndroidPlatform::CreateFontGroup(const nsAString &aFamilies,
|
|
|
|
const gfxFontStyle *aStyle,
|
|
|
|
gfxUserFontSet* aUserFontSet)
|
|
|
|
{
|
2011-09-23 04:15:36 -07:00
|
|
|
return new gfxFontGroup(aFamilies, aStyle, aUserFontSet);
|
2010-05-11 10:27:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FT_Library
|
|
|
|
gfxAndroidPlatform::GetFTLibrary()
|
|
|
|
{
|
|
|
|
return gPlatformFTLibrary;
|
|
|
|
}
|
|
|
|
|
2010-07-15 23:03:45 -07:00
|
|
|
gfxFontEntry*
|
|
|
|
gfxAndroidPlatform::MakePlatformFont(const gfxProxyFontEntry *aProxyEntry,
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint8_t *aFontData, uint32_t aLength)
|
2010-07-15 23:03:45 -07:00
|
|
|
{
|
|
|
|
return gfxPlatformFontList::PlatformFontList()->MakePlatformFont(aProxyEntry,
|
|
|
|
aFontData,
|
|
|
|
aLength);
|
|
|
|
}
|
|
|
|
|
2012-09-24 08:02:49 -07:00
|
|
|
TemporaryRef<ScaledFont>
|
2012-07-24 03:18:37 -07:00
|
|
|
gfxAndroidPlatform::GetScaledFontForFont(DrawTarget* aTarget, gfxFont *aFont)
|
2011-11-02 12:55:03 -07:00
|
|
|
{
|
2012-07-25 23:48:24 -07:00
|
|
|
NativeFont nativeFont;
|
2012-07-24 03:18:39 -07:00
|
|
|
if (aTarget->GetType() == BACKEND_CAIRO) {
|
|
|
|
nativeFont.mType = NATIVE_FONT_CAIRO_FONT_FACE;
|
|
|
|
nativeFont.mFont = NULL;
|
|
|
|
return Factory::CreateScaledFontWithCairo(nativeFont, aFont->GetAdjustedSize(), aFont->GetCairoScaledFont());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(aFont->GetType() == gfxFont::FONT_TYPE_FT2, "Expecting Freetype font");
|
2011-11-02 12:55:03 -07:00
|
|
|
nativeFont.mType = NATIVE_FONT_SKIA_FONT_FACE;
|
2012-05-16 15:30:10 -07:00
|
|
|
nativeFont.mFont = static_cast<gfxFT2FontBase*>(aFont)->GetFontOptions();
|
2012-07-24 03:18:39 -07:00
|
|
|
return Factory::CreateScaledFontForNativeFont(nativeFont, aFont->GetAdjustedSize());
|
2011-11-02 12:55:03 -07:00
|
|
|
}
|
|
|
|
|
2012-02-08 14:52:57 -08:00
|
|
|
bool
|
|
|
|
gfxAndroidPlatform::FontHintingEnabled()
|
|
|
|
{
|
|
|
|
// In "mobile" builds, we sometimes use non-reflow-zoom, so we
|
|
|
|
// might not want hinting. Let's see.
|
2013-01-15 07:19:28 -08:00
|
|
|
|
2012-02-08 14:52:57 -08:00
|
|
|
#ifdef MOZ_USING_ANDROID_JAVA_WIDGETS
|
|
|
|
// On android-java, we currently only use gecko to render web
|
|
|
|
// content that can always be be non-reflow-zoomed. So turn off
|
|
|
|
// hinting.
|
|
|
|
//
|
2013-01-15 07:19:28 -08:00
|
|
|
// XXX when gecko-android-java is used as an "app runtime", we may
|
|
|
|
// want to re-enable hinting for non-browser processes there.
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_B2G
|
|
|
|
// On B2G, the UX preference is currently to keep hinting disabled
|
|
|
|
// for all text (see bug 829523).
|
2012-02-08 14:52:57 -08:00
|
|
|
return false;
|
|
|
|
#endif // MOZ_USING_ANDROID_JAVA_WIDGETS
|
2013-01-15 07:19:28 -08:00
|
|
|
|
|
|
|
// Currently, we don't have any other targets, but if/when we do,
|
|
|
|
// decide how to handle them here.
|
|
|
|
|
|
|
|
NS_NOTREACHED("oops, what platform is this?");
|
|
|
|
return gfxPlatform::FontHintingEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
gfxAndroidPlatform::RequiresLinearZoom()
|
|
|
|
{
|
|
|
|
#ifdef MOZ_USING_ANDROID_JAVA_WIDGETS
|
|
|
|
// On android-java, we currently only use gecko to render web
|
|
|
|
// content that can always be be non-reflow-zoomed.
|
|
|
|
//
|
|
|
|
// XXX when gecko-android-java is used as an "app runtime", we may
|
|
|
|
// want to treat it like B2G and use linear zoom only for the web
|
|
|
|
// browser process, not other apps.
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MOZ_B2G
|
|
|
|
// On B2G, we need linear zoom for the browser, but otherwise prefer
|
|
|
|
// the improved glyph spacing that results from respecting the device
|
|
|
|
// pixel resolution for glyph layout (see bug 816614).
|
|
|
|
return XRE_GetProcessType() == GeckoProcessType_Content &&
|
|
|
|
ContentChild::GetSingleton()->IsForBrowser();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_NOTREACHED("oops, what platform is this?");
|
|
|
|
return gfxPlatform::RequiresLinearZoom();
|
2012-02-08 14:52:57 -08:00
|
|
|
}
|
2012-05-15 12:41:20 -07:00
|
|
|
|
|
|
|
int
|
|
|
|
gfxAndroidPlatform::GetScreenDepth() const
|
|
|
|
{
|
|
|
|
return mScreenDepth;
|
|
|
|
}
|