Image owns its alt text.

This commit is contained in:
2025-08-21 17:36:42 -04:00
parent b934b115ed
commit 4274490a86
2 changed files with 14 additions and 13 deletions

View File

@ -131,11 +131,11 @@ int ImageMapsList::link (object::Object *key, int x, int y)
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
Image::Image(const char *altText) Image::Image(const std::optional< std::string_view > altText)
{ {
DBG_OBJ_CREATE ("dw::Image"); DBG_OBJ_CREATE ("dw::Image");
registerName ("dw::Image", typeid(*this)); registerName ("dw::Image", typeid(*this));
this->altText = altText ? dStrdup (altText) : NULL; this->altText = altText;
altTextWidth = -1; // not yet calculated altTextWidth = -1; // not yet calculated
buffer = NULL; buffer = NULL;
bufWidth = bufHeight = -1; bufWidth = bufHeight = -1;
@ -151,8 +151,6 @@ Image::Image(const char *altText)
Image::~Image() Image::~Image()
{ {
if (altText)
free(altText);
if (buffer) if (buffer)
buffer->unref (); buffer->unref ();
if (mapKey) if (mapKey)
@ -177,10 +175,10 @@ void Image::sizeRequestSimpl (core::Requisition *requisition)
requisition->ascent = buffer->getRootHeight (); requisition->ascent = buffer->getRootHeight ();
requisition->descent = 0; requisition->descent = 0;
} else { } else {
if (altText && altText[0]) { if (altText && altText.value()[0]) {
if (altTextWidth == -1) if (altTextWidth == -1)
altTextWidth = altTextWidth =
layout->textWidth (getStyle()->font, altText, strlen (altText)); layout->textWidth (getStyle()->font, altText.value().c_str(), altText.value().size());
requisition->width = altTextWidth; requisition->width = altTextWidth;
requisition->ascent = getStyle()->font->ascent; requisition->ascent = getStyle()->font->ascent;
@ -220,10 +218,10 @@ void Image::getExtremesSimpl (core::Extremes *extremes)
if (buffer) if (buffer)
contentWidth = buffer->getRootWidth (); contentWidth = buffer->getRootWidth ();
else { else {
if (altText && altText[0]) { if (altText && altText.value()[0]) {
if (altTextWidth == -1) if (altTextWidth == -1)
altTextWidth = altTextWidth =
layout->textWidth (getStyle()->font, altText, strlen (altText)); layout->textWidth (getStyle()->font, altText.value().c_str(), altText.value().size());
contentWidth = altTextWidth; contentWidth = altTextWidth;
} else } else
contentWidth = 0; contentWidth = 0;
@ -422,14 +420,14 @@ void Image::draw (core::View *view, core::Rectangle *area,
} else { } else {
core::View *clippingView; core::View *clippingView;
if (altText && altText[0]) { if (altText && altText.value()[0]) {
core::View *usedView = view; core::View *usedView = view;
clippingView = NULL; clippingView = NULL;
if (altTextWidth == -1) if (altTextWidth == -1)
altTextWidth = altTextWidth =
layout->textWidth (getStyle()->font, altText, strlen (altText)); layout->textWidth (getStyle()->font, altText.value().c_str(), altText.value().size());
if ((getContentWidth() < altTextWidth) || if ((getContentWidth() < altTextWidth) ||
(getContentHeight() < (getContentHeight() <
@ -445,7 +443,7 @@ void Image::draw (core::View *view, core::Rectangle *area,
core::style::Color::SHADING_NORMAL, core::style::Color::SHADING_NORMAL,
allocation.x + boxOffsetX (), allocation.x + boxOffsetX (),
allocation.y + boxOffsetY (), allocation.y + boxOffsetY (),
getContentWidth(), getContentHeight(), altText); getContentWidth(), getContentHeight(), altText.value().c_str());
if (clippingView) if (clippingView)
view->mergeClippingView (clippingView); view->mergeClippingView (clippingView);

View File

@ -3,6 +3,9 @@
#include "core.hh" #include "core.hh"
#include <optional>
#include <string>
namespace dw { namespace dw {
/** /**
@ -115,7 +118,7 @@ public:
class Image: public core::Widget, public core::ImgRenderer class Image: public core::Widget, public core::ImgRenderer
{ {
private: private:
char *altText; std::optional< std::string > altText;
core::Imgbuf *buffer; core::Imgbuf *buffer;
int bufWidth, bufHeight; int bufWidth, bufHeight;
int altTextWidth; int altTextWidth;
@ -145,7 +148,7 @@ protected:
//core::Iterator *iterator (Content::Type mask, bool atEnd); //core::Iterator *iterator (Content::Type mask, bool atEnd);
public: public:
Image(const char *altText); Image(std::optional< std::string_view > altText);
~Image(); ~Image();
// For images, the minimal width is not well defined, and // For images, the minimal width is not well defined, and