1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "NativeFontResourceGDI.h"
8
9 #include "Logging.h"
10 #include "mozilla/RefPtr.h"
11 #include "ScaledFontWin.h"
12
13 namespace mozilla {
14 namespace gfx {
15
16 /* static */
17 already_AddRefed<NativeFontResourceGDI>
Create(uint8_t * aFontData,uint32_t aDataLength,bool aNeedsCairo)18 NativeFontResourceGDI::Create(uint8_t *aFontData, uint32_t aDataLength,
19 bool aNeedsCairo)
20 {
21 DWORD numberOfFontsAdded;
22 HANDLE fontResourceHandle = ::AddFontMemResourceEx(aFontData, aDataLength,
23 0, &numberOfFontsAdded);
24 if (!fontResourceHandle) {
25 gfxWarning() << "Failed to add memory font resource.";
26 return nullptr;
27 }
28
29 RefPtr<NativeFontResourceGDI> fontResouce =
30 new NativeFontResourceGDI(fontResourceHandle, aNeedsCairo);
31
32 return fontResouce.forget();
33 }
34
~NativeFontResourceGDI()35 NativeFontResourceGDI::~NativeFontResourceGDI()
36 {
37 ::RemoveFontMemResourceEx(mFontResourceHandle);
38 }
39
40 already_AddRefed<ScaledFont>
CreateScaledFont(uint32_t aIndex,Float aGlyphSize,const uint8_t * aInstanceData,uint32_t aInstanceDataLength)41 NativeFontResourceGDI::CreateScaledFont(uint32_t aIndex, Float aGlyphSize,
42 const uint8_t* aInstanceData, uint32_t aInstanceDataLength)
43 {
44 if (aInstanceDataLength < sizeof(LOGFONT)) {
45 gfxWarning() << "GDI scaled font instance data is truncated.";
46 return nullptr;
47 }
48
49 const LOGFONT* logFont = reinterpret_cast<const LOGFONT*>(aInstanceData);
50
51 // Constructor for ScaledFontWin dereferences and copies the LOGFONT, so we
52 // are safe to pass this reference.
53 RefPtr<ScaledFontBase> scaledFont = new ScaledFontWin(logFont, aGlyphSize);
54 if (mNeedsCairo && !scaledFont->PopulateCairoScaledFont()) {
55 gfxWarning() << "Unable to create cairo scaled font GDI font.";
56 return nullptr;
57 }
58
59 return scaledFont.forget();
60 }
61
62 } // gfx
63 } // mozilla
64