# HPS - ComputeTextExtent example

**URL:** https://forum.techsoft3d.com/t/hps-computetextextent-example/2823
**Category:** HOOPS Visualize Desktop
**Tags:** how-to, knowledge-base
**Created:** [October 31, 2023, 9:31pm UTC](https://forum.techsoft3d.com/t/hps-computetextextent-example/2823 "2023-10-31T21:31:42Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![simon](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@simon](https://forum.techsoft3d.com/u/simon)
#### Post date: [October 31, 2023, 9:31pm UTC](https://forum.techsoft3d.com/t/hps-computetextextent-example/2823/1 "2023-10-31T21:31:42Z")

</div>

Below is sample code on how to calculate text size to draw a box around it. Please note that text’s ascender/descender are not included in calculation.

HPS also have pre-define background you can use as well.  
[[Text — HOOPS Visualize HPS 2023 SP2 U2 Documentation](https://docs.techsoft3d.com/hps/latest/prog_guide/0203_text.html#text-backgrounds)]([https://Pre-defined](https://Pre-defined) backround)

Sample code using hps\_mfc\_sandbox:

```auto
void CHPSView::OnUserCode3()
{
	// TODO: Add your command handler code here

    GetCanvas().GetFrontView().GetSegmentKey().GetCameraControl()
        .SetPosition(Point(0, 0, 5))
        .SetTarget(Point(0, 0, 0))
        .SetUpVector(Vector(0, 1, 0))
        .SetField(2.5f, 2.5f)
        .SetProjection(Camera::Projection::Orthographic);
    GetCanvas().GetFrontView().FitWorld().UpdateWithNotifier().Wait();
}

void CHPSView::OnUserCode4()
{
	// TODO: Add your command handler code here

    Point textPosition(-0.75f, 0.0f, 0.0f);
    TextKey text = GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey().InsertText(textPosition, "Testing");
    text.SetFont("roman").SetAlignment(Text::Alignment::Center).SetTransform(Text::Transform::NonTransformable);

    float xFrac;
    float yFrac;
	SprocketPath myPath(GetCanvas());
    myPath.GetKeyPath().ComputeTextExtent("Testing", xFrac, yFrac);

    Point textWindowPosition;
	myPath.GetKeyPath().ConvertCoordinate(Coordinate::Space::Object, textPosition, Coordinate::Space::Window, textWindowPosition);

    PointArray textWindowExtentPoints(5);
    textWindowExtentPoints[0] = textWindowPosition + Vector(-xFrac, -yFrac, 0);
    textWindowExtentPoints[1] = textWindowPosition + Vector(xFrac, -yFrac, 0);
    textWindowExtentPoints[2] = textWindowPosition + Vector(xFrac, yFrac, 0);
    textWindowExtentPoints[3] = textWindowPosition + Vector(-xFrac, yFrac, 0);
    textWindowExtentPoints[4] = textWindowExtentPoints[0];

    PointArray textObjectExtentPoints(5);
	myPath.GetKeyPath().ConvertCoordinate(
        Coordinate::Space::Window, textWindowExtentPoints, Coordinate::Space::Object, textObjectExtentPoints);

	GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey().Subsegment("text_bound").Flush();
    GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey().Subsegment("text_bound").InsertLine(textObjectExtentPoints);

	GetCanvas().GetFrontView().GetAttachedModel().GetSegmentKey().GetVisibilityControl().SetLines(true);

    GetCanvas().GetFrontView().UpdateWithNotifier().Wait();

}

```

Screenshot:

 ![Snag_156cda6f](https://us1.discourse-cdn.com/flex020/uploads/techsoft3d/original/2X/1/17f88a116a9f2ae3bc19e69109bdb18664c13250.png)
