1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
| unit FMX.Canvas.D2D;
procedure TTextLayoutD2D.DoRenderLayout; var TextRange: TDWriteTextRange; TextFormat: IDWriteTextFormat; I: Integer; Attr: TTextAttributedRange; TrimOptions: TDwriteTrimming; TrimmingSign: IDWriteInlineObject; FontDescriptor: TDWriteFontDescriptor; SolidBrush: ID2D1SolidColorBrush; begin FLayout := nil; FTextRect := TRectF.Empty;
FontDescriptor := GetFontStyles(Font);
if Succeeded(TCanvasD2D.SharedDWriteFactory.CreateTextFormat(PChar(FontDescriptor.FamilyName), FFontCollection, FontDescriptor.Weight, FontDescriptor.Style, FontDescriptor.Stretch, Font.Size, PChar(FLocaleName), TextFormat)) then try if RightToLeft then TextFormat.SetReadingDirection(DWRITE_READING_DIRECTION_RIGHT_TO_LEFT) else TextFormat.SetReadingDirection(DWRITE_READING_DIRECTION_LEFT_TO_RIGHT);
if WordWrap then TextFormat.SetWordWrapping(DWRITE_WORD_WRAPPING_WRAP) else TextFormat.SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
FillChar(TrimOptions, SizeOf(TDwriteTrimming), 0); case Trimming of TTextTrimming.None: TrimOptions.granularity := DWRITE_TRIMMING_GRANULARITY_NONE; TTextTrimming.Character: TrimOptions.granularity := DWRITE_TRIMMING_GRANULARITY_CHARACTER; TTextTrimming.Word: TrimOptions.granularity := DWRITE_TRIMMING_GRANULARITY_WORD; end;
TrimmingSign := nil; if Trimming <> TTextTrimming.None then TCanvasD2D.SharedDWriteFactory.CreateEllipsisTrimmingSign(TextFormat, TrimmingSign); TextFormat.SetTrimming(TrimOptions, TrimmingSign); TrimmingSign := nil;
if Succeeded(TCanvasD2D.SharedDWriteFactory.CreateTextLayout(PChar(Text), Text.Length, TextFormat, MaxSize.X - Padding.Left - Padding.Right, MaxSize.Y - Padding.Top - Padding.Bottom, FLayout)) then begin TextRange.StartPosition := 0; TextRange.Length := Text.Length; FLayout.SetStrikethrough(TFontStyle.fsStrikeOut in Font.StyleExt.SimpleStyle, TextRange); [b]FLayout.SetUnderline(TFontStyle.fsUnderline in Font.StyleExt.SimpleStyle, TextRange);[/b] FLayout.SetFontWeight(FontDescriptor.Weight, TextRange);
for I := 0 to AttributesCount - 1 do begin Attr := Attributes[I]; TextRange.startPosition := Attr.Range.Pos; TextRange.length := Attr.Range.Length; if Attr.Attribute.Font <> nil then begin FLayout.SetStrikethrough(TFontStyle.fsStrikeOut in Attr.Attribute.Font.StyleExt.SimpleStyle, TextRange); FLayout.SetUnderline(TFontStyle.fsUnderline in Attr.Attribute.Font.StyleExt.SimpleStyle, TextRange); if not SameValue(Attr.Attribute.Font.Size, Font.Size, TEpsilon.FontSize) then FLayout.SetFontSize(Attr.Attribute.Font.Size, TextRange);
FontDescriptor := GetFontStyles(Attr.Attribute.Font); FLayout.SetFontWeight(FontDescriptor.Weight, TextRange); FLayout.SetFontStyle(FontDescriptor.Style, TextRange); FLayout.SetFontStretch(FontDescriptor.Stretch, TextRange); FLayout.SetFontFamilyName(PChar(FontDescriptor.FamilyName), TextRange); end; if (Attr.Attribute.Color <> TAlphaColorRec.Null) and Succeeded(TCanvasD2D.SharedRenderTarget.CreateSolidColorBrush(D2Color(Attr.Attribute.Color, Opacity), nil, SolidBrush)) then FLayout.SetDrawingEffect(SolidBrush, TextRange); end;
case HorizontalAlign of TTextAlign.Center: FLayout.SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); TTextAlign.Leading: FLayout.SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING); TTextAlign.Trailing: FLayout.SetTextAlignment(DWRITE_TEXT_ALIGNMENT_TRAILING); end; case VerticalAlign of TTextAlign.Center: FLayout.SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); TTextAlign.Leading: FLayout.SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR); TTextAlign.Trailing: FLayout.SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_FAR); end; FLayout.GetMetrics(FMetrics) FLayout.GetOverhangMetrics(FOverhangMetrics); UpdateTextRect; end; finally TextFormat := nil; end; end; |