material-foundation / flutter-packages

A collection of useful Material Design packages
https://pub.dev/publishers/material.io
Apache License 2.0
833 stars 157 forks source link

Allow obtaining the font files #102

Open DavBfr opened 4 years ago

DavBfr commented 4 years ago

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:

BraveEvidence commented 4 years ago

++1

JaqueiraDev commented 4 years ago

That would be awesome!!!

ahuruConnect commented 3 years ago

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

mukhtharcm commented 3 years ago

Much Needed Feature!

derrick56007 commented 3 years ago

bump

arpanpreneur commented 3 years ago

Absolutely Required

eaedev commented 3 years ago

I will love it

EducatedDeer commented 3 years ago

This is a much required feature please

Joseph-Nathan commented 3 years ago

any new news !!

ganeshchenniah commented 3 years ago

anu update ?

GuvanchBayryyyev commented 3 years ago

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 commented 3 years ago

@GuvanchBayryyyev can you please elaborate?

guidezpl commented 1 year ago

This is proving more complex than initially thought and requires a thorough refactor, along with tackling https://github.com/material-foundation/flutter-packages/issues/129.

tdenniston commented 2 weeks ago

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 commented 1 day ago

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.