Example #1
0
        /// <summary>
        /// Returns the raw sprite from the specified character.
        /// </summary>
        internal Bitmap GetRawChar(char c, FontFace font = null)
        {
            ImportFont(font);
            font ??= CurrentFont;

            if (WhiteSpaceInfo.IsWhiteSpace(c) || c == '\n')
            {
                return(null);
            }

            CharIndex i = ImageHelper.GetCharIndex(c, CharMap);

            if (!i.IsSuccess || !font.SheetUrls.Keys.Contains(i.Page))
            {
                // TODO: Return UNKNOWN unicode character
                return(null);
            }

            // TODO: Handle directory assignment
            using var bmp = new Bitmap($"{DefaultFontDirectory}{font.SheetUrls[i.Page]}");

            int x = font.CharWidth * i.Column;
            int y = font.CharHeight * i.Row;

            var    crop = new Rectangle(x, y, font.CharWidth, font.CharHeight);
            Bitmap tmp  = ImageHelper.Crop(bmp, crop);

            return(tmp);
        }
Example #2
0
        // TODO: Implement max width and max height comparisons
        private TextData CreateText(string content, FontFace font, Padding?imagePadding = null, bool trimEmptyPixels = true, bool extendOnOffset = false)
        {
            ImportFont(font);
            font ??= CurrentFont;

            bool    canTrim = trimEmptyPixels && !font.IsMonospace;
            Padding padding = imagePadding ?? Padding.Empty;

            if (!extendOnOffset)
            {
                extendOnOffset = font.Overrides?.Any(x => x.GetOffset() != null) ?? false;
            }

            var cursor       = new Cursor();
            var charData     = new List <CharData>();
            var imageMap     = new Dictionary <char, Bitmap>();
            int cursorHeight = font.Padding.Height + font.CharHeight;

            int charIndex    = 0;
            int renderWidth  = 0;
            int renderHeight = 0;
            int yMaxOffset   = 0;

            foreach (char c in content)
            {
                // NOTE: Calculate the character's initial width and padding
                Padding charPadding = font.Padding.Clone();
                int     drawWidth   = font.GetCharWidth(c);

                // NOTE: Handle all line breaks here
                if (c == '\n')
                {
                    int vX = padding.Left + cursor.X;
                    int vY = padding.Top + cursor.Y;

                    charData.Add(new CharData(null, c, vX, vY, 0, font.CharHeight));

                    if (cursor.X > renderWidth)
                    {
                        renderWidth = cursor.X;
                    }

                    cursor.X  = charPadding.Left;
                    cursor.Y += cursorHeight + 1;

                    if (cursor.Y > renderHeight)
                    {
                        renderHeight = cursor.Y;
                    }

                    charIndex++;
                    continue;
                }

                // NOTE: Handle all whitespace characters
                if (WhiteSpaceInfo.IsWhiteSpace(c))
                {
                    int emptyWidth = canTrim ? drawWidth : drawWidth + charPadding.Width;
                    int vX         = padding.Left + cursor.X;
                    int vY         = padding.Top + cursor.Y;

                    charData.Add(new CharData(null, c, vX, vY, emptyWidth, font.CharHeight));
                    cursor.X += emptyWidth;
                    charIndex++;
                    continue;
                }

                // NOTE: Try to assign a matching sprite for this character
                if (!imageMap.ContainsKey(c))
                {
                    imageMap[c] = GetChar(c, font, trimEmptyPixels);
                }

                // NOTE: Ignore handling this sprite if there isn't an image assigned to it
                if (imageMap[c] == null)
                {
                    continue;
                }

                // NOTE: Otherwise, if an image for this character does exist, try to assign the actual width to it

                if (canTrim)
                {
                    drawWidth = imageMap[c].Width;
                }

                int cursorWidth = charPadding.Width + drawWidth;

                yMaxOffset += Math.Max(font.GetCharOffset(c).Y, 0);

                // NOTE: No left padding if it's the first character
                if (charIndex == 0)
                {
                    cursorWidth     -= charPadding.Left;
                    charPadding.Left = 0;
                }

                // NOTE: No right padding if it's the final character
                if (charIndex == content.Length - 1)
                {
                    cursorWidth      -= charPadding.Right;
                    charPadding.Right = 0;
                }

                if (charIndex > 0 && charIndex < content.Length - 1)
                {
                    char?beforeChar = content.ElementAtOrDefault(charIndex - 1);
                    char?afterChar  = content.ElementAtOrDefault(charIndex + 1);

                    if (beforeChar.HasValue && beforeChar != '\n')
                    {
                        cursorWidth     -= charPadding.Left;
                        charPadding.Left = 0;
                    }

                    if (afterChar.HasValue)
                    {
                        if (afterChar == '\n' || WhiteSpaceInfo.IsWhiteSpace(afterChar.Value))
                        {
                            cursorWidth      -= charPadding.Right;
                            charPadding.Right = 0;
                        }
                    }
                }

                int pX = padding.Left + cursor.X;
                int pY = padding.Top + cursor.Y;
                charData.Add(new CharData(imageMap[c], c, pX, pY, drawWidth, font.CharHeight, charPadding, font.GetCharOffset(c)));
                cursor.X += cursorWidth;
                charIndex++;
            }

            int height = renderHeight + font.CharHeight;
            int width  = cursor.X > renderWidth ? cursor.X : renderWidth;

            // NOTE: If the image is extended from offsets, include it to the final height
            if (extendOnOffset)
            {
                height += yMaxOffset;
            }

            return(new TextData(content, padding, width, height, charData));
        }
Example #3
0
 private static bool IsNonEmptyChar(char c)
 => !WhiteSpaceInfo.IsWhiteSpace(c) && c != '\n';