本文整理汇总了C++中IDWriteFont类的典型用法代码示例。如果您正苦于以下问题:C++ IDWriteFont类的具体用法?C++ IDWriteFont怎么用?C++ IDWriteFont使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IDWriteFont类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetBestFontAttributes
ULONG GetBestFontAttributes(IDWriteFontCollection* fontCollection, const WCHAR* fontFamilyName, const FontFaceInfo& desiredAttributes)
{
DWRITE_FONT_WEIGHT fontWeight = desiredAttributes.fontWeight;
DWRITE_FONT_STYLE fontStyle = desiredAttributes.fontStyle;
DWRITE_FONT_STRETCH fontStretch = desiredAttributes.fontStretch;
IDWriteFontFamily* fontFamily = NULL;
IDWriteFont* font = NULL;
if (SUCCEEDED(GetFontFamily(fontCollection, fontFamilyName, &fontFamily)))
{
if (SUCCEEDED(fontFamily->GetFirstMatchingFont(fontWeight, fontStretch, fontStyle, &font)))
{
fontWeight = font->GetWeight();
fontStyle = font->GetStyle();
fontStretch = font->GetStretch();
}
}
SafeRelease(&font);
SafeRelease(&fontFamily);
FontFaceInfo fontFaceInfo(L"", fontWeight, fontStyle, fontStretch);
return fontFaceInfo.PackedFontAttributes();
}
示例2: getFontHeightToEmSizeFactor
const float getFontHeightToEmSizeFactor(Font& font, IDWriteFontCollection& dwFontCollection)
{
// To set the font size factor, we need to get the font metrics
BOOL fontFound;
uint32 fontIndex;
// Search for the font in the font collection using the font name
HRESULT hr = dwFontCollection.FindFamilyName (font.getTypefaceName().toWideCharPointer(),
&fontIndex, &fontFound);
if (! fontFound)
fontIndex = 0;
IDWriteFontFamily* dwFontFamily = nullptr;
hr = dwFontCollection.GetFontFamily (fontIndex, &dwFontFamily);
IDWriteFont* dwFont = nullptr;
hr = dwFontFamily->GetFirstMatchingFont (DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
DWRITE_FONT_STYLE_NORMAL, &dwFont);
IDWriteFontFace* dwFontFace = nullptr;
hr = dwFont->CreateFontFace (&dwFontFace);
// Font metrics are in font design units
DWRITE_FONT_METRICS dwFontMetrics;
dwFontFace->GetMetrics (&dwFontMetrics);
const float totalHeight = std::abs ((float) dwFontMetrics.ascent) + std::abs ((float) dwFontMetrics.descent);
const float fontHeightToEmSizeFactor = (float) dwFontMetrics.designUnitsPerEm / totalHeight;
safeRelease (&dwFontFace);
safeRelease (&dwFont);
safeRelease (&dwFontFamily);
return fontHeightToEmSizeFactor;
}
示例3: lengthof
bool CDirectWriteRenderer::GetFontMetrics(CDirectWriteFont &Font, FontMetrics *pMetrics)
{
if (pMetrics == nullptr)
return false;
if (m_pRenderTarget == nullptr)
return false;
HRESULT hr = E_UNEXPECTED;
IDWriteTextFormat *pTextFormat = Font.GetTextFormat();
if (pTextFormat != nullptr) {
IDWriteFontCollection *pFontCollection;
hr = pTextFormat->GetFontCollection(&pFontCollection);
if (SUCCEEDED(hr)) {
WCHAR szName[256];
hr = pTextFormat->GetFontFamilyName(szName, lengthof(szName));
if (SUCCEEDED(hr)) {
UINT32 Index;
BOOL fExists;
hr = pFontCollection->FindFamilyName(szName, &Index, &fExists);
if (SUCCEEDED(hr)) {
IDWriteFontFamily *pFontFamily;
hr = pFontCollection->GetFontFamily(Index, &pFontFamily);
if (SUCCEEDED(hr)) {
IDWriteFont *pFont;
hr = pFontFamily->GetFirstMatchingFont(
pTextFormat->GetFontWeight(),
pTextFormat->GetFontStretch(),
pTextFormat->GetFontStyle(),
&pFont);
if (SUCCEEDED(hr)) {
DWRITE_FONT_METRICS Metrics;
pFont->GetMetrics(&Metrics);
const float Ratio = pTextFormat->GetFontSize() / static_cast<float>(Metrics.designUnitsPerEm);
pMetrics->Ascent = Metrics.ascent * Ratio;
pMetrics->Descent = Metrics.descent * Ratio;
pMetrics->LineGap = Metrics.lineGap * Ratio;
pFont->Release();
}
pFontFamily->Release();
}
}
}
}
pFontCollection->Release();
}
return SUCCEEDED(hr);
}
示例4: CreateDWFontFromGDIFamilyName
bool TextFormatD2D::GetDWPropertiesFromGDIProperties(
const WCHAR* gdiFamilyName, const bool gdiBold, const bool gdiItalic,
DWRITE_FONT_WEIGHT& dwFontWeight, DWRITE_FONT_STYLE& dwFontStyle,
DWRITE_FONT_STRETCH& dwFontStretch, WCHAR* dwFamilyName, UINT dwFamilyNameSize)
{
bool result = false;
IDWriteFont* dwFont = CreateDWFontFromGDIFamilyName(gdiFamilyName);
if (dwFont)
{
if (GetFamilyNameFromDWFont(dwFont, dwFamilyName, dwFamilyNameSize))
{
dwFontWeight = dwFont->GetWeight();
if (gdiBold)
{
if (dwFontWeight == DWRITE_FONT_WEIGHT_NORMAL)
{
dwFontWeight = DWRITE_FONT_WEIGHT_BOLD;
}
else if (dwFontWeight < DWRITE_FONT_WEIGHT_ULTRA_BOLD)
{
// If 'gdiFamilyName' was e.g. 'Segoe UI Light', |dwFontWeight| wil be equal to
// DWRITE_FONT_WEIGHT_LIGHT. If |gdiBold| is true in that case, we need to
// increase the weight a little more for similar results with GDI+.
// TODO: Is +100 enough?
dwFontWeight = (DWRITE_FONT_WEIGHT)(dwFontWeight + 100);
}
}
dwFontStyle = dwFont->GetStyle();
if (gdiItalic && dwFontStyle == DWRITE_FONT_STYLE_NORMAL)
{
dwFontStyle = DWRITE_FONT_STYLE_ITALIC;
}
dwFontStretch = dwFont->GetStretch();
result = true;
}
dwFont->Release();
}
return result;
}
示例5: styleChanged
static void styleChanged(struct fontDialog *f)
{
LRESULT pos;
BOOL selected;
IDWriteFont *font;
selected = cbGetCurSel(f->styleCombobox, &pos);
if (!selected) // on deselect, do nothing
return;
f->curStyle = pos;
font = (IDWriteFont *) cbGetItemData(f->styleCombobox, (WPARAM) (f->curStyle));
// these are for the nearest match when changing the family; see below
f->weight = font->GetWeight();
f->style = font->GetStyle();
f->stretch = font->GetStretch();
queueRedrawSampleText(f);
}
示例6: GetDWritePropertiesFromGDIProperties
HRESULT GetDWritePropertiesFromGDIProperties(
IDWriteFactory* factory, const WCHAR* gdiFamilyName, const bool gdiBold, const bool gdiItalic,
DWRITE_FONT_WEIGHT& dwriteFontWeight, DWRITE_FONT_STYLE& dwriteFontStyle,
DWRITE_FONT_STRETCH& dwriteFontStretch, WCHAR* dwriteFamilyName, UINT dwriteFamilyNameSize)
{
HRESULT hr = E_FAIL;
IDWriteFont* dwriteFont = CreateDWriteFontFromGDIFamilyName(factory, gdiFamilyName);
if (dwriteFont)
{
hr = GetFamilyNameFromDWriteFont(dwriteFont, dwriteFamilyName, dwriteFamilyNameSize);
if (SUCCEEDED(hr))
{
GetPropertiesFromDWriteFont(
dwriteFont, gdiBold, gdiItalic, &dwriteFontWeight, &dwriteFontStyle, &dwriteFontStretch);
}
dwriteFont->Release();
}
return hr;
}
示例7: FindDWriteFontInFontFamilyByGDIFamilyName
IDWriteFont* FindDWriteFontInFontFamilyByGDIFamilyName(
IDWriteFontFamily* fontFamily, const WCHAR* gdiFamilyName)
{
const UINT32 fontFamilyFontCount = fontFamily->GetFontCount();
for (UINT32 j = 0; j < fontFamilyFontCount; ++j)
{
IDWriteFont* font;
HRESULT hr = fontFamily->GetFont(j, &font);
if (SUCCEEDED(hr))
{
WCHAR buffer[LF_FACESIZE];
hr = GetGDIFamilyNameFromDWriteFont(font, buffer, _countof(buffer));
if (SUCCEEDED(hr) && _wcsicmp(gdiFamilyName, buffer) == 0)
{
return font;
}
font->Release();
}
}
return nullptr;
}
示例8: getFontHeightToEmSizeFactor
//==================================================================================================
static float getFontHeightToEmSizeFactor (IDWriteFont& dwFont)
{
ComSmartPtr<IDWriteFontFace> dwFontFace;
dwFont.CreateFontFace (dwFontFace.resetAndGetPointerAddress());
if (dwFontFace == nullptr)
return 1.0f;
DWRITE_FONT_METRICS dwFontMetrics;
dwFontFace->GetMetrics (&dwFontMetrics);
const float totalHeight = (float) (std::abs (dwFontMetrics.ascent) + std::abs (dwFontMetrics.descent));
return dwFontMetrics.designUnitsPerEm / totalHeight;
}
示例9: memset
static QFontEngine *loadEngine(int script, const QFontDef &request,
HDC fontHdc, int dpi, bool rawMode,
const QtFontDesc *desc,
const QStringList &family_list)
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
bool useDevice = (request.styleStrategy & QFont::PreferDevice) && fontHdc;
HDC hdc = shared_dc();
QString font_name = desc != 0 ? desc->family->name : request.family;
if (useDevice) {
hdc = fontHdc;
font_name = request.family;
}
bool stockFont = false;
bool preferClearTypeAA = false;
HFONT hfont = 0;
#if !defined(QT_NO_DIRECTWRITE)
bool useDirectWrite = (request.hintingPreference == QFont::PreferNoHinting)
|| (request.hintingPreference == QFont::PreferVerticalHinting);
IDWriteFont *directWriteFont = 0;
#else
bool useDirectWrite = false;
#endif
if (rawMode) { // will choose a stock font
int f, deffnt = SYSTEM_FONT;
QString fam = desc != 0 ? desc->family->name.toLower() : request.family.toLower();
if (fam == QLatin1String("default"))
f = deffnt;
else if (fam == QLatin1String("system"))
f = SYSTEM_FONT;
#ifndef Q_WS_WINCE
else if (fam == QLatin1String("system_fixed"))
f = SYSTEM_FIXED_FONT;
else if (fam == QLatin1String("ansi_fixed"))
f = ANSI_FIXED_FONT;
else if (fam == QLatin1String("ansi_var"))
f = ANSI_VAR_FONT;
else if (fam == QLatin1String("device_default"))
f = DEVICE_DEFAULT_FONT;
else if (fam == QLatin1String("oem_fixed"))
f = OEM_FIXED_FONT;
#endif
else if (fam[0] == QLatin1Char('#'))
f = fam.right(fam.length()-1).toInt();
else
f = deffnt;
hfont = (HFONT)GetStockObject(f);
if (!hfont) {
qErrnoWarning("QFontEngine::loadEngine: GetStockObject failed");
hfont = systemFont();
}
stockFont = true;
} else {
int hint = FF_DONTCARE;
switch (request.styleHint) {
case QFont::Helvetica:
hint = FF_SWISS;
break;
case QFont::Times:
hint = FF_ROMAN;
break;
case QFont::Courier:
hint = FF_MODERN;
break;
case QFont::OldEnglish:
hint = FF_DECORATIVE;
break;
case QFont::System:
hint = FF_MODERN;
break;
default:
break;
}
lf.lfHeight = -qRound(request.pixelSize);
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
if (desc == 0 || desc->style->key.weight == 50)
lf.lfWeight = FW_DONTCARE;
else
lf.lfWeight = (desc->style->key.weight*900)/99;
lf.lfItalic = (desc != 0 && desc->style->key.style != QFont::StyleNormal);
lf.lfCharSet = DEFAULT_CHARSET;
int strat = OUT_DEFAULT_PRECIS;
if (request.styleStrategy & QFont::PreferBitmap) {
strat = OUT_RASTER_PRECIS;
#ifndef Q_WS_WINCE
} else if (request.styleStrategy & QFont::PreferDevice) {
//.........这里部分代码省略.........
示例10: DWriteCreateResources
HRESULT DWriteCreateResources(HDC hdc, wchar_t *text, HFONT hfont)
{
HRESULT hr = S_OK;
// If the DirectWrite factory doesn't exist, create the resources,
// only create these resources once.
if (!g_pDWriteFactory)
{
HWND hwnd;
RECT r;
// DirectWrite variables.
IDWriteFontFamily* pFontFamily = NULL;
IDWriteFont* pFont = NULL;
IDWriteLocalizedStrings* pFamilyNames = NULL;
// Logical (GDI) font.
LOGFONT lf = {};
UINT32 length = 0;
UINT32 index = 0;
float fontSize = 0;
// length of the string
UINT32 textLength = 0;
wchar_t *name = NULL;
// Get a handle to the DC and the window rect.
hwnd = WindowFromDC(hdc);
GetClientRect(hwnd, &r);
// Calculate the string length.
textLength = UINT32(wcslen(text));
// Create the DirectWrite factory.
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&g_pDWriteFactory)
);
// Create a GDI interop interface.
if (SUCCEEDED(hr))
{
hr = g_pDWriteFactory->GetGdiInterop(&g_pGdiInterop);
}
if (SUCCEEDED(hr))
{
// Get a logical font from the font handle.
GetObject(hfont, sizeof(LOGFONT), &lf);
}
// Convert to a DirectWrite font.
if (SUCCEEDED(hr))
{
hr = g_pGdiInterop->CreateFontFromLOGFONT(&lf, &pFont);
}
// Get the font family.
if (SUCCEEDED(hr))
{
hr = pFont->GetFontFamily(&pFontFamily);
}
// Get a list of localized family names.
if (SUCCEEDED(hr))
{
hr = pFontFamily->GetFamilyNames(&pFamilyNames);
}
// Select the first locale. This is OK, because we are not displaying the family name.
index = 0;
// Get the length of the family name.
if (SUCCEEDED(hr))
{
hr = pFamilyNames->GetStringLength(index, &length);
}
if (SUCCEEDED(hr))
{
// Allocate a new string.
name = new (std::nothrow) wchar_t[length+1];
if (name == NULL)
{
hr = E_OUTOFMEMORY;
}
}
// Get the actual family name.
if (SUCCEEDED(hr))
{
hr = pFamilyNames->GetString(index, name, length+1);
}
if (SUCCEEDED(hr))
{
// Calculate the font size.
//.........这里部分代码省略.........
示例11: PixelsToDipsY
HRESULT
DWriteContext::SetLOGFONT(const LOGFONTW &logFont, float fontSize)
{
// Most of this function is copy from: http://msdn.microsoft.com/en-us/library/windows/desktop/dd941783(v=vs.85).aspx
HRESULT hr = S_OK;
IDWriteFont *font = NULL;
IDWriteFontFamily *fontFamily = NULL;
IDWriteLocalizedStrings *localizedFamilyNames = NULL;
if (SUCCEEDED(hr))
{
hr = mGdiInterop->CreateFontFromLOGFONT(&logFont, &font);
}
// Get the font family to which this font belongs.
if (SUCCEEDED(hr))
{
hr = font->GetFontFamily(&fontFamily);
}
// Get the family names. This returns an object that encapsulates one or
// more names with the same meaning but in different languages.
if (SUCCEEDED(hr))
{
hr = fontFamily->GetFamilyNames(&localizedFamilyNames);
}
// Get the family name at index zero. If we were going to display the name
// we'd want to try to find one that matched the use locale, but for
// purposes of creating a text format object any language will do.
wchar_t familyName[100];
if (SUCCEEDED(hr))
{
hr = localizedFamilyNames->GetString(0, familyName,
ARRAYSIZE(familyName));
}
if (SUCCEEDED(hr))
{
// If no font size was passed in use the lfHeight of the LOGFONT.
if (fontSize == 0)
{
// Convert from pixels to DIPs.
fontSize = PixelsToDipsY(logFont.lfHeight);
if (fontSize < 0)
{
// Negative lfHeight represents the size of the em unit.
fontSize = -fontSize;
}
else
{
// Positive lfHeight represents the cell height (ascent +
// descent).
DWRITE_FONT_METRICS fontMetrics;
font->GetMetrics(&fontMetrics);
// Convert the cell height (ascent + descent) from design units
// to ems.
float cellHeight = static_cast<float>(
fontMetrics.ascent + fontMetrics.descent)
/ fontMetrics.designUnitsPerEm;
// Divide the font size by the cell height to get the font em
// size.
fontSize /= cellHeight;
}
}
}
// The text format includes a locale name. Ideally, this would be the
// language of the text, which may or may not be the same as the primary
// language of the user. However, for our purposes the user locale will do.
wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
if (SUCCEEDED(hr))
{
if (GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) == 0)
hr = HRESULT_FROM_WIN32(GetLastError());
}
if (SUCCEEDED(hr))
{
// Create the text format object.
hr = mDWriteFactory->CreateTextFormat(
familyName,
NULL, // no custom font collection
font->GetWeight(),
font->GetStyle(),
font->GetStretch(),
fontSize,
localeName,
&mTextFormat);
}
if (SUCCEEDED(hr))
{
mFontWeight = static_cast<DWRITE_FONT_WEIGHT>(logFont.lfWeight);
mFontStyle = logFont.lfItalic ? DWRITE_FONT_STYLE_ITALIC
: DWRITE_FONT_STYLE_NORMAL;
//.........这里部分代码省略.........
示例12: Dispose
void TextFormatD2D::SetProperties(
const WCHAR* fontFamily, int size, bool bold, bool italic,
const FontCollection* fontCollection)
{
auto fontCollectionD2D = (FontCollectionD2D*)fontCollection;
Dispose();
WCHAR dwriteFamilyName[LF_FACESIZE];
DWRITE_FONT_WEIGHT dwriteFontWeight =
bold ? DWRITE_FONT_WEIGHT_BOLD : DWRITE_FONT_WEIGHT_REGULAR;
DWRITE_FONT_STYLE dwriteFontStyle =
italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
DWRITE_FONT_STRETCH dwriteFontStretch = DWRITE_FONT_STRETCH_NORMAL;
const float dwriteFontSize = size * (4.0f / 3.0f);
// |fontFamily| uses the GDI/GDI+ font naming convention so try to create DirectWrite font
// using the GDI family name and then create a text format using the DirectWrite family name
// obtained from it.
HRESULT hr = Util::GetDWritePropertiesFromGDIProperties(
CanvasD2D::c_DWFactory.Get(), fontFamily, bold, italic, dwriteFontWeight, dwriteFontStyle,
dwriteFontStretch, dwriteFamilyName, _countof(dwriteFamilyName));
if (SUCCEEDED(hr))
{
hr = CanvasD2D::c_DWFactory->CreateTextFormat(
dwriteFamilyName,
nullptr,
dwriteFontWeight,
dwriteFontStyle,
dwriteFontStretch,
dwriteFontSize,
L"",
&m_TextFormat);
}
if (FAILED(hr))
{
IDWriteFontCollection* dwriteFontCollection = nullptr;
// If |fontFamily| is not in the system collection, use the font collection from
// |fontCollectionD2D| if possible.
if (!Util::IsFamilyInSystemFontCollection(CanvasD2D::c_DWFactory.Get(), fontFamily) &&
(fontCollectionD2D && fontCollectionD2D->InitializeCollection()))
{
IDWriteFont* dwriteFont = Util::FindDWriteFontInFontCollectionByGDIFamilyName(
fontCollectionD2D->m_Collection, fontFamily);
if (dwriteFont)
{
hr = Util::GetFamilyNameFromDWriteFont(
dwriteFont, dwriteFamilyName, _countof(dwriteFamilyName));
if (SUCCEEDED(hr))
{
fontFamily = dwriteFamilyName;
Util::GetPropertiesFromDWriteFont(
dwriteFont, bold, italic, &dwriteFontWeight, &dwriteFontStyle,
&dwriteFontStretch);
}
dwriteFont->Release();
}
dwriteFontCollection = fontCollectionD2D->m_Collection;
}
// Fallback in case above fails.
hr = CanvasD2D::c_DWFactory->CreateTextFormat(
fontFamily,
dwriteFontCollection,
dwriteFontWeight,
dwriteFontStyle,
dwriteFontStretch,
dwriteFontSize,
L"",
&m_TextFormat);
}
if (SUCCEEDED(hr))
{
SetHorizontalAlignment(GetHorizontalAlignment());
SetVerticalAlignment(GetVerticalAlignment());
// Get the family name to in case CreateTextFormat() fallbacked on some other family name.
hr = m_TextFormat->GetFontFamilyName(dwriteFamilyName, _countof(dwriteFamilyName));
if (FAILED(hr)) return;
Microsoft::WRL::ComPtr<IDWriteFontCollection> collection;
Microsoft::WRL::ComPtr<IDWriteFontFamily> fontFamily;
UINT32 familyNameIndex;
BOOL exists;
if (FAILED(m_TextFormat->GetFontCollection(collection.GetAddressOf())) ||
FAILED(collection->FindFamilyName(dwriteFamilyName, &familyNameIndex, &exists)) ||
FAILED(collection->GetFontFamily(familyNameIndex, fontFamily.GetAddressOf())))
{
return;
}
Microsoft::WRL::ComPtr<IDWriteFont> font;
hr = fontFamily->GetFirstMatchingFont(
m_TextFormat->GetFontWeight(),
m_TextFormat->GetFontStretch(),
//.........这里部分代码省略.........
示例13: fontDialogDrawSampleText
static void fontDialogDrawSampleText(struct fontDialog *f, ID2D1RenderTarget *rt)
{
D2D1_COLOR_F color;
D2D1_BRUSH_PROPERTIES props;
ID2D1SolidColorBrush *black;
IDWriteFont *font;
IDWriteLocalizedStrings *sampleStrings;
BOOL exists;
WCHAR *sample;
WCHAR *family;
IDWriteTextFormat *format;
D2D1_RECT_F rect;
HRESULT hr;
color.r = 0.0;
color.g = 0.0;
color.b = 0.0;
color.a = 1.0;
ZeroMemory(&props, sizeof (D2D1_BRUSH_PROPERTIES));
props.opacity = 1.0;
// identity matrix
props.transform._11 = 1;
props.transform._22 = 1;
hr = rt->CreateSolidColorBrush(
&color,
&props,
&black);
if (hr != S_OK)
logHRESULT(L"error creating solid brush", hr);
font = (IDWriteFont *) cbGetItemData(f->styleCombobox, (WPARAM) f->curStyle);
hr = font->GetInformationalStrings(DWRITE_INFORMATIONAL_STRING_SAMPLE_TEXT, &sampleStrings, &exists);
if (hr != S_OK)
exists = FALSE;
if (exists) {
sample = fontCollectionCorrectString(f->fc, sampleStrings);
sampleStrings->Release();
} else
sample = L"The quick brown fox jumps over the lazy dog.";
// DirectWrite doesn't allow creating a text format from a font; we need to get this ourselves
family = cbGetItemText(f->familyCombobox, f->curFamily);
hr = dwfactory->CreateTextFormat(family,
NULL,
font->GetWeight(),
font->GetStyle(),
font->GetStretch(),
// typographic points are 1/72 inch; this parameter is 1/96 inch
// fortunately Microsoft does this too, in https://msdn.microsoft.com/en-us/library/windows/desktop/dd371554%28v=vs.85%29.aspx
f->curSize * (96.0 / 72.0),
// see http://stackoverflow.com/questions/28397971/idwritefactorycreatetextformat-failing and https://msdn.microsoft.com/en-us/library/windows/desktop/dd368203.aspx
// TODO use the current locale again?
L"",
&format);
if (hr != S_OK)
logHRESULT(L"error creating IDWriteTextFormat", hr);
uiFree(family);
rect.left = 0;
rect.top = 0;
rect.right = realGetSize(rt).width;
rect.bottom = realGetSize(rt).height;
rt->DrawText(sample, wcslen(sample),
format,
&rect,
black,
// TODO really?
D2D1_DRAW_TEXT_OPTIONS_NONE,
DWRITE_MEASURING_MODE_NATURAL);
format->Release();
if (exists)
uiFree(sample);
black->Release();
}
示例14: main
int main(void)
{
int argc;
LPWSTR *argv;
WCHAR *fontname, *string;
int len;
HDC dc;
HFONT font, prevfont;
IDWriteFontCollection *sysfc;
UINT32 index;
BOOL exists;
IDWriteFontFamily *dwfamily;
IDWriteFont *dwfont;
IDWriteFontFace *dwface;
featurePreparer *features;
HRESULT hr;
// TODO would using wmain() be adequate?
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argv == NULL)
dieLE("error getting command-line arguments");
if (argc != 3) {
fprintf(stderr, "usage: %ws font string\n", argv[0]);
return 1;
}
fontname = argv[1];
string = argv[2];
len = wcslen(string);
// DirectWrite requires COM
hr = CoInitialize(NULL);
if (hr != S_OK)
die("error initializing COM", hr);
// Uniscribe requires a device context with the font to use
dc = GetDC(NULL);
if (dc == NULL)
dieLE("error getting screen HDC for Uniscribe");
// TODO DEFAULT_CHARSET might affect the results we get
font = CreateFontW(0, 0, 0, 0, 0, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontname);
if (font == NULL)
dieLE("error creating font for Uniscribe");
prevfont = (HFONT) SelectObject(dc, font);
if (prevfont == NULL)
dieLE("error selecting font into HDC for Uniscribe");
// and initialize DirectWrite
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
__uuidof (IDWriteFactory),
(IUnknown **) (&dwfactory));
if (hr != S_OK)
die("error initializing DirectWrite", hr);
// and load the font *there*
hr = dwfactory->GetSystemFontCollection(&sysfc, TRUE);
if (hr != S_OK)
die("error loading DirectWrite system font collection", hr);
hr = sysfc->FindFamilyName(fontname, &index, &exists);
if (hr != S_OK)
die("error finding DirectWrite font family", hr);
if (!exists)
die("font not found in DirectWrite system font collection", E_FAIL);
hr = sysfc->GetFontFamily(index, &dwfamily);
if (hr != S_OK)
die("error loading DirectWrite font family", hr);
hr = dwfamily->GetFirstMatchingFont(DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
&dwfont);
if (hr != S_OK)
die("error loading DirectWrite font object", hr);
hr = dwfont->CreateFontFace(&dwface);
if (hr != S_OK)
die("error creating DirectWrite font face", hr);
// first, uniscribe only; no features are used
// uniscribeTest(dc, string, len, NULL,
// doScriptItemize, doScriptShape, "Uniscribe");
// next, unprepared features (NULL values)
features = new featurePreparer;
//features->add('z','e','r','o',1);
//features->add('f','r','a','c',1);features->prepare(len);
// uniscribeTest(dc, string, len, features,
// doScriptItemizeOpenType, doScriptShapeOpenType, "Uniscribe OpenType");
directwriteAnalyzerTest(dwface, string, len, features);
delete features;
dwface->Release();
dwfont->Release();
dwfamily->Release();
sysfc->Release();
dwfactory->Release();
SelectObject(dc, prevfont);
DeleteObject(font);
ReleaseDC(NULL, dc);
CoUninitialize();
return 0;
}