コード例 #1
0
        internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, VisualElement.MeasureMode widthMode, float height, VisualElement.MeasureMode heightMode, ITextHandle textHandle)
        {
            float measuredWidth  = float.NaN;
            float measuredHeight = float.NaN;

            if (textToMeasure == null || (ve.computedStyle.unityFont == null && ve.computedStyle.unityFontDefinition.IsEmpty()))
            {
                return(new Vector2(measuredWidth, measuredHeight));
            }

            var elementScaling = ve.ComputeGlobalScale();

            if (elementScaling.x + elementScaling.y <= 0 || ve.scaledPixelsPerPoint <= 0)
            {
                return(Vector2.zero);
            }

            float pixelsPerPoint = ve.scaledPixelsPerPoint;
            float pixelOffset    = 0.02f;
            float pointOffset    = pixelOffset / pixelsPerPoint;

            if (widthMode == VisualElement.MeasureMode.Exactly)
            {
                measuredWidth = width;
            }
            else
            {
                var textParams = MeshGenerationContextUtils.TextParams.MakeStyleBased(ve, textToMeasure);
                textParams.wordWrap = false;

                // Case 1215962: round up as yoga could decide to round down and text would start wrapping
                measuredWidth = textHandle.ComputeTextWidth(textParams, pixelsPerPoint);
                measuredWidth = measuredWidth < pointOffset ? 0 : AlignmentUtils.CeilToPixelGrid(measuredWidth, pixelsPerPoint, pixelOffset);

                if (widthMode == VisualElement.MeasureMode.AtMost)
                {
                    measuredWidth = Mathf.Min(measuredWidth, width);
                }
            }

            if (heightMode == VisualElement.MeasureMode.Exactly)
            {
                measuredHeight = height;
            }
            else
            {
                var textParams = MeshGenerationContextUtils.TextParams.MakeStyleBased(ve, textToMeasure);
                textParams.wordWrapWidth = measuredWidth;

                measuredHeight = textHandle.ComputeTextHeight(textParams, pixelsPerPoint);
                measuredHeight = measuredHeight < pointOffset ? 0 : AlignmentUtils.CeilToPixelGrid(measuredHeight, pixelsPerPoint, pixelOffset);

                if (heightMode == VisualElement.MeasureMode.AtMost)
                {
                    measuredHeight = Mathf.Min(measuredHeight, height);
                }
            }

            return(new Vector2(measuredWidth, measuredHeight));
        }
コード例 #2
0
 public float ComputeTextHeight(MeshGenerationContextUtils.TextParams parms, float scaling)
 {
     return(textHandle.ComputeTextHeight(parms, scaling));
 }