Esempio n. 1
0
        void RenderAndShowMeasureBox()
        {
            bool flipY = chkFlipY.Checked;



            //set some Gdi+ props...
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.White);

            Typography.OpenFont.Typeface typeface = _currentTextPrinter.Typeface;
            Typography.OpenFont.TypefaceExtension2.UpdateAllCffGlyphBounds(typeface);


            float pxscale     = typeface.CalculateScaleToPixelFromPointSize(_currentTextPrinter.FontSizeInPoints);
            int   lineSpacing = (int)System.Math.Ceiling((double)typeface.CalculateLineSpacing(LineSpacingChoice.TypoMetric) * pxscale);


            if (flipY)
            {
                //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, -500); // Translate the drawing area accordingly
            }


            //--------------------------------
            //textspan measurement sample
            //--------------------------------
            _currentTextPrinter.HintTechnique     = (HintTechnique)lstHintList.SelectedItem;
            _currentTextPrinter.PositionTechnique = (PositionTechnique)cmbPositionTech.SelectedItem;
            _currentTextPrinter.UpdateGlyphLayoutSettings();

            //render at specific pos
            float x_pos = 0, y_pos = lineSpacing * 2;

            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 -= lineSpacing;//next line
            _currentTextPrinter.FillColor = Color.Red;

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

            //Example 3: MeasureString
            UnscaledGlyphPlanList glyphPlans = new UnscaledGlyphPlanList();

            _currentTextPrinter.GlyphLayoutMan.GenerateUnscaledGlyphPlans(glyphPlans);
            MeasuredStringBox strBox = _currentTextPrinter.GlyphLayoutMan.LayoutAndMeasureString(
                textBuffer, 0, textBuffer.Length,
                _currentTextPrinter.FontSizeInPoints);

            int   j           = glyphPlans.Count;
            float backup_xpos = x_pos;

            for (int i = 0; i < j; ++i)
            {
                UnscaledGlyphPlan         glyphPlan = glyphPlans[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);
                x_pos += glyphPlan.AdvanceX * pxscale;
            }

            x_pos = backup_xpos;

            g.FillRectangle(Brushes.Red, new RectangleF(0, 0, 5, 5));//reference point(0,0)
            g.FillRectangle(Brushes.Green, new RectangleF(x_pos, y_pos, 3, 3));


            float x_pos2 = x_pos + strBox.width + 10;


            g.DrawRectangle(Pens.Black, x_pos, y_pos + strBox.DescendingInPx, strBox.width, strBox.ClipHeightInPx);
            g.DrawRectangle(Pens.Red, x_pos, y_pos + strBox.DescendingInPx, strBox.width, strBox.LineSpaceInPx);

            g.DrawLine(Pens.Blue, x_pos, y_pos, x_pos2, y_pos);                                                  //baseline
            g.DrawLine(Pens.Green, x_pos, y_pos + strBox.DescendingInPx, x_pos2, y_pos + strBox.DescendingInPx); //descending
            g.DrawLine(Pens.Magenta, x_pos, y_pos + strBox.AscendingInPx, x_pos2, y_pos + strBox.AscendingInPx); //ascending


            ////------------
            ////draw another line (for reference)
            y_pos -= lineSpacing;//next line


            _currentTextPrinter.FillColor = Color.Black;

            _currentTextPrinter.DrawFromGlyphPlans(
                new GlyphPlanSequence(_reusableUnscaledGlyphPlanList),
                x_pos,
                y_pos
                );
            //transform back
            if (flipY)
            {
                g.ScaleTransform(1.0F, -1.0F);    // Flip the Y-Axis
                g.TranslateTransform(0.0F, -500); // Translate the drawing area accordingly
            }

            //---------

            //txtMsgInfo.Text = "choice:" + choice.ToString() + "=" + lineSpacing.ToString();
        }
 public static bool DoesSupportUnicode(
     this Typeface typeface,
     BitposAndAssciatedUnicodeRanges bitposAndAssocUnicodeRange)
 {
     return(DoesSupportUnicode(typeface.Languages, bitposAndAssocUnicodeRange.Bitpos));
 }
Esempio n. 3
0
        void RenderAndShowMeasureBox()
        {
            bool flipY = chkFlipY.Checked;


            //set some Gdi+ props...
            _g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            _g.Clear(Color.White);

            Typography.OpenFont.Typeface typeface = _currentTextPrinter.Typeface;
            Typography.OpenFont.Extensions.TypefaceExtensions.UpdateAllCffGlyphBounds(typeface);


            float pxscale     = typeface.CalculateScaleToPixelFromPointSize(_currentTextPrinter.FontSizeInPoints);
            int   lineSpacing = (int)System.Math.Ceiling((double)typeface.CalculateLineSpacing(LineSpacingChoice.TypoMetric) * pxscale);


            if (flipY)
            {
                //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, -500); // Translate the drawing area accordingly
            }


            //--------------------------------
            //textspan measurement sample
            //--------------------------------
            _currentTextPrinter.HintTechnique     = (HintTechnique)lstHintList.SelectedItem;
            _currentTextPrinter.PositionTechnique = (PositionTechnique)cmbPositionTech.SelectedItem;
            _currentTextPrinter.UpdateGlyphLayoutSettings();

            //render at specific pos
            float x_pos = 0, y_pos = lineSpacing * 2;

            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
            if (chkEnableMultiTypefaces.Checked)
            {
                _ftmGlyphPlansList.Clear();
                _currentTextPrinter.GenerateGlyphPlans(textBuffer, 0, textBuffer.Length, _ftmGlyphPlansList);
                //2.2
                //and we can print the formatted glyph plan later.
                y_pos -= lineSpacing;//next line
                _currentTextPrinter.FillColor = Color.Red;

                _currentTextPrinter.DrawFromFormattedGlyphPlans(
                    _ftmGlyphPlansList,
                    x_pos,
                    y_pos
                    );


                //--------------------------------------------------
                //Example 3: MeasureString
                //3.1 use data in formatted_glyph_plan_list to measure string => OK, OR
                //3.2 use another 'light-weight' text printer to measure string (without drawing actual string)

                //3.1
                _currentTextPrinter.MeasureGlyphPlanList(_ftmGlyphPlansList, out int measureWidth);
                _g.DrawRectangle(Pens.Red, x_pos, y_pos, measureWidth, 30);


                float baseline = y_pos;

                //3.2 draw each glyph bounds
                {
                    int m = _ftmGlyphPlansList.Count;

                    for (int i = 0; i < m; ++i)
                    {
                        FormattedGlyphPlanSeq seq          = _ftmGlyphPlansList[i];
                        ResolvedFont          resolvedFont = seq.ResolvedFont;
                        Typeface localTypeface             = resolvedFont.Typeface;

                        pxscale = resolvedFont.GetScaleToPixelFromPointInSize();
                        GlyphPlanSequence glyph_seq = seq.Seq;
                        var snapToPxScale           = new GlyphPlanSequenceSnapPixelScaleLayout(seq.Seq, 0, glyph_seq.Count, pxscale);


                        x_pos += (resolvedFont.WhitespaceWidth * seq.PrefixWhitespaceCount);

                        int nn = 0;///
                        while (snapToPxScale.Read())
                        {
                            //float cx = (float)Math.Round(snapToPxScale.ExactX + x_pos);
                            //float cy = (float)Math.Floor(snapToPxScale.ExactY + baseline);

                            UnscaledGlyphPlan glyphPlan = glyph_seq[nn];
                            Glyph             glyph     = localTypeface.GetGlyph(glyphPlan.glyphIndex);

                            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 * pxscale);
                            _g.DrawRectangle(Pens.Red, glyph_x + xmin, y_pos + ymin, xmax - xmin, ymax - ymin);

                            x_pos += glyphPlan.AdvanceX * pxscale;

                            nn++;
                        }

                        x_pos += (resolvedFont.WhitespaceWidth * seq.PostfixWhitespaceCount);
                    }
                }

                _ftmGlyphPlansList.Clear();
            }
            else
            {
                _reusableUnscaledGlyphPlanList.Clear();
                _currentTextPrinter.GenerateGlyphPlans(textBuffer, 0, textBuffer.Length, _reusableUnscaledGlyphPlanList);
                //2.2
                //and we can print the formatted glyph plan later.
                y_pos -= lineSpacing;//next line
                _currentTextPrinter.FillColor = Color.Red;


                //
                GlyphPlanSequence seq = new GlyphPlanSequence(_reusableUnscaledGlyphPlanList);

                _currentTextPrinter.DrawFromGlyphPlans(
                    seq,
                    x_pos,
                    y_pos
                    );

                //--------------------------------------------------
                //Example 3: MeasureString

                float descending_px    = _currentTextPrinter.FontDescedingPx;
                float ascending_px     = _currentTextPrinter.FontAscendingPx;
                float lineSpacing_px   = _currentTextPrinter.FontLineSpacingPx;
                float clippedHeight_px = _currentTextPrinter.FontClipHeightPx;

                _currentTextPrinter.MeasureGlyphPlanSeq(seq, out int textspanW);

                UnscaledGlyphPlanList glyphPlans = new UnscaledGlyphPlanList();
                _currentTextPrinter.GenerateGlyphPlans(textBuffer, 0, textBuffer.Length, glyphPlans);


                int   j           = glyphPlans.Count;
                float backup_xpos = x_pos;
                for (int i = 0; i < j; ++i)
                {
                    UnscaledGlyphPlan glyphPlan = glyphPlans[i];
                    Glyph             glyph     = typeface.GetGlyph(glyphPlan.glyphIndex);
                    //
                    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 * pxscale);
                    _g.DrawRectangle(Pens.Red, glyph_x + xmin, y_pos + ymin, xmax - xmin, ymax - ymin);
                    x_pos += glyphPlan.AdvanceX * pxscale;
                }

                x_pos = backup_xpos;

                _g.FillRectangle(Brushes.Red, new RectangleF(0, 0, 5, 5));//reference point(0,0)
                _g.FillRectangle(Brushes.Green, new RectangleF(x_pos, y_pos, 3, 3));


                float x_pos2 = x_pos + textspanW + 10;


                _g.DrawRectangle(Pens.Black, x_pos, y_pos + descending_px, textspanW, clippedHeight_px);
                _g.DrawRectangle(Pens.Red, x_pos, y_pos + descending_px, textspanW, lineSpacing_px);

                _g.DrawLine(Pens.Blue, x_pos, y_pos, x_pos2, y_pos);                                  //baseline
                _g.DrawLine(Pens.Green, x_pos, y_pos + descending_px, x_pos2, y_pos + descending_px); //descending
                _g.DrawLine(Pens.Magenta, x_pos, y_pos + ascending_px, x_pos2, y_pos + ascending_px); //ascending


                ////------------
                ////draw another line (for reference)
                y_pos -= lineSpacing;//next line


                _currentTextPrinter.FillColor = Color.Black;

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



            //transform back
            if (flipY)
            {
                _g.ScaleTransform(1.0F, -1.0F);    // Flip the Y-Axis
                _g.TranslateTransform(0.0F, -500); // Translate the drawing area accordingly
            }

            //---------

            //txtMsgInfo.Text = "choice:" + choice.ToString() + "=" + lineSpacing.ToString();
        }
Esempio n. 4
0
 public static bool HasSvgTable(this Typeface typeface)
 {
     return(typeface._svgTable != null);
 }
        internal Typeface ReadTableEntryCollection(TableEntryCollection tables, BinaryReader input)
        {
            OS2Table  os2Table  = ReadTableIfExists(tables, input, new OS2Table());
            NameEntry nameEntry = ReadTableIfExists(tables, input, new NameEntry());

            Head              header            = ReadTableIfExists(tables, input, new Head());
            MaxProfile        maximumProfile    = ReadTableIfExists(tables, input, new MaxProfile());
            HorizontalHeader  horizontalHeader  = ReadTableIfExists(tables, input, new HorizontalHeader());
            HorizontalMetrics horizontalMetrics = ReadTableIfExists(tables, input, new HorizontalMetrics(horizontalHeader.HorizontalMetricsCount, maximumProfile.GlyphCount));

            //---
            PostTable postTable = ReadTableIfExists(tables, input, new PostTable());
            CFFTable  ccf       = ReadTableIfExists(tables, input, new CFFTable());

            //--------------
            Cmap           cmaps          = ReadTableIfExists(tables, input, new Cmap());
            GlyphLocations glyphLocations = ReadTableIfExists(tables, input, new GlyphLocations(maximumProfile.GlyphCount, header.WideGlyphLocations));

            Glyf glyf = ReadTableIfExists(tables, input, new Glyf(glyphLocations));
            //--------------
            Gasp gaspTable             = ReadTableIfExists(tables, input, new Gasp());
            VerticalDeviceMetrics vdmx = ReadTableIfExists(tables, input, new VerticalDeviceMetrics());
            //--------------


            Kern kern = ReadTableIfExists(tables, input, new Kern()); //deprecated
            //--------------
            //advanced typography
            GDEF gdef      = ReadTableIfExists(tables, input, new GDEF());
            GSUB gsub      = ReadTableIfExists(tables, input, new GSUB());
            GPOS gpos      = ReadTableIfExists(tables, input, new GPOS());
            BASE baseTable = ReadTableIfExists(tables, input, new BASE());
            JSTF jstf      = ReadTableIfExists(tables, input, new JSTF());

            COLR           colr = ReadTableIfExists(tables, input, new COLR());
            CPAL           cpal = ReadTableIfExists(tables, input, new CPAL());
            VerticalHeader vhea = ReadTableIfExists(tables, input, new VerticalHeader());

            if (vhea != null)
            {
                VerticalMetrics vmtx = ReadTableIfExists(tables, input, new VerticalMetrics(vhea.NumOfLongVerMetrics));
            }



            //test math table
            MathTable mathtable    = ReadTableIfExists(tables, input, new MathTable());
            EBLCTable fontBmpTable = ReadTableIfExists(tables, input, new EBLCTable());
            //---------------------------------------------
            //about truetype instruction init

            //---------------------------------------------
            Typeface typeface            = null;
            bool     isPostScriptOutline = false;

            if (glyf == null)
            {
                //check if this is cff table ?
                if (ccf == null)
                {
                    //TODO: review here
                    throw new NotSupportedException();
                }
                //...
                //PostScript outline font
                isPostScriptOutline = true;
                typeface            = new Typeface(
                    nameEntry,
                    header.Bounds,
                    header.UnitsPerEm,
                    ccf,
                    horizontalMetrics,
                    os2Table);
            }
            else
            {
                typeface = new Typeface(
                    nameEntry,
                    header.Bounds,
                    header.UnitsPerEm,
                    glyf.Glyphs,
                    horizontalMetrics,
                    os2Table);
            }

            //----------------------------
            typeface.CmapTable  = cmaps;
            typeface.KernTable  = kern;
            typeface.GaspTable  = gaspTable;
            typeface.MaxProfile = maximumProfile;
            typeface.HheaTable  = horizontalHeader;
            //----------------------------

            if (!isPostScriptOutline)
            {
                FpgmTable fpgmTable = ReadTableIfExists(tables, input, new FpgmTable());
                //control values table
                CvtTable cvtTable = ReadTableIfExists(tables, input, new CvtTable());
                if (cvtTable != null)
                {
                    typeface.ControlValues = cvtTable._controlValues;
                }
                if (fpgmTable != null)
                {
                    typeface.FpgmProgramBuffer = fpgmTable._programBuffer;
                }
                PrepTable propProgramTable = ReadTableIfExists(tables, input, new PrepTable());
                if (propProgramTable != null)
                {
                    typeface.PrepProgramBuffer = propProgramTable._programBuffer;
                }
            }
            //-------------------------
            typeface.LoadOpenFontLayoutInfo(
                gdef,
                gsub,
                gpos,
                baseTable,
                colr,
                cpal);

            //------------


            //test
            {
                SvgTable svgTable = ReadTableIfExists(tables, input, new SvgTable());
                if (svgTable != null)
                {
                    typeface._svgTable = svgTable;
                }
            }

            typeface.PostTable = postTable;
            if (mathtable != null)
            {
                var mathGlyphLoader = new MathGlyphLoader();
                mathGlyphLoader.LoadMathGlyph(typeface, mathtable);
            }
#if DEBUG
            //test
            //int found = typeface.GetGlyphIndexByName("Uacute");
            if (typeface.IsCffFont)
            {
                //optional
                typeface.UpdateAllCffGlyphBounds();
            }
#endif
            return(typeface);
        }
Esempio n. 6
0
 public static bool HasMathTable(this Typeface typeface)
 {
     return(typeface.MathConsts != null);
 }
Esempio n. 7
0
        public Typeface Read(Stream stream, ReadFlags readFlags = ReadFlags.Full)
        {
            var little = BitConverter.IsLittleEndian;

            using (var input = new ByteOrderSwappingBinaryReader(stream))
            {
                ushort majorVersion  = input.ReadUInt16();
                ushort minorVersion  = input.ReadUInt16();
                ushort tableCount    = input.ReadUInt16();
                ushort searchRange   = input.ReadUInt16();
                ushort entrySelector = input.ReadUInt16();
                ushort rangeShift    = input.ReadUInt16();
                var    tables        = new TableEntryCollection();
                for (int i = 0; i < tableCount; i++)
                {
                    tables.AddEntry(new UnreadTableEntry(ReadTableHeader(input)));
                }
                //------------------------------------------------------------------
                OS2Table  os2Table  = ReadTableIfExists(tables, input, new OS2Table());
                NameEntry nameEntry = ReadTableIfExists(tables, input, new NameEntry());


                Head              header            = ReadTableIfExists(tables, input, new Head());
                MaxProfile        maximumProfile    = ReadTableIfExists(tables, input, new MaxProfile());
                HorizontalHeader  horizontalHeader  = ReadTableIfExists(tables, input, new HorizontalHeader());
                HorizontalMetrics horizontalMetrics = ReadTableIfExists(tables, input, new HorizontalMetrics(horizontalHeader.HorizontalMetricsCount, maximumProfile.GlyphCount));

                //--------------
                Cmap           cmaps          = ReadTableIfExists(tables, input, new Cmap());
                GlyphLocations glyphLocations = ReadTableIfExists(tables, input, new GlyphLocations(maximumProfile.GlyphCount, header.WideGlyphLocations));
                Glyf           glyf           = ReadTableIfExists(tables, input, new Glyf(glyphLocations));
                //--------------
                Gasp gaspTable             = ReadTableIfExists(tables, input, new Gasp());
                VerticalDeviceMatrics vdmx = ReadTableIfExists(tables, input, new VerticalDeviceMatrics());
                //--------------
                PostTable postTable = ReadTableIfExists(tables, input, new PostTable());
                Kern      kern      = ReadTableIfExists(tables, input, new Kern());
                //--------------
                //advanced typography
                GDEF           gdef      = ReadTableIfExists(tables, input, new GDEF());
                GSUB           gsub      = ReadTableIfExists(tables, input, new GSUB());
                GPOS           gpos      = ReadTableIfExists(tables, input, new GPOS());
                BASE           baseTable = ReadTableIfExists(tables, input, new BASE());
                VerticalHeader vhea      = ReadTableIfExists(tables, input, new VerticalHeader());
                if (vhea != null)
                {
                    VerticalMatric vmtx = ReadTableIfExists(tables, input, new VerticalMatric(vhea.NumOfLongVerMatrics));
                }

                EBLCTable fontBmpTable = ReadTableIfExists(tables, input, new EBLCTable());
                //---------------------------------------------
                //about truetype instruction init



                //---------------------------------------------
                var typeface = new Typeface(
                    nameEntry,
                    header.Bounds,
                    header.UnitsPerEm,
                    glyf.Glyphs,
                    cmaps.CharMaps,
                    horizontalMetrics,
                    os2Table);
                //----------------------------
                typeface.KernTable  = kern;
                typeface.GaspTable  = gaspTable;
                typeface.MaxProfile = maximumProfile;
                //----------------------------
                FpgmTable fpgmTable = ReadTableIfExists(tables, input, new FpgmTable());
                //control values table
                CvtTable cvtTable = ReadTableIfExists(tables, input, new CvtTable());
                if (cvtTable != null)
                {
                    typeface.ControlValues = cvtTable.controlValues;
                }
                if (fpgmTable != null)
                {
                    typeface.FpgmProgramBuffer = fpgmTable.programBuffer;
                }
                PrepTable propProgramTable = ReadTableIfExists(tables, input, new PrepTable());
                if (propProgramTable != null)
                {
                    typeface.PrepProgramBuffer = propProgramTable.programBuffer;
                }
                //-------------------------
                typeface.LoadOpenFontLayoutInfo(
                    gdef,
                    gsub,
                    gpos,
                    baseTable);
                return(typeface);
            }
        }
