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

Allow obtaining the font files #102

Open
DavBfr opened this issue Apr 26, 2020 · 15 comments · May be fixed by #611
Open

Allow obtaining the font files #102

DavBfr opened this issue Apr 26, 2020 · 15 comments · May be fixed by #611
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed p: google_fonts

Comments

@DavBfr
Copy link

DavBfr commented Apr 26, 2020

Request

I'm the developer of the library pdf. This library renders PDF files from dart code. It can load TTF fonts to draw text in the document.

As far as I can see, this google-fonts library does not expose the font files, URL, or bytes in any way.

I would need something to get the Uint8List font data for any available fonts without duplicating this library.

Usage

import 'dart:io';
import 'dart:typed_data';

import 'package:pdf/widgets.dart';

void main() async {
  final pdf = Document();

  // Load the file here.
  // A google_font API would be much appreciated to replace this line
  // like: final Uint8List fontData = await GoogleFonts.getFontData('Lato');
  final Uint8List fontData = await File('open-sans.ttf').readAsBytes();
  final ttf = Font.ttf(fontData.buffer.asByteData());

  pdf.addPage(
    Page(
      build: (context) {
        return Center(
          child: Text(
            'Hello World',
            style: TextStyle(
              font: ttf,
              fontSize: 40,
            ),
          ),
        );
      },
    ),
  );

  await File('output.pdf').writeAsBytes(pdf.save());
}

Platforms:

  • iOS, Android, macOS, Web
@BraveEvidence
Copy link

++1

@JaqueiraDev
Copy link

That would be awesome!!!

@ahuruConnect
Copy link

Any progress on this? would be great to have this feature.

@mukhtharcm
Copy link

Much Needed Feature!

@derrick56007
Copy link

bump

@arpanpreneur
Copy link

Absolutely Required

@eaedev
Copy link

eaedev commented Mar 21, 2021

I will love it

@EducatedDeer
Copy link

This is a much required feature please

@Joseph-Nathan
Copy link

any new news !!

@ganeshchenniah
Copy link

anu update ?

@GuvanchBayryyyev
Copy link

I am also using Google Fonts, I first download it and using like this

theme: pw.ThemeData.withFont(
        base:
            Font.ttf(await rootBundle.load("assets/fonts/Sarabun-Regular.ttf")),
        bold: Font.ttf(await rootBundle.load("assets/fonts/Sarabun-Bold.ttf")),
      ),

@salvadorbarb1
Copy link

@GuvanchBayryyyev can you please elaborate?

@guidezpl guidezpl added enhancement New feature or request help wanted Extra attention is needed labels Oct 7, 2021
@guidezpl guidezpl changed the title [IMPROVEMENT] Allow getting the font files Allow obtaining the font files May 20, 2022
@guidezpl guidezpl self-assigned this Jun 12, 2023
@guidezpl
Copy link
Collaborator

This is proving more complex than initially thought and requires a thorough refactor, along with tackling #129.

@tdenniston
Copy link

tdenniston commented Aug 30, 2024

It seems that this could be done somewhat reasonably by allowing users to construct a FontLoader instance instead of always constructing one in loadFontByteData. Something like this, in google_fonts.dart:

class _Config {
  // .. snip ..

  /// If provided, this function will be invoked to construct a font loader
  /// instance for loading the given family name. If left null, a font loader
  /// instance will be constructed automatically.
  FontLoader Function(String familyName)? fontLoaderBuilder;
}

Then in google_fonts_base.dart:

Future<void> loadFontByteData(
  String familyWithVariantString,
  Future<ByteData?>? byteData,
) async {
  if (byteData == null) return;
  final fontData = await byteData;
  if (fontData == null) return;

  final FontLoader fontLoader =
      GoogleFonts.config.fontLoaderBuilder?.call(familyWithVariantString) ??
          FontLoader(familyWithVariantString);
  fontLoader.addFont(Future.value(fontData));
  await fontLoader.load();
}

Users could then implement a FontLoader subclass like so:

class MyFontLoader extends FontLoader {
  MyFontLoader(super.family);

  @override
  void addFont(Future<ByteData> bytes) {
    super.addFont(bytes.then((bytes) {
      log('Loaded font $family, size ${bytes.lengthInBytes}');
      return bytes;
    }));
  }
}

and register it like this:

GoogleFonts.config.fontLoaderBuilder = (n) => MyFontLoader(n);

@guidezpl If this seems appropriate I am happy to open a PR with this change.

tdenniston added a commit to tdenniston/flutter-packages that referenced this issue Aug 31, 2024
This adds a new public API, `GoogleFontsLoader`, and a corresponding new
instance field `fontLoader` in the global `GoogleFonts.config` class.

The new API allows users to hook the loading of fonts into the Flutter
engine, which is useful in scenarios where the actual font data is
needed for other purposes. The newly added default font loader simply
uses Flutter's `FontLoader` as before.

Fixes material-foundation#102.
@tdenniston tdenniston linked a pull request Aug 31, 2024 that will close this issue
2 tasks
@tdenniston
Copy link

NB most of the Google-served fonts are packaged in WOFF2 format, which is not directly supported by Flutter. In that sense this API addition is somewhat questionable, though it could be considered future-facing for a later Flutter that does support WOFF2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed p: google_fonts
Projects
None yet
Development

Successfully merging a pull request may close this issue.