ScaledFontWin.cpp (3960B)
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 "ScaledFontWin.h" 8 #include "UnscaledFontGDI.h" 9 10 #include "AutoHelpersWin.h" 11 #include "Logging.h" 12 #include "nsString.h" 13 14 #include "skia/include/ports/SkTypeface_win.h" 15 16 #include "cairo-win32.h" 17 18 #include "HelpersWinFonts.h" 19 20 namespace mozilla { 21 namespace gfx { 22 23 ScaledFontWin::ScaledFontWin(const LOGFONT* aFont, 24 const RefPtr<UnscaledFont>& aUnscaledFont, 25 Float aSize) 26 : ScaledFontBase(aUnscaledFont, aSize), mLogFont(*aFont) {} 27 28 bool UnscaledFontGDI::GetFontFileData(FontFileDataOutput aDataCallback, 29 void* aBaton) { 30 AutoDC dc; 31 AutoSelectFont font(dc.GetDC(), &mLogFont); 32 33 // Check for a font collection first. 34 uint32_t table = 0x66637474; // 'ttcf' 35 uint32_t tableSize = ::GetFontData(dc.GetDC(), table, 0, nullptr, 0); 36 if (tableSize == GDI_ERROR) { 37 // Try as if just a single font. 38 table = 0; 39 tableSize = ::GetFontData(dc.GetDC(), table, 0, nullptr, 0); 40 if (tableSize == GDI_ERROR) { 41 return false; 42 } 43 } 44 45 UniquePtr<uint8_t[]> fontData(new uint8_t[tableSize]); 46 47 uint32_t sizeGot = 48 ::GetFontData(dc.GetDC(), table, 0, fontData.get(), tableSize); 49 if (sizeGot != tableSize) { 50 return false; 51 } 52 53 aDataCallback(fontData.get(), tableSize, 0, aBaton); 54 return true; 55 } 56 57 bool ScaledFontWin::GetFontInstanceData(FontInstanceDataOutput aCb, 58 void* aBaton) { 59 aCb(reinterpret_cast<uint8_t*>(&mLogFont), sizeof(mLogFont), nullptr, 0, 60 aBaton); 61 return true; 62 } 63 64 bool UnscaledFontGDI::GetFontInstanceData(FontInstanceDataOutput aCb, 65 void* aBaton) { 66 aCb(reinterpret_cast<uint8_t*>(&mLogFont), sizeof(mLogFont), aBaton); 67 return true; 68 } 69 70 bool UnscaledFontGDI::GetFontDescriptor(FontDescriptorOutput aCb, 71 void* aBaton) { 72 // Because all the callers of this function are preparing a recorded 73 // event to be played back in another process, it's not helpful to ever 74 // return a font descriptor, since it isn't meaningful in another 75 // process. Those callers will always need to send full font data, and 76 // returning false here will ensure that that happens. 77 return false; 78 } 79 80 already_AddRefed<UnscaledFont> UnscaledFontGDI::CreateFromFontDescriptor( 81 const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex) { 82 if (aDataLength < sizeof(LOGFONT)) { 83 gfxWarning() << "GDI font descriptor is truncated."; 84 return nullptr; 85 } 86 87 const LOGFONT* logFont = reinterpret_cast<const LOGFONT*>(aData); 88 RefPtr<UnscaledFont> unscaledFont = new UnscaledFontGDI(*logFont); 89 return unscaledFont.forget(); 90 } 91 92 already_AddRefed<ScaledFont> UnscaledFontGDI::CreateScaledFont( 93 Float aGlyphSize, const uint8_t* aInstanceData, 94 uint32_t aInstanceDataLength, const FontVariation* aVariations, 95 uint32_t aNumVariations) { 96 if (aInstanceDataLength < sizeof(LOGFONT)) { 97 gfxWarning() << "GDI unscaled font instance data is truncated."; 98 return nullptr; 99 } 100 return MakeAndAddRef<ScaledFontWin>( 101 reinterpret_cast<const LOGFONT*>(aInstanceData), this, aGlyphSize); 102 } 103 104 AntialiasMode ScaledFontWin::GetDefaultAAMode() { 105 return GetSystemDefaultAAMode(); 106 } 107 108 SkTypeface* ScaledFontWin::CreateSkTypeface() { 109 return SkCreateTypefaceFromLOGFONT(mLogFont).release(); 110 } 111 112 cairo_font_face_t* ScaledFontWin::CreateCairoFontFace( 113 cairo_font_options_t* aFontOptions) { 114 if (mLogFont.lfFaceName[0] == 0) { 115 return nullptr; 116 } 117 return cairo_win32_font_face_create_for_logfontw(&mLogFont); 118 } 119 120 } // namespace gfx 121 } // namespace mozilla