Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved GlyphPathBuilder files to Typography.Contours project #106

Merged
merged 1 commit into from
May 15, 2018

Conversation

Happypig375
Copy link
Contributor

Originally the goal of #105 but its discussion only surrounded creating a .NET Standard project for Typography.Contours.

This reaches the goal, merging the two duplicated batches of GlyphPathBuilder files, one older (DrawingGL.Common), one newer (PixelFarm.Typography), with a successful build.

@prepare
Copy link
Member

prepare commented May 14, 2018

give me a time for testing this.

@prepare
Copy link
Member

prepare commented May 15, 2018

@Happypig375,

on this version,
you will see 2 GlyphPathBuilders ?


Haha,
That is my intention.

Why?

I want to demonstrate 'how to implement GlyphPathBuilder'.

other libs can implement its own too. (eg. implement it with some DirectX , OpenGL)
=> so GlyphPathBuilder is not in the Typography.OpenFont or GlyphLayout.

I provide some examples

  1. GdiPlusSample.WinForms's GlyphPathBuilder
    Basic GlyphPathBuilder on Gdi+. you will see that GdiPlusSample.WinForms dose not reference
    Typography.Contours

  2. PixelFarm's Typography.Contours.GlyphPathBuilder
    This is a PixelFarm's version, the builder has GlyphOutlineAnalyzer ,
    GlyphDynamicOutline that are in developing stage, not need by others.


Conclusion: so=> leave it as that intention.

Thank you for this PRs.
and
I'm waiting for your Next PR(s)

:)

@prepare prepare closed this May 15, 2018
@prepare
Copy link
Member

prepare commented May 15, 2018

Sorry for confusion
see: 948522c

@Happypig375
Copy link
Contributor Author

Sorry, but I have another point of view on this.

Let me start by comparing the two batches of files.
GlyphPathBuilderBase.cs

$ git diff HEAD:Demo/Shared/GlyphPathBuilder/GlyphPathBuilderBase.cs PixelFarm.T                                                                                                                                                                                               ypography/1.1_Drawing_Fonts/GlyphPathBuilderBase.cs
diff --git a/Demo/Shared/GlyphPathBuilder/GlyphPathBuilderBase.cs b/PixelFarm.Ty                                                                                                                                                                                               pography/1.1_Drawing_Fonts/GlyphPathBuilderBase.cs
index 305bff9..0e5649c 100644
--- a/Demo/Shared/GlyphPathBuilder/GlyphPathBuilderBase.cs
+++ b/PixelFarm.Typography/1.1_Drawing_Fonts/GlyphPathBuilderBase.cs
@@ -1,7 +1,7 @@
 //MIT, 2016-2017, WinterDev

 using Typography.OpenFont;
