Inheritance: Resource
コード例 #1
0
ファイル: FontFileImporter.cs プロジェクト: gitMaxim/duality
 public void ImportFile(string srcFile, string targetName, string targetDir)
 {
     string[] output = this.GetOutputFiles(srcFile, targetName, targetDir);
     Font res = new Font();
     res.LoadCustomFamilyData(srcFile);
     res.ReloadData();
     res.Save(output[0]);
 }
コード例 #2
0
ファイル: FontFileImporter.cs プロジェクト: ninja2003/duality
        public void ImportFile(string srcFile, string targetName, string targetDir)
        {
            string[] output = this.GetOutputFiles(srcFile, targetName, targetDir);
            Font font = new Font();

            font.SourcePath = srcFile;
            font.EmbeddedTrueTypeFont = File.ReadAllBytes(srcFile);
            font.RenderGlyphs();
            font.Save(output[0]);
        }
コード例 #3
0
        public void Import(IAssetImportEnvironment env)
        {
            // Handle all available input. No need to filter or ask for this anymore, as
            // the preparation step already made a selection with AcceptsInput. We won't
            // get any input here that didn't match.
            foreach (AssetImportInput input in env.Input)
            {
                // Request a target Resource with a name matching the input
                ContentRef <DualityFont> targetRef = env.GetOutput <DualityFont>(input.AssetName);

                // If we successfully acquired one, proceed with the import
                if (targetRef.IsAvailable)
                {
                    DualityFont target = targetRef.Res;

                    // Retrieve import parameters
                    float     size            = env.GetOrInitParameter(targetRef, "Size", 16.0f);
                    FontStyle style           = env.GetOrInitParameter(targetRef, "Style", FontStyle.Regular);
                    string    extendedCharSet = env.GetOrInitParameter(targetRef, "ExtendedCharSet", string.Empty);
                    bool      antialiasing    = env.GetOrInitParameter(targetRef, "AntiAlias", true);
                    bool      monospace       = env.GetOrInitParameter(targetRef, "Monospace", false);

                    // Load the TrueType Font and render all the required glyphs
                    byte[]           trueTypeData = File.ReadAllBytes(input.Path);
                    RenderedFontData fontData     = this.RenderGlyphs(
                        trueTypeData,
                        size,
                        style,
                        !string.IsNullOrEmpty(extendedCharSet) ? new FontCharSet(extendedCharSet) : null,
                        antialiasing,
                        monospace);

                    // Transfer our rendered Font data to the Font Resource
                    target.SetGlyphData(
                        fontData.Bitmap,
                        fontData.Atlas,
                        fontData.GlyphData,
                        fontData.Metrics);

                    // Add the requested output to signal that we've done something with it
                    env.AddOutput(targetRef, input.Path);
                }
            }
        }
コード例 #4
0
        public void Import(IAssetImportEnvironment env)
        {
            foreach (AssetImportInput input in env.Input)
            {
                // Request a target Resource with a name matching the input
                ContentRef <Duality.Resources.Font> targetRef = env.GetOutput <Duality.Resources.Font>(input.AssetName);

                // If we successfully acquired one, proceed with the import
                if (targetRef.IsAvailable)
                {
                    Duality.Resources.Font target = targetRef.Res;

                    var fileFormat = FigureOutFontFileFormat(input.Path);

                    switch (fileFormat)
                    {
                    case FontFileFormat.BMFont:
                    {
                        var fontData = LoadBMFontData(input.Path);

                        var textureData = LoadPixelData(fontData, Path.GetFullPath(Path.GetDirectoryName(input.Path)));
                        target.SetGlyphData(textureData,
                                            fontData.Atlas.ToArray(),
                                            fontData.Glyps.ToArray(),
                                            fontData.Height,
                                            fontData.Ascent,
                                            fontData.BodyAscent,
                                            fontData.Descent,
                                            fontData.Baseline);

                        target.Size = fontData.Size;
                        //target.Kerning = true;


                        env.AddOutput(targetRef, input.Path);

                        break;
                    }
                    }
                }
            }
        }
コード例 #5
0
 private void RetrieveResources()
 {
     if (!this.bigFont.IsAvailable)
     {
         Font bigFontRes = new Font();
         bigFontRes.Family = System.Drawing.FontFamily.GenericSansSerif.Name;
         bigFontRes.Size = 32;
         bigFontRes.Kerning = true;
         bigFontRes.ReloadData();
         ContentProvider.AddContent(bigFont.Path, bigFontRes);
     }
 }
