internal static extern void cairo_scaled_font_extents(IntPtr scaled_font, out FontExtents extents);
internal static extern void cairo_font_extents(IntPtr cr, out FontExtents extents);
internal static extern void cairo_scaled_font_extents (IntPtr scaled_font, out FontExtents extents);
internal static extern void cairo_font_extents (IntPtr cr, out FontExtents extents);
protected override void onDraw(Context gr) { base.onDraw (gr); gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize (Font.Size); gr.FontOptions = Interface.FontRenderingOptions; gr.Antialias = Interface.Antialias; rText = new Rectangle(new Size( measureRawSize(LayoutingType.Width), measureRawSize(LayoutingType.Height))); rText.Width -= 2 * Margin; rText.Height -= 2 * Margin; widthRatio = 1f; heightRatio = 1f; Rectangle cb = ClientRectangle; rText.X = cb.X; rText.Y = cb.Y; if (horizontalStretch) { widthRatio = (float)cb.Width / (float)rText.Width; if (!verticalStretch) heightRatio = widthRatio; } if (verticalStretch) { heightRatio = (float)cb.Height / (float)rText.Height; if (!horizontalStretch) widthRatio = heightRatio; } rText.Width = (int)(widthRatio * (float)rText.Width); rText.Height = (int)(heightRatio * (float)rText.Height); switch (TextAlignment) { case Alignment.TopLeft: //ok rText.X = cb.X; rText.Y = cb.Y; break; case Alignment.Top: //ok rText.Y = cb.Y; rText.X = cb.X + cb.Width / 2 - rText.Width / 2; break; case Alignment.TopRight: //ok rText.Y = cb.Y; rText.X = cb.Right - rText.Width; break; case Alignment.Left://ok rText.X = cb.X; rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2; break; case Alignment.Right://ok rText.X = cb.X + cb.Width - rText.Width; rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2; break; case Alignment.Bottom://ok rText.X = cb.Width / 2 - rText.Width / 2; rText.Y = cb.Height - rText.Height; break; case Alignment.BottomLeft://ok rText.X = cb.X; rText.Y = cb.Bottom - rText.Height; break; case Alignment.BottomRight://ok rText.Y = cb.Bottom - rText.Height; rText.X = cb.Right - rText.Width; break; case Alignment.Center://ok rText.X = cb.X + cb.Width / 2 - rText.Width / 2; rText.Y = cb.Y + cb.Height / 2 - rText.Height / 2; break; } gr.FontMatrix = new Matrix(widthRatio * (float)Font.Size, 0, 0, heightRatio * (float)Font.Size, 0, 0); fe = gr.FontExtents; #region draw text cursor if (HasFocus && Selectable) { if (mouseLocalPos >= 0) { computeTextCursor(gr); if (SelectionInProgress) { if (SelBegin < 0){ SelBegin = new Point(CurrentColumn, CurrentLine); SelStartCursorPos = textCursorPos; SelRelease = -1; }else{ SelRelease = new Point(CurrentColumn, CurrentLine); if (SelRelease == SelBegin) SelRelease = -1; else SelEndCursorPos = textCursorPos; } } }else computeTextCursorPosition(gr); Foreground.SetAsSource (gr); gr.LineWidth = 1.0; gr.MoveTo (0.5 + textCursorPos + rText.X, rText.Y + CurrentLine * fe.Height); gr.LineTo (0.5 + textCursorPos + rText.X, rText.Y + (CurrentLine + 1) * fe.Height); gr.Stroke(); } #endregion //****** debug selection ************* // if (SelRelease >= 0) { // gr.Color = Color.Green; // Rectangle R = new Rectangle ( // rText.X + (int)SelEndCursorPos - 2, // rText.Y + (int)(SelRelease.Y * fe.Height), // 4, // (int)fe.Height); // gr.Rectangle (R); // gr.Fill (); // } // if (SelBegin >= 0) { // gr.Color = Color.UnmellowYellow; // Rectangle R = new Rectangle ( // rText.X + (int)SelStartCursorPos - 2, // rText.Y + (int)(SelBegin.Y * fe.Height), // 4, // (int)fe.Height); // gr.Rectangle (R); // gr.Fill (); // } //******************* for (int i = 0; i < lines.Count; i++) { string l = lines [i].Replace ("\t", new String (' ', Interface.TabSize)); int lineLength = (int)gr.TextExtents (l).XAdvance; Rectangle lineRect = new Rectangle ( rText.X, rText.Y + (int)Math.Ceiling(i * fe.Height), lineLength, (int)Math.Ceiling(fe.Height)); // if (TextAlignment == Alignment.Center || // TextAlignment == Alignment.Top || // TextAlignment == Alignment.Bottom) // lineRect.X += (rText.Width - lineLength) / 2; // else if (TextAlignment == Alignment.Right || // TextAlignment == Alignment.TopRight || // TextAlignment == Alignment.BottomRight) // lineRect.X += (rText.Width - lineLength); if (Selectable) { if (SelRelease >= 0 && i >= selectionStart.Y && i <= selectionEnd.Y) { gr.SetSourceColor (selBackground); Rectangle selRect = lineRect; int cpStart = (int)SelStartCursorPos, cpEnd = (int)SelEndCursorPos; if (SelBegin.Y > SelRelease.Y) { cpStart = cpEnd; cpEnd = (int)SelStartCursorPos; } if (i == selectionStart.Y) { selRect.Width -= cpStart; selRect.Left += cpStart; } if (i == selectionEnd.Y) selRect.Width -= (lineLength - cpEnd); gr.Rectangle (selRect); gr.Fill (); } } if (string.IsNullOrWhiteSpace (l)) continue; Foreground.SetAsSource (gr); gr.MoveTo (lineRect.X, rText.Y + fe.Ascent + fe.Height * i); gr.ShowText (l); gr.Fill (); } }
protected override int measureRawSize(LayoutingType lt) { if (lines == null) lines = getLines; using (ImageSurface img = new ImageSurface (Format.Argb32, 10, 10)) { using (Context gr = new Context (img)) { //Cairo.FontFace cf = gr.GetContextFontFace (); gr.SelectFontFace (Font.Name, Font.Slant, Font.Wheight); gr.SetFontSize (Font.Size); fe = gr.FontExtents; te = new TextExtents(); if (lt == LayoutingType.Height){ int lc = lines.Count; //ensure minimal height = text line height if (lc == 0) lc = 1; return (int)Math.Ceiling(fe.Height * lc) + Margin * 2; } foreach (string s in lines) { string l = s.Replace("\t", new String (' ', Interface.TabSize)); TextExtents tmp = gr.TextExtents (l); if (tmp.XAdvance > te.XAdvance) te = tmp; } return (int)Math.Ceiling (te.XAdvance) + Margin * 2; } } }