ImageEn, unit hyieutils

TIEDictionary.GetDictionary

TIEDictionary.GetDictionary


Declaration

function GetDictionary(const key: WideString; recursive: boolean = true): TIEDictionary;


Description

Returns the TIEDictionary value to which the key is mapped in this dictionary. If the key doesn't exist an exception is raised.

Parameter Description
key A key in this dictionary
recursive If true then it will also search for this key in sub-dictionaries
Note: Use '#content' as the key to get content from tags (like <tag ...> content </tag>), e.g. dict := xmpDict.GetDictionary('#content', false);


Examples

// Dump dictionary content as XML
var
  dict: TIEDictionary;
begin
  dict := TIEDictionary.Create();
  dict.Insert('doublekey', 10.1);
  dict.Insert('integerkey', 100);
  dict.Insert('stringkey', 'hello');
  dict.Insert('bool_true', true);
  dict.Insert('bool_false', false);
  dict.Insert('dict', TIEDictionary.Create());
    dict.GetDictionary('dict').Insert('one', 1);
    dict.GetDictionary('dict').Insert('two', 'two');
  dict.Insert('list', TObjectList.Create());
    dict.GetList('list').Add( TIEDictionaryValueWideString.Create('mike') );
    dict.GetList('list').Add( TIEDictionaryValueWideString.Create('robert') );
    dict.GetList('list').Add( TIEDictionaryValueWideString.Create('john') );
    dict.GetList('list').Add( TIEDictionaryValueInteger.Create(2013) );

  memo1.Text := dict.Dump(ieplXML);

  dict.free;
end;


// Output all entries of dc:subject->rdf:Bag of JPEG XMP
{
Example XMP content:

  <dc:subject>
    <rdf:Bag>
       <rdf:li>China</rdf:li>
       <rdf:li>Jan 2008</rdf:li>
       <rdf:li>Shanghai</rdf:li>
    </rdf:Bag>
  </dc:subject>

Output:

  China
  Jan 2008
  Shanghai
}
var
  xmpDict: TIEDictionary;  // Dict containing image XMP data
  navDict: TIEDictionary;  // Dict to navigate XML structure
  list: TObjectList;
  i: integer;
begin
  ImageEnView1.IO.LoadFromFile( 'D:\XMP_Data_202112.JPG' );

  // Get XMP
  xmpDict := ImageEnView1.IO.Params.Dict.GetDictionary('XMP');

  // Output all entries of dc:subject->rdf:Bag into "memo1"
  navDict := xmpDict.GetDictionary('dc:subject', True);
  navDict := navDict.GetDictionary('#content', false);
  navDict := navDict.GetDictionary('rdf:Bag', false);
  navDict := navDict.GetDictionary('#content', false);
  list := navDict.GetList('rdf:li', false);
  for i := 0 to list.Count - 1 do
  begin
    navDict := TIEDictionary(list[i]).GetDictionary('#content', false);
    memo1.lines.add( navDict.GetString('#text', false) );
  end;
end;


// Parse all DictionaryEntries from XML file
// Download file from: www.imageen.com/files/UnitTestFiles/XML_TEST.xml
{
SAMPLE:

<?xml version="1.0" encoding="UTF-8"?>
<Proofreader>
 <Rules Language="EN">
   <DictionaryEntries>
     <Entry>absolute</Entry>
     <Entry>add</Entry>
     <Entry>ancestor</Entry>
     <Entry>anchor</Entry>
     <Entry>begin</Entry>
     ...


OUTPUT:

absolute
add
ancestor
anchor
begin
...
}
var
  ss : TStringList;
  xmlDict: TIEDictionary;  // Dict to parse source XML
  navDict: TIEDictionary;  // Dict to navigate XML structure
  list: TObjectList;
  i: integer;
begin
  ss := TStringList.Create;
  xmlDict := TIEDictionary.Create();
  ss.LoadFromFile('D:\XML_TEST.xml');
  xmlDict.parse( ss.Text );

  // Output all entries of Proofreader->Rules->DictionaryEntries into "memo1"
  navDict := xmlDict.GetDictionary('Proofreader', false);
  navDict := navDict.GetDictionary('#content', false);
  navDict := TIEDictionary(navDict.GetList('Rules', false)[0]);
  navDict := navDict.GetDictionary('#content', false);
  navDict := navDict.GetDictionary('DictionaryEntries', false);
  navDict := navDict.GetDictionary('#content', false);
  list := navDict.GetList('Entry', false);
  for i := 0 to list.Count - 1 do
  begin
    navDict := TIEDictionary(list[i]).GetDictionary('#content', false);
    memo1.lines.add( navDict.GetString('#text', false) );
  end;

  xmlDict.free;
  ss.Free;
end;


// Save a PNG file as a lossless WebP with maximum compression (using ImageMagick plug-in)
var
  imDict: TIEDictionary
begin
  ImageEnView1.IO.LoadFromFile( 'D:\Image.png' );
  imDict := TIEDictionary.Create();
  imDict.Insert( 'webp:lossless', true );
  imDict.Insert( 'webp:method', 0 );
  imDict.Insert( 'webp:auto-filter', true );
  ImageEnView1.IO.Params.Dict.Insert( 'ImageMagick', imDict );
  ImageEnView1.IO.SaveToFile( 'D:\Image_out.webp' );
end;

// List items in "ImageMagick" dictionary
var
  dict: TIEDictionary;
  curr: TIEStrStrEnumerator;
  key, val: String;
begin
  dict := TIEDictionary( ImageEnView1.IO.Params.Dict.Get( 'ImageMagick', True, False ));
  if dict = nil then
    exit;

  curr := TIEStrStrEnumerator.Create();
  try
    while dict.GetNext( curr ) do
    begin
      key := curr.item.key;
      val := IEDictValueToStr( curr.item.value );
      Memo1.Lines.Add( key + '=' + val );
    end;
  finally
    curr.Free();
  end;
end;
{
Result:
webp:method=0
webp:lossless=true
webp:auto-filter=true
}