コード例 #6
0
ファイル: FormattedText.cs プロジェクト: ninja2003/duality
            public Element NextElement(bool stopAtLineBreak = false)
            {
                if (this.elemIndex >= this.parent.elements.Length) return null;
                Element elem = this.parent.elements[this.elemIndex];

                if (elem is TextElement && this.font != null)
                {
                    TextElement textElem = elem as TextElement;

                    string textToDisplay;
                    string fittingText;
                    // Word wrap by glyph / word
                    if (this.parent.maxWidth > 0 && this.parent.wrapMode != WrapMode.Element)
                    {
                        FitTextMode fitMode = FitTextMode.ByChar;
                        if (this.parent.wrapMode == WrapMode.Word)
                            fitMode = (this.lineAlign == Alignment.Right) ? FitTextMode.ByWordLeadingSpace : FitTextMode.ByWordTrailingSpace;
                        textToDisplay = textElem.Text.Substring(this.curElemWrapIndex, textElem.Text.Length - this.curElemWrapIndex);
                        fittingText = this.font.FitText(textToDisplay, this.lineAvailWidth - (this.offset.X - this.lineBeginX), fitMode);

                        // If by-word results in instant line break: Do it by glyph instead
                        if (this.offset.X == this.lineBeginX && fittingText.Length == 0 && this.parent.wrapMode == WrapMode.Word)
                            fittingText = this.font.FitText(textToDisplay, this.lineAvailWidth - (this.offset.X - this.lineBeginX), FitTextMode.ByChar);

                        // If doing it by glyph results in an instant line break: Use at least one glyph anyway
                        if (this.lineAvailWidth == this.parent.maxWidth &&
                            this.offset.X == this.lineBeginX &&
                            this.parent.maxHeight == 0 &&
                            fittingText.Length == 0) fittingText = textToDisplay.Substring(0, 1);
                    }
                    // No word wrap (or by whole element)
                    else
                    {
                        textToDisplay = textElem.Text;
                        fittingText = textElem.Text;
                    }
                    Vector2 textElemSize = this.font.MeasureText(fittingText);

                    // Perform word wrap by whole Element
                    if (this.parent.maxWidth > 0 && this.parent.wrapMode == WrapMode.Element)
                    {
                        if ((this.lineAvailWidth < this.parent.maxWidth || this.offset.X > this.lineBeginX || this.parent.maxHeight > 0) &&
                            this.offset.X + textElemSize.X > this.lineAvailWidth)
                        {
                            if (stopAtLineBreak)	return null;
                            else					this.PerformNewLine();
                            if (this.offset.Y + this.lineHeight > this.parent.maxHeight) return null;
                        }
                    }

                    this.curElemText = fittingText;
                    this.curElemVertTextIndex = this.vertTextIndex[this.fontIndex];
                    this.curElemOffset = this.offset;

                    // If it all fits: Stop wrap mode, proceed with next element
                    if (fittingText.Length == textToDisplay.Length)
                    {
                        this.curElemWrapIndex = 0;
                        this.elemIndex++;
                    }
                    // If only some part fits: Move wrap index & return
                    else if (fittingText.Length > 0)
                    {
                        this.curElemWrapIndex += fittingText.Length;
                    }
                    // If nothing fits: Begin a new line & return
                    else
                    {
                        if (stopAtLineBreak)	return null;
                        else					this.PerformNewLine();
                        if (this.parent.maxHeight != 0 && this.offset.Y + this.lineHeight > this.parent.maxHeight) return null;
                    }

                    this.vertTextIndex[this.fontIndex] += fittingText.Length * 4;
                    this.offset.X += textElemSize.X;
                    this.lineWidth += textElemSize.X;
                    this.lineHeight = Math.Max(this.lineHeight, this.font.LineSpacing);
                    this.lineBaseLine = Math.Max(this.lineBaseLine, this.font.BaseLine);
                }
                else if (elem is TextElement && this.font == null)
                {
                    this.elemIndex++;
                }
                else if (elem is IconElement)
                {
                    IconElement iconElem = elem as IconElement;
                    bool iconValid = this.parent.icons != null && iconElem.IconIndex >= 0 && iconElem.IconIndex < this.parent.icons.Length;
                    Icon icon = iconValid ? this.parent.icons[iconElem.IconIndex] : new Icon();

                    // Word Wrap
                    if (this.parent.maxWidth > 0)
                    {
                        while ((this.lineAvailWidth < this.parent.maxWidth || this.offset.X > this.lineBeginX || this.parent.maxHeight > 0) &&
                            this.offset.X - this.lineBeginX + icon.size.X > this.lineAvailWidth)
                        {
                            if (stopAtLineBreak)	return null;
                            else					this.PerformNewLine();
                            if (this.offset.Y + this.lineHeight > this.parent.maxHeight) return null;
                        }
                    }

                    this.curElemVertIconIndex = this.vertIconIndex;
                    this.curElemOffset = this.offset;

                    this.vertIconIndex += 4;
                    this.offset.X += icon.size.X;
                    this.lineWidth += icon.size.X;
                    this.lineHeight = Math.Max(this.lineHeight, icon.size.Y);
                    this.lineBaseLine = Math.Max(this.lineBaseLine, (int)Math.Round(icon.size.Y));
                    this.elemIndex++;
                }
                else if (elem is FontChangeElement)
                {
                    FontChangeElement fontChangeElem = elem as FontChangeElement;
                    this.fontIndex = fontChangeElem.FontIndex;

                    bool fontValid = this.parent.fonts != null && this.fontIndex >= 0 && this.fontIndex < this.parent.fonts.Length;
                    ContentRef<Font> font = fontValid ? this.parent.fonts[this.fontIndex] : null;
                    this.font = font.Res;
                    this.elemIndex++;
                }
                else if (elem is ColorChangeElement)
                {
                    ColorChangeElement colorChangeElem = elem as ColorChangeElement;
                    this.color = colorChangeElem.Color;
                    this.elemIndex++;
                }
                else if (elem is AlignChangeElement)
                {
                    AlignChangeElement alignChangeElem = elem as AlignChangeElement;
                    this.lineAlign = alignChangeElem.Align;
                    this.elemIndex++;
                }
                else if (elem is NewLineElement)
                {
                    this.elemIndex++;
                    if (stopAtLineBreak)	return null;
                    else					this.PerformNewLine();
                    if (this.parent.maxHeight != 0 && this.offset.Y + this.lineHeight > this.parent.maxHeight) return null;
                }

                return elem;
            }
