Example #1
0
        /// <summary>
        /// Gets OUTLINETEXTMETRICW structure
        /// </summary>
        /// <returns>Outline text metric class</returns>
        public WinOutlineTextMetric GetOutlineTextMetricsApi()
        {
            // get buffer size
            Int32 BufSize = GetOutlineTextMetrics(GDIHandle, 0, IntPtr.Zero);

            if (BufSize == 0)
            {
                ThrowSystemErrorException("Calling GetOutlineTextMetrics (get buffer size) failed");
            }

            // allocate buffer to receive outline text metrics information
            AllocateBuffer(BufSize);

            // get outline text metrics information
            if (GetOutlineTextMetrics(GDIHandle, BufSize, Buffer) == 0)
            {
                ThrowSystemErrorException("Calling GetOutlineTextMetrics failed");
            }

            // create WinOutlineTextMetric class
            WinOutlineTextMetric WOTM = new WinOutlineTextMetric(this);

            // free buffer for outline text metrics
            FreeBuffer();

            // exit
            return(WOTM);
        }
        ////////////////////////////////////////////////////////////////////
        // Constructor
        ////////////////////////////////////////////////////////////////////

        public PdfFont
        (
            PdfDocument Document,                                       // PDF document main object
            String FontFamilyName,                                      // font family name
            FontStyle FontStyle,                                        // font style (Regular, Bold, Italic or Bold | Italic
            Boolean EmbeddedFont                                        // embed font in PDF document file
        ) : base(Document, false, "/Font")
        {
            // save embedded font flag
            this.EmbeddedFont = EmbeddedFont;

            // font style cannot be underline or strikeout
            if ((FontStyle & (FontStyle.Underline | FontStyle.Strikeout)) != 0)
            {
                throw new ApplicationException("Font resource cannot have underline or strikeout");
            }

            // create resource code
            ResourceCode = Document.GenerateResourceNumber('F');

            // get font family structure
            FontFamily = new FontFamily(FontFamilyName);

            // test font style availability
            if (!FontFamily.IsStyleAvailable(FontStyle))
            {
                throw new ApplicationException("Font style not available for font family");
            }

            // design height
            DesignHeight = FontFamily.GetEmHeight(FontStyle);

            // Ascent, descent and line spacing for a one point font
            PdfAscent      = WindowsToPdf(FontFamily.GetCellAscent(FontStyle));
            PdfDescent     = WindowsToPdf(FontFamily.GetCellDescent(FontStyle)); // positive number
            PdfLineSpacing = WindowsToPdf(FontFamily.GetLineSpacing(FontStyle));

            // create design font
            DesignFont = new Font(FontFamily, DesignHeight, FontStyle, GraphicsUnit.Pixel);

            // create windows sdk font info object
            FontInfo = new FontApi(DesignFont, DesignHeight);

            // get character width array
            CharWidthArray = FontInfo.GetCharWidthApi(0, 255);

            // get array of glyph bounding box and width
            GlyphArray = FontInfo.GetGlyphMetricsApi(0, 255);

            // reset active (used) characters to not used
            ActiveChar = new Boolean[256];

            // get outline text metrics structure
            WinOutlineTextMetric OTM = FontInfo.GetOutlineTextMetricsApi();

            // license
            if ((OTM.otmfsType & 2) != 0)
            {
                throw new ApplicationException("Font " + FontFamilyName + " is not licensed for embedding");
            }

            // make sure we have true type font and not device font
            if ((OTM.otmTextMetric.tmPitchAndFamily & 0xe) != 6)
            {
                throw new ApplicationException("Font must be True Type and vector");
            }

            // PDF font flags
            FontFlags = 0;
            if ((OTM.otmfsSelection & 1) != 0)
            {
                FontFlags |= PdfFontFlags.Italic;
            }
            // roman font is a serif font
            if ((OTM.otmTextMetric.tmPitchAndFamily >> 4) == 1)
            {
                FontFlags |= PdfFontFlags.Serif;
            }
            if ((OTM.otmTextMetric.tmPitchAndFamily >> 4) == 4)
            {
                FontFlags |= PdfFontFlags.Script;
            }
            // #define SYMBOL_CHARSET 2
            if (OTM.otmTextMetric.tmCharSet == 2)
            {
                FontFlags   |= PdfFontFlags.Symbolic;
                SymbolicFont = true;
            }
            else
            {
                FontFlags   |= PdfFontFlags.Nonsymbolic;
                SymbolicFont = false;
            }

            // #define TMPF_FIXED_PITCH 0x01 (Note very carefully that those meanings are the opposite of what the constant name implies.)
            if ((OTM.otmTextMetric.tmPitchAndFamily & 1) == 0)
            {
                FontFlags |= PdfFontFlags.FixedPitch;
            }

            // strikeout
            PdfStrikeoutPosition = WindowsToPdf(OTM.otmsStrikeoutPosition);
            PdfStrikeoutWidth    = WindowsToPdf((Int32)OTM.otmsStrikeoutSize);

            // underline
            PdfUnderlinePosition = WindowsToPdf(OTM.otmsUnderscorePosition);
            PdfUnderlineWidth    = WindowsToPdf(OTM.otmsUnderscoreSize);

            // subscript
            PdfSubscriptSize     = WindowsToPdf(OTM.otmptSubscriptSize.Y);
            PdfSubscriptPosition = WindowsToPdf(OTM.otmptSubscriptOffset.Y);

            // superscript
            PdfSuperscriptSize     = WindowsToPdf(OTM.otmptSuperscriptSize.Y);
            PdfSuperscriptPosition = WindowsToPdf(OTM.otmptSuperscriptOffset.Y);

            PdfItalicAngle = (Double)OTM.otmItalicAngle / 10.0;
            PdfFontWeight  = OTM.otmTextMetric.tmWeight;

            // exit
            return;
        }