【问题标题】:changing font and text sizes in SVG files in a batch批量更改 SVG 文件中的字体和文本大小
【发布时间】:2019-05-05 22:29:07
【问题描述】:

我的 SVG 文件很少。

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

有什么办法吗?

谢谢

【问题讨论】:

    标签: svg


    【解决方案1】:

    问题解决了。这是 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) )
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-12
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多