コード例 #1
0
StringArray Font::findAllTypefaceFamilies()
{
    StringArray results;
    #if JUCE_USE_DIRECTWRITE
    const Direct2DFactories& factories = Direct2DFactories::getInstance();

    if (factories.systemFonts != nullptr)
    {
        ComSmartPtr<IDWriteFontFamily> dwFontFamily;
        uint32 fontFamilyCount = 0;
        fontFamilyCount = factories.systemFonts->GetFontFamilyCount();

        for (uint32 i = 0; i < fontFamilyCount; ++i)
        {
                HRESULT hr = factories.systemFonts->GetFontFamily (i, dwFontFamily.resetAndGetPointerAddress());

                ComSmartPtr<IDWriteLocalizedStrings> dwFamilyNames;
                hr = dwFontFamily->GetFamilyNames (dwFamilyNames.resetAndGetPointerAddress());
                jassert (dwFamilyNames != nullptr);

                uint32 index = 0;
                BOOL exists = false;
                hr = dwFamilyNames->FindLocaleName (L"en-us", &index, &exists);
                if (! exists)
                    index = 0;

                uint32 length = 0;
                hr = dwFamilyNames->GetStringLength (index, &length);

                HeapBlock <wchar_t> familyName (length + 1);
                hr = dwFamilyNames->GetString (index, familyName, length + 1);

                results.add(String (familyName));
        }
    }
    else
    #endif
    {
        HDC dc = CreateCompatibleDC (0);

        {
            LOGFONTW lf = { 0 };
            lf.lfWeight = FW_DONTCARE;
            lf.lfOutPrecision = OUT_OUTLINE_PRECIS;
            lf.lfQuality = DEFAULT_QUALITY;
            lf.lfCharSet = DEFAULT_CHARSET;
            lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
            lf.lfPitchAndFamily = FF_DONTCARE;

            EnumFontFamiliesEx (dc, &lf,
                                (FONTENUMPROCW) &FontEnumerators::fontEnum1,
                                (LPARAM) &results, 0);
        }

        DeleteDC (dc);
    }

    results.sort (true);
    return results;
}
コード例 #2
0
StringArray Font::findAllTypefaceStyles(const String& family)
{
    StringArray results;

    #if JUCE_USE_DIRECTWRITE
    const Direct2DFactories& factories = Direct2DFactories::getInstance();

    if (factories.systemFonts != nullptr)
    {
        BOOL fontFound = false;
        uint32 fontIndex = 0;
        HRESULT hr = factories.systemFonts->FindFamilyName (family.toWideCharPointer(), &fontIndex, &fontFound);
        if (! fontFound)
            fontIndex = 0;

        // Get the font family using the search results
        // Fonts like: Times New Roman, Times New Roman Bold, Times New Roman Italic are all in the same font family
        ComSmartPtr<IDWriteFontFamily> dwFontFamily;
        hr = factories.systemFonts->GetFontFamily (fontIndex, dwFontFamily.resetAndGetPointerAddress());

        // Get the font faces
        ComSmartPtr<IDWriteFont> dwFont;
        uint32 fontFacesCount = 0;
        fontFacesCount = dwFontFamily->GetFontCount();

        for (uint32 i = 0; i < fontFacesCount; ++i)
        {
                hr = dwFontFamily->GetFont (i, dwFont.resetAndGetPointerAddress());

                ComSmartPtr<IDWriteLocalizedStrings> dwFaceNames;
                hr = dwFont->GetFaceNames (dwFaceNames.resetAndGetPointerAddress());
                jassert (dwFaceNames != nullptr);

                uint32 index = 0;
                BOOL exists = false;
                hr = dwFaceNames->FindLocaleName (L"en-us", &index, &exists);
                if (! exists)
                    index = 0;

                uint32 length = 0;
                hr = dwFaceNames->GetStringLength (index, &length);

                HeapBlock <wchar_t> styleName (length + 1);
                hr = dwFaceNames->GetString (index, styleName, length + 1);

                String style (styleName);

                // For unknown reasons, DirectWrite does not enumerate the Arial Narrow fonts properly.
                // It uses the same style name "Narrow" for all four fonts. Since only one of these fonts
                // is accessible, we will ignore the duplicates.
                if (style == "Narrow" && results.contains("Narrow")) continue;
                // DirectWrite automatically adds extra bold and oblique font styles which are algorithmic
                // style simulations. These styles don't show up in any other software. We will ignore them.
                if (dwFont->GetSimulations() != DWRITE_FONT_SIMULATIONS_NONE) continue;

                results.add(style);
        }
    }
    else
    #endif
    {
        results.add ("Regular");
        results.add ("Italic");
        results.add ("Bold");
        results.add ("Bold Italic");
    }

    return results;
}