michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsTArray.h" michael@0: #include "nsString.h" michael@0: #include "nsDependentString.h" michael@0: michael@0: #include "prinrval.h" michael@0: michael@0: #include "gfxContext.h" michael@0: #include "gfxFont.h" michael@0: #include "gfxPlatform.h" michael@0: michael@0: #include "gfxFontTest.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: struct TestEntry { michael@0: const char* mFamilies; michael@0: const char* mString; michael@0: }; michael@0: michael@0: TestEntry testList[] = { michael@0: #include "per-word-runs.h" michael@0: { nullptr, nullptr } // terminator michael@0: }; michael@0: michael@0: static already_AddRefed michael@0: MakeContext () michael@0: { michael@0: const int size = 200; michael@0: michael@0: nsRefPtr surface; michael@0: michael@0: surface = gfxPlatform::GetPlatform()-> michael@0: CreateOffscreenSurface(IntSize(size, size), michael@0: gfxASurface::ContentFromFormat(gfxImageFormat::RGB24)); michael@0: nsRefPtr ctx = new gfxContext(surface); michael@0: return ctx.forget(); michael@0: } michael@0: michael@0: const char* lastFamilies = nullptr; michael@0: michael@0: static void michael@0: RunTest (TestEntry *test, gfxContext *ctx) { michael@0: nsRefPtr fontGroup; michael@0: if (!lastFamilies || strcmp(lastFamilies, test->mFamilies)) { michael@0: gfxFontStyle style_western_normal_16 (mozilla::gfx::FontStyle::NORMAL, michael@0: 400, michael@0: 0, michael@0: 16.0, michael@0: NS_NewPermanentAtom(NS_LITERAL_STRING("en")), michael@0: 0.0, michael@0: false, false, michael@0: NS_LITERAL_STRING("")); michael@0: michael@0: fontGroup = gfxPlatform::GetPlatform()->CreateFontGroup(NS_ConvertUTF8toUTF16(test->mFamilies), &style_western_normal_16, nullptr); michael@0: } michael@0: michael@0: nsAutoPtr textRun; michael@0: uint32_t i; michael@0: bool isASCII = true; michael@0: for (i = 0; test->mString[i]; ++i) { michael@0: if (test->mString[i] & 0x80) { michael@0: isASCII = false; michael@0: } michael@0: } michael@0: gfxTextRunFactory::Parameters params = { michael@0: ctx, nullptr, nullptr, nullptr, 0, 60 michael@0: }; michael@0: uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT; michael@0: uint32_t length; michael@0: gfxFontTestStore::NewStore(); michael@0: if (isASCII) { michael@0: flags |= gfxTextRunFactory::TEXT_IS_ASCII | michael@0: gfxTextRunFactory::TEXT_IS_8BIT; michael@0: length = strlen(test->mString); michael@0: textRun = fontGroup->MakeTextRun(reinterpret_cast(test->mString), length, ¶ms, flags); michael@0: } else { michael@0: NS_ConvertUTF8toUTF16 str(nsDependentCString(test->mString)); michael@0: length = str.Length(); michael@0: textRun = fontGroup->MakeTextRun(str.get(), length, ¶ms, flags); michael@0: } michael@0: michael@0: // Should we test drawing? michael@0: // textRun->Draw(ctx, gfxPoint(0,0), 0, length, nullptr, nullptr, nullptr); michael@0: michael@0: textRun->GetAdvanceWidth(0, length, nullptr); michael@0: gfxFontTestStore::DeleteStore(); michael@0: } michael@0: michael@0: uint32_t iterations = 1; michael@0: michael@0: TEST(Gfx, TextRunPref) { michael@0: nsRefPtr context = MakeContext(); michael@0: michael@0: // Start timing michael@0: PRIntervalTime start = PR_IntervalNow(); michael@0: michael@0: for (uint32_t i = 0; i < iterations; ++i) { michael@0: for (uint test = 0; michael@0: test < ArrayLength(testList) - 1; michael@0: test++) michael@0: { michael@0: RunTest(&testList[test], context); michael@0: } michael@0: } michael@0: michael@0: PRIntervalTime end = PR_IntervalNow(); michael@0: michael@0: printf("Elapsed time (ms): %d\n", PR_IntervalToMilliseconds(end - start)); michael@0: michael@0: }