Browse Source

Merge pull request #43 from ncarrillo/feat-cairo-textalignment

Feat cairo textalignment
pull/58/head
Steven Kirk 12 years ago
parent
commit
e6fe2cbff8
  1. 11
      Cairo/Perspex.Cairo/CairoExtensions.cs
  2. 3
      Cairo/Perspex.Cairo/CairoPlatform.cs
  3. 10
      Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs
  4. 1
      Perspex.Controls/Presenters/TextPresenter.cs
  5. 11
      Perspex.Controls/TextBlock.cs
  6. 7
      Perspex.SceneGraph/Media/FontStyle.cs
  7. 2
      Perspex.SceneGraph/Media/FormattedText.cs
  8. 1
      Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs
  9. 3
      Windows/Perspex.Direct2D1/Direct2D1Platform.cs
  10. 3
      Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs
  11. 12
      Windows/Perspex.Direct2D1/PrimitiveExtensions.cs

11
Cairo/Perspex.Cairo/CairoExtensions.cs

@ -56,5 +56,16 @@ namespace Perspex.Cairo
return Pango.Weight.Ultralight; return Pango.Weight.Ultralight;
} }
public static Pango.Alignment ToCairo(this Perspex.Media.TextAlignment alignment)
{
if (alignment == Perspex.Media.TextAlignment.Left)
return Pango.Alignment.Left;
if (alignment == Perspex.Media.TextAlignment.Centered)
return Pango.Alignment.Center;
return Pango.Alignment.Right;
}
} }
} }

3
Cairo/Perspex.Cairo/CairoPlatform.cs

@ -35,9 +35,10 @@ namespace Perspex.Cairo
string fontFamily, string fontFamily,
double fontSize, double fontSize,
FontStyle fontStyle, FontStyle fontStyle,
TextAlignment textAlignment,
Perspex.Media.FontWeight fontWeight) Perspex.Media.FontWeight fontWeight)
{ {
return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, fontWeight); return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, textAlignment, fontWeight);
} }
public IRenderer CreateRenderer(IPlatformHandle handle, double width, double height) public IRenderer CreateRenderer(IPlatformHandle handle, double width, double height)

10
Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs

@ -20,6 +20,7 @@ namespace Perspex.Cairo.Media
string fontFamily, string fontFamily,
double fontSize, double fontSize,
FontStyle fontStyle, FontStyle fontStyle,
TextAlignment textAlignment,
FontWeight fontWeight) FontWeight fontWeight)
{ {
var context = Locator.Current.GetService<Pango.Context>(); var context = Locator.Current.GetService<Pango.Context>();
@ -32,6 +33,8 @@ namespace Perspex.Cairo.Media
Style = (Pango.Style)fontStyle, Style = (Pango.Style)fontStyle,
Weight = fontWeight.ToCairo() Weight = fontWeight.ToCairo()
}; };
this.Layout.Alignment = textAlignment.ToCairo();
} }
public Size Constraint public Size Constraint
@ -43,6 +46,7 @@ namespace Perspex.Cairo.Media
set set
{ {
this.Layout.Width = Pango.Units.FromDouble(value.Width); this.Layout.Width = Pango.Units.FromDouble(value.Width);
} }
} }
@ -98,6 +102,12 @@ namespace Perspex.Cairo.Media
int width; int width;
int height; int height;
this.Layout.GetPixelSize(out width, out height); this.Layout.GetPixelSize(out width, out height);
if (this.Layout.Alignment == Pango.Alignment.Right)
{
return new Size(width, height);
}
return new Size(width, height); return new Size(width, height);
} }

1
Perspex.Controls/Presenters/TextPresenter.cs

