批量更改SVG文件中的字体和文本大小

时间:2019-04-24 17:19:53

标签: svg

我有几个hundert SVG文件。

我正在寻找一种方法来更改所有字体,文本大小并重置最终的转换矩阵(以使文本不失真),以使所有文件中的文本都相同。

有什么想法怎么做?

谢谢

1 个答案:

答案 0 :(得分:0)

问题解决了。这是Matlab的代码,用于更改svg对象的属性。


% this script requires the following functions

% struct2xml
% https://www.mathworks.com/matlabcentral/fileexchange/28639-struct2xml

% xml2struct
% https://uk.mathworks.com/matlabcentral/fileexchange/28518-xml2struct


% read the file into structure

filename = 'car_on_a_road.svg';

XML = xml2struct(filename);

% the text and its params are stored in flowRoot
for i = 1:length(XML.svg.g.flowRoot)
    %     modify fonts
    if length(XML.svg.g.flowRoot{i}.flowPara) == 1
        if isfield(XML.svg.g.flowRoot{i}.flowPara.Attributes,'style')==1
        XML.svg.g.flowRoot{i}.flowPara.Attributes.style='font-size:16px';
        end
    end

    %     for 2 line text
    if length(XML.svg.g.flowRoot{i}.flowPara) == 2
        XML.svg.g.flowRoot{i}.flowPara{1}.Attributes.style='font-style:italic;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:100%;font-family:''Times New Roman'';-inkscape-font-specification:''Times New Roman, Italic'';text-align:start;writing-mode:lr-tb;text-anchor:start';
        XML.svg.g.flowRoot{i}.flowPara{2}.Attributes.style='font-style:italic;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:100%;font-family:''Times New Roman'';-inkscape-font-specification:''Times New Roman, Italic'';text-align:start;writing-mode:lr-tb;text-anchor:start';
    end

    if isfield(XML.svg.g.flowRoot{i}.Attributes,'style')==1
     XML.svg.g.flowRoot{i}.Attributes.style='font-style:italic;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:16px;line-height:100%;font-family:''Times New Roman'';-inkscape-font-specification:''Times New Roman, Italic'';text-align:start;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none';   
    end



    % only do if "transform" field exists
    if isfield(XML.svg.g.flowRoot{i}.Attributes,'transform')==1
        % only do if "matrix" text exists in this field
        if contains(XML.svg.g.flowRoot{i}.Attributes.transform,'matrix')
            % read original text position
            parsed_matrix_data = regexp(XML.svg.g.flowRoot{i}.Attributes.transform,',','split');
            %       
            E = str2double(parsed_matrix_data{5});
            F = str2double(regexprep(parsed_matrix_data{6},'[)]',''));

            % reset transformation matrix to remove any stretching of the text
            XML.svg.g.flowRoot{i}.Attributes.transform = sprintf('translate(%f, %f )',E,F);
        end

    end

end

% save to file, to different dir
struct2xml( XML, strcat('modified\',filename) )