Esempio n. 8
0
        internal bool ReadTableEntryCollection(Typeface typeface, RestoreTicket ticket, TableEntryCollection tables, BinaryReader input)
        {
            if (ticket != null)
            {
                return(ReadTableEntryCollectionOnRestoreMode(typeface, ticket, tables, input));
            }

            typeface.SetTableEntryCollection(tables.CloneTableHeaders());

            var rd = new EntriesReaderHelper(tables, input);
            //PART 1: basic information
            OS2Table          os2Table          = rd.Read(new OS2Table());
            Meta              meta              = rd.Read(new Meta());
            NameEntry         nameEntry         = rd.Read(new NameEntry());
            Head              head              = rd.Read(new Head());
            MaxProfile        maxProfile        = rd.Read(new MaxProfile());
            HorizontalHeader  horizontalHeader  = rd.Read(new HorizontalHeader());
            HorizontalMetrics horizontalMetrics = rd.Read(new HorizontalMetrics(horizontalHeader.NumberOfHMetrics, maxProfile.GlyphCount));
            VerticalHeader    vhea              = rd.Read(new VerticalHeader());

            if (vhea != null)
            {
                VerticalMetrics vmtx = rd.Read(new VerticalMetrics(vhea.NumOfLongVerMetrics));
            }

            OS2FsSelection os2Select = new OS2FsSelection(os2Table.fsSelection);

            typeface._useTypographicMertic = os2Select.USE_TYPO_METRICS;

            Cmap cmaps = rd.Read(new Cmap());
            VerticalDeviceMetrics vdmx = rd.Read(new VerticalDeviceMetrics());
            Kern kern = rd.Read(new Kern());
            //------------------------------------
            //PART 2: glyphs detail
            //2.1 True type font

            GlyphLocations glyphLocations = rd.Read(new GlyphLocations(maxProfile.GlyphCount, head.WideGlyphLocations));
            Glyf           glyf           = rd.Read(new Glyf(glyphLocations));
            Gasp           gaspTable      = rd.Read(new Gasp());
            COLR           colr           = rd.Read(new COLR());
            CPAL           cpal           = rd.Read(new CPAL());

            //2.2 Cff font
            PostTable postTable = rd.Read(new PostTable());
            CFFTable  cff       = rd.Read(new CFFTable());

            //additional math table (if available)
            MathTable mathtable = rd.Read(new MathTable());
            //------------------------------------

            //PART 3: advanced typography
            GDEF gdef      = rd.Read(new GDEF());
            GSUB gsub      = rd.Read(new GSUB());
            GPOS gpos      = rd.Read(new GPOS());
            BASE baseTable = rd.Read(new BASE());
            JSTF jstf      = rd.Read(new JSTF());

            STAT stat = rd.Read(new STAT());

            if (stat != null)
            {
                //variable font
                FVar fvar = rd.Read(new FVar());
                if (fvar != null)
                {
                    GVar gvar = rd.Read(new GVar());
                    CVar cvar = rd.Read(new CVar());
                    HVar hvar = rd.Read(new HVar());
                    MVar mvar = rd.Read(new MVar());
                    AVar avar = rd.Read(new AVar());
                }
            }

            bool isPostScriptOutline = false;
            bool isBitmapFont        = false;

            typeface.SetBasicTypefaceTables(os2Table, nameEntry, head, horizontalMetrics);
            if (glyf == null)
            {
                //check if this is cff table ?
                if (cff == null)
                {
                    //check  cbdt/cblc ?
                    CBLC cblcTable = rd.Read(new CBLC());
                    if (cblcTable != null)
                    {
                        CBDT cbdtTable = rd.Read(new CBDT());
                        //read cbdt
                        //bitmap font

                        BitmapFontGlyphSource bmpFontGlyphSrc = new BitmapFontGlyphSource(cblcTable);
                        bmpFontGlyphSrc.LoadCBDT(cbdtTable);
                        Glyph[] glyphs = bmpFontGlyphSrc.BuildGlyphList();
                        typeface.SetBitmapGlyphs(glyphs, bmpFontGlyphSrc);
                        isBitmapFont = true;
                    }
                    else
                    {
                        //TODO:
                        EBLC fontBmpTable = rd.Read(new EBLC());
                        throw new NotSupportedException();
                    }
                }
                else
                {
                    isPostScriptOutline = true;
                    typeface.SetCffFontSet(cff.Cff1FontSet);
                }
            }
            else
            {
                typeface.SetTtfGlyphs(glyf.Glyphs);
            }

            //----------------------------
            typeface.CmapTable  = cmaps;
            typeface.KernTable  = kern;
            typeface.MaxProfile = maxProfile;
            typeface.HheaTable  = horizontalHeader;
            //----------------------------
            typeface.GaspTable = gaspTable;

            if (!isPostScriptOutline && !isBitmapFont)
            {
                //for true-type font outline
                FpgmTable fpgmTable = rd.Read(new FpgmTable());
                //control values table
                CvtTable  cvtTable         = rd.Read(new CvtTable());
                PrepTable propProgramTable = rd.Read(new PrepTable());

                typeface.ControlValues     = cvtTable?._controlValues;
                typeface.FpgmProgramBuffer = fpgmTable?._programBuffer;
                typeface.PrepProgramBuffer = propProgramTable?._programBuffer;
            }

            //-------------------------
            typeface.LoadOpenFontLayoutInfo(
                gdef,
                gsub,
                gpos,
                baseTable,
                colr,
                cpal);
            //------------

            typeface.SetSvgTable(rd.Read(new SvgTable()));
            typeface.PostTable = postTable;

            if (mathtable != null)
            {
                MathGlyphLoader.LoadMathGlyph(typeface, mathtable);
            }
#if DEBUG
            //test
            //int found = typeface.GetGlyphIndexByName("Uacute");
            if (typeface.IsCffFont)
            {
                //optional
                typeface.UpdateAllCffGlyphBounds();
            }
#endif
            typeface.UpdateLangs(meta);
            typeface.UpdateFrequentlyUsedValues();
            return(true);
        }
