将文本写入 XPS OM
本主题介绍如何将文本写入 XPS OM。
通过创建 IXpsOMGlyphs 接口并设置其格式,然后将 IXpsOMGlyphs 接口添加到页面或画布的视觉对象列表中,将文本放置到 XPS OM 中。 每个 IXpsOMGlyphs 接口表示一个字形串,这是一系列共享通用格式的连续字符。 在字符格式元素(例如字体或字号)更改或在换行时,必须创建新的 IXpsOMGlyphs 接口并将其添加到视觉对象列表中。
可以使用 IXpsOMGlyphs 接口的方法设置字形串的某些属性。 但是,某些属性与其他属性交互,并且必须使用 IXpsOMGlyphsEditor 接口进行设置。
在程序中使用以下代码示例之前,请阅读常见的 XPS 文档编程任务中的免责声明。
代码示例
根据字符串创建字形串
字形串通常分几步来创建,包括加载字形串使用的字体资源、设置填充画笔、指定字号和起始位置,以及设置 Unicode 字符串。
代码示例的以下部分包含一个例程,该例程接受某些变量,包括字号、颜色和位置以及要写入的字符。 然后,该代码将创建字形串,然后将其添加到页面。 代码示例假定已发生初始化 XPS OM 中所述的初始化,并且文档至少有一个页面。 若要详细了解如何创建空白的 XPS OM,请参阅创建空白 XPS OM。
// Write Text to an XPS OM
HRESULT
WriteText_AddTextToPage(
// A pre-created object factory
__in IXpsOMObjectFactory *xpsFactory,
// The font resource to use for this run
__in IXpsOMFontResource *xpsFont,
// The font size
__in float fontEmSize,
// The solid color brush to use for the font
__in IXpsOMSolidColorBrush *xpsBrush,
// The starting location of this glyph run
__in XPS_POINT *origin,
// The text to use for this run
__in LPCWSTR unicodeString,
// The page on which to write this glyph run
__inout IXpsOMPage *xpsPage
)
{
// The data type definitions are included in this function
// for the convenience of this code example. In an actual
// implementation they would probably belong in a header file.
HRESULT hr = S_OK;
XPS_POINT glyphsOrigin = {origin->x, origin->y};
IXpsOMGlyphsEditor *glyphsEditor = NULL;
IXpsOMGlyphs *xpsGlyphs = NULL;
IXpsOMVisualCollection *pageVisuals = NULL;
// Create a new Glyphs object and set its properties.
hr = xpsFactory->CreateGlyphs(xpsFont, &xpsGlyphs);
hr = xpsGlyphs->SetOrigin(&glyphsOrigin);
hr = xpsGlyphs->SetFontRenderingEmSize(fontEmSize);
hr = xpsGlyphs->SetFillBrushLocal(xpsBrush);
// Some properties are inter-dependent so they
// must be changed by using a GlyphsEditor.
hr = xpsGlyphs->GetGlyphsEditor(&glyphsEditor);
hr = glyphsEditor->SetUnicodeString(unicodeString);
hr = glyphsEditor->ApplyEdits();
// Add the new Glyphs object to the page
hr = xpsPage->GetVisuals(&pageVisuals);
hr = pageVisuals->Append(xpsGlyphs);
// Release interface pointers.
if (NULL != xpsGlyphs) xpsGlyphs->Release();
if (NULL != glyphsEditor) glyphsEditor->Release();
if (NULL != pageVisuals) pageVisuals->Release();
return hr;
}
加载和创建资源
要创建 IXpsOMGlyphs 接口,需要使用字体资源。 在许多情况下,文本块使用相同的字体和颜色。 因此,代码示例的此部分将创建字体资源接口,将在页面上放置文本的调用中使用这些接口。
// fontFileName is the name of the font file and it
// is defined outside of this example.
HRESULT hr = S_OK;
GUID fontNameGuid;
WCHAR guidString[128] = {0};
WCHAR uriString[256] = {0};
IStream *fontStream = NULL;
IOpcPartUri *fontUri = NULL;
IXpsOMFontResource *fontResource = NULL;
IXpsOMVisualCollection *pageVisuals = NULL;
IXpsOMPage *page = NULL;
IXpsOMVisual *canvasVisual = NULL;
IXpsOMSolidColorBrush *xpsTextColor = NULL;
XPS_COLOR xpsColorBodyText;
// Create font stream.
hr = xpsFactory->CreateReadOnlyStreamOnFile (
fontFileName, &fontStream );
// Create new obfuscated part name for this resource using a GUID.
hr = CoCreateGuid( &fontNameGuid );
hr = StringFromGUID2(
fontNameGuid,
guidString,
ARRAYSIZE(guidString));
// Create a URI string for this font resource that will place
// the font part in the /Resources/Fonts folder of the package.
wcscpy_s(uriString, ARRAYSIZE(uriString), L"/Resources/Fonts/");
// Create the part name using the GUID string as the name and
// ".odttf" as the extension GUID string start and ends with
// curly braces so they are removed.
wcsncat_s(uriString, ARRAYSIZE(uriString),
guidString + 1, wcslen(guidString) - 2);
wcscat_s(uriString, ARRAYSIZE(uriString), L".odttf");
// Create the font URI interface.
hr = xpsFactory->CreatePartUri(
uriString,
&fontUri);
// Create the font resource.
hr = xpsFactory->CreateFontResource(
fontStream,
XPS_FONT_EMBEDDING_OBFUSCATED,
fontUri,
FALSE, // isObfSourceStream
&fontResource);
if (NULL != fontUri) fontUri->Release();
// Create the brush to use for the font.
xpsColorBodyText.colorType = XPS_COLOR_TYPE_SRGB;
xpsColorBodyText.value.sRGB.alpha = 0xFF;
xpsColorBodyText.value.sRGB.red = 0x00;
xpsColorBodyText.value.sRGB.green = 0x00;
xpsColorBodyText.value.sRGB.blue = 0x00;
hr = xpsFactory->CreateSolidColorBrush(
&xpsColorBodyText,
NULL, // This color type does not use a color profile resource.
&xpsTextColor);
// xpsTextColor is released after it has been used.
在页面中绘制文本
代码示例的最后一个部分为每个类似格式的文本串创建字形串。 若要在最后这个部分执行代码,需要 xpsFactory 接口以及字体资源和文本颜色画笔,还必须已将它们实例化和初始化。 在此示例中,第一部分中所述的函数用于创建字形串并将其添加到页面。
// The following interfaces are created outside of this example.
// The page on which to place the text.
IXpsOMPage *page = NULL;
// The object factory used by this program.
IXpsOMObjectFactory *xpsFactory = NULL;
// The font resource created in the previous snippet.
IXpsOMFontResource *fontResource = NULL;
// The color brush created in the previous snippet.
IXpsOMSolidColorBrush *xpsTextColor = NULL;
// The following variables are defined outside of this example.
// An array of pointers to the Unicode strings to write.
LPCWSTR *textRuns = NULL;
// An array of start points that correspond to the
// strings in textRuns.
XPS_POINT *startPoints = NULL;
// The number of text runs to add to the page.
UINT32 numRuns = 0;
HRESULT hr = S_OK;
FLOAT fontSize = 7.56f;
UINT32 thisRun;
// Add all the text runs to the page.
thisRun = 0;
while (thisRun < numRuns) {
hr = WriteText_AddTextToPage(
xpsFactory,
fontResource,
fontSize,
xpsTextColor,
&startPoints[thisRun],
textRuns[thisRun],
page);
thisRun++;
}
相关主题
-
后续步骤
-
本部分使用的内容
-
详细信息