So Transparent That I Can't Catch It...Using Rghost to Emulate Ghostscript Command as Would Be on Virtual Printer

Sending ghostscript output file directly into ruby executable

Basically, you can't do this. The pdfwrite device opens the output stream as 'seekable' which clearly isn't compatible with a pipe or other similar construct. You need to have a local file.

Use Ghostscript to convert each page of a PDF to images and the output is still PDF

Your -switches include -dDetectDuplicateImages=true which under the circumstances should be superfluous and the device selection can be from one of four as pointed out by KenS.

gs -o 0.999.watermask.compact.screen.pdf -sDEVICE=pdfimage32 -dPDFSETTINGS=/screen 0.999.watermask.pdf

Sample Image

If you want to emulate MS Print As Image PDF on Windows you would find the result in some ways inferior (and often many times bigger). But for comparison it would be,

NOTE:- "%%printer%%... is for a batch file for a command line use "%printer%...

gswin64c.exe -sDEVICE=mswinpr2 -dNoCancel -o "%%printer%%Microsoft Print to PDF" -dPDFSETTINGS=/screen -f "0.999.watermask.pdf"

https://i.stack.imgur.com/kP9IO.png

Querying Ghostscript for the default options/settings of an output device (such as 'pdfwrite' or 'tiffg4')

Since Ghostscript is a full-blown PostScript interpreter, you can also send PostScript snippets to it which do not cause the drawing of page elements, but which query it for its internal state.

If you want to know what the default settings of the Display are, when you ask it via gs some.pdf to just display a PDF on screen, you could try this:

gs \
-c "currentpagedevice {exch ==only ( ) print == } forall"

On Windows this becomes:

gswin32c.exe ^
-c "currentpagedevice {exch ==only ( ) print == } forall"

The result is a list of /SomeName somevalue pairs which describe the settings used for rendering pages to the current screen.

This is so because usually the display is the default device for Ghostscript to send its output to. Now you may notice that you'll see an empty Ghostscript window pop up, which you'll have to close.... Ah, how about adding some options to avoid the popup window?

gs \
-o /dev/null \
-dNODISPLAY \
-c "currentpagedevice {exch ==only ( ) print == } forall"

Or, on Windows:

gswin32c.exe ^
-o nul ^
-dNODISPLAY ^
-c "currentpagedevice {exch ==only ( ) print == } forall"

But this will change the query return values, because you (unintentionally) changed the output device settings:

gs -c "currentpagedevice {exch ==only ( ) print == } forall" | grep Resolution

Result:

HWResolution [86.5426483 86.5426483]
/.MarginsHWResolution [1152.0 1152.0]

Compare this to

gs \
-o /dev/null \
-dNODISPLAY \
-c "currentpagedevice {exch ==only ( ) print == } forall" \
| grep Resolution

Result:

/HWResolution [72.0 72.0]
/.MarginsHWResolution [72.0 72.0]

So, please avoid this trap. I successfully fell into it a few years ago, and didn't even notice it for quite a long time...

Now assuming you want to query for the default settings of the PDF writing device, run this one:

gs \
-o /dev/null \
-sDEVICE=pdfwrite \
-c "currentpagedevice {exch ==only ( ) print == } forall" \
| tee ghostscript-pdfwrite-default-pagedevice-settings.txt

You'll now have all settings for the pdfwrite device in a *.txt file. and you may repeat that with some other interesting Ghostscript devices and then compare them for all their detailled differences:

for _dev in \
pswrite ps2write pdfwrite \
tiffg3 tiffg4 tiff12nc tiff24nc tiff32nc tiff48nc tiffsep \
jpeg jpeggray jpegcmyk \
png16 png16m png256 png48 pngalpha pnggray pngmono; \
do \
gs \
-o /dev/null \
-sDEVICE=${_dev} \
-c "currentpagedevice {exch ==only ( ) print == } forall" \
| sort \
| tee ghostscript-${_dev}-default-pagedevice-settings.txt; \
done

It's rather interesting to compare the settings for, say, the pswrite and ps2write devices like this (and also discover parameters which are available for the one, but not the other device):

sdiff -sbB ghostscript-ps*write-default-pagedevice-settings.txt

Update:

As you may imagine this is also a great way to compare different Ghostscript versions, and track how default settings may have changed for different devices in recent releases. This is especially interesting if you want to find out about all the newly implemented color profile and ICC support which is now present in Ghostscript.

Also, to avoid the return of just -dict- for certain key values, use the === instead of == macro. === acts like == but also prints the content of dictionaries.