@ -165,6 +165,7 @@ namespace Perspex.Controls.Presenters
this.FontFamily, this.FontFamily,
this.FontSize, this.FontSize,
this.FontStyle, this.FontStyle,
TextAlignment.Left,
this.FontWeight)) this.FontWeight))
{ {
return formattedText.Measure(); return formattedText.Measure();

11
Perspex.Controls/TextBlock.cs

@ -37,6 +37,9 @@ namespace Perspex.Controls
public static readonly PerspexProperty<TextWrapping> TextWrappingProperty = public static readonly PerspexProperty<TextWrapping> TextWrappingProperty =
PerspexProperty.Register<TextBlock, TextWrapping>("TextWrapping"); PerspexProperty.Register<TextBlock, TextWrapping>("TextWrapping");
public static readonly PerspexProperty<TextAlignment> TextAlignmentProperty =
PerspexProperty.Register<TextBlock, TextAlignment>("TextAlignment");
private FormattedText formattedText; private FormattedText formattedText;
private Size constraint; private Size constraint;
@ -45,6 +48,7 @@ namespace Perspex.Controls
{ {
Observable.Merge( Observable.Merge(
this.GetObservable(TextProperty).Select(_ => Unit.Default), this.GetObservable(TextProperty).Select(_ => Unit.Default),
this.GetObservable(TextAlignmentProperty).Select(_ => Unit.Default),
this.GetObservable(FontSizeProperty).Select(_ => Unit.Default), this.GetObservable(FontSizeProperty).Select(_ => Unit.Default),
this.GetObservable(FontStyleProperty).Select(_ => Unit.Default)) this.GetObservable(FontStyleProperty).Select(_ => Unit.Default))
.Subscribe(_ => .Subscribe(_ =>
@ -114,6 +118,12 @@ namespace Perspex.Controls
set { this.SetValue(TextWrappingProperty, value); } set { this.SetValue(TextWrappingProperty, value); }
} }
public TextAlignment TextAlignment
{
get { return this.GetValue(TextAlignmentProperty); }
set { this.SetValue(TextAlignmentProperty, value); }
}
public override void Render(IDrawingContext context) public override void Render(IDrawingContext context)
{ {
Brush background = this.Background; Brush background = this.Background;
@ -133,6 +143,7 @@ namespace Perspex.Controls
this.FontFamily, this.FontFamily,
this.FontSize, this.FontSize,
this.FontStyle, this.FontStyle,
this.TextAlignment,
this.FontWeight); this.FontWeight);
result.Constraint = this.constraint; result.Constraint = this.constraint;
return result; return result;

7
Perspex.SceneGraph/Media/FontStyle.cs

@ -12,4 +12,11 @@ namespace Perspex.Media
Oblique, Oblique,
Italic, Italic,
} }
public enum TextAlignment
{
Left,
Centered,
Right,
}
} }

2
Perspex.SceneGraph/Media/FormattedText.cs

@ -18,6 +18,7 @@ namespace Perspex.Media
string fontFamilyName, string fontFamilyName,
double fontSize, double fontSize,
FontStyle fontStyle, FontStyle fontStyle,
TextAlignment textAlignment,
FontWeight fontWeight) FontWeight fontWeight)
{ {
this.Text = text; this.Text = text;
@ -32,6 +33,7 @@ namespace Perspex.Media
fontFamilyName, fontFamilyName,
fontSize, fontSize,
fontStyle, fontStyle,
textAlignment,
fontWeight); fontWeight);
} }

1
Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs

@ -17,6 +17,7 @@ namespace Perspex.Platform
string fontFamily, string fontFamily,
double fontSize, double fontSize,
FontStyle fontStyle, FontStyle fontStyle,
TextAlignment textAlignment,
FontWeight fontWeight); FontWeight fontWeight);
IStreamGeometryImpl CreateStreamGeometry(); IStreamGeometryImpl CreateStreamGeometry();

3
Windows/Perspex.Direct2D1/Direct2D1Platform.cs

@ -42,9 +42,10 @@ namespace Perspex.Direct2D1
string fontFamily, string fontFamily,
double fontSize, double fontSize,
FontStyle fontStyle, FontStyle fontStyle,
TextAlignment textAlignment,
FontWeight fontWeight) FontWeight fontWeight)
{ {
return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, fontWeight); return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, textAlignment, fontWeight);
} }
public IRenderer CreateRenderer(IPlatformHandle handle, double width, double height) public IRenderer CreateRenderer(IPlatformHandle handle, double width, double height)

3
Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs

@ -21,6 +21,7 @@ namespace Perspex.Direct2D1.Media
string fontFamily, string fontFamily,
double fontSize, double fontSize,
FontStyle fontStyle, FontStyle fontStyle,
TextAlignment textAlignment,
FontWeight fontWeight) FontWeight fontWeight)
{ {
var factory = Locator.Current.GetService<DWrite.Factory>(); var factory = Locator.Current.GetService<DWrite.Factory>();
@ -38,6 +39,8 @@ namespace Perspex.Direct2D1.Media
format, format,
float.MaxValue, float.MaxValue,
float.MaxValue); float.MaxValue);
this.TextLayout.TextAlignment = textAlignment.ToDirect2D();
} }
public Size Constraint public Size Constraint

12
Windows/Perspex.Direct2D1/PrimitiveExtensions.cs

@ -9,6 +9,7 @@ namespace Perspex.Direct2D1
using System.Linq; using System.Linq;
using SharpDX; using SharpDX;
using SharpDX.Direct2D1; using SharpDX.Direct2D1;
using DWrite = SharpDX.DirectWrite;
public static class PrimitiveExtensions public static class PrimitiveExtensions
{ {
@ -128,5 +129,16 @@ namespace Perspex.Direct2D1
(float)rect.Width, (float)rect.Width,
(float)rect.Height); (float)rect.Height);
} }
public static DWrite.TextAlignment ToDirect2D(this Perspex.Media.TextAlignment alignment)
{
if (alignment == Perspex.Media.TextAlignment.Left)
return DWrite.TextAlignment.Leading;
if (alignment == Perspex.Media.TextAlignment.Centered)
return DWrite.TextAlignment.Center;
return DWrite.TextAlignment.Trailing;
}
} }
} }

Loading…
Cancel
Save