使用 RenderTarget.DrawTextLayout() 方法主要是设置 IDWriteTextLayout 接口;
构建 IDWriteTextLayout 接口需要 IDWriteTextFormat 接口。

IDWriteTextLayout 接口是经过完全分析和格式化后的文本块。

{RenderTarget.DrawTextLayout() 方法}
procedure DrawTextLayout(
  origin: D2D1_POINT_2F;                    //起点
  const textLayout: IDWriteTextLayout;      //分析和格式化后的文本块
  const defaultForegroundBrush: ID2D1Brush; //前景画刷
  options: TD2D1DrawTextOptions             //见下(参数4);默认值 D2D1_DRAW_TEXT_OPTIONS_NONE
); stdcall;

{参数4}
options: TD2D1DrawTextOptions {指定是禁用文本对齐还是启用剪切到布局矩形, 可使用按位组合值}
D2D1_DRAW_TEXT_OPTIONS_NO_SNAP = 1; //文本不垂直对齐到像素边界。建议对要进行动画处理的文本采用此设置。
D2D1_DRAW_TEXT_OPTIONS_CLIP    = 2; //文本剪切到布局矩形。
D2D1_DRAW_TEXT_OPTIONS_NONE    = 0; //文本垂直对齐到像素边界,但是不剪切到布局矩形。


{IDWriteTextLayout 接口提供的方法}
IDWriteTextLayout.Draw                     //使用指定的客户端绘制上下文绘制文本
IDWriteTextLayout.GetClusterMetrics        //检索每个字形群集的逻辑属性和度量
IDWriteTextLayout.DetermineMinWidth        //确定在整个词的各字符之间不断开的前提下可以为布局设置的最小宽度
IDWriteTextLayout.GetDrawingEffect         //获取在指定文本位置处应用程序定义的绘制效果
IDWriteTextLayout.GetFontCollection        //获取与指定位置处的文本关联的字体集合
IDWriteTextLayout.GetFontFamilyName        //复制指定位置处文本的字体系列名称
IDWriteTextLayout.GetFontFamilyNameLength  //获取当前位置处的字体系列名称的长度
IDWriteTextLayout.GetFontSize              //获取指定位置处文本的字体高度(以 em 为单位)
IDWriteTextLayout.GetFontStretch           //获取指定位置处文本的字体拉伸方式
IDWriteTextLayout.GetFontStyle             //获取指定位置处文本的字体样式(也称作斜度)
IDWriteTextLayout.GetFontWeight            //获取指定位置处文本的字体粗细
IDWriteTextLayout.GetInlineObject          //获取给定位置处的嵌入式对象
IDWriteTextLayout.GetLineMetrics           //检索文本字符串的每一文本行的相关信息
IDWriteTextLayout.GetLocaleName            //获取指定位置处文本的区域设置名称
IDWriteTextLayout.GetLocaleNameLength      //获取指定位置处文本的区域设置名称的长度
IDWriteTextLayout.GetMaxHeight             //获取布局最大高度
IDWriteTextLayout.GetMaxWidth              //获取布局最大宽度
IDWriteTextLayout.GetMetrics               //检索格式化后的字符串的总体度量
IDWriteTextLayout.GetOverhangMetrics       //返回布局及其包含的所有对象(包括文本字形和嵌入式对象)的延伸量(以与设备无关的像素(DIP)为单位)
IDWriteTextLayout.GetStrikethrough         //获取指定位置处文本中存在的删除线
IDWriteTextLayout.GetTypography            //获取指定位置处文本的版式设置
IDWriteTextLayout.GetUnderline             //获取指定位置处文本中存在的下划线
IDWriteTextLayout.HitTestPoint             //获取布局框中指定点的 DWRITE_HIT_TEST_METRICS 结构; 它还报告该点是否在由文本布局表示的文本字符串内
IDWriteTextLayout.HitTestTextPosition      //获取相对于布局框左上角的指定文本的位置以及包含围绕该文本位置的输出几何图形的 DWRITE_HIT_TEST_METRICS 结构
IDWriteTextLayout.HitTestTextRange         //在给定起始文本位置和文本长度的情况下, 获取指定文本位置的 DWRITE_HIT_TEST_METRICS 结构
IDWriteTextLayout.SetDrawingEffect         //设置应用程序定义的绘制效果
IDWriteTextLayout.SetFontCollection        //设置字体集合
IDWriteTextLayout.SetFontFamilyName        //为指定文本范围内的文本设置以 Null 结尾的字体系列名称
IDWriteTextLayout.SetFontSize              //设置指定文本范围内文本的字体高度(以 em 为单位)
IDWriteTextLayout.SetFontStretch           //设置指定文本范围内文本的字体拉伸方式
IDWriteTextLayout.SetFontStyle             //设置指定文本范围内文本的字体样式
IDWriteTextLayout.SetFontWeight            //设置指定文本范围内文本的字体粗细
IDWriteTextLayout.SetInlineObject          //设置嵌入式对象
IDWriteTextLayout.SetLocaleName            //设置指定文本范围内文本的区域设置名称
IDWriteTextLayout.SetMaxHeight             //设置布局最大高度
IDWriteTextLayout.SetMaxWidth              //设置布局最大宽度
IDWriteTextLayout.SetStrikethrough         //为指定文本范围内的文本设置删除线
IDWriteTextLayout.SetTypography            //为指定文本范围内的文本设置字体版式功能
IDWriteTextLayout.SetUnderline             //为指定文本范围内的文本设置下划线


测试代码:

uses Direct2D, D2D1;

{构建 DWRITE_TEXT_RANGE 结构的函数}
function DWriteTextRange(pos,len: Cardinal): TDwriteTextRange;
begin
  Result.startPosition := pos;
  Result.length := len;
end;

{构建 DWRITE_FONT_FEATURE 结构的函数}
function DWriteFontFeature(nameTag: Integer; parameter: Cardinal): TDwriteFontFeature;
begin
  Result.nameTag := nameTag;
  Result.parameter := parameter; //选择器索引, 0 表示禁用
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  cvs: TDirect2DCanvas;
  str: string;
  iTextFormat: IDWriteTextFormat;
  iSolidColorBrush: ID2D1SolidColorBrush;
  iTextLayout: IDWriteTextLayout;
  iTypography: IDWriteTypography;
begin
  str := 'Hello World using DirectWrite!';

  {建立 IDWriteTextFormat}
  DWriteFactory.CreateTextFormat(
    'Gabriola', //Gabriola 是 Win7 的新字体
    nil,
    DWRITE_FONT_WEIGHT_REGULAR,
    DWRITE_FONT_STYLE_NORMAL,
    DWRITE_FONT_STRETCH_NORMAL,
    72.0,
    'en-us',
    iTextFormat
  );
  iTextFormat.SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
  iTextFormat.SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);

  {建立 IDWriteTextLayout}
  DWriteFactory.CreateTextLayout(
    PWideChar(str), //文本
    Length(str),    //文本长度
    iTextFormat,    //IDWriteTextFormat 接口
    ClientWidth,    //宽度
    ClientHeight,   //高度
    iTextLayout     //输出 IDWriteTextLayout
  );

  {设置部分文本的字号、下划线、粗体}
  iTextLayout.SetFontSize(100.0, DWriteTextRange(18, 6)); //指定从 18 个字符起的 6 个字符的大小为 100
  iTextLayout.SetUnderline(True, DWriteTextRange(18, 11));
  iTextLayout.SetFontWeight(DWRITE_FONT_WEIGHT_BOLD, DWriteTextRange(18, 11));

  {设置不同的花式变体, 只对支持该功能的字体(如 Gabriola)有效}
  DWriteFactory.CreateTypography(iTypography);
  iTypography.AddFontFeature(DWriteFontFeature(DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_7, 1));
  iTextLayout.SetTypography(iTypography, DWriteTextRange(0, Length(str)));

  cvs := TDirect2DCanvas.Create(Canvas, ClientRect);
  cvs.RenderTarget.CreateSolidColorBrush(D2D1ColorF(clGreen), nil, iSolidColorBrush);
  cvs.RenderTarget.BeginDraw;
  cvs.RenderTarget.Clear(D2D1ColorF(clWhite));
  cvs.RenderTarget.DrawTextLayout(D2D1PointF(0,0), iTextLayout, iSolidColorBrush);
  cvs.RenderTarget.EndDraw();
  cvs.Free;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  Repaint;
end;


效果图:



测试其它字体和其它花式的程序:
posted on 2011-04-13 12:39  万一  阅读(3020)  评论(0编辑  收藏  举报