So here is the example output for the pdfwrite device. Remember, Ghostscript's pdfwrite device is meant to provide mostly the same functionality as Adobe Acrobat Distiller (with the additional feature that it does not only accept PostScript as input, but also PDFs, so you can sort of redistill existing PDF files in order to repair, improve or otherwise manipulate them). Therefore, Ghostscript's pdfdevice honors most of the setdistillerparams operator which the original Distiller also supports:

gs \
-o /dev/null \
-sDEVICE=pdfwrite \
-c "currentpagedevice {exch ==only ( ) print === } forall" \
| sort

/%MediaDestination 0
/%MediaSource 0
/.AlwaysEmbed []
/.HWMargins [0.0 0.0 0.0 0.0]
/.IgnoreNumCopies false
/.LockSafetyParams false
/.MarginsHWResolution [720.0 720.0]
/.MediaSize [612.0 792.0]
/.NeverEmbed [/Courier /Courier-Bold /Courier-Oblique /Courier-BoldOblique /Helvetica /Helvetica-Bold /Helvetica-Oblique /Helvetica-BoldOblique /Times-Roman /Times-Bold /Times-Italic /Times-BoldItalic /Symbol /ZapfDingbats]
/ASCII85EncodePages false
/AllowIncrementalCFF false
/AllowPSRepeatFunctions false
/AlwaysEmbed []
/AntiAliasColorImages false [*]
/AntiAliasGrayImages false [*]
/AntiAliasMonoImages false [*]
/AutoFilterColorImages true
/AutoFilterGrayImages true
/AutoPositionEPSFiles true
/AutoRotatePages /PageByPage
/BeginPage {--.callbeginpage--}
/Binding /Left [*]
/BitsPerPixel 24
/BlueValues 256
/CalCMYKProfile (None) [*]
/CalGrayProfile (None) [*]
/CalRGBProfile (None) [*]
/CannotEmbedFontPolicy /Warning [*]
/CenterPages false
/ColorACSImageDict << /Blend 1 /VSamples [2 1 1 2] /QFactor 0.9 /HSamples [2 1 1 2] >>
/ColorConversionStrategy /LeaveColorUnchanged
/ColorImageDepth -1
/ColorImageDict << /Blend 1 /VSamples [2 1 1 2] /QFactor 0.9 /HSamples [2 1 1 2] >>
/ColorImageDownsampleThreshold 1.5
/ColorImageDownsampleType /Subsample
/ColorImageFilter /DCTEncode
/ColorImageResolution 150
/ColorValues 16777216
/Colors 3
/CompatibilityLevel 1.4
/CompressEntireFile false
/CompressFonts true
/CompressPages true
/ConvertCMYKImagesToRGB false
/ConvertImagesToIndexed true
/CoreDistVersion 5000
/CreateJobTicket false [*]
/DSCEncodingToUnicode []
/DefaultRenderingIntent /Default
/DetectBlends true [*]
/DetectDuplicateImages true
/DeviceGrayToK true
/DeviceLinkProfile ()
/DoNumCopies false
/DoThumbnails false [*]
/DocumentTimeSeq 0
/DocumentUUID ()
/DownsampleColorImages false
/DownsampleGrayImages false
/DownsampleMonoImages false
/EmbedAllFonts true
/EmitDSCWarnings false [*]
/EncodeColorImages true
/EncodeGrayImages true
/EncodeMonoImages true
/EncryptionR 0
/EndPage {--.callendpage--} [*]
/FirstObjectNumber 1
/FitPages false
/ForOPDFRead false
/GraphicICCProfile ()
/GraphicIntent 0
/GraphicsAlphaBits 1
/GrayACSImageDict << /Blend 1 /VSamples [2 1 1 2] /QFactor 0.9 /HSamples [2 1 1 2] >>
/GrayImageDepth -1
/GrayImageDict << /Blend 1 /VSamples [2 1 1 2] /QFactor 0.9 /HSamples [2 1 1 2] >>
/GrayImageDownsampleThreshold 1.5
/GrayImageDownsampleType /Subsample
/GrayImageFilter /DCTEncode
/GrayImageResolution 150
/GrayValues 256
/GreenValues 256
/HWResolution [720.0 720.0]
/HWSize [6120 7920]
/HaveCIDSystem false
/HaveTransparency true
/HaveTrueTypes true
/HighLevelDevice true
/ImageICCProfile ()
/ImageIntent 0
/ImageMemory 524288 [*]
/ImagingBBox null
/InputAttributes << 59 << /PageSize [612 792] >> 36 << /PageSize [649 918] >> 13 << /PageSize [595 842] >> 49 << /PageSize [354 499] >> 26 << /PageSize [2004 2835] >> 3 << /PageSize [792 1224] >> 62 << /PageSize [595 792] >> 39 << /PageSize [612 936] >> 16 << /PageSize [297 420] >> 52 << /PageSize [1460 2064] >> 29 << /PageSize [709 1001] >> 6 << /PageSize [2448 3168] >> 42 << /PageSize [396 612] >> 19 << /PageSize [105 148] >> 55 << /PageSize [516 729] >> 32 << /PageSize [2599 3677] >> 9 << /PageSize [1684 2384] >> 45 << /PageSize [1417 2004] >> 22 << /PageSize [1296 1728] >> 58 << /PageSize [612 1008] >> 35 << /PageSize [918 1298] >> 12 << /PageSize [842 1191] >> 48 << /PageSize [499 709] >> 25 << /PageSize [2835 4008] >> 2 << /PageSize [612 792] >> 61 << /PageSize [612 792] >> 38 << /PageSize [323 459] >> 15 << /PageSize [420 595] >> 51 << /PageSize [2064 2920] >> 28 << /PageSize [1001 1417] >> 5 << /PageSize [1585 2448] >> 64 << /PageSize [0 0 524287 524287] >> 41 << /PageSize [283 420] >> 18 << /PageSize [148 210] >> 54 << /PageSize [729 1032] >> 31 << /PageSize [354 499] >> 8 << /PageSize [2384 3370] >> 44 << /PageSize [2004 2835] >> 21 << /PageSize [864 1296] >> 57 << /PageSize [1224 792] >> 34 << /PageSize [1298 1837] >> 11 << /PageSize [1191 1684] >> 47 << /PageSize [709 1001] >> 24 << /PageSize [2592 3456] >> 1 << /PageSize [792 1224] >> 60 << /PageSize [612 792] >> 37 << /PageSize [459 649] >> 14 << /PageSize [595 842] >> 50 << /PageSize [2920 4127] >> 27 << /PageSize [1417 2004] >> 4 << /PageSize [1224 1585] >> 63 << /PageSize [792 1224] >> 40 << /PageSize [612 936] >> 17 << /PageSize [210 297] >> 53 << /PageSize [1032 1460] >> 30 << /PageSize [499 709] >> 7 << /PageSize [2016 2880] >> 43 << /PageSize [2835 4008] >> 20 << /PageSize [648 864] >> 56 << /PageSize [363 516] >> 33 << /PageSize [1837 2599] >> 10 << /PageSize [73 105] >> 46 << /PageSize [1001 1417] >> 23 << /PageSize [1728 2592] >> 0 << /PageSize [612.0 792.0] >> >>
/Install {--.callinstall--}
/InstanceUUID ()
/IsDistiller true
/KeyLength 0
/LZWEncodePages false
/Margins [0.0 0.0]
/MaxClipPathSize 12000
/MaxInlineImageSize 4000
/MaxPatternBitmap 0
/MaxSeparations 3
/MaxShadingBitmapSize 256000
/MaxSubsetPct 100
/MaxViewerMemorySize -1
/MonoImageDepth -1
/MonoImageDict << /K -1 >>
/MonoImageDownsampleThreshold 1.5
/MonoImageDownsampleType /Subsample
/MonoImageFilter /CCITTFaxEncode
/MonoImageResolution 300
/Name (pdfwrite)
/NeverEmbed [/Courier /Courier-Bold /Courier-Oblique /Courier-BoldOblique /Helvetica /Helvetica-Bold /Helvetica-Oblique /Helvetica-BoldOblique /Times-Roman /Times-Bold /Times-Italic /Times-BoldItalic /Symbol /ZapfDingbats]
/NoEncrypt ()
/NoT3CCITT false
/NumCopies null
/OPM 1
/OffOptimizations 0
/Optimize false [*]
/OutputAttributes << 0 << >> >>
/OutputDevice /pdfwrite
/OutputFile (/dev/null)
/OutputICCProfile (default_rgb.icc)
/OwnerPassword ()
/PDFA 0
/PDFACompatibilityPolicy 0
/PDFEndPage -1
/PDFStartPage 1
/PDFX false
/PDFXBleedBoxToTrimBoxOffset [0.0 0.0 0.0 0.0]
/PDFXSetBleedBoxToMediaBox true
/PDFXTrimBoxToMediaBoxOffset [0.0 0.0 0.0 0.0]
/PageCount 0
/PageDeviceName null
/PageOffset [0 0]
/PageSize [612.0 792.0]
/ParseDSCComments true
/ParseDSCCommentsForDocInfo true
/PatternImagemask false
/Permissions -4
/Policies << /PolicyReport {--dup-- /.LockSafetyParams --known-- {/setpagedevice --.systemvar-- /invalidaccess signalerror} --if-- --pop--} /PageSize 0 /PolicyNotFound 1 >>
/PreserveCopyPage true [*]
/PreserveDeviceN true
/PreserveEPSInfo true [*]
/PreserveHalftoneInfo false [*]
/PreserveOPIComments true [*]
/PreserveOverprintSettings true
/PreserveSMask true
/PreserveSeparation true
/PreserveTrMode true
/PrintStatistics false
/ProcessColorModel /DeviceRGB
/ProduceDSC true
/ProofProfile ()
/ReAssignCharacters true
/ReEncodeCharacters true
/RedValues 256
/RenderIntent 0
/RotatePages false
/SeparationColorNames []
/Separations false
/SetPageSize false
/SubsetFonts true
/TextAlphaBits 1
/TextICCProfile ()
/TextIntent 0
/TransferFunctionInfo /Preserve
/UCRandBGInfo /Preserve
/UseCIEColor false
/UseFastColor false
/UseFlateCompression true
/UsePrologue false [*]
/UserPassword ()
/WantsToUnicode true
/sRGBProfile (None) [*]

[*] Note:

According to the official documentation, the following settings (which are supported by Adobe Acrobat Distiller) currently on Ghostscript can be set and queried, but setting them does have no effect:

  • /AntiAliasColorImages
  • /AntiAliasGrayImages
  • /AntiAliasMonoImages
  • /AutoPositionEPSFiles
  • /Binding
  • /CalCMYKProfile
  • /CalGrayProfile
  • /CalRGBKProfile
  • /CannotEmbedFontPolicy
  • /ConvertImagesToIndexed
  • /CreateJobTicket
  • /DetectBlends
  • /DoThumbnails
  • /EmitDSCWarnings
  • /EndPage
  • /ImageMemory
  • /LockDistillerParams
  • /Optimize
  • /PreserveCopyPage
  • /PreserveEPSInfo
  • /PreserveHalftoneInfo
  • /PreserveOPIComments
  • /sRGBProfile
  • /StartPage
  • /UsePrologue

Avoid tryCatch to return something in case of fail

If you just want stop to not include the beginning part of the error message you just set the call. argument to FALSE.

f <- function(x){
tryCatch(
expr = {
x <- 1 + x
return(x)
}, error = function(cond){
stop("non-numeric argument to binary operator", call.=FALSE)
})
}
x <- 1
x <- f("a")

