Ejemplo n.º 1
0
 static void FT_CHECK(FT_Error check)
 {
     if (check != FT_Error.Ok)
     {
         throw new InvalidOperationException(check.ToString());
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Parse a list and calls a given iterator function on each element. Note that parsing is stopped as soon as
        /// one of the iterator calls returns a non-zero value.
        /// </summary>
        /// <param name="iterator">An iterator function, called on each node of the list.</param>
        /// <param name="user">A user-supplied field which is passed as the second argument to the iterator.</param>
        public void Iterate(FT_List_Iterator iterator, IntPtr user)
        {
            FT_Error err = FT.FT_List_Iterate(Reference, iterator, user);

            if (err != FT_Error.FT_Err_Ok)
            {
                throw new FreeTypeException(err);
            }
        }
Ejemplo n.º 3
0
        public Font LoadFontFromStream(Stream stream, FontFormat format)
        {
            FT_Face     face;
            FT_Error    error = FT_Error.FT_Err_Ok;
            IDisposable source;

            if (stream is UnmanagedMemoryStream ums)
            {
                unsafe
                {
                    error = FT_New_Memory_Face(this.library, (IntPtr)ums.PositionPointer, (int)ums.Length, 0, &face);
                }
                source = ums;
            }
            else
            {
                MemoryStream memoryStream;

                if (stream is MemoryStream ms)
                {
                    memoryStream = ms;
                }
                else
                {
                    int sourceLength;
                    checked
                    {
                        sourceLength = (int)stream.Length;
                    }
                    memoryStream = new MemoryStream(sourceLength);
                    stream.CopyTo(memoryStream);
                }

                unsafe
                {
                    fixed(byte *ptr = memoryStream.GetBuffer())
                    {
                        error = FT_New_Memory_Face(this.library, (IntPtr)ptr, (int)memoryStream.Length, 0, &face);
                    }
                }
                source = memoryStream;
            }


            if (error != FT_Error.FT_Err_Ok)
            {
                throw new ApplicationException(error.ToString());
            }

            return(new FreeTypeFont(face, source));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Decompress a zipped input buffer into an output buffer. This function is modeled after zlib's ‘uncompress’
        /// function.
        /// </summary>
        /// <remarks>
        /// This function may return <see cref="Error.UnimplementedFeature"/> if your build of FreeType was not
        /// compiled with zlib support.
        /// </remarks>
        /// <param name="input">The input buffer.</param>
        /// <param name="output">The output buffer.</param>
        /// <returns>The length of the used data in output.</returns>
        public unsafe int GzipUncompress(byte[] input, byte[] output)
        {
            IntPtr len = (IntPtr)output.Length;

            fixed(byte *inPtr = input, outPtr = output)
            {
                FT_Error err = FT.FT_Gzip_Uncompress(Reference, (IntPtr)outPtr, ref len, (IntPtr)inPtr, (IntPtr)input.Length);

                if (err != FT_Error.FT_Err_Ok)
                {
                    throw new FreeTypeException(err);
                }
            }

            return((int)len);
        }
        /// <summary>
        /// Gets the error string for the specified error code.
        /// </summary>
        private static String GetErrorString(FT_Error err)
        {
            switch (err)
            {
            case FT_Error.FT_Err_Ok:
                return("no error");

            case FT_Error.FT_Err_Cannot_Open_Resource:
                return("cannot open resource");

            case FT_Error.FT_Err_Unknown_File_Format:
                return("unknown file format");

            case FT_Error.FT_Err_Invalid_File_Format:
                return("broken file");

            case FT_Error.FT_Err_Invalid_Version:
                return("invalid FreeType version");

            case FT_Error.FT_Err_Lower_Module_Version:
                return("module version is too low");

            case FT_Error.FT_Err_Invalid_Argument:
                return("invalid argument");

            case FT_Error.FT_Err_Unimplemented_Feature:
                return("unimplemented feature");

            case FT_Error.FT_Err_Invalid_Table:
                return("broken table");

            case FT_Error.FT_Err_Invalid_Offset:
                return("broken offset within table");

            case FT_Error.FT_Err_Array_Too_Large:
                return("array allocation size too large");

            case FT_Error.FT_Err_Missing_Module:
                return("missing module");

            case FT_Error.FT_Err_Missing_Property:
                return("missing property");

            case FT_Error.FT_Err_Invalid_Glyph_Index:
                return("invalid glyph index");

            case FT_Error.FT_Err_Invalid_Character_Code:
                return("invalid character code");

            case FT_Error.FT_Err_Invalid_Glyph_Format:
                return("invalid glyph format");

            case FT_Error.FT_Err_Cannot_Render_Glyph:
                return("cannot render this glyph format");

            case FT_Error.FT_Err_Invalid_Outline:
                return("invalid outline");

            case FT_Error.FT_Err_Invalid_Composite:
                return("invalid composite glyph");

            case FT_Error.FT_Err_Too_Many_Hints:
                return("too many hints");

            case FT_Error.FT_Err_Invalid_Pixel_Size:
                return("invalid pixel size");

            case FT_Error.FT_Err_Invalid_Handle:
                return("invalid object handle");

            case FT_Error.FT_Err_Invalid_Library_Handle:
                return("invalid library handle");

            case FT_Error.FT_Err_Invalid_Driver_Handle:
                return("invalid module handle");

            case FT_Error.FT_Err_Invalid_Face_Handle:
                return("invalid face handle");

            case FT_Error.FT_Err_Invalid_Size_Handle:
                return("invalid size handle");

            case FT_Error.FT_Err_Invalid_Slot_Handle:
                return("invalid glyph slot handle");

            case FT_Error.FT_Err_Invalid_CharMap_Handle:
                return("invalid charmap handle");

            case FT_Error.FT_Err_Invalid_Cache_Handle:
                return("invalid cache manager handle");

            case FT_Error.FT_Err_Invalid_Stream_Handle:
                return("invalid stream handle");

            case FT_Error.FT_Err_Too_Many_Drivers:
                return("too many modules");

            case FT_Error.FT_Err_Too_Many_Extensions:
                return("too many extensions");

            case FT_Error.FT_Err_Out_Of_Memory:
                return("out of memory");

            case FT_Error.FT_Err_Unlisted_Object:
                return("unlisted object");

            case FT_Error.FT_Err_Cannot_Open_Stream:
                return("cannot open stream");

            case FT_Error.FT_Err_Invalid_Stream_Seek:
                return("invalid stream seek");

            case FT_Error.FT_Err_Invalid_Stream_Skip:
                return("invalid stream skip");

            case FT_Error.FT_Err_Invalid_Stream_Read:
                return("invalid stream read");

            case FT_Error.FT_Err_Invalid_Stream_Operation:
                return("invalid stream operation");

            case FT_Error.FT_Err_Invalid_Frame_Operation:
                return("invalid frame operation");

            case FT_Error.FT_Err_Nested_Frame_Access:
                return("nested frame access");

            case FT_Error.FT_Err_Invalid_Frame_Read:
                return("invalid frame read");

            case FT_Error.FT_Err_Raster_Uninitialized:
                return("raster uninitialized");

            case FT_Error.FT_Err_Raster_Corrupted:
                return("raster corrupted");

            case FT_Error.FT_Err_Raster_Overflow:
                return("raster overflow");

            case FT_Error.FT_Err_Raster_Negative_Height:
                return("negative height while rastering");

            case FT_Error.FT_Err_Too_Many_Caches:
                return("too many registered caches");

            case FT_Error.FT_Err_Invalid_Opcode:
                return("invalid opcode");

            case FT_Error.FT_Err_Too_Few_Arguments:
                return("too few arguments");

            case FT_Error.FT_Err_Stack_Overflow:
                return("stack overflow");

            case FT_Error.FT_Err_Code_Overflow:
                return("code overflow");

            case FT_Error.FT_Err_Bad_Argument:
                return("bad argument");

            case FT_Error.FT_Err_Divide_By_Zero:
                return("division by zero");

            case FT_Error.FT_Err_Invalid_Reference:
                return("invalid reference");

            case FT_Error.FT_Err_Debug_OpCode:
                return("found debug opcode");

            case FT_Error.FT_Err_ENDF_In_Exec_Stream:
                return("found ENDF opcode in execution stream");

            case FT_Error.FT_Err_Nested_DEFS:
                return("nested DEFS");

            case FT_Error.FT_Err_Invalid_CodeRange:
                return("invalid code range");

            case FT_Error.FT_Err_Execution_Too_Long:
                return("execution context too long");

            case FT_Error.FT_Err_Too_Many_Function_Defs:
                return("too many function definitions");

            case FT_Error.FT_Err_Too_Many_Instruction_Defs:
                return("too many instruction definitions");

            case FT_Error.FT_Err_Table_Missing:
                return("SFNT font table missing");

            case FT_Error.FT_Err_Horiz_Header_Missing:
                return("horizontal header (hhea) table missing");

            case FT_Error.FT_Err_Locations_Missing:
                return("locations (loca) table missing");

            case FT_Error.FT_Err_Name_Table_Missing:
                return("name table missing");

            case FT_Error.FT_Err_CMap_Table_Missing:
                return("character map (cmap) table missing");

            case FT_Error.FT_Err_Hmtx_Table_Missing:
                return("horizontal metrics (hmtx) table missing");

            case FT_Error.FT_Err_Post_Table_Missing:
                return("PostScript (post) table missing");

            case FT_Error.FT_Err_Invalid_Horiz_Metrics:
                return("invalid horizontal metrics");

            case FT_Error.FT_Err_Invalid_CharMap_Format:
                return("invalid character map (cmap) format");

            case FT_Error.FT_Err_Invalid_PPem:
                return("invalid ppem value");

            case FT_Error.FT_Err_Invalid_Vert_Metrics:
                return("invalid vertical metrics");

            case FT_Error.FT_Err_Could_Not_Find_Context:
                return("could not find context");

            case FT_Error.FT_Err_Invalid_Post_Table_Format:
                return("invalid PostScript (post) table format");

            case FT_Error.FT_Err_Invalid_Post_Table:
                return("invalid PostScript (post) table");

            case FT_Error.FT_Err_DEF_In_Glyf_Bytecode:
                return("found FDEF or IDEF opcode in glyf bytecode");

            case FT_Error.FT_Err_Missing_Bitmap:
                return("missing bitmap in strike");

            case FT_Error.FT_Err_Syntax_Error:
                return("opcode syntax error");

            case FT_Error.FT_Err_Stack_Underflow:
                return("argument stack underflow");

            case FT_Error.FT_Err_Ignore:
                return("ignore");

            case FT_Error.FT_Err_No_Unicode_Glyph_Name:
                return("no Unicode glyph name found");

            case FT_Error.FT_Err_Glyph_Too_Big:
                return("glyph too big for hinting");

            case FT_Error.FT_Err_Missing_Startfont_Field:
                return("`STARTFONT' field missing");

            case FT_Error.FT_Err_Missing_Font_Field:
                return("`FONT' field missing");

            case FT_Error.FT_Err_Missing_Size_Field:
                return("`SIZE' field missing");

            case FT_Error.FT_Err_Missing_Fontboundingbox_Field:
                return("`FONTBOUNDINGBOX' field missing");

            case FT_Error.FT_Err_Missing_Chars_Field:
                return("`CHARS' field missing");

            case FT_Error.FT_Err_Missing_Startchar_Field:
                return("`STARTCHAR' field missing");

            case FT_Error.FT_Err_Missing_Encoding_Field:
                return("`ENCODING' field missing");

            case FT_Error.FT_Err_Missing_Bbx_Field:
                return("`BBX' field missing");

            case FT_Error.FT_Err_Bbx_Too_Big:
                return("`BBX' too big");

            case FT_Error.FT_Err_Corrupted_Font_Header:
                return("Font header corrupted or missing fields");

            case FT_Error.FT_Err_Corrupted_Font_Glyphs:
                return("Font glyphs corrupted or missing fields");

            default:
                return("unknown error");
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FreeTypeException"/> class.
 /// </summary>
 public FreeTypeException(FT_Error err)
     : base(GetErrorString(err))
 {
 }
//    *
//	 * Build a MultiShape from chars (one Shape per character)
//	 * \exception Ogre::InternalErrorException Freetype error
//	 * \todo Need to split shapes of multi region chars. For example the letter \c O
//	 * has two shapes, but they are connected to one shape.
//
        public MultiShape realizeShapes()
        {
            MultiShape retVal = new MultiShape();

            FT_Library   ftlib = new FT_Library();
            FT_Face      face  = new FT_Face();
            FT_GlyphSlot slot  = new FT_GlyphSlot();

            FT_Error error = FT_Init_FreeType(ftlib);

            if (error == 0)
            {
                error = FT_New_Face(ftlib, getFontFileByName().c_str(), 0, face);
                if (error == FT_Err_Unknown_File_Format)
                {
                    //C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
                    //C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
                    throw ExceptionFactory.create(Mogre.ExceptionCodeType <Mogre.Exception.ExceptionCodes.ERR_INTERNAL_ERROR>(), "FreeType ERROR: FT_Err_Unknown_File_Format", "Procedural::TextShape::realizeShapes()", __FILE__, __LINE__);
                    ;
                }
                else if (error != null)
                {
                    //C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
                    //C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
                    throw ExceptionFactory.create(Mogre.ExceptionCodeType <Mogre.Exception.ExceptionCodes.ERR_INTERNAL_ERROR>(), "FreeType ERROR: FT_New_Face - " + StringConverter.toString(error), "Procedural::TextShape::realizeShapes()", __FILE__, __LINE__);
                    ;
                }
                else
                {
                    FT_Set_Pixel_Sizes(face, 0, mFontSize);

                    int px = 0;
                    int py = 0;
                    slot = face.glyph;

                    for (int n = 0; n < mText.length(); n++)
                    {
                        error = FT_Load_Char(face, mText[n], FT_LOAD_NO_BITMAP);
                        if (error != null)
                        {
                            continue;
                        }

                        Shape s = new Shape();

                        int         nContours = face.glyph.outline.n_contours;
                        int         startPos  = 0;
                        string      tags      = face.glyph.outline.tags;
                        FT_Vector[] vec       = face.glyph.outline.points;

                        for (int k = 0; k < nContours; k++)
                        {
                            if (k > 0)
                            {
                                startPos = face.glyph.outline.contours[k - 1] + 1;
                            }
                            int endPos = face.glyph.outline.contours[k] + 1;

                            Vector2 lastPoint = Vector2.ZERO;
                            for (int j = startPos; j < endPos; j++)
                            {
                                if (FT_CURVE_TAG(tags[j]) == FT_CURVE_TAG_ON)
                                {
                                    lastPoint = Vector2((float)vec[j].x, (float)vec[j].y);
                                    s.addPoint(lastPoint / 64.0f);
                                }
                                else
                                {
                                    if (FT_CURVE_TAG(tags[j]) == FT_CURVE_TAG_CUBIC)
                                    {
                                        int prevPoint = j - 1;
                                        if (j == 0)
                                        {
                                            prevPoint = endPos - 1;
                                        }
                                        int nextIndex = j + 1;
                                        if (nextIndex >= endPos)
                                        {
                                            nextIndex = startPos;
                                        }
                                        Vector2[] nextPoint = new Vector2[nextIndex]((float)vec.x, (float)vec[nextIndex].y);
                                        if ((FT_CURVE_TAG(tags[prevPoint]) != FT_CURVE_TAG_ON) && (FT_CURVE_TAG(tags[prevPoint]) == FT_CURVE_TAG_CUBIC))
                                        {
                                            BezierCurve2 bc = new BezierCurve2();
                                            bc.addPoint(Vector2((float)vec[prevPoint].x, (float)vec[prevPoint].y) / 64.0f);
                                            bc.addPoint(Vector2((float)vec[j].x, (float)vec[j].y) / 64.0f);
                                            bc.addPoint(Vector2((float)vec[nextIndex].x, (float)vec[nextIndex].y) / 64.0f);
                                            s.appendShape(bc.realizeShape());
                                        }
                                    }
                                    else
                                    {
                                        Vector2[] conicPoint = new Vector2[j]((float)vec.x, (float)vec[j].y);
                                        if (j == startPos)
                                        {
                                            if ((FT_CURVE_TAG(tags[endPos - 1]) != FT_CURVE_TAG_ON) && (FT_CURVE_TAG(tags[endPos - 1]) != FT_CURVE_TAG_CUBIC))
                                            {
                                                Vector2[] lastConnic = new Vector2[endPos - 1]((float)vec.x, (float)vec[endPos - 1].y);
                                                lastPoint = (conicPoint + lastConnic) / 2;
                                            }
                                        }

                                        int nextIndex = j + 1;
                                        if (nextIndex >= endPos)
                                        {
                                            nextIndex = startPos;
                                        }

                                        Vector2[] nextPoint = new Vector2[nextIndex]((float)vec.x, (float)vec[nextIndex].y);

                                        bool nextIsConnic = (FT_CURVE_TAG(tags[nextIndex]) != FT_CURVE_TAG_ON) && (FT_CURVE_TAG(tags[nextIndex]) != FT_CURVE_TAG_CUBIC);
                                        if (nextIsConnic)
                                        {
                                            nextPoint = (conicPoint + nextPoint) / 2;
                                        }

                                        int          pc = s.getPointCount();
                                        BezierCurve2 bc = new BezierCurve2();
                                        if (pc == 0)
                                        {
                                            bc.addPoint(Vector2.ZERO);
                                        }
                                        else
                                        {
                                            bc.addPoint(s.getPoint(pc - 1));
                                        }
                                        bc.addPoint(lastPoint / 64.0f);
                                        bc.addPoint(conicPoint / 64.0f);
                                        bc.addPoint(nextPoint / 64.0f);
                                        if (pc == 0)
                                        {
                                            s.appendShape(bc.realizeShape());
                                        }
                                        else
                                        {
                                            List <Vector2> subShape = bc.realizeShape().getPoints();
                                            for (List <Vector2> .Enumerator iter = subShape.GetEnumerator(); iter.MoveNext(); iter++)
                                            {
                                                if (iter != subShape.GetEnumerator())
                                                {
                                                    s.addPoint(iter.Current);
                                                }
                                            }
                                        }

                                        if (nextIsConnic)
                                        {
//
//ORIGINAL LINE: lastPoint = nextPoint;
                                            lastPoint = (nextPoint);
                                        }
                                    }
                                }
                            }
                        }

                        s.close();
                        s.translate((float)px, (float)py);
                        retVal.addShape(s);

                        px += slot.advance.x >> 6;
                        py += slot.advance.y >> 6;
                    }
                    FT_Done_Face(face);
                }
                FT_Done_FreeType(ftlib);
            }
            else
            {
                //C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __LINE__ macro:
                //C++ TO C# CONVERTER TODO TASK: There is no direct equivalent in C# to the C++ __FILE__ macro:
                throw ExceptionFactory.create(Mogre.ExceptionCodeType <Mogre.Exception.ExceptionCodes.ERR_INTERNAL_ERROR>(), "FreeType ERROR: FT_Init_FreeTyp", "Procedural::TextShape::realizeShapes()", __FILE__, __LINE__);
                ;
            }

            return(retVal);
        }