From 4274490a866ea0a461e97a9c873a5c3a76be75b676cf7e0d09a1dfb77fd0241d Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Thu, 21 Aug 2025 17:36:42 -0400 Subject: [PATCH] Image owns its alt text. --- dw/image.cc | 20 +++++++++----------- dw/image.hh | 7 +++++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/dw/image.cc b/dw/image.cc index 8941b44..55cce25 100644 --- a/dw/image.cc +++ b/dw/image.cc @@ -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"); registerName ("dw::Image", typeid(*this)); - this->altText = altText ? dStrdup (altText) : NULL; + this->altText = altText; altTextWidth = -1; // not yet calculated buffer = NULL; bufWidth = bufHeight = -1; @@ -151,8 +151,6 @@ Image::Image(const char *altText) Image::~Image() { - if (altText) - free(altText); if (buffer) buffer->unref (); if (mapKey) @@ -177,10 +175,10 @@ void Image::sizeRequestSimpl (core::Requisition *requisition) requisition->ascent = buffer->getRootHeight (); requisition->descent = 0; } else { - if (altText && altText[0]) { + if (altText && altText.value()[0]) { if (altTextWidth == -1) altTextWidth = - layout->textWidth (getStyle()->font, altText, strlen (altText)); + layout->textWidth (getStyle()->font, altText.value().c_str(), altText.value().size()); requisition->width = altTextWidth; requisition->ascent = getStyle()->font->ascent; @@ -220,10 +218,10 @@ void Image::getExtremesSimpl (core::Extremes *extremes) if (buffer) contentWidth = buffer->getRootWidth (); else { - if (altText && altText[0]) { + if (altText && altText.value()[0]) { if (altTextWidth == -1) altTextWidth = - layout->textWidth (getStyle()->font, altText, strlen (altText)); + layout->textWidth (getStyle()->font, altText.value().c_str(), altText.value().size()); contentWidth = altTextWidth; } else contentWidth = 0; @@ -422,14 +420,14 @@ void Image::draw (core::View *view, core::Rectangle *area, } else { core::View *clippingView; - if (altText && altText[0]) { + if (altText && altText.value()[0]) { core::View *usedView = view; clippingView = NULL; if (altTextWidth == -1) altTextWidth = - layout->textWidth (getStyle()->font, altText, strlen (altText)); + layout->textWidth (getStyle()->font, altText.value().c_str(), altText.value().size()); if ((getContentWidth() < altTextWidth) || (getContentHeight() < @@ -445,7 +443,7 @@ void Image::draw (core::View *view, core::Rectangle *area, core::style::Color::SHADING_NORMAL, allocation.x + boxOffsetX (), allocation.y + boxOffsetY (), - getContentWidth(), getContentHeight(), altText); + getContentWidth(), getContentHeight(), altText.value().c_str()); if (clippingView) view->mergeClippingView (clippingView); diff --git a/dw/image.hh b/dw/image.hh index fc66910..5cf4c82 100644 --- a/dw/image.hh +++ b/dw/image.hh @@ -3,6 +3,9 @@ #include "core.hh" +#include +#include + namespace dw { /** @@ -115,7 +118,7 @@ public: class Image: public core::Widget, public core::ImgRenderer { private: - char *altText; + std::optional< std::string > altText; core::Imgbuf *buffer; int bufWidth, bufHeight; int altTextWidth; @@ -145,7 +148,7 @@ protected: //core::Iterator *iterator (Content::Type mask, bool atEnd); public: - Image(const char *altText); + Image(std::optional< std::string_view > altText); ~Image(); // For images, the minimal width is not well defined, and