Ejemplo n.º 1
0
        private Glyph GetGlyph(char character)
        {
            Glyph glyph;

            if (!glyphs.TryGetValue(character, out glyph))
            {
                // if we have a loaded ttf try to create the glyph data
                if (_ofTypeface != null)
                {
                    var storage    = new VertexStorage();
                    var translator = new VertexSourceGlyphTranslator(storage);
                    var glyphIndex = _ofTypeface.LookupIndex(character);
                    var ttfGlyph   = _ofTypeface.GetGlyphByIndex(glyphIndex);
                    //
                    Typography.OpenFont.IGlyphReaderExtensions.Read(translator, ttfGlyph.GlyphPoints, ttfGlyph.EndPoints);

                    //
                    glyph             = new Glyph();
                    glyph.unicode     = character;
                    glyph.horiz_adv_x = _ofTypeface.GetHAdvanceWidthFromGlyphIndex(glyphIndex);

                    glyphs.Add(character, glyph);

                    // Wrap glyph data with ClosedLoopGlyphData to ensure all loops are correctly closed
                    glyph.glyphData = new ClosedLoopGlyphData(storage);
                }
            }

            return(glyph);
        }
Ejemplo n.º 2
0
        internal IVertexSource GetGlyphForCharacter(char character)
        {
            if (_ofTypeface != null)
            {
                // TODO: MAKE SURE THIS IS OFF!!!!!!! It is un-needed and only for debugging
                //glyphs.Clear();
            }

            // TODO: check for multi character glyphs (we don't currently support them in the reader).
            Glyph glyph = null;

            if (!glyphs.TryGetValue(character, out glyph))
            {
                // if we have a loaded ttf try to create the glyph data
                if (_ofTypeface != null)
                {
                    var storage    = new VertexStorage();
                    var translator = new VertexSourceGlyphTranslator(storage);
                    var glyphIndex = _ofTypeface.LookupIndex(character);
                    var ttfGlyph   = _ofTypeface.GetGlyphByIndex(glyphIndex);
                    //
                    Typography.OpenFont.IGlyphReaderExtensions.Read(translator, ttfGlyph.GlyphPoints, ttfGlyph.EndPoints);

                    //
                    glyph             = new Glyph();
                    glyph.unicode     = character;
                    glyph.horiz_adv_x = _ofTypeface.GetHAdvanceWidthFromGlyphIndex(glyphIndex);

                    glyphs.Add(character, glyph);

                    // Wrap glyph data with ClosedLoopGlyphData to ensure all loops are correctly closed
                    glyph.glyphData = new ClosedLoopGlyphData(storage);
                }
            }

            return(glyph?.glyphData);
        }
Ejemplo n.º 3
0
        private void cmdMeasureTextSpan_Click(object sender, System.EventArgs e)
        {
            //set some Gdi+ props...
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.White);
            //credit:
            //http://stackoverflow.com/questions/1485745/flip-coordinates-when-drawing-to-control
            g.ScaleTransform(1.0F, -1.0F);           // Flip the Y-Axis
            g.TranslateTransform(0.0F, -(float)300); // Translate the drawing area accordingly

            //--------------------------------
            //textspan measurement sample
            //--------------------------------
            _currentTextPrinter.HintTechnique     = (HintTechnique)lstHintList.SelectedItem;
            _currentTextPrinter.PositionTechnique = (PositionTechnique)cmbPositionTech.SelectedItem;
            //render at specific pos
            float x_pos = 0, y_pos = 100;

            char[] textBuffer = txtInputChar.Text.ToCharArray();

            //Example 1: this is a basic draw sample
            _currentTextPrinter.FillColor      = Color.Black;
            _currentTextPrinter.TargetGraphics = g;
            _currentTextPrinter.DrawString(
                textBuffer,
                0,
                textBuffer.Length,
                x_pos,
                y_pos
                );
            //
            //--------------------------------------------------
            //Example 2: print glyph plan to 'user' list-> then draw it (or hold it/ not draw)
            //you can create you own class to hold userGlyphPlans.***
            //2.1

            _reusableUnscaledGlyphPlanList.Clear();
            _currentTextPrinter.GenerateGlyphPlan(textBuffer, 0, textBuffer.Length, _reusableUnscaledGlyphPlanList);
            //2.2
            //and we can print the formatted glyph plan later.
            y_pos -= _currentTextPrinter.FontLineSpacingPx;
            _currentTextPrinter.FillColor = Color.Red;

            _currentTextPrinter.DrawFromGlyphPlans(
                new GlyphPlanSequence(_reusableUnscaledGlyphPlanList),
                x_pos,
                y_pos
                );

            //Example 3: MeasureString

            Typography.OpenFont.Typeface typeface = _currentTextPrinter.Typeface;

            UnscaledGlyphPlanList userGlyphPlans = new UnscaledGlyphPlanList();

            _currentTextPrinter.GlyphLayoutMan.GenerateUnscaledGlyphPlans(userGlyphPlans);

            MeasuredStringBox strBox = new MeasuredStringBox();

            throw new System.NotSupportedException();

            //_currentTextPrinter.GlyphLayoutMan.LayoutAndMeasureString(
            //  textBuffer, 0, textBuffer.Length,
            //  _currentTextPrinter.FontSizeInPoints,
            //  true,
            //  userGlyphPlans);

            float x_pos2 = x_pos + strBox.width + 10;

            g.DrawRectangle(Pens.Red, x_pos, y_pos + strBox.descending, strBox.width, strBox.CalculateLineHeight());
            g.DrawLine(Pens.Blue, x_pos, y_pos, x_pos2, y_pos);                                          //baseline
            g.DrawLine(Pens.Green, x_pos, y_pos + strBox.descending, x_pos2, y_pos + strBox.descending); //descending
            g.DrawLine(Pens.Magenta, x_pos, y_pos + strBox.ascending, x_pos2, y_pos + strBox.ascending); //ascending


            Typography.OpenFont.TypefaceExtension2.UpdateAllCffGlyphBounds(typeface);
            float pxscale = typeface.CalculateScaleToPixelFromPointSize(_currentTextPrinter.FontSizeInPoints);

            int j = userGlyphPlans.Count;

            for (int i = 0; i < j; ++i)
            {
                UnscaledGlyphPlan         glyphPlan = userGlyphPlans[i];
                Typography.OpenFont.Glyph glyph     = typeface.GetGlyphByIndex(glyphPlan.glyphIndex);
                //
                Typography.OpenFont.Bounds b = glyph.Bounds;
                //
                float xmin = b.XMin * pxscale;
                float ymin = b.YMin * pxscale;
                //
                float xmax = b.XMax * pxscale;
                float ymax = b.YMax * pxscale;
                //
                float glyph_x = x_pos + glyphPlan.OffsetX;
                g.DrawRectangle(Pens.Red, glyph_x + xmin, y_pos + ymin, xmax - xmin, ymax - ymin);
            }


            //------------
            _currentTextPrinter.FillColor = Color.Black;
            //transform back
            g.ScaleTransform(1.0F, -1.0F);           // Flip the Y-Axis
            g.TranslateTransform(0.0F, -(float)300); // Translate the drawing area accordingly
        }