diff mbox series

[3/3] package/googlefontdirectory: allow generating static fonts

Message ID 20240228145013.411919-3-christian@klarinett.li
State New
Headers show
Series [1/3] package/python-fonttools: enable host-package | expand

Commit Message

Christian Hitz Feb. 28, 2024, 2:50 p.m. UTC
From: Christian Hitz <christian.hitz@bbv.ch>

Google's fontdirecty started to include variable fonts. Some components
(notably Qt) do not (yet) support these variable fonts. In order to still
use these fonts they can be converted to static fonts at build time.

This conversion is enabled by setting
BR2_PACKAGE_GOOGLEFONTDIRECTORY_CREATE_STATIC=y. The static font
configuration is then selected by setting
BR2_PACKAGE_GOOGLEFONTDIRECTORY_CREATE_STATIC_WEIGHTS_WIDTHS.

Signed-off-by: Christian Hitz <christian.hitz@bbv.ch>
---
 package/googlefontdirectory/Config.in         | 17 ++++++++++
 .../googlefontdirectory.mk                    | 31 +++++++++++++++++++
 2 files changed, 48 insertions(+)
diff mbox series

Patch

diff --git a/package/googlefontdirectory/Config.in b/package/googlefontdirectory/Config.in
index cf2814d88f..f2d3e18511 100644
--- a/package/googlefontdirectory/Config.in
+++ b/package/googlefontdirectory/Config.in
@@ -16,4 +16,21 @@  config BR2_PACKAGE_GOOGLEFONTDIRECTORY_FONTS
 	  Font directory code base. For example: ufl/ubuntu,
 	  apache/kranky or ofl/gleego.
 
+config BR2_PACKAGE_GOOGLEFONTDIRECTORY_CREATE_STATIC
+	bool "Convert variable fonts to static"
+	default "false"
+	help
+	  Convert the selected fonts to static fonts.
+
+if BR2_PACKAGE_GOOGLEFONTDIRECTORY_CREATE_STATIC
+
+config BR2_PACKAGE_GOOGLEFONTDIRECTORY_CREATE_STATIC_WEIGHTS_WIDTHS
+	string "List of font variants to generate"
+	default "Regular:400:100 Bold:700:100"
+	help
+	  Specify a space-separated list of Name:Weight:Width tuples
+	  describing the requested static font variants.
+
+endif
+
 endif
diff --git a/package/googlefontdirectory/googlefontdirectory.mk b/package/googlefontdirectory/googlefontdirectory.mk
index b0e5b6e532..71ccdf3fc6 100644
--- a/package/googlefontdirectory/googlefontdirectory.mk
+++ b/package/googlefontdirectory/googlefontdirectory.mk
@@ -27,6 +27,35 @@  endif
 
 GOOGLEFONTDIRECTORY_LICENSE = $(subst $(space),$(comma)$(space),$(GOOGLEFONTDIRECTORY_ALL_LICENSES))
 
+ifeq ($(BR2_PACKAGE_GOOGLEFONTDIRECTORY_CREATE_STATIC),y)
+GOOGLEFONTDIRECTORY_DEPENDENCIES += host-python-fonttools
+GOOGLEFONTDIRECTORY_FONT_VARIANTS = \
+	$(call qstrip,$(BR2_PACKAGE_GOOGLEFONTDIRECTORY_CREATE_STATIC_WEIGHTS_WIDTHS))
+
+elementFromColonTuple = $(word $2,$(subst :, ,$1))
+define GOOGLEFONTDIRECTORY_BUILD_CMDS
+	$(foreach f,$(GOOGLEFONTDIRECTORY_FONTS), \
+		$(foreach v,$(GOOGLEFONTDIRECTORY_FONT_VARIANTS), \
+			$(foreach d,$(wildcard $(@D)/$(f)/*.ttf), \
+				$(HOST_DIR)/bin/fonttools varLib.mutator -q --output=$(@D)/temp.ttf $(d) wght=$(call elementFromColonTuple,$v,2) wdth=$(call elementFromColonTuple,$v,3)
+				$(HOST_DIR)/bin/ttx -q $(@D)/temp.ttf
+				$(SED) "s/Regular/$(call elementFromColonTuple,$v,1)/g" $(@D)/temp.ttx
+				$(HOST_DIR)/bin/ttx -q -o $(@D)/$(basename $(notdir $f))-$(call LOWERCASE,$(call elementFromColonTuple,$v,1)).ttf $(@D)/temp.ttx
+				$(RM) $(@D)/temp.ttf $(@D)/temp.ttx
+			)
+		)
+	)
+endef
+
+define GOOGLEFONTDIRECTORY_INSTALL_TARGET_CMDS
+	$(foreach d,$(GOOGLEFONTDIRECTORY_FONTS), \
+		mkdir -p $(TARGET_DIR)/usr/share/fonts/$(notdir $(d))
+		-$(INSTALL) -m 0644 -t $(TARGET_DIR)/usr/share/fonts/$(notdir $(d)) $(@D)/$(basename $(notdir $d))-*.ttf
+	)
+endef
+
+else
+
 define GOOGLEFONTDIRECTORY_INSTALL_TARGET_CMDS
 	$(foreach d,$(GOOGLEFONTDIRECTORY_FONTS), \
 		mkdir -p $(TARGET_DIR)/usr/share/fonts/$(notdir $(d))
@@ -34,4 +63,6 @@  define GOOGLEFONTDIRECTORY_INSTALL_TARGET_CMDS
 	)
 endef
 
+endif
+
 $(eval $(generic-package))