static System.Drawing.SizeF StringSize(string text, int fontSize)
 {
     TextBlock txtMeasure = new TextBlock();
     txtMeasure.FontSize = fontSize;
     txtMeasure.Text = text;
     txtMeasure.Measure (new Size (1, 1));
     return new System.Drawing.SizeF((float)txtMeasure.ActualWidth, (float)txtMeasure.ActualHeight);
 }
Example #2
0
 static System.Drawing.SizeF StringSize(string text, int fontSize, FontFamily fontFamily, bool bold)
 {
     TextBlock txtMeasure = new TextBlock();
     txtMeasure.FontFamily = fontFamily;
     txtMeasure.FontSize = fontSize;
     txtMeasure.FontWeight = bold ? Windows.UI.Text.FontWeights.Bold : Windows.UI.Text.FontWeights.Normal;
     txtMeasure.Text = text;
     txtMeasure.Measure (new Size (1, 1));
     return new System.Drawing.SizeF((float)txtMeasure.ActualWidth, (float)txtMeasure.ActualHeight);
 }
Example #3
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 text size.</returns>
        public OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight)
        {
            if (string.IsNullOrEmpty(text))
            {
                return OxySize.Empty;
            }

            var tb = new TextBlock { Text = text };

            if (fontFamily != null)
            {
                tb.FontFamily = new FontFamily(fontFamily);
            }

            if (fontSize > 0)
            {
                tb.FontSize = fontSize;
            }

            tb.FontWeight = GetFontWeight(fontWeight);

            tb.Measure(new Size(1000, 1000));

            return new OxySize(tb.ActualWidth, tb.ActualHeight);
        }
Example #4
0
        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="p">The position.</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 void DrawText(
            ScreenPoint p,
            string text,
            OxyColor fill,
            string fontFamily,
            double fontSize,
            double fontWeight,
            double rotate,
            OxyPlot.HorizontalAlignment halign,
            OxyPlot.VerticalAlignment valign,
            OxySize? maxSize)
        {
            var tb = new TextBlock { Text = text, Foreground = new SolidColorBrush(fill.ToColor()) };

            // tb.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Animated);
            if (fontFamily != null)
            {
                tb.FontFamily = new FontFamily(fontFamily);
            }

            if (fontSize > 0)
            {
                tb.FontSize = fontSize;
            }

            tb.FontWeight = GetFontWeight(fontWeight);

            tb.Measure(new Size(1000, 1000));
            var size = new Size(tb.ActualWidth, tb.ActualHeight);
            if (maxSize != null)
            {
                if (size.Width > maxSize.Value.Width)
                {
                    size.Width = maxSize.Value.Width;
                }

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

                tb.Clip = new RectangleGeometry { Rect = new Rect(0, 0, size.Width, size.Height) };
            }

            double dx = 0;
            if (halign == OxyPlot.HorizontalAlignment.Center)
            {
                dx = -size.Width / 2;
            }

            if (halign == OxyPlot.HorizontalAlignment.Right)
            {
                dx = -size.Width;
            }

            double dy = 0;
            if (valign == OxyPlot.VerticalAlignment.Middle)
            {
                dy = -size.Height / 2;
            }

            if (valign == OxyPlot.VerticalAlignment.Bottom)
            {
                dy = -size.Height;
            }

            var transform = new TransformGroup();
            transform.Children.Add(new TranslateTransform { X = (int)dx, Y = (int)dy });
            if (!rotate.Equals(0))
            {
                transform.Children.Add(new RotateTransform { Angle = rotate });
            }

            transform.Children.Add(new TranslateTransform { X = (int)p.X, Y = (int)p.Y });
            tb.RenderTransform = transform;
            this.ApplyTooltip(tb);

            if (this.clip)
            {
                // add a clipping container that is not rotated
                var c = new Canvas();
                c.Children.Add(tb);
                this.Add(c);
            }
            else
            {
                this.Add(tb);
            }
        }