Error: non-numeric argument to binary operator

x
[1] 1

Enabling drag'n'drop in Vue, without build tools

the solution I came up with is based on creating a ghost element, removing dragged Item from items array.

First of All put itemId and innerContainerId on the item and innerContainer components, and also define eventListeners on item component:

   <div
v-for="innerContainer in innerContainers"
:data-innerContainerId="innerContainer.id"
class="innerContainersStyle drop-zone"
>

for item:

 <Item
class="itemStyle"
:item="item"
:data-itemId="item.id"
@dragstart="dragStart"
@drag="dragMove"

v-for="item in getItemsFromInnerContainer(innerContainer.id)"
>{{item.name}}</Item>

declaring functions:

    const dragStart = (event) => {
event.preventDefault();
//find Item in items and save
this.draggingItem = this.items.find(i => { i.id == event.target.dataset.itemId})
//remove item from initial container:
this.items.splice(1, this.items.indexOf(this.draggingItem))

//make the ghost elem: (big father is a parent element that can safely be relatively positioned)

const bigFather = document.querySelector('body')
const newDiv = `${event.target.outerHTML}`
bigFather.style.position = 'relative'
bigFather.insertAdjacentHTML('beforeend', newDiv)
let element = bigFather.lastElementChild;
// set the ghost element id to easily remove it on drop:
element.id = 'ghost-element'
setDraggingElStyle(element, event)
}

Set ghost element style:

    const setDraggingElStyle = (element, event) => {
const targetRect = event.target.getBoundingClientRect()
const translateLeft = `${event.clientX - targetRect.left}px`
const translateTop = `${event.clientY - targetRect.top}px`

element.style.position = 'absolute';
element.style.width = `${window.getComputedStyle(event.target).width}`
element.style.height = `${window.getComputedStyle(event.target).height}`
//you can set ghost element opacity here:
element.style.opacity = `0.5`
element.style.margin = '0 !important'
// set the ghost element background image, also you can set its opacity through rgba
element.style.backgroundImage = 'linear-gradient( rgba(0,0,0,.5) , rgba(0,0,0,.2) , rgba(0,0,0,.5) ) '
element.dir = 'rtl'
element.style.transform = `translate(-${translateLeft}, -${translateTop})`
setTopLeft(event, element)
}

