private float MeasureNativeFontText(EntityManager em, Entity e, ref Text2DPrivateNative privNative, string text, string family, float2 rectTransformSize) { Text2DStyle style = em.GetComponentData <Text2DStyle>(e); Text2DRenderer tr = em.GetComponentData <Text2DRenderer>(e); Text2DStyleNativeFont styleNative = em.GetComponentData <Text2DStyleNativeFont>(e); NativeFont nf = em.GetComponentData <NativeFont>(styleNative.font); float newSize = style.size * nf.worldUnitsToPt; float outWidth = 0.0f, outHeight = 0.0f; js_measureText(text, family, newSize, styleNative.weight, styleNative.italic, out outWidth, out outHeight); if (em.HasComponent <Text2DAutoFit>(e)) { Text2DAutoFit autoFit = em.GetComponentData <Text2DAutoFit>(e); float epsilon = 0.001f; // Re-measure if the text is too long/short or too tall if (math.abs(outWidth - rectTransformSize.x) >= epsilon || outHeight > rectTransformSize.y) { float ratio = 1.0f; if (outWidth > 0.0f) { ratio = rectTransformSize.x / outWidth; } newSize = (newSize * ratio < autoFit.maxSize) ? newSize * ratio : autoFit.maxSize; js_measureText(text, family, newSize, styleNative.weight, styleNative.italic, out outWidth, out outHeight); if (outHeight > rectTransformSize.y) { if (outHeight > 0.0f) { ratio = rectTransformSize.y / outHeight; } newSize = (newSize * ratio < autoFit.maxSize) ? newSize * ratio : autoFit.maxSize; js_measureText(text, family, newSize, styleNative.weight, styleNative.italic, out outWidth, out outHeight); } } float minSize = autoFit.minSize; if (newSize < minSize) { //TODO: Update when float.ToString will not throws an exception anymore string l = "Text: " + text; l += " is not renderable, the generated font size " + ((int)newSize).ToString(); l += " is smaller than the minimum allowed " + ((int)minSize).ToString(); Debug.Log(l); } } //TODO: Update when float.ToString will not throws an exception anymore string line = "Text with native font, measured with: " + ((int)outWidth).ToString(); line += " measured height: "; line += ((int)outHeight).ToString(); Debug.Log(line); //Adjust text bounds with pivot privNative.bounds.x = -(rectTransformSize.x / 2) + (rectTransformSize.x * (tr.pivot.x)); privNative.bounds.y = -(rectTransformSize.y / 2) + (rectTransformSize.y * (tr.pivot.y)); privNative.bounds.width = outWidth; privNative.bounds.height = outHeight; return(newSize); }
public bool CheckNativeFontComponents(EntityManager em, ref Text2DRenderer tr, ref Text2DStyleNativeFont nf) { if (!em.Exists(tr.style)) { Debug.Log("Entity Text2DRenderer.style is not set "); return(false); } if (!em.Exists(nf.font)) { Debug.Log("Entity Text2DStyleNativeFont.font is not set "); return(false); } if (!em.HasComponent <Text2DStyle>(tr.style)) { Debug.LogFormat("Missing a Text2DStyle component on entity: {0}", tr.style.ToString()); return(false); } if (!em.HasComponent <NativeFont>(nf.font)) { Debug.LogFormat("Missing a NativeFont component on entity: {0}", nf.font.ToString()); return(false); } return(true); }