Example #5
0
        public void ExtTextOut(Canvas surface, double x, double y, ExtTextOutOptions options, Rect dimensions, string text, double[] dx)
        {
            TextBlock block = null;
            Grid container = null;
            double deviceX = LtoDX(x);
            double deviceY = LtoDY(y);
            double textWidth;
            double textHeight;
            Geometry clipGeometry = null;

            if (dx == null)
            {
                // No character placement
                block = new TextBlock
                    {
                        Inlines = {
                            new Run
                                {
                                    Text = text
                                }
                        }
                    };

                ApplyTextStyle(block, deviceY, true, options);

                //Canvas.SetLeft(block, deviceX);

                surface.Children.Add(block);
                block.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                textHeight = block.DesiredSize.Height;
                textWidth = block.DesiredSize.Width;
            }
            else
            {
                // Place individual characters
                int stringLength = text.Length;
                double currentPos = 0; // LtoDX(x);
                container = new Grid();

                for (int i = 0; i < stringLength; i++)
                {
                    block = new TextBlock
                        {
                            Inlines = {
                                new Run
                                {
                                    Text = text.Substring(i, 1)
                                }
                            }
                        };
                    //TextOptions.SetTextFormattingMode(block, TextFormattingMode.Display);
                    ApplyTextStyle(block, deviceY, false, null);

                    if (i > 0)
                    {
                        currentPos += ScaleWidth(dx[i - 1]);
                    }
                    block.Margin = new Thickness(currentPos, 0, 0, 0);
                    // TODO: Character width
                    //block.Width = dx[i];

                    container.Children.Add(block);
                }

                //Canvas.SetLeft(container, deviceX);
                ApplyTextContainerStyle(container, deviceY);

                surface.Children.Add(container);
                container.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                textHeight = container.DesiredSize.Height;
                textWidth = container.DesiredSize.Width;
            }

            // Horizontal alignment
            if ((_currentState.TextAlign & (ushort)TextAlignmentMode.TA_CENTER) == (ushort)TextAlignmentMode.TA_CENTER)
            {
                deviceX -= textWidth / 2.0;
            }
            else if ((_currentState.TextAlign & (ushort)TextAlignmentMode.TA_RIGHT) == (ushort)TextAlignmentMode.TA_RIGHT)
            {
                deviceX -= textWidth;
            }

            // Vertical Alignment
            if ((_currentState.TextAlign & (ushort)TextAlignmentMode.TA_BASELINE) == (ushort)TextAlignmentMode.TA_BASELINE)
            {
                double baseline;

            #if NETFX_CORE
                baseline = textHeight;
            #else
                if (block != null)
                {
                    var formattedText = new FormattedText(
                         text,
                         System.Globalization.CultureInfo.CurrentCulture,
                         block.FlowDirection,
                         new Typeface(block.FontFamily, block.FontStyle, block.FontWeight, block.FontStretch, block.FontFamily),
                         block.FontSize,
                         block.Foreground);

                    baseline = formattedText.Baseline;
                }
                else
                {
                    baseline = textHeight;
                }
            #endif

                deviceY -= baseline;
            }
            else if ((_currentState.TextAlign & (ushort)TextAlignmentMode.TA_BOTTOM) == (ushort)TextAlignmentMode.TA_BOTTOM)
            {
                deviceY -= textHeight;
            }

            // Clip
            clipGeometry = GetClipGeometryAsChild(
                new Rect(
                    deviceX,
                    deviceY,
                    textWidth,
                    textHeight)
                );

            if (container == null)
            {
                Canvas.SetLeft(block, deviceX);
                Canvas.SetTop(block, deviceY);

            #if !NETFX_CORE
                if (clipGeometry != null)
                {
                    block.Clip = clipGeometry;
                }
            #endif
            }
            else
            {
                Canvas.SetLeft(container, deviceX);
                Canvas.SetTop(container, deviceY);

            #if !NETFX_CORE
                if (clipGeometry != null)
                {
                    container.Clip = clipGeometry;
                }
            #endif
            }

            if ((options & ExtTextOutOptions.ETO_OPAQUE) == ExtTextOutOptions.ETO_OPAQUE)
            {
                block.Height = ScaleHeight(dimensions.Height);
                block.Width = ScaleWidth(dimensions.Width);
            }

            //if ((currentState.TextAlign & (ushort)TextAlignmentMode.TA_UPDATECP) == (ushort)TextAlignmentMode.TA_UPDATECP)
            //{
            //    // Update current pos
            //    Point pos = currentState.CurrentPosition;

            //    // TODO: Mapx and y
            //    pos.X = Canvas.GetLeft(block) + block.ActualWidth;
            //    pos.Y = Canvas.GetTop(block) + block.ActualHeight;

            //    currentState.CurrentPosition = pos;
            //}
        }
        public override object createFont(int face, int style, int size)
        {
            if (fontCache.ContainsKey(face | style | size))
            {
                return fontCache[face | style | size];
            }

            int a = 24;
            switch (size)
            {
                case 8: //com.codename1.ui.Font._fSIZE_1SMALL:
                    a = 15;
                    break;
                case 16: //com.codename1.ui.Font._fSIZE_1LARGE:
                    a = 56;
                    break;
            }
            
            NativeFont nf = new NativeFont();
            nf.height = a;
            nf.systemFace = face;
            nf.systemSize = size;
            nf.systemStyle = style;
            using (AutoResetEvent are = new AutoResetEvent(false))
            {
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    TextBlock tb = new TextBlock();
                    tb.FontSize = nf.height;
                    tb.Text = "Xp";
                    tb.Measure(new Size(1000000, 1000000));
                    nf.actualHeight = (int)tb.ActualHeight;
                    are.Set();
                });
                are.WaitOne();
            }

            if ((style & com.codename1.ui.Font._fSTYLE_1BOLD) == com.codename1.ui.Font._fSTYLE_1BOLD)
            {
                nf.bold = true;
            }
            if ((style & com.codename1.ui.Font._fSTYLE_1ITALIC) == com.codename1.ui.Font._fSTYLE_1ITALIC)
            {
                nf.italic = true;
            }

            fontCache[face | style | size] = nf;
            return nf;
        }
 public override int stringWidth(java.lang.Object n1, java.lang.String n2)
 {
     NativeFont font = f(n1);
     string str = toCSharp(n2);
     StringFontPair sfp = new StringFontPair(str, font);
     if (stringWidthCache.ContainsKey(sfp))
     {
         return stringWidthCache[sfp];
     }
     int result = 0;
     using (AutoResetEvent are = new AutoResetEvent(false))
     {
         System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
         {
             TextBlock tb = new TextBlock();
             tb.FontSize = font.height;
             tb.Text = str;
             tb.Measure(new Size(1000000, 1000000));
             result = (int)tb.ActualWidth;
             are.Set();
         });
         are.WaitOne();
     }
     stringWidthCache.Add(sfp, result);
     return result;
 }