Skip to content

Commit 77ed03d

Browse files
committed
item language 'elng' can be get/set using either plain items or text-items (#1559)
1 parent 0d257b9 commit 77ed03d

6 files changed

Lines changed: 114 additions & 27 deletions

File tree

libheif/api/libheif/heif_items.cc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,50 @@ void heif_release_item_data(const heif_context* ctx, uint8_t** item_data)
182182
}
183183

184184

185+
heif_error heif_item_get_property_extended_language(const heif_context* context,
186+
heif_item_id itemId,
187+
char** out_language)
188+
{
189+
if (!out_language || !context) {
190+
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"};
191+
}
192+
193+
auto elng = context->context->find_property<Box_elng>(itemId);
194+
if (!elng) {
195+
return elng.error_struct(context->context.get());
196+
}
197+
198+
std::string lang = (*elng)->get_extended_language();
199+
*out_language = new char[lang.length() + 1];
200+
strcpy(*out_language, lang.c_str());
201+
202+
return heif_error_success;
203+
}
204+
205+
206+
heif_error heif_item_set_extended_language(heif_context* context,
207+
heif_item_id item_id,
208+
const char* language, heif_property_id* out_optional_propertyId)
209+
{
210+
if (!context || !language) {
211+
return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL passed"};
212+
}
213+
214+
Result<heif_property_id> property_id_result = context->context->add_text_property(item_id,
215+
language);
216+
217+
if (auto err = property_id_result.error()) {
218+
return err.error_struct(context->context.get());
219+
}
220+
221+
if (out_optional_propertyId) {
222+
*out_optional_propertyId = *property_id_result;
223+
}
224+
225+
return heif_error_success;
226+
}
227+
228+
185229
size_t heif_context_get_item_references(const heif_context* ctx,
186230
heif_item_id from_item_id,
187231
int index,

libheif/api/libheif/heif_items.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,27 @@ LIBHEIF_API
167167
void heif_release_item_data(const heif_context* ctx, uint8_t** item_data);
168168

169169

170+
// ------------------------- item language -------------------------
171+
172+
/**
173+
* Get the extended language associated with the item.
174+
* The item is usually a text item.
175+
*
176+
* @param context the heif file context containg the item.
177+
* @param itemId the identifier for the item
178+
* @param out_language output parameter with the item's language. Free with heif_string_release().
179+
* @return heif_error_ok on success, or an error value indicating the problem
180+
*/
181+
LIBHEIF_API
182+
heif_error heif_item_get_property_extended_language(const heif_context* context,
183+
heif_item_id itemId,
184+
char** out_language);
185+
186+
LIBHEIF_API
187+
heif_error heif_item_set_extended_language(heif_context* context,
188+
heif_item_id item_id,
189+
const char* language, heif_property_id* out_optional_propertyId);
190+
170191
// ------------------------- item references -------------------------
171192

172193
/**

libheif/api/libheif/heif_text.cc

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,16 @@ const char* heif_text_item_get_content(heif_text_item* text_item)
117117
return text_c;
118118
}
119119

120-
struct heif_error heif_item_get_property_extended_language(const heif_context* context,
121-
heif_item_id itemId,
122-
char** out_language)
120+
121+
heif_error heif_text_item_get_property_extended_language(const heif_text_item* text_item, char** out_language)
123122
{
124-
if (!out_language || !context) {
123+
if (!out_language || !text_item) {
125124
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"};
126125
}
127126

128-
auto elng = context->context->find_property<Box_elng>(itemId);
127+
auto elng = text_item->context->find_property<Box_elng>(text_item->text_item->get_item_id());
129128
if (!elng) {
130-
return elng.error_struct(context->context.get());
129+
return elng.error_struct(text_item->context.get());
131130
}
132131

133132
std::string lang = (*elng)->get_extended_language();
@@ -137,27 +136,22 @@ struct heif_error heif_item_get_property_extended_language(const heif_context* c
137136
return heif_error_success;
138137
}
139138

140-
struct heif_error heif_text_item_set_extended_language(heif_text_item* text_item, const char *language, heif_property_id* out_optional_propertyId)
139+
140+
heif_error heif_text_item_set_extended_language(heif_text_item* text_item, const char *language, heif_property_id* out_optional_propertyId)
141141
{
142142
if (!text_item || !language) {
143143
return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL passed"};
144144
}
145145

146-
if (auto img = text_item->context->get_image(text_item->text_item->get_item_id(), false)) {
147-
auto existing_elng = img->get_property<Box_elng>();
148-
if (existing_elng) {
149-
existing_elng->set_lang(std::string(language));
150-
return heif_error_success;
151-
}
152-
}
146+
Result<heif_property_id> property_id_result = text_item->context->add_text_property(text_item->text_item->get_item_id(),
147+
language);
153148

154-
auto elng = std::make_shared<Box_elng>();
155-
elng->set_lang(std::string(language));
156-
157-
heif_property_id id = text_item->context->add_property(text_item->text_item->get_item_id(), elng, false);
149+
if (auto err = property_id_result.error()) {
150+
return err.error_struct(text_item->context.get());
151+
}
158152

159153
if (out_optional_propertyId) {
160-
*out_optional_propertyId = id;
154+
*out_optional_propertyId = *property_id_result;
161155
}
162156

163157
return heif_error_success;

libheif/api/libheif/heif_text.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,16 @@ LIBHEIF_API
104104
const char* heif_text_item_get_content(heif_text_item* text_item);
105105

106106
/**
107-
* Get the extended language associated with the text item.
107+
* This function is similar to heif_item_get_property_extended_language(), but
108+
* takes a `heif_text_item` as parameter.
108109
*
109-
* @param context the context to get the text item from, usually from a file operation
110-
* @param itemId the identifier for the text item
111-
* @param out_language pointer to pointer to the resulting language
112-
* @return heif_error_ok on success, or an error value indicating the problem
110+
* @param text_item The text item for which we are requesting the language.
111+
* @param out_language Output parameter for the text language. Free with heif_string_release().
112+
* @return
113113
*/
114114
LIBHEIF_API
115-
heif_error heif_item_get_property_extended_language(const heif_context* context,
116-
heif_item_id itemId,
117-
char** out_language);
115+
heif_error heif_text_item_get_property_extended_language(const heif_text_item* text_item,
116+
char** out_language);
118117

119118
// --- adding text items
120119

libheif/context.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,24 @@ Result<heif_item_id> HeifContext::add_pyramid_group(const std::vector<heif_item_
18441844
}
18451845

18461846

1847+
Result<heif_property_id> HeifContext::add_text_property(heif_item_id itemId, const std::string& language)
1848+
{
1849+
if (find_property<Box_elng>(itemId)) {
1850+
return Error{
1851+
heif_error_Usage_error,
1852+
heif_suberror_Unspecified,
1853+
"Item already has an 'elng' language property."
1854+
};
1855+
}
1856+
1857+
auto elng = std::make_shared<Box_elng>();
1858+
elng->set_lang(std::string(language));
1859+
1860+
heif_property_id id = add_property(itemId, elng, false);
1861+
return id;
1862+
}
1863+
1864+
18471865
Error HeifContext::interpret_heif_file_sequences()
18481866
{
18491867
m_tracks.clear();

libheif/context.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ class HeifContext : public ErrorBuffer
165165

166166
Result<heif_item_id> add_pyramid_group(const std::vector<heif_item_id>& layers);
167167

168+
Result<heif_property_id> add_text_property(heif_item_id, const std::string& language);
169+
170+
168171
// --- region items
169172

170173
void add_region_item(std::shared_ptr<RegionItem> region_item)
@@ -271,6 +274,14 @@ class HeifContext : public ErrorBuffer
271274
return result;
272275
}
273276

277+
template<typename T>
278+
bool has_property(heif_item_id itemId) const
279+
{
280+
auto file = this->get_heif_file();
281+
auto result = file->get_property_for_item<T>(itemId);
282+
return result != nullptr;
283+
}
284+
274285
private:
275286
std::map<heif_item_id, std::shared_ptr<ImageItem>> m_all_images;
276287

0 commit comments

Comments
 (0)