this function moves the ghost element anywhere you drag it:

    const setTopLeft = (event, ghostElement) => {

if (!ghostElement) ghostElement = document.querySelector('#ghost-element')
let touchX = event.clientX
let touchY = event.clientY
try {
element.style.top = `${ window.scrollY + touchY}px`
element.style.left = `${window.scrollX + touchX}px`
} catch(err) {
console.log(element)
}
}

It has to update ghost elem top and left on drag:

    const dragMove = (event, element) => setTopLeft(event, element)

const dragEnd = (event, cb) => {
let targetEl;
// targetEl is set to be the destination innerContainer
targetEl = document.elementFromPoint(event.clientX, event.clientY);
const isDropZone = returnRealDropZone(targetEl, 2)
if (!isDropZone) return

targetEl = returnRealDropZone(targetEl, 2)
let targetContainerId = targetEl.dataset.innerContainerId
this.draggingItem.innerContainer = targetContainerId
// by pushing dragging item into items array (of course with the new innerContainer ID) it will be
// shown in the new container items.
this.items.push(this.draggingItem)
// you may want to prevent new created item from being created at the end of target container array.
// I suggest you index the container's children to put this item exactly where you want

//delete ghost element:
document.querySelector(`#ghost-element`).remove()
if (!targetEl ) return console.log('there is no targetEl')
cb(targetEl, event)
}

function returnRealDropZone(element, parentsCount) {
if (element.classList.contains('drop-zone')) return element
else if (parentsCount > 0 ) {
return returnRealDropZone(element.parentElement, parentsCount-1)
} else {
return false
}
}

UPDATE

here is code sandbox fit to your simplified project:
(Also changed the Item.js and put itemStyle in computed properties)

  computed: {
itemStyle() {
return {
width: this.getWidth() + "px",
height: this.calcHeight() + "px",
top: this.calcTop() + "px"
};
}

},

you can see codesanbox that works for me:

https://codesandbox.io/s/gifted-roentgen-pxyhdf?file=/Item.js

UPDATE 2

I can see that my solution won't work on Firefox; that's because Firefox returns DragEvent.clientX = 0.
To fix that, I have an idea which I'm pretty sure is not an economic and clean way!
On Mounted hook, you can get the mouse position from dragover event (on window) and set it on component properties (this.mouseX and this.mouseY)

  mounted() {
//get mouse coordinations for firefox
//(because firefox doesnt get DragEvent.clientX and DragEvent.clientY properly)
window.addEventListener('dragover', event => {
this.mouseX = event.x
this.mouseY = event.y
})
}

Then, you can use this properties in setTopLeft function in case the browser (firefox) return DragEvent.clientX = 0.

setTopLeft(event) {
let touchX = event.clientX || this.mouseX ;
let touchY = event.clientY || this.mouseY ;
.
.
.
}

(I also edited codesandbox)

How to remove ghost image when dragging an img using CSS or JavaScript?

Since @Kaiido don't want to answer the question, I'll answer this question myself using his advice.

As @Kaiido pointed out in the comments of the question, jQuery wraps the dom events. The original event is available through event.originalEvent. So I can just modify my "Trial and error #3" like

$('img').on('dragstart', function (event) {
var emptyImage = document.createElement('img');
// Set the src to be a 0x0 gif
emptyImage.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
event.originalEvent.dataTransfer.setDragImage(emptyImage, 0, 0);
});

Drawing a semi-transparent rectangle in iOS

I guess that the reason why you can't see the transparency of your rect is the presence of a background color in your view.

Would you try setting the view background color like this:

self.backgroundColor = [UIColor clearColor];

BTW, this statement, as well as any statement setting a property of your custom view (e.g., self.alpha = 0.7;) should go into the init method.

EDIT:

as pointed out by @borrrden, the real property to set in order to make the trick is possibly opaque. This is usually set to YES and thus implies the following, according to Apple Docs:

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.

Setting the background color to clearColor will likely set the value of this property to NO as a collateral effect.

EDIT:

I do not understand why you are overriding drawRect in a custom UIView to draw a semi-transparent rectangle, maybe because I do not know what you are trying to do. In any case, an alternative approach to this you might want to look into is using nested UIViews or nested CALayers, where you assign a different alpha/opacity to any of them.

If you are just interested in rectangles with varying degrees of opacity, this would be as simple as nesting UIViews in a parent view and assign the each of them a semi-transparent background color.



Related Topics



Leave a reply



Submit