ThousandthOfEmRealDoubles.cs 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//
// 
//
// Description: ThousandthOfEmRealDoubles class
//
//

using System;
using System.Diagnostics;
using System.Collections.Generic;

using System.Windows;

using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;

namespace MS.Internal.TextFormatting
{
    /// <summary>
    /// This is a fixed-size implementation of IList&lt;double&gt;. It is aimed to reduce the double values storage 
    /// while providing enough precision for glyph run operations. Current usage pattern suggests that there is no
    /// need to support resizing functionality (i.e. Add(), Insert(), Remove(), RemoveAt()).
    ///  
    /// For each double being stored, it will try to scale the value to 16-bit integer expressed in 1/1000th of 
    /// the given Em size. The scale will only be done if the precision remains no less than 1/2000th of an inch.
    /// 
    /// There are two scenarios where the given double value can not be scaled to 16-bit integer:
    /// o The given Em size is so big such that 1/1000th of it is not precise enough. 
    /// o The given double value is so big such that the scaled value cannot be fit into a short. 
    /// 
    /// If either of these cases happens (expected to happen rarely), this array implementation will fall back to store all 
    /// values as double. 
    /// </summary>
    internal sealed class ThousandthOfEmRealDoubles : IList<double>
    {
        //----------------------------------
        // Constructor
        //----------------------------------
        internal ThousandthOfEmRealDoubles(
            double emSize,
            int    capacity
            )
        {
            Debug.Assert(capacity >= 0);
            _emSize = emSize;
            InitArrays(capacity);
        }
        
        internal ThousandthOfEmRealDoubles(
            double        emSize,
            IList<double> realValues
            )
        {
            Debug.Assert(realValues != null);
            _emSize = emSize;            
            InitArrays(realValues.Count);            

            // do the setting
            for (int i = 0; i < Count; i++)
            {
                this[i] = realValues[i];
            }
}

        //-------------------------------------
        // Internal properties
        //-------------------------------------
        public int Count
        {
            get
            {
                if (_shortList != null)
                {
                    return _shortList.Length;
                }
                else
                {
                    return _doubleList.Length;
                }
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }        

        public double this[int index]
        {
            get
            {
                // Let underlying array do boundary check
                if (_shortList != null)
                {                    
                    return ThousandthOfEmToReal(_shortList[index]);
                }
                else
                {
                    return _doubleList[index];
                }
            }

            set
            {
                // Let underlying array do boundary check
                if (_shortList != null)
                {
                    short sValue;
                    if (RealToThousandthOfEm(value, out sValue))
                    {
                        _shortList[index] = sValue;
                    }
                    else
                    {
                        // The input double can't be scaled. We will 
                        // fall back to use double[] now                        
                        _doubleList = new double[_shortList.Length];
                        for (int i = 0; i < _shortList.Length; i++)
                        {
                            _doubleList[i] = ThousandthOfEmToReal(_shortList[i]);
                        }

                        _doubleList[index] = value; // set the current value
                        _shortList = null;          // deprecate the short array from now on
                    }
                }
                else
                {
                    _doubleList[index] = value; // we are using double array 
                }
            }
        }

        //------------------------------------
        // internal methods
        //------------------------------------
        public int IndexOf(double item)
        {
            // linear search 
            for (int i = 0; i < Count; i++)
            {
                if (this[i] == item)
                {
                    return i;
                }
            }            
            
            return -1;
        }

        public void Clear()
        {
            // zero the stored values
            if (_shortList != null)
            {
                for (int i = 0; i < _shortList.Length; i++)
                {
                    _shortList[i] = 0;
                }
            }
            else
            {
                for (int i = 0; i < _doubleList.Length; i++)
                {
                    _doubleList[i] = 0;
                }
            }
        }

        public bool Contains(double item)
        {
            return IndexOf(item) >= 0;
        }

        public void CopyTo(double[] array, int arrayIndex)
        {            
            // parameter validations
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }

            if (array.Rank != 1)
            {
                throw new ArgumentException(
                    SR.Get(SRID.Collection_CopyTo_ArrayCannotBeMultidimensional), 
                    "array");                
            }

            if (arrayIndex < 0)
            {
                throw new ArgumentOutOfRangeException("arrayIndex");
            }

            if (arrayIndex >= array.Length)
            {
                throw new ArgumentException(
                    SR.Get(
                        SRID.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, 
                        "arrayIndex", 
                        "array"),
                    "arrayIndex");
            }

            if ((array.Length - Count - arrayIndex) < 0)
            {
                throw new ArgumentException(
                    SR.Get(
                        SRID.Collection_CopyTo_NumberOfElementsExceedsArrayLength,
                        "arrayIndex",
                        "array"));
            }           
            

            // do the copying here
            for (int i = 0; i < Count; i++)
            {
                array[arrayIndex + i] = this[i];
            }
        }

        public IEnumerator<double> GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return this[i];
            }
        }        

	System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<double>)this).GetEnumerator();
        }


        public void Add(double value)
        {
            // not supported, same as double[] 
            throw new NotSupportedException(SR.Get(SRID.CollectionIsFixedSize));                           
        }

        public void Insert(int index, double item)
        {
            // not supported, same as double[] 
            throw new NotSupportedException(SR.Get(SRID.CollectionIsFixedSize));                           
        }

        public bool Remove(double item)
        {
            // not supported, same as double[]             
            throw new NotSupportedException(SR.Get(SRID.CollectionIsFixedSize));                           
        }

        public void RemoveAt(int index)
        {
            // not supported, same as double[]             
            throw new NotSupportedException(SR.Get(SRID.CollectionIsFixedSize));                           
        }

        //---------------------------------------------
        // Private methods
        //---------------------------------------------       
        private void InitArrays(int capacity)
        {
            if (_emSize > CutOffEmSize)
            {
                // use double storage when emsize is too big
                _doubleList = new double[capacity];
            }
            else
            {
                // store value as scaled short.
                _shortList = new short[capacity];
            }            
        }

        private bool RealToThousandthOfEm(double value, out short thousandthOfEm)
        {
            double scaled = (value / _emSize) * ToThousandthOfEm;
            
            if (scaled > short.MaxValue || scaled < short.MinValue)
            {
                // value too big to fit into a short
                thousandthOfEm = 0;
                return false;
            }
            else
            {
                // round to nearest short
                thousandthOfEm = (short) Math.Round(scaled);
                return true;
            }
        }

        private double ThousandthOfEmToReal(short thousandthOfEm)
        {
            return ((double)thousandthOfEm) * ToReal * _emSize;
        }        

        //----------------------------------------
        // Private members
        //----------------------------------------
        private short[]  _shortList;  // scaled short values
        private double[] _doubleList; // fall-back double list, is null for most cases
        private double   _emSize;     // em size to scaled with

        // Default scaling is 1/1000 emsize.         
        private const double ToThousandthOfEm = 1000.0;
        private const double ToReal           = 1.0 / ToThousandthOfEm;

        // To achieve precsion of no less than 1/2000 of an inch, font Em size must be no greater than 48. 
        // i.e. 48px is 1/2 inch. 1000th of Em size at 48px is 1/2000 inch.
        private const double CutOffEmSize = 48;         
}    
}