syncfusion / flutter-widgets

Syncfusion Flutter widgets libraries include high quality UI widgets and file-format packages to help you create rich, high-quality applications for iOS, Android, and web from a single code base.
1.6k stars 786 forks source link

[syncfusion_flutter_charts] Crash in PdfFont.measureString with Greek language/locale #1440

Closed kinex closed 9 months ago

kinex commented 1 year ago

One user of my app has reported a crash happening when trying to create a PDF. His device language is Greek and the app language is English. This crash log is most probably related to his issue:

Invalid argument (The character is not supported by the font.): 924. Error thrown main.

0  ???                            0x0 StandardWidthTable._returnValue + 115 (pdf_font_metrics.dart:115)
1  ???                            0x0 StandardWidthTable.[] + 112 (pdf_font_metrics.dart:112)
2  ???                            0x0 PdfStandardFontHelper.getCharWidthInternal + 436 (pdf_standard_font.dart:436)
3  ???                            0x0 PdfStandardFontHelper.getLineWidth + 452 (pdf_standard_font.dart:452)
4  ???                            0x0 PdfFontHelper.getLineWidth + 286 (pdf_font.dart:286)
5  ???                            0x0 PdfStringLayouter._getLineWidth + 261 (pdf_string_layouter.dart:261)
6  ???                            0x0 PdfStringLayouter._layoutLine + 114 (pdf_string_layouter.dart:114)
7  ???                            0x0 PdfStringLayouter._doLayout + 57 (pdf_string_layouter.dart:57)
8  ???                            0x0 PdfStringLayouter.layout + 45 (pdf_string_layouter.dart:45)
9  ???                            0x0 PdfFont.measureString + 131 (pdf_font.dart:131)

Flutter 3.13.3 syncfusion_flutter_pdf 22.2.12

irfanajaffer commented 9 months ago

We have observed that you are attempting to utilize the PdfStandardFont for rendering unicode characters. According to our analysis, using the PdfStandardFont for this purpose is not recommended. We recommend switching to the PdfTruetypefont to address the encountered exception

You can add text using the font data, by initializing PdfFont class as PdfTrueTypeFont class. The font data can be loaded from the disk. The font data can be initialized to PdfTrueTypeFont as a list of bytes or base64 string format. The following code snippet explains this

//Create a new PDF document
PdfDocument document = PdfDocument();

//Draw text
document.pages.add().graphics.drawString('Hello World!!!',
    PdfTrueTypeFont(File('Arial.ttf').readAsBytesSync(), 14),
    brush: PdfBrushes.black, bounds: Rect.fromLTWH(10, 10, 300, 50));

//Saves the document
File('Output.pdf').writeAsBytes(await document.save());

//Disposes the document
document.dispose();
kinex commented 9 months ago

I am using PdfStandardFont only for drawing things like app name, short date/time and page numbers in the page header/footer. The crash happens in code drawing the app name (which is not different in Greek and contains only normal ASCII characters). Anyway, I will apply your suggested fix and let the user to test it. Thanks!