Example #1
0
        public CCTLLine GetLine(int startRange, int endRange)
        {
            var breakText = label.Text;

            var stringRange  = breakText.Substring(startRange, endRange - startRange);
            int stringLength = string.IsNullOrEmpty(stringRange) ? 0 : stringRange.TrimEnd().Length;

            if (stringLength <= 0)
            {
                return(CCTLLine.NewCTLLine());
            }

            endRange = startRange + stringLength;

            CCFontLetterDefinition letterDefinition = new CCFontLetterDefinition();

            int charXOffset = 0;
            int charYOffset = 0;
            int charAdvance = 0;

            CCPoint letterPosition;
            var     nextFontPositionX        = 0.0f;
            var     nextFontPositionY        = 0.0f;
            var     contentScaleFactorWidth  = CCLabel.DefaultTexelToContentSizeRatios.Width;
            var     contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height;
            var     line = CCTLLine.NewCTLLine();
            var     additionalKerning = label.AdditionalKerning;

            for (int x = startRange; x < endRange; x++)
            {
                var c = breakText[x];

                if (fontAtlas.GetLetterDefinitionForChar(c, out letterDefinition))
                {
                    charXOffset = (int)letterDefinition.XOffset;
                    charYOffset = (int)letterDefinition.YOffset;
                    charAdvance = (int)letterDefinition.XAdvance;
                }
                else
                {
                    charXOffset = -1;
                    charYOffset = -1;
                    charAdvance = -1;
                }

                var kerning = kernings[x];
                letterDefinition.Kerning = kerning;

                letterPosition.X = (nextFontPositionX + charXOffset + kerning) / contentScaleFactorWidth;
                letterPosition.Y = (nextFontPositionY - charYOffset) / contentScaleFactorHeight;


                nextFontPositionX += charAdvance + kerning + additionalKerning;

                line.AddGlyph(letterPosition, letterDefinition, x);
            }

            return(line);
        }
Example #2
0
        internal override CCFontAtlas CreateFontAtlas()
        {
            var atlas = new CCFontAtlas(this);

            // Try to load the texture
            CCTexture2D atlasTexture = null;

            float loadedSize = fontSize;

            SpriteFont font = CCSpriteFontCache.SharedInstance.TryLoadFont(fontName, fontSize, out loadedSize);

            if (font == null)
            {
                atlasTexture = new CCTexture2D();
            }
            else
            {
                atlasTexture = new CCTexture2D(font.Texture);
            }

            // add the texture (only one for now)
            atlas.AddTexture(atlasTexture, 0);

            // Set the atlas's common height
            atlas.CommonHeight = font.LineSpacing;
            // Set the default character to us if a character does not exist in the font
            atlas.DefaultCharacter = font.DefaultCharacter;

            var glyphs     = font.GetGlyphs();
            var reusedRect = Rectangle.Empty;

            foreach (var character in font.Characters)
            {
                var glyphDefintion = new CCFontLetterDefinition();

                glyphDefintion.LetterChar = character;

                var glyphDef = glyphs[character];

                glyphDefintion.XOffset = glyphDef.LeftSideBearing + glyphDef.Cropping.X;

                glyphDefintion.YOffset = glyphDef.Cropping.Y;

                reusedRect             = glyphDef.BoundsInTexture;
                glyphDefintion.Subrect = new CCRect(reusedRect.X, reusedRect.Y, reusedRect.Width, reusedRect.Height);

                reusedRect = glyphDef.Cropping;
                glyphDefintion.Cropping = new CCRect(reusedRect.X, reusedRect.Y, reusedRect.Width, reusedRect.Height);

                glyphDefintion.TextureID = 0;

                glyphDefintion.IsValidDefinition = true;
                glyphDefintion.XAdvance          = (int)(font.Spacing + glyphDef.Width + glyphDef.RightSideBearing);//(int)glyphDef.WidthIncludingBearings;

                atlas.AddLetterDefinition(glyphDefintion);
            }

            return(atlas);
        }
        internal bool AddGlyph(CCPoint point, CCFontLetterDefinition letterDef, int spriteIndex)
        {

            var letterInfo = new LetterInfo();

            letterInfo.Definition = letterDef;
            letterInfo.Position = point;
            letterInfo.ContentSize.Width = letterDef.Width;
            letterInfo.ContentSize.Height = letterDef.Height;
            bounds.Width = letterInfo.Position.X + letterDef.XAdvance + letterDef.Kerning;
            if (bounds.Height < letterDef.Height)
                bounds.Height = letterDef.Height;

            letterInfo.AtlasIndex = spriteIndex;
            glyphRun.Add(letterInfo);

            return letterInfo.Definition.IsValidDefinition;
        }
Example #4
0
        internal bool AddGlyph(CCPoint point, CCFontLetterDefinition letterDef, int spriteIndex)
        {
            var tmpInfo = new LetterInfo();

            tmpInfo.Definition         = letterDef;
            tmpInfo.Position           = point;
            tmpInfo.ContentSize.Width  = letterDef.Width;
            tmpInfo.ContentSize.Height = letterDef.Height;
            bounds.Width = tmpInfo.Position.X + tmpInfo.ContentSize.Width;
            if (bounds.Height < letterDef.Height)
            {
                bounds.Height = letterDef.Height;
            }

            tmpInfo.AtlasIndex = spriteIndex;
            glyphRun.Add(tmpInfo);

            return(tmpInfo.Definition.IsValidDefinition);
        }
Example #5
0
        internal bool AddGlyph(CCPoint point, CCFontLetterDefinition letterDef, int spriteIndex)
        {
            var letterInfo = new LetterInfo();

            letterInfo.Definition         = letterDef;
            letterInfo.Position           = point;
            letterInfo.ContentSize.Width  = letterDef.Width;
            letterInfo.ContentSize.Height = letterDef.Height;
            bounds.Width = letterInfo.Position.X + letterDef.XAdvance + letterDef.Kerning;
            if (bounds.Height < letterDef.Height)
            {
                bounds.Height = letterDef.Height;
            }

            letterInfo.AtlasIndex = spriteIndex;
            glyphRun.Add(letterInfo);

            return(letterInfo.Definition.IsValidDefinition);
        }
Example #6
0
        public int SuggestLineBreak(int startIndex, float width)
        {
            var breakText = label.Text;

            int stringLength = string.IsNullOrEmpty(breakText) ? 0 : breakText.Length;

            if (stringLength <= 0)
            {
                return(0);
            }

            CCFontLetterDefinition letterDefinition = new CCFontLetterDefinition();

            bool  isStartOfLine = false;
            float startOfLine   = -1;

            float scaleX = label.ScaleX;

            var limit       = stringLength;
            int charXOffset = 0;
            int charYOffset = 0;
            int charAdvance = 0;

            CCPoint letterPosition;
            var     nextFontPositionX        = 0.0f;
            var     nextFontPositionY        = 0.0f;
            var     contentScaleFactorWidth  = CCLabel.DefaultTexelToContentSizeRatios.Width;
            var     contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height;

            var breakPosition = 0;

            var lineBreaking = label.LabelFormat.LineBreaking;

            for (int j = startIndex; j < limit; j++)
            {
                var c            = breakText[j];
                var breakElement = lineBreakElements[j];
                breakPosition = breakElement.Position;

                if (breakElement.Condition == LineBreakCondition.Mandatory)
                {
                    return(breakPosition - startIndex);
                }

                if (fontAtlas.GetLetterDefinitionForChar(c, out letterDefinition))
                {
                    charXOffset = (int)letterDefinition.XOffset;
                    charYOffset = (int)letterDefinition.YOffset;
                    charAdvance = (int)letterDefinition.XAdvance;
                }
                else
                {
                    charXOffset = -1;
                    charYOffset = -1;
                    charAdvance = -1;
                }

                var kerning = kernings[j];
                letterPosition.X = (nextFontPositionX + charXOffset + kerning) / contentScaleFactorWidth;
                letterPosition.Y = (nextFontPositionY - charYOffset) / contentScaleFactorHeight;

                nextFontPositionX += charAdvance + kerning;

                if (!isStartOfLine)
                {
                    startOfLine   = letterPosition.X * scaleX;;
                    isStartOfLine = true;
                }

                // 1) Whitespace.
                // TODO: Handle CJK
                bool isCJK = false;

                // Whitespace. Allowed
                if (breakElement.Condition == LineBreakCondition.Allowed)
                {
                    continue;
                }


                float posRight = (letterPosition.X + letterDefinition.Width) * scaleX;

                //Console.WriteLine(c + " " + posRight + "lw: " + lineWidth + " le: " + breakElement);

                // Out of bounds.
                if (posRight - startOfLine > width)
                {
                    if (lineBreaking == CCLabelLineBreak.Word && !isCJK)
                    {
                        // Calculate the previous allowable position based on the Line Breaking element condition.
                        int na = j;
                        while (breakElement.Condition != LineBreakCondition.Allowed && na-- > 0)
                        {
                            breakElement = lineBreakElements[na];
                        }
                        return(breakElement.Position + 1 - startIndex);
                    }
                    else
                    {
                        return(breakElement.Position - startIndex);
                    }
                }
            }

            return(breakPosition + 1 - startIndex);
        }
Example #7
0
        internal override CCFontAtlas CreateFontAtlas()
        {
            var atlas = new CCFontAtlas(this);

            // Check if any Bitmap Font configurations are loaded at all.
            if (Configuration.Glyphs.Count == 0)
                return null;

            if (Configuration.CommonHeight == 0)
                return null;


            // Set the atlas's common height
            atlas.CommonHeight = Configuration.CommonHeight;

            foreach (var glyph in Configuration.Glyphs)
            {
                var glyphDefintion = new CCFontLetterDefinition();

                glyphDefintion.LetterChar = (char)glyph.Key;

                var glyphDef = glyph.Value;

                glyphDefintion.XOffset = glyphDef.XOffset;
                glyphDefintion.YOffset = glyphDef.YOffset;

                glyphDefintion.Subrect = glyphDef.Subrect;

                glyphDefintion.TextureID = 0;

                glyphDefintion.IsValidDefinition = true;
                glyphDefintion.XAdvance = glyphDef.XAdvance;

                atlas.AddLetterDefinition(glyphDefintion);
            }

            // Try to load the texture
            CCTexture2D atlasTexture = null;

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

            if (atlasTexture == null)
                atlasTexture = new CCTexture2D();

            // add the texture (only one for now)
            atlas.AddTexture(atlasTexture, 0);

            return atlas;
        }
Example #8
0
        public bool GetLetterDefinitionForChar(char letteCharUTF16, out CCFontLetterDefinition outDefinition)
        {
            var found = FontLetterDefinitions.TryGetValue(letteCharUTF16, out outDefinition);

            return((found) ? outDefinition.IsValidDefinition : false);
        }
Example #9
0
 public void AddLetterDefinition(CCFontLetterDefinition letterDefinition)
 {
     FontLetterDefinitions[letterDefinition.LetterChar] = letterDefinition;
 }
