I'm looking for an advice how to display high amount of images horizontally aligned with caption:
there are hundreds or even thousands images in the gallery
dimensions of images are unknown, their height is specified (eg. 4cm), width shall be scaled
images shall be aligned to the top/left
caption shall be centered to image, if caption is wider than image, caption shall be hyphenated, no overflow to side-captions or to below image shall happen
line break shall be detected, no horizontal overflow shall occur (count of images at different lines can differ based on images dimensions)
page break shall be detected, no vertical overflow shall happen
no page break shall be forced before or after gallery
solution shall not affect settings of any other images in document (there are for example other galleries with full-page size or individual text-wrapped images: none of already working parts of document shall be damaged using global settings)
Below is example how the gallery should look like (done in GIMP, it's not an output from any TeX source):
To be honest, I don't know at all how to implement such gallery. All examples I've found in Google presumed that count of images and dimensions were known and images were positioned manually at page, which is not fulfilled by above requirements.
I've attached some source below, but all attempts are just non-working nonsense. Nevertheless, attached source is useful, because it contains at least configuration of my document.
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage{spverbatim}
\usepackage[unicode]{hyperref}
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar`#1 \mskip 0mu plus 1mu\penalty100\relax}}}
\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z
\usepackage[]{graphicx}
\usepackage{grffile}
\usepackage{color}
\usepackage{wrapfig}
\usepackage{float}
\usepackage[section]{placeins}
\usepackage{hologo}
\usepackage[skip=8pt]{caption}
\usepackage{pdfpages}
\usepackage{needspace}
\intextsep=0pt
\DeclareTextFontCommand{\mytexttt}{\ttfamily\hyphenchar\font=45\relax}
\makeatletter
\renewcommand{\l@section}{\@dottedtocline{1}{1.5em}{2.6em}}
\renewcommand{\l@subsection}{\@dottedtocline{2}{4.0em}{3.6em}}
\renewcommand{\l@subsubsection}{\@dottedtocline{3}{7.4em}{4.8em}}
%\setlength{\@fptop}{0pt}
%\setlength{\@fpbot}{0pt plus 1fil}
\makeatother
\begin{document}
\raggedbottom
\chapter{Chapter}
\section{Section}
\begin{figure}[H]
\centering
\rule{6cm}{4cm}
\caption{Dokument}
\end{figure}
\begin{figure}[H]
\centering
\rule{3cm}{4cm}
\caption{Dokument}
\end{figure}
\begin{figure}[H]
\centering
\rule{5cm}{4cm}
\caption{Dokument}
\end{figure}
\begin{figure}[H]
\centering
\rule{7cm}{4cm}
\caption{Dokument}
\end{figure}
\chapter{Chapter}
\section{Section}
\rule{6cm}{4cm}
\rule{3cm}{4cm}
\rule{5cm}{4cm}
\rule{7cm}{4cm}
\rule{6cm}{4cm}
\rule{3cm}{4cm}
\rule{5cm}{4cm}
\rule{7cm}{4cm}
\rule{6cm}{4cm}
\rule{3cm}{4cm}
\rule{5cm}{4cm}
\rule{7cm}{4cm}
\rule{6cm}{4cm}
\rule{3cm}{4cm}
\rule{5cm}{4cm}
\rule{7cm}{4cm}
\chapter{Chapter}
\section{Section}
\begin{wrapfigure}{L}{0pt}
\centering
\rule{6cm}{4cm}
\caption{Dokument}
\end{wrapfigure}
\begin{wrapfigure}{L}{0pt}
\centering
\rule{3cm}{4cm}
\caption{Dokument}
\end{wrapfigure}
\begin{wrapfigure}{L}{0pt}
\centering
\rule{5cm}{4cm}
\caption{Dokument}
\end{wrapfigure}
\begin{wrapfigure}{L}{0pt}
\centering
\rule{7cm}{4cm}
\caption{Dokument}
\end{wrapfigure}
\chapter{Chapter}
\section{Section}
\begin{figure}[H]
\minipage{0pt}
\centering
\rule{6cm}{4cm}
\caption{Dokument}
\endminipage
\minipage{0pt}
\centering
\rule{3cm}{4cm}
\caption{Dokument}
\endminipage
\minipage{0pt}
\centering
\rule{5cm}{4cm}
\caption{Dokument}
\endminipage
\minipage{0pt}
\centering
\rule{7cm}{4cm}
\caption{Dokument}
\endminipage
\end{figure}
\chapter{Chapter}
\section{Section}
\begin{figure}[H]
\rule{6cm}{4cm}
\rule{3cm}{4cm}
\rule{5cm}{4cm}
\rule{7cm}{4cm}
\rule{6cm}{4cm}
\rule{3cm}{4cm}
\rule{5cm}{4cm}
\rule{7cm}{4cm}
\rule{6cm}{4cm}
\rule{3cm}{4cm}
\rule{5cm}{4cm}
\rule{7cm}{4cm}
\end{figure}
\end{document}
Any advice is welcome, because I'm not solving only some particular issue, but I'm looking for an approach how to implement above requirements.