Esempio n. 9
0
        bool ReadTableEntryCollectionOnRestoreMode(Typeface typeface, RestoreTicket ticket, TableEntryCollection tables, BinaryReader input)
        {
            //RESTORE MODE
            //check header matches

            if (!typeface.IsTrimmed() ||
                !typeface.CompareOriginalHeadersWithNewlyLoadOne(tables.CloneTableHeaders()))
            {
                return(false);
            }


            var rd = new EntriesReaderHelper(tables, input);
            //PART 1: basic information
            //..
            //------------------------------------
            //PART 2: glyphs detail
            //2.1 True type font
            GlyphLocations glyphLocations = ticket.HasTtf ? rd.Read(new GlyphLocations(typeface.MaxProfile.GlyphCount, typeface.Head.WideGlyphLocations)) : null;
            Glyf           glyf           = ticket.HasTtf ? rd.Read(new Glyf(glyphLocations)) : null;

            typeface.GaspTable = ticket.GaspTable ? rd.Read(new Gasp()) : null;

            typeface.SetColorAndPalTable(
                ticket.COLRTable ? rd.Read(new COLR()) : null,
                ticket.CPALTable ? rd.Read(new CPAL()) : null);


            //2.2 Cff font
            CFFTable cff = ticket.HasCff ? rd.Read(new CFFTable()) : null;

            bool isPostScriptOutline = false;
            bool isBitmapFont        = false;

            if (glyf == null)
            {
                //check if this is cff table ?
                if (cff == null)
                {
                    //check  cbdt/cblc ?
                    if (ticket.HasBitmapSource)
                    {
                        //reload only CBDT (embeded bitmap)
                        CBDT cbdt = rd.Read(new CBDT());
                        typeface._bitmapFontGlyphSource.LoadCBDT(cbdt);
                        //just clone existing glyph
                        isBitmapFont = true;
                    }
                    else
                    {
                        //?
                        throw new NotSupportedException();
                    }
                }
                else
                {
                    isPostScriptOutline = true;
                    typeface.SetCffFontSet(cff.Cff1FontSet);
                }
            }
            else
            {
                typeface.SetTtfGlyphs(glyf.Glyphs);
            }

            if (!isPostScriptOutline && !isBitmapFont)
            {
                //for true-type font outline
                FpgmTable fpgmTable = rd.Read(new FpgmTable());
                //control values table
                CvtTable  cvtTable         = rd.Read(new CvtTable());
                PrepTable propProgramTable = rd.Read(new PrepTable());

                typeface.ControlValues     = cvtTable?._controlValues;
                typeface.FpgmProgramBuffer = fpgmTable?._programBuffer;
                typeface.PrepProgramBuffer = propProgramTable?._programBuffer;
            }

            if (ticket.HasSvg)
            {
                typeface.SetSvgTable(rd.Read(new SvgTable()));
            }


#if DEBUG
            //test
            //int found = typeface.GetGlyphIndexByName("Uacute");
            if (typeface.IsCffFont)
            {
                //optional???
                typeface.UpdateAllCffGlyphBounds();
            }
#endif
            typeface._typefaceTrimMode = TrimMode.Restored;
            return(true);
        }
