time | Calls | line |
---|
| | 1 | function objUnitsModified = modifyUnitsForPrint(...
|
| | 2 | modifyRevertFlag, varargin)
|
| | 3 | % MODIFYUNITSFORPRINT Modifies or restores a figure's axes and other
|
| | 4 | % object's units for printing. This undocumented helper function is for
|
| | 5 | % internal use.
|
| | 6 |
|
| | 7 | % This function is called during the print path. See usage in
|
| | 8 | % alternatePrintPath.m
|
| | 9 |
|
| | 10 | % MODIFYUNITSFORPRINT('modify', h) can be used to modify the units of the
|
| | 11 | % axes and other objects. The return will be: objUnitsModified
|
| | 12 | % which is a struct of cell arrays of the objects whose units
|
| | 13 | % were set to normalized, along with other related properties that were
|
| | 14 | % modified so that the output scales correctly (e.g. props that are
|
| | 15 | % implicitly measured in POINTS, such as line wides, marker sizes, and font units
|
| | 16 |
|
| | 17 | % The modifyRevertFlag can be used when calling this function
|
| | 18 | % to 'revert' the changes made during the 'modify' step
|
| | 19 | % MODIFYUNITSFORPRINT('revert', h, pixelObjects) reverts
|
| | 20 | % the units to their original values, before 'modify' was called.
|
| | 21 | % and restores/reverts the other related properties that had been modified
|
| | 22 | % so that the output scales correctly
|
| | 23 |
|
| | 24 | % Copyright 2013-2017 The MathWorks, Inc.
|
| | 25 |
|
< 0.001 | 2 | 26 | narginchk(2, 3)
|
| | 27 |
|
| | 28 | % The set of units to modify
|
< 0.001 | 2 | 29 | unitsToModify = {'centimeters', 'inches', 'characters', 'pixels', 'points'};
|
| | 30 |
|
< 0.001 | 2 | 31 | if strcmp(modifyRevertFlag, 'modify')
|
< 0.001 | 1 | 32 | narginchk(3, 3)
|
< 0.001 | 1 | 33 | h = varargin{1};
|
| | 34 | % we want to look at all objects, and can save some time by
|
| | 35 | % caching the handles up front
|
< 0.001 | 1 | 36 | if ishghandle(h, 'figure')
|
| | 37 | h = findall(h);
|
| | 38 | end
|
< 0.001 | 1 | 39 | dpiAdjustment = varargin{2};
|
| | 40 | % Find all objects with units of centimeters, inches, characters, or
|
| | 41 | % pixels, and change them to normalized so they can be printed
|
| | 42 | % appropriately. They will be stored as fields in struct
|
| | 43 | % objUnitsModified
|
0.001 | 1 | 44 | hUnits = findall(h, '-property', 'units', '-depth', 0);
|
| | 45 |
|
0.017 | 1 | 46 | objUnitsModified = getObjWithUnits(hUnits, ...
|
| 1 | 47 | 'Units', unitsToModify);
|
| | 48 |
|
0.001 | 1 | 49 | unitsModified = structfun(@(x) ~isempty(x), objUnitsModified);
|
< 0.001 | 1 | 50 | if any(unitsModified)
|
| | 51 | % If any units need changing, set them to normalized
|
< 0.001 | 1 | 52 | unitsToChange = unitsToModify(unitsModified);
|
< 0.001 | 1 | 53 | for idx=1:length(unitsToChange)
|
< 0.001 | 5 | 54 | cellfun(@(ph) set(ph, 'Units', 'normalized'), objUnitsModified.(unitsToChange{idx}).handles,...
|
| | 55 | 'UniformOutput', false);
|
< 0.001 | 5 | 56 | end
|
< 0.001 | 1 | 57 | end
|
| | 58 |
|
| | 59 | % for fontunits of pixels - change to points rather than normalized
|
0.002 | 1 | 60 | hPixelFontUnits = findall(h, '-property', 'fontunits', 'fontunits', 'pixels', '-depth', 0);
|
< 0.001 | 1 | 61 | objUnitsModified.fontunitsPixels = updatePixelFontUnits(hPixelFontUnits);
|
| | 62 |
|
| | 63 | % call scaleForPrinting on those chart objects that have the method
|
0.003 | 1 | 64 | selfScalingObjects = findall(h, '-isa','matlab.graphics.chart.Chart', 'visible', 'on', '-method', 'scaleForPrinting', '-depth', 0);
|
< 0.001 | 1 | 65 | objUnitsModified.selfScalingObjects = selfScalingObjects;
|
< 0.001 | 1 | 66 | for sObj = 1:numel(selfScalingObjects)
|
| | 67 | selfScalingObjects(sObj).scaleForPrinting('modify', dpiAdjustment);
|
| | 68 | end
|
| | 69 |
|
| | 70 | % NOTE: - when restoring, need to restore the font size for the scaled
|
| | 71 | % fonts first, before restoring font units to pixels (if any, from
|
| | 72 | % above)
|
| | 73 | % for fontunits that are "measured", need to adjust scale if paperpos >
|
| | 74 | % screen size
|
< 0.001 | 1 | 75 | if dpiAdjustment ~= 1
|
| | 76 | fontUnitsSelector = {'fontunits', 'inches', '-or', 'fontunits', 'points', '-or', ...
|
| | 77 | 'fontunits', 'centimeters'};
|
| | 78 | hMeasuredFontUnits = findall(h, '-property', 'fontunits', fontUnitsSelector, '-depth', 0);
|
| | 79 | assumedPointsFontUnits = findall(h, '-not', '-property', 'fontunits', '-property', 'fontsize', '-depth', 0);
|
| | 80 | hMeasuredFontUnits = [hMeasuredFontUnits; assumedPointsFontUnits];
|
| | 81 | scale = 1.0 / dpiAdjustment;
|
| | 82 | objUnitsModified.fontunitsMeasured = scaleObjectSizes(hMeasuredFontUnits, scale, 'FontSize', 'fontsize');
|
| | 83 |
|
| | 84 | % likewise scale line widths
|
| | 85 | lw = findall(h, 'visible', 'on', '-property', 'LineWidth', '-depth', 0);
|
| | 86 | objUnitsModified.lineobjects = scaleObjectSizes(lw, scale, 'LineWidth', 'linewidth');
|
| | 87 |
|
| | 88 | % and marker sizes
|
| | 89 | ms = findall(h, {'visible', 'on', '-property', 'MarkerSize', '-property', 'Marker', '-not', 'Marker', 'none'}, '-depth', 0);
|
| | 90 | objUnitsModified.markerobjects = scaleObjectSizes(ms, scale, 'MarkerSize', 'markersize');
|
| | 91 |
|
< 0.001 | 1 | 92 | else
|
< 0.001 | 1 | 93 | objUnitsModified.fontunitsMeasured = [];
|
< 0.001 | 1 | 94 | objUnitsModified.lineobjects = [];
|
< 0.001 | 1 | 95 | objUnitsModified.markerobjects = [];
|
< 0.001 | 1 | 96 | end
|
< 0.001 | 1 | 97 | elseif strcmp(modifyRevertFlag, 'revert')
|
< 0.001 | 1 | 98 | narginchk(2, 2)
|
< 0.001 | 1 | 99 | objUnitsModified = varargin{1};
|
| | 100 |
|
< 0.001 | 1 | 101 | if isempty(objUnitsModified)
|
| | 102 | return
|
| | 103 | end
|
| | 104 | % revert fontsizes for objects w/fontunits that were "measured"
|
| | 105 | % (undoes any scaling that was needed to account for paperposition
|
| | 106 | % size > screen size
|
< 0.001 | 1 | 107 | if ~isempty(objUnitsModified.fontunitsMeasured)
|
| | 108 | cellfun(@(ph, sz) set(ph, 'FontSize', sz), ...
|
| | 109 | objUnitsModified.fontunitsMeasured.handles, objUnitsModified.fontunitsMeasured.fontsize, 'UniformOutput', false);
|
| | 110 |
|
| | 111 | cellfun(@(ph, modeValue) setPropMode(ph, 'FontSizeMode', modeValue), ...
|
| | 112 | objUnitsModified.fontunitsMeasured.handles, objUnitsModified.fontunitsMeasured.fontsizemode, 'UniformOutput', false);
|
| | 113 | end
|
| | 114 | % revert fontunits for objects which were modified
|
< 0.001 | 1 | 115 | if ~isempty(objUnitsModified.fontunitsPixels)
|
| | 116 | if all(cellfun( @(ph) ishghandle(ph), objUnitsModified.fontunitsPixels.handles))
|
| | 117 | cellfun(@(ph, fSize) set(ph, 'FontUnits', 'pixels', 'FontSize', fSize),...
|
| | 118 | objUnitsModified.fontunitsPixels.handles, objUnitsModified.fontunitsPixels.fontsize, 'UniformOutput', false);
|
| | 119 |
|
| | 120 | % restore modes as well
|
| | 121 | cellfun(@(ph, modeValue) setPropMode(ph, 'FontSizeMode', modeValue), ...
|
| | 122 | objUnitsModified.fontunitsPixels.handles, objUnitsModified.fontunitsPixels.fontsizemode, 'UniformOutput', false);
|
| | 123 | cellfun(@(ph, modeValue) setPropMode(ph, 'FontUnitsMode', modeValue), ...
|
| | 124 | objUnitsModified.fontunitsPixels.handles, objUnitsModified.fontunitsPixels.fontunitsmode, 'UniformOutput', false);
|
| | 125 | end
|
| | 126 | end
|
| | 127 | % restore line widths and modes
|
< 0.001 | 1 | 128 | if ~isempty(objUnitsModified.lineobjects)
|
| | 129 | cellfun(@(ph, sz) set(ph, 'LineWidth', sz), ...
|
| | 130 | objUnitsModified.lineobjects.handles, objUnitsModified.lineobjects.linewidth, 'UniformOutput', false);
|
| | 131 |
|
| | 132 | cellfun(@(ph, modeValue) setPropMode(ph, 'LineWidthMode', modeValue), ...
|
| | 133 | objUnitsModified.lineobjects.handles, objUnitsModified.lineobjects.linewidthmode, 'UniformOutput', false);
|
| | 134 | end
|
| | 135 | % restore marker sizes and modes
|
< 0.001 | 1 | 136 | if ~isempty(objUnitsModified.markerobjects)
|
| | 137 | cellfun(@(ph, sz) set(ph, 'MarkerSize', sz), ...
|
| | 138 | objUnitsModified.markerobjects.handles, objUnitsModified.markerobjects.markersize, 'UniformOutput', false);
|
| | 139 |
|
| | 140 | cellfun(@(ph, modeValue) setPropMode(ph, 'MarkerSizeMode', modeValue), ...
|
| | 141 | objUnitsModified.markerobjects.handles, objUnitsModified.markerobjects.markersizemode, 'UniformOutput', false);
|
| | 142 | end
|
| | 143 |
|
< 0.001 | 1 | 144 | selfScalingObjects = objUnitsModified.selfScalingObjects;
|
< 0.001 | 1 | 145 | for sObj = 1:numel(selfScalingObjects)
|
| | 146 | selfScalingObjects(sObj).scaleForPrinting('revert');
|
| | 147 | end
|
| | 148 |
|
| | 149 | % Revert units and position for objects which were modified
|
| | 150 | % Need to loop over property sets because vectorized sets with cell
|
| | 151 | % array inputs is not enabled.
|
< 0.001 | 1 | 152 | for idx=1:length(unitsToModify)
|
< 0.001 | 5 | 153 | units = unitsToModify{idx};
|
< 0.001 | 5 | 154 | if ~isempty(objUnitsModified.(units))
|
< 0.001 | 5 | 155 | if all(cellfun(@(ph) ishghandle(ph), objUnitsModified.(units).handles))
|
< 0.001 | 5 | 156 | cellfun(@(ph, pos) set(ph, 'Units', units, 'Position', pos),...
|
| 5 | 157 | objUnitsModified.(units).handles, objUnitsModified.(units).positions, 'UniformOutput', false);
|
| | 158 | % restore modes as well ...
|
< 0.001 | 5 | 159 | cellfun(@(ph, modeValue) setPropMode(ph, 'PositionMode', modeValue), ...
|
| 5 | 160 | objUnitsModified.(units).handles,objUnitsModified.(units).positionmode, 'UniformOutput', false);
|
< 0.001 | 5 | 161 | cellfun(@(ph, modeValue) setPropMode(ph, 'UnitsMode', modeValue), ...
|
| 5 | 162 | objUnitsModified.(units).handles,objUnitsModified.(units).unitsmode, 'UniformOutput', false);
|
| | 163 |
|
< 0.001 | 5 | 164 | end
|
< 0.001 | 5 | 165 | end
|
< 0.001 | 5 | 166 | end
|
| | 167 | else
|
| | 168 | error(message('MATLAB:modifyunitsforprint:invalidFirstArgument'))
|
< 0.001 | 1 | 169 | end
|
| | 170 |
|
< 0.001 | 2 | 171 | end
|
Other subfunctions in this file are not included in this listing.