Example #10
0
        internal override CCFontAtlas CreateFontAtlas()
        {
            var atlas = new CCFontAtlas(this);

            // Check if any Bitmap Font configurations are loaded at all.
            if (Configuration.Glyphs.Count == 0)
            {
                return(null);
            }

            if (Configuration.CommonHeight == 0)
            {
                return(null);
            }


            // Set the atlas's common height
            atlas.CommonHeight = Configuration.CommonHeight;

            foreach (var glyph in Configuration.Glyphs)
            {
                var glyphDefintion = new CCFontLetterDefinition();

                glyphDefintion.LetterChar = (char)glyph.Key;

                var glyphDef = glyph.Value;

                glyphDefintion.XOffset = glyphDef.XOffset;
                glyphDefintion.YOffset = glyphDef.YOffset;

                glyphDefintion.Subrect = glyphDef.Subrect;

                glyphDefintion.TextureID = 0;

                glyphDefintion.IsValidDefinition = true;
                glyphDefintion.XAdvance          = glyphDef.XAdvance;

                atlas.AddLetterDefinition(glyphDefintion);
            }

            // Try to load the texture
            CCTexture2D atlasTexture = null;

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

            if (atlasTexture == null)
            {
                atlasTexture = new CCTexture2D();
            }

            // add the texture (only one for now)
            atlas.AddTexture(atlasTexture, 0);

            return(atlas);
        }
        public CCTLLine GetLine (int startRange, int endRange)
        {
            var breakText = label.Text;

            var stringRange = breakText.Substring(startRange, endRange - startRange);
            int stringLength = string.IsNullOrEmpty(stringRange) ? 0 : stringRange.TrimEnd().Length;

            if (stringLength <= 0)
                return CCTLLine.NewCTLLine();

            endRange = startRange + stringLength;

            CCFontLetterDefinition letterDefinition = new CCFontLetterDefinition();

            int charXOffset = 0;
            int charYOffset = 0;
            int charAdvance = 0;

            CCPoint letterPosition;
            var nextFontPositionX = 0.0f;
            var nextFontPositionY = 0.0f;
            var contentScaleFactorWidth = CCLabel.DefaultTexelToContentSizeRatios.Width;
            var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height;
            var line = CCTLLine.NewCTLLine();
            var additionalKerning = label.AdditionalKerning;

            for (int x = startRange; x < endRange; x++)
            {
                var c = breakText[x];

                if (fontAtlas.GetLetterDefinitionForChar(c, out letterDefinition))
                {
                    charXOffset         = (int)letterDefinition.XOffset;
                    charYOffset         = (int)letterDefinition.YOffset;
                    charAdvance         = (int)letterDefinition.XAdvance;
                }
                else
                {
                    charXOffset         = -1;
                    charYOffset         = -1;
                    charAdvance         = -1;
                }

                var kerning = kernings[x];
                letterDefinition.Kerning = kerning;

                letterPosition.X = (nextFontPositionX + charXOffset + kerning) / contentScaleFactorWidth;
                letterPosition.Y = (nextFontPositionY - charYOffset) / contentScaleFactorHeight;


                nextFontPositionX += charAdvance + kerning + additionalKerning;

                line.AddGlyph(letterPosition, letterDefinition, x);
            }

            return line;
        }
        public int SuggestLineBreak (int startIndex, float width)
        {
            var breakText = label.Text;

            int stringLength = string.IsNullOrEmpty(breakText) ? 0 : breakText.Length;
            if (stringLength <= 0)
                return 0;

            CCFontLetterDefinition letterDefinition = new CCFontLetterDefinition();

            bool isStartOfLine = false;
            float startOfLine = -1;
            var additionalKerning = label.AdditionalKerning;

            float scaleX = label.ScaleX;

            var limit = stringLength;
            int charXOffset = 0;
            int charYOffset = 0;
            int charAdvance = 0;

            CCPoint letterPosition;
            var nextFontPositionX = 0.0f;
            var nextFontPositionY = 0.0f;
            var contentScaleFactorWidth = CCLabel.DefaultTexelToContentSizeRatios.Width;
            var contentScaleFactorHeight = CCLabel.DefaultTexelToContentSizeRatios.Height;

            var breakPosition = 0;

            var lineBreaking = label.LabelFormat.LineBreaking;

            for (int j = startIndex; j < limit; j++)
            {            
                var c = breakText[j];
                var breakElement = lineBreakElements[j];
                breakPosition = breakElement.Position;

                if (breakElement.Condition == LineBreakCondition.Mandatory)
                    return breakPosition - startIndex;

                if (fontAtlas.GetLetterDefinitionForChar(c, out letterDefinition))
                {
                    charXOffset         = (int)letterDefinition.XOffset;
                    charYOffset         = (int)letterDefinition.YOffset;
                    charAdvance         = (int)letterDefinition.XAdvance;
                }
                else
                {
                    charXOffset         = -1;
                    charYOffset         = -1;
                    charAdvance         = -1;
                }

                var kerning = kernings[j];
                letterPosition.X = (nextFontPositionX + charXOffset + kerning) / contentScaleFactorWidth;
                letterPosition.Y = (nextFontPositionY - charYOffset) / contentScaleFactorHeight;

                nextFontPositionX += charAdvance + kerning + additionalKerning;

                if (!isStartOfLine)
                {
                    startOfLine = letterPosition.X * scaleX;;
                    isStartOfLine  = true;
                }

                // 1) Whitespace.
                // TODO: Handle CJK
                bool isCJK = false;

                // Whitespace. Allowed
                if (breakElement.Condition == LineBreakCondition.Allowed)
                {
                    continue;
                }


                float posRight = (letterPosition.X + letterDefinition.Width) * scaleX;

                //Console.WriteLine(c + " " + posRight + "lw: " + lineWidth + " le: " + breakElement);

                // Out of bounds.
                if (posRight - startOfLine > width)
                {
                    if (lineBreaking == CCLabelLineBreak.Word && !isCJK)
                    {
                        // Calculate the previous allowable position based on the Line Breaking element condition.
                        int na = j;
                        while (breakElement.Condition != LineBreakCondition.Allowed && na-- > 0)
                        {
                            breakElement = lineBreakElements[na];
                        }
                        var count = breakElement.Position + 1 - startIndex;
                        return count > 0 ? count : 1;
                    }
                    else
                    {
                        return breakElement.Position - startIndex;
                    }
                }
            }

            return breakPosition + 1 - startIndex;
        }
Example #13
0
        internal override CCFontAtlas CreateFontAtlas()
        {
            float loadedSize = fontSize;
            SpriteFont font = CCSpriteFontCache.SharedInstance.TryLoadFont(fontName, fontSize, out loadedSize);
            if (font == null)
            {
                return null;
            }
#if XNA
            CCTexture2D atlasTexture = new CCTexture2D();
#else
            CCTexture2D atlasTexture = new CCTexture2D(font.Texture);
#endif

            if (loadedSize != 0)
            {
                fontScale = fontSize / loadedSize * CCSpriteFontCache.FontScale;
            }

            var atlas = new CCFontAtlas(this);
            // add the texture (only one for now)
            atlas.AddTexture(atlasTexture, 0);

            // Set the atlas's common height
            atlas.CommonHeight = font.LineSpacing;
            // Set the default character to us if a character does not exist in the font
            atlas.DefaultCharacter = font.DefaultCharacter;

#if !XNA
            
            var glyphs = font.GetGlyphs();
            var reusedRect = Rectangle.Empty;

            foreach ( var character in font.Characters)
            {
                var glyphDefintion = new CCFontLetterDefinition();

                glyphDefintion.LetterChar = character;

                var glyphDef = glyphs[character];

                glyphDefintion.XOffset = glyphDef.LeftSideBearing + glyphDef.Cropping.X;

                glyphDefintion.YOffset = glyphDef.Cropping.Y;

                reusedRect = glyphDef.BoundsInTexture;
                glyphDefintion.Subrect = new CCRect(reusedRect.X, reusedRect.Y, reusedRect.Width, reusedRect.Height);

                reusedRect = glyphDef.Cropping;
                glyphDefintion.Cropping = new CCRect(reusedRect.X, reusedRect.Y, reusedRect.Width, reusedRect.Height);

                glyphDefintion.TextureID = 0;

                glyphDefintion.IsValidDefinition = true;
                //glyphDefintion.XAdvance = (int)(font.Spacing + glyphDef.Width + glyphDef.RightSideBearing);
                glyphDefintion.XAdvance = (int)glyphDef.WidthIncludingBearings;

                atlas.AddLetterDefinition(glyphDefintion);

            }
#endif
            return atlas;
        }
Example #14
0
 public void AddLetterDefinition(CCFontLetterDefinition letterDefinition)
 {
     FontLetterDefinitions[letterDefinition.LetterChar] = letterDefinition;
 }
Example #15
0
        public bool GetLetterDefinitionForChar(char letteCharUTF16, out CCFontLetterDefinition outDefinition)
        {
            var found = FontLetterDefinitions.TryGetValue(letteCharUTF16, out outDefinition);

            return (found) ? outDefinition.IsValidDefinition : false;
        }