本主题介绍如何将文本写入 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++;
}
相关主题
-
后续步骤
-
本部分使用的内容
-
有关详细信息