-
+using Typography.Rendering;

 namespace Typography.Contours
 {
@@ -17,15 +17,22 @@ public abstract class GlyphPathBuilderBase
         TrueTypeInterpreter _trueTypeInterpreter;
         protected GlyphPointF[] _outputGlyphPoints;
         protected ushort[] _outputContours;
+
+
+
+        protected OpenFont.CFF.Cff1Font _ownerCff;
+        protected OpenFont.CFF.Cff1GlyphData _cffGlyphData;
+
         /// <summary>
         /// scale for converting latest glyph points to latest request font siz                                                                                                                                                                                               e
         /// </summary>
         float _recentPixelScale;
         bool _useInterpreter;
+
         public GlyphPathBuilderBase(Typeface typeface)
         {
             _typeface = typeface;
-            this.UseTrueTypeInstructions = false;//default?
+            this.UseTrueTypeInstructions = true;//default?
             _trueTypeInterpreter = new TrueTypeInterpreter();
             _trueTypeInterpreter.SetTypeFace(typeface);
             _recentPixelScale = 1;
@@ -56,9 +63,25 @@ public void BuildFromGlyphIndex(ushort glyphIndex, float size                                                                                                                                                                                               InPoints)
         {
             //
             Glyph glyph = _typeface.GetGlyphByIndex(glyphIndex);
+
+
+            //for true type font
             this._outputGlyphPoints = glyph.GlyphPoints;
             this._outputContours = glyph.EndPoints;

+
+            //------------
+            //temp fix for Cff Font
+            if (glyph.IsCffGlyph)
+            {
+                this._cffGlyphData = glyph.GetCff1GlyphData();
+                this._ownerCff = glyph.GetOwnerCff();
+            }
+
+            //---------------
+
+
+
             if ((RecentFontSizeInPixels = Typeface.ConvPointsToPixels(sizeInPoi                                                                                                                                                                                               nts)) < 0)
             {
                 //convert to pixel size
@@ -69,10 +92,12 @@ public void BuildFromGlyphIndex(ushort glyphIndex, float siz                                                                                                                                                                                               eInPoints)
             else
             {
                 _recentPixelScale = Typeface.CalculateScaleToPixel(RecentFontSi                                                                                                                                                                                               zeInPixels);
+                IsSizeChanged = true;
             }
             //-------------------------------------
             FitCurrentGlyph(glyphIndex, glyph);
         }
+        protected bool IsSizeChanged { get; set; }
         protected float RecentFontSizeInPixels { get; private set; }
         protected virtual void FitCurrentGlyph(ushort glyphIndex, Glyph glyph)
         {
@@ -87,13 +112,25 @@ protected virtual void FitCurrentGlyph(ushort glyphIndex, G                                                                                                                                                                                               lyph glyph)
                 //all points are scaled from _trueTypeInterpreter,
                 //so not need further scale.=> set _recentPixelScale=1
                 _recentPixelScale = 1;
-            }
+            }
         }
         public virtual void ReadShapes(IGlyphTranslator tx)
         {
             //read output from glyph points
-            tx.Read(this._outputGlyphPoints, this._outputContours, _recentPixel                                                                                                                                                                                               Scale);
+
+            if (this._cffGlyphData != null)
+            {
+                tx.Read(this._ownerCff, this._cffGlyphData, _recentPixelScale);
+            }
+            else
+            {
+                tx.Read(this._outputGlyphPoints, this._outputContours, _recentP                                                                                                                                                                                               ixelScale);
+            }
+
         }
+
+
+
     }

     public static class GlyphPathBuilderExtensions
(END)
         public virtual void ReadShapes(IGlyphTranslator tx)
         {
             //read output from glyph points
-            tx.Read(this._outputGlyphPoints, this._outputContours, _recentPixelScale);
+
+            if (this._cffGlyphData != null)
+            {
+                tx.Read(this._ownerCff, this._cffGlyphData, _recentPixelScale);
+            }
+            else
+            {
+                tx.Read(this._outputGlyphPoints, this._outputContours, _recentPixelScale);
+            }
+
         }
+
+
+
     }

     public static class GlyphPathBuilderExtensions

As you can see, there are only additions, but no deletions for PixelFarm.Typography as compared to DrawingGL.Common.

The new one in PixelFarm.Typography supports CFF fonts but the old one in DrawingGL.Common does not. I suspect that you updated the one in PixelFarm.Typography but forgot to do so for DrawingGL.Common.

By merging them, you only need to update code in one place and not worry about the possibility of forgetting to update another file of the same type.

GlyphPathBuilder.cs

git diff HEAD:Demo/Shared/GlyphPathBuilder/GlyphPathBuilder.cs PixelFarm.Typog                                                                                                                                                                                               raphy/1.1_Drawing_Fonts/GlyphPathBuilder.cs                                                                                                                                                                                                                                    diff --git a/Demo/Shared/GlyphPathBuilder/GlyphPathBuilder.cs b/PixelFarm.Typogr                                                                                                                                                                                               aphy/1.1_Drawing_Fonts/GlyphPathBuilder.cs
index 9fd4ca6..4c9e894 100644
--- a/Demo/Shared/GlyphPathBuilder/GlyphPathBuilder.cs
+++ b/PixelFarm.Typography/1.1_Drawing_Fonts/GlyphPathBuilder.cs
@@ -1,15 +1,130 @@
 //MIT, 2016-2017, WinterDev

