Ejemplo n.º 1
0
        /// <summary>
        /// Allows for customised or precalculated ascent mesurements to be passed, fastest way to draw a string
        /// </summary>
        /// <param name="g"></param>
        /// <param name="pos"></param>
        /// <param name="fontTable"></param>
        /// <param name="mesurements"></param>
        public void drawString(Graphics g, PointF pos, FontFamily[] fontTable, lineInfo mesurements)
        {
            int    i, j;
            string printString;
            Font   printFont;

            SizeF charSize;
            //float across=pos.X;
            RTFChar fc;
            PointF  charPos = pos;

            if (data == null)
            {
                return;
            }

            for (i = 0; i < data.Length; i++)
            {
                fc          = data[i];
                printString = fc.Char.ToString();

                //adjust character position to meat the baseline
                charPos.Y = pos.Y + mesurements.baseline - mesurements.ascents[i];

                //apend any characters drawn in the same style
                for (j = (i + 1); (j < data.Length) && (fc.compareFormat(data[j])); j++)
                {
                    printString += data[j].Char;
                }

                //update i to the end of the common string
                i = j - 1;

                //cache the print font
                printFont = fc.getFont(fontTable);

                //charSize = fc.measure(g, fontTable);
                charSize = g.MeasureString(printString, printFont, 999, RTFLine.defalutFormat());

                //draw the string
                g.DrawString(printString, printFont, new SolidBrush(fc.col), charPos, RTFLine.defalutFormat());

                charPos.X += charSize.Width;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// measure the dimensions of the line
        /// </summary>
        /// <param name="g">Graphics context</param>
        /// <param name="fontTable">Table of fonts</param>
        /// <returns>lineInfo struct with information on the line.</returns>
        public lineInfo measure(Graphics g, FontFamily[] fontTable)
        {
            int      i;
            lineInfo info;

            info.mesurement = new SizeF(0.0f, 0.0f);
            info.baseline   = 0;
            info.ascents    = new float[0];
            SizeF charSize;
            float ascent;

            if (data == null)
            {
                return(info);
            }

            info.ascents = new float[data.Length];

            string  printString;
            Font    printFont;
            RTFChar fc;
            int     j;

            for (i = 0; i < data.Length; i++)
            {
                fc          = data[i];
                printString = fc.Char.ToString();

                //apend any characters drawn in the same style
                for (j = (i + 1); (j < data.Length) && (fc.compareFormat(data[j])); j++)
                {
                    printString += data[j].Char;
                }

                printFont = fc.getFont(fontTable);
                charSize  = g.MeasureString(printString, printFont, 999, RTFLine.defalutFormat());

                ascent = fontTable[data[i].fontIndex].GetCellAscent(data[i].FontStyle);
                ascent = data[i].size * (ascent / fontTable[data[i].fontIndex].GetEmHeight(data[i].FontStyle)) * 1.3f;

                if (ascent > info.baseline)
                {
                    info.baseline = ascent;
                }

                info.ascents[i] = ascent;
                for (int j2 = i; j2 < j; j2++)
                {
                    info.ascents[j2] = ascent;
                }
                //update i to the end of the common string
                i = j - 1;

                //printFont = fc.getFont(fontTable);
                //charSize = g.MeasureString(printString, printFont,999,formattedLine.defalutFormat());
                info.mesurement.Width += charSize.Width;

                if (charSize.Height > info.mesurement.Height)
                {
                    info.mesurement.Height = charSize.Height;
                }
            }

            /*
             * for(i=0;i<data.Length;i++)
             *      {
             *      //set the height to the maximum character height
             *
             *      charSize = data[i].measure(g, fontTable);
             *      if(charSize.Height > info.mesurement.Height)
             *              {
             *              info.mesurement.Height = charSize.Height;
             *              }
             *
             *      //calculate the acsent of the character
             *      ascent = fontTable[data[i].fontIndex].GetCellAscent(data[i].FontStyle);
             *
             *      //convert to pixels
             *      //ascent = data[i].size * (ascent / fontTable[data[i].fontIndex].GetEmHeight(data[i].FontStyle));
             *      //ascent = data[i].size / fontTable[data[i].fontIndex].GetEmHeight(data[i].FontStyle) * ascent;
             *      ascent = data[i].size * (ascent / fontTable[data[i].fontIndex].GetEmHeight(data[i].FontStyle))*1.3f;
             *
             *      //cache this in the mesurements for later use when rendering the string
             *      info.ascents[i] = ascent;
             *
             *      //baseline might not be related to the biggest font, but the one with the most ascent
             *      if(ascent > info.baseline)
             *              {
             *              info.baseline = ascent;
             *              }
             *      info.mesurement.Width += charSize.Width;
             *      }
             */

            return(info);
        }