コード例 #7
0
ファイル: FormattedText.cs プロジェクト: ninja2003/duality
            public RenderState(RenderState other)
            {
                this.parent = other.parent;
                this.vertTextIndex = other.vertTextIndex.Clone() as int[];
                this.vertIconIndex = other.vertIconIndex;
                this.offset = other.offset;
                this.elemIndex = other.elemIndex;
                this.lineIndex = other.lineIndex;

                this.fontIndex = other.fontIndex;
                this.font = other.font;
                this.color = other.color;

                this.lineBeginX = other.lineBeginX;
                this.lineWidth = other.lineWidth;
                this.lineAvailWidth = other.lineAvailWidth;
                this.lineHeight = other.lineHeight;
                this.lineBaseLine = other.lineBaseLine;
                this.lineAlign = other.lineAlign;

                this.curElemOffset = other.curElemOffset;
                this.curElemVertTextIndex = other.curElemVertTextIndex;
                this.curElemVertIconIndex = other.curElemVertIconIndex;
                this.curElemWrapIndex = other.curElemWrapIndex;
                this.curElemText = other.curElemText;
            }
コード例 #8
0
ファイル: FormattedText.cs プロジェクト: ninja2003/duality
            public RenderState(FormattedText parent)
            {
                this.parent = parent;
                this.vertTextIndex = new int[this.parent.fonts != null ? this.parent.fonts.Length : 0];
                this.font = (this.parent.fonts != null && this.parent.fonts.Length > 0) ? this.parent.fonts[0].Res : null;
                this.color = ColorRgba.White;
                this.lineAlign = parent.lineAlign;

                this.PeekLineStats();
                this.offset.X = this.lineBeginX;
            }
