예제 #1
0
        /// <summary>Loads this systems values from the given style.</summary>
        public void Load(Css.Style style, Css.ReflowDocument document)
        {
            // Get the range:
            Css.Value range = style[Css.Properties.RangeProperty.GlobalProperty];

            if (range != null && !range.IsAuto)
            {
                Css.ValueSet rangeSet = range as Css.ValueSet;

                int rangeSize = (rangeSet != null && rangeSet.Spacer == ",")?range.Count : 1;

                if (rangeSize > 1)
                {
                    AdditionalRanges = new CounterRange[rangeSize];

                    int overallMin = 0;
                    int overallMax = 0;

                    // For each one..
                    for (int i = 0; i < rangeSize; i++)
                    {
                        int min = range[i][0].GetInteger(null, null);
                        int max = range[i][1].GetInteger(null, null);

                        if (i == 0)
                        {
                            overallMin = min;
                            overallMax = max;
                        }
                        else
                        {
                            if (min < overallMin)
                            {
                                overallMin = min;
                            }

                            if (max > overallMax)
                            {
                                overallMax = max;
                            }
                        }

                        AdditionalRanges[i] = new CounterRange(min, max);
                    }

                    Min = overallMin;
                    Max = overallMax;
                }
                else
                {
                    AdditionalRanges = null;

                    // Set min/max:
                    Min = range[0].GetInteger(null, null);
                    Max = range[1].GetInteger(null, null);
                }
            }

            // Get the pad:
            Css.Value pad = style[Css.Properties.Pad.GlobalProperty];

            if (pad != null)
            {
                PadMin = pad[0].GetInteger(null, null);

                if (pad is Css.ValueSet)
                {
                    PadSymbol = pad[1].Text;
                }
                else
                {
                    PadSymbol = "";
                }
            }

            // Prefix:
            Css.Value prefix = style[Css.Properties.Prefix.GlobalProperty];

            if (prefix != null)
            {
                Prefix = prefix.Text;
            }

            // Suffix:
            Css.Value suffix = style[Css.Properties.Suffix.GlobalProperty];

            if (suffix != null)
            {
                Suffix = suffix.Text;
            }

            // Negative:
            Css.Value negative = style[Css.Properties.Negative.GlobalProperty];

            if (negative != null)
            {
                NegativePrefix = negative[0].Text;

                if (negative is Css.ValueSet)
                {
                    NegativeSuffix = negative[1].Text;
                }
                else
                {
                    NegativeSuffix = "";
                }
            }

            // Symbols:
            LoadSymbols(style);

            // Fallback:
            Css.Value fallback = style[Css.Properties.Fallback.GlobalProperty];

            if (fallback != null)
            {
                string fbName = fallback[0].Text;

                if (fbName == "none" || fbName == "decimal")
                {
                    fbName = null;
                }

                if (fbName != null)
                {
                    // Get by built in name:
                    Fallback = Counters.CounterSystems.Get(fbName);

                    if (Fallback == null)
                    {
                        // Get by doc name:
                        CounterStyleRule rule;
                        if (document.CssCounters.TryGetValue(fbName, out rule))
                        {
                            // Get the system:
                            Fallback = rule.System;
                        }
                    }
                }

                if (Fallback == null)
                {
                    // Default to decimal system:
                    Fallback = CounterSystems.Decimal;
                }
            }
        }
예제 #2
0
        /// <summary>Gets the counter text for the given value using this system.</summary>
        /// <param name='prefixed'>True if the prefix/ suffix should be added.</param>
        public string Get(int value, bool prefixed)
        {
            // If the value is out of range, fallback:
            if (value < Min || value > Max)
            {
                // Try fallback:
                if (Fallback == null)
                {
                    return("");
                }

                return(Fallback.Get(value, prefixed));
            }
            else if (AdditionalRanges != null)
            {
                // Must fall in one of them (there won't be many):
                bool inRange = false;

                for (int i = 0; i < AdditionalRanges.Length; i++)
                {
                    CounterRange range = AdditionalRanges[i];

                    if (value >= range.Min && value <= range.Max)
                    {
                        inRange = true;
                        break;
                    }
                }

                if (!inRange)
                {
                    // Try fallback:
                    if (Fallback == null)
                    {
                        return("");
                    }

                    return(Fallback.Get(value, prefixed));
                }
            }

            bool negative = false;

            if (value < 0)
            {
                // Ignore the negative:
                value    = -value;
                negative = true;
            }

            // Get it:
            string result = GetPositive(value);

            if (result == null)
            {
                // Try fallback:
                if (Fallback == null)
                {
                    return("");
                }

                return(Fallback.Get(value, prefixed));
            }

            if (result.Length < PadMin)
            {
                // Pad it:
                for (int i = result.Length; i < PadMin; i++)
                {
                    // Prepend:
                    result = PadSymbol + result;
                }
            }

            if (negative)
            {
                // Add the negative pref/suf:
                result = NegativePrefix + result + NegativeSuffix;
            }

            if (!prefixed)
            {
                // Don't add prefix/ suffix.
                return(result);
            }

            return(Prefix + result + Suffix);
        }