Beispiel #1
0
        static Glyph ReadCompositeGlyph(Glyph[] createdGlyhps, BinaryReader reader, int contourCount, Bounds bounds)
        {
            //------------------------------------------------------
            //https://www.microsoft.com/typography/OTSPEC/glyf.htm
            //Composite Glyph Description

            //This is the table information needed for composite glyphs (numberOfContours is -1).
            //A composite glyph starts with two USHORT values (“flags” and “glyphIndex,” i.e. the index of the first contour in this composite glyph);
            //the data then varies according to “flags”).
            //Type  Name    Description
            //USHORT    flags   component flag
            //USHORT    glyphIndex  glyph index of component
            //VARIABLE  argument1   x-offset for component or point number; type depends on bits 0 and 1 in component flags
            //VARIABLE  argument2   y-offset for component or point number; type depends on bits 0 and 1 in component flags
            //---------
            //see more at https://fontforge.github.io/assets/old/Composites/index.html
            //---------

            Glyph finalGlyph = null;
            CompositeGlyphFlags flags;

            do
            {
                flags = (CompositeGlyphFlags)reader.ReadUInt16();
                ushort glyphIndex = reader.ReadUInt16();
                Glyph  newGlyph   = Glyph.Clone(createdGlyhps[glyphIndex]);

                short  arg1     = 0;
                short  arg2     = 0;
                ushort arg1and2 = 0;

                if (HasFlag(flags, CompositeGlyphFlags.ARG_1_AND_2_ARE_WORDS))
                {
                    arg1 = reader.ReadInt16();
                    arg2 = reader.ReadInt16();
                }
                else
                {
                    byte a1 = reader.ReadByte();
                    byte a2 = reader.ReadByte();
                    arg1and2 = (ushort)((a1 << 8) | a2);
                }
                //-----------------------------------------
                float xscale  = 1;
                float scale01 = 0;
                float scale10 = 0;
                float yscale  = 1;

                bool useMatrix = false;
                //-----------------------------------------
                bool hasScale = false;
                if (HasFlag(flags, CompositeGlyphFlags.WE_HAVE_A_SCALE))
                {
                    //If the bit WE_HAVE_A_SCALE is set,
                    //the scale value is read in 2.14 format-the value can be between -2 to almost +2.
                    //The glyph will be scaled by this value before grid-fitting.
                    xscale   = yscale = ((float)reader.ReadInt16()) / (1 << 14); /* Format 2.14 */
                    hasScale = true;
                }
                else if (HasFlag(flags, CompositeGlyphFlags.WE_HAVE_AN_X_AND_Y_SCALE))
                {
                    xscale   = ((float)reader.ReadInt16()) / (1 << 14); /* Format 2.14 */
                    yscale   = ((float)reader.ReadInt16()) / (1 << 14); /* Format 2.14 */
                    hasScale = true;
                }
                else if (HasFlag(flags, CompositeGlyphFlags.WE_HAVE_A_TWO_BY_TWO))
                {
                    //The bit WE_HAVE_A_TWO_BY_TWO allows for linear transformation of the X and Y coordinates by specifying a 2 × 2 matrix.
                    //This could be used for scaling and 90-degree*** rotations of the glyph components, for example.

                    //2x2 matrix

                    //The purpose of USE_MY_METRICS is to force the lsb and rsb to take on a desired value.
                    //For example, an i-circumflex (U+00EF) is often composed of the circumflex and a dotless-i.
                    //In order to force the composite to have the same metrics as the dotless-i,
                    //set USE_MY_METRICS for the dotless-i component of the composite.
                    //Without this bit, the rsb and lsb would be calculated from the hmtx entry for the composite
                    //(or would need to be explicitly set with TrueType instructions).

                    //Note that the behavior of the USE_MY_METRICS operation is undefined for rotated composite components.
                    useMatrix = true;
                    hasScale  = true;
                    xscale    = ((float)reader.ReadInt16()) / (1 << 14); /* Format 2.14 */
                    scale01   = ((float)reader.ReadInt16()) / (1 << 14); /* Format 2.14 */
                    scale10   = ((float)reader.ReadInt16()) / (1 << 14); /* Format 2.14 */
                    yscale    = ((float)reader.ReadInt16()) / (1 << 14); /* Format 2.14 */

                    if (HasFlag(flags, CompositeGlyphFlags.UNSCALED_COMPONENT_OFFSET))
                    {
                    }
                    else
                    {
                    }
                    if (HasFlag(flags, CompositeGlyphFlags.USE_MY_METRICS))
                    {
                    }
                }

                //--------------------------------------------------------------------
                if (HasFlag(flags, CompositeGlyphFlags.ARGS_ARE_XY_VALUES))
                {
                    //Argument1 and argument2 can be either x and y offsets to be added to the glyph or two point numbers.
                    //x and y offsets to be added to the glyph
                    //When arguments 1 and 2 are an x and a y offset instead of points and the bit ROUND_XY_TO_GRID is set to 1,
                    //the values are rounded to those of the closest grid lines before they are added to the glyph.
                    //X and Y offsets are described in FUnits.

                    if (useMatrix)
                    {
                        //use this matrix
                        Glyph.TransformNormalWith2x2Matrix(newGlyph, xscale, scale01, scale10, yscale);
                        Glyph.OffsetXY(newGlyph, (short)(arg1), arg2);
                    }
                    else
                    {
                        if (hasScale)
                        {
                            if (xscale == 1.0 && yscale == 1.0)
                            {
                            }
                            else
                            {
                                Glyph.TransformNormalWith2x2Matrix(newGlyph, xscale, 0, 0, yscale);
                            }
                            Glyph.OffsetXY(newGlyph, arg1, arg2);
                        }
                        else
                        {
                            if (HasFlag(flags, CompositeGlyphFlags.ROUND_XY_TO_GRID))
                            {
                                //TODO: implement round xy to grid***
                                //----------------------------
                            }
                            //just offset***
                            Glyph.OffsetXY(newGlyph, arg1, arg2);
                        }
                    }
                }
                else
                {
                    //two point numbers.
                    //the first point number indicates the point that is to be matched to the new glyph.
                    //The second number indicates the new glyph's “matched” point.
                    //Once a glyph is added,its point numbers begin directly after the last glyphs (endpoint of first glyph + 1)
                }

                //
                if (finalGlyph == null)
                {
                    finalGlyph = newGlyph;
                }
                else
                {
                    //merge
                    Glyph.AppendGlyph(finalGlyph, newGlyph);
                }
            } while (HasFlag(flags, CompositeGlyphFlags.MORE_COMPONENTS));
            if (HasFlag(flags, CompositeGlyphFlags.WE_HAVE_INSTRUCTIONS))
            {
                ushort numInstr = reader.ReadUInt16();
                byte[] insts    = reader.ReadBytes(numInstr);
                finalGlyph.GlyphInstructions = insts;
            }
            //F2DOT14   16-bit signed fixed number with the low 14 bits of fraction (2.14).
            //Transformation Option
            //
            //The C pseudo-code fragment below shows how the composite glyph information is stored and parsed; definitions for “flags” bits follow this fragment:
            //  do {
            //    USHORT flags;
            //    USHORT glyphIndex;
            //    if ( flags & ARG_1_AND_2_ARE_WORDS) {
            //    (SHORT or FWord) argument1;
            //    (SHORT or FWord) argument2;
            //    } else {
            //        USHORT arg1and2; /* (arg1 << 8) | arg2 */
            //    }
            //    if ( flags & WE_HAVE_A_SCALE ) {
            //        F2Dot14  scale;    /* Format 2.14 */
            //    } else if ( flags & WE_HAVE_AN_X_AND_Y_SCALE ) {
            //        F2Dot14  xscale;    /* Format 2.14 */
            //        F2Dot14  yscale;    /* Format 2.14 */
            //    } else if ( flags & WE_HAVE_A_TWO_BY_TWO ) {
            //        F2Dot14  xscale;    /* Format 2.14 */
            //        F2Dot14  scale01;   /* Format 2.14 */
            //        F2Dot14  scale10;   /* Format 2.14 */
            //        F2Dot14  yscale;    /* Format 2.14 */
            //    }
            //} while ( flags & MORE_COMPONENTS )
            //if (flags & WE_HAVE_INSTR){
            //    USHORT numInstr
            //    BYTE instr[numInstr]
            //------------------------------------------------------------


            return((finalGlyph == null) ? Glyph.Empty : finalGlyph);
        }