332 const char *filename;
333 int i;
334 jobjectArray sizeArray = NULL;
335 jfloat *dims;
336
337 // NOTE: cupsGetPPD returns a pointer to a filename of a temporary file.
338 // unlink() must be called to remove the file after using it.
339 filename = j2d_cupsGetPPD(name);
340 (*env)->ReleaseStringUTFChars(env, printer, name);
341 CHECK_NULL_RETURN(filename, NULL);
342 if ((ppd = j2d_ppdOpenFile(filename)) == NULL) {
343 unlink(filename);
344 DPRINTF("unable to open PPD %s\n", filename)
345 return NULL;
346 }
347 option = j2d_ppdFindOption(ppd, "PageSize");
348 if (option != NULL && option->num_choices > 0) {
349 // create array of dimensions - (num_choices * 6)
350 //to cover length & height
351 DPRINTF( "CUPSfuncs::option->num_choices %d\n", option->num_choices)
352 sizeArray = (*env)->NewFloatArray(env, option->num_choices*6);
353 if (sizeArray == NULL) {
354 unlink(filename);
355 j2d_ppdClose(ppd);
356 DPRINTF("CUPSfuncs::bad alloc new float array\n", "")
357 (*env)->ExceptionClear(env);
358 JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
359 return NULL;
360 }
361
362 dims = (*env)->GetFloatArrayElements(env, sizeArray, NULL);
363 if (dims == NULL) {
364 unlink(filename);
365 j2d_ppdClose(ppd);
366 (*env)->ExceptionClear(env);
367 JNU_ThrowOutOfMemoryError(env, "Could not create printer name");
368 return NULL;
369 }
370 for (i = 0; i<option->num_choices; i++) {
371 choice = (option->choices)+i;
372 size = j2d_ppdPageSize(ppd, choice->choice);
373 if (size != NULL) {
374 // paper width and height
375 dims[i*6] = size->width;
376 dims[(i*6)+1] = size->length;
377 // paper printable area
378 dims[(i*6)+2] = size->left;
379 dims[(i*6)+3] = size->top;
380 dims[(i*6)+4] = size->right;
381 dims[(i*6)+5] = size->bottom;
382 }
383 }
384
385 (*env)->ReleaseFloatArrayElements(env, sizeArray, dims, 0);
386 }
387
388 j2d_ppdClose(ppd);
389 unlink(filename);
390 return sizeArray;
391 }
|
332 const char *filename;
333 int i;
334 jobjectArray sizeArray = NULL;
335 jfloat *dims;
336
337 // NOTE: cupsGetPPD returns a pointer to a filename of a temporary file.
338 // unlink() must be called to remove the file after using it.
339 filename = j2d_cupsGetPPD(name);
340 (*env)->ReleaseStringUTFChars(env, printer, name);
341 CHECK_NULL_RETURN(filename, NULL);
342 if ((ppd = j2d_ppdOpenFile(filename)) == NULL) {
343 unlink(filename);
344 DPRINTF("unable to open PPD %s\n", filename)
345 return NULL;
346 }
347 option = j2d_ppdFindOption(ppd, "PageSize");
348 if (option != NULL && option->num_choices > 0) {
349 // create array of dimensions - (num_choices * 6)
350 //to cover length & height
351 DPRINTF( "CUPSfuncs::option->num_choices %d\n", option->num_choices)
352 // +1 is for storing the default media index
353 sizeArray = (*env)->NewFloatArray(env, option->num_choices*6+1);
354 if (sizeArray == NULL) {
355 unlink(filename);
356 j2d_ppdClose(ppd);
357 DPRINTF("CUPSfuncs::bad alloc new float array\n", "")
358 (*env)->ExceptionClear(env);
359 JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
360 return NULL;
361 }
362
363 dims = (*env)->GetFloatArrayElements(env, sizeArray, NULL);
364 if (dims == NULL) {
365 unlink(filename);
366 j2d_ppdClose(ppd);
367 (*env)->ExceptionClear(env);
368 JNU_ThrowOutOfMemoryError(env, "Could not create printer name");
369 return NULL;
370 }
371 for (i = 0; i<option->num_choices; i++) {
372 choice = (option->choices)+i;
373 // get the index of the default page
374 if (!strcmp(choice->choice, option->defchoice)) {
375 dims[option->num_choices*6] = (float)i;
376 }
377 size = j2d_ppdPageSize(ppd, choice->choice);
378 if (size != NULL) {
379 // paper width and height
380 dims[i*6] = size->width;
381 dims[(i*6)+1] = size->length;
382 // paper printable area
383 dims[(i*6)+2] = size->left;
384 dims[(i*6)+3] = size->top;
385 dims[(i*6)+4] = size->right;
386 dims[(i*6)+5] = size->bottom;
387 }
388 }
389
390 (*env)->ReleaseFloatArrayElements(env, sizeArray, dims, 0);
391 }
392
393 j2d_ppdClose(ppd);
394 unlink(filename);
395 return sizeArray;
396 }
|