Esempio n. 10
0
        public Typeface Read(Stream stream, ReadFlags readFlags = ReadFlags.Full)
        {
            bool little = BitConverter.IsLittleEndian;

            using (var input = new ByteOrderSwappingBinaryReader(stream))
            {
                ushort majorVersion  = input.ReadUInt16();
                ushort minorVersion  = input.ReadUInt16();
                ushort tableCount    = input.ReadUInt16();
                ushort searchRange   = input.ReadUInt16();
                ushort entrySelector = input.ReadUInt16();
                ushort rangeShift    = input.ReadUInt16();
                var    tables        = new TableEntryCollection();
                for (int i = 0; i < tableCount; i++)
                {
                    tables.AddEntry(new UnreadTableEntry(ReadTableHeader(input)));
                }
                //------------------------------------------------------------------
                OS2Table  os2Table  = ReadTableIfExists(tables, input, new OS2Table());
                NameEntry nameEntry = ReadTableIfExists(tables, input, new NameEntry());

                Head              header            = ReadTableIfExists(tables, input, new Head());
                MaxProfile        maximumProfile    = ReadTableIfExists(tables, input, new MaxProfile());
                HorizontalHeader  horizontalHeader  = ReadTableIfExists(tables, input, new HorizontalHeader());
                HorizontalMetrics horizontalMetrics = ReadTableIfExists(tables, input, new HorizontalMetrics(horizontalHeader.HorizontalMetricsCount, maximumProfile.GlyphCount));

                //---
                PostTable postTable = ReadTableIfExists(tables, input, new PostTable());
                CFFTable  ccf       = ReadTableIfExists(tables, input, new CFFTable());

                //--------------
                Cmap           cmaps          = ReadTableIfExists(tables, input, new Cmap());
                GlyphLocations glyphLocations = ReadTableIfExists(tables, input, new GlyphLocations(maximumProfile.GlyphCount, header.WideGlyphLocations));

                Glyf glyf = ReadTableIfExists(tables, input, new Glyf(glyphLocations));
                //--------------
                Gasp gaspTable             = ReadTableIfExists(tables, input, new Gasp());
                VerticalDeviceMetrics vdmx = ReadTableIfExists(tables, input, new VerticalDeviceMetrics());
                //--------------


                Kern kern = ReadTableIfExists(tables, input, new Kern());
                //--------------
                //advanced typography
                GDEF           gdef      = ReadTableIfExists(tables, input, new GDEF());
                GSUB           gsub      = ReadTableIfExists(tables, input, new GSUB());
                GPOS           gpos      = ReadTableIfExists(tables, input, new GPOS());
                BASE           baseTable = ReadTableIfExists(tables, input, new BASE());
                COLR           colr      = ReadTableIfExists(tables, input, new COLR());
                CPAL           cpal      = ReadTableIfExists(tables, input, new CPAL());
                VerticalHeader vhea      = ReadTableIfExists(tables, input, new VerticalHeader());
                if (vhea != null)
                {
                    VerticalMetrics vmtx = ReadTableIfExists(tables, input, new VerticalMetrics(vhea.NumOfLongVerMetrics));
                }



                //test math table
                MathTable mathtable    = ReadTableIfExists(tables, input, new MathTable());
                EBLCTable fontBmpTable = ReadTableIfExists(tables, input, new EBLCTable());
                //---------------------------------------------
                //about truetype instruction init

                //---------------------------------------------
                Typeface typeface            = null;
                bool     isPostScriptOutline = false;
                if (glyf == null)
                {
                    //check if this is cff table ?
                    if (ccf == null)
                    {
                        //TODO: review here
                        throw new NotSupportedException();
                    }
                    //...
                    //PostScript outline font
                    isPostScriptOutline = true;
                    typeface            = new Typeface(
                        nameEntry,
                        header.Bounds,
                        header.UnitsPerEm,
                        ccf,
                        horizontalMetrics,
                        os2Table);
                }
                else
                {
                    typeface = new Typeface(
                        nameEntry,
                        header.Bounds,
                        header.UnitsPerEm,
                        glyf.Glyphs,
                        horizontalMetrics,
                        os2Table);
                }

                //----------------------------
                typeface.CmapTable  = cmaps;
                typeface.KernTable  = kern;
                typeface.GaspTable  = gaspTable;
                typeface.MaxProfile = maximumProfile;
                typeface.HheaTable  = horizontalHeader;
                //----------------------------

                if (!isPostScriptOutline)
                {
                    FpgmTable fpgmTable = ReadTableIfExists(tables, input, new FpgmTable());
                    //control values table
                    CvtTable cvtTable = ReadTableIfExists(tables, input, new CvtTable());
                    if (cvtTable != null)
                    {
                        typeface.ControlValues = cvtTable.controlValues;
                    }
                    if (fpgmTable != null)
                    {
                        typeface.FpgmProgramBuffer = fpgmTable.programBuffer;
                    }
                    PrepTable propProgramTable = ReadTableIfExists(tables, input, new PrepTable());
                    if (propProgramTable != null)
                    {
                        typeface.PrepProgramBuffer = propProgramTable.programBuffer;
                    }
                }
                //-------------------------
                typeface.LoadOpenFontLayoutInfo(
                    gdef,
                    gsub,
                    gpos,
                    baseTable,
                    colr,
                    cpal);

                //------------


                //test
                {
                    SvgTable svgTable = ReadTableIfExists(tables, input, new SvgTable());
                    if (svgTable != null)
                    {
                        typeface._svgTable = svgTable;
                    }
                }

                typeface.PostTable = postTable;
                if (mathtable != null)
                {
                    var mathGlyphLoader = new MathGlyphLoader();
                    mathGlyphLoader.LoadMathGlyph(typeface, mathtable);
                }
#if DEBUG
                //test
                int found = typeface.GetGlyphIndexByName("Uacute");
                if (typeface.IsCffFont)
                {
                    //optional
                    typeface.UpdateAllCffGlyphBounds();
                }
#endif
                return(typeface);
            }
        }