コード例 #1
0
        /// <summary>
        /// Measures the text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontSize">Size of the font.</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 (string.IsNullOrEmpty (text) || fontFamily == null) {
                return OxySize.Empty;
            }

            var fontName = GetActualFontName (fontFamily, fontWeight);
            var font = this.GetCachedFont (fontName, (float)fontSize);
            using (var attributedString = new NSAttributedString (text, new CTStringAttributes {
                ForegroundColorFromContext = true,
                Font = font
            })) {
                using (var textLine = new CTLine (attributedString)) {
                    float lineHeight, delta;
                    this.GetFontMetrics (font, out lineHeight, out delta);

                    // the text position must be set to get the correct bounds
                    this.gctx.TextPosition = new PointF (0, 0);

                    var bounds = textLine.GetImageBounds (this.gctx);
                    var width = bounds.Left + bounds.Width;

                    return new OxySize (width, lineHeight);
                }
            }
        }
コード例 #2
0
        public SizeF MeasureString(string textg, Font font, RectangleF rect)
        {
            // As per documentation
            // https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101
            //
            // * Note * Not sure if we should save off the graphic state, set context transform to identity
            //  and restore state to do the measurement.  Something to be looked into.
            //			context.TextPosition = rect.Location;
            //			var startPos = context.TextPosition;
            //			context.SelectFont ( font.nativeFont.PostScriptName,
            //			                    font.SizeInPoints,
            //			                    CGTextEncoding.MacRoman);
            //			context.SetTextDrawingMode(CGTextDrawingMode.Invisible);
            //
            //			context.SetCharacterSpacing(1);
            //			var textMatrix = CGAffineTransform.MakeScale(1f,-1f);
            //			context.TextMatrix = textMatrix;
            //
            //			context.ShowTextAtPoint(rect.X, rect.Y, textg, textg.Length);
            //			var endPos = context.TextPosition;
            //
            //			var measure = new SizeF(endPos.X - startPos.X, font.nativeFont.CapHeightMetric);

            var atts = buildAttributedString(textg, font);

            // for now just a line not sure if this is going to work
            CTLine line = new CTLine(atts);
            var lineBounds = line.GetImageBounds(context);
            // Create and initialize some values from the bounds.
            float ascent;
            float descent;
            float leading;
            double lineWidth = line.GetTypographicBounds(out ascent, out descent, out leading);

            var measure = new SizeF((float)lineWidth, ascent + descent);

            return measure;
        }
コード例 #3
0
        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="p">The position of the text.</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)
        {
            if (string.IsNullOrEmpty (text)) {
                return;
            }

            var fontName = GetActualFontName (fontFamily, fontWeight);

            var font = this.GetCachedFont (fontName, fontSize);
            using (var attributedString = new NSAttributedString (text, new CTStringAttributes {
                ForegroundColorFromContext = true,
                Font = font
            })) {
                using (var textLine = new CTLine (attributedString)) {
                    float width;
                    float height;

                    this.gctx.TextPosition = new PointF (0, 0);

                    float lineHeight, delta;
                    this.GetFontMetrics (font, out lineHeight, out delta);

                    var bounds = textLine.GetImageBounds (this.gctx);

                    var x0 = 0;
                    var y0 = delta;

                    if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom) {
                        width = bounds.Left + bounds.Width;
                        height = lineHeight;
                    } else {
                        width = height = 0f;
                    }

                    if (maxSize.HasValue) {
                        if (width > maxSize.Value.Width) {
                            width = (float)maxSize.Value.Width;
                        }

                        if (height > maxSize.Value.Height) {
                            height = (float)maxSize.Value.Height;
                        }
                    }

                    var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width);
                    var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height);

                    this.SetFill (fill);
                    this.SetAlias (false);

                    this.gctx.SaveState ();
                    this.gctx.TranslateCTM ((float)p.X, (float)p.Y);
                    if (!rotate.Equals (0)) {
                        this.gctx.RotateCTM ((float)(rotate / 180 * Math.PI));
                    }

                    this.gctx.TranslateCTM ((float)dx + x0, (float)dy + y0);
                    this.gctx.ScaleCTM (1f, -1f);

                    if (maxSize.HasValue) {
                        var clipRect = new RectangleF (-x0, y0, (float)Math.Ceiling (width), (float)Math.Ceiling (height));
                        this.gctx.ClipToRect (clipRect);
                    }

                    textLine.Draw (this.gctx);

                    this.gctx.RestoreState ();
                }
            }
        }