How to apply Width and Height of Text on CanvasDevice's DrawText, that means some text width is too small some text too high how to calculate this and apply

Saranya Karthik 40 Reputation points
2024-05-07T02:46:56.3133333+00:00

//JSON file

"Templates": [

  {

    "TemplateID": "1",

    "TemplateName": "WHITE",

    "CardFeeder": "A",

    "Sides": [

      {

        "Side": "B",

        "Orientation": "PT", // portrait card

        "W": "648", //card width

        "H": "1016", //card height

        "FF": "HelveticaNeue-Bold.ttf#Helvetica Neue",

        "FC": "#000000",

        "DPI": "300",

        "FN": {

          "X": "37",

          "Y": "248",

          "W": "155",

          "H": "200",

          "FS": "32",

          "FF": "HelveticaNeue-Bold.ttf#Helvetica Neue",

          "FC": "#000000",

          "FW": "Bold",

          "Underline": "0",

          "Italic": "0",

          "TA": "C"

        },

		"LN": {

          "X": "37",

          "Y": "287",

          "W": "155",

          "H": "200",

          "FS": "32",

          "FF": "HelveticaNeue-Bold.ttf#Helvetica Neue",

          "FC": "#000000",

          "FW": "Bold",

          "Underline": "0",

          "Italic": "0",

          "TA": "C"

        }

		}

		]

		}

		]
```-----------------------------------------------

Source code

if (CardTheme.Orientation.ToUpper().Equals("PT"))

```vba
                        {

                            if (CardTheme.FN != null)

                            {

                                ICanvasBrush fontBrush = null;

                                FontWeight fontWeight = FontWeights.Normal;

                                //Color fontColor = ParseColor(themeProperty.MID.FC);

                                if (string.IsNullOrEmpty(CardTheme.FN.FC) == false)

                                {

                                    fontBrush = ParseColor(CardTheme.FN.FC);

                                }

                                else

                                {

                                    fontBrush = ParseColor("#000000");

                                }

                                if (string.IsNullOrEmpty(CardTheme.FN.FW) == false)

                                {

                                    fontWeight = ConvertToFontWeight(CardTheme.FN.FW);

                                }

                                else

                                {

                                    fontWeight = FontWeights.Bold;

                                }

                                if (string.IsNullOrEmpty(CardTheme.FN.Underline) == false)

                                {

                                    if (CardTheme.FN.Underline.Equals("1"))

                                    {

                                        underLine = true;

                                    }

                                    else if (CardTheme.FN.Underline.Equals("0"))

                                    {

                                        underLine = false;

                                    }

                                }

                                if (string.IsNullOrEmpty(CardTheme.FN.Italic) == false)

                                {

                                    if (CardTheme.FN.Italic.Equals("1"))

                                    {

                                        italic = true;

                                    }

                                    else if (CardTheme.FN.Italic.Equals("0"))

                                    {

                                        italic = false;

                                    }

                                }

                                if (string.IsNullOrEmpty(CardTheme.FN.FF) == false)

                                {

                                    fontFamilyName = $"ms-appx:///Assets/{fontfolderName}/{CardTheme.FN.FF}";

                                }

                                else if (string.IsNullOrEmpty(CardTheme.FF) == false)

                                {

                                    fontFamilyName = /*CardTheme.FF;*/$"ms-appx:///Assets/{CardTheme.FF}";

                                }

                                else

                                {

                                    fontFamilyName = $"ms-appx:///Assets/HelveticaNeue-Bold.ttf";

                                }

                                //ICanvasBrush fontBrush = ParseColor(CardTheme.FN.FC);

                                //FontWeight fontWeight = ConvertToFontWeight(CardTheme.FN.FW);

                                //fontFamilyName = $"ms-appx:///Assets/{fontfolderName}/{CardTheme.FN.FF}";

                                // for testing

                                //string fontFamilyName = "Helvetica Neue";

                                CanvasTextFormat textFormat = new CanvasTextFormat

                                {

                                    FontSize = CardTheme.FN.FS,

                                    FontWeight = fontWeight,

                                    FontFamily = fontFamilyName,

                                    HorizontalAlignment = CanvasHorizontalAlignment.Left,

                                    VerticalAlignment = CanvasVerticalAlignment.Center

                                };

                                if (italic)

                                {

                                    textFormat.FontStyle = FontStyle.Italic;

                                }

                                ds.DrawText($"{memberData.firstName}", CardTheme.FN.X, CardTheme.FN.Y, fontBrush, textFormat);

                            }

                            if (CardTheme.LN != null)

                            {

                                ICanvasBrush fontBrush = null;

                                FontWeight fontWeight = FontWeights.Normal;

                                //Color fontColor = ParseColor(themeProperty.MID.FC);

                                if (string.IsNullOrEmpty(CardTheme.LN.FC) == false)

                                {

                                    fontBrush = ParseColor(CardTheme.LN.FC);

                                }

                                else

                                {

                                    fontBrush = ParseColor("#000000");

                                }

                                if (string.IsNullOrEmpty(CardTheme.LN.FW) == false)

                                {

                                    fontWeight = ConvertToFontWeight(CardTheme.LN.FW);

                                }

                                else

                                {

                                    fontWeight = FontWeights.Bold;

                                }

                                if (string.IsNullOrEmpty(CardTheme.LN.Underline) == false)

                                {

                                    if (CardTheme.LN.Underline.Equals("1"))

                                    {

                                        underLine = true;

                                    }

                                    else if (CardTheme.LN.Underline.Equals("0"))

                                    {

                                        underLine = false;

                                    }

                                }

                                if (string.IsNullOrEmpty(CardTheme.LN.Italic) == false)

                                {

                                    if (CardTheme.LN.Italic.Equals("1"))

                                    {

                                        italic = true;

                                    }

                                    else if (CardTheme.LN.Italic.Equals("0"))

                                    {

                                        italic = false;

                                    }

                                }

                                if (string.IsNullOrEmpty(CardTheme.LN.FF) == false)

                                {

                                    fontFamilyName = $"ms-appx:///Assets/{fontfolderName}/{CardTheme.LN.FF}";

                                }

                                else if (string.IsNullOrEmpty(CardTheme.FF) == false)

                                {

                                    fontFamilyName = /*CardTheme.FF;*/$"ms-appx:///Assets/{CardTheme.FF}";

                                }

                                else

                                {

                                    fontFamilyName = $"ms-appx:///Assets/HelveticaNeue-Bold.ttf";

                                }

                                //ICanvasBrush fontBrush = ParseColor(CardTheme.LN.FC);

                                //FontWeight fontWeight = ConvertToFontWeight(CardTheme.LN.FW);

                                //fontFamilyName = $"ms-appx:///Assets/{fontfolderName}/{CardTheme.LN.FF}";

                                CanvasTextFormat textFormat = new CanvasTextFormat

                                {

                                    FontSize = CardTheme.LN.FS,

                                    FontWeight = fontWeight,

                                    FontFamily = fontFamilyName,

                                    HorizontalAlignment = CanvasHorizontalAlignment.Left,

                                    VerticalAlignment = CanvasVerticalAlignment.Center

                                };


                                

                                if (italic)

                                {

                                    textFormat.FontStyle = FontStyle.Italic;

                                }

                                ds.DrawText($"{memberData.lastName}", CardTheme.LN.X, CardTheme.LN.Y, fontBrush, textFormat);

                            }

                        }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,574 questions
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,574 questions
{count} votes