-
-
+using System;
+using System.Collections.Generic;
+using Typography.OpenFont;
 namespace Typography.Contours
 {
-    //-----------------------------------
-    //sample GlyphPathBuilder :
-    //for your flexiblity of glyph path builder.
-    //-----------------------------------
+
     public class GlyphPathBuilder : GlyphPathBuilderBase
     {
-        public GlyphPathBuilder(Typography.OpenFont.Typeface typeface) : base(t                                                                                                                                                                                               ypeface) { }
:
aphy/1.1_Drawing_Fonts/GlyphPathBuilder.cs
index 9fd4ca6..4c9e894 100644
--- a/Demo/Shared/GlyphPathBuilder/GlyphPathBuilder.cs
+++ b/PixelFarm.Typography/1.1_Drawing_Fonts/GlyphPathBuilder.cs
@@ -1,15 +1,130 @@
 //MIT, 2016-2017, WinterDev

-
-
+using System;
+using System.Collections.Generic;
+using Typography.OpenFont;
 namespace Typography.Contours
 {
-    //-----------------------------------
-    //sample GlyphPathBuilder :
-    //for your flexiblity of glyph path builder.
-    //-----------------------------------
+
     public class GlyphPathBuilder : GlyphPathBuilderBase
     {
-        public GlyphPathBuilder(Typography.OpenFont.Typeface typeface) : base(typeface) { }
+        GlyphOutlineAnalyzer _fitShapeAnalyzer = new GlyphOutlineAnalyzer();
+        Dictionary<ushort, GlyphDynamicOutline> _fitoutlineCollection = new Dictionary<ushort, GlyphDynamicOutline>();
+        GlyphDynamicOutline _latestDynamicOutline;
+
+        public GlyphPathBuilder(Typeface typeface)
+            : base(typeface)
+        {
+
+            //for specific typeface ***
+            //float offsetLenFromMasterOutline = GlyphDynamicEdgeOffset;
+            //_latestDynamicOutline.SetDynamicEdgeOffsetFromMasterOutline(offsetLenFromMasterOutline / toPixelScale);
+        }
+
+#if DEBUG
+        public bool dbugAlwaysDoCurveAnalysis;
+#endif
+
+        internal bool TemporaryDisableCustomFit { get; set; }
+        /// <summary>
+        /// glyph dynamic edge offset
+        /// </summary>
+        public float GlyphDynamicEdgeOffset { get; set; }
+
+        protected override void FitCurrentGlyph(ushort glyphIndex, Glyph glyph)
+        {
+            //not use interperter so we need to scale it with our mechanism
+            //this demonstrate our auto hint engine ***
+            //you can change this to your own hint engine***
+            _latestDynamicOutline = null;//reset
+            if (this.UseTrueTypeInstructions)
+            {
+                base.FitCurrentGlyph(glyphIndex, glyph);
+            }
+            else
+            {
+                //
+                if (TemporaryDisableCustomFit)
+                {
+                    return;
+                }
+                //
+                if (this.UseVerticalHinting)
+                {
+                    if (!_fitoutlineCollection.TryGetValue(glyphIndex, out _latestDynamicOutline))
+                    {
+
+                        //---------------------------------------------
+                        //test code
+                        //GlyphContourBuilder contBuilder = new GlyphContourBuilder();
+                        //contBuilder.Reset();
+                        //int x = 100, y = 120, w = 700, h = 200;
+                        //contBuilder.MoveTo(x, y);
+                        //contBuilder.LineTo(x + w, y);
+                        //contBuilder.LineTo(x + w, y + h);
+                        //contBuilder.LineTo(x, y + h);
+                        //contBuilder.CloseFigure();
+                        //---------------------------------------------
+                        _latestDynamicOutline = _fitShapeAnalyzer.CreateDynamicOutline(
+                            this._outputGlyphPoints,
+                            this._outputContours);
+                        //add more information for later scaling process
+                        _latestDynamicOutline.OriginalAdvanceWidth = glyph.OriginalAdvanceWidth;
+                        _latestDynamicOutline.OriginalGlyphControlBounds = glyph.Bounds;
+                        //store to our dynamic outline collection
+                        //so we can reuse it
+                        _fitoutlineCollection.Add(glyphIndex, _latestDynamicOutline);
+                        //-------------------
+                        //
+                        _latestDynamicOutline.GenerateOutput(null, Typeface.CalculateScaleToPixel(RecentFontSizeInPixels));
+                        //-------------------
+
+                    }
+                    else
+                    {
+                        if (IsSizeChanged)
+                        {
+                            _latestDynamicOutline.GenerateOutput(null, Typeface.CalculateScaleToPixel(RecentFontSizeInPixels));
+                            IsSizeChanged = false;
+                        }
+                    }
+                }
+            }
+        }
+        public override void ReadShapes(IGlyphTranslator tx)
+        {
+            //read output shape from dynamic outline
+
+            if (this.UseTrueTypeInstructions)
+            {
+                base.ReadShapes(tx);
+                return;
+            }
+            if (!TemporaryDisableCustomFit && this.UseVerticalHinting)
+            {
+                //read from our auto hint fitoutline
+                //need scale from original.
+                float toPixelScale = Typeface.CalculateScaleToPixel(RecentFontSizeInPixels);
+                if (toPixelScale < 0)
+                {
+                    toPixelScale = 1;
+                }
+                _latestDynamicOutline.GenerateOutput(tx, toPixelScale);
+            }
+            else
+            {
+                base.ReadShapes(tx);
+            }
+        }
+
+        public GlyphDynamicOutline LatestGlyphFitOutline
+        {
+            get
+            {
+                return _latestDynamicOutline;
+            }
+        }
+
+
     }
 }

As you mentioned,

This is a PixelFarm's version, the builder has GlyphOutlineAnalyzer ,
GlyphDynamicOutline that are in developing stage, not need by others.

However, that is the purpose of branches that are not the master branch. They are supposed to contain highly experimental code. Not to mention that Typography is not yet stable anyway (#96).

HintTechnique.cs

$ git diff HEAD:Demo/Shared/GlyphPathBuilder/HintTechnique.cs PixelFarm.Typography/1.1_Drawing_Fonts/HintTechnique.cs

No output. Aka the files are the same.
Again, updating this file needs to be done twice.

As for

I want to demonstrate 'how to implement GlyphPathBuilder'.

other libs can implement its own too. (eg. implement it with some DirectX , OpenGL)
=> so GlyphPathBuilder is not in the Typography.OpenFont or GlyphLayout.

That is the point of GlyphBuilderBase.cs. Having a base type means others can inherit it.
Nevertheless, this base type exists in two different places right now and without running a git diff like I did, how are users supposed to know which one to use? The demo all point to the old one in DrawingGL.Common, and I was going to open an issue for this before I, upon re-reading your responses, found out that the one hidden in PixelFarm.Typography is not only newer, but supports CFF fonts too.

Also, the current GlyphBuilder supports reading from IGlyphTranslators. Instead of reinventing the wheel, I can just implement IGlyphTranslator and there is no need to implement GlyphBuilder myself.

That is why I support moving GlyphBuilder to Typography.Contours, a public-facing project for others to reference. Their namespaces already match too!

@prepare
Copy link
Member

prepare commented May 15, 2018

@Happypig375 👍

You are right.
I forgot to update the GdiPlusSample.WinForms's GlyphPathBuilder
with new Cff!

Give me a time to correct that.


@prepare
Copy link
Member

prepare commented May 15, 2018

I'm not sure that other projects want to use the Contours and it still in development stage.

@prepare prepare reopened this May 15, 2018
@Happypig375
Copy link
Contributor Author

Ok, but the types in DrawingGL.Common are nevertheless useful. I suppose I will reference it then.

@prepare prepare merged commit 1d09c5f into LayoutFarm:dev May 15, 2018
@prepare
Copy link
Member

prepare commented May 15, 2018

merged with minor diff => 2194e11

Thank you

@Happypig375
Copy link
Contributor Author

Thanks!

I will send another PR for IWritablePath and GlyphTranslatorToPath tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants