-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from aMytho/tag-inspector
Add Tag inspector
- Loading branch information
Showing
15 changed files
with
357 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
@* Copyright (C) Jonathan Shull - See license file at github.com/amytho/pdf-acc-toolset *@ | ||
|
||
@using iText.Kernel.Pdf.Tagutils; | ||
@using Pdf_Acc_Toolset.Services.Util; | ||
@using iText.Kernel.Pdf.Tagging; | ||
@using iText.Kernel.Pdf; | ||
@using iText.Layout; | ||
@using Pdf_Acc_Toolset.Services.UI; | ||
|
||
<div class="p-2 border border-l-2 border-y-0 border-r-0 border-solid border-gray-500" | ||
@oncontextmenu:stopPropagation="true" @oncontextmenu:preventDefault="true" @oncontextmenu="ShowTagInfo"> | ||
<div class="flex flex-row p-1"> | ||
<div> | ||
<button @onclick:stopPropagation="true" @onclick:preventDefault="true" @onclick="toggleKids" | ||
class="cursor-pointer border border-solid border-gray-900 p-1 text-lg font-semibold" title="Toggle Children">@ShowHideBtn()</button> | ||
</div> | ||
<p class="ml-2"> @GetTagType() @GetTitle()</p> | ||
</div> | ||
|
||
<div class="ml-1 @ShowChildrenClass()"> | ||
@foreach (TagTreePointer tag in GetChildren()) | ||
{ | ||
<div class="m-2"> | ||
<TagNode node="tag" /> | ||
</div> | ||
} | ||
</div> | ||
</div> | ||
|
||
|
||
@code { | ||
[Parameter] | ||
public TagTreePointer node { get; set; } | ||
|
||
private bool showKids = true; | ||
|
||
private string GetTagType() | ||
{ | ||
// Returns the tag name in the role mapped form | ||
return TagUtil.ConvertRole(new PdfName(node.GetRole())); | ||
} | ||
|
||
private bool HasChildTag() | ||
{ | ||
return node.GetKidsRoles().Count != 0; | ||
} | ||
|
||
private IList<TagTreePointer> GetChildren() | ||
{ | ||
// List which will be returned with the tags | ||
List<TagTreePointer> matchingTags = new(); | ||
|
||
// Start looking | ||
CheckChildElement(node); | ||
|
||
// Recursive function to check all tags in the tag tree | ||
void CheckChildElement(TagTreePointer children) | ||
{ | ||
// Create a copy of the current tag | ||
PdfStructElem currentTag = children.GetContext().GetPointerStructElem(children); | ||
|
||
// Get each child | ||
IList<IStructureNode> kids = currentTag.GetKids(); | ||
|
||
// Check each child | ||
if (kids != null && kids.Count > 0) | ||
{ | ||
for (int i = 0; i < kids.Count; i++) | ||
{ | ||
// If the kid is null or content, skip it | ||
if (kids[i] == null || kids[i].GetType() != typeof(PdfStructElem)) | ||
{ | ||
continue; | ||
} | ||
|
||
// Store a copy to check children | ||
PdfStructElem elem = children.GetContext().GetPointerStructElem(children); | ||
TagTreePointer childPointer = children.GetContext().CreatePointerForStructElem(elem); | ||
matchingTags.Add(childPointer.MoveToKid(i)); | ||
} | ||
} | ||
} | ||
|
||
// Return result | ||
return matchingTags; | ||
} | ||
|
||
private void toggleKids() | ||
{ | ||
this.showKids = !this.showKids; | ||
} | ||
|
||
private string GetTitle() | ||
{ | ||
AccessibilityProperties properties = node.GetProperties(); | ||
PdfObject titleObject = node.GetContext().GetPointerStructElem(node).GetPdfObject().Get(PdfName.T); | ||
if (titleObject is not null) { | ||
return titleObject.ToString(); | ||
} | ||
return ""; | ||
} | ||
|
||
private void ShowTagInfo() | ||
{ | ||
AccessibilityProperties properties = node.GetProperties(); | ||
string actualText = properties.GetActualText(); | ||
string altText = properties.GetAlternateDescription(); | ||
string title = GetTitle(); | ||
byte[] idArray = properties.GetStructureElementId(); | ||
|
||
if (idArray is null || idArray.Length == 0) { | ||
TagInspectorService.NotifyTagSelection(null, altText, actualText, null, title); | ||
} else { | ||
TagInspectorService.NotifyTagSelection(null, altText, actualText, idArray.ToString(), title); | ||
} | ||
} | ||
|
||
private string ShowHideBtn() | ||
{ | ||
if (showKids) { | ||
return "v"; | ||
} else { | ||
return "..."; | ||
} | ||
} | ||
|
||
private string ShowChildrenClass() { | ||
// Return a hidden css class if kids are supposed to be hidden | ||
// TO-DO: This seems to run for every child when a parent changes visibility. | ||
// This is very bad for performance. | ||
if (showKids) { | ||
return ""; | ||
} else { | ||
return "hidden"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@* Copyright (C) Jonathan Shull - See license file at github.com/amytho/pdf-acc-toolset *@ | ||
<div class="top relative flex bg-slate-700 rounded-b-md p-2"> | ||
<button class="ml-auto" @onclick="CloseInspector">Close</button> | ||
</div> | ||
|
||
@if (!pdfVisible) { | ||
<p class="ml-2 mt-2 text-lg text-red-500"> | ||
You must upload a PDF before you can view the current tag tree. | ||
</p> | ||
} | ||
|
||
@if (pdfVisible) { | ||
<div class="p-4"> | ||
<Pdf_Acc_Toolset.Pages.Inspector.Nodes.TagNode node="GetTagTree()" /> | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (C) Jonathan Shull - See license file at github.com/amytho/pdf-acc-toolset | ||
using Microsoft.AspNetCore.Components; | ||
using Pdf_Acc_Toolset.Services; | ||
using Pdf_Acc_Toolset.Services.UI; | ||
|
||
|
||
namespace Pdf_Acc_Toolset.Pages.Inspector; | ||
|
||
public partial class TagInspector | ||
{ | ||
[Parameter] | ||
public EventCallback<int> OnCloseInspector { get; set; } | ||
|
||
private bool pdfVisible = false; | ||
|
||
protected override void OnInitialized() | ||
{ | ||
TagInspectorService.PdfReady += OnPdfReady; | ||
TagInspectorService.PdfClosed += OnPdfClosed; | ||
|
||
if (PdfManager.outFile != null) { | ||
pdfVisible = true; | ||
} | ||
base.OnInitialized(); | ||
} | ||
|
||
private void OnPdfReady() | ||
{ | ||
Console.WriteLine("Tag View Created"); | ||
this.pdfVisible = true; | ||
} | ||
|
||
private void OnPdfClosed(object sender, EventArgs e) | ||
{ | ||
this.pdfVisible = false; | ||
} | ||
|
||
private void CloseInspector() | ||
{ | ||
this.OnCloseInspector.InvokeAsync(); | ||
} | ||
|
||
private iText.Kernel.Pdf.Tagutils.TagTreePointer GetTagTree() | ||
{ | ||
return PdfManager.GetTagRoot(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@* Copyright (C) Jonathan Shull - See license file at github.com/amytho/pdf-acc-toolset *@ | ||
|
||
@using Pdf_Acc_Toolset.Services.UI; | ||
|
||
@if (@Tag is not null) | ||
{ | ||
<p class="text-lg font-semibold"> | ||
Tag info | ||
</p> | ||
@if (Tag.Title.Length != 0) { | ||
<p class="mt-2">Title: @Tag.Title</p> | ||
} else { | ||
<p class="mt-2">Title: None</p> | ||
} | ||
<p>Alt Text: @Tag.AltText</p> | ||
<p>Actual Text: @Tag.ActualText</p> | ||
<p>ID: @Tag.ID</p> | ||
} | ||
else | ||
{ | ||
<p class="text-lg font-semibold text-red-500"> | ||
No Tag Selected | ||
</p> | ||
<p class="font-semibold">Right-click a tag to view its properties.</p> | ||
} | ||
|
||
|
||
@code { | ||
|
||
private UserTagSelection Tag { get; set; } | ||
|
||
protected override void OnInitialized() | ||
{ | ||
TagInspectorService.TagSelected += OnTagUpdated; | ||
base.OnInitialized(); | ||
} | ||
|
||
private void OnTagUpdated(object sender, UserTagSelection tag) | ||
{ | ||
this.Tag = tag; | ||
StateHasChanged(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (C) Jonathan Shull - See license file at github.com/amytho/pdf-acc-toolset | ||
namespace Pdf_Acc_Toolset.Services.UI; | ||
|
||
public class TagInspectorService | ||
{ | ||
public static event Action PdfReady; | ||
public static EventHandler PdfClosed; // This must be an event handler since it is synchronous | ||
public static EventHandler<UserTagSelection> TagSelected { get; set; } | ||
|
||
public static void NotifyPdfReady() | ||
{ | ||
PdfReady?.Invoke(); | ||
} | ||
|
||
public static void NotifyPdfClose() | ||
{ | ||
PdfClosed?.Invoke(null, null); | ||
} | ||
|
||
public static void NotifyTagSelection(string tagType, string altText, string actualText, string id, string title) | ||
{ | ||
TagSelected.Invoke(null, new UserTagSelection(tagType, altText, actualText, id, title)); | ||
} | ||
} | ||
|
||
public class UserTagSelection(string tagType, string altText, string actualText, string id, string title) | ||
{ | ||
public string TagType = tagType ?? "None"; | ||
public string AltText = altText ?? "None"; | ||
public string ActualText = actualText ?? "None"; | ||
public string ID = id ?? "None"; | ||
public string Title = title ?? "None"; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.