コード例 #1
0
 public static CCBMFontConfiguration Create(Stream fntFile, string fntFileName)
 {
     var pRet = new CCBMFontConfiguration();
     using (StreamReader sr = new StreamReader(fntFile))
     {
         string contents = sr.ReadToEnd();
         if (pRet.InitWithString(contents, fntFileName))
         {
             return pRet;
         }
     }
     return (null);
 }
コード例 #2
0
 /// <summary>
 /// This create method is left in the project because this class is loaded
 /// by the ContentManager. This create factory is a legitimate use of the
 /// self factory pattern.
 /// </summary>
 /// <param name="fntFile"></param>
 /// <returns></returns>
 public static CCBMFontConfiguration Create(string fntFile)
 {
     try
     {
         return CCApplication.SharedApplication.Content.Load<CCBMFontConfiguration>(fntFile);
     }
     catch (ContentLoadException)
     {
         var pRet = new CCBMFontConfiguration();
         if (pRet.InitWithFNTFile(fntFile))
         {
             return pRet;
         }
     }
     return null;
 }
コード例 #3
0
        protected virtual bool InitWithString(string theString, string fntFile, CCSize dimentions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                                              CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert(m_pConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                         "Invalid params for CCLabelBMFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                CCBMFontConfiguration newConf = FNTConfigLoadFile(fntFile);
                if (newConf == null)
                {
                    CCLog.Log("CCLabelBMFont: Impossible to create font. Please check file: '{0}'", fntFile);
                    return false;
                }

                m_pConfiguration = newConf;

                m_sFntFile = fntFile;

                if (texture == null)
                {
                    try
                    {
                        texture = CCTextureCache.SharedTextureCache.AddImage(m_pConfiguration.AtlasName);
                    }
                    catch (Exception)
                    {
                        // Try the 'images' ref location just in case.
                        try
                        {
                            texture =
                                CCTextureCache.SharedTextureCache.AddImage(System.IO.Path.Combine("images",
                                                                                                  m_pConfiguration
                                                                                                      .AtlasName));
                        }
                        catch (Exception)
                        {
                            // Lastly, try <font_path>/images/<font_name>
                            string dir = System.IO.Path.GetDirectoryName(m_pConfiguration.AtlasName);
                            string fname = System.IO.Path.GetFileName(m_pConfiguration.AtlasName);
                            string newName = System.IO.Path.Combine(System.IO.Path.Combine(dir, "images"), fname);
                            texture = CCTextureCache.SharedTextureCache.AddImage(newName);
                        }
                    }
                }
            }
            else
            {
                texture = new CCTexture2D();
            }

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            if (base.InitWithTexture(texture, theString.Length))
            {
                m_tDimensions = dimentions;

                m_pHAlignment = hAlignment;
                m_pVAlignment = vAlignment;

                m_cDisplayedOpacity = m_cRealOpacity = 255;
                m_tDisplayedColor = m_tRealColor = CCTypes.CCWhite;
                m_bCascadeOpacityEnabled = true;
                m_bCascadeColorEnabled = true;

                m_obContentSize = CCSize.Zero;

                m_bIsOpacityModifyRGB = m_pobTextureAtlas.Texture.HasPremultipliedAlpha;
                AnchorPoint = new CCPoint(0.5f, 0.5f);

                m_tImageOffset = imageOffset;

                m_pReusedChar = new CCSprite();
                m_pReusedChar.InitWithTexture(m_pobTextureAtlas.Texture, CCRect.Zero, false);
                m_pReusedChar.BatchNode = this;

                SetString(theString, true);

                return true;
            }
            return false;
        }
コード例 #4
0
        protected virtual bool InitWithString(string theString, string fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset)
        {
            Debug.Assert(m_pConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null), "Invalid params for CCLabelBMFont");

            CCTexture2D texture;

            if (!String.IsNullOrEmpty(fntFile))
            {
                CCBMFontConfiguration newConf = FNTConfigLoadFile(fntFile);
                Debug.Assert(newConf != null, "CCLabelBMFont: Impossible to create font. Please check file");

                m_pConfiguration = newConf;

                m_sFntFile = fntFile;

                try
                {
                texture = CCTextureCache.SharedTextureCache.AddImage(m_pConfiguration.AtlasName);
            }
                catch (Exception)
                {
                    // Try the 'images' ref location just in case.
                    try
                    {
                        texture = CCTextureCache.SharedTextureCache.AddImage(System.IO.Path.Combine("images", m_pConfiguration.AtlasName));
                    }
                    catch (Exception)
                    {
                        // Lastly, try <font_path>/images/<font_name>
                        string dir = System.IO.Path.GetDirectoryName(m_pConfiguration.AtlasName);
                        string fname = System.IO.Path.GetFileName(m_pConfiguration.AtlasName);
                        string newName = System.IO.Path.Combine(System.IO.Path.Combine(dir, "images"), fname);
                        texture = CCTextureCache.SharedTextureCache.AddImage(newName);
                    }
                }
            }
            else
            {
                texture = new CCTexture2D();
            }

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            if (base.InitWithTexture(texture, theString.Length))
            {
                m_pAlignment = alignment;
                m_tImageOffset = imageOffset;
                m_fWidth = width;
                m_cOpacity = 255;
                m_tColor = CCTypes.CCWhite;
                m_tContentSize = CCSize.Zero;
                m_bIsOpacityModifyRGB = m_pobTextureAtlas.Texture.HasPremultipliedAlpha;
                Label = (theString);
                AnchorPoint = new CCPoint(0.5f, 0.5f);
                return true;
            }
            return false;
        }
コード例 #5
0
ファイル: CCLabel.cs プロジェクト: huaqiangs/cocos2d-xna
        private CCBMFontConfiguration InitializeFont(string fontName, float fontSize, string charset)
        {
            if (m_pData == null)
            {
                InitializeTTFAtlas(1024, 1024);
            }

            if (String.IsNullOrEmpty(charset))
            {
                charset = " ";
            }

            var chars = new CCRawList<char>();

            var fontKey = GetFontKey(fontName, fontSize);

            CCBMFontConfiguration fontConfig;

            if (!s_pConfigurations.TryGetValue(fontKey, out fontConfig))
            {
                fontConfig = new CCBMFontConfiguration();
                s_pConfigurations.Add(fontKey, fontConfig);
            }

            for (int i = 0; i < charset.Length; i++)
            {
                var ch = charset[i];
                if (!fontConfig.m_pFontDefDictionary.ContainsKey(ch) && chars.IndexOf(ch) == -1)
                {
                    chars.Add(ch);
                }
            }

            if (chars.Count == 0)
            {
                return fontConfig;
            }

            CreateFont(fontName, fontSize, chars);

            fontConfig.m_nCommonHeight = (int)Math.Ceiling(GetFontHeight());

            int[] data = null;

            for (int i = 0; i < chars.Count; i++)
            {
                var s = chars[i].ToString();

                var charSize = GetMeasureString(s);

                int w = (int)Math.Ceiling(charSize.Width + 2);
                int h = (int)Math.Ceiling(charSize.Height + 2);

                if (data == null || data.Length < (w * h))
                {
                    data = new int[w * h];
                }

                unsafe
                {
                    int stride;
                    byte* pBase = GetBitmapData(s, out stride);

                    int minX = w;
                    int maxX = 0;
                    int minY = h;
                    int maxY = 0;

                    for (int y = 0; y < h; y++)
                    {
                        var row = (int*)(pBase + y * stride);

                        for (int x = 0; x < w; x++)
                        {
                            if (row[x] != 0)
                            {
                                minX = Math.Min(minX, x);
                                maxX = Math.Max(maxX, x);
                                minY = Math.Min(minY, y);
                                maxY = Math.Max(maxY, y);
                            }
                        }
                    }

                    w = Math.Max(maxX - minX + 1, 1);
                    h = Math.Max(maxY - minY + 1, 1);

                    //maxX = minX + w;
                    //maxY = minY + h;

                    int index = 0;
                    for (int y = minY; y <= maxY; y++)
                    {
                        var row = (int*)(pBase + y * stride);
                        for (int x = minX; x <= maxX; x++)
                        {
                            data[index] = row[x];
                            index++;
                        }
                    }

                    var region = AllocateRegion(w, h);

                    if (region.x >= 0)
                    {
                        SetRegionData(region, data, w);

                        var info = GetKerningInfo(chars[i]);

                        var fontDef = new CCBMFontConfiguration.CCBMFontDef()
                        {
                            charID = chars[i],
                            rect = new CCRect(region.x, region.y, region.width, region.height),
                            xOffset = minX, // + (int)Math.Ceiling(info.A),
                            yOffset = minY,
                            xAdvance = (int)Math.Ceiling(info.A + info.B + info.C)
                        };

                        fontConfig.CharacterSet.Add(chars[i]);
                        fontConfig.m_pFontDefDictionary.Add(chars[i], fontDef);
                    }
                    else
                    {
                        CCLog.Log("Texture atlas is full");
                    }
                }
            }

            m_bTextureDirty = true;

            return fontConfig;
        }
コード例 #6
0
 /// <summary>
 /// This create method is left in the project because this class is loaded
 /// by the ContentManager. This create factory is a legitimate use of the
 /// self factory pattern.
 /// </summary>
 /// <param name="fntFile"></param>
 /// <returns></returns>
 public static CCBMFontConfiguration Create(string fntFile)
 {
     try
     {
         return CCApplication.SharedApplication.Content.Load<CCBMFontConfiguration>(fntFile);
     }
     catch (InvalidCastException)
     {
         // Legacy check
         try
         {
             return new CCBMFontConfiguration(CCApplication.SharedApplication.Content.Load<cocos2d.CCBMFontConfiguration>(fntFile));
         }
         catch (Exception ex)
         {
             CCLog.Log("Failed to load FNT file at " + fntFile);
             CCLog.Log(ex.ToString());
         }
     }
     catch (Exception ex)
     {
         CCLog.Log("Failed to load FNT file at " + fntFile);
         CCLog.Log(ex.ToString());
     }
     var pRet = new CCBMFontConfiguration();
     if (pRet.InitWithFNTFile(fntFile))
     {
         return pRet;
     }
     return null;
 }
コード例 #7
0
        protected virtual bool InitWithString(string theString, string fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset)
        {
            Debug.Assert(m_pConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null), "Invalid params for CCLabelBMFont");

            CCTexture2D texture;

            if (!String.IsNullOrEmpty(fntFile))
            {
                CCBMFontConfiguration newConf = FNTConfigLoadFile(fntFile);
                Debug.Assert(newConf != null, "CCLabelBMFont: Impossible to create font. Please check file");

                m_pConfiguration = newConf;

                m_sFntFile = fntFile;

                try
                {
                    texture = CCTextureCache.SharedTextureCache.AddImage(m_pConfiguration.AtlasName);
                }
                catch (Exception)
                {
                    // Try the 'images' ref location just in case.
                    try
                    {
                        texture = CCTextureCache.SharedTextureCache.AddImage(System.IO.Path.Combine("images", m_pConfiguration.AtlasName));
                    }
                    catch (Exception)
                    {
                        // Lastly, try <font_path>/images/<font_name>
                        string dir     = System.IO.Path.GetDirectoryName(m_pConfiguration.AtlasName);
                        string fname   = System.IO.Path.GetFileName(m_pConfiguration.AtlasName);
                        string newName = System.IO.Path.Combine(System.IO.Path.Combine(dir, "images"), fname);
                        texture = CCTextureCache.SharedTextureCache.AddImage(newName);
                    }
                }
            }
            else
            {
                texture = new CCTexture2D();
            }

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            if (base.InitWithTexture(texture, theString.Length))
            {
                m_pAlignment          = alignment;
                m_tImageOffset        = imageOffset;
                m_fWidth              = width;
                m_cOpacity            = 255;
                m_tColor              = CCTypes.CCWhite;
                m_tContentSize        = CCSize.Zero;
                m_bIsOpacityModifyRGB = m_pobTextureAtlas.Texture.HasPremultipliedAlpha;
                Label       = (theString);
                AnchorPoint = new CCPoint(0.5f, 0.5f);
                return(true);
            }
            return(false);
        }
コード例 #8
0
        protected virtual bool InitWithString(string theString, string fntFile, CCSize dimentions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment,
                                              CCPoint imageOffset, CCTexture2D texture)
        {
            Debug.Assert(m_pConfiguration == null, "re-init is no longer supported");
            Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null),
                         "Invalid params for CCLabelBMFont");

            if (!String.IsNullOrEmpty(fntFile))
            {
                CCBMFontConfiguration newConf = FNTConfigLoadFile(fntFile);
                if (newConf == null)
                {
                    CCLog.Log("CCLabelBMFont: Impossible to create font. Please check file: '{0}'", fntFile);
                    return(false);
                }

                m_pConfiguration = newConf;

                m_sFntFile = fntFile;

                if (texture == null)
                {
                    try
                    {
                        texture = CCTextureCache.SharedTextureCache.AddImage(m_pConfiguration.AtlasName);
                    }
                    catch (Exception)
                    {
                        // Try the 'images' ref location just in case.
                        try
                        {
                            texture =
                                CCTextureCache.SharedTextureCache.AddImage(System.IO.Path.Combine("images",
                                                                                                  m_pConfiguration
                                                                                                  .AtlasName));
                        }
                        catch (Exception)
                        {
                            // Lastly, try <font_path>/images/<font_name>
                            string dir     = System.IO.Path.GetDirectoryName(m_pConfiguration.AtlasName);
                            string fname   = System.IO.Path.GetFileName(m_pConfiguration.AtlasName);
                            string newName = System.IO.Path.Combine(System.IO.Path.Combine(dir, "images"), fname);
                            texture = CCTextureCache.SharedTextureCache.AddImage(newName);
                        }
                    }
                }
            }
            else
            {
                texture = new CCTexture2D();
            }

            if (String.IsNullOrEmpty(theString))
            {
                theString = String.Empty;
            }

            if (base.InitWithTexture(texture, theString.Length))
            {
                m_tDimensions = dimentions;

                m_pHAlignment = hAlignment;
                m_pVAlignment = vAlignment;

                m_cDisplayedOpacity      = m_cRealOpacity = 255;
                m_tDisplayedColor        = m_tRealColor = CCTypes.CCWhite;
                m_bCascadeOpacityEnabled = true;
                m_bCascadeColorEnabled   = true;

                m_obContentSize = CCSize.Zero;

                m_bIsOpacityModifyRGB = m_pobTextureAtlas.Texture.HasPremultipliedAlpha;
                AnchorPoint           = new CCPoint(0.5f, 0.5f);

                m_tImageOffset = imageOffset;

                m_pReusedChar = new CCSprite();
                m_pReusedChar.InitWithTexture(m_pobTextureAtlas.Texture, CCRect.Zero, false);
                m_pReusedChar.BatchNode = this;

                SetString(theString, true);

                return(true);
            }
            return(false);
        }