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); }
public CCTLTextLayout(CCLabel label) { this.label = label; this.fontAtlas = label.FontAtlas; this.font = label.FontAtlas.Font; this.text = label.Text; // Obtain the kernings for the text. ComputeHorizontalKernings(this.text); // Flag the possible line breaks base on the current label text. LineBreak(this.text); }
protected void InitSpriteFont(string theString, string fntFile, float fontSize, CCSize dimensions, CCLabelFormat labelFormat, CCPoint imageOffset, CCTexture2D texture) { Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null), "Invalid params for CCLabel SpriteFont"); if (!String.IsNullOrEmpty(fntFile)) { try { FontAtlas = CCFontAtlasCache.GetFontAtlasSpriteFont(fntFile, fontSize, imageOffset); } catch {} if (FontAtlas == null) { CCLog.Log("SpriteFont CCLabel: Impossible to create font. Please check file: '{0}'", fntFile); return; } } AnchorPoint = CCPoint.AnchorMiddle; currentLabelType = CCLabelType.SpriteFont; if (String.IsNullOrEmpty(theString)) { theString = String.Empty; } // Initialize the TextureAtlas along with children. var capacity = theString.Length; BlendFunc = CCBlendFunc.AlphaBlend; if (capacity == 0) { capacity = defaultSpriteBatchCapacity; } UpdateBlendFunc(); // no lazy alloc in this node Children = new CCRawList <CCNode>(capacity); Descendants = new CCRawList <CCSprite>(capacity); this.labelDimensions = dimensions; horzAlignment = labelFormat.Alignment; vertAlignment = labelFormat.LineAlignment; IsOpacityCascaded = true; ContentSize = CCSize.Zero; IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha; AnchorPoint = CCPoint.AnchorMiddle; ImageOffset = imageOffset; Text = theString; }
protected void InitSpriteFont(string theString, string fntFile, float fontSize, CCSize dimensions, CCLabelFormat labelFormat, CCPoint imageOffset, CCTexture2D texture) { Debug.Assert((theString == null && fntFile == null) || (theString != null && fntFile != null), "Invalid params for CCLabel SpriteFont"); if (!String.IsNullOrEmpty(fntFile)) { try { FontAtlas = CCFontAtlasCache.GetFontAtlasSpriteFont(fntFile, fontSize, imageOffset); Scale = FontAtlas.Font.FontScale; } catch {} if (FontAtlas == null) { CCLog.Log("SpriteFont CCLabel: Impossible to create font. Please check file: '{0}'", fntFile); return; } } AnchorPoint = CCPoint.AnchorMiddle; LabelType = CCLabelType.SpriteFont; if (String.IsNullOrEmpty(theString)) { theString = String.Empty; } // Initialize the TextureAtlas along with children. var capacity = theString.Length; BlendFunc = CCBlendFunc.AlphaBlend; if (capacity == 0) { capacity = defaultSpriteBatchCapacity; } UpdateBlendFunc(); // no lazy alloc in this node Children = new CCRawList<CCNode>(capacity); Descendants = new CCRawList<CCSprite>(capacity); this.labelDimensions = dimensions; horzAlignment = labelFormat.Alignment; vertAlignment = labelFormat.LineAlignment; IsOpacityCascaded = true; ContentSize = CCSize.Zero; IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha; AnchorPoint = CCPoint.AnchorMiddle; ImageOffset = imageOffset; Text = theString; }
protected void InitBMFont(string theString, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, CCPoint imageOffset, CCTexture2D texture) { Debug.Assert(FontConfiguration == 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)) { try { FontAtlas = CCFontAtlasCache.GetFontAtlasFNT(fntFile, imageOffset); } catch {} if (FontAtlas == null) { CCLog.Log("Bitmap Font CCLabel: Impossible to create font. Please check file: '{0}'", fntFile); return; } } AnchorPoint = CCPoint.AnchorMiddle; FontConfiguration = CCBMFontConfiguration.FontConfigurationWithFile(fntFile); LabelType = CCLabelType.BitMapFont; if (String.IsNullOrEmpty(theString)) { theString = String.Empty; } // Initialize the TextureAtlas along with children. var capacity = theString.Length; BlendFunc = CCBlendFunc.AlphaBlend; if (capacity == 0) { capacity = defaultSpriteBatchCapacity; } UpdateBlendFunc(); // no lazy alloc in this node Children = new CCRawList<CCNode>(capacity); Descendants = new CCRawList<CCSprite>(capacity); this.labelDimensions = dimensions; horzAlignment = hAlignment; vertAlignment = vAlignment; IsOpacityCascaded = true; // We use base here so we do not trigger an update internally. base.ContentSize = CCSize.Zero; IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha; AnchorPoint = CCPoint.AnchorMiddle; ImageOffset = imageOffset; Text = theString; }
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; }
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); }
protected void InitBMFont(string theString, string fntFile, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, CCPoint imageOffset, CCTexture2D texture) { Debug.Assert(FontConfiguration == 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)) { try { FontAtlas = CCFontAtlasCache.GetFontAtlasFNT(fntFile, imageOffset); } catch {} if (FontAtlas == null) { CCLog.Log("Bitmap Font CCLabel: Impossible to create font. Please check file: '{0}'", fntFile); return; } } AnchorPoint = CCPoint.AnchorMiddle; FontConfiguration = CCBMFontConfiguration.FontConfigurationWithFile(fntFile); LabelType = CCLabelType.BitMapFont; if (String.IsNullOrEmpty(theString)) { theString = String.Empty; } // Initialize the TextureAtlas along with children. var capacity = theString.Length; BlendFunc = CCBlendFunc.AlphaBlend; if (capacity == 0) { capacity = defaultSpriteBatchCapacity; } UpdateBlendFunc(); // no lazy alloc in this node Children = new CCRawList <CCNode>(capacity); Descendants = new CCRawList <CCSprite>(capacity); this.labelDimensions = dimensions; horzAlignment = hAlignment; vertAlignment = vAlignment; IsOpacityCascaded = true; // We use base here so we do not trigger an update internally. base.ContentSize = CCSize.Zero; IsColorModifiedByOpacity = TextureAtlas.Texture.HasPremultipliedAlpha; AnchorPoint = CCPoint.AnchorMiddle; ImageOffset = imageOffset; Text = theString; }
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; }