tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

NativeFontResourceGDI.cpp (1677B)


      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 #include "UnscaledFontGDI.h"
     13 
     14 namespace mozilla {
     15 namespace gfx {
     16 
     17 /* static */
     18 already_AddRefed<NativeFontResourceGDI> NativeFontResourceGDI::Create(
     19    const uint8_t* aFontData, uint32_t aDataLength) {
     20  DWORD numberOfFontsAdded;
     21  HANDLE fontResourceHandle = ::AddFontMemResourceEx(
     22      (PVOID)aFontData, aDataLength, 0, &numberOfFontsAdded);
     23  if (!fontResourceHandle) {
     24    gfxWarning() << "Failed to add memory font resource.";
     25    return nullptr;
     26  }
     27 
     28  RefPtr<NativeFontResourceGDI> fontResouce =
     29      new NativeFontResourceGDI(fontResourceHandle, aDataLength);
     30 
     31  return fontResouce.forget();
     32 }
     33 
     34 NativeFontResourceGDI::~NativeFontResourceGDI() {
     35  ::RemoveFontMemResourceEx(mFontResourceHandle);
     36 }
     37 
     38 already_AddRefed<UnscaledFont> NativeFontResourceGDI::CreateUnscaledFont(
     39    uint32_t aIndex, const uint8_t* aInstanceData,
     40    uint32_t aInstanceDataLength) {
     41  if (aInstanceDataLength < sizeof(LOGFONT)) {
     42    gfxWarning() << "GDI unscaled font instance data is truncated.";
     43    return nullptr;
     44  }
     45 
     46  const LOGFONT* logFont = reinterpret_cast<const LOGFONT*>(aInstanceData);
     47  RefPtr<UnscaledFont> unscaledFont = new UnscaledFontGDI(*logFont);
     48  return unscaledFont.forget();
     49 }
     50 
     51 }  // namespace gfx
     52 }  // namespace mozilla