コード例 #9
0
        public void Import(IAssetImportEnvironment env)
        {
            // Handle all available input. No need to filter or ask for this anymore, as
            // the preparation step already made a selection with AcceptsInput. We won't
            // get any input here that didn't match.
            foreach (AssetImportInput input in env.Input)
            {
                // Request a target Resource with a name matching the input
                ContentRef <DualityFont> targetRef = env.GetOutput <DualityFont>(input.AssetName);

                // If we successfully acquired one, proceed with the import
                if (targetRef.IsAvailable)
                {
                    DualityFont target = targetRef.Res;

                    // Retrieve import parameters
                    float               size          = env.GetOrInitParameter(targetRef, "Size", 16.0f);
                    FontStyle           style         = env.GetOrInitParameter(targetRef, "Style", FontStyle.Regular);
                    string              customCharSet = env.GetOrInitParameter(targetRef, "CustomCharSet", string.Empty);
                    List <UnicodeBlock> unicodeBlocks = env.GetOrInitParameter(targetRef, "UnicodeBlocks", new List <UnicodeBlock>(DefaultBlocks));
                    bool antialiasing = env.GetOrInitParameter(targetRef, "AntiAlias", true);
                    bool monospace    = env.GetOrInitParameter(targetRef, "Monospace", false);

                    HashSet <char> fullCharSet = new HashSet <char>();

                    if (!string.IsNullOrWhiteSpace(customCharSet))
                    {
                        string[] blocks = customCharSet.Split(',');
                        ulong    start  = 0;
                        ulong    end    = 0;

                        foreach (string block in blocks)
                        {
                            string[] limits = block.Split(new[] { '-' }, 3);
                            if (!ulong.TryParse(limits[0], NumberStyles.HexNumber, null, out start))
                            {
                                Log.Editor.WriteError("Cannot parse value " + limits[0] + "; CustomCharSet will be ignored. Please verify the value and repeat the import.");
                            }

                            if (limits.Length == 1)
                            {
                                end = start;
                            }
                            else
                            {
                                if (limits.Length == 2 && !ulong.TryParse(limits[1], NumberStyles.HexNumber, null, out end))
                                {
                                    Log.Editor.WriteError("Cannot parse value " + limits[1] + "; CustomCharSet will be ignored. Please verify the value and repeat the import.");
                                }

                                else if (limits.Length > 2)
                                {
                                    Log.Editor.WriteError("Unexpected values " + limits[2] + " in range " + block + " will be ignored. Please verify the value and repeat the import.");
                                }

                                if (start > end)
                                {
                                    Log.Editor.WriteWarning(start + " is bigger than " + end + "; block will be ignored. Please verify the value and repeat the import.");
                                }
                            }

                            for (char c = (char)start; c <= (char)end; c++)
                            {
                                if (!char.IsControl(c))
                                {
                                    fullCharSet.Add(c);
                                }
                            }
                        }
                    }

                    if (unicodeBlocks != null)
                    {
                        Type unicodeBlockType     = typeof(UnicodeBlock);
                        Type unicodeRangeAttrType = typeof(UnicodeRangeAttribute);

                        foreach (UnicodeBlock block in unicodeBlocks)
                        {
                            UnicodeRangeAttribute range = unicodeBlockType.GetMember(block.ToString())
                                                          .First()
                                                          .GetCustomAttributes(unicodeRangeAttrType, false)
                                                          .FirstOrDefault() as UnicodeRangeAttribute;

                            if (range != null)
                            {
                                for (char c = (char)range.CharStart; c <= (char)range.CharEnd; c++)
                                {
                                    if (!char.IsControl(c))
                                    {
                                        fullCharSet.Add(c);
                                    }
                                }
                            }
                        }
                    }

                    // Load the TrueType Font and render all the required glyphs
                    byte[]           trueTypeData = File.ReadAllBytes(input.Path);
                    RenderedFontData fontData     = this.RenderGlyphs(
                        trueTypeData,
                        size,
                        style,
                        new FontCharSet(new string(fullCharSet.ToArray())),
                        antialiasing,
                        monospace);

                    // Transfer our rendered Font data to the Font Resource
                    target.SetGlyphData(
                        fontData.Bitmap,
                        fontData.Atlas,
                        fontData.GlyphData,
                        fontData.Metrics);

                    // Add the requested output to signal that we've done something with it
                    env.AddOutput(targetRef, input.Path);
                }
            }
        }
コード例 #10
0
		private void RetrieveResources()
		{
			if (!this.bigFont.IsAvailable)
			{
				Font bigFontRes = new Font();
				bigFontRes.Family = System.Drawing.FontFamily.GenericSansSerif.Name;
				bigFontRes.Size = 32;
				bigFontRes.Kerning = true;
				bigFontRes.GlyphRenderHint = Duality.Resources.Font.RenderHint.AntiAlias;
				bigFontRes.ReloadData();
				ContentProvider.RegisterContent("__editor__bigfont__", bigFontRes);
			}
		}