Bug 1866777 - Disable test_race_cache_with_network.js on windows opt for frequent...
[gecko.git] / gfx / 2d / ScaledFontWin.cpp
blob5a90e5ad018a03bebcc76d972845d8400906f17a
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/. */
7 #include "ScaledFontWin.h"
8 #include "UnscaledFontGDI.h"
10 #include "AutoHelpersWin.h"
11 #include "Logging.h"
12 #include "nsString.h"
14 #include "skia/include/ports/SkTypeface_win.h"
16 #include "cairo-win32.h"
18 #include "HelpersWinFonts.h"
20 namespace mozilla {
21 namespace gfx {
23 ScaledFontWin::ScaledFontWin(const LOGFONT* aFont,
24 const RefPtr<UnscaledFont>& aUnscaledFont,
25 Float aSize)
26 : ScaledFontBase(aUnscaledFont, aSize), mLogFont(*aFont) {}
28 bool UnscaledFontGDI::GetFontFileData(FontFileDataOutput aDataCallback,
29 void* aBaton) {
30 AutoDC dc;
31 AutoSelectFont font(dc.GetDC(), &mLogFont);
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;
45 UniquePtr<uint8_t[]> fontData(new uint8_t[tableSize]);
47 uint32_t sizeGot =
48 ::GetFontData(dc.GetDC(), table, 0, fontData.get(), tableSize);
49 if (sizeGot != tableSize) {
50 return false;
53 aDataCallback(fontData.get(), tableSize, 0, aBaton);
54 return true;
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;
64 bool UnscaledFontGDI::GetFontInstanceData(FontInstanceDataOutput aCb,
65 void* aBaton) {
66 aCb(reinterpret_cast<uint8_t*>(&mLogFont), sizeof(mLogFont), aBaton);
67 return true;
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;
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;
87 const LOGFONT* logFont = reinterpret_cast<const LOGFONT*>(aData);
88 RefPtr<UnscaledFont> unscaledFont = new UnscaledFontGDI(*logFont);
89 return unscaledFont.forget();
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;
100 return MakeAndAddRef<ScaledFontWin>(
101 reinterpret_cast<const LOGFONT*>(aInstanceData), this, aGlyphSize);
104 AntialiasMode ScaledFontWin::GetDefaultAAMode() {
105 return GetSystemDefaultAAMode();
108 SkTypeface* ScaledFontWin::CreateSkTypeface() {
109 return SkCreateTypefaceFromLOGFONT(mLogFont);
112 cairo_font_face_t* ScaledFontWin::CreateCairoFontFace(
113 cairo_font_options_t* aFontOptions) {
114 if (mLogFont.lfFaceName[0] == 0) {
115 return nullptr;
117 return cairo_win32_font_face_create_for_logfontw(&mLogFont);
120 } // namespace gfx
121 } // namespace mozilla