/// <summary>
        /// 创建字体如果能找到则直接返回
        /// </summary>
        /// <param name="fileName">字体TTF文件路径</param>
        /// <param name="fontName">字体名</param>
        /// <param name="fontSize">字体大小</param>
        /// <returns></returns>
        public IntPtr CreateFontFromBuffer(byte[] buffer, string fontName, uint faceIndex = 0)
        {
            IntPtr ret        = FindFont(fontName, faceIndex);
            var    defaultPrt = default(IntPtr);

            if (ret != defaultPrt)
            {
                return(ret);
            }
            if (buffer == null || buffer.Length <= 0)
            {
                return(defaultPrt);
            }
            int err = New_Memory_Face(m_FreeTypePointer, buffer, faceIndex, out ret);

            if (err != 0)
            {
                throw new Exception(string.Format("Could not open font: errCode {0:D}", err));
            }

            FontType key = new FontType();

            key.fontName  = fontName;
            key.faceIndex = faceIndex;
            FreeTypeFont font = new FreeTypeFont();

            font.font       = ret;
            m_FontsMap[key] = font;
            ApplyFont(font);
            return(ret);
        }
Beispiel #2
0
 // 查找文字
 public PicNode <FontRectKey> FindChar(char value, FreeTypeFont font)
 {
     if (m_Combine == null || font == null || font.font == null)
     {
         return(null);
     }
     return(m_Combine.FindPicNode(value, font.currentSize, font.sizeType, font.hDpi, font.vDpi));
 }
 private void ChangeFontSize(FreeTypeFont font, uint size)
 {
     if (font == null || font.font == default(IntPtr))
     {
         return;
     }
     if (font.currentSize == size)
     {
         return;
     }
     font.currentSize = size;
     ApplyFont(font);
 }
        private void ChangePixelMode(uint size, FreeTypeFont font)
        {
            if (font == null || font.font == null)
            {
                return;
            }
            bool isChanged = font.currentSize != size || font.sizeType != FreeTypeSizeType.usePixel;

            if (!isChanged)
            {
                return;
            }
            font.currentSize = size;
            font.sizeType    = FreeTypeSizeType.usePixel;
            ApplyFont(font);
        }
        private void ChangeDPIMode(uint hDpi, uint vDpi, uint size, FreeTypeFont font)
        {
            if (font == null || font.font == null)
            {
                return;
            }
            bool isChanged = font.hDpi != hDpi || font.vDpi != vDpi || font.currentSize != size || font.sizeType != FreeTypeSizeType.useDPI;

            if (!isChanged)
            {
                return;
            }
            font.hDpi        = hDpi;
            font.vDpi        = vDpi;
            font.currentSize = size;
            font.sizeType    = FreeTypeSizeType.useDPI;
            ApplyFont(font);
        }
        private void DestroyFont(FreeTypeFont font)
        {
            if (font == null)
            {
                return;
            }
            var defaultPtr = default(IntPtr);

            if (font.font != defaultPtr)
            {
                Done_Face(font.font);
                font.font = defaultPtr;
            }
            if (font.fontTexture != null)
            {
                font.fontTexture.OnDestroy();
                font.fontTexture = null;
            }
        }
        private void ApplyFont(FreeTypeFont font)
        {
            if (font == null || font.font == default(IntPtr))
            {
                return;
            }
            int ret = -1;

            if (font.sizeType == FreeTypeSizeType.useDPI)
            {
                ret = Set_Char_Size(font.font, 0, (int)font.currentSize, font.hDpi, font.vDpi);
            }
            else if (font.sizeType == FreeTypeSizeType.usePixel)
            {
                ret = Set_Pixel_Sizes(font.font, 0, font.currentSize);
            }
            if (ret != 0)
            {
                throw new Exception("ApplyFont Error!");
            }
        }