Skip to content

Incorrect line break with Japanese text containing space on Flutter Web (package:pdf) #1887

@TrongBa

Description

@TrongBa

Describe the bug

When rendering Japanese text with package:pdf on Flutter Web, the line is always broken at the ASCII space inside the Japanese sentence if the text exceeds the available width.

This happens even if I try to replace the space with non-breaking spaces or full-width spaces.
It looks like line breaking is strictly done on spaces (Western word-based breaking), which does not match typical CJK behavior.

Environment

Flutter: 3.35.3

Dart: (from Flutter 3.35.3, stable)

Platform: Flutter Web

Browser: (e.g. Chrome latest, macOS / Windows)

Package: pdf: 3.11.3

To Reproduce

Code snippet to reproduce the behavior:

import 'dart:io';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf/pdf.dart';

Future<void> generatePdf() async {
  final doc = pw.Document();

  const text =
      '※ これは日本語のサンプルテキストです。改行の挙動を確認するために少し長めの文章にしています。';

  doc.addPage(
    pw.Page(
      pageFormat: PdfPageFormat.a4,
      build: (context) {
        return pw.Container(
          // Force a relatively small width to see wrapping
          width: 200,
          child: pw.Text(
            text,
            // using a Japanese-capable font here
          ),
        );
      },
    ),
  );

  final file = File('example.pdf');
  await file.writeAsBytes(await doc.save());
}

Expected behavior

For CJK text, I would expect line breaking to behave more naturally for Japanese:

  • The engine should be able to break between Japanese characters (CJK grapheme clusters), even when there is no ASCII space.

  • A single ASCII space between a symbol and Japanese text (e.g. ※ これは日本語のサンプルテキストです。) should not forcibly move the entire remainder of the sentence to the next line as soon as the width is exceeded.

If the line cannot fit the whole sentence, it should:

  • Keep ※ and the following Japanese text on the same line as much as possible, and

  • Break at a reasonable CJK boundary later in the sentence, instead of always breaking exactly at the first space.

Screenshots

Image

Desktop (please complete the following information):

  • iOS
  • Android
  • Browser
  • Windows
  • Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions