function export_fig_to_pdf_emf(fname, W_in, H_in, FS, FN)
% Force true 10-pt fonts, lock physical size, and export PDF + EMF (vector).
% Usage:
%   % after your plotting:
%   export_fig_to_pdf_emf('myFigure', 4.30, 3.30, 10, 'Arial');

if nargin < 1 || isempty(fname), fname = 'myFigure'; end
if nargin < 2 || isempty(W_in),  W_in  = 4.30; end
if nargin < 3 || isempty(H_in),  H_in  = 3.30; end
if nargin < 4 || isempty(FS),    FS    = 10;   end
if nargin < 5 || isempty(FN),    FN    = 'Arial'; end

f = gcf; ax = gca;

% ---------- Fonts: make EVERYTHING 10 pt ----------
set(f, 'DefaultAxesFontName', FN, ...
       'DefaultTextFontName', FN, ...
       'DefaultLegendFontName', FN, ...
       'DefaultAxesFontSize', FS, ...
       'DefaultTextFontSize', FS, ...
       'DefaultLegendFontSize', FS, ...
       'DefaultAxesTitleFontSizeMultiplier', 1, ...
       'DefaultAxesLabelFontSizeMultiplier', 1);

% Defaults only affect new objects; force existing ones too:
set(findall(f,'-property','FontUnits'), 'FontUnits','points');
set(findall(f,'-property','FontName'),  'FontName', FN);
set(findall(f,'-property','FontSize'),  'FontSize', FS);

ax.FontName = FN;
ax.FontSize = FS;

% ---------- Physical size & margins ----------
f.Units = 'inches';
f.Position(3:4) = [W_in, H_in];       % author at the exact width you’ll use in Word
ax.Units = 'inches';

% Tight inset + safety pad to avoid clipping at edges
ti  = ax.TightInset;                  % [left bottom right top] inches
pad = 0.06;                           % tweak 0.03–0.10 if needed
ax.LooseInset = max(ax.LooseInset, ti + pad);

set(ax,'Clipping','off');             % avoid stroke clipping on export

% ---------- Paper matches figure (no extra page border) ----------
f.PaperUnits        = 'inches';
f.PaperPositionMode = 'manual';
f.PaperPosition     = [0 0 W_in H_in];
f.PaperSize         = [W_in H_in];

set(f,'Renderer','painters');         % ensure vector output

% ---------- Exports ----------
% Vector PDF
print(f, [fname '.pdf'], '-dpdf', '-painters');

% Vector EMF (Windows only)
if ispc
    print(f, [fname '_word.emf'], '-dmeta', '-painters');
else
    warning('EMF export is Windows-only. On macOS/Linux, use SVG: exportgraphics(gcf,''%s.svg'',''ContentType'',''vector'');', fname);
end
end