Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text widget is rendering consecutive multiple line breaks as single line break (\n) #991

Closed
deepak786 opened this issue Mar 30, 2022 · 21 comments · May be fixed by #1260
Closed

Text widget is rendering consecutive multiple line breaks as single line break (\n) #991

deepak786 opened this issue Mar 30, 2022 · 21 comments · May be fixed by #1260
Labels
bug Something isn't working stale Issue marked for deletion

Comments

@deepak786
Copy link
Contributor

final pdf = pw.Document();

pdf.addPage(pw.Page(
      pageFormat: PdfPageFormat.a4,
      build: (pw.Context context) {
        return pw.Center(
          child: pw.Text("Hello World\n\nHello World\n\nHello World"),
        ); // Center
      })); // Page

flutter_01

pdf: 3.7.3
@deepak786 deepak786 added bug Something isn't working needs triage labels Mar 30, 2022
@deepak786 deepak786 changed the title Text widget is not rendering the line breaks (\n) Text widget is rendering consecutive multiple line breaks as single line break (\n) Mar 30, 2022
@deepak786
Copy link
Contributor Author

It was working fine with version 3.7.1

@deepak786
Copy link
Contributor Author

Hi @DavBfr,

any update?

@DavBfr
Copy link
Owner

DavBfr commented Apr 5, 2022

No update, it's a bug to be fixed.

@deepak786
Copy link
Contributor Author

deepak786 commented May 30, 2022

@DavBfr any update here?
We are not able to use the updated version of the library until this is fixed.

Is this issue from the Pdfium library?
I'm not able to understand the cause of this issue.

@deepak786
Copy link
Contributor Author

I checked, the issue starts from this commit 13f95f1

@ducdac
Copy link

ducdac commented Jun 27, 2022

@deepak786 I also have the same problem

@deepak786
Copy link
Contributor Author

@DavBfr any update on this?

@jazzbpn
Copy link

jazzbpn commented Feb 6, 2023

Any updates on this issue ?

@rohansohonee1
Copy link

rohansohonee1 commented Feb 9, 2023

@DavBfr

In ttf_parser file, function _parseGlyphs(), this is the if condition which may be causing this issue

if (glyphSizes[glyphIndex] == 0) {
        glyphInfoMap[glyphIndex] = PdfFontMetrics(
          left: 0,
          top: 0,
          right: 0,
          bottom: 0,
          ascent: 0,
          descent: 0,
          advanceWidth: advanceWidth / unitsPerEm,
          leftBearing: leftBearing / unitsPerEm,
        );
        continue;
      }

if we change the condition to also check the advanceWidth

if (glyphSizes[glyphIndex] == 0 && advanceWidth == 0) {
        glyphInfoMap[glyphIndex] = PdfFontMetrics(
          left: 0,
          top: 0,
          right: 0,
          bottom: 0,
          ascent: 0,
          descent: 0,
          advanceWidth: 0, // this will always be zero.
          leftBearing: leftBearing / unitsPerEm,
        );
        continue;
      }

then it resolves this issue, but I want to confirm since I don't have enough knowledge about the ttf. I have learnt about this advance width definition from link https://docs.aspose.com/font/net/what-is-font/glyph/

If the above solution seems fine to you, then I will create the PR for the same.

@DavBfr
Copy link
Owner

DavBfr commented Feb 9, 2023

Yes please create a PR. We'll see what the automated tests have to say.

@DavBfr
Copy link
Owner

DavBfr commented Feb 9, 2023

I think this issue has been fixed some time ago already.

@rohansohonee1
Copy link

rohansohonee1 commented Feb 9, 2023

I have tested with latest master (printing/example/main.dart with multiple line breaks), and this issue is present.

@DavBfr
Copy link
Owner

DavBfr commented Feb 9, 2023

This is what I have:
Screenshot 2023-02-09 at 12 07 50

Row(children: [
  Text(
    'Hello World\nHello World\nHello World',
  ),
  Text(
    'Hello World\n\nHello World\n\nHello World',
  ),
  Text(
    'Hello World\n\n\nHello World\n\n\nHello World',
  ),
]),

@rohansohonee1
Copy link

Use the google font in the text style to reproduce the issue

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the stale Issue marked for deletion label Apr 15, 2023
@JetA2
Copy link

JetA2 commented Apr 16, 2023

The output seems to depend on the font used.

If I use the default font, it works.
If I load a TTF font, I have the issue.

@github-actions
Copy link

Closing this stale issue because it has no activity.

@JetA2
Copy link

JetA2 commented Apr 22, 2023

This is what I get when using the following code:

// ignore_for_file: public_member_api_docs

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

Future<void> main() async {
  runApp(const MyApp('Printing Demo'));
}

class MyApp extends StatelessWidget {
  const MyApp(this.title, {Key? key}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text(title)),
        body: PdfPreview(
          build: (format) => _generatePdf(format, title),
        ),
      ),
    );
  }

  Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
    final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true);
    final font = await PdfGoogleFonts.nunitoExtraLight();

    pdf.addPage(
      pw.Page(
        pageFormat: format,
        build: (context) {
          return pw.Column(
            children: [
              pw.Text("Test\n\nTest\n\n\nTest\n\n\n\nTest",
                  style: pw.TextStyle(font: font, fontSize: 30)),
            ],
          );
        },
      ),
    );

    return pdf.save();
  }
}

Screenshot 2023-04-22 at 17 29 53

This pull request seems to fix the issue:
f24ddfa

@RamiHI
Copy link

RamiHI commented Jun 5, 2023

any updates?

@ahmeedev
Copy link

ahmeedev commented Aug 1, 2023

HI, any updates on this issue?

@ahmeedev
Copy link

ahmeedev commented Aug 1, 2023

This is what I get when using the following code:

// ignore_for_file: public_member_api_docs

import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

Future<void> main() async {
  runApp(const MyApp('Printing Demo'));
}

class MyApp extends StatelessWidget {
  const MyApp(this.title, {Key? key}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text(title)),
        body: PdfPreview(
          build: (format) => _generatePdf(format, title),
        ),
      ),
    );
  }

  Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
    final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true);
    final font = await PdfGoogleFonts.nunitoExtraLight();

    pdf.addPage(
      pw.Page(
        pageFormat: format,
        build: (context) {
          return pw.Column(
            children: [
              pw.Text("Test\n\nTest\n\n\nTest\n\n\n\nTest",
                  style: pw.TextStyle(font: font, fontSize: 30)),
            ],
          );
        },
      ),
    );

    return pdf.save();
  }
}
Screenshot 2023-04-22 at 17 29 53

This pull request seems to fix the issue: f24ddfa

No, it didn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale Issue marked for deletion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants