\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{JoMe/JoMe}

% ============================================================================
% ENGINE CHECK
% This class embeds the original TrueType font files (Times New Roman,
% Franklin Gothic, Wingdings) via fontspec, therefore it MUST be compiled
% with LuaLaTeX (recommended) or XeLaTeX. It will NOT work with pdfLaTeX.
% ============================================================================
\RequirePackage{iftex}
\ifPDFTeX
  \PackageError{JoMe}{%
    This class uses fontspec with the bundled .ttf fonts and requires\MessageBreak
    LuaLaTeX (recommended) or XeLaTeX. Please compile with `lualatex'%
  }{Change the compiler to lualatex and rebuild.}
\fi

% Base class
\LoadClass[9pt, twoside, fleqn]{extarticle}

% Packages
\RequirePackage[fleqn]{amsmath}
\RequirePackage{fontspec}        % load the .ttf font files
% TeX text ligatures for ALL text fonts loaded below:
%   --  -> en dash (–),  ---  -> em dash (—),  ``/'' -> smart quotes.
% (TrueType fonts do not provide these by default; Wingdings/math opt out.)
\defaultfontfeatures{Ligatures=TeX}
\RequirePackage{unicode-math}    % Times-compatible math
\RequirePackage{geometry}
\RequirePackage{multicol}
\RequirePackage[cmyk]{xcolor}
\RequirePackage{tikz}
\RequirePackage{enumitem}
\RequirePackage{fancyhdr}
\RequirePackage{etoolbox}
\RequirePackage{float}
\RequirePackage{caption}
\RequirePackage{cite}
\RequirePackage[protrusion=false,verbose=silent]{microtype}
\usepackage{subcaption}
\usepackage{url}
\usepackage[per-mode=fraction]{siunitx}
\usepackage{tabulary}



% ---------------------------------------------------------
% FONTS -- loaded directly from the bundled .ttf files in JoMe/
% ---------------------------------------------------------
% Body text : Times New Roman (regular/bold/italic/bolditalic)
\setmainfont{Times New Roman}[
  Path        = JoMe/,
  Extension   = .ttf,
  UprightFont     = times,
  BoldFont        = timesbd,
  ItalicFont      = timesi,
  BoldItalicFont  = timesbi ]

% Times-compatible math font (Times New Roman has no math companion)
\setmathfont{TeX Gyre Termes Math}

% --- Franklin Gothic Book (regular + italic) ---------------------------------
% Abstract / Keywords body, Highlights bullets, reference list, footers,
% Data-availability-style blocks.
\newfontfamily\fgbook[
  Path=JoMe/, Extension=.ttf,
  UprightFont=FRABK, ItalicFont=FRABKIT,
  BoldFont=FRADM, BoldItalicFont=FRADMIT]{Franklin Gothic Book} % bold -> Demi weight

% Franklin Gothic Book, additionally condensed to 85 % (first-page header/footer)
\newfontfamily\fgbookC[
  Path=JoMe/, Extension=.ttf,
  UprightFont=FRABK, ItalicFont=FRABKIT,
  FakeStretch=0.85]{Franklin Gothic Book}

% --- Franklin Gothic Demi (regular + italic) ---------------------------------
% Labels: Abstract, Keywords, Highlights, Povzetek, Ključne besede; paginae.
\newfontfamily\fgdemi[
  Path=JoMe/, Extension=.ttf,
  UprightFont=FRADM, ItalicFont=FRADMIT]{Franklin Gothic Demi}

% --- Franklin Gothic Demi Cond -----------------------------------------------
% Plain (title).
\newfontfamily\fgcond[
  Path=JoMe/, Extension=.ttf,
  UprightFont=FRADMCN]{Franklin Gothic Demi Cond}

% Condensed to an additional 85 % : headings, figure/table captions,
% running-page section box, References title.
\newfontfamily\fgcondH[
  Path=JoMe/, Extension=.ttf,
  UprightFont=FRADMCN, FakeStretch=0.85]{Franklin Gothic Demi Cond}

% Condensed to an additional 90 % : in-text citations and reference numbers.
\newfontfamily\fgcondC[
  Path=JoMe/, Extension=.ttf,
  UprightFont=FRADMCN, FakeStretch=0.90]{Franklin Gothic Demi Cond}

% --- Wingdings (envelope icon) -----------------------------------------------
\newfontfamily\wingding[
  Path=JoMe/, Extension=.ttf,
  UprightFont=wingding]{Wingdings}



% Additional if for keywords
\newif\if@firstkeyword

% Custom colors
\definecolor{JoMEblue}{cmyk}{0.89, 0.43, 0, 0.20}

% Set base font size : Times New Roman 9 pt / 10.8 pt leading
\makeatletter
\renewcommand\normalsize{%
	\fontsize{9pt}{10.8pt}\selectfont
}
\makeatother

% Body text : justified, first-line indent 4 mm
\setlength{\parindent}{4mm}



% ---------------------------------------
% Page layout: exact dimensions
% ---------------------------------------
\geometry{
  paperwidth=210mm,     % Page width
  paperheight=277mm,    % Page height
  %
  % ---------------------------------------------------------
  % COLUMN SEPARATION
  % ---------------------------------------------------------
  columnsep=6mm,
  %
  % ---------------------------------------------------------
  % COLUMN WIDTHS
  % Each column must be exactly 88 mm wide.
  % The space between the two columns must be 6 mm.
  %
  % Total text width:
  %   88 mm (left column)
  % +  6 mm (column gap)
  % + 88 mm (right column)
  % = 182 mm total text width
  % ---------------------------------------------------------
  % textwidth=182mm, % overdefined
  %
  % ---------------------------------------------------------
  % LEFT AND RIGHT MARGINS
  % Page width = 210 mm
  % Text width = 182 mm
  %
  % Remaining horizontal space:
  %   210 mm − 182 mm = 28 mm
  %
  % Since margins must be symmetric:
  %   28 mm / 2 = 14 mm on each side
  % ---------------------------------------------------------
  left=14mm,
  right=14mm,
  %
  % ---------------------------------------------------------
  % COLUMN HEIGHT
  % Required column height is exactly 242 mm.
  % ---------------------------------------------------------
  % textheight=242mm, % overdefined
  %
  % ---------------------------------------------------------
  % TOP AND BOTTOM MARGINS
  % Page height = 277 mm
  % Text height = 242 mm
  %
  % Remaining vertical space:
  %   277 mm − 242 mm = 35 mm
  %
  % Split evenly:
  %   35 mm / 2 = 17.5 mm top and bottom margins
  % ---------------------------------------------------------
  top=17.5mm,
  bottom=17.5mm
}

% ---------------------------------------------------------
% Force exact column widths
% ---------------------------------------------------------
\setlength{\columnsep}{6mm}     % gap between columns
\setlength{\columnwidth}{88mm}  % exact width of each column


% ---------------------------------------
% Disable hyphenation (word breaking)
% ---------------------------------------
\hyphenpenalty=10000
\exhyphenpenalty=10000
\sloppy
% With hyphenation off + justified text, some lines cannot be set tightly.
% Allow extra inter-word stretch (so lines fit) and stop reporting the
% resulting "Underfull \hbox" messages, which are unavoidable here.
\emergencystretch=3em
\hbadness=10000

% ---------------------------------------
% Disable orphans and widows
% ---------------------------------------
\clubpenalty=10000  % no orphans (first line of paragraph at bottom of page)
\widowpenalty=10000 % no widows (last line of paragraph at top of page)



% ---------------------------------------
% title (full width)
% Franklin Gothic Demi Cond, left aligned
% ---------------------------------------
\renewcommand{\title}[1]{%
	\par\vspace{1em}%
	{\noindent
		\parbox[t]{\textwidth}{%
			\raggedright
			\fgcond\fontsize{24pt}{28pt}\selectfont
			#1%
		}%
	}%
	\par\vspace{1em}%
}




% ---------------------------------------------------------
% Envelope icon (Wingdings glyph F02A), coloured JoMEblue
% - \mailicon    : superscript, used after the author name
% - \mailiconbig : normal position, used before the corresponding e-mail
% ---------------------------------------------------------
\newcommand{\affil}[1]{\textsuperscript{#1}}

\newcommand{\mailiconbig}{\textcolor{JoMEblue}{\wingding\symbol{"F02A}}}
\newcommand{\mailicon}{\textsuperscript{\textcolor{JoMEblue}{\wingding\symbol{"F02A}}}}


% ---------------------------------------------------------
% Unified authors–affiliations–corresponding block
% - Authors      : Times New Roman 10 pt / 12 pt, spacing 2.5 mm above, 1 mm below
% - Corresponding: Times New Roman  8 pt / 9.6 pt, spacing 1 mm above
% ---------------------------------------------------------
\newcommand{\authors}[3]{%
  \par\vspace{2.5mm}%
  % Authors line
  {\noindent\fontsize{10pt}{12pt}\selectfont #1\par}%
  % Affiliations line
  {\noindent\fontsize{8pt}{9.6pt}\selectfont #2\par}%
  % Corresponding author line
  \vspace{1mm}%
  {\noindent\fontsize{8pt}{9.6pt}\selectfont \mailiconbig\,#3\par}%
  \vspace{1mm}%
}



% ---------------------------------------
% Abstract (full width)
% Label   : Franklin Gothic Demi        9 pt / 10.8 pt
% Body    : Franklin Gothic Book italic 9 pt / 10.8 pt
% Spacing : 10 mm above, 2.5 mm below
% ---------------------------------------
\renewenvironment{abstract}{%
  \par\vspace{1em}%
  \noindent{\fgdemi\fontsize{9pt}{10.8pt}\selectfont Abstract}\hspace{3mm}%
  \fgbook\itshape\fontsize{9pt}{10.8pt}\selectfont\ignorespaces
}{%
  \par\vspace{2.5mm}%
}

% ---------------------------------------------------------
% Keywords (full width):
% - user writes \item keyword
% - output is a single line: keyword1, keyword2, keyword3
% Label : Franklin Gothic Demi        9 pt / 10.8 pt
% Body  : Franklin Gothic Book italic 9 pt / 10.8 pt
% ---------------------------------------------------------
\makeatletter
\newenvironment{keywords}{%
  \par\vspace{2.5mm}%
  \noindent{\fgdemi\fontsize{9pt}{10.8pt}\selectfont Keywords}%
  \hspace{3mm}%
  \fgbook\itshape\fontsize{9pt}{10.8pt}\selectfont
  \@firstkeywordtrue
  \renewcommand{\item}[1]{%
    \unskip % remove any space before this item
    \if@firstkeyword
      \global\@firstkeywordfalse
      ##1%
    \else
      , ##1%
    \fi
  }%
}{%
  \par\vspace{2.5mm}%
}
\makeatother

% ---------------------------------------
% Highlights (full width)
% Label   : Franklin Gothic Demi        9 pt / 10.8 pt, indent 6 mm
% Bullets : Franklin Gothic Book italic 9 pt / 10.8 pt, indent 6 mm,
%           blue ▪ (small square) marker
% ---------------------------------------
\newcommand{\JoMEsquare}{\textcolor{JoMEblue}{\rule[0.15ex]{0.5ex}{0.5ex}}} % ▪ blue bullet
\newenvironment{highlights}{%
  \par\vspace{1mm}%
  \noindent\hspace*{6mm}{\fgdemi\fontsize{9pt}{10.8pt}\selectfont Highlights}%
  \par\vspace{-0.3em}%
  \fgbook\itshape\fontsize{9pt}{10.8pt}\selectfont
  \begin{itemize}[labelindent=6mm, leftmargin=*]
    \setlength\itemsep{-0.3em}
    \renewcommand{\labelitemi}{\JoMEsquare}% blue square marker
}{%
  \end{itemize}
  \par%
}

% ---------------------------------------
% Two-column start/stop
% ---------------------------------------
\newcommand{\starttwocolumns}{%
\vspace*{1em}%
\begin{multicols}{2}%
  % multicol lowers \hbadness to 5000; restore our no-warning setting so the
  % unavoidable loose lines (hyphenation off + narrow columns) stay silent.
  \hbadness=10000\relax
  \emergencystretch=3em\relax
  }

  \newcommand{\stoptwocolumns}{%
\end{multicols}
}



% ---------------------------------------
% Section headings
% Heading 1 & 2 : Franklin Gothic Demi Cond, 10 pt / 12 pt, +85 % condensed,
%                 blue, left aligned, 2 mm below.
%   * Level 1 is written in UPPER CASE, level 2 Capitalized (done by the author).
%   * An em-space (M-space) separates the number from the text — this is the
%     LaTeX default (\@seccntformat uses \quad), a fixed width that does not
%     stretch under justification.
%   * Before a level-1 heading : 2 blank lines; before level-2 : 1 blank line.
% ---------------------------------------
\setcounter{secnumdepth}{2}

\makeatletter
\renewcommand{\section}{%
  \@startsection{section}% name of the sectioning command
    {1}% level (1 = section, 2 = subsection, ...)
    {0pt}% indent from the left margin
    {-\dimexpr2\baselineskip\relax}% BEFORE skip : two lines
    {2mm}% AFTER skip (space below the heading)
    {\color{JoMEblue}\fgcondH\fontsize{10pt}{12pt}\selectfont}% style of the heading
}
\makeatother

\makeatletter
\renewcommand{\subsection}{%
  \@startsection{subsection}{2}{0pt}%
    {-\baselineskip}% BEFORE skip : one line
    {2mm}%
    {\color{JoMEblue}\fgcondH\fontsize{10pt}{12pt}\selectfont}%
}
\makeatother

\makeatletter
\renewcommand{\subsubsection}{%
  \@startsection{subsubsection}{3}{0pt}%
    {-\baselineskip}%
    {2mm}%
    {\color{JoMEblue}\fgcondH\normalsize}%
}
\makeatother



% ---------------------------------------
% header/footer
% ---------------------------------------
% Square separator ▪ for headers and footers
\newcommand{\svsquare}{\raisebox{0.5ex}{\hspace{1mm}\rule{0.6ex}{0.6ex}\hspace{1mm}}}
% Clear all header/footer styles
\fancypagestyle{firstpageheader}{
  \fancyhf{} % clear header/footer

  % Left side -- Franklin Gothic Book (regular) 7 pt / 8.4 pt, +85 % condensed
  \fancyhead[L]{%
    \fgbookC\fontsize{7pt}{8.4pt}\selectfont
    © The Authors. CC BY 4.0 Int. Licencee: SV-JME%
  }

  % Right side -- Franklin Gothic Book (regular) 7 pt / 8.4 pt, +85 % condensed
  \fancyhead[R]{%
    \fgbookC\fontsize{7pt}{8.4pt}\selectfont
    Strojniški vestnik -- Journal of Mechanical Engineering\,
    \svsquare\ VOL \volumenumber\,
    \svsquare\ NO \issuenumber\,
    \svsquare\ Y \yearnumber%
  }

  % Horizontal rule under header : 0.5 pt
  \renewcommand{\headrulewidth}{0.5pt}

  % Footer -- Franklin Gothic Book (regular) 9 pt / 10.8 pt, +85 % condensed
  %           page number (pagina) in Franklin Gothic Demi
  \fancyfoot[L]{\fgbookC\fontsize{9pt}{10.8pt}\selectfont DOI: \articledoi}
  \fancyfoot[R]{\fgdemi\fontsize{9pt}{10.8pt}\selectfont \thepage}
}

\fancypagestyle{otherpages}{
	\fancyhf{} % clear header/footer

	% Running-page section box : 40 mm × 4 mm, blue,
	% padding 1.5 mm left/right, 0.5 mm top/bottom.
	% Section label : Franklin Gothic Demi Cond, white, 9 pt / 10.8 pt, +85 % condensed.
	% LEFT (even pages)
	\fancyhead[LE]{%
		{\setlength{\fboxsep}{0pt}%
		\colorbox{JoMEblue}{%
			\parbox[c][4mm][c]{40mm}{%
				\color{white}\fgcondH\fontsize{9pt}{10.8pt}\selectfont
				\hspace*{1.5mm}\articlesection
			}%
		}}%
	}

	% RIGHT (odd pages)
	\fancyhead[RO]{%
		{\setlength{\fboxsep}{0pt}%
		\colorbox{JoMEblue}{%
			\parbox[c][4mm][c]{40mm}{%
				\raggedleft
				\color{white}\fgcondH\fontsize{9pt}{10.8pt}\selectfont
				\articlesection\hspace*{1.5mm}
			}%
		}}%
	}

	% No horizontal rule under header
	\renewcommand{\headrulewidth}{0pt}

	% Footer -- Franklin Gothic Book (regular) 9 pt / 10.8 pt,
	%           ▪ bullet separator, page number (pagina) in Franklin Gothic Demi.
	% Left side
	\fancyfoot[LE]{%
		\fgbook\fontsize{9pt}{10.8pt}\selectfont
		{\fgdemi\thepage} \svsquare\
		SV-JME\ \svsquare\
		VOL \volumenumber\ \svsquare\
		NO \issuenumber\ \svsquare\
		Y \yearnumber
	}

	% Right side
	\fancyfoot[RO]{%
		\fgbook\fontsize{9pt}{10.8pt}\selectfont
		SV-JME\ \svsquare\
		VOL \volumenumber\ \svsquare\
		NO \issuenumber\ \svsquare\
		Y \yearnumber\ \svsquare\
		{\fgdemi\thepage}
	}
}


% Apply headers
\AtBeginDocument{%
	\thispagestyle{firstpageheader} % first page only
	\pagestyle{otherpages}          % all other pages
}



% ---------------------------------------
% Equations
% ---------------------------------------
\setlength{\mathindent}{4mm}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{3pt}
\setlength{\abovedisplayshortskip}{0pt}
\setlength{\belowdisplayshortskip}{2pt}


% ---------------------------------------
% Itemize and enumerate
% ---------------------------------------
\setlist[itemize]{labelindent=6mm, leftmargin=*, itemsep=-0.3em, topsep=0.3em, label=\textcolor{JoMEblue}{\textbullet}}
\setlist[enumerate]{labelindent=6mm, leftmargin=*, itemsep=-0.3em, topsep=0.3em, label=\textcolor{JoMEblue}{\arabic*.}}

% ---------------------------------------
% Tables
% Caption : centred, Franklin Gothic Demi Cond, 8 pt / 9.6 pt, +85 % condensed,
%           M-space after the number, placed ABOVE the table, 1 mm below caption.
%           Horizontal rules 0.5 pt (\hline).
% ---------------------------------------
\setlength{\arrayrulewidth}{0.5pt} % 0.5 pt horizontal rules
\DeclareCaptionFont{fontTable}{\fgcondH\fontsize{8pt}{9.6pt}\selectfont}
\captionsetup[table]{
	labelsep=quad,       % M-space (em) between "Table 1" and the text
	justification=centering,
	singlelinecheck=false,
	font=fontTable,
	skip=2.85pt             % 1 mm between caption and table
}


% ---------------------------------------
% Figures
% Caption : centred, Franklin Gothic Demi Cond, 8 pt / 9.6 pt, +85 % condensed,
%           M-space after the number, placed BELOW the figure.
% ---------------------------------------
\DeclareCaptionFont{fontFigure}{\fgcondH\fontsize{8pt}{9.6pt}\selectfont}
\captionsetup[figure]{
	labelsep=quad,            % M-space (em) after "Fig. 1"
	name=Fig.,                % use "Fig." instead of "Figure"
	justification=centering,  % center the caption
	singlelinecheck=false,
	font=fontFigure,
	skip=4pt                  % small space between caption and figure
}

% Full-width figure environment
\newenvironment{widefigure}{
	\begin{figure*}[t]
}{
	\end{figure*}
}

% Caption font also usable for sub-captions (subfigure a), b), ...).
\captionsetup[sub]{font=fontFigure}


% ---------------------------------------
% References
% Title      : Heading 1 & 2 style (Franklin Gothic Demi Cond, +85 %, blue).
% List body  : Franklin Gothic Book italic, 8 pt / 9.6 pt, left indent 6 mm.
% Number [n] : Franklin Gothic Demi Cond, +90 % condensed, blue.
% In-text [n]: Franklin Gothic Demi Cond, +90 % condensed, blue.
% ---------------------------------------
\makeatletter
% In-text citations [1] : Franklin Gothic Demi Cond +90 %, blue
\renewcommand{\@cite}[2]{%
	{\upshape\fgcondC\textcolor{JoMEblue}{[#1]}}%
}
% Reference number [1] : Franklin Gothic Demi Cond +90 %, blue
\renewcommand{\@biblabel}[1]{%
	{\upshape\fgcondC\textcolor{JoMEblue}{[#1]}}%
}
% Reference list : Franklin Gothic Book italic 8 pt / 9.6 pt, left indent 6 mm
\renewenvironment{thebibliography}[1]{%
	\par\vspace{2ex}%
	\noindent{\color{JoMEblue}\fgcondH\fontsize{10pt}{12pt}\selectfont References}\par\vspace{-1ex}%
	\begingroup % start local group
	\fgbook\itshape\fontsize{8pt}{9.6pt}\selectfont % 8 pt italic references
	\list{\@biblabel{\arabic{enumiv}}}{%
		\settowidth\labelwidth{\@biblabel{#1}}%
		\leftmargin\labelwidth
		\advance\leftmargin\labelsep
		\usecounter{enumiv}%
		\setlength{\itemsep}{0pt}% no space between items
		\setlength{\parsep}{0pt}% no paragraph spacing
	}%
}{%
	\endlist
	\endgroup % end local group
}
\makeatother



% ---------------------------------------
% Data availability & Author contribution & AI statement
% & Slovene abstract / keywords
% Label : Franklin Gothic Demi        9 pt / 10.8 pt
% Body  : Franklin Gothic Book italic 9 pt / 10.8 pt, 2.5 mm below
% ---------------------------------------
\newcommand{\JoMEinfoblock}[1]{%
	\par\vspace{0.5em}%
	\noindent{\fgdemi\fontsize{9pt}{10.8pt}\selectfont #1}\hspace{3mm}%
	\fgbook\itshape\fontsize{9pt}{10.8pt}\selectfont\ignorespaces
}
\newenvironment{dataAvailability}{\JoMEinfoblock{Data availability}}{\par\vspace{2.5mm}}
\newenvironment{authorContribution}{\JoMEinfoblock{Author contribution}}{\par\vspace{2.5mm}}
\newenvironment{AIstatement}{\JoMEinfoblock{AI Assisted Writing}}{\par\vspace{2.5mm}}

% ---------------------------------------
% Slovene abstract and keywords
% (Povzetek/Ključne besede labels: Franklin Gothic Demi; body: Data-availability style)
% ---------------------------------------
\newenvironment{povzetek}{\JoMEinfoblock{Povzetek}}{\par\vspace{2.5mm}}
\newenvironment{kljucnebesede}{\JoMEinfoblock{Ključne besede}}{\par\vspace{2.5mm}}


% ---------------------------------------
% Appendix
% ---------------------------------------
\newcommand{\startappendix}{
	% Reset counters
	\setcounter{figure}{0}
	\setcounter{table}{0}
	\setcounter{equation}{0}
	% Prefix with S
	\renewcommand{\thefigure}{S\arabic{figure}}
	\renewcommand{\thetable}{S\arabic{table}}
	\renewcommand{\theequation}{S\arabic{equation}}
}


\endinput
