Пример #1
0
 /// <summary>Creates a frozen <see cref="Paint"/> instance from the given parameters.</summary>
 public static Font CreateFrozen(string typefaceName, double fontSize, FontWeight fontWeight) {
    var font = new Font(typefaceName, fontSize);
    font.Weight = fontWeight;
    font.Freeze();
    return font;
 }
Пример #2
0
      /// <summary/>
      protected override Size MeasureOverride(Size constraint) {
         var desiredSize = new Size();

         _labelGlyphs = null;
         if (ShowLabels && LabelTexts != null && LabelTexts.Length > 0) {
            var labelFont = new Font(new Typeface(LabelFontFamily, FontStyles.Normal, LabelFontWeight, FontStretches.Normal), LabelFontSize);
            labelFont.Freeze();
            var G = _labelGlyphs = new TextGlyph[LabelTexts.Length];
            for (var i = 0; i < LabelTexts.Length; i++) {
               G[i] = new TextGlyph(LabelTexts[i], labelFont);
               desiredSize.Width = Math.Max(desiredSize.Width, G[i].Width);
               desiredSize.Height = Math.Max(desiredSize.Height, G[i].Height);
            }
            if (_isHorizontal) {
               desiredSize.Height += LabelPadding;
            } else {
               desiredSize.Width += LabelPadding;
            }
         }

         return desiredSize;
      }
Пример #3
0
      /// <summary/>
      public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
         if (value == null) {
            throw new ArgumentNullException("value");
         }
         var fontString = value as string;
         if (fontString.IsEmpty()) {
            return base.ConvertFrom(context, culture, value);
         }

         if (fontString.StartsWith("@") && fontString.IndexOf(",") < 0 && ResourceProvider != null) {
            return ResourceProvider.GetResource<Font>(fontString);
         }

         var fontFamily = Font.DefaultFamily;
         var fontSize = Font.DefaultSize;
         var fontWeight = Font.DefaultWeight;
         var fontStyle = Font.DefaultStyle;
         var fontStretch = Font.DefaultStretch;

         var parts = fontString.Trim().Split(_reComma);
         var nparts = parts.Length;
         if (nparts == 0) return null;
         if (nparts > 5) {
            throw new InvalidCastException();
         }
         for (var i = 0; i < nparts; i++) {
            parts[i] = parts[i].RemoveAll(new Regex(@"\,"));
         }

         if (nparts >= 1 && parts[0].NotEmpty()) {
            fontFamily = _parseFontFamily(context, culture, parts[0]);
         }
         if (nparts >= 2 && parts[1].NotEmpty()) {
            fontSize = Double.Parse(parts[1]);
         }
         if (nparts >= 3 && parts[2].NotEmpty()) {
            var converter = new FontWeightConverter();
            fontWeight = _parseFontWeight(context, culture, parts[2]);
         }
         if (nparts >= 4 && parts[3].NotEmpty()) {
            var converter = new FontStyleConverter();
            fontStyle = _parseFontStyle(context, culture, parts[3]);
         }
         if (nparts >= 5 && parts[4].NotEmpty()) {
            fontStretch = _parseFontStretch(context, culture, parts[4]);
         }

         var face = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
         var font = new Font(face, fontSize);
         font.Freeze();
         return font;
      }