fix: Corrected font-weight support for span

This commit is contained in:
Dimitris - Rafail Katsampas
2025-01-26 14:58:08 +02:00
committed by Nathan Walker
parent b12c41532a
commit 045986de8f
6 changed files with 17 additions and 36 deletions

View File

@ -47,7 +47,7 @@ export class Font extends FontBase {
return this._typeface;
}
getUIFont(defaultFont: UIFont): UIFont {
getUIFont(defaultFont: any): any {
return undefined;
}
}
@ -121,7 +121,7 @@ function loadFontFromFile(fontFamily: string, font: Font): android.graphics.Type
function createTypeface(font: Font): android.graphics.Typeface {
const fontFamilies = parseFontFamily(font.fontFamily);
const fontWeight = font.fontWeight;
const isNumericFontWeightSupported = SDK_VERSION > 27;
const isNumericFontWeightSupported = SDK_VERSION >= 28;
let result: android.graphics.Typeface;
let fontStyle: number = 0;

View File

@ -109,7 +109,7 @@ export class Font extends FontBase {
});
}
getAndroidTypeface(): android.graphics.Typeface {
getAndroidTypeface(): any {
return undefined;
}
}

View File

@ -3,7 +3,7 @@ import { getClosestPropertyValue, maxLinesProperty, textOverflowProperty } from
import { ShadowCSSValues } from '../styling/css-shadow';
// Requires
import { Font, isFontWeightBold } from '../styling/font';
import { Font } from '../styling/font';
import { backgroundColorProperty } from '../styling/style-properties';
import { TextBaseCommon, formattedTextProperty, textAlignmentProperty, textDecorationProperty, textProperty, textTransformProperty, textShadowProperty, textStrokeProperty, letterSpacingProperty, whiteSpaceProperty, lineHeightProperty, resetSymbol } from './text-base-common';
import { Color } from '../../color';
@ -593,33 +593,17 @@ function createSpannableStringBuilder(formattedString: FormattedString, defaultF
function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span, start: number, end: number, defaultFontSize: number): void {
const spanStyle = span.style;
const bold = isFontWeightBold(spanStyle.fontWeight);
const italic = spanStyle.fontStyle === 'italic';
const align = spanStyle.verticalAlignment;
// We set font style using StyleSpan in case the font doesn't support font styles
if (bold && italic) {
ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (bold) {
ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (italic) {
ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.ITALIC), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
const fontFamily = span.fontFamily;
if (fontFamily) {
const font = new Font(fontFamily, 0, spanStyle.fontStyle, spanStyle.fontWeight, spanStyle.fontScaleInternal, spanStyle.fontVariationSettings);
const typeface = font.getAndroidTypeface() || android.graphics.Typeface.create(fontFamily, 0);
const typefaceSpan: android.text.style.TypefaceSpan = new org.nativescript.widgets.CustomTypefaceSpan(fontFamily, typeface);
const font = new Font(spanStyle.fontFamily, spanStyle.fontSize, spanStyle.fontStyle, spanStyle.fontWeight, spanStyle.fontScaleInternal, spanStyle.fontVariationSettings);
const typefaceSpan = new org.nativescript.widgets.CustomTypefaceSpan(font.getAndroidTypeface());
ssb.setSpan(typefaceSpan, start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (spanStyle.fontSize) {
ssb.setSpan(new android.text.style.AbsoluteSizeSpan(layout.toDevicePixels(spanStyle.fontSize)), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
const realFontSize = span.fontSize;
if (realFontSize) {
ssb.setSpan(new android.text.style.AbsoluteSizeSpan(realFontSize * layout.getDisplayDensity()), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
const color = span.color;
const color = spanStyle.color;
if (color) {
ssb.setSpan(new android.text.style.ForegroundColorSpan(color.android), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
@ -646,7 +630,7 @@ function setSpanModifiers(ssb: android.text.SpannableStringBuilder, span: Span,
if (align) {
initializeBaselineAdjustedSpan();
ssb.setSpan(new BaselineAdjustedSpan(defaultFontSize * layout.getDisplayDensity(), align), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new BaselineAdjustedSpan(layout.toDevicePixels(defaultFontSize), align), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
const tappable = span.tappable;

View File

@ -654,7 +654,7 @@
}
export class CustomTypefaceSpan extends android.text.style.TypefaceSpan {
constructor(family: string, typeface: android.graphics.Typeface);
constructor(typeface: android.graphics.Typeface);
}
}
}

View File

@ -7,15 +7,12 @@ import android.text.style.TypefaceSpan;
/**
* Created by hhristov on 2/27/17.
* Updated by CatchABus on 1/26/25.
*/
@SuppressLint("ParcelCreator")
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface typeface;
public CustomTypefaceSpan(String family, Typeface typeface) {
super(family);
this.typeface = typeface;
public CustomTypefaceSpan(Typeface typeface) {
super(typeface);
}
public void updateDrawState(TextPaint ds) {
@ -30,7 +27,7 @@ public class CustomTypefaceSpan extends TypefaceSpan {
final Typeface old = paint.getTypeface();
final int oldStyle = (old == null) ? 0 : old.getStyle();
Typeface typeface = this.typeface;
Typeface typeface = this.getTypeface();
int fake = oldStyle & ~typeface.getStyle();
if ((fake & android.graphics.Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);