diff --git a/AndroidCompat/src/main/java/android/graphics/Canvas.java b/AndroidCompat/src/main/java/android/graphics/Canvas.java index 8c4c83b6..92384a04 100644 --- a/AndroidCompat/src/main/java/android/graphics/Canvas.java +++ b/AndroidCompat/src/main/java/android/graphics/Canvas.java @@ -10,9 +10,11 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.Shape; +import java.awt.font.TextAttribute; import java.awt.font.GlyphVector; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; +import java.text.AttributedString; import java.util.ArrayList; import java.util.List; @@ -44,13 +46,16 @@ public final class Canvas { drawText(new String(text, index, count), x, y, paint); } - public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { + public void drawText(@NonNull String str, float x, float y, @NonNull Paint paint) { applyPaint(paint); - GlyphVector glyphVector = paint.getFont().createGlyphVector(canvas.getFontRenderContext(), text); + AttributedString text = paint.getTypeface().createWithFallback(str); + canvas.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); + // TODO: fix with fallback fonts + GlyphVector glyphVector = paint.getTypeface().getFont().createGlyphVector(canvas.getFontRenderContext(), text.getIterator()); Shape textShape = glyphVector.getOutline(); switch (paint.getStyle()) { case Paint.Style.FILL: - canvas.drawString(text, x, y); + canvas.drawString(text.getIterator(), x, y); break; case Paint.Style.STROKE: save(); @@ -178,7 +183,7 @@ public final class Canvas { } private void applyPaint(Paint paint) { - canvas.setFont(paint.getFont()); + canvas.setFont(paint.getTypeface().getFont()); java.awt.Color color = Color.valueOf(paint.getColorLong()).toJavaColor(); canvas.setColor(color); canvas.setStroke(new BasicStroke(paint.getStrokeWidth(), BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); diff --git a/AndroidCompat/src/main/java/android/graphics/Paint.java b/AndroidCompat/src/main/java/android/graphics/Paint.java index 5ee2585c..da2a61d6 100644 --- a/AndroidCompat/src/main/java/android/graphics/Paint.java +++ b/AndroidCompat/src/main/java/android/graphics/Paint.java @@ -65,9 +65,9 @@ public class Paint { @ColorLong private long mShadowLayerColor; private int mFlags; - private Font mFont = new Font(null); private Style mStyle = Style.FILL; private float mStrokeWidth = 1.0f; + private Typeface mTypeface = Typeface.DEFAULT; private static final Object sCacheLock = new Object(); @@ -278,10 +278,10 @@ public class Paint { mShadowLayerDy = 0.0f; mShadowLayerColor = Color.pack(0); - setFlags(ANTI_ALIAS_FLAG); - mFont = new Font(null); mStyle = Style.FILL; mStrokeWidth = 1.0f; + mTypeface = Typeface.DEFAULT; + setFlags(ANTI_ALIAS_FLAG); } public void set(Paint src) { @@ -314,9 +314,9 @@ public class Paint { mShadowLayerColor = paint.mShadowLayerColor; mFlags = paint.mFlags; - mFont = paint.mFont; mStyle = paint.mStyle; mStrokeWidth = paint.mStrokeWidth; + mTypeface = paint.mTypeface; } /** @hide */ @@ -368,13 +368,13 @@ public class Paint { fontAttributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR); } - Map atts = (Map) mFont.getAttributes(); + Map atts = mTypeface.getAttributes(); Object weight = atts.getOrDefault(TextAttribute.WEIGHT, null); if (weight instanceof Float) { fontAttributes.put(TextAttribute.WEIGHT, weight); } - mFont = mFont.deriveFont(fontAttributes); + mTypeface = mTypeface.deriveFont(fontAttributes); } public int getHinting() { @@ -605,14 +605,14 @@ public class Paint { } public Typeface getTypeface() { - return new Typeface(mFont); + return mTypeface; } public Typeface setTypeface(Typeface typeface) { Map fontAttributes = new HashMap(); fontAttributes.put(TextAttribute.WEIGHT, typeface.getJavaWeight()); - mFont = typeface.getFont() - .deriveFont(mFont.getStyle(), mFont.getSize()) + mTypeface = typeface + .deriveFont(mTypeface.getFont().getStyle(), mTypeface.getFont().getSize()) .deriveFont(fontAttributes); setFlags(mFlags); return typeface; @@ -693,16 +693,12 @@ public class Paint { throw new RuntimeException("Stub!"); } - public Font getFont() { - return mFont; - } - public float getTextSize() { - return mFont.getSize2D(); + return mTypeface.getFont().getSize2D(); } public void setTextSize(float textSize) { - mFont = mFont.deriveFont(textSize); + mTypeface = mTypeface.deriveFont(textSize); } public float getTextScaleX() { @@ -836,7 +832,7 @@ public class Paint { public float getFontMetrics(FontMetrics metrics) { java.awt.Canvas c = new java.awt.Canvas(); - java.awt.FontMetrics m = c.getFontMetrics(mFont); + java.awt.FontMetrics m = c.getFontMetrics(mTypeface.getFont()); metrics.top = m.getMaxDescent(); metrics.ascent = m.getAscent(); metrics.descent = m.getDescent(); diff --git a/AndroidCompat/src/main/java/android/graphics/Typeface.java b/AndroidCompat/src/main/java/android/graphics/Typeface.java index 6feaee70..dfd90d73 100644 --- a/AndroidCompat/src/main/java/android/graphics/Typeface.java +++ b/AndroidCompat/src/main/java/android/graphics/Typeface.java @@ -30,9 +30,15 @@ import java.io.FileDescriptor; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; +import java.text.AttributedString; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.List; +import java.util.stream.Stream; +import java.util.stream.Collectors; import android.annotation.NonNull; @@ -65,6 +71,7 @@ public class Typeface { public static final String DEFAULT_FAMILY = "sans-serif"; private final Font mFont; + private final List mFallbackFonts; /** Returns the typeface's weight value */ public int getWeight() { @@ -271,6 +278,76 @@ public class Typeface { public Typeface(Font fnt) { mFont = fnt; + mFallbackFonts = Collections.emptyList(); + } + + public Typeface(Font fnt, List fallbackFonts) { + mFont = fnt; + mFallbackFonts = fallbackFonts; + } + + public Map getAttributes() { + return (Map) mFont.getAttributes(); + } + + public Typeface deriveFont(Map attributes) { + Font mainFont = mFont.deriveFont(attributes); + List fallbacks = mFallbackFonts.stream().map(font -> font.deriveFont(attributes)) + .collect(Collectors.toList()); + return new Typeface(mainFont, fallbacks); + } + + public Typeface deriveFont(float size) { + Font mainFont = mFont.deriveFont(size); + List fallbacks = mFallbackFonts.stream().map(font -> font.deriveFont(size)) + .collect(Collectors.toList()); + return new Typeface(mainFont, fallbacks); + } + + public Typeface deriveFont(int style, float size) { + Font mainFont = mFont.deriveFont(style, size); + List fallbacks = mFallbackFonts.stream().map(font -> font.deriveFont(style, size)) + .collect(Collectors.toList()); + return new Typeface(mainFont, fallbacks); + } + + public AttributedString createWithFallback(String text) { + AttributedString result = new AttributedString(text); + + int textLength = text.length(); + result.addAttribute(TextAttribute.FONT, mFont, 0, textLength); + + int i = 0; + while (true) { + int until = mFont.canDisplayUpTo(result.getIterator(), i, textLength); + if (until == -1) break; + + boolean found = false; + // find a fallback font from `until` + for (int j = 0; j < mFallbackFonts.size(); ++j) { + int fallbackUntil = until; + for (; fallbackUntil < textLength; ++fallbackUntil) { + if (mFont.canDisplay(text.charAt(fallbackUntil)) || !mFallbackFonts.get(j).canDisplay(text.charAt(fallbackUntil))) + break; + } + if (fallbackUntil > until) { + // use this and advance + int end = fallbackUntil >= 0 ? fallbackUntil : textLength; + result.addAttribute(TextAttribute.FONT, mFallbackFonts.get(j), until, end); + Log.v(TAG, String.format("Fallback: from %d to %d using %s", until, end, mFallbackFonts.get(j).getName())); + i = end; + found = true; + break; + } + } + + if (found) continue; + + Log.w(TAG, String.format("No fallback font found at %d, skipping", until)); + i = until + 1; + } + + return result; } public static Typeface createFromFile(@Nullable File file) { @@ -316,12 +393,30 @@ public class Typeface { return mFont.hashCode(); } + private static Font loadFontAsset(String font) { + try (InputStream defaultNormalStream = ClassLoader.getSystemClassLoader().getResourceAsStream("font/" + font)) { + return Font.createFont(Font.TRUETYPE_FONT, defaultNormalStream).deriveFont(12.0f); + } catch (Exception ex) { + Log.e(TAG, "Failed to load " + font, ex); + return null; + } + } + + private static Typeface withFallback(Font baseFallback, String mainFont, String... fonts) { + Font main = loadFontAsset(mainFont); + if (main == null) main = new Font(null, 0, 12); + List fallbacks = Stream.concat(Arrays.stream(fonts).map(Typeface::loadFontAsset).filter(f -> f != null), Stream.of(baseFallback)) + .collect(Collectors.toList()); + Log.v(TAG, String.format("Loaded font %s with %d fallback fonts", main.getName(), fallbacks.size())); + return new Typeface(main, fallbacks); + } + static { - DEFAULT = new Typeface(new Font(null, 0, 12)); - DEFAULT_BOLD = new Typeface(new Font(null, Font.BOLD, 12)); - SANS_SERIF = new Typeface(new Font(Font.SANS_SERIF, 0, 12)); - SERIF = new Typeface(new Font(Font.SERIF, 0, 12)); - MONOSPACE = new Typeface(new Font(Font.MONOSPACED, 0, 12)); + DEFAULT = withFallback(new Font(null, 0, 12), "NotoSans/NotoSans-VariableFont_wdth,wght.ttf", "NotoSans/NotoSansSymbols2-Regular.ttf", "NotoSans/NotoEmoji-VariableFont_wght.ttf"); + DEFAULT_BOLD = DEFAULT.deriveFont(Font.BOLD); + SANS_SERIF = DEFAULT; + SERIF = withFallback(new Font(Font.SERIF, 0, 12), "NotoSans/NotoSerif-VariableFont_wdth,wght.ttf", "NotoSans/NotoSansSymbols2-Regular.ttf", "NotoSans/NotoEmoji-VariableFont_wght.ttf"); + MONOSPACE = withFallback(new Font(Font.MONOSPACED, 0, 12), "NotoSans/NotoSansMono-VariableFont_wdth,wght.ttf", "NotoSans/NotoSansSymbols2-Regular.ttf", "NotoSans/NotoEmoji-VariableFont_wght.ttf"); } } diff --git a/AndroidCompat/src/main/java/android/text/StaticLayout.java b/AndroidCompat/src/main/java/android/text/StaticLayout.java index e5a01438..4fc19e1c 100644 --- a/AndroidCompat/src/main/java/android/text/StaticLayout.java +++ b/AndroidCompat/src/main/java/android/text/StaticLayout.java @@ -366,9 +366,8 @@ public class StaticLayout extends Layout { mRightIndents = b.mRightIndents; String str = b.mText.subSequence(b.mStart, b.mEnd).toString(); - AttributedString text = new AttributedString(str); - text.addAttribute(TextAttribute.FONT, getPaint().getFont()); - FontRenderContext frc = new FontRenderContext(getPaint().getFont().getTransform(), RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT); + AttributedString text = getPaint().getTypeface().createWithFallback(str); + FontRenderContext frc = new FontRenderContext(null, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT); LineBreakMeasurer measurer = new LineBreakMeasurer(text.getIterator(), frc); // TODO: directions diff --git a/AndroidCompat/src/main/java/android/text/TextLine.java b/AndroidCompat/src/main/java/android/text/TextLine.java index 8c9f972d..3774875c 100644 --- a/AndroidCompat/src/main/java/android/text/TextLine.java +++ b/AndroidCompat/src/main/java/android/text/TextLine.java @@ -27,6 +27,8 @@ import android.text.Layout.Directions; import android.text.Layout.TabStops; import java.awt.RenderingHints; import java.awt.font.FontRenderContext; +import java.awt.font.TextMeasurer; +import java.text.AttributedString; public class TextLine { @@ -149,7 +151,9 @@ public class TextLine { public float metrics(FontMetricsInt fmi, @Nullable RectF drawBounds, boolean returnDrawWidth, @Nullable LineInfo lineInfo) { FontRenderContext frc = new FontRenderContext(null, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT); - return (float) mPaint.getFont().getStringBounds(mText.toString(), mStart, mStart + mLen, frc).getWidth(); + AttributedString text = mPaint.getTypeface().createWithFallback(mText.toString()); + TextMeasurer tm = new TextMeasurer(text.getIterator(), frc); + return (float) tm.getLayout(mStart, mStart + mLen).getBounds().getWidth(); } public float measure(@IntRange(from = 0) int offset, boolean trailing, diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoColorEmoji-Regular.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoColorEmoji-Regular.ttf new file mode 100644 index 00000000..041d9d97 Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoColorEmoji-Regular.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoEmoji-VariableFont_wght.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoEmoji-VariableFont_wght.ttf new file mode 100644 index 00000000..0731d6a8 Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoEmoji-VariableFont_wght.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoSans-Italic-VariableFont_wdth,wght.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoSans-Italic-VariableFont_wdth,wght.ttf new file mode 100644 index 00000000..6245ba01 Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoSans-Italic-VariableFont_wdth,wght.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoSans-VariableFont_wdth,wght.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoSans-VariableFont_wdth,wght.ttf new file mode 100644 index 00000000..9530d84d Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoSans-VariableFont_wdth,wght.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoSansMono-VariableFont_wdth,wght.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoSansMono-VariableFont_wdth,wght.ttf new file mode 100644 index 00000000..c1c4bec8 Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoSansMono-VariableFont_wdth,wght.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoSansSymbols2-Regular.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoSansSymbols2-Regular.ttf new file mode 100644 index 00000000..78162689 Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoSansSymbols2-Regular.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoSerif-Italic-VariableFont_wdth,wght.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoSerif-Italic-VariableFont_wdth,wght.ttf new file mode 100644 index 00000000..da5b65df Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoSerif-Italic-VariableFont_wdth,wght.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/NotoSerif-VariableFont_wdth,wght.ttf b/AndroidCompat/src/main/resources/font/NotoSans/NotoSerif-VariableFont_wdth,wght.ttf new file mode 100644 index 00000000..1a24c3a0 Binary files /dev/null and b/AndroidCompat/src/main/resources/font/NotoSans/NotoSerif-VariableFont_wdth,wght.ttf differ diff --git a/AndroidCompat/src/main/resources/font/NotoSans/OFL.txt b/AndroidCompat/src/main/resources/font/NotoSans/OFL.txt new file mode 100644 index 00000000..ce2c42c5 --- /dev/null +++ b/AndroidCompat/src/main/resources/font/NotoSans/OFL.txt @@ -0,0 +1,93 @@ +Copyright 2022 The Noto Project Authors (https://github.com/notofonts/symbols) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://openfontlicense.org + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE.