/// <summary> /// The measure text. /// </summary> /// <param name="text">The text.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">The font size.</param> /// <param name="fontWeight">The font weight.</param> /// <returns>The size of the text.</returns> public override OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (text == null) { return(OxySize.Empty); } this.g.Save(); #if GTKSHARP3 Pango.Layout layout = new Layout(this.c); #else Pango.Layout layout = Pango.CairoHelper.CreateLayout(this.g); #endif Pango.FontDescription font = new Pango.FontDescription(); font.Family = fontFamily; font.Weight = (fontWeight >= 700) ? Pango.Weight.Bold : Pango.Weight.Normal; font.AbsoluteSize = (int)(fontSize * Pango.Scale.PangoScale); layout.FontDescription = font; layout.SetText(text); Pango.Rectangle inkRect; Pango.Rectangle logicalRect; layout.GetExtents(out inkRect, out logicalRect); this.g.Restore(); layout.Dispose(); return(new OxySize(logicalRect.Width / Pango.Scale.PangoScale, logicalRect.Height / Pango.Scale.PangoScale)); }
/// <summary> /// Draws the text. /// </summary> /// <param name="p">The p.</param> /// <param name="text">The text.</param> /// <param name="fill">The fill color.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <param name="rotate">The rotation angle.</param> /// <param name="halign">The horizontal alignment.</param> /// <param name="valign">The vertical alignment.</param> /// <param name="maxSize">The maximum size of the text.</param> public override void DrawText( ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalAlignment halign, VerticalAlignment valign, OxySize?maxSize) { Pango.Layout layout = Pango.CairoHelper.CreateLayout(this.g); Pango.FontDescription font = new Pango.FontDescription(); font.Family = fontFamily; font.Weight = (fontWeight >= 700) ? Pango.Weight.Bold : Pango.Weight.Normal; font.AbsoluteSize = (int)(fontSize * Pango.Scale.PangoScale); layout.FontDescription = font; layout.SetText(text); Pango.Rectangle inkRect; Pango.Rectangle size; layout.GetExtents(out inkRect, out size); size.Width /= (int)Pango.Scale.PangoScale; size.Height /= (int)Pango.Scale.PangoScale; if (maxSize != null) { int maxWidth = (int)Math.Min((Double)Int32.MaxValue, maxSize.Value.Width); int maxHeight = (int)Math.Min((Double)Int32.MaxValue, maxSize.Value.Height); size.Width = Math.Min(size.Width, maxWidth); size.Height = Math.Min(size.Height, maxHeight); } this.g.Save(); double dx = 0; if (halign == HorizontalAlignment.Center) { dx = -size.Width / 2; } if (halign == HorizontalAlignment.Right) { dx = -size.Width; } double dy = 0; if (valign == VerticalAlignment.Middle) { dy = -size.Height / 2; } if (valign == VerticalAlignment.Bottom) { dy = -size.Height; } this.g.Translate(p.X, p.Y); if (Math.Abs(rotate) > double.Epsilon) { this.g.Rotate(rotate * Math.PI / 180.0); } this.g.Translate(dx, dy); g.Rectangle(0, 0, size.Width + 0.1f, size.Height + 0.1f); g.Clip(); this.g.SetSourceColor(fill); Pango.CairoHelper.ShowLayout(this.g, layout); this.g.Restore(); }
private void DrawStringTBLR(PageText pt, Cairo.Context g, Cairo.Rectangle r) { StyleInfo si = pt.SI; string s = pt.Text; g.Save(); layout = CairoHelper.CreateLayout(g); //Pango fonts are scaled to 72dpi, Windows fonts uses 96dpi float fontsize = (si.FontSize * 72 / 96); var font = FontDescription.FromString($"{si.GetFontFamily().Name} {fontsize * PixelsX(1)}"); if (si.FontStyle == FontStyleEnum.Italic) { font.Style = Style.Italic; } switch (si.FontWeight) { case FontWeightEnum.Bold: case FontWeightEnum.Bolder: case FontWeightEnum.W500: case FontWeightEnum.W600: case FontWeightEnum.W700: case FontWeightEnum.W800: case FontWeightEnum.W900: font.Weight = Weight.Bold; break; } FontDescription oldfont = layout.FontDescription; layout.FontDescription = font; switch (si.TextAlign) { case TextAlignEnum.Right: layout.Alignment = Alignment.Right; break; case TextAlignEnum.Center: layout.Alignment = Alignment.Center; break; case TextAlignEnum.Left: default: layout.Alignment = Alignment.Left; break; } layout.Width = Units.FromPixels((int)(r.Height - si.PaddingTop - si.PaddingBottom - 2)); layout.Wrap = WrapMode.WordChar; layout.SetText(s); Rectangle logical; Rectangle ink; layout.GetExtents(out ink, out logical); double height = logical.Height / Scale.PangoScale; double y = 0; double x = 0; switch (si.VerticalAlign) { case VerticalAlignEnum.Top: x = r.X + si.PaddingLeft; break; case VerticalAlignEnum.Middle: x = r.X + (r.Width - height) / 2; break; case VerticalAlignEnum.Bottom: x = r.X + (r.Width - height) + si.PaddingLeft; break; } // draw the background DrawBackground(g, r, si); Cairo.Rectangle box = new Cairo.Rectangle( x, r.Y + r.Height - si.PaddingBottom - 1, r.Height - si.PaddingBottom - si.PaddingTop, r.Width - si.PaddingLeft + si.PaddingRight); g.Color = si.Color.ToCairoColor(); g.Rotate(270 * Math.PI / 180.0); CairoHelper.UpdateLayout(g, layout); g.MoveTo(-box.Y, box.X); CairoHelper.ShowLayout(g, layout); layout.FontDescription = oldfont; g.Restore(); }
private void DrawStringHorizontal(PageText pt, Cairo.Context g, Cairo.Rectangle r) { StyleInfo si = pt.SI; string s = pt.Text; g.Save(); layout = CairoHelper.CreateLayout(g); float fontsize = si.FontSize * 72f / 96f; var font = FontDescription.FromString(string.Format("{0} {1}", si.GetFontFamily().Name, fontsize * PixelsX(1))); if (si.FontStyle == FontStyleEnum.Italic) { font.Style = Style.Italic; } switch (si.FontWeight) { case FontWeightEnum.Bold: case FontWeightEnum.Bolder: case FontWeightEnum.W500: case FontWeightEnum.W600: case FontWeightEnum.W700: case FontWeightEnum.W800: case FontWeightEnum.W900: font.Weight = Weight.Bold; break; } FontDescription oldfont = layout.FontDescription; layout.FontDescription = font; switch (si.TextAlign) { case TextAlignEnum.Right: layout.Alignment = Alignment.Right; break; case TextAlignEnum.Center: layout.Alignment = Alignment.Center; break; case TextAlignEnum.Left: default: layout.Alignment = Alignment.Left; break; } layout.Width = Units.FromPixels((int)(r.Width - si.PaddingLeft - si.PaddingRight - 2)); layout.Wrap = WrapMode.WordChar; layout.SetText(s); Rectangle logical; Rectangle ink; layout.GetExtents(out ink, out logical); double height = logical.Height / Scale.PangoScale; double y = 0; switch (si.VerticalAlign) { case VerticalAlignEnum.Top: y = r.Y + si.PaddingTop; break; case VerticalAlignEnum.Middle: y = r.Y + (r.Height - height) / 2; break; case VerticalAlignEnum.Bottom: y = r.Y + (r.Height - height) - si.PaddingBottom; break; } // draw the background DrawBackground(g, r, si); Cairo.Rectangle box = new Cairo.Rectangle( r.X + si.PaddingLeft + 1, y, r.Width, r.Height); g.Color = si.Color.ToCairoColor(); g.MoveTo(box.X, box.Y); CairoHelper.ShowLayout(g, layout); layout.FontDescription = oldfont; g.Restore(); }