Quellcode durchsuchen

LibGfx/TIFF: Honor the default value for single count tags

Some tags have a default value, we should return this value in
Metadata's getters when no value has been read from the input file.

Note that we don't support default values for tags with a count bigger
than one.
Lucas CHOLLET vor 1 Jahr
Ursprung
Commit
34e9059ae8
1 geänderte Dateien mit 15 neuen und 3 gelöschten Zeilen
  1. 15 3
      Userland/Libraries/LibGfx/TIFFGenerator.py

+ 15 - 3
Userland/Libraries/LibGfx/TIFFGenerator.py

@@ -9,7 +9,7 @@ import re
 from enum import Enum
 from collections import namedtuple
 from pathlib import Path
-from typing import List, Type
+from typing import Any, List, Type
 
 
 class EnumWithExportName(Enum):
@@ -251,6 +251,12 @@ def pascal_case_to_snake_case(name: str) -> str:
     return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
 
 
+def default_value_to_cpp(value: Any) -> str:
+    if isinstance(value, EnumWithExportName):
+        return f'TIFF::{value.export_name()}::{value.name}'
+    return str(value)
+
+
 def generate_getter(tag: Tag) -> str:
     biggest_type = retrieve_biggest_type(tag.types)
     variant_inner_type = tiff_type_to_cpp(biggest_type)
@@ -262,7 +268,8 @@ def generate_getter(tag: Tag) -> str:
         tag_final_type = f"TIFF::{tag.associated_enum.__name__}"
         extracted_value_template = f"static_cast<{tag_final_type}>({extracted_value_template})"
 
-    if len(tag.counts) == 1 and tag.counts[0] == 1 or is_container(biggest_type):
+    single_count = len(tag.counts) == 1 and tag.counts[0] == 1 or is_container(biggest_type)
+    if single_count:
         return_type = tag_final_type
         if is_container(biggest_type):
             return_type += ' const&'
@@ -289,11 +296,16 @@ def generate_getter(tag: Tag) -> str:
 
     signature = fR"    Optional<{return_type}> {pascal_case_to_snake_case(tag.name)}() const"
 
+    if tag.default and single_count:
+        return_if_empty = f'{default_value_to_cpp(tag.default)}'
+    else:
+        return_if_empty = 'OptionalNone {}'
+
     body = fR"""
     {{
         auto const& possible_value = m_data.get("{tag.name}"sv);
         if (!possible_value.has_value())
-            return OptionalNone {{}};
+            return {return_if_empty};
         {unpacked_